]> cloud.milkyroute.net Git - dolphin.git/blob - src/views/dolphinview.cpp
Also reconnect signals for the directory lister when changing the view or the 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 loadDirectory(url);
532
533 // When changing the URL there is no need to keep the version
534 // data of the previous URL.
535 m_viewAccessor.dirModel()->clearVersionData();
536
537 emit startedPathLoading(url);
538
539 // Reconnect to the (probably) new selection model and directory lister
540 connectViewAccessor();
541
542 if (hadSelection || hasSelection()) {
543 emitSelectionChangedSignal();
544 }
545 }
546
547 void DolphinView::selectAll()
548 {
549 m_viewAccessor.itemView()->selectAll();
550 }
551
552 void DolphinView::invertSelection()
553 {
554 QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel();
555 const QAbstractItemModel* itemModel = selectionModel->model();
556
557 const QModelIndex topLeft = itemModel->index(0, 0);
558 const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1,
559 itemModel->columnCount() - 1);
560
561 const QItemSelection selection(topLeft, bottomRight);
562 selectionModel->select(selection, QItemSelectionModel::Toggle);
563 }
564
565 void DolphinView::clearSelection()
566 {
567 m_viewAccessor.itemView()->clearSelection();
568 }
569
570 void DolphinView::renameSelectedItems()
571 {
572 KFileItemList items = selectedItems();
573 const int itemCount = items.count();
574 if (itemCount < 1) {
575 return;
576 }
577
578 if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
579 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
580 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
581 m_viewAccessor.itemView()->edit(proxyIndex);
582 } else {
583 RenameDialog* dialog = new RenameDialog(this, items);
584 dialog->setAttribute(Qt::WA_DeleteOnClose);
585 dialog->show();
586 dialog->raise();
587 dialog->activateWindow();
588 }
589 }
590
591 void DolphinView::trashSelectedItems()
592 {
593 const KUrl::List list = simplifiedSelectedUrls();
594 KonqOperations::del(this, KonqOperations::TRASH, list);
595 }
596
597 void DolphinView::deleteSelectedItems()
598 {
599 const KUrl::List list = simplifiedSelectedUrls();
600 const bool del = KonqOperations::askDeleteConfirmation(list,
601 KonqOperations::DEL,
602 KonqOperations::DEFAULT_CONFIRMATION,
603 this);
604
605 if (del) {
606 KIO::Job* job = KIO::del(list);
607 connect(job, SIGNAL(result(KJob*)),
608 this, SLOT(slotDeleteFileFinished(KJob*)));
609 }
610 }
611
612 void DolphinView::cutSelectedItems()
613 {
614 QMimeData* mimeData = selectionMimeData();
615 KonqMimeData::addIsCutSelection(mimeData, true);
616 QApplication::clipboard()->setMimeData(mimeData);
617 }
618
619 void DolphinView::copySelectedItems()
620 {
621 QMimeData* mimeData = selectionMimeData();
622 QApplication::clipboard()->setMimeData(mimeData);
623 }
624
625 void DolphinView::paste()
626 {
627 pasteToUrl(url());
628 }
629
630 void DolphinView::pasteIntoFolder()
631 {
632 const KFileItemList items = selectedItems();
633 if ((items.count() == 1) && items.first().isDir()) {
634 pasteToUrl(items.first().url());
635 }
636 }
637
638 void DolphinView::setShowPreview(bool show)
639 {
640 if (m_showPreview == show) {
641 return;
642 }
643
644 const KUrl viewPropsUrl = rootUrl();
645 ViewProperties props(viewPropsUrl);
646 props.setShowPreview(show);
647
648 m_showPreview = show;
649 const int oldZoomLevel = m_viewModeController->zoomLevel();
650 emit showPreviewChanged();
651
652 // Enabling or disabling the preview might change the icon size of the view.
653 // As the view does not emit a signal when the icon size has been changed,
654 // the used zoom level of the controller must be adjusted manually:
655 updateZoomLevel(oldZoomLevel);
656 }
657
658 void DolphinView::setShowHiddenFiles(bool show)
659 {
660 if (m_viewAccessor.dirLister()->showingDotFiles() == show) {
661 return;
662 }
663
664 const KUrl viewPropsUrl = rootUrl();
665 ViewProperties props(viewPropsUrl);
666 props.setShowHiddenFiles(show);
667
668 m_viewAccessor.dirLister()->setShowingDotFiles(show);
669 emit showHiddenFilesChanged();
670 }
671
672 void DolphinView::setCategorizedSorting(bool categorized)
673 {
674 if (categorized == categorizedSorting()) {
675 return;
676 }
677
678 // setCategorizedSorting(true) may only get invoked
679 // if the view supports categorized sorting
680 Q_ASSERT(!categorized || supportsCategorizedSorting());
681
682 ViewProperties props(rootUrl());
683 props.setCategorizedSorting(categorized);
684 props.save();
685
686 m_storedCategorizedSorting = categorized;
687 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
688
689 emit categorizedSortingChanged();
690 }
691
692 void DolphinView::mouseReleaseEvent(QMouseEvent* event)
693 {
694 QWidget::mouseReleaseEvent(event);
695 setActive(true);
696 }
697
698 bool DolphinView::eventFilter(QObject* watched, QEvent* event)
699 {
700 switch (event->type()) {
701 case QEvent::FocusIn:
702 if (watched == m_viewAccessor.itemView()) {
703 m_dolphinViewController->requestActivation();
704 }
705 break;
706
707 case QEvent::DragEnter:
708 if (watched == m_viewAccessor.itemView()->viewport()) {
709 setActive(true);
710 }
711 break;
712
713 case QEvent::KeyPress:
714 if (watched == m_viewAccessor.itemView()) {
715 // clear the selection when Escape has been pressed
716 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
717 if (keyEvent->key() == Qt::Key_Escape) {
718 clearSelection();
719 }
720 }
721 break;
722
723 case QEvent::Wheel:
724 if (watched == m_viewAccessor.itemView()->viewport()) {
725 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
726 // (the user is probably trying to scroll during a selection in that case)
727 QWheelEvent* wheelEvent = static_cast<QWheelEvent*>(event);
728 if (wheelEvent->modifiers() & Qt::ControlModifier && !(wheelEvent->buttons() & Qt::LeftButton)) {
729 const int delta = wheelEvent->delta();
730 const int level = zoomLevel();
731 if (delta > 0) {
732 setZoomLevel(level + 1);
733 } else if (delta < 0) {
734 setZoomLevel(level - 1);
735 }
736 return true;
737 }
738 }
739 break;
740
741 default:
742 break;
743 }
744
745 return QWidget::eventFilter(watched, event);
746 }
747
748 void DolphinView::activate()
749 {
750 setActive(true);
751 }
752
753 void DolphinView::triggerItem(const KFileItem& item)
754 {
755 const Qt::KeyboardModifiers modifier = QApplication::keyboardModifiers();
756 if ((modifier & Qt::ShiftModifier) || (modifier & Qt::ControlModifier)) {
757 // items are selected by the user, hence don't trigger the
758 // item specified by 'index'
759 return;
760 }
761
762 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
763 if (item.isNull() || m_isContextMenuOpen) {
764 return;
765 }
766
767 emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart
768 }
769
770 void DolphinView::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected)
771 {
772 const int count = selectedItemsCount();
773 const bool selectionStateChanged = ((count > 0) && (selected.count() == count)) ||
774 ((count == 0) && !deselected.isEmpty());
775
776 // If nothing has been selected before and something got selected (or if something
777 // was selected before and now nothing is selected) the selectionChangedSignal must
778 // be emitted asynchronously as fast as possible to update the edit-actions.
779 m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300);
780 m_selectionChangedTimer->start();
781 }
782
783 void DolphinView::emitSelectionChangedSignal()
784 {
785 m_selectionChangedTimer->stop();
786 emit selectionChanged(selectedItems());
787 }
788
789 void DolphinView::openContextMenu(const QPoint& pos,
790 const QList<QAction*>& customActions)
791 {
792 KFileItem item;
793 const QModelIndex index = m_viewAccessor.itemView()->indexAt(pos);
794 if (index.isValid() && (index.column() == DolphinModel::Name)) {
795 const QModelIndex dolphinModelIndex = m_viewAccessor.proxyModel()->mapToSource(index);
796 item = m_viewAccessor.dirModel()->itemForIndex(dolphinModelIndex);
797 }
798
799 m_isContextMenuOpen = true; // TODO: workaround for Qt-issue 207192
800 emit requestContextMenu(item, url(), customActions);
801 m_isContextMenuOpen = false;
802 }
803
804 void DolphinView::dropUrls(const KFileItem& destItem,
805 const KUrl& destPath,
806 QDropEvent* event)
807 {
808 addNewFileNames(event->mimeData());
809 DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
810 }
811
812 void DolphinView::updateSorting(DolphinView::Sorting sorting)
813 {
814 ViewProperties props(rootUrl());
815 props.setSorting(sorting);
816
817 m_viewAccessor.proxyModel()->setSorting(sorting);
818
819 emit sortingChanged(sorting);
820 }
821
822 void DolphinView::updateSortOrder(Qt::SortOrder order)
823 {
824 ViewProperties props(rootUrl());
825 props.setSortOrder(order);
826
827 m_viewAccessor.proxyModel()->setSortOrder(order);
828
829 emit sortOrderChanged(order);
830 }
831
832 void DolphinView::updateSortFoldersFirst(bool foldersFirst)
833 {
834 ViewProperties props(rootUrl());
835 props.setSortFoldersFirst(foldersFirst);
836
837 m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
838
839 emit sortFoldersFirstChanged(foldersFirst);
840 }
841
842 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList& info)
843 {
844 ViewProperties props(rootUrl());
845 props.setAdditionalInfo(info);
846 props.save();
847
848 m_viewAccessor.itemDelegate()->setShowInformation(info);
849
850 emit additionalInfoChanged();
851 }
852
853 void DolphinView::updateAdditionalInfoActions(KActionCollection* collection)
854 {
855 const AdditionalInfoAccessor& infoAccessor = AdditionalInfoAccessor::instance();
856
857 const KFileItemDelegate::InformationList checkedInfo = m_viewAccessor.itemDelegate()->showInformation();
858 const KFileItemDelegate::InformationList infoKeys = infoAccessor.keys();
859
860 const bool enable = (m_mode == DolphinView::DetailsView) ||
861 (m_mode == DolphinView::IconsView);
862
863 foreach (const KFileItemDelegate::Information& info, infoKeys) {
864 const QString name = infoAccessor.actionCollectionName(info, AdditionalInfoAccessor::AdditionalInfoType);
865 QAction* action = collection->action(name);
866 Q_ASSERT(action != 0);
867 action->setEnabled(enable);
868 action->setChecked(checkedInfo.contains(info));
869 }
870 }
871
872 QPair<bool, QString> DolphinView::pasteInfo() const
873 {
874 return KonqOperations::pasteInfo(url());
875 }
876
877 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles)
878 {
879 m_tabsForFiles = tabsForFiles;
880 }
881
882 bool DolphinView::isTabsForFilesEnabled() const
883 {
884 return m_tabsForFiles;
885 }
886
887 bool DolphinView::itemsExpandable() const
888 {
889 return m_viewAccessor.itemsExpandable();
890 }
891
892 void DolphinView::restoreState(QDataStream& stream)
893 {
894 // Restore the URL of the current item that had the keyboard focus
895 stream >> m_activeItemUrl;
896
897 // Restore the root URL
898 KUrl rootUrl;
899 stream >> rootUrl;
900 m_viewAccessor.setRootUrl(rootUrl);
901
902 // Restore the view position
903 stream >> m_restoredContentsPosition;
904
905 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
906 QSet<KUrl> urlsToExpand;
907 stream >> urlsToExpand;
908 const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
909 if (expander != 0) {
910 m_expanderActive = true;
911 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
912 }
913 else {
914 m_expanderActive = false;
915 }
916 }
917
918 void DolphinView::saveState(QDataStream& stream)
919 {
920 // Save the URL of the current item that has the keyboard focus
921 KFileItem currentItem;
922 const QAbstractItemView* view = m_viewAccessor.itemView();
923
924 if (view != 0) {
925 const QModelIndex proxyIndex = view->currentIndex();
926 const QModelIndex dirModelIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
927 currentItem = m_viewAccessor.dirModel()->itemForIndex(dirModelIndex);
928 }
929
930 KUrl currentItemUrl;
931 if (!currentItem.isNull()) {
932 currentItemUrl = currentItem.url();
933 }
934
935 stream << currentItemUrl;
936
937 // Save the root URL
938 stream << m_viewAccessor.rootUrl();
939
940 // Save view position
941 const int x = view->horizontalScrollBar()->value();
942 const int y = view->verticalScrollBar()->value();
943 stream << QPoint(x, y);
944
945 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
946 stream << m_viewAccessor.expandedUrls();
947 }
948
949 bool DolphinView::hasSelection() const
950 {
951 const QAbstractItemView* view = m_viewAccessor.itemView();
952 return (view != 0) && view->selectionModel()->hasSelection();
953 }
954
955 void DolphinView::observeCreatedItem(const KUrl& url)
956 {
957 m_createdItemUrl = url;
958 connect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
959 this, SLOT(selectAndScrollToCreatedItem()));
960 }
961
962 void DolphinView::selectAndScrollToCreatedItem()
963 {
964 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
965 if (dirIndex.isValid()) {
966 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
967 m_viewAccessor.itemView()->setCurrentIndex(proxyIndex);
968 }
969
970 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(const QModelIndex&, int, int)),
971 this, SLOT(selectAndScrollToCreatedItem()));
972 m_createdItemUrl = KUrl();
973 }
974
975 void DolphinView::showHoverInformation(const KFileItem& item)
976 {
977 emit requestItemInfo(item);
978 }
979
980 void DolphinView::clearHoverInformation()
981 {
982 emit requestItemInfo(KFileItem());
983 }
984
985 void DolphinView::slotDeleteFileFinished(KJob* job)
986 {
987 if (job->error() == 0) {
988 emit operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
989 } else if (job->error() != KIO::ERR_USER_CANCELED) {
990 emit errorMessage(job->errorString());
991 }
992 }
993
994 void DolphinView::slotDirListerCompleted()
995 {
996 if (!m_expanderActive) {
997 slotLoadingCompleted();
998 }
999
1000 if (!m_newFileNames.isEmpty()) {
1001 // select all newly added items created by a paste operation or
1002 // a drag & drop operation, and clear the previous selection
1003 m_viewAccessor.itemView()->clearSelection();
1004 const int rowCount = m_viewAccessor.proxyModel()->rowCount();
1005 QItemSelection selection;
1006 for (int row = 0; row < rowCount; ++row) {
1007 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->index(row, 0);
1008 const QModelIndex dirIndex = m_viewAccessor.proxyModel()->mapToSource(proxyIndex);
1009 const KUrl url = m_viewAccessor.dirModel()->itemForIndex(dirIndex).url();
1010 if (m_newFileNames.contains(url.fileName())) {
1011 selection.merge(QItemSelection(proxyIndex, proxyIndex), QItemSelectionModel::Select);
1012 }
1013 }
1014 m_viewAccessor.itemView()->selectionModel()->select(selection, QItemSelectionModel::Select);
1015
1016 m_newFileNames.clear();
1017 }
1018 }
1019
1020 void DolphinView::slotLoadingCompleted()
1021 {
1022 m_expanderActive = false;
1023
1024 if (!m_activeItemUrl.isEmpty()) {
1025 // assure that the current item remains visible
1026 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_activeItemUrl);
1027 if (dirIndex.isValid()) {
1028 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1029 QAbstractItemView* view = m_viewAccessor.itemView();
1030 const bool clearSelection = !hasSelection();
1031 view->setCurrentIndex(proxyIndex);
1032 if (clearSelection) {
1033 view->clearSelection();
1034 }
1035 m_activeItemUrl.clear();
1036 }
1037 }
1038
1039 if (!m_selectedItems.isEmpty()) {
1040 const KUrl& baseUrl = url();
1041 KUrl url;
1042 QItemSelection newSelection;
1043 foreach(const KFileItem& item, m_selectedItems) {
1044 url = item.url().upUrl();
1045 if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) {
1046 QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item));
1047 newSelection.select(index, index);
1048 }
1049 }
1050 m_viewAccessor.itemView()->selectionModel()->select(newSelection,
1051 QItemSelectionModel::ClearAndSelect
1052 | QItemSelectionModel::Current);
1053 m_selectedItems.clear();
1054 }
1055
1056 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1057 // because the view might not be in its final state yet.
1058 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection);
1059
1060 emit finishedPathLoading(url());
1061 }
1062
1063 void DolphinView::slotRefreshItems()
1064 {
1065 if (m_assureVisibleCurrentIndex) {
1066 m_assureVisibleCurrentIndex = false;
1067 m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1068 }
1069 }
1070
1071 void DolphinView::loadDirectory(const KUrl& url, bool reload)
1072 {
1073 if (!url.isValid()) {
1074 const QString location(url.pathOrUrl());
1075 if (location.isEmpty()) {
1076 emit errorMessage(i18nc("@info:status", "The location is empty."));
1077 } else {
1078 emit errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location));
1079 }
1080 return;
1081 }
1082
1083 KDirLister* dirLister = m_viewAccessor.dirLister();
1084 dirLister->openUrl(url, reload ? KDirLister::Reload : KDirLister::NoFlags);
1085 }
1086
1087 void DolphinView::applyViewProperties()
1088 {
1089 const ViewProperties props(rootUrl());
1090
1091 const Mode mode = props.viewMode();
1092 if (m_mode != mode) {
1093 const int oldZoomLevel = m_viewModeController->zoomLevel();
1094
1095 m_mode = mode;
1096 createView();
1097 emit modeChanged();
1098
1099 updateZoomLevel(oldZoomLevel);
1100 }
1101 if (m_viewAccessor.itemView() == 0) {
1102 createView();
1103 }
1104
1105 Q_ASSERT(m_viewAccessor.itemView() != 0);
1106 Q_ASSERT(m_viewAccessor.itemDelegate() != 0);
1107
1108 const bool showHiddenFiles = props.showHiddenFiles();
1109 if (showHiddenFiles != m_viewAccessor.dirLister()->showingDotFiles()) {
1110 m_viewAccessor.dirLister()->setShowingDotFiles(showHiddenFiles);
1111 emit showHiddenFilesChanged();
1112 }
1113
1114 m_storedCategorizedSorting = props.categorizedSorting();
1115 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1116 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1117 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1118 emit categorizedSortingChanged();
1119 }
1120
1121 const DolphinView::Sorting sorting = props.sorting();
1122 if (sorting != m_viewAccessor.proxyModel()->sorting()) {
1123 m_viewAccessor.proxyModel()->setSorting(sorting);
1124 emit sortingChanged(sorting);
1125 }
1126
1127 const Qt::SortOrder sortOrder = props.sortOrder();
1128 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1129 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1130 emit sortOrderChanged(sortOrder);
1131 }
1132
1133 const bool sortFoldersFirst = props.sortFoldersFirst();
1134 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1135 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1136 emit sortFoldersFirstChanged(sortFoldersFirst);
1137 }
1138
1139 KFileItemDelegate::InformationList info = props.additionalInfo();
1140 if (info != m_viewAccessor.itemDelegate()->showInformation()) {
1141 m_viewAccessor.itemDelegate()->setShowInformation(info);
1142 emit additionalInfoChanged();
1143 }
1144
1145 const bool showPreview = props.showPreview();
1146 if (showPreview != m_showPreview) {
1147 m_showPreview = showPreview;
1148 const int oldZoomLevel = m_viewModeController->zoomLevel();
1149 emit showPreviewChanged();
1150
1151 // Enabling or disabling the preview might change the icon size of the view.
1152 // As the view does not emit a signal when the icon size has been changed,
1153 // the used zoom level of the controller must be adjusted manually:
1154 updateZoomLevel(oldZoomLevel);
1155 }
1156 }
1157
1158 void DolphinView::createView()
1159 {
1160 deleteView();
1161
1162 Q_ASSERT(m_viewAccessor.itemView() == 0);
1163 Q_ASSERT(m_dolphinViewController->itemView() == 0);
1164 m_viewAccessor.createView(this, m_dolphinViewController, m_viewModeController, m_mode);
1165
1166 QAbstractItemView* view = m_viewAccessor.itemView();
1167 Q_ASSERT(view != 0);
1168 view->installEventFilter(this);
1169 view->viewport()->installEventFilter(this);
1170
1171 m_dolphinViewController->setItemView(view);
1172
1173 const int zoomLevel = ZoomLevelInfo::zoomLevelForIconSize(view->iconSize());
1174 m_viewModeController->setZoomLevel(zoomLevel);
1175
1176 connectViewAccessor();
1177
1178 setFocusProxy(m_viewAccessor.layoutTarget());
1179 m_topLayout->insertWidget(1, m_viewAccessor.layoutTarget());
1180 }
1181
1182 void DolphinView::deleteView()
1183 {
1184 QAbstractItemView* view = m_viewAccessor.itemView();
1185 Q_ASSERT((m_dolphinViewController->itemView() == 0) || (m_dolphinViewController->itemView() == view));
1186 m_dolphinViewController->setItemView(0);
1187
1188 if (view != 0) {
1189 disconnectViewAccessor();
1190
1191 // It's important to set the keyboard focus to the parent
1192 // before deleting the view: Otherwise when having a split
1193 // view the other view will get the focus and will request
1194 // an activation (see DolphinView::eventFilter()).
1195 setFocusProxy(0);
1196 setFocus();
1197
1198 m_viewModeController->disconnect(view);
1199
1200 m_viewAccessor.deleteView();
1201 }
1202 }
1203
1204 void DolphinView::pasteToUrl(const KUrl& url)
1205 {
1206 addNewFileNames(QApplication::clipboard()->mimeData());
1207 KonqOperations::doPaste(this, url);
1208 }
1209
1210 void DolphinView::updateZoomLevel(int oldZoomLevel)
1211 {
1212 const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1213 if (oldZoomLevel != newZoomLevel) {
1214 m_viewModeController->setZoomLevel(newZoomLevel);
1215 emit zoomLevelChanged(newZoomLevel);
1216 }
1217 }
1218
1219 KUrl::List DolphinView::simplifiedSelectedUrls() const
1220 {
1221 KUrl::List urls;
1222
1223 const KFileItemList items = selectedItems();
1224 foreach (const KFileItem &item, items) {
1225 urls.append(item.url());
1226 }
1227
1228
1229 if (itemsExpandable()) {
1230 urls = KDirModel::simplifiedUrlList(urls);
1231 }
1232
1233 return urls;
1234 }
1235
1236 QMimeData* DolphinView::selectionMimeData() const
1237 {
1238 const QAbstractItemView* view = m_viewAccessor.itemView();
1239 Q_ASSERT((view != 0) && (view->selectionModel() != 0));
1240 const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection());
1241 return m_viewAccessor.dirModel()->mimeData(selection.indexes());
1242 }
1243
1244 void DolphinView::addNewFileNames(const QMimeData* mimeData)
1245 {
1246 const KUrl::List urls = KUrl::List::fromMimeData(mimeData);
1247 foreach (const KUrl& url, urls) {
1248 m_newFileNames.insert(url.fileName());
1249 }
1250 }
1251
1252 QItemSelection DolphinView::childrenMatchingPattern(const QModelIndex& parent, const QRegExp& pattern) const
1253 {
1254 QItemSelection matchingIndexes;
1255 const DolphinSortFilterProxyModel* proxyModel = m_viewAccessor.proxyModel();
1256 const DolphinModel* dolphinModel = m_viewAccessor.dirModel();
1257
1258 const int rowCount = proxyModel->rowCount(parent);
1259
1260 for (int row = 0; row < rowCount; ++row) {
1261 QModelIndex index = proxyModel->index(row, 0, parent);
1262 QModelIndex sourceIndex = proxyModel->mapToSource(index);
1263
1264 if (sourceIndex.isValid() && pattern.exactMatch(dolphinModel->data(sourceIndex).toString())) {
1265 matchingIndexes += QItemSelectionRange(index);
1266 }
1267
1268 if (proxyModel->hasChildren(index)) {
1269 matchingIndexes += childrenMatchingPattern(index, pattern);
1270 }
1271 }
1272
1273 return matchingIndexes;
1274 }
1275
1276 void DolphinView::connectViewAccessor()
1277 {
1278 KDirLister* dirLister = m_viewAccessor.dirLister();
1279 connect(dirLister, SIGNAL(redirection(KUrl,KUrl)),
1280 this, SLOT(slotRedirection(KUrl,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(completed()),
1297 this, SLOT(slotDirListerCompleted()));
1298 disconnect(dirLister, SIGNAL(refreshItems(const QList<QPair<KFileItem,KFileItem>>&)),
1299 this, SLOT(slotRefreshItems()));
1300
1301 QAbstractItemView* view = m_viewAccessor.itemView();
1302 disconnect(view->selectionModel(), SIGNAL(selectionChanged(QItemSelection, QItemSelection)),
1303 this, SLOT(slotSelectionChanged(QItemSelection, QItemSelection)));
1304 }
1305
1306 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) :
1307 m_rootUrl(),
1308 m_iconsView(0),
1309 m_detailsView(0),
1310 m_columnsContainer(0),
1311 m_proxyModel(proxyModel),
1312 m_dragSource(0)
1313 {
1314 }
1315
1316 DolphinView::ViewAccessor::~ViewAccessor()
1317 {
1318 delete m_dragSource;
1319 m_dragSource = 0;
1320 }
1321
1322 void DolphinView::ViewAccessor::createView(QWidget* parent,
1323 DolphinViewController* dolphinViewController,
1324 const ViewModeController* viewModeController,
1325 Mode mode)
1326 {
1327 Q_ASSERT(itemView() == 0);
1328
1329 switch (mode) {
1330 case IconsView:
1331 m_iconsView = new DolphinIconsView(parent,
1332 dolphinViewController,
1333 viewModeController,
1334 m_proxyModel);
1335 break;
1336
1337 case DetailsView:
1338 m_detailsView = new DolphinDetailsView(parent,
1339 dolphinViewController,
1340 viewModeController,
1341 m_proxyModel);
1342 break;
1343
1344 case ColumnView:
1345 m_columnsContainer = new DolphinColumnViewContainer(parent,
1346 dolphinViewController,
1347 viewModeController);
1348 if (!m_rootUrl.isEmpty() && m_rootUrl.isParentOf(viewModeController->url())) {
1349 // The column-view must show several columns starting with m_rootUrl as
1350 // first column and viewModeController->url() as last column.
1351 m_columnsContainer->showColumn(m_rootUrl);
1352 m_columnsContainer->showColumn(viewModeController->url());
1353 }
1354 break;
1355
1356 default:
1357 Q_ASSERT(false);
1358 }
1359 }
1360
1361 void DolphinView::ViewAccessor::deleteView()
1362 {
1363 if (m_columnsContainer != 0) {
1364 m_columnsContainer->close();
1365 m_columnsContainer->disconnect();
1366 m_columnsContainer->deleteLater();
1367 m_columnsContainer = 0;
1368 } else {
1369 QAbstractItemView* view = itemView();
1370 if (view != 0) {
1371 view->close();
1372 view->disconnect();
1373
1374 if (DragAndDropHelper::instance().isDragSource(view)) {
1375 // The view is a drag source (the feature "Open folders
1376 // during drag operations" is used). Deleting the view
1377 // during an ongoing drag operation is not allowed, so
1378 // this will postponed.
1379 if (m_dragSource != 0) {
1380 // the old stored view is obviously not the drag source anymore
1381 m_dragSource->deleteLater();
1382 m_dragSource = 0;
1383 }
1384 view->hide();
1385 m_dragSource = view;
1386 } else {
1387 view->deleteLater();
1388 view = 0;
1389 }
1390 }
1391
1392 m_iconsView = 0;
1393 m_detailsView = 0;
1394 }
1395 }
1396
1397 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl& url)
1398 {
1399 if (m_columnsContainer != 0) {
1400 m_columnsContainer->showColumn(url);
1401 }
1402 }
1403
1404 QAbstractItemView* DolphinView::ViewAccessor::itemView() const
1405 {
1406 if (m_iconsView != 0) {
1407 return m_iconsView;
1408 }
1409
1410 if (m_detailsView != 0) {
1411 return m_detailsView;
1412 }
1413
1414 if (m_columnsContainer != 0) {
1415 return m_columnsContainer->activeColumn();
1416 }
1417
1418 return 0;
1419 }
1420
1421 KFileItemDelegate* DolphinView::ViewAccessor::itemDelegate() const
1422 {
1423 return static_cast<KFileItemDelegate*>(itemView()->itemDelegate());
1424 }
1425
1426 QWidget* DolphinView::ViewAccessor::layoutTarget() const
1427 {
1428 if (m_columnsContainer != 0) {
1429 return m_columnsContainer;
1430 }
1431 return itemView();
1432 }
1433
1434 void DolphinView::ViewAccessor::setRootUrl(const KUrl& rootUrl)
1435 {
1436 m_rootUrl = rootUrl;
1437 }
1438
1439 KUrl DolphinView::ViewAccessor::rootUrl() const
1440 {
1441 return (m_columnsContainer != 0) ? m_columnsContainer->rootUrl() : m_rootUrl;
1442 }
1443
1444 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1445 {
1446 return m_iconsView != 0;
1447 }
1448
1449 bool DolphinView::ViewAccessor::itemsExpandable() const
1450 {
1451 return (m_detailsView != 0) && m_detailsView->itemsExpandable();
1452 }
1453
1454 QSet<KUrl> DolphinView::ViewAccessor::expandedUrls() const
1455 {
1456 if (m_detailsView != 0) {
1457 return m_detailsView->expandedUrls();
1458 }
1459
1460 return QSet<KUrl>();
1461 }
1462
1463 const DolphinDetailsViewExpander* DolphinView::ViewAccessor::setExpandedUrls(const QSet<KUrl>& urlsToExpand)
1464 {
1465 if ((m_detailsView != 0) && m_detailsView->itemsExpandable() && !urlsToExpand.isEmpty()) {
1466 // Check if another expander is already active and stop it if necessary.
1467 if(!m_detailsViewExpander.isNull()) {
1468 m_detailsViewExpander->stop();
1469 }
1470
1471 m_detailsViewExpander = new DolphinDetailsViewExpander(m_detailsView, urlsToExpand);
1472 return m_detailsViewExpander;
1473 }
1474 else {
1475 return 0;
1476 }
1477 }
1478
1479 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1480 {
1481 // the details view requires no reloading of the directory, as it maps
1482 // the file item delegate info to its columns internally
1483 return m_detailsView != 0;
1484 }
1485
1486 DolphinModel* DolphinView::ViewAccessor::dirModel() const
1487 {
1488 return static_cast<DolphinModel*>(proxyModel()->sourceModel());
1489 }
1490
1491 DolphinSortFilterProxyModel* DolphinView::ViewAccessor::proxyModel() const
1492 {
1493 if (m_columnsContainer != 0) {
1494 return static_cast<DolphinSortFilterProxyModel*>(m_columnsContainer->activeColumn()->model());
1495 }
1496 return m_proxyModel;
1497 }
1498
1499 KDirLister* DolphinView::ViewAccessor::dirLister() const
1500 {
1501 return dirModel()->dirLister();
1502 }
1503
1504 void DolphinView::slotRedirection(const KUrl& oldUrl, const KUrl& newUrl)
1505 {
1506 if (oldUrl.equals(url(), KUrl::CompareWithoutTrailingSlash)) {
1507 emit redirection(oldUrl, newUrl);
1508 m_viewModeController->redirectToUrl(newUrl); // #186947
1509 }
1510 }
1511
1512 void DolphinView::restoreContentsPosition()
1513 {
1514 if (!m_restoredContentsPosition.isNull()) {
1515 const int x = m_restoredContentsPosition.x();
1516 const int y = m_restoredContentsPosition.y();
1517 m_restoredContentsPosition = QPoint();
1518
1519 QAbstractItemView* view = m_viewAccessor.itemView();
1520 Q_ASSERT(view != 0);
1521 view->horizontalScrollBar()->setValue(x);
1522 view->verticalScrollBar()->setValue(y);
1523 }
1524 }
1525
1526 #include "dolphinview.moc"