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