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