]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Show an undetermined progress indication within one second after changing an URL.
[dolphin.git] / src / views / dolphinview.cpp
1 /***************************************************************************
2 * Copyright (C) 2006-2009 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 <QAbstractItemView>
24 #include <QApplication>
25 #include <QClipboard>
26 #include <QKeyEvent>
27 #include <QItemSelection>
28 #include <QBoxLayout>
29 #include <QTimer>
30 #include <QScrollBar>
31
32 #include <kactioncollection.h>
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kiconeffect.h>
36 #include <kfileitem.h>
37 #include <klocale.h>
38 #include <kio/deletejob.h>
39 #include <kio/netaccess.h>
40 #include <kio/previewjob.h>
41 #include <kjob.h>
42 #include <kmenu.h>
43 #include <kmessagebox.h>
44 #include <konq_fileitemcapabilities.h>
45 #include <konq_operations.h>
46 #include <konqmimedata.h>
47 #include <ktoggleaction.h>
48 #include <kurl.h>
49
50 #include "additionalinfoaccessor.h"
51 #include "dolphinmodel.h"
52 #include "dolphincolumnviewcontainer.h"
53 #include "dolphinviewcontroller.h"
54 #include "dolphindetailsview.h"
55 #include "dolphinfileitemdelegate.h"
56 #include "dolphinnewfilemenuobserver.h"
57 #include "dolphinsortfilterproxymodel.h"
58 #include "dolphin_detailsmodesettings.h"
59 #include "dolphiniconsview.h"
60 #include "dolphin_generalsettings.h"
61 #include "draganddrophelper.h"
62 #include "renamedialog.h"
63 #include "settings/dolphinsettings.h"
64 #include "viewmodecontroller.h"
65 #include "viewproperties.h"
66 #include "zoomlevelinfo.h"
67 #include "dolphindetailsviewexpander.h"
68
69 DolphinView::DolphinView(QWidget* parent,
70 const KUrl& url,
71 DolphinSortFilterProxyModel* proxyModel) :
72 QWidget(parent),
73 m_active(true),
74 m_showPreview(false),
75 m_storedCategorizedSorting(false),
76 m_tabsForFiles(false),
77 m_isContextMenuOpen(false),
78 m_assureVisibleCurrentIndex(false),
79 m_mode(DolphinView::IconsView),
80 m_topLayout(0),
81 m_dolphinViewController(0),
82 m_viewModeController(0),
83 m_viewAccessor(proxyModel),
84 m_selectionChangedTimer(0),
85 m_activeItemUrl(),
86 m_restoredContentsPosition(),
87 m_createdItemUrl(),
88 m_selectedItems(),
89 m_newFileNames()
90 {
91 m_topLayout = new QVBoxLayout(this);
92 m_topLayout->setSpacing(0);
93 m_topLayout->setMargin(0);
94
95 m_dolphinViewController = new DolphinViewController(this);
96
97 m_viewModeController = new ViewModeController(this);
98 m_viewModeController->setUrl(url);
99
100 connect(m_viewModeController, SIGNAL(urlChanged(const KUrl&)),
101 this, SIGNAL(urlChanged(const KUrl&)));
102
103 connect(m_dolphinViewController, SIGNAL(requestContextMenu(const QPoint&, const QList<QAction*>&)),
104 this, SLOT(openContextMenu(const QPoint&, const QList<QAction*>&)));
105 connect(m_dolphinViewController, SIGNAL(urlsDropped(const KFileItem&, const KUrl&, QDropEvent*)),
106 this, SLOT(dropUrls(const KFileItem&, const KUrl&, QDropEvent*)));
107 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
108 this, SLOT(updateSorting(DolphinView::Sorting)));
109 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
110 this, SLOT(updateSortOrder(Qt::SortOrder)));
111 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
112 this, SLOT(updateSortFoldersFirst(bool)));
113 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList&)),
114 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList&)));
115 connect(m_dolphinViewController, SIGNAL(itemTriggered(const KFileItem&)),
116 this, SLOT(triggerItem(const KFileItem&)));
117 connect(m_dolphinViewController, SIGNAL(tabRequested(const KUrl&)),
118 this, SIGNAL(tabRequested(const KUrl&)));
119 connect(m_dolphinViewController, SIGNAL(activated()),
120 this, SLOT(activate()));
121 connect(m_dolphinViewController, SIGNAL(itemEntered(const KFileItem&)),
122 this, SLOT(showHoverInformation(const KFileItem&)));
123 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
124 this, SLOT(clearHoverInformation()));
125 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
126 m_viewModeController, SLOT(setUrl(KUrl)));
127
128 // When a new item has been created by the "Create New..." menu, the item should
129 // get selected and it must be assured that the item will get visible. As the
130 // creation is done asynchronously, several signals must be checked:
131 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(const KUrl&)),
132 this, SLOT(observeCreatedItem(const KUrl&)));
133
134 m_selectionChangedTimer = new QTimer(this);
135 m_selectionChangedTimer->setSingleShot(true);
136 m_selectionChangedTimer->setInterval(300);
137 connect(m_selectionChangedTimer, SIGNAL(timeout()),
138 this, SLOT(emitSelectionChangedSignal()));
139
140 applyViewProperties();
141 m_topLayout->addWidget(m_viewAccessor.layoutTarget());
142 }
143
144 DolphinView::~DolphinView()
145 {
146 }
147
148 KUrl DolphinView::url() const
149 {
150 return m_viewModeController->url();
151 }
152
153 KUrl DolphinView::rootUrl() const
154 {
155 const KUrl viewUrl = url();
156 const KUrl root = m_viewAccessor.rootUrl();
157 if (root.isEmpty() || !root.isParentOf(viewUrl)) {
158 return viewUrl;
159 }
160 return root;
161 }
162
163 void DolphinView::setActive(bool active)
164 {
165 if (active == m_active) {
166 return;
167 }
168
169 m_active = active;
170
171 QColor color = KColorScheme(QPalette::Active, KColorScheme::View).background().color();
172 if (active) {
173 emitSelectionChangedSignal();
174 } else {
175 color.setAlpha(150);
176 }
177
178 QWidget* viewport = m_viewAccessor.itemView()->viewport();
179 QPalette palette;
180 palette.setColor(viewport->backgroundRole(), color);
181 viewport->setPalette(palette);
182
183 update();
184
185 if (active) {
186 m_viewAccessor.itemView()->setFocus();
187 emit activated();
188 }
189
190 m_viewModeController->indicateActivationChange(active);
191 }
192
193 bool DolphinView::isActive() const
194 {
195 return m_active;
196 }
197
198 void DolphinView::setMode(Mode mode)
199 {
200 if (mode == m_mode) {
201 return; // the wished mode is already set
202 }
203
204 const int oldZoomLevel = m_viewModeController->zoomLevel();
205 m_mode = mode;
206
207 // remember the currently selected items, so that they will
208 // be restored after reloading the directory
209 m_selectedItems = selectedItems();
210
211 deleteView();
212
213 const KUrl viewPropsUrl = rootUrl();
214 ViewProperties props(viewPropsUrl);
215 props.setViewMode(m_mode);
216 createView();
217
218 // the file item delegate has been recreated, apply the current
219 // additional information manually
220 const KFileItemDelegate::InformationList infoList = props.additionalInfo();
221 m_viewAccessor.itemDelegate()->setShowInformation(infoList);
222 emit additionalInfoChanged();
223
224 // Not all view modes support categorized sorting. Adjust the sorting model
225 // if changing the view mode results in a change of the categorized sorting
226 // capabilities.
227 m_storedCategorizedSorting = props.categorizedSorting();
228 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
229 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
230 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
231 emit categorizedSortingChanged();
232 }
233
234 emit modeChanged();
235
236 updateZoomLevel(oldZoomLevel);
237 loadDirectory(viewPropsUrl);
238 }
239
240 DolphinView::Mode DolphinView::mode() const
241 {
242 return m_mode;
243 }
244
245 bool DolphinView::showPreview() const
246 {
247 return m_showPreview;
248 }
249
250 bool DolphinView::showHiddenFiles() const
251 {
252 return m_viewAccessor.dirLister()->showingDotFiles();
253 }
254
255 bool DolphinView::categorizedSorting() const
256 {
257 // If all view modes would support categorized sorting, returning
258 // m_viewAccessor.proxyModel()->isCategorizedModel() would be the way to go. As
259 // currently only the icons view supports caterized sorting, we remember
260 // the stored view properties state in m_storedCategorizedSorting and
261 // return this state. The application takes care to disable the corresponding
262 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
263 // that this setting is not applied to the current view mode.
264 return m_storedCategorizedSorting;
265 }
266
267 bool DolphinView::supportsCategorizedSorting() const
268 {
269 return m_viewAccessor.supportsCategorizedSorting();
270 }
271
272 KFileItemList DolphinView::items() const
273 {
274 return m_viewAccessor.dirLister()->items();
275 }
276
277 KFileItemList DolphinView::selectedItems() const
278 {
279 KFileItemList itemList;
280 const QAbstractItemView* view = m_viewAccessor.itemView();
281 if (view == 0) {
282 return itemList;
283 }
284
285 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
286
287 const QModelIndexList indexList = selection.indexes();
288 foreach (const QModelIndex &index, indexList) {
289 KFileItem item = m_viewAccessor.dirModel()->itemForIndex(index);
290 if (!item.isNull()) {
291 itemList.append(item);
292 }
293 }
294
295 return itemList;
296 }
297
298 int DolphinView::selectedItemsCount() const
299 {
300 const QAbstractItemView* view = m_viewAccessor.itemView();
301 if (view == 0) {
302 return 0;
303 }
304
305 return view->selectionModel()->selectedIndexes().count();
306 }
307
308 void DolphinView::markUrlsAsSelected(const QList<KUrl>& urls)
309 {
310 foreach (const KUrl& url, urls) {
311 KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url);
312 m_selectedItems.append(item);
313 }
314 }
315
316 void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled)
317 {
318 const QItemSelection matchingIndexes = childrenMatchingPattern(QModelIndex(), pattern);
319 const QItemSelectionModel::SelectionFlags command = enabled
320 ? QItemSelectionModel::Select
321 : QItemSelectionModel::Deselect;
322 m_viewAccessor.itemView()->selectionModel()->select(matchingIndexes, command);
323 }
324
325 void DolphinView::setZoomLevel(int level)
326 {
327 if (level < ZoomLevelInfo::minimumLevel()) {
328 level = ZoomLevelInfo::minimumLevel();
329 } else if (level > ZoomLevelInfo::maximumLevel()) {
330 level = ZoomLevelInfo::maximumLevel();
331 }
332
333 if (level != zoomLevel()) {
334 m_viewModeController->setZoomLevel(level);
335 emit zoomLevelChanged(level);
336 }
337 }
338
339 int DolphinView::zoomLevel() const
340 {
341 return m_viewModeController->zoomLevel();
342 }
343
344 void DolphinView::setSorting(Sorting sorting)
345 {
346 if (sorting != this->sorting()) {
347 updateSorting(sorting);
348 }
349 }
350
351 DolphinView::Sorting DolphinView::sorting() const
352 {
353 return m_viewAccessor.proxyModel()->sorting();
354 }
355
356 void DolphinView::setSortOrder(Qt::SortOrder order)
357 {
358 if (sortOrder() != order) {
359 updateSortOrder(order);
360 }
361 }
362
363 Qt::SortOrder DolphinView::sortOrder() const
364 {
365 return m_viewAccessor.proxyModel()->sortOrder();
366 }
367
368 void DolphinView::setSortFoldersFirst(bool foldersFirst)
369 {
370 if (sortFoldersFirst() != foldersFirst) {
371 updateSortFoldersFirst(foldersFirst);
372 }
373 }
374
375 bool DolphinView::sortFoldersFirst() const
376 {
377 return m_viewAccessor.proxyModel()->sortFoldersFirst();
378 }
379
380 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info)
381 {
382 const KUrl viewPropsUrl = rootUrl();
383 ViewProperties props(viewPropsUrl);
384 props.setAdditionalInfo(info);
385 m_viewAccessor.itemDelegate()->setShowInformation(info);
386
387 emit additionalInfoChanged();
388
389 if (m_viewAccessor.reloadOnAdditionalInfoChange()) {
390 loadDirectory(viewPropsUrl);
391 }
392 }
393
394 KFileItemDelegate::InformationList DolphinView::additionalInfo() const
395 {
396 return m_viewAccessor.itemDelegate()->showInformation();
397 }
398
399 void DolphinView::reload()
400 {
401 QByteArray viewState;
402 QDataStream saveStream(&viewState, QIODevice::WriteOnly);
403 saveState(saveStream);
404 m_selectedItems= selectedItems();
405
406 setUrl(url());
407 loadDirectory(url(), true);
408
409 QDataStream restoreStream(viewState);
410 restoreState(restoreStream);
411 }
412
413 void DolphinView::stopLoading()
414 {
415 m_viewAccessor.dirLister()->stop();
416 }
417
418 void DolphinView::refresh()
419 {
420 const bool oldActivationState = m_active;
421 const int oldZoomLevel = m_viewModeController->zoomLevel();
422 m_active = true;
423
424 createView();
425 applyViewProperties();
426 reload();
427
428 setActive(oldActivationState);
429 updateZoomLevel(oldZoomLevel);
430 }
431
432 void DolphinView::setNameFilter(const QString& nameFilter)
433 {
434 m_viewModeController->setNameFilter(nameFilter);
435 }
436
437 void DolphinView::calculateItemCount(int& fileCount,
438 int& folderCount,
439 KIO::filesize_t& totalFileSize) const
440 {
441 foreach (const KFileItem& item, m_viewAccessor.dirLister()->items()) {
442 if (item.isDir()) {
443 ++folderCount;
444 } else {
445 ++fileCount;
446 totalFileSize += item.size();
447 }
448 }
449 }
450
451 QString DolphinView::statusBarText() const
452 {
453 QString text;
454 int folderCount = 0;
455 int fileCount = 0;
456 KIO::filesize_t totalFileSize = 0;
457
458 if (hasSelection()) {
459 // give a summary of the status of the selected files
460 const KFileItemList list = selectedItems();
461 if (list.isEmpty()) {
462 // when an item is triggered, it is temporary selected but selectedItems()
463 // will return an empty list
464 return text;
465 }
466
467 KFileItemList::const_iterator it = list.begin();
468 const KFileItemList::const_iterator end = list.end();
469 while (it != end) {
470 const KFileItem& item = *it;
471 if (item.isDir()) {
472 ++folderCount;
473 } else {
474 ++fileCount;
475 totalFileSize += item.size();
476 }
477 ++it;
478 }
479
480 if (folderCount + fileCount == 1) {
481 // if only one item is selected, show the filename
482 const QString name = list.first().text();
483 text = (folderCount == 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name) :
484 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
485 name, KIO::convertSize(totalFileSize));
486 } else {
487 // at least 2 items are selected
488 const QString foldersText = i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount);
489 const QString filesText = i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount);
490 if ((folderCount > 0) && (fileCount > 0)) {
491 text = i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
492 foldersText, filesText, KIO::convertSize(totalFileSize));
493 } else if (fileCount > 0) {
494 text = i18nc("@info:status files (size)", "%1 (%2)", filesText, KIO::convertSize(totalFileSize));
495 } else {
496 Q_ASSERT(folderCount > 0);
497 text = foldersText;
498 }
499 }
500 } else {
501 calculateItemCount(fileCount, folderCount, totalFileSize);
502 text = KIO::itemsSummaryString(fileCount + folderCount,
503 fileCount, folderCount,
504 totalFileSize, true);
505 }
506
507 return text;
508 }
509
510 QList<QAction*> DolphinView::versionControlActions(const KFileItemList& items) const
511 {
512 return m_dolphinViewController->versionControlActions(items);
513 }
514
515 void DolphinView::setUrl(const KUrl& url)
516 {
517 if (m_viewModeController->url() == url) {
518 return;
519 }
520
521 const bool hadSelection = hasSelection();
522
523 // The selection model and directory lister might change in the case of the column view:
524 disconnectViewAccessor();
525
526 m_newFileNames.clear();
527
528 m_viewModeController->setUrl(url); // emits urlChanged, which we forward
529 m_viewAccessor.prepareUrlChange(url);
530 applyViewProperties();
531
532 // When changing the URL there is no need to keep the version
533 // data of the previous URL.
534 m_viewAccessor.dirModel()->clearVersionData();
535
536 // Reconnect to the (probably) new selection model and directory lister
537 connectViewAccessor();
538 loadDirectory(url);
539
540 if (hadSelection || hasSelection()) {
541 emitSelectionChangedSignal();
542 }
543 }
544
545 void DolphinView::selectAll()
546 {
547 m_viewAccessor.itemView()->selectAll();
548 }
549
550 void DolphinView::invertSelection()
551 {
552 QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
553 const QAbstractItemModel* itemModel = selectionModel->model();
554
555 const QModelIndex topLeft = itemModel->index(0, 0);
556 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
557 itemModel->columnCount() - 1);
558
559 const QItemSelection selection(topLeft, bottomRight);
560 selectionModel->select(selection, QItemSelectionModel::Toggle);
561 }
562
563 void DolphinView::clearSelection()
564 {
565 m_viewAccessor.itemView()->clearSelection();
566 }
567
568 void DolphinView::renameSelectedItems()
569 {
570 KFileItemList items = selectedItems();
571 const int itemCount = items.count();
572 if (itemCount < 1) {
573 return;
574 }
575
576 if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
577 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
578 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
579 m_viewAccessor.itemView()->edit(proxyIndex);
580 } else {
581 RenameDialog* dialog = new RenameDialog(this, items);
582 dialog->setAttribute(Qt::WA_DeleteOnClose);
583 dialog->show();
584 dialog->raise();
585 dialog->activateWindow();
586 }
587 }
588
589 void DolphinView::trashSelectedItems()
590 {
591 const KUrl::List list = simplifiedSelectedUrls();
592 KonqOperations::del(this, KonqOperations::TRASH, list);
593 }
594
595 void DolphinView::deleteSelectedItems()
596 {
597 const KUrl::List list = simplifiedSelectedUrls();
598 const bool del = KonqOperations::askDeleteConfirmation(list,
599 KonqOperations::DEL,
600 KonqOperations::DEFAULT_CONFIRMATION,
601 this);
602
603 if (del) {
604 KIO::Job* job = KIO::del(list);
605 connect(job, SIGNAL(result(KJob*)),
606 this, SLOT(slotDeleteFileFinished(KJob*)));
607 }
608 }
609
610 void DolphinView::cutSelectedItems()
611 {
612 QMimeData* mimeData = selectionMimeData();
613 KonqMimeData::addIsCutSelection(mimeData, true);
614 QApplication::clipboard()->setMimeData(mimeData);
615 }
616
617 void DolphinView::copySelectedItems()
618 {
619 QMimeData* mimeData = selectionMimeData();
620 QApplication::clipboard()->setMimeData(mimeData);
621 }
622
623 void DolphinView::paste()
624 {
625 pasteToUrl(url());
626 }
627
628 void DolphinView::pasteIntoFolder()
629 {
630 const KFileItemList items = selectedItems();
631 if ((items.count() == 1) && items.first().isDir()) {
632 pasteToUrl(items.first().url());
633 }
634 }
635
636 void DolphinView::setShowPreview(bool show)
637 {
638 if (m_showPreview == show) {
639 return;
640 }
641
642 const KUrl viewPropsUrl = rootUrl();
643 ViewProperties props(viewPropsUrl);
644 props.setShowPreview(show);
645
646 m_showPreview = show;
647 const int oldZoomLevel = m_viewModeController->zoomLevel();
648 emit showPreviewChanged();
649
650 // Enabling or disabling the preview might change the icon size of the view.
651 // As the view does not emit a signal when the icon size has been changed,
652 // the used zoom level of the controller must be adjusted manually:
653 updateZoomLevel(oldZoomLevel);
654 }
655
656 void DolphinView::setShowHiddenFiles(bool show)
657 {
658 if (m_viewAccessor.dirLister()->showingDotFiles() == show) {
659 return;
660 }
661
662 const KUrl viewPropsUrl = rootUrl();
663 ViewProperties props(viewPropsUrl);
664 props.setShowHiddenFiles(show);
665
666 m_viewAccessor.dirLister()->setShowingDotFiles(show);
667 emit showHiddenFilesChanged();
668 }
669
670 void DolphinView::setCategorizedSorting(bool categorized)
671 {
672 if (categorized == categorizedSorting()) {
673 return;
674 }
675
676 // setCategorizedSorting(true) may only get invoked
677 // if the view supports categorized sorting
678 Q_ASSERT(!categorized || supportsCategorizedSorting());
679
680 ViewProperties props(rootUrl());
681 props.setCategorizedSorting(categorized);
682 props.save();
683
684 m_storedCategorizedSorting = categorized;
685 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
686
687 emit categorizedSortingChanged();
688 }
689
690 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
691 {
692 QWidget::mouseReleaseEvent(event);
693 setActive(true);
694 }
695
696 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
697 {
698 switch (event->type()) {
699 case QEvent::FocusIn:
700 if (watched == m_viewAccessor.itemView()) {
701 m_dolphinViewController->requestActivation();
702 }
703 break;
704
705 case QEvent::DragEnter:
706 if (watched == m_viewAccessor.itemView()->viewport()) {
707 setActive(true);
708 }
709 break;
710
711 case QEvent::KeyPress:
712 if (watched == m_viewAccessor.itemView()) {
713 // clear the selection when Escape has been pressed
714 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
715 if (keyEvent->key() == Qt::Key_Escape) {
716 clearSelection();
717 }
718 }
719 break;
720
721 case QEvent::Wheel:
722 if (watched == m_viewAccessor.itemView()->viewport()) {
723 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
724 // (the user is probably trying to scroll during a selection in that case)
725 QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
726 if (wheelEvent->modifiers() & Qt::ControlModifier && !(wheelEvent->buttons() & Qt::LeftButton)) {
727 const int delta = wheelEvent->delta();
728 const int level = zoomLevel();
729 if (delta > 0) {
730 setZoomLevel(level + 1);
731 } else if (delta < 0) {
732 setZoomLevel(level - 1);
733 }
734 return true;
735 }
736 }
737 break;
738
739 default:
740 break;
741 }
742
743 return QWidget::eventFilter(watched, event);
744 }
745
746 void DolphinView::activate()
747 {
748 setActive(true);
749 }
750
751 void DolphinView::triggerItem(const KFileItem& item)
752 {
753 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
754 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
755 // items are selected by the user, hence don't trigger the
756 // item specified by 'index'
757 return;
758 }
759
760 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
761 if (item.isNull() || m_isContextMenuOpen) {
762 return;
763 }
764
765 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
766 }
767
768 void DolphinView::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
769 {
770 const int count = selectedItemsCount();
771 const bool selectionStateChanged = ((count > 0) && (selected.count() == count)) ||
772 ((count == 0) && !deselected.isEmpty());
773
774 // If nothing has been selected before and something got selected (or if something
775 // was selected before and now nothing is selected) the selectionChangedSignal must
776 // be emitted asynchronously as fast as possible to update the edit-actions.
777 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
778 m_selectionChangedTimer->start();
779 }
780
781 void DolphinView::emitSelectionChangedSignal()
782 {
783 m_selectionChangedTimer->stop();
784 emit selectionChanged(selectedItems());
785 }
786
787 void DolphinView::openContextMenu(const QPoint& pos,
788 const QList<QAction*>& customActions)
789 {
790 KFileItem item;
791 const QModelIndex index = m_viewAccessor.itemView()->indexAt(pos);
792 if (index.isValid() && (index.column() == DolphinModel::Name)) {
793 const QModelIndex dolphinModelIndex = m_viewAccessor.proxyModel()->mapToSource(index);
794 item = m_viewAccessor.dirModel()->itemForIndex(dolphinModelIndex);
795 }
796
797 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
798 emit requestContextMenu(item, url(), customActions);
799 m_isContextMenuOpen = false;
800 }
801
802 void DolphinView::dropUrls(const KFileItem& destItem,
803 const KUrl& destPath,
804 QDropEvent* event)
805 {
806 addNewFileNames(event->mimeData());
807 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
808 }
809
810 void DolphinView::updateSorting(DolphinView::Sorting sorting)
811 {
812 ViewProperties props(rootUrl());
813 props.setSorting(sorting);
814
815 m_viewAccessor.proxyModel()->setSorting(sorting);
816
817 emit sortingChanged(sorting);
818 }
819
820 void DolphinView::updateSortOrder(Qt::SortOrder order)
821 {
822 ViewProperties props(rootUrl());
823 props.setSortOrder(order);
824
825 m_viewAccessor.proxyModel()->setSortOrder(order);
826
827 emit sortOrderChanged(order);
828 }
829
830 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
831 {
832 ViewProperties props(rootUrl());
833 props.setSortFoldersFirst(foldersFirst);
834
835 m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
836
837 emit sortFoldersFirstChanged(foldersFirst);
838 }
839
840 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
841 {
842 ViewProperties props(rootUrl());
843 props.setAdditionalInfo(info);
844 props.save();
845
846 m_viewAccessor.itemDelegate()->setShowInformation(info);
847
848 emit additionalInfoChanged();
849 }
850
851 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
852 {
853 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
854
855 const KFileItemDelegate::InformationList checkedInfo = m_viewAccessor.itemDelegate()->showInformation();
856 const KFileItemDelegate::InformationList infoKeys = infoAccessor.keys();
857
858 const bool enable = (m_mode == DolphinView::DetailsView) ||
859 (m_mode == DolphinView::IconsView);
860
861 foreach (const KFileItemDelegate::Information& info, infoKeys) {
862 const QString name = infoAccessor.actionCollectionName(info, AdditionalInfoAccessor::AdditionalInfoType);
863 QAction* action = collection->action(name);
864 Q_ASSERT(action != 0);
865 action->setEnabled(enable);
866 action->setChecked(checkedInfo.contains(info));
867 }
868 }
869
870 QPair<bool, QString> DolphinView::pasteInfo() const
871 {
872 return KonqOperations::pasteInfo(url());
873 }
874
875 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
876 {
877 m_tabsForFiles = tabsForFiles;
878 }
879
880 bool DolphinView::isTabsForFilesEnabled() const
881 {
882 return m_tabsForFiles;
883 }
884
885 bool DolphinView::itemsExpandable() const
886 {
887 return m_viewAccessor.itemsExpandable();
888 }
889
890 void DolphinView::restoreState(QDataStream& stream)
891 {
892 // Restore the URL of the current item that had the keyboard focus
893 stream >> m_activeItemUrl;
894
895 // Restore the root URL
896 KUrl rootUrl;
897 stream >> rootUrl;
898 m_viewAccessor.setRootUrl(rootUrl);
899
900 // Restore the view position
901 stream >> m_restoredContentsPosition;
902
903 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
904 QSet<KUrl> urlsToExpand;
905 stream >> urlsToExpand;
906 const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
907 if (expander != 0) {
908 m_expanderActive = true;
909 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
910 }
911 else {
912 m_expanderActive = false;
913 }
914 }
915
916 void DolphinView::saveState(QDataStream& stream)
917 {
918 // Save the URL of the current item that has the keyboard focus
919 KFileItem currentItem;
920 const QAbstractItemView* view = m_viewAccessor.itemView();
921
922 if (view != 0) {
923 const QModelIndex proxyIndex = view->currentIndex();
924 const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
925 currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
926 }
927
928 KUrl currentItemUrl;
929 if (!currentItem.isNull()) {
930 currentItemUrl = currentItem.url();
931 }
932
933 stream << currentItemUrl;
934
935 // Save the root URL
936 stream << m_viewAccessor.rootUrl();
937
938 // Save view position
939 const int x = view->horizontalScrollBar()->value();
940 const int y = view->verticalScrollBar()->value();
941 stream << QPoint(x, y);
942
943 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
944 stream << m_viewAccessor.expandedUrls();
945 }
946
947 bool DolphinView::hasSelection() const
948 {
949 const QAbstractItemView* view = m_viewAccessor.itemView();
950 return (view != 0) && view->selectionModel()->hasSelection();
951 }
952
953 void DolphinView::observeCreatedItem(const KUrl& url)
954 {
955 m_createdItemUrl = url;
956 connect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
957 this, SLOT(selectAndScrollToCreatedItem()));
958 }
959
960 void DolphinView::selectAndScrollToCreatedItem()
961 {
962 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
963 if (dirIndex.isValid()) {
964 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
965 m_viewAccessor.itemView()->setCurrentIndex(proxyIndex);
966 }
967
968 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
969 this, SLOT(selectAndScrollToCreatedItem()));
970 m_createdItemUrl = KUrl();
971 }
972
973 void DolphinView::showHoverInformation(const KFileItem& item)
974 {
975 emit requestItemInfo(item);
976 }
977
978 void DolphinView::clearHoverInformation()
979 {
980 emit requestItemInfo(KFileItem());
981 }
982
983 void DolphinView::slotDeleteFileFinished(KJob* job)
984 {
985 if (job->error() == 0) {
986 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
987 } else if (job->error() != KIO::ERR_USER_CANCELED) {
988 emit errorMessage(job->errorString());
989 }
990 }
991
992 void DolphinView::slotDirListerCompleted()
993 {
994 if (!m_expanderActive) {
995 slotLoadingCompleted();
996 }
997
998 if (!m_newFileNames.isEmpty()) {
999 // select all newly added items created by a paste operation or
1000 // a drag & drop operation, and clear the previous selection
1001 m_viewAccessor.itemView()->clearSelection();
1002 const int rowCount = m_viewAccessor.proxyModel()->rowCount();
1003 QItemSelection selection;
1004 for (int row = 0; row < rowCount; ++row) {
1005 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->index(row, 0);
1006 const QModelIndex dirIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1007 const KUrl url = m_viewAccessor.dirModel()->itemForIndex(dirIndex).url();
1008 if (m_newFileNames.contains(url.fileName())) {
1009 selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
1010 }
1011 }
1012 m_viewAccessor.itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
1013
1014 m_newFileNames.clear();
1015 }
1016 }
1017
1018 void DolphinView::slotLoadingCompleted()
1019 {
1020 m_expanderActive = false;
1021
1022 if (!m_activeItemUrl.isEmpty()) {
1023 // assure that the current item remains visible
1024 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_activeItemUrl);
1025 if (dirIndex.isValid()) {
1026 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1027 QAbstractItemView* view = m_viewAccessor.itemView();
1028 const bool clearSelection = !hasSelection();
1029 view->setCurrentIndex(proxyIndex);
1030 if (clearSelection) {
1031 view->clearSelection();
1032 }
1033 m_activeItemUrl.clear();
1034 }
1035 }
1036
1037 if (!m_selectedItems.isEmpty()) {
1038 const KUrl& baseUrl = url();
1039 KUrl url;
1040 QItemSelection newSelection;
1041 foreach(const KFileItem& item, m_selectedItems) {
1042 url = item.url().upUrl();
1043 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1044 QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
1045 newSelection.select(index, index);
1046 }
1047 }
1048 m_viewAccessor.itemView()->selectionModel()->select(newSelection,
1049 QItemSelectionModel::ClearAndSelect
1050 | QItemSelectionModel::Current);
1051 m_selectedItems.clear();
1052 }
1053
1054 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1055 // because the view might not be in its final state yet.
1056 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection);
1057
1058 emit finishedPathLoading(url());
1059 }
1060
1061 void DolphinView::slotRefreshItems()
1062 {
1063 if (m_assureVisibleCurrentIndex) {
1064 m_assureVisibleCurrentIndex = false;
1065 m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1066 }
1067 }
1068
1069 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1070 {
1071 if (!url.isValid()) {
1072 const QString location(url.pathOrUrl());
1073 if (location.isEmpty()) {
1074 emit errorMessage(i18nc("@info:status", "The location is empty."));
1075 } else {
1076 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1077 }
1078 return;
1079 }
1080
1081 KDirLister* dirLister = m_viewAccessor.dirLister();
1082 dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1083 }
1084
1085 void DolphinView::applyViewProperties()
1086 {
1087 const ViewProperties props(rootUrl());
1088
1089 const Mode mode = props.viewMode();
1090 if (m_mode != mode) {
1091 const int oldZoomLevel = m_viewModeController->zoomLevel();
1092
1093 m_mode = mode;
1094 createView();
1095 emit modeChanged();
1096
1097 updateZoomLevel(oldZoomLevel);
1098 }
1099 if (m_viewAccessor.itemView() == 0) {
1100 createView();
1101 }
1102
1103 Q_ASSERT(m_viewAccessor.itemView() != 0);
1104 Q_ASSERT(m_viewAccessor.itemDelegate() != 0);
1105
1106 const bool showHiddenFiles = props.showHiddenFiles();
1107 if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
1108 m_viewAccessor.dirLister()->setShowingDotFiles(showHiddenFiles);
1109 emit showHiddenFilesChanged();
1110 }
1111
1112 m_storedCategorizedSorting = props.categorizedSorting();
1113 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1114 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1115 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1116 emit categorizedSortingChanged();
1117 }
1118
1119 const DolphinView::Sorting sorting = props.sorting();
1120 if (sorting != m_viewAccessor.proxyModel()->sorting()) {
1121 m_viewAccessor.proxyModel()->setSorting(sorting);
1122 emit sortingChanged(sorting);
1123 }
1124
1125 const Qt::SortOrder sortOrder = props.sortOrder();
1126 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1127 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1128 emit sortOrderChanged(sortOrder);
1129 }
1130
1131 const bool sortFoldersFirst = props.sortFoldersFirst();
1132 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1133 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1134 emit sortFoldersFirstChanged(sortFoldersFirst);
1135 }
1136
1137 KFileItemDelegate::InformationList info = props.additionalInfo();
1138 if (info != m_viewAccessor.itemDelegate()->showInformation()) {
1139 m_viewAccessor.itemDelegate()->setShowInformation(info);
1140 emit additionalInfoChanged();
1141 }
1142
1143 const bool showPreview = props.showPreview();
1144 if (showPreview != m_showPreview) {
1145 m_showPreview = showPreview;
1146 const int oldZoomLevel = m_viewModeController->zoomLevel();
1147 emit showPreviewChanged();
1148
1149 // Enabling or disabling the preview might change the icon size of the view.
1150 // As the view does not emit a signal when the icon size has been changed,
1151 // the used zoom level of the controller must be adjusted manually:
1152 updateZoomLevel(oldZoomLevel);
1153 }
1154 }
1155
1156 void DolphinView::createView()
1157 {
1158 deleteView();
1159
1160 Q_ASSERT(m_viewAccessor.itemView() == 0);
1161 Q_ASSERT(m_dolphinViewController->itemView() == 0);
1162 m_viewAccessor.createView(this, m_dolphinViewController, m_viewModeController, m_mode);
1163
1164 QAbstractItemView* view = m_viewAccessor.itemView();
1165 Q_ASSERT(view != 0);
1166 view->installEventFilter(this);
1167 view->viewport()->installEventFilter(this);
1168
1169 m_dolphinViewController->setItemView(view);
1170
1171 const int zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(view->iconSize());
1172 m_viewModeController->setZoomLevel(zoomLevel);
1173
1174 connectViewAccessor();
1175
1176 setFocusProxy(m_viewAccessor.layoutTarget());
1177 m_topLayout->insertWidget(1, m_viewAccessor.layoutTarget());
1178 }
1179
1180 void DolphinView::deleteView()
1181 {
1182 QAbstractItemView* view = m_viewAccessor.itemView();
1183 Q_ASSERT((m_dolphinViewController->itemView() == 0) || (m_dolphinViewController->itemView() == view));
1184 m_dolphinViewController->setItemView(0);
1185
1186 if (view != 0) {
1187 disconnectViewAccessor();
1188
1189 // It's important to set the keyboard focus to the parent
1190 // before deleting the view: Otherwise when having a split
1191 // view the other view will get the focus and will request
1192 // an activation (see DolphinView::eventFilter()).
1193 setFocusProxy(0);
1194 setFocus();
1195
1196 m_viewModeController->disconnect(view);
1197
1198 m_viewAccessor.deleteView();
1199 }
1200 }
1201
1202 void DolphinView::pasteToUrl(const KUrl& url)
1203 {
1204 addNewFileNames(QApplication::clipboard()->mimeData());
1205 KonqOperations::doPaste(this, url);
1206 }
1207
1208 void DolphinView::updateZoomLevel(int oldZoomLevel)
1209 {
1210 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1211 if (oldZoomLevel != newZoomLevel) {
1212 m_viewModeController->setZoomLevel(newZoomLevel);
1213 emit zoomLevelChanged(newZoomLevel);
1214 }
1215 }
1216
1217 KUrl::List DolphinView::simplifiedSelectedUrls() const
1218 {
1219 KUrl::List urls;
1220
1221 const KFileItemList items = selectedItems();
1222 foreach (const KFileItem &item, items) {
1223 urls.append(item.url());
1224 }
1225
1226
1227 if (itemsExpandable()) {
1228 urls = KDirModel::simplifiedUrlList(urls);
1229 }
1230
1231 return urls;
1232 }
1233
1234 QMimeData* DolphinView::selectionMimeData() const
1235 {
1236 const QAbstractItemView* view = m_viewAccessor.itemView();
1237 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1238 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
1239 return m_viewAccessor.dirModel()->mimeData(selection.indexes());
1240 }
1241
1242 void DolphinView::addNewFileNames(const QMimeData* mimeData)
1243 {
1244 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1245 foreach (const KUrl& url, urls) {
1246 m_newFileNames.insert(url.fileName());
1247 }
1248 }
1249
1250 QItemSelection DolphinView::childrenMatchingPattern(const QModelIndex& parent, const QRegExp& pattern) const
1251 {
1252 QItemSelection matchingIndexes;
1253 const DolphinSortFilterProxyModel* proxyModel = m_viewAccessor.proxyModel();
1254 const DolphinModel* dolphinModel = m_viewAccessor.dirModel();
1255
1256 const int rowCount = proxyModel->rowCount(parent);
1257
1258 for (int row = 0; row < rowCount; ++row) {
1259 QModelIndex index = proxyModel->index(row, 0, parent);
1260 QModelIndex sourceIndex = proxyModel->mapToSource(index);
1261
1262 if (sourceIndex.isValid() && pattern.exactMatch(dolphinModel->data(sourceIndex).toString())) {
1263 matchingIndexes += QItemSelectionRange(index);
1264 }
1265
1266 if (proxyModel->hasChildren(index)) {
1267 matchingIndexes += childrenMatchingPattern(index, pattern);
1268 }
1269 }
1270
1271 return matchingIndexes;
1272 }
1273
1274 void DolphinView::connectViewAccessor()
1275 {
1276 KDirLister* dirLister = m_viewAccessor.dirLister();
1277 connect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
1278 this, SLOT(slotRedirection(KUrl,KUrl)));
1279 connect(dirLister, SIGNAL(started(KUrl)),
1280 this, SIGNAL(startedPathLoading(KUrl)));
1281 connect(dirLister, SIGNAL(completed()),
1282 this, SLOT(slotDirListerCompleted()));
1283 connect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
1284 this, SLOT(slotRefreshItems()));
1285
1286 QAbstractItemView* view = m_viewAccessor.itemView();
1287 connect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
1288 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
1289 }
1290
1291 void DolphinView::disconnectViewAccessor()
1292 {
1293 KDirLister* dirLister = m_viewAccessor.dirLister();
1294 disconnect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
1295 this, SLOT(slotRedirection(KUrl,KUrl)));
1296 disconnect(dirLister, SIGNAL(started(KUrl)),
1297 this, SIGNAL(startedPathLoading(KUrl)));
1298 disconnect(dirLister, SIGNAL(completed()),
1299 this, SLOT(slotDirListerCompleted()));
1300 disconnect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
1301 this, SLOT(slotRefreshItems()));
1302
1303 QAbstractItemView* view = m_viewAccessor.itemView();
1304 disconnect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
1305 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
1306 }
1307
1308 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) :
1309 m_rootUrl(),
1310 m_iconsView(0),
1311 m_detailsView(0),
1312 m_columnsContainer(0),
1313 m_proxyModel(proxyModel),
1314 m_dragSource(0)
1315 {
1316 }
1317
1318 DolphinView::ViewAccessor::~ViewAccessor()
1319 {
1320 delete m_dragSource;
1321 m_dragSource = 0;
1322 }
1323
1324 void DolphinView::ViewAccessor::createView(QWidget* parent,
1325 DolphinViewController* dolphinViewController,
1326 const ViewModeController* viewModeController,
1327 Mode mode)
1328 {
1329 Q_ASSERT(itemView() == 0);
1330
1331 switch (mode) {
1332 case IconsView:
1333 m_iconsView = new DolphinIconsView(parent,
1334 dolphinViewController,
1335 viewModeController,
1336 m_proxyModel);
1337 break;
1338
1339 case DetailsView:
1340 m_detailsView = new DolphinDetailsView(parent,
1341 dolphinViewController,
1342 viewModeController,
1343 m_proxyModel);
1344 break;
1345
1346 case ColumnView:
1347 m_columnsContainer = new DolphinColumnViewContainer(parent,
1348 dolphinViewController,
1349 viewModeController);
1350 if (!m_rootUrl.isEmpty() && m_rootUrl.isParentOf(viewModeController->url())) {
1351 // The column-view must show several columns starting with m_rootUrl as
1352 // first column and viewModeController->url() as last column.
1353 m_columnsContainer->showColumn(m_rootUrl);
1354 m_columnsContainer->showColumn(viewModeController->url());
1355 }
1356 break;
1357
1358 default:
1359 Q_ASSERT(false);
1360 }
1361 }
1362
1363 void DolphinView::ViewAccessor::deleteView()
1364 {
1365 if (m_columnsContainer != 0) {
1366 m_columnsContainer->close();
1367 m_columnsContainer->disconnect();
1368 m_columnsContainer->deleteLater();
1369 m_columnsContainer = 0;
1370 } else {
1371 QAbstractItemView* view = itemView();
1372 if (view != 0) {
1373 view->close();
1374 view->disconnect();
1375
1376 if (DragAndDropHelper::instance().isDragSource(view)) {
1377 // The view is a drag source (the feature "Open folders
1378 // during drag operations" is used). Deleting the view
1379 // during an ongoing drag operation is not allowed, so
1380 // this will postponed.
1381 if (m_dragSource != 0) {
1382 // the old stored view is obviously not the drag source anymore
1383 m_dragSource->deleteLater();
1384 m_dragSource = 0;
1385 }
1386 view->hide();
1387 m_dragSource = view;
1388 } else {
1389 view->deleteLater();
1390 view = 0;
1391 }
1392 }
1393
1394 m_iconsView = 0;
1395 m_detailsView = 0;
1396 }
1397 }
1398
1399 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl& url)
1400 {
1401 if (m_columnsContainer != 0) {
1402 m_columnsContainer->showColumn(url);
1403 }
1404 }
1405
1406 QAbstractItemView* DolphinView::ViewAccessor::itemView() const
1407 {
1408 if (m_iconsView != 0) {
1409 return m_iconsView;
1410 }
1411
1412 if (m_detailsView != 0) {
1413 return m_detailsView;
1414 }
1415
1416 if (m_columnsContainer != 0) {
1417 return m_columnsContainer->activeColumn();
1418 }
1419
1420 return 0;
1421 }
1422
1423 KFileItemDelegate* DolphinView::ViewAccessor::itemDelegate() const
1424 {
1425 return static_cast<KFileItemDelegate*>(itemView()->itemDelegate());
1426 }
1427
1428 QWidget* DolphinView::ViewAccessor::layoutTarget() const
1429 {
1430 if (m_columnsContainer != 0) {
1431 return m_columnsContainer;
1432 }
1433 return itemView();
1434 }
1435
1436 void DolphinView::ViewAccessor::setRootUrl(const KUrl& rootUrl)
1437 {
1438 m_rootUrl = rootUrl;
1439 }
1440
1441 KUrl DolphinView::ViewAccessor::rootUrl() const
1442 {
1443 return (m_columnsContainer != 0) ? m_columnsContainer->rootUrl() : m_rootUrl;
1444 }
1445
1446 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1447 {
1448 return m_iconsView != 0;
1449 }
1450
1451 bool DolphinView::ViewAccessor::itemsExpandable() const
1452 {
1453 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1454 }
1455
1456 QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
1457 {
1458 if (m_detailsView != 0) {
1459 return m_detailsView->expandedUrls();
1460 }
1461
1462 return QSet<KUrl>();
1463 }
1464
1465 const DolphinDetailsViewExpander* DolphinView::ViewAccessor::setExpandedUrls(const QSet<KUrl>& urlsToExpand)
1466 {
1467 if ((m_detailsView != 0) && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
1468 // Check if another expander is already active and stop it if necessary.
1469 if(!m_detailsViewExpander.isNull()) {
1470 m_detailsViewExpander->stop();
1471 }
1472
1473 m_detailsViewExpander = new DolphinDetailsViewExpander(m_detailsView, urlsToExpand);
1474 return m_detailsViewExpander;
1475 }
1476 else {
1477 return 0;
1478 }
1479 }
1480
1481 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1482 {
1483 // the details view requires no reloading of the directory, as it maps
1484 // the file item delegate info to its columns internally
1485 return m_detailsView != 0;
1486 }
1487
1488 DolphinModel* DolphinView::ViewAccessor::dirModel() const
1489 {
1490 return static_cast<DolphinModel*>(proxyModel()->sourceModel());
1491 }
1492
1493 DolphinSortFilterProxyModel* DolphinView::ViewAccessor::proxyModel() const
1494 {
1495 if (m_columnsContainer != 0) {
1496 return static_cast<DolphinSortFilterProxyModel*>(m_columnsContainer->activeColumn()->model());
1497 }
1498 return m_proxyModel;
1499 }
1500
1501 KDirLister* DolphinView::ViewAccessor::dirLister() const
1502 {
1503 return dirModel()->dirLister();
1504 }
1505
1506 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1507 {
1508 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
1509 emit redirection(oldUrl, newUrl);
1510 m_viewModeController->redirectToUrl(newUrl); // #186947
1511 }
1512 }
1513
1514 void DolphinView::restoreContentsPosition()
1515 {
1516 if (!m_restoredContentsPosition.isNull()) {
1517 const int x = m_restoredContentsPosition.x();
1518 const int y = m_restoredContentsPosition.y();
1519 m_restoredContentsPosition = QPoint();
1520
1521 QAbstractItemView* view = m_viewAccessor.itemView();
1522 Q_ASSERT(view != 0);
1523 view->horizontalScrollBar()->setValue(x);
1524 view->verticalScrollBar()->setValue(y);
1525 }
1526 }
1527
1528 #include "dolphinview.moc"