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