]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
Fix the reproducible problem after the fix:
[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 (categorized == categorizedSorting()) {
223 return;
224 }
225
226 if (!categorized && !supportsCategorizedSorting())
227 {
228 m_proxyModel->setCategorizedModel(categorized);
229 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
230
231 emit categorizedSortingChanged();
232
233 return;
234 }
235
236 Q_ASSERT(m_iconsView != 0);
237
238 ViewProperties props(viewPropertiesUrl());
239 props.setCategorizedSorting(categorized);
240 props.save();
241
242 m_proxyModel->setCategorizedModel(categorized);
243 m_proxyModel->sort(m_proxyModel->sortColumn(), m_proxyModel->sortOrder());
244
245 emit categorizedSortingChanged();
246 }
247
248 bool DolphinView::categorizedSorting() const
249 {
250 return m_proxyModel->isCategorizedModel();
251 }
252
253 bool DolphinView::supportsCategorizedSorting() const
254 {
255 return m_iconsView != 0;
256 }
257
258 void DolphinView::selectAll()
259 {
260 itemView()->selectAll();
261 }
262
263 void DolphinView::invertSelection()
264 {
265 if (isColumnViewActive()) {
266 // QAbstractItemView does not offer a virtual method invertSelection()
267 // as counterpart to QAbstractItemView::selectAll(). This makes it
268 // necessary to delegate the inverting of the selection to the
269 // column view, as only the selection of the active column should get
270 // inverted.
271 m_columnView->invertSelection();
272 } else {
273 QItemSelectionModel* selectionModel = itemView()->selectionModel();
274 const QAbstractItemModel* itemModel = selectionModel->model();
275
276 const QModelIndex topLeft = itemModel->index(0, 0);
277 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
278 itemModel->columnCount() - 1);
279
280 const QItemSelection selection(topLeft, bottomRight);
281 selectionModel->select(selection, QItemSelectionModel::Toggle);
282 }
283 }
284
285 bool DolphinView::hasSelection() const
286 {
287 return itemView()->selectionModel()->hasSelection();
288 }
289
290 void DolphinView::clearSelection()
291 {
292 itemView()->selectionModel()->clear();
293 }
294
295 QList<KFileItem> DolphinView::selectedItems() const
296 {
297 const QAbstractItemView* view = itemView();
298
299 // Our view has a selection, we will map them back to the DolphinModel
300 // and then fill the KFileItemList.
301 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
302
303 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
304 QList<KFileItem> itemList;
305
306 const QModelIndexList indexList = selection.indexes();
307 foreach (QModelIndex index, indexList) {
308 KFileItem item = m_dolphinModel->itemForIndex(index);
309 if (!item.isNull()) {
310 itemList.append(item);
311 }
312 }
313
314 return itemList;
315 }
316
317 KUrl::List DolphinView::selectedUrls() const
318 {
319 KUrl::List urls;
320 const QList<KFileItem> list = selectedItems();
321 for ( QList<KFileItem>::const_iterator it = list.begin(), end = list.end();
322 it != end; ++it ) {
323 urls.append((*it).url());
324 }
325 return urls;
326 }
327
328 KFileItem DolphinView::fileItem(const QModelIndex& index) const
329 {
330 const QModelIndex dolphinModelIndex = m_proxyModel->mapToSource(index);
331 return m_dolphinModel->itemForIndex(dolphinModelIndex);
332 }
333
334 void DolphinView::setContentsPosition(int x, int y)
335 {
336 QAbstractItemView* view = itemView();
337
338 // the ColumnView takes care itself for the horizontal scrolling
339 if (!isColumnViewActive()) {
340 view->horizontalScrollBar()->setValue(x);
341 }
342 view->verticalScrollBar()->setValue(y);
343
344 m_loadingDirectory = false;
345 }
346
347 QPoint DolphinView::contentsPosition() const
348 {
349 const int x = itemView()->horizontalScrollBar()->value();
350 const int y = itemView()->verticalScrollBar()->value();
351 return QPoint(x, y);
352 }
353
354 void DolphinView::zoomIn()
355 {
356 m_controller->triggerZoomIn();
357 }
358
359 void DolphinView::zoomOut()
360 {
361 m_controller->triggerZoomOut();
362 }
363
364 bool DolphinView::isZoomInPossible() const
365 {
366 return m_controller->isZoomInPossible();
367 }
368
369 bool DolphinView::isZoomOutPossible() const
370 {
371 return m_controller->isZoomOutPossible();
372 }
373
374 void DolphinView::setSorting(Sorting sorting)
375 {
376 if (sorting != this->sorting()) {
377 updateSorting(sorting);
378 }
379 }
380
381 DolphinView::Sorting DolphinView::sorting() const
382 {
383 return m_proxyModel->sorting();
384 }
385
386 void DolphinView::setSortOrder(Qt::SortOrder order)
387 {
388 if (sortOrder() != order) {
389 updateSortOrder(order);
390 }
391 }
392
393 Qt::SortOrder DolphinView::sortOrder() const
394 {
395 return m_proxyModel->sortOrder();
396 }
397
398 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
399 {
400 const KUrl viewPropsUrl = viewPropertiesUrl();
401 ViewProperties props(viewPropsUrl);
402 props.setAdditionalInfo(info);
403
404 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
405 m_fileItemDelegate->setAdditionalInformation(info);
406
407 emit additionalInfoChanged(info);
408 startDirLister(viewPropsUrl, true);
409 }
410
411 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
412 {
413 return m_fileItemDelegate->additionalInformation();
414 }
415
416 void DolphinView::reload()
417 {
418 setUrl(url());
419 startDirLister(url(), true);
420 }
421
422 void DolphinView::refresh()
423 {
424 createView();
425 applyViewProperties(m_controller->url());
426 reload();
427 updateViewportColor();
428 }
429
430 void DolphinView::updateView(const KUrl& url, const KUrl& rootUrl)
431 {
432 if (m_controller->url() == url) {
433 return;
434 }
435
436 const bool restoreColumnView = !rootUrl.isEmpty()
437 && !rootUrl.equals(url, KUrl::CompareWithoutTrailingSlash)
438 && rootUrl.isParentOf(url);
439
440 m_controller->setUrl(url); // emits urlChanged, which we forward
441
442 if (restoreColumnView) {
443 applyViewProperties(rootUrl);
444 Q_ASSERT(itemView() == m_columnView);
445 startDirLister(rootUrl);
446 m_columnView->showColumn(url);
447 } else {
448 applyViewProperties(url);
449 startDirLister(url);
450 }
451
452 itemView()->setFocus();
453
454 emit startedPathLoading(url);
455 }
456
457 void DolphinView::setUrl(const KUrl& url)
458 {
459 updateView(url, KUrl());
460 }
461
462 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
463 {
464 QWidget::mouseReleaseEvent(event);
465 setActive(true);
466 }
467 void DolphinView::activate()
468 {
469 setActive(true);
470 }
471
472 void DolphinView::triggerItem(const QModelIndex& index)
473 {
474 Q_ASSERT(index.isValid());
475
476 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
477 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
478 // items are selected by the user, hence don't trigger the
479 // item specified by 'index'
480 return;
481 }
482
483 const KFileItem item = m_dolphinModel->itemForIndex(m_proxyModel->mapToSource(index));
484
485 if (item.isNull()) {
486 return;
487 }
488
489 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
490 }
491
492 void DolphinView::generatePreviews(const QList<KFileItem>& items)
493 {
494 if (m_controller->showPreview()) {
495 KIO::PreviewJob* job = KIO::filePreview(items, 128);
496 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
497 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
498 }
499 }
500
501 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
502 {
503 Q_ASSERT(!item.isNull());
504 if (item.url().directory() != m_dirLister->url().path()) {
505 // the preview job is still working on items of an older URL, hence
506 // the item is not part of the directory model anymore
507 return;
508 }
509
510 const QModelIndex idx = m_dolphinModel->indexForItem(item);
511 if (idx.isValid() && (idx.column() == 0)) {
512 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
513 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
514 KIconEffect iconEffect;
515 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
516 m_dolphinModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
517 } else {
518 m_dolphinModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
519 }
520 }
521 }
522
523 void DolphinView::emitSelectionChangedSignal()
524 {
525 emit selectionChanged(DolphinView::selectedItems());
526 }
527
528 void DolphinView::startDirLister(const KUrl& url, bool reload)
529 {
530 if (!url.isValid()) {
531 const QString location(url.pathOrUrl());
532 if (location.isEmpty()) {
533 emit errorMessage(i18nc("@info:status", "The location is empty."));
534 } else {
535 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
536 }
537 return;
538 }
539
540 m_cutItemsCache.clear();
541 m_loadingDirectory = true;
542
543 m_dirLister->stop();
544
545 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
546 m_initializeColumnView = false;
547
548 if (keepOldDirs) {
549 // keeping old directories is only necessary for hierarchical views
550 // like the column view
551 if (reload) {
552 // for the column view it is not enough to reload the directory lister,
553 // so this task is delegated to the column view directly
554 m_columnView->reload();
555 } else if (m_dirLister->directories().contains(url)) {
556 // The dir lister contains the directory already, so
557 // KDirLister::openUrl() may not get invoked twice.
558 m_dirLister->updateDirectory(url);
559 } else {
560 const KUrl& dirListerUrl = m_dirLister->url();
561 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
562 // The current URL is not a child of the dir lister
563 // URL. This may happen when e. g. a place has been selected
564 // and hence the view must be reset.
565 m_dirLister->openUrl(url, false, false);
566 }
567 }
568 } else {
569 m_dirLister->openUrl(url, false, reload);
570 }
571 }
572
573 KUrl DolphinView::viewPropertiesUrl() const
574 {
575 if (isColumnViewActive()) {
576 return m_dirLister->url();
577 }
578
579 return url();
580 }
581
582 void DolphinView::applyViewProperties(const KUrl& url)
583 {
584 if (isColumnViewActive() && m_dirLister->url().isParentOf(url)) {
585 // The column view is active, hence don't apply the view properties
586 // of sub directories (represented by columns) to the view. The
587 // view always represents the properties of the first column.
588 return;
589 }
590
591 const ViewProperties props(url);
592
593 const Mode mode = props.viewMode();
594 if (m_mode != mode) {
595 m_mode = mode;
596 createView();
597 emit modeChanged();
598
599 if (m_mode == ColumnView) {
600 // The mode has been changed to the Column View. When starting the dir
601 // lister with DolphinView::startDirLister() it is important to give a
602 // hint that the dir lister may not keep the current directory
603 // although this is the default for showing a hierarchy.
604 m_initializeColumnView = true;
605 }
606 }
607 if (itemView() == 0) {
608 createView();
609 }
610 Q_ASSERT(itemView() != 0);
611 Q_ASSERT(m_fileItemDelegate != 0);
612
613 const bool showHiddenFiles = props.showHiddenFiles();
614 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
615 m_dirLister->setShowingDotFiles(showHiddenFiles);
616 emit showHiddenFilesChanged();
617 }
618
619 const bool categorized = props.categorizedSorting();
620 if (categorized != categorizedSorting()) {
621 m_proxyModel->setCategorizedModel(categorized);
622 emit categorizedSortingChanged();
623 }
624
625 const DolphinView::Sorting sorting = props.sorting();
626 if (sorting != m_proxyModel->sorting()) {
627 m_proxyModel->setSorting(sorting);
628 emit sortingChanged(sorting);
629 }
630
631 const Qt::SortOrder sortOrder = props.sortOrder();
632 if (sortOrder != m_proxyModel->sortOrder()) {
633 m_proxyModel->setSortOrder(sortOrder);
634 emit sortOrderChanged(sortOrder);
635 }
636
637 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
638 if (info != m_fileItemDelegate->additionalInformation()) {
639 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
640 m_fileItemDelegate->setAdditionalInformation(info);
641 emit additionalInfoChanged(info);
642 }
643
644 const bool showPreview = props.showPreview();
645 if (showPreview != m_controller->showPreview()) {
646 m_controller->setShowPreview(showPreview);
647 emit showPreviewChanged();
648 }
649 }
650
651 void DolphinView::changeSelection(const QList<KFileItem>& selection)
652 {
653 clearSelection();
654 if (selection.isEmpty()) {
655 return;
656 }
657 const KUrl& baseUrl = url();
658 KUrl url;
659 QItemSelection new_selection;
660 foreach(const KFileItem& item, selection) {
661 url = item.url().upUrl();
662 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
663 QModelIndex index = m_proxyModel->mapFromSource(m_dolphinModel->indexForItem(item));
664 new_selection.select(index, index);
665 }
666 }
667 itemView()->selectionModel()->select(new_selection,
668 QItemSelectionModel::ClearAndSelect
669 | QItemSelectionModel::Current);
670 }
671
672 void DolphinView::openContextMenu(const QPoint& pos)
673 {
674 KFileItem item;
675
676 const QModelIndex index = itemView()->indexAt(pos);
677 if (isValidNameIndex(index)) {
678 item = fileItem(index);
679 }
680
681 emit requestContextMenu(item, url());
682 }
683
684 void DolphinView::dropUrls(const KUrl::List& urls,
685 const KUrl& destPath,
686 const QModelIndex& destIndex,
687 QWidget* source)
688 {
689 KFileItem directory;
690 if (isValidNameIndex(destIndex)) {
691 KFileItem item = fileItem(destIndex);
692 Q_ASSERT(!item.isNull());
693 if (item.isDir()) {
694 // the URLs are dropped above a directory
695 directory = item;
696 }
697 }
698
699 if ((directory.isNull()) && (source == itemView())) {
700 // The dropping is done into the same viewport where
701 // the dragging has been started. Just ignore this...
702 return;
703 }
704
705 const KUrl& destination = (directory.isNull()) ?
706 destPath : directory.url();
707 dropUrls(urls, destination);
708 }
709
710 void DolphinView::dropUrls(const KUrl::List& urls,
711 const KUrl& destination)
712 {
713 emit urlsDropped(urls, destination);
714 }
715
716 void DolphinView::updateSorting(DolphinView::Sorting sorting)
717 {
718 ViewProperties props(viewPropertiesUrl());
719 props.setSorting(sorting);
720
721 m_proxyModel->setSorting(sorting);
722
723 emit sortingChanged(sorting);
724 }
725
726 void DolphinView::updateSortOrder(Qt::SortOrder order)
727 {
728 ViewProperties props(viewPropertiesUrl());
729 props.setSortOrder(order);
730
731 m_proxyModel->setSortOrder(order);
732
733 emit sortOrderChanged(order);
734 }
735
736 void DolphinView::emitContentsMoved()
737 {
738 // only emit the contents moved signal if:
739 // - no directory loading is ongoing (this would reset the contents position
740 // always to (0, 0))
741 // - if the Column View is active: the column view does an automatic
742 // positioning during the loading operation, which must be remembered
743 if (!m_loadingDirectory || isColumnViewActive()) {
744 const QPoint pos(contentsPosition());
745 emit contentsMoved(pos.x(), pos.y());
746 }
747 }
748
749 void DolphinView::updateCutItems()
750 {
751 // restore the icons of all previously selected items to the
752 // original state...
753 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
754 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
755 while (it != end) {
756 const QModelIndex index = m_dolphinModel->indexForUrl((*it).url);
757 if (index.isValid()) {
758 m_dolphinModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
759 }
760 ++it;
761 }
762 m_cutItemsCache.clear();
763
764 // ... and apply an item effect to all currently cut items
765 applyCutItemEffect();
766 }
767
768 void DolphinView::showHoverInformation(const QModelIndex& index)
769 {
770 if (hasSelection()) {
771 return;
772 }
773
774 const KFileItem item = fileItem(index);
775 if (!item.isNull()) {
776 emit requestItemInfo(item);
777 }
778 }
779
780 void DolphinView::clearHoverInformation()
781 {
782 emit requestItemInfo(KFileItem());
783 }
784
785
786 void DolphinView::createView()
787 {
788 // delete current view
789 QAbstractItemView* view = itemView();
790 if (view != 0) {
791 m_topLayout->removeWidget(view);
792 view->close();
793 view->deleteLater();
794 view = 0;
795 m_iconsView = 0;
796 m_detailsView = 0;
797 m_columnView = 0;
798 m_fileItemDelegate = 0;
799 }
800
801 Q_ASSERT(m_iconsView == 0);
802 Q_ASSERT(m_detailsView == 0);
803 Q_ASSERT(m_columnView == 0);
804
805 // ... and recreate it representing the current mode
806 switch (m_mode) {
807 case IconsView: {
808 const KUrl viewPropsUrl = viewPropertiesUrl();
809 const ViewProperties props(viewPropsUrl);
810
811 m_iconsView = new DolphinIconsView(this, m_controller);
812 m_iconsView->setCategoryDrawer(new DolphinCategoryDrawer());
813 view = m_iconsView;
814 setCategorizedSorting(props.categorizedSorting());
815 break;
816 }
817
818 case DetailsView:
819 m_detailsView = new DolphinDetailsView(this, m_controller);
820 view = m_detailsView;
821 setCategorizedSorting(false);
822 break;
823
824 case ColumnView:
825 m_columnView = new DolphinColumnView(this, m_controller);
826 view = m_columnView;
827 setCategorizedSorting(false);
828 break;
829 }
830
831 Q_ASSERT(view != 0);
832
833 m_fileItemDelegate = new KFileItemDelegate(view);
834 view->setItemDelegate(m_fileItemDelegate);
835
836 view->setModel(m_proxyModel);
837 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
838
839 new KMimeTypeResolver(view, m_dolphinModel);
840 m_topLayout->insertWidget(1, view);
841
842 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
843 this, SLOT(emitSelectionChangedSignal()));
844 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
845 this, SLOT(emitContentsMoved()));
846 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
847 this, SLOT(emitContentsMoved()));
848 view->setFocus();
849 }
850
851 QAbstractItemView* DolphinView::itemView() const
852 {
853 if (m_detailsView != 0) {
854 return m_detailsView;
855 } else if (m_columnView != 0) {
856 return m_columnView;
857 }
858
859 return m_iconsView;
860 }
861
862 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
863 {
864 return index.isValid() && (index.column() == DolphinModel::Name);
865 }
866
867 bool DolphinView::isCutItem(const KFileItem& item) const
868 {
869 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
870 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
871
872 const KUrl& itemUrl = item.url();
873 KUrl::List::const_iterator it = cutUrls.begin();
874 const KUrl::List::const_iterator end = cutUrls.end();
875 while (it != end) {
876 if (*it == itemUrl) {
877 return true;
878 }
879 ++it;
880 }
881
882 return false;
883 }
884
885 void DolphinView::applyCutItemEffect()
886 {
887 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
888 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
889 return;
890 }
891
892 KFileItemList items(m_dirLister->items());
893 KFileItemList::const_iterator it = items.begin();
894 const KFileItemList::const_iterator end = items.end();
895 while (it != end) {
896 KFileItem* item = *it;
897 if (isCutItem(*item)) {
898 const QModelIndex index = m_dolphinModel->indexForItem(*item);
899 // Huh? the item is already known
900 //const KFileItem item = m_dolphinModel->itemForIndex(index);
901 const QVariant value = m_dolphinModel->data(index, Qt::DecorationRole);
902 if (value.type() == QVariant::Icon) {
903 const QIcon icon(qvariant_cast<QIcon>(value));
904 QPixmap pixmap = icon.pixmap(128, 128);
905
906 // remember current pixmap for the item to be able
907 // to restore it when other items get cut
908 CutItem cutItem;
909 cutItem.url = item->url();
910 cutItem.pixmap = pixmap;
911 m_cutItemsCache.append(cutItem);
912
913 // apply icon effect to the cut item
914 KIconEffect iconEffect;
915 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
916 m_dolphinModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
917 }
918 }
919 ++it;
920 }
921 }
922
923 void DolphinView::updateViewportColor()
924 {
925 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
926 if (m_active) {
927 emit urlChanged(url()); // Hmm, this is a hack; the url hasn't really changed.
928 emit selectionChanged(selectedItems());
929 } else {
930 color.setAlpha(0);
931 }
932
933 QWidget* viewport = itemView()->viewport();
934 QPalette palette;
935 palette.setColor(viewport->backgroundRole(), color);
936 viewport->setPalette(palette);
937 }
938
939 #include "dolphinview.moc"