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