]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
simplify + add todo (early commit to make room for the next kdebase-wide commit)
[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 <kdirmodel.h>
33 #include <kdirlister.h>
34 #include <kfileitemdelegate.h>
35 #include <klocale.h>
36 #include <kiconeffect.h>
37 #include <kio/netaccess.h>
38 #include <kio/renamedialog.h>
39 #include <kio/previewjob.h>
40 #include <kmimetyperesolver.h>
41 #include <konqmimedata.h>
42 #include <konq_operations.h>
43 #include <kurl.h>
44
45 #include "dolphincolumnview.h"
46 #include "dolphincontroller.h"
47 #include "dolphinsortfilterproxymodel.h"
48 #include "dolphindetailsview.h"
49 #include "dolphiniconsview.h"
50 #include "dolphinitemcategorizer.h"
51 #include "renamedialog.h"
52 #include "viewproperties.h"
53 #include "dolphinsettings.h"
54 #include "dolphin_generalsettings.h"
55
56 DolphinView::DolphinView(QWidget* parent,
57 const KUrl& url,
58 KDirLister* dirLister,
59 KDirModel* dirModel,
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_dirModel(dirModel),
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 KFileItemList&)),
88 this, SLOT(generatePreviews(const KFileItemList&)));
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 QModelIndex&, QWidget*)),
97 this, SLOT(dropUrls(const KUrl::List&, 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 QColor color = KColorScheme(KColorScheme::View).background();
138 if (active) {
139 emit urlChanged(url());
140 emit selectionChanged(selectedItems());
141 } else {
142 color.setAlpha(0);
143 }
144
145 QWidget* viewport = itemView()->viewport();
146 QPalette palette;
147 palette.setColor(viewport->backgroundRole(), color);
148 viewport->setPalette(palette);
149
150 update();
151
152 if (active) {
153 emit activated();
154 }
155 }
156
157 bool DolphinView::isActive() const
158 {
159 return m_active;
160 }
161
162 void DolphinView::setMode(Mode mode)
163 {
164 if (mode == m_mode) {
165 return; // the wished mode is already set
166 }
167
168 m_mode = mode;
169
170 if (isColumnViewActive()) {
171 // When changing the mode in the column view, it makes sense
172 // to go back to the root URL of the column view automatically.
173 // Otherwise there it would not be possible to turn off the column view
174 // without focusing the first column.
175 setUrl(m_dirLister->url());
176 m_controller->setUrl(m_dirLister->url());
177 }
178
179 ViewProperties props(url());
180 props.setViewMode(m_mode);
181
182 createView();
183 startDirLister(url());
184
185 emit modeChanged();
186 }
187
188 DolphinView::Mode DolphinView::mode() const
189 {
190 return m_mode;
191 }
192
193 void DolphinView::setShowPreview(bool show)
194 {
195 ViewProperties props(url());
196 props.setShowPreview(show);
197
198 m_controller->setShowPreview(show);
199 emit showPreviewChanged();
200
201 startDirLister(url(), true);
202 }
203
204 bool DolphinView::showPreview() const
205 {
206 return m_controller->showPreview();
207 }
208
209 void DolphinView::setShowHiddenFiles(bool show)
210 {
211 if (m_dirLister->showingDotFiles() == show) {
212 return;
213 }
214
215 ViewProperties props(url());
216 props.setShowHiddenFiles(show);
217
218 m_dirLister->setShowingDotFiles(show);
219 emit showHiddenFilesChanged();
220
221 startDirLister(url(), true);
222 }
223
224 bool DolphinView::showHiddenFiles() const
225 {
226 return m_dirLister->showingDotFiles();
227 }
228
229 void DolphinView::setCategorizedSorting(bool categorized)
230 {
231 if (!supportsCategorizedSorting() || (categorized == categorizedSorting())) {
232 return;
233 }
234
235 Q_ASSERT(m_iconsView != 0);
236 if (categorized) {
237 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
238 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
239 } else {
240 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
241 m_iconsView->setItemCategorizer(0);
242 delete categorizer;
243 }
244
245 ViewProperties props(url());
246 props.setCategorizedSorting(categorized);
247 props.save();
248
249 emit categorizedSortingChanged();
250 }
251
252 bool DolphinView::categorizedSorting() const
253 {
254 if (!supportsCategorizedSorting()) {
255 return false;
256 }
257
258 Q_ASSERT(m_iconsView != 0);
259 return m_iconsView->itemCategorizer() != 0;
260 }
261
262 bool DolphinView::supportsCategorizedSorting() const
263 {
264 return m_iconsView != 0;
265 }
266
267 void DolphinView::selectAll()
268 {
269 selectAll(QItemSelectionModel::Select);
270 }
271
272 void DolphinView::invertSelection()
273 {
274 selectAll(QItemSelectionModel::Toggle);
275 }
276
277 bool DolphinView::hasSelection() const
278 {
279 return itemView()->selectionModel()->hasSelection();
280 }
281
282 void DolphinView::clearSelection()
283 {
284 itemView()->selectionModel()->clear();
285 }
286
287 KFileItemList DolphinView::selectedItems() const
288 {
289 const QAbstractItemView* view = itemView();
290
291 // Our view has a selection, we will map them back to the DirModel
292 // and then fill the KFileItemList.
293 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
294
295 const QItemSelection selection = m_proxyModel->mapSelectionToSource(view->selectionModel()->selection());
296 KFileItemList itemList;
297
298 const QModelIndexList indexList = selection.indexes();
299 QModelIndexList::const_iterator end = indexList.end();
300 for (QModelIndexList::const_iterator it = indexList.begin(); it != end; ++it) {
301 Q_ASSERT((*it).isValid());
302
303 KFileItem* item = m_dirModel->itemForIndex(*it);
304 if (item != 0) {
305 itemList.append(item);
306 }
307 }
308
309 return itemList;
310 }
311
312 KUrl::List DolphinView::selectedUrls() const
313 {
314 KUrl::List urls;
315
316 const KFileItemList list = selectedItems();
317 KFileItemList::const_iterator it = list.begin();
318 const KFileItemList::const_iterator end = list.end();
319 while (it != end) {
320 KFileItem* item = *it;
321 urls.append(item->url());
322 ++it;
323 }
324
325 return urls;
326 }
327
328 KFileItem* DolphinView::fileItem(const QModelIndex index) const
329 {
330 const QModelIndex dirModelIndex = m_proxyModel->mapToSource(index);
331 return m_dirModel->itemForIndex(dirModelIndex);
332 }
333
334 void DolphinView::setContentsPosition(int x, int y)
335 {
336 QAbstractItemView* view = itemView();
337 view->horizontalScrollBar()->setValue(x);
338 view->verticalScrollBar()->setValue(y);
339
340 m_loadingDirectory = false;
341 }
342
343 QPoint DolphinView::contentsPosition() const
344 {
345 const int x = itemView()->horizontalScrollBar()->value();
346 const int y = itemView()->verticalScrollBar()->value();
347 return QPoint(x, y);
348 }
349
350 void DolphinView::zoomIn()
351 {
352 m_controller->triggerZoomIn();
353 }
354
355 void DolphinView::zoomOut()
356 {
357 m_controller->triggerZoomOut();
358 }
359
360 bool DolphinView::isZoomInPossible() const
361 {
362 return m_controller->isZoomInPossible();
363 }
364
365 bool DolphinView::isZoomOutPossible() const
366 {
367 return m_controller->isZoomOutPossible();
368 }
369
370 void DolphinView::setSorting(Sorting sorting)
371 {
372 if (sorting != this->sorting()) {
373 updateSorting(sorting);
374 }
375 }
376
377 DolphinView::Sorting DolphinView::sorting() const
378 {
379 return m_proxyModel->sorting();
380 }
381
382 void DolphinView::setSortOrder(Qt::SortOrder order)
383 {
384 if (sortOrder() != order) {
385 updateSortOrder(order);
386 }
387 }
388
389 Qt::SortOrder DolphinView::sortOrder() const
390 {
391 return m_proxyModel->sortOrder();
392 }
393
394 void DolphinView::setAdditionalInfo(KFileItemDelegate::AdditionalInformation info)
395 {
396 ViewProperties props(url());
397 props.setAdditionalInfo(info);
398
399 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
400 m_fileItemDelegate->setAdditionalInformation(info);
401
402 emit additionalInfoChanged(info);
403 startDirLister(url(), true);
404 }
405
406 KFileItemDelegate::AdditionalInformation DolphinView::additionalInfo() const
407 {
408 return m_fileItemDelegate->additionalInformation();
409 }
410
411 void DolphinView::reload()
412 {
413 setUrl(url());
414 startDirLister(url(), true);
415 }
416
417 void DolphinView::refresh()
418 {
419 createView();
420 applyViewProperties(m_controller->url());
421 reload();
422 }
423
424 void DolphinView::setUrl(const KUrl& url)
425 {
426 if (m_controller->url() == url) {
427 return;
428 }
429
430 m_controller->setUrl(url);
431
432 applyViewProperties(url);
433
434 startDirLister(url);
435 emit urlChanged(url);
436 }
437
438 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
439 {
440 QWidget::mouseReleaseEvent(event);
441 setActive(true);;
442 }
443 void DolphinView::activate()
444 {
445 setActive(true);
446 }
447
448 void DolphinView::triggerItem(const QModelIndex& index)
449 {
450 if (!isValidNameIndex(index)) {
451 clearSelection();
452 showHoverInformation(index);
453 return;
454 }
455
456 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
457 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
458 // items are selected by the user, hence don't trigger the
459 // item specified by 'index'
460 return;
461 }
462
463 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
464 if (item == 0) {
465 return;
466 }
467
468 // The stuff below should be moved to ViewContainer and be just a signal?
469
470 // Prefer the local path over the URL.
471 bool isLocal;
472 KUrl url = item->mostLocalUrl(isLocal);
473
474 if (item->isDir()) {
475 setUrl(url);
476 } else if (item->isFile()) {
477 // allow to browse through ZIP and tar files
478 KMimeType::Ptr mime = item->mimeTypePtr();
479 if (mime->is("application/zip")) {
480 url.setProtocol("zip");
481 setUrl(url);
482 } else if (mime->is("application/x-tar") ||
483 mime->is("application/x-tarz") ||
484 mime->is("application/x-bzip-compressed-tar") ||
485 mime->is("application/x-compressed-tar") ||
486 mime->is("application/x-tzo")) {
487 url.setProtocol("tar");
488 setUrl(url);
489 } else {
490 item->run();
491 }
492 } else {
493 item->run();
494 }
495 }
496
497 void DolphinView::generatePreviews(const KFileItemList& items)
498 {
499 if (m_controller->showPreview()) {
500
501 // Must turn QList<KFileItem *> to QList<KFileItem>...
502 QList<KFileItem> itemsToPreview;
503 foreach( KFileItem* it, items )
504 itemsToPreview.append( *it );
505
506 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
507 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
508 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
509 }
510 }
511
512 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
513 {
514 Q_ASSERT(!item.isNull());
515 if (item.url().directory() != m_dirLister->url().path()) {
516 // the preview job is still working on items of an older URL, hence
517 // the item is not part of the directory model anymore
518 return;
519 }
520
521 const QModelIndex idx = m_dirModel->indexForItem(item);
522 if (idx.isValid() && (idx.column() == 0)) {
523 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
524 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
525 KIconEffect iconEffect;
526 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
527 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
528 } else {
529 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
530 }
531 }
532 }
533
534 void DolphinView::emitSelectionChangedSignal()
535 {
536 emit selectionChanged(DolphinView::selectedItems());
537 }
538
539 void DolphinView::startDirLister(const KUrl& url, bool reload)
540 {
541 if (!url.isValid()) {
542 const QString location(url.pathOrUrl());
543 if (location.isEmpty()) {
544 emit errorMessage(i18n("The location is empty."));
545 } else {
546 emit errorMessage(i18n("The location '%1' is invalid.", location));
547 }
548 return;
549 }
550
551 m_cutItemsCache.clear();
552 m_loadingDirectory = true;
553
554 m_dirLister->stop();
555
556 bool openDir = true;
557 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
558 m_initializeColumnView = false;
559
560 if (keepOldDirs) {
561 if (reload) {
562 keepOldDirs = false;
563
564 const KUrl& dirListerUrl = m_dirLister->url();
565 if (dirListerUrl.isValid()) {
566 const KUrl::List dirs = m_dirLister->directories();
567 KUrl url;
568 foreach(url, dirs) {
569 m_dirLister->updateDirectory(url);
570 }
571 openDir = false;
572 }
573 } else if (m_dirLister->directories().contains(url)) {
574 // The dir lister contains the directory already, so
575 // KDirLister::openUrl() may not been invoked twice.
576 m_dirLister->updateDirectory(url);
577 openDir = false;
578 } else {
579 const KUrl& dirListerUrl = m_dirLister->url();
580 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
581 // The current URL is not a child of the dir lister
582 // URL. This may happen when e. g. a bookmark has been selected
583 // and hence the view must be reset.
584 keepOldDirs = false;
585 }
586 }
587 }
588
589 if (openDir) {
590 m_dirLister->openUrl(url, keepOldDirs, reload);
591 }
592 }
593
594 void DolphinView::applyViewProperties(const KUrl& url)
595 {
596 const ViewProperties props(url);
597
598 const Mode mode = props.viewMode();
599 bool changeMode = (m_mode != mode);
600 if (changeMode && isColumnViewActive()) {
601 // The column view is active. Only change the
602 // mode if the current URL is no child of the column view.
603 if (m_dirLister->url().isParentOf(url)) {
604 changeMode = false;
605 }
606 }
607
608 if (changeMode) {
609 m_mode = mode;
610 createView();
611 emit modeChanged();
612
613 if (m_mode == ColumnView) {
614 // The mode has been changed to the Column View. When starting the dir
615 // lister with DolphinView::startDirLister() it is important to give a
616 // hint that the dir lister may not keep the current directory
617 // although this is the default for showing a hierarchy.
618 m_initializeColumnView = true;
619 }
620 }
621 if (itemView() == 0) {
622 createView();
623 }
624 Q_ASSERT(itemView() != 0);
625 Q_ASSERT(m_fileItemDelegate != 0);
626
627 const bool showHiddenFiles = props.showHiddenFiles();
628 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
629 m_dirLister->setShowingDotFiles(showHiddenFiles);
630 emit showHiddenFilesChanged();
631 }
632
633 const bool categorized = props.categorizedSorting();
634 if (categorized != categorizedSorting()) {
635 if (supportsCategorizedSorting()) {
636 Q_ASSERT(m_iconsView != 0);
637 if (categorized) {
638 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
639 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
640 } else {
641 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
642 m_iconsView->setItemCategorizer(0);
643 delete categorizer;
644 }
645 }
646 emit categorizedSortingChanged();
647 }
648
649 const DolphinView::Sorting sorting = props.sorting();
650 if (sorting != m_proxyModel->sorting()) {
651 m_proxyModel->setSorting(sorting);
652 emit sortingChanged(sorting);
653 }
654
655 const Qt::SortOrder sortOrder = props.sortOrder();
656 if (sortOrder != m_proxyModel->sortOrder()) {
657 m_proxyModel->setSortOrder(sortOrder);
658 emit sortOrderChanged(sortOrder);
659 }
660
661 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
662 if (info != m_fileItemDelegate->additionalInformation()) {
663 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
664 m_fileItemDelegate->setAdditionalInformation(info);
665 emit additionalInfoChanged(info);
666 }
667
668 const bool showPreview = props.showPreview();
669 if (showPreview != m_controller->showPreview()) {
670 m_controller->setShowPreview(showPreview);
671 emit showPreviewChanged();
672 }
673 }
674
675 void DolphinView::changeSelection(const KFileItemList& selection)
676 {
677 clearSelection();
678 if (selection.isEmpty()) {
679 return;
680 }
681 const KUrl& baseUrl = url();
682 KUrl url;
683 QItemSelection new_selection;
684 foreach(KFileItem* item, selection) {
685 url = item->url().upUrl();
686 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
687 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
688 new_selection.select(index, index);
689 }
690 }
691 itemView()->selectionModel()->select(new_selection,
692 QItemSelectionModel::ClearAndSelect
693 | QItemSelectionModel::Current);
694 }
695
696 void DolphinView::openContextMenu(const QPoint& pos)
697 {
698 KFileItem* item = 0;
699
700 const QModelIndex index = itemView()->indexAt(pos);
701 if (isValidNameIndex(index)) {
702 item = fileItem(index);
703 }
704
705 emit requestContextMenu(item, url());
706 }
707
708 void DolphinView::dropUrls(const KUrl::List& urls,
709 const QModelIndex& index,
710 QWidget* source)
711 {
712 KFileItem* directory = 0;
713 if (isValidNameIndex(index)) {
714 KFileItem* item = fileItem(index);
715 Q_ASSERT(item != 0);
716 if (item->isDir()) {
717 // the URLs are dropped above a directory
718 directory = item;
719 }
720 }
721
722 if ((directory == 0) && (source == itemView())) {
723 // The dropping is done into the same viewport where
724 // the dragging has been started. Just ignore this...
725 return;
726 }
727
728 const KUrl& destination = (directory == 0) ?
729 url() : directory->url();
730 dropUrls(urls, destination);
731 }
732
733 void DolphinView::dropUrls(const KUrl::List& urls,
734 const KUrl& destination)
735 {
736 emit urlsDropped(urls, destination);
737 }
738
739 void DolphinView::updateSorting(DolphinView::Sorting sorting)
740 {
741 ViewProperties props(url());
742 props.setSorting(sorting);
743
744 m_proxyModel->setSorting(sorting);
745
746 emit sortingChanged(sorting);
747 }
748
749 void DolphinView::updateSortOrder(Qt::SortOrder order)
750 {
751 ViewProperties props(url());
752 props.setSortOrder(order);
753
754 m_proxyModel->setSortOrder(order);
755
756 emit sortOrderChanged(order);
757 }
758
759 void DolphinView::emitContentsMoved()
760 {
761 // only emit the contents moved signal if:
762 // - no directory loading is ongoing (this would reset the contents position
763 // always to (0, 0))
764 // - if the Column View is active: the column view does an automatic
765 // positioning during the loading operation, which must be remembered
766 if (!m_loadingDirectory || isColumnViewActive()) {
767 const QPoint pos(contentsPosition());
768 emit contentsMoved(pos.x(), pos.y());
769 }
770 }
771
772 void DolphinView::updateCutItems()
773 {
774 // restore the icons of all previously selected items to the
775 // original state...
776 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
777 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
778 while (it != end) {
779 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
780 if (index.isValid()) {
781 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
782 }
783 ++it;
784 }
785 m_cutItemsCache.clear();
786
787 // ... and apply an item effect to all currently cut items
788 applyCutItemEffect();
789 }
790
791 void DolphinView::showHoverInformation(const QModelIndex& index)
792 {
793 if (hasSelection()) {
794 return;
795 }
796
797 const KFileItem* item = fileItem(index);
798 if (item != 0) {
799 emit requestItemInfo(*item);
800 }
801 }
802
803 void DolphinView::clearHoverInformation()
804 {
805 emit requestItemInfo(KFileItem());
806 }
807
808
809 void DolphinView::createView()
810 {
811 // delete current view
812 QAbstractItemView* view = itemView();
813 if (view != 0) {
814 m_topLayout->removeWidget(view);
815 view->close();
816 if (view == m_iconsView) {
817 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
818 m_iconsView->setItemCategorizer(0);
819 delete categorizer;
820 }
821 view->deleteLater();
822 view = 0;
823 m_iconsView = 0;
824 m_detailsView = 0;
825 m_columnView = 0;
826 m_fileItemDelegate = 0;
827 }
828
829 Q_ASSERT(m_iconsView == 0);
830 Q_ASSERT(m_detailsView == 0);
831 Q_ASSERT(m_columnView == 0);
832
833 // ... and recreate it representing the current mode
834 switch (m_mode) {
835 case IconsView:
836 m_iconsView = new DolphinIconsView(this, m_controller);
837 view = m_iconsView;
838 break;
839
840 case DetailsView:
841 m_detailsView = new DolphinDetailsView(this, m_controller);
842 view = m_detailsView;
843 break;
844
845 case ColumnView:
846 m_columnView = new DolphinColumnView(this, m_controller);
847 view = m_columnView;
848 break;
849 }
850
851 Q_ASSERT(view != 0);
852
853 m_fileItemDelegate = new KFileItemDelegate(view);
854 view->setItemDelegate(m_fileItemDelegate);
855
856 view->setModel(m_proxyModel);
857 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
858
859 new KMimeTypeResolver(view, m_dirModel);
860 m_topLayout->insertWidget(1, view);
861
862 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
863 this, SLOT(emitSelectionChangedSignal()));
864 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
865 this, SLOT(emitContentsMoved()));
866 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
867 this, SLOT(emitContentsMoved()));
868 }
869
870 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
871 {
872 QItemSelectionModel* selectionModel = itemView()->selectionModel();
873 const QAbstractItemModel* itemModel = selectionModel->model();
874
875 const QModelIndex topLeft = itemModel->index(0, 0);
876 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
877 itemModel->columnCount() - 1);
878
879 QItemSelection selection(topLeft, bottomRight);
880 selectionModel->select(selection, flags);
881 }
882
883 QAbstractItemView* DolphinView::itemView() const
884 {
885 if (m_detailsView != 0) {
886 return m_detailsView;
887 } else if (m_columnView != 0) {
888 return m_columnView;
889 }
890
891 return m_iconsView;
892 }
893
894 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
895 {
896 return index.isValid() && (index.column() == KDirModel::Name);
897 }
898
899 bool DolphinView::isCutItem(const KFileItem& item) const
900 {
901 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
902 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
903
904 const KUrl& itemUrl = item.url();
905 KUrl::List::const_iterator it = cutUrls.begin();
906 const KUrl::List::const_iterator end = cutUrls.end();
907 while (it != end) {
908 if (*it == itemUrl) {
909 return true;
910 }
911 ++it;
912 }
913
914 return false;
915 }
916
917 void DolphinView::applyCutItemEffect()
918 {
919 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
920 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
921 return;
922 }
923
924 KFileItemList items(m_dirLister->items());
925 KFileItemList::const_iterator it = items.begin();
926 const KFileItemList::const_iterator end = items.end();
927 while (it != end) {
928 KFileItem* item = *it;
929 if (isCutItem(*item)) {
930 const QModelIndex index = m_dirModel->indexForItem(*item);
931 const KFileItem* item = m_dirModel->itemForIndex(index);
932 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
933 if ((value.type() == QVariant::Icon) && (item != 0)) {
934 const QIcon icon(qvariant_cast<QIcon>(value));
935 QPixmap pixmap = icon.pixmap(128, 128);
936
937 // remember current pixmap for the item to be able
938 // to restore it when other items get cut
939 CutItem cutItem;
940 cutItem.url = item->url();
941 cutItem.pixmap = pixmap;
942 m_cutItemsCache.append(cutItem);
943
944 // apply icon effect to the cut item
945 KIconEffect iconEffect;
946 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
947 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
948 }
949 }
950 ++it;
951 }
952 }
953
954 #include "dolphinview.moc"