]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Revert as it doesn't fix the problem completely... diving into this... sorry
[dolphin.git] / src / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
20
21 #include "dolphinview.h"
22
23 #include <QApplication>
24 #include <QClipboard>
25 #include <QKeyEvent>
26 #include <QItemSelection>
27 #include <QBoxLayout>
28 #include <QTimer>
29 #include <QScrollBar>
30
31 #include <kcolorscheme.h>
32 #include <kdirlister.h>
33 #include <kfileitemdelegate.h>
34 #include <klocale.h>
35 #include <kiconeffect.h>
36 #include <kio/netaccess.h>
37 #include <kio/renamedialog.h>
38 #include <kio/previewjob.h>
39 #include <kmimetyperesolver.h>
40 #include <konqmimedata.h>
41 #include <konq_operations.h>
42 #include <kurl.h>
43
44 #include "dolphinmodel.h"
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinsortfilterproxymodel.h"
48 #include "dolphindetailsview.h"
49 #include "dolphiniconsview.h"
50 #include "renamedialog.h"
51 #include "viewproperties.h"
52 #include "dolphinsettings.h"
53 #include "dolphin_generalsettings.h"
54 #include "dolphincategorydrawer.h"
55
56 DolphinView::DolphinView(QWidget* parent,
57 const KUrl& url,
58 KDirLister* dirLister,
59 DolphinModel* dolphinModel,
60 DolphinSortFilterProxyModel* proxyModel) :
61 QWidget(parent),
62 m_active(true),
63 m_loadingDirectory(false),
64 m_initializeColumnView(false),
65 m_mode(DolphinView::IconsView),
66 m_topLayout(0),
67 m_controller(0),
68 m_iconsView(0),
69 m_detailsView(0),
70 m_columnView(0),
71 m_fileItemDelegate(0),
72 m_dolphinModel(dolphinModel),
73 m_dirLister(dirLister),
74 m_proxyModel(proxyModel)
75 {
76 setFocusPolicy(Qt::StrongFocus);
77 m_topLayout = new QVBoxLayout(this);
78 m_topLayout->setSpacing(0);
79 m_topLayout->setMargin(0);
80
81 QClipboard* clipboard = QApplication::clipboard();
82 connect(clipboard, SIGNAL(dataChanged()),
83 this, SLOT(updateCutItems()));
84
85 connect(m_dirLister, SIGNAL(completed()),
86 this, SLOT(updateCutItems()));
87 connect(m_dirLister, SIGNAL(newItems(const QList<KFileItem>&)),
88 this, SLOT(generatePreviews(const QList<KFileItem>&)));
89
90 m_controller = new DolphinController(this);
91 m_controller->setUrl(url);
92 connect(m_controller, SIGNAL(urlChanged(const KUrl&)),
93 this, SIGNAL(urlChanged(const KUrl&)));
94 connect(m_controller, SIGNAL(requestContextMenu(const QPoint&)),
95 this, SLOT(openContextMenu(const QPoint&)));
96 connect(m_controller, SIGNAL(urlsDropped(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)),
97 this, SLOT(dropUrls(const KUrl::List&, const KUrl&, const QModelIndex&, QWidget*)));
98 connect(m_controller, SIGNAL(sortingChanged(DolphinView::Sorting)),
99 this, SLOT(updateSorting(DolphinView::Sorting)));
100 connect(m_controller, SIGNAL(sortOrderChanged(Qt::SortOrder)),
101 this, SLOT(updateSortOrder(Qt::SortOrder)));
102 connect(m_controller, SIGNAL(itemTriggered(const QModelIndex&)),
103 this, SLOT(triggerItem(const QModelIndex&)));
104 connect(m_controller, SIGNAL(activated()),
105 this, SLOT(activate()));
106 connect(m_controller, SIGNAL(itemEntered(const QModelIndex&)),
107 this, SLOT(showHoverInformation(const QModelIndex&)));
108 connect(m_controller, SIGNAL(viewportEntered()),
109 this, SLOT(clearHoverInformation()));
110
111 applyViewProperties(url);
112 m_topLayout->addWidget(itemView());
113 }
114
115 DolphinView::~DolphinView()
116 {
117 }
118
119 const KUrl& DolphinView::url() const
120 {
121 return m_controller->url();
122 }
123
124 KUrl DolphinView::rootUrl() const
125 {
126 return isColumnViewActive() ? m_dirLister->url() : url();
127 }
128
129 void DolphinView::setActive(bool active)
130 {
131 if (active == m_active) {
132 return;
133 }
134
135 m_active = active;
136
137 updateViewportColor();
138 update();
139
140 if (active) {
141 emit activated();
142 }
143 }
144
145 bool DolphinView::isActive() const
146 {
147 return m_active;
148 }
149
150 void DolphinView::setMode(Mode mode)
151 {
152 if (mode == m_mode) {
153 return; // the wished mode is already set
154 }
155
156 m_mode = mode;
157
158 if (isColumnViewActive()) {
159 // When changing the mode in the column view, it makes sense
160 // to go back to the root URL of the column view automatically.
161 // Otherwise there it would not be possible to turn off the column view
162 // without focusing the first column.
163 setUrl(m_dirLister->url());
164 m_controller->setUrl(m_dirLister->url());
165 }
166
167 const KUrl viewPropsUrl = viewPropertiesUrl();
168 ViewProperties props(viewPropsUrl);
169 props.setViewMode(m_mode);
170
171 createView();
172 startDirLister(viewPropsUrl);
173
174 emit modeChanged();
175 }
176
177 DolphinView::Mode DolphinView::mode() const
178 {
179 return m_mode;
180 }
181
182 void DolphinView::setShowPreview(bool show)
183 {
184 const KUrl viewPropsUrl = viewPropertiesUrl();
185 ViewProperties props(viewPropsUrl);
186 props.setShowPreview(show);
187
188 m_controller->setShowPreview(show);
189 emit showPreviewChanged();
190
191 startDirLister(viewPropsUrl, true);
192 }
193
194 bool DolphinView::showPreview() const
195 {
196 return m_controller->showPreview();
197 }
198
199 void DolphinView::setShowHiddenFiles(bool show)
200 {
201 if (m_dirLister->showingDotFiles() == show) {
202 return;
203 }
204
205 const KUrl viewPropsUrl = viewPropertiesUrl();
206 ViewProperties props(viewPropsUrl);
207 props.setShowHiddenFiles(show);
208
209 m_dirLister->setShowingDotFiles(show);
210 emit showHiddenFilesChanged();
211
212 startDirLister(viewPropsUrl, true);
213 }
214
215 bool DolphinView::showHiddenFiles() const
216 {
217 return m_dirLister->showingDotFiles();
218 }
219
220 void DolphinView::setCategorizedSorting(bool categorized)
221 {
222 if (!supportsCategorizedSorting() || (categorized == categorizedSorting())) {
223 return;
224 }
225
226 Q_ASSERT(m_iconsView != 0);
227
228 ViewProperties props(viewPropertiesUrl());
229 props.setCategorizedSorting(categorized);
230 props.save();
231
232 m_proxyModel->setCategorizedModel(categorized);
233 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
234
235 emit categorizedSortingChanged();
236 }
237
238 bool DolphinView::categorizedSorting() const
239 {
240 return m_proxyModel->isCategorizedModel();
241 }
242
243 bool DolphinView::supportsCategorizedSorting() const
244 {
245 return m_iconsView != 0;
246 }
247
248 void DolphinView::selectAll()
249 {
250 itemView()->selectAll();
251 }
252
253 void DolphinView::invertSelection()
254 {
255 if (isColumnViewActive()) {
256 // QAbstractItemView does not offer a virtual method invertSelection()
257 // as counterpart to QAbstractItemView::selectAll(). This makes it
258 // necessary to delegate the inverting of the selection to the
259 // column view, as only the selection of the active column should get
260 // inverted.
261 m_columnView->invertSelection();
262 } else {
263 QItemSelectionModel* selectionModel = itemView()->selectionModel();
264 const QAbstractItemModel* itemModel = selectionModel->model();
265
266 const QModelIndex topLeft = itemModel->index(0, 0);
267 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
268 itemModel->columnCount() - 1);
269
270 const QItemSelection selection(topLeft, bottomRight);
271 selectionModel->select(selection, QItemSelectionModel::Toggle);
272 }
273 }
274
275 bool DolphinView::hasSelection() const
276 {
277 return itemView()->selectionModel()->hasSelection();
278 }
279
280 void DolphinView::clearSelection()
281 {
282 itemView()->selectionModel()->clear();
283 }
284
285 QList<KFileItem> DolphinView::selectedItems() const
286 {
287 const QAbstractItemView* view = itemView();
288
289 // Our view has a selection, we will map them back to the DolphinModel
290 // and then fill the KFileItemList.
291 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
292
293 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
294 QList<KFileItem> itemList;
295
296 const QModelIndexList indexList = selection.indexes();
297 foreach (QModelIndex index, indexList) {
298 KFileItem item = m_dolphinModel->itemForIndex(index);
299 if (!item.isNull()) {
300 itemList.append(item);
301 }
302 }
303
304 return itemList;
305 }
306
307 KUrl::List DolphinView::selectedUrls() const
308 {
309 KUrl::List urls;
310 const QList<KFileItem> list = selectedItems();
311 for ( QList<KFileItem>::const_iterator it = list.begin(), end = list.end();
312 it != end; ++it ) {
313 urls.append((*it).url());
314 }
315 return urls;
316 }
317
318 KFileItem DolphinView::fileItem(const QModelIndex& index) const
319 {
320 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
321 return m_dolphinModel->itemForIndex(dolphinModelIndex);
322 }
323
324 void DolphinView::setContentsPosition(int x, int y)
325 {
326 QAbstractItemView* view = itemView();
327
328 // the ColumnView takes care itself for the horizontal scrolling
329 if (!isColumnViewActive()) {
330 view->horizontalScrollBar()->setValue(x);
331 }
332 view->verticalScrollBar()->setValue(y);
333
334 m_loadingDirectory = false;
335 }
336
337 QPoint DolphinView::contentsPosition() const
338 {
339 const int x = itemView()->horizontalScrollBar()->value();
340 const int y = itemView()->verticalScrollBar()->value();
341 return QPoint(x, y);
342 }
343
344 void DolphinView::zoomIn()
345 {
346 m_controller->triggerZoomIn();
347 }
348
349 void DolphinView::zoomOut()
350 {
351 m_controller->triggerZoomOut();
352 }
353
354 bool DolphinView::isZoomInPossible() const
355 {
356 return m_controller->isZoomInPossible();
357 }
358
359 bool DolphinView::isZoomOutPossible() const
360 {
361 return m_controller->isZoomOutPossible();
362 }
363
364 void DolphinView::setSorting(Sorting sorting)
365 {
366 if (sorting != this->sorting()) {
367 updateSorting(sorting);
368 }
369 }
370
371 DolphinView::Sorting DolphinView::sorting() const
372 {
373 return m_proxyModel->sorting();
374 }
375
376 void DolphinView::setSortOrder(Qt::SortOrder order)
377 {
378 if (sortOrder() != order) {
379 updateSortOrder(order);
380 }
381 }
382
383 Qt::SortOrder DolphinView::sortOrder() const
384 {
385 return m_proxyModel->sortOrder();
386 }
387
388 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
389 {
390 const KUrl viewPropsUrl = viewPropertiesUrl();
391 ViewProperties props(viewPropsUrl);
392 props.setAdditionalInfo(info);
393
394 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
395 m_fileItemDelegate->setAdditionalInformation(info);
396
397 emit additionalInfoChanged(info);
398 startDirLister(viewPropsUrl, true);
399 }
400
401 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
402 {
403 return m_fileItemDelegate->additionalInformation();
404 }
405
406 void DolphinView::reload()
407 {
408 setUrl(url());
409 startDirLister(url(), true);
410 }
411
412 void DolphinView::refresh()
413 {
414 createView();
415 applyViewProperties(m_controller->url());
416 reload();
417 updateViewportColor();
418 }
419
420 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
421 {
422 if (m_controller->url() == url) {
423 return;
424 }
425
426 const bool restoreColumnView = !rootUrl.isEmpty()
427 && !rootUrl.equals(url, KUrl::CompareWithoutTrailingSlash)
428 && rootUrl.isParentOf(url);
429
430 m_controller->setUrl(url); // emits urlChanged, which we forward
431
432 if (restoreColumnView) {
433 applyViewProperties(rootUrl);
434 Q_ASSERT(itemView() == m_columnView);
435 startDirLister(rootUrl);
436 m_columnView->showColumn(url);
437 } else {
438 applyViewProperties(url);
439 startDirLister(url);
440 }
441
442 itemView()->setFocus();
443
444 emit startedPathLoading(url);
445 }
446
447 void DolphinView::setUrl(const KUrl& url)
448 {
449 updateView(url, KUrl());
450 }
451
452 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
453 {
454 QWidget::mouseReleaseEvent(event);
455 setActive(true);
456 }
457 void DolphinView::activate()
458 {
459 setActive(true);
460 }
461
462 void DolphinView::triggerItem(const QModelIndex& index)
463 {
464 Q_ASSERT(index.isValid());
465
466 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
467 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
468 // items are selected by the user, hence don't trigger the
469 // item specified by 'index'
470 return;
471 }
472
473 const KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
474
475 if (item.isNull()) {
476 return;
477 }
478
479 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
480 }
481
482 void DolphinView::generatePreviews(const QList<KFileItem>& items)
483 {
484 if (m_controller->showPreview()) {
485 KIO::PreviewJob* job = KIO::filePreview(items, 128);
486 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
487 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
488 }
489 }
490
491 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
492 {
493 Q_ASSERT(!item.isNull());
494 if (item.url().directory() != m_dirLister->url().path()) {
495 // the preview job is still working on items of an older URL, hence
496 // the item is not part of the directory model anymore
497 return;
498 }
499
500 const QModelIndex idx = m_dolphinModel->indexForItem(item);
501 if (idx.isValid() && (idx.column() == 0)) {
502 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
503 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
504 KIconEffect iconEffect;
505 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
506 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
507 } else {
508 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
509 }
510 }
511 }
512
513 void DolphinView::emitSelectionChangedSignal()
514 {
515 emit selectionChanged(DolphinView::selectedItems());
516 }
517
518 void DolphinView::startDirLister(const KUrl& url, bool reload)
519 {
520 if (!url.isValid()) {
521 const QString location(url.pathOrUrl());
522 if (location.isEmpty()) {
523 emit errorMessage(i18nc("@info:status", "The location is empty."));
524 } else {
525 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
526 }
527 return;
528 }
529
530 m_cutItemsCache.clear();
531 m_loadingDirectory = true;
532
533 m_dirLister->stop();
534
535 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
536 m_initializeColumnView = false;
537
538 if (keepOldDirs) {
539 // keeping old directories is only necessary for hierarchical views
540 // like the column view
541 if (reload) {
542 // for the column view it is not enough to reload the directory lister,
543 // so this task is delegated to the column view directly
544 m_columnView->reload();
545 } else if (m_dirLister->directories().contains(url)) {
546 // The dir lister contains the directory already, so
547 // KDirLister::openUrl() may not get invoked twice.
548 m_dirLister->updateDirectory(url);
549 } else {
550 const KUrl& dirListerUrl = m_dirLister->url();
551 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
552 // The current URL is not a child of the dir lister
553 // URL. This may happen when e. g. a place has been selected
554 // and hence the view must be reset.
555 m_dirLister->openUrl(url, false, false);
556 }
557 }
558 } else {
559 m_dirLister->openUrl(url, false, reload);
560 }
561 }
562
563 KUrl DolphinView::viewPropertiesUrl() const
564 {
565 if (isColumnViewActive()) {
566 return m_dirLister->url();
567 }
568
569 return url();
570 }
571
572 void DolphinView::applyViewProperties(const KUrl& url)
573 {
574 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
575 // The column view is active, hence don't apply the view properties
576 // of sub directories (represented by columns) to the view. The
577 // view always represents the properties of the first column.
578 return;
579 }
580
581 const ViewProperties props(url);
582
583 const Mode mode = props.viewMode();
584 if (m_mode != mode) {
585 m_mode = mode;
586 createView();
587 emit modeChanged();
588
589 if (m_mode == ColumnView) {
590 // The mode has been changed to the Column View. When starting the dir
591 // lister with DolphinView::startDirLister() it is important to give a
592 // hint that the dir lister may not keep the current directory
593 // although this is the default for showing a hierarchy.
594 m_initializeColumnView = true;
595 }
596 }
597 if (itemView() == 0) {
598 createView();
599 }
600 Q_ASSERT(itemView() != 0);
601 Q_ASSERT(m_fileItemDelegate != 0);
602
603 const bool showHiddenFiles = props.showHiddenFiles();
604 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
605 m_dirLister->setShowingDotFiles(showHiddenFiles);
606 emit showHiddenFilesChanged();
607 }
608
609 const bool categorized = props.categorizedSorting();
610 if (categorized != categorizedSorting()) {
611 m_proxyModel->setCategorizedModel(categorized);
612 emit categorizedSortingChanged();
613 }
614
615 const DolphinView::Sorting sorting = props.sorting();
616 if (sorting != m_proxyModel->sorting()) {
617 m_proxyModel->setSorting(sorting);
618 emit sortingChanged(sorting);
619 }
620
621 const Qt::SortOrder sortOrder = props.sortOrder();
622 if (sortOrder != m_proxyModel->sortOrder()) {
623 m_proxyModel->setSortOrder(sortOrder);
624 emit sortOrderChanged(sortOrder);
625 }
626
627 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
628 if (info != m_fileItemDelegate->additionalInformation()) {
629 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
630 m_fileItemDelegate->setAdditionalInformation(info);
631 emit additionalInfoChanged(info);
632 }
633
634 const bool showPreview = props.showPreview();
635 if (showPreview != m_controller->showPreview()) {
636 m_controller->setShowPreview(showPreview);
637 emit showPreviewChanged();
638 }
639 }
640
641 void DolphinView::changeSelection(const QList<KFileItem>& selection)
642 {
643 clearSelection();
644 if (selection.isEmpty()) {
645 return;
646 }
647 const KUrl& baseUrl = url();
648 KUrl url;
649 QItemSelection new_selection;
650 foreach(const KFileItem& item, selection) {
651 url = item.url().upUrl();
652 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
653 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
654 new_selection.select(index, index);
655 }
656 }
657 itemView()->selectionModel()->select(new_selection,
658 QItemSelectionModel::ClearAndSelect
659 | QItemSelectionModel::Current);
660 }
661
662 void DolphinView::openContextMenu(const QPoint& pos)
663 {
664 KFileItem item;
665
666 const QModelIndex index = itemView()->indexAt(pos);
667 if (isValidNameIndex(index)) {
668 item = fileItem(index);
669 }
670
671 emit requestContextMenu(item, url());
672 }
673
674 void DolphinView::dropUrls(const KUrl::List& urls,
675 const KUrl& destPath,
676 const QModelIndex& destIndex,
677 QWidget* source)
678 {
679 KFileItem directory;
680 if (isValidNameIndex(destIndex)) {
681 KFileItem item = fileItem(destIndex);
682 Q_ASSERT(!item.isNull());
683 if (item.isDir()) {
684 // the URLs are dropped above a directory
685 directory = item;
686 }
687 }
688
689 if ((directory.isNull()) && (source == itemView())) {
690 // The dropping is done into the same viewport where
691 // the dragging has been started. Just ignore this...
692 return;
693 }
694
695 const KUrl& destination = (directory.isNull()) ?
696 destPath : directory.url();
697 dropUrls(urls, destination);
698 }
699
700 void DolphinView::dropUrls(const KUrl::List& urls,
701 const KUrl& destination)
702 {
703 emit urlsDropped(urls, destination);
704 }
705
706 void DolphinView::updateSorting(DolphinView::Sorting sorting)
707 {
708 ViewProperties props(viewPropertiesUrl());
709 props.setSorting(sorting);
710
711 m_proxyModel->setSorting(sorting);
712
713 emit sortingChanged(sorting);
714 }
715
716 void DolphinView::updateSortOrder(Qt::SortOrder order)
717 {
718 ViewProperties props(viewPropertiesUrl());
719 props.setSortOrder(order);
720
721 m_proxyModel->setSortOrder(order);
722
723 emit sortOrderChanged(order);
724 }
725
726 void DolphinView::emitContentsMoved()
727 {
728 // only emit the contents moved signal if:
729 // - no directory loading is ongoing (this would reset the contents position
730 // always to (0, 0))
731 // - if the Column View is active: the column view does an automatic
732 // positioning during the loading operation, which must be remembered
733 if (!m_loadingDirectory || isColumnViewActive()) {
734 const QPoint pos(contentsPosition());
735 emit contentsMoved(pos.x(), pos.y());
736 }
737 }
738
739 void DolphinView::updateCutItems()
740 {
741 // restore the icons of all previously selected items to the
742 // original state...
743 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
744 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
745 while (it != end) {
746 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
747 if (index.isValid()) {
748 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
749 }
750 ++it;
751 }
752 m_cutItemsCache.clear();
753
754 // ... and apply an item effect to all currently cut items
755 applyCutItemEffect();
756 }
757
758 void DolphinView::showHoverInformation(const QModelIndex& index)
759 {
760 if (hasSelection()) {
761 return;
762 }
763
764 const KFileItem item = fileItem(index);
765 if (!item.isNull()) {
766 emit requestItemInfo(item);
767 }
768 }
769
770 void DolphinView::clearHoverInformation()
771 {
772 emit requestItemInfo(KFileItem());
773 }
774
775
776 void DolphinView::createView()
777 {
778 // delete current view
779 QAbstractItemView* view = itemView();
780 if (view != 0) {
781 m_topLayout->removeWidget(view);
782 view->close();
783 view->deleteLater();
784 view = 0;
785 m_iconsView = 0;
786 m_detailsView = 0;
787 m_columnView = 0;
788 m_fileItemDelegate = 0;
789 }
790
791 Q_ASSERT(m_iconsView == 0);
792 Q_ASSERT(m_detailsView == 0);
793 Q_ASSERT(m_columnView == 0);
794
795 // ... and recreate it representing the current mode
796 switch (m_mode) {
797 case IconsView:
798 m_iconsView = new DolphinIconsView(this, m_controller);
799 m_iconsView->setCategoryDrawer(new DolphinCategoryDrawer());
800 view = m_iconsView;
801 break;
802
803 case DetailsView:
804 m_detailsView = new DolphinDetailsView(this, m_controller);
805 view = m_detailsView;
806 break;
807
808 case ColumnView:
809 m_columnView = new DolphinColumnView(this, m_controller);
810 view = m_columnView;
811 break;
812 }
813
814 Q_ASSERT(view != 0);
815
816 m_fileItemDelegate = new KFileItemDelegate(view);
817 view->setItemDelegate(m_fileItemDelegate);
818
819 view->setModel(m_proxyModel);
820 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
821
822 new KMimeTypeResolver(view, m_dolphinModel);
823 m_topLayout->insertWidget(1, view);
824
825 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
826 this, SLOT(emitSelectionChangedSignal()));
827 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
828 this, SLOT(emitContentsMoved()));
829 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
830 this, SLOT(emitContentsMoved()));
831 view->setFocus();
832 }
833
834 QAbstractItemView* DolphinView::itemView() const
835 {
836 if (m_detailsView != 0) {
837 return m_detailsView;
838 } else if (m_columnView != 0) {
839 return m_columnView;
840 }
841
842 return m_iconsView;
843 }
844
845 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
846 {
847 return index.isValid() && (index.column() == DolphinModel::Name);
848 }
849
850 bool DolphinView::isCutItem(const KFileItem& item) const
851 {
852 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
853 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
854
855 const KUrl& itemUrl = item.url();
856 KUrl::List::const_iterator it = cutUrls.begin();
857 const KUrl::List::const_iterator end = cutUrls.end();
858 while (it != end) {
859 if (*it == itemUrl) {
860 return true;
861 }
862 ++it;
863 }
864
865 return false;
866 }
867
868 void DolphinView::applyCutItemEffect()
869 {
870 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
871 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
872 return;
873 }
874
875 KFileItemList items(m_dirLister->items());
876 KFileItemList::const_iterator it = items.begin();
877 const KFileItemList::const_iterator end = items.end();
878 while (it != end) {
879 KFileItem* item = *it;
880 if (isCutItem(*item)) {
881 const QModelIndex index = m_dolphinModel->indexForItem(*item);
882 // Huh? the item is already known
883 //const KFileItem item = m_dolphinModel->itemForIndex(index);
884 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
885 if (value.type() == QVariant::Icon) {
886 const QIcon icon(qvariant_cast<QIcon>(value));
887 QPixmap pixmap = icon.pixmap(128, 128);
888
889 // remember current pixmap for the item to be able
890 // to restore it when other items get cut
891 CutItem cutItem;
892 cutItem.url = item->url();
893 cutItem.pixmap = pixmap;
894 m_cutItemsCache.append(cutItem);
895
896 // apply icon effect to the cut item
897 KIconEffect iconEffect;
898 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
899 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
900 }
901 }
902 ++it;
903 }
904 }
905
906 void DolphinView::updateViewportColor()
907 {
908 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
909 if (m_active) {
910 emit urlChanged(url()); // Hmm, this is a hack; the url hasn't really changed.
911 emit selectionChanged(selectedItems());
912 } else {
913 color.setAlpha(0);
914 }
915
916 QWidget* viewport = itemView()->viewport();
917 QPalette palette;
918 palette.setColor(viewport->backgroundRole(), color);
919 viewport->setPalette(palette);
920 }
921
922 #include "dolphinview.moc"