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