]> cloud.milkyroute.net Git - dolphin.git/blob - src/dolphinview.cpp
* fixed issue that some view properties like "show in groups" are ignored when a...
[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 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 = KGlobalSettings::baseColor();
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 reload();
419 }
420
421 void DolphinView::setUrl(const KUrl& url)
422 {
423 if (m_controller->url() == url) {
424 return;
425 }
426
427 m_controller->setUrl(url);
428
429 applyViewProperties(url);
430
431 startDirLister(url);
432 emit urlChanged(url);
433 }
434
435 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
436 {
437 QWidget::mouseReleaseEvent(event);
438 setActive(true);;
439 }
440 void DolphinView::activate()
441 {
442 setActive(true);
443 }
444
445 void DolphinView::triggerItem(const QModelIndex& index)
446 {
447 if (!isValidNameIndex(index)) {
448 clearSelection();
449 showHoverInformation(index);
450 return;
451 }
452
453 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
454 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
455 // items are selected by the user, hence don't trigger the
456 // item specified by 'index'
457 return;
458 }
459
460 KFileItem* item = m_dirModel->itemForIndex(m_proxyModel->mapToSource(index));
461 if (item == 0) {
462 return;
463 }
464
465 // Prefer the local path over the URL. This assures that the
466 // volume space information is correct. Assuming that the URL is media:/sda1,
467 // and the local path is /windows/C: For the URL the space info is related
468 // to the root partition (and hence wrong) and for the local path the space
469 // info is related to the windows partition (-> correct).
470 const QString localPath(item->localPath());
471 KUrl url;
472 if (localPath.isEmpty()) {
473 url = item->url();
474 } else {
475 url = localPath;
476 }
477
478 if (item->isDir()) {
479 setUrl(url);
480 } else if (item->isFile()) {
481 // allow to browse through ZIP and tar files
482 KMimeType::Ptr mime = item->mimeTypePtr();
483 if (mime->is("application/zip")) {
484 url.setProtocol("zip");
485 setUrl(url);
486 } else if (mime->is("application/x-tar") ||
487 mime->is("application/x-tarz") ||
488 mime->is("application/x-bzip-compressed-tar") ||
489 mime->is("application/x-compressed-tar") ||
490 mime->is("application/x-tzo")) {
491 url.setProtocol("tar");
492 setUrl(url);
493 } else {
494 item->run();
495 }
496 } else {
497 item->run();
498 }
499 }
500
501 void DolphinView::generatePreviews(const KFileItemList& items)
502 {
503 if (m_controller->showPreview()) {
504
505 // Must turn QList<KFileItem *> to QList<KFileItem>...
506 QList<KFileItem> itemsToPreview;
507 foreach( KFileItem* it, items )
508 itemsToPreview.append( *it );
509
510 KIO::PreviewJob* job = KIO::filePreview(itemsToPreview, 128);
511 connect(job, SIGNAL(gotPreview(const KFileItem&, const QPixmap&)),
512 this, SLOT(showPreview(const KFileItem&, const QPixmap&)));
513 }
514 }
515
516 void DolphinView::showPreview(const KFileItem& item, const QPixmap& pixmap)
517 {
518 Q_ASSERT(!item.isNull());
519 if (item.url().directory() != m_dirLister->url().path()) {
520 // the preview job is still working on items of an older URL, hence
521 // the item is not part of the directory model anymore
522 return;
523 }
524
525 const QModelIndex idx = m_dirModel->indexForItem(item);
526 if (idx.isValid() && (idx.column() == 0)) {
527 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
528 if (KonqMimeData::decodeIsCutSelection(mimeData) && isCutItem(item)) {
529 KIconEffect iconEffect;
530 const QPixmap cutPixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
531 m_dirModel->setData(idx, QIcon(cutPixmap), Qt::DecorationRole);
532 } else {
533 m_dirModel->setData(idx, QIcon(pixmap), Qt::DecorationRole);
534 }
535 }
536 }
537
538 void DolphinView::emitSelectionChangedSignal()
539 {
540 emit selectionChanged(DolphinView::selectedItems());
541 }
542
543 void DolphinView::startDirLister(const KUrl& url, bool reload)
544 {
545 if (!url.isValid()) {
546 const QString location(url.pathOrUrl());
547 if (location.isEmpty()) {
548 emit errorMessage(i18n("The location is empty."));
549 } else {
550 emit errorMessage(i18n("The location '%1' is invalid.", location));
551 }
552 return;
553 }
554
555 m_cutItemsCache.clear();
556 m_loadingDirectory = true;
557
558 m_dirLister->stop();
559
560 bool openDir = true;
561 bool keepOldDirs = isColumnViewActive() && !m_initializeColumnView;
562 m_initializeColumnView = false;
563
564 if (keepOldDirs) {
565 if (reload) {
566 keepOldDirs = false;
567
568 const KUrl& dirListerUrl = m_dirLister->url();
569 if (dirListerUrl.isValid()) {
570 const KUrl::List dirs = m_dirLister->directories();
571 KUrl url;
572 foreach(url, dirs) {
573 m_dirLister->updateDirectory(url);
574 }
575 openDir = false;
576 }
577 } else if (m_dirLister->directories().contains(url)) {
578 // The dir lister contains the directory already, so
579 // KDirLister::openUrl() may not been invoked twice.
580 m_dirLister->updateDirectory(url);
581 openDir = false;
582 } else {
583 const KUrl& dirListerUrl = m_dirLister->url();
584 if ((dirListerUrl == url) || !m_dirLister->url().isParentOf(url)) {
585 // The current URL is not a child of the dir lister
586 // URL. This may happen when e. g. a bookmark has been selected
587 // and hence the view must be reset.
588 keepOldDirs = false;
589 }
590 }
591 }
592
593 if (openDir) {
594 m_dirLister->openUrl(url, keepOldDirs, reload);
595 }
596 }
597
598 void DolphinView::applyViewProperties(const KUrl& url)
599 {
600 const ViewProperties props(url);
601
602 const Mode mode = props.viewMode();
603 bool changeMode = (m_mode != mode);
604 if (changeMode && isColumnViewActive()) {
605 // The column view is active. Only change the
606 // mode if the current URL is no child of the column view.
607 if (m_dirLister->url().isParentOf(url)) {
608 changeMode = false;
609 }
610 }
611
612 if (changeMode) {
613 m_mode = mode;
614 createView();
615 emit modeChanged();
616
617 if (m_mode == ColumnView) {
618 // The mode has been changed to the Column View. When starting the dir
619 // lister with DolphinView::startDirLister() it is important to give a
620 // hint that the dir lister may not keep the current directory
621 // although this is the default for showing a hierarchy.
622 m_initializeColumnView = true;
623 }
624 }
625 if (itemView() == 0) {
626 createView();
627 }
628 Q_ASSERT(itemView() != 0);
629 Q_ASSERT(m_fileItemDelegate != 0);
630
631 const bool showHiddenFiles = props.showHiddenFiles();
632 if (showHiddenFiles != m_dirLister->showingDotFiles()) {
633 m_dirLister->setShowingDotFiles(showHiddenFiles);
634 emit showHiddenFilesChanged();
635 }
636
637 const bool categorized = props.categorizedSorting();
638 if (categorized != categorizedSorting()) {
639 if (supportsCategorizedSorting()) {
640 Q_ASSERT(m_iconsView != 0);
641 if (categorized) {
642 Q_ASSERT(m_iconsView->itemCategorizer() == 0);
643 m_iconsView->setItemCategorizer(new DolphinItemCategorizer());
644 } else {
645 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
646 m_iconsView->setItemCategorizer(0);
647 delete categorizer;
648 }
649 }
650 emit categorizedSortingChanged();
651 }
652
653 const DolphinView::Sorting sorting = props.sorting();
654 if (sorting != m_proxyModel->sorting()) {
655 m_proxyModel->setSorting(sorting);
656 emit sortingChanged(sorting);
657 }
658
659 const Qt::SortOrder sortOrder = props.sortOrder();
660 if (sortOrder != m_proxyModel->sortOrder()) {
661 m_proxyModel->setSortOrder(sortOrder);
662 emit sortOrderChanged(sortOrder);
663 }
664
665 KFileItemDelegate::AdditionalInformation info = props.additionalInfo();
666 if (info != m_fileItemDelegate->additionalInformation()) {
667 m_controller->setShowAdditionalInfo(info != KFileItemDelegate::NoInformation);
668 m_fileItemDelegate->setAdditionalInformation(info);
669 emit additionalInfoChanged(info);
670 }
671
672 const bool showPreview = props.showPreview();
673 if (showPreview != m_controller->showPreview()) {
674 m_controller->setShowPreview(showPreview);
675 emit showPreviewChanged();
676 }
677 }
678
679 void DolphinView::changeSelection(const KFileItemList& selection)
680 {
681 clearSelection();
682 if (selection.isEmpty()) {
683 return;
684 }
685 const KUrl& baseUrl = url();
686 KUrl url;
687 QItemSelection new_selection;
688 foreach(KFileItem* item, selection) {
689 url = item->url().upUrl();
690 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
691 QModelIndex index = m_proxyModel->mapFromSource(m_dirModel->indexForItem(*item));
692 new_selection.select(index, index);
693 }
694 }
695 itemView()->selectionModel()->select(new_selection,
696 QItemSelectionModel::ClearAndSelect
697 | QItemSelectionModel::Current);
698 }
699
700 void DolphinView::openContextMenu(const QPoint& pos)
701 {
702 KFileItem* item = 0;
703
704 const QModelIndex index = itemView()->indexAt(pos);
705 if (isValidNameIndex(index)) {
706 item = fileItem(index);
707 }
708
709 emit requestContextMenu(item, url());
710 }
711
712 void DolphinView::dropUrls(const KUrl::List& urls,
713 const QModelIndex& index,
714 QWidget* source)
715 {
716 KFileItem* directory = 0;
717 if (isValidNameIndex(index)) {
718 KFileItem* item = fileItem(index);
719 Q_ASSERT(item != 0);
720 if (item->isDir()) {
721 // the URLs are dropped above a directory
722 directory = item;
723 }
724 }
725
726 if ((directory == 0) && (source == itemView())) {
727 // The dropping is done into the same viewport where
728 // the dragging has been started. Just ignore this...
729 return;
730 }
731
732 const KUrl& destination = (directory == 0) ?
733 url() : directory->url();
734 dropUrls(urls, destination);
735 }
736
737 void DolphinView::dropUrls(const KUrl::List& urls,
738 const KUrl& destination)
739 {
740 emit urlsDropped(urls, destination);
741 }
742
743 void DolphinView::updateSorting(DolphinView::Sorting sorting)
744 {
745 ViewProperties props(url());
746 props.setSorting(sorting);
747
748 m_proxyModel->setSorting(sorting);
749
750 emit sortingChanged(sorting);
751 }
752
753 void DolphinView::updateSortOrder(Qt::SortOrder order)
754 {
755 ViewProperties props(url());
756 props.setSortOrder(order);
757
758 m_proxyModel->setSortOrder(order);
759
760 emit sortOrderChanged(order);
761 }
762
763 void DolphinView::emitContentsMoved()
764 {
765 // only emit the contents moved signal if:
766 // - no directory loading is ongoing (this would reset the contents position
767 // always to (0, 0))
768 // - if the Column View is active: the column view does an automatic
769 // positioning during the loading operation, which must be remembered
770 if (!m_loadingDirectory || isColumnViewActive()) {
771 const QPoint pos(contentsPosition());
772 emit contentsMoved(pos.x(), pos.y());
773 }
774 }
775
776 void DolphinView::updateCutItems()
777 {
778 // restore the icons of all previously selected items to the
779 // original state...
780 QList<CutItem>::const_iterator it = m_cutItemsCache.begin();
781 QList<CutItem>::const_iterator end = m_cutItemsCache.end();
782 while (it != end) {
783 const QModelIndex index = m_dirModel->indexForUrl((*it).url);
784 if (index.isValid()) {
785 m_dirModel->setData(index, QIcon((*it).pixmap), Qt::DecorationRole);
786 }
787 ++it;
788 }
789 m_cutItemsCache.clear();
790
791 // ... and apply an item effect to all currently cut items
792 applyCutItemEffect();
793 }
794
795 void DolphinView::showHoverInformation(const QModelIndex& index)
796 {
797 if (hasSelection()) {
798 return;
799 }
800
801 const KFileItem* item = fileItem(index);
802 if (item != 0) {
803 emit requestItemInfo(item->url());
804 }
805 }
806
807 void DolphinView::clearHoverInformation()
808 {
809 emit requestItemInfo(KUrl());
810 }
811
812
813 void DolphinView::createView()
814 {
815 // delete current view
816 QAbstractItemView* view = itemView();
817 if (view != 0) {
818 m_topLayout->removeWidget(view);
819 view->close();
820 if (view == m_iconsView) {
821 KItemCategorizer* categorizer = m_iconsView->itemCategorizer();
822 m_iconsView->setItemCategorizer(0);
823 delete categorizer;
824 }
825 view->deleteLater();
826 view = 0;
827 m_iconsView = 0;
828 m_detailsView = 0;
829 m_columnView = 0;
830 m_fileItemDelegate = 0;
831 }
832
833 Q_ASSERT(m_iconsView == 0);
834 Q_ASSERT(m_detailsView == 0);
835 Q_ASSERT(m_columnView == 0);
836
837 // ... and recreate it representing the current mode
838 switch (m_mode) {
839 case IconsView:
840 m_iconsView = new DolphinIconsView(this, m_controller);
841 view = m_iconsView;
842 break;
843
844 case DetailsView:
845 m_detailsView = new DolphinDetailsView(this, m_controller);
846 view = m_detailsView;
847 break;
848
849 case ColumnView:
850 m_columnView = new DolphinColumnView(this, m_controller);
851 view = m_columnView;
852 break;
853 }
854
855 Q_ASSERT(view != 0);
856
857 m_fileItemDelegate = new KFileItemDelegate(view);
858 view->setItemDelegate(m_fileItemDelegate);
859
860 view->setModel(m_proxyModel);
861 view->setSelectionMode(QAbstractItemView::ExtendedSelection);
862
863 new KMimeTypeResolver(view, m_dirModel);
864 m_topLayout->insertWidget(1, view);
865
866 connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)),
867 this, SLOT(emitSelectionChangedSignal()));
868 connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)),
869 this, SLOT(emitContentsMoved()));
870 connect(view->horizontalScrollBar(), SIGNAL(valueChanged(int)),
871 this, SLOT(emitContentsMoved()));
872 }
873
874 void DolphinView::selectAll(QItemSelectionModel::SelectionFlags flags)
875 {
876 QItemSelectionModel* selectionModel = itemView()->selectionModel();
877 const QAbstractItemModel* itemModel = selectionModel->model();
878
879 const QModelIndex topLeft = itemModel->index(0, 0);
880 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
881 itemModel->columnCount() - 1);
882
883 QItemSelection selection(topLeft, bottomRight);
884 selectionModel->select(selection, flags);
885 }
886
887 QAbstractItemView* DolphinView::itemView() const
888 {
889 if (m_detailsView != 0) {
890 return m_detailsView;
891 } else if (m_columnView != 0) {
892 return m_columnView;
893 }
894
895 return m_iconsView;
896 }
897
898 bool DolphinView::isValidNameIndex(const QModelIndex& index) const
899 {
900 return index.isValid() && (index.column() == KDirModel::Name);
901 }
902
903 bool DolphinView::isCutItem(const KFileItem& item) const
904 {
905 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
906 const KUrl::List cutUrls = KUrl::List::fromMimeData(mimeData);
907
908 const KUrl& itemUrl = item.url();
909 KUrl::List::const_iterator it = cutUrls.begin();
910 const KUrl::List::const_iterator end = cutUrls.end();
911 while (it != end) {
912 if (*it == itemUrl) {
913 return true;
914 }
915 ++it;
916 }
917
918 return false;
919 }
920
921 void DolphinView::applyCutItemEffect()
922 {
923 const QMimeData* mimeData = QApplication::clipboard()->mimeData();
924 if (!KonqMimeData::decodeIsCutSelection(mimeData)) {
925 return;
926 }
927
928 KFileItemList items(m_dirLister->items());
929 KFileItemList::const_iterator it = items.begin();
930 const KFileItemList::const_iterator end = items.end();
931 while (it != end) {
932 KFileItem* item = *it;
933 if (isCutItem(*item)) {
934 const QModelIndex index = m_dirModel->indexForItem(*item);
935 const KFileItem* item = m_dirModel->itemForIndex(index);
936 const QVariant value = m_dirModel->data(index, Qt::DecorationRole);
937 if ((value.type() == QVariant::Icon) && (item != 0)) {
938 const QIcon icon(qvariant_cast<QIcon>(value));
939 QPixmap pixmap = icon.pixmap(128, 128);
940
941 // remember current pixmap for the item to be able
942 // to restore it when other items get cut
943 CutItem cutItem;
944 cutItem.url = item->url();
945 cutItem.pixmap = pixmap;
946 m_cutItemsCache.append(cutItem);
947
948 // apply icon effect to the cut item
949 KIconEffect iconEffect;
950 pixmap = iconEffect.apply(pixmap, K3Icon::Desktop, K3Icon::DisabledState);
951 m_dirModel->setData(index, QIcon(pixmap), Qt::DecorationRole);
952 }
953 }
954 ++it;
955 }
956 }
957
958 #include "dolphinview.moc"