1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
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. *
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. *
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 ***************************************************************************/
21 #include "dolphinview.h"
23 #include <QAbstractItemView>
24 #include <QApplication>
27 #include <QItemSelection>
32 #include <kactioncollection.h>
33 #include <kcolorscheme.h>
34 #include <kdirlister.h>
35 #include <kiconeffect.h>
36 #include <kfileitem.h>
38 #include <kio/deletejob.h>
39 #include <kio/netaccess.h>
40 #include <kio/previewjob.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>
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"
70 DolphinView::DolphinView(QWidget
* parent
,
72 DolphinSortFilterProxyModel
* proxyModel
) :
76 m_storedCategorizedSorting(false),
77 m_tabsForFiles(false),
78 m_isContextMenuOpen(false),
79 m_assureVisibleCurrentIndex(false),
80 m_mode(DolphinView::IconsView
),
82 m_dolphinViewController(0),
83 m_viewModeController(0),
84 m_viewAccessor(proxyModel
),
85 m_selectionChangedTimer(0),
88 m_restoredContentsPosition(),
93 m_topLayout
= new QVBoxLayout(this);
94 m_topLayout
->setSpacing(0);
95 m_topLayout
->setMargin(0);
97 m_dolphinViewController
= new DolphinViewController(this);
99 m_viewModeController
= new ViewModeController(this);
100 m_viewModeController
->setUrl(url
);
102 connect(m_viewModeController
, SIGNAL(urlChanged(const KUrl
&)),
103 this, SIGNAL(urlChanged(const KUrl
&)));
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
)));
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()));
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
&)));
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()));
150 applyViewProperties();
151 m_topLayout
->addWidget(m_viewAccessor
.layoutTarget());
154 DolphinView::~DolphinView()
158 KUrl
DolphinView::url() const
160 return m_viewModeController
->url();
163 KUrl
DolphinView::rootUrl() const
165 const KUrl viewUrl
= url();
166 const KUrl root
= m_viewAccessor
.rootUrl();
167 if (root
.isEmpty() || !root
.isParentOf(viewUrl
)) {
173 void DolphinView::setActive(bool active
)
175 if (active
== m_active
) {
181 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
183 emitSelectionChangedSignal();
188 QWidget
* viewport
= m_viewAccessor
.itemView()->viewport();
190 palette
.setColor(viewport
->backgroundRole(), color
);
191 viewport
->setPalette(palette
);
196 m_viewAccessor
.itemView()->setFocus();
200 m_viewModeController
->indicateActivationChange(active
);
203 bool DolphinView::isActive() const
208 void DolphinView::setMode(Mode mode
)
210 if (mode
== m_mode
) {
211 return; // the wished mode is already set
214 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
217 // remember the currently selected items, so that they will
218 // be restored after reloading the directory
219 m_selectedItems
= selectedItems();
223 const KUrl viewPropsUrl
= rootUrl();
224 ViewProperties
props(viewPropsUrl
);
225 props
.setViewMode(m_mode
);
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();
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
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();
246 updateZoomLevel(oldZoomLevel
);
247 loadDirectory(viewPropsUrl
);
250 DolphinView::Mode
DolphinView::mode() const
255 bool DolphinView::showPreview() const
257 return m_showPreview
;
260 bool DolphinView::showHiddenFiles() const
262 return m_viewAccessor
.dirLister()->showingDotFiles();
265 bool DolphinView::categorizedSorting() const
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
;
277 bool DolphinView::supportsCategorizedSorting() const
279 return m_viewAccessor
.supportsCategorizedSorting();
282 bool DolphinView::hasSelection() const
284 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
285 return (view
!= 0) && view
->selectionModel()->hasSelection();
288 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
290 foreach (const KUrl
& url
, urls
) {
291 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
292 m_selectedItems
.append(item
);
296 KFileItemList
DolphinView::selectedItems() const
298 KFileItemList itemList
;
299 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
304 const QItemSelection selection
= m_viewAccessor
.proxyModel()->mapSelectionToSource(view
->selectionModel()->selection());
306 const QModelIndexList indexList
= selection
.indexes();
307 foreach (const QModelIndex
&index
, indexList
) {
308 KFileItem item
= m_viewAccessor
.dirModel()->itemForIndex(index
);
309 if (!item
.isNull()) {
310 itemList
.append(item
);
317 KUrl::List
DolphinView::selectedUrls() const
320 const KFileItemList list
= selectedItems();
321 foreach (const KFileItem
&item
, list
) {
322 urls
.append(item
.url());
327 int DolphinView::selectedItemsCount() const
329 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
334 return view
->selectionModel()->selectedIndexes().count();
337 QItemSelectionModel
* DolphinView::selectionModel() const
339 return m_viewAccessor
.itemView()->selectionModel();
342 void DolphinView::setZoomLevel(int level
)
344 if (level
< ZoomLevelInfo::minimumLevel()) {
345 level
= ZoomLevelInfo::minimumLevel();
346 } else if (level
> ZoomLevelInfo::maximumLevel()) {
347 level
= ZoomLevelInfo::maximumLevel();
350 if (level
!= zoomLevel()) {
351 m_viewModeController
->setZoomLevel(level
);
352 emit
zoomLevelChanged(level
);
356 int DolphinView::zoomLevel() const
358 return m_viewModeController
->zoomLevel();
361 void DolphinView::setSorting(Sorting sorting
)
363 if (sorting
!= this->sorting()) {
364 updateSorting(sorting
);
368 DolphinView::Sorting
DolphinView::sorting() const
370 return m_viewAccessor
.proxyModel()->sorting();
373 void DolphinView::setSortOrder(Qt::SortOrder order
)
375 if (sortOrder() != order
) {
376 updateSortOrder(order
);
380 Qt::SortOrder
DolphinView::sortOrder() const
382 return m_viewAccessor
.proxyModel()->sortOrder();
385 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
387 if (sortFoldersFirst() != foldersFirst
) {
388 updateSortFoldersFirst(foldersFirst
);
392 bool DolphinView::sortFoldersFirst() const
394 return m_viewAccessor
.proxyModel()->sortFoldersFirst();
397 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
399 const KUrl viewPropsUrl
= rootUrl();
400 ViewProperties
props(viewPropsUrl
);
401 props
.setAdditionalInfo(info
);
402 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
404 emit
additionalInfoChanged();
406 if (m_viewAccessor
.reloadOnAdditionalInfoChange()) {
407 loadDirectory(viewPropsUrl
);
411 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
413 return m_viewAccessor
.itemDelegate()->showInformation();
416 void DolphinView::reload()
418 QByteArray viewState
;
419 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
420 saveState(saveStream
);
421 m_selectedItems
= selectedItems();
424 loadDirectory(url(), true);
426 QDataStream
restoreStream(viewState
);
427 restoreState(restoreStream
);
430 void DolphinView::stopLoading()
432 m_viewAccessor
.dirLister()->stop();
435 void DolphinView::refresh()
437 const bool oldActivationState
= m_active
;
438 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
442 applyViewProperties();
445 setActive(oldActivationState
);
446 updateZoomLevel(oldZoomLevel
);
449 void DolphinView::setNameFilter(const QString
& nameFilter
)
451 m_viewModeController
->setNameFilter(nameFilter
);
454 void DolphinView::calculateItemCount(int& fileCount
,
456 KIO::filesize_t
& totalFileSize
) const
458 foreach (const KFileItem
& item
, m_viewAccessor
.dirLister()->items()) {
463 totalFileSize
+= item
.size();
468 QString
DolphinView::statusBarText() const
473 KIO::filesize_t totalFileSize
= 0;
475 if (hasSelection()) {
476 // give a summary of the status of the selected files
477 const KFileItemList list
= selectedItems();
478 if (list
.isEmpty()) {
479 // when an item is triggered, it is temporary selected but selectedItems()
480 // will return an empty list
484 KFileItemList::const_iterator it
= list
.begin();
485 const KFileItemList::const_iterator end
= list
.end();
487 const KFileItem
& item
= *it
;
492 totalFileSize
+= item
.size();
497 if (folderCount
+ fileCount
== 1) {
498 // if only one item is selected, show the filename
499 const QString name
= list
.first().text();
500 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
501 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
502 name
, KIO::convertSize(totalFileSize
));
504 // at least 2 items are selected
505 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
506 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
507 if ((folderCount
> 0) && (fileCount
> 0)) {
508 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
509 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
510 } else if (fileCount
> 0) {
511 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
513 Q_ASSERT(folderCount
> 0);
518 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
519 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
520 fileCount
, folderCount
,
521 totalFileSize
, true);
527 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
529 return m_dolphinViewController
->versionControlActions(items
);
532 void DolphinView::setUrl(const KUrl
& url
)
534 if (m_viewModeController
->url() == url
) {
538 // The selection model might change in the case of the column view. Disconnect
539 // from the current selection model and reconnect later after the URL switch.
540 const bool hadSelection
= hasSelection();
541 QAbstractItemView
* view
= m_viewAccessor
.itemView();
542 disconnect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
543 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
545 m_newFileNames
.clear();
547 m_viewModeController
->setUrl(url
); // emits urlChanged, which we forward
548 m_viewAccessor
.prepareUrlChange(url
);
549 applyViewProperties();
552 // When changing the URL there is no need to keep the version
553 // data of the previous URL.
554 m_viewAccessor
.dirModel()->clearVersionData();
556 emit
startedPathLoading(url
);
558 // Reconnect to the (probably) new selection model
559 view
= m_viewAccessor
.itemView();
560 connect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
561 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
562 if (hadSelection
|| hasSelection()) {
563 emitSelectionChangedSignal();
567 void DolphinView::selectAll()
569 m_viewAccessor
.itemView()->selectAll();
572 void DolphinView::invertSelection()
574 QItemSelectionModel
* selectionModel
= m_viewAccessor
.itemView()->selectionModel();
575 const QAbstractItemModel
* itemModel
= selectionModel
->model();
577 const QModelIndex topLeft
= itemModel
->index(0, 0);
578 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
579 itemModel
->columnCount() - 1);
581 const QItemSelection
selection(topLeft
, bottomRight
);
582 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
585 void DolphinView::clearSelection()
587 m_viewAccessor
.itemView()->clearSelection();
590 void DolphinView::renameSelectedItems()
592 KFileItemList items
= selectedItems();
593 const int itemCount
= items
.count();
598 if ((itemCount
== 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
599 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForItem(items
.first());
600 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
601 m_viewAccessor
.itemView()->edit(proxyIndex
);
603 RenameDialog
* dialog
= new RenameDialog(this, items
);
604 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
607 dialog
->activateWindow();
611 void DolphinView::trashSelectedItems()
613 const KUrl::List list
= simplifiedSelectedUrls();
614 KonqOperations::del(this, KonqOperations::TRASH
, list
);
617 void DolphinView::deleteSelectedItems()
619 const KUrl::List list
= simplifiedSelectedUrls();
620 const bool del
= KonqOperations::askDeleteConfirmation(list
,
622 KonqOperations::DEFAULT_CONFIRMATION
,
626 KIO::Job
* job
= KIO::del(list
);
627 connect(job
, SIGNAL(result(KJob
*)),
628 this, SLOT(slotDeleteFileFinished(KJob
*)));
632 void DolphinView::cutSelectedItems()
634 QMimeData
* mimeData
= selectionMimeData();
635 KonqMimeData::addIsCutSelection(mimeData
, true);
636 QApplication::clipboard()->setMimeData(mimeData
);
639 void DolphinView::copySelectedItems()
641 QMimeData
* mimeData
= selectionMimeData();
642 QApplication::clipboard()->setMimeData(mimeData
);
645 void DolphinView::paste()
650 void DolphinView::pasteIntoFolder()
652 const KFileItemList items
= selectedItems();
653 if ((items
.count() == 1) && items
.first().isDir()) {
654 pasteToUrl(items
.first().url());
658 void DolphinView::setShowPreview(bool show
)
660 if (m_showPreview
== show
) {
664 const KUrl viewPropsUrl
= rootUrl();
665 ViewProperties
props(viewPropsUrl
);
666 props
.setShowPreview(show
);
668 m_showPreview
= show
;
669 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
670 emit
showPreviewChanged();
672 // Enabling or disabling the preview might change the icon size of the view.
673 // As the view does not emit a signal when the icon size has been changed,
674 // the used zoom level of the controller must be adjusted manually:
675 updateZoomLevel(oldZoomLevel
);
678 void DolphinView::setShowHiddenFiles(bool show
)
680 if (m_viewAccessor
.dirLister()->showingDotFiles() == show
) {
684 const KUrl viewPropsUrl
= rootUrl();
685 ViewProperties
props(viewPropsUrl
);
686 props
.setShowHiddenFiles(show
);
688 m_viewAccessor
.dirLister()->setShowingDotFiles(show
);
689 emit
showHiddenFilesChanged();
692 void DolphinView::setCategorizedSorting(bool categorized
)
694 if (categorized
== categorizedSorting()) {
698 // setCategorizedSorting(true) may only get invoked
699 // if the view supports categorized sorting
700 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
702 ViewProperties
props(rootUrl());
703 props
.setCategorizedSorting(categorized
);
706 m_storedCategorizedSorting
= categorized
;
707 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
709 emit
categorizedSortingChanged();
712 void DolphinView::toggleSortOrder()
714 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
715 Qt::DescendingOrder
:
720 void DolphinView::toggleSortFoldersFirst()
722 setSortFoldersFirst(!sortFoldersFirst());
725 void DolphinView::toggleAdditionalInfo(QAction
* action
)
727 const KFileItemDelegate::Information info
=
728 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
730 KFileItemDelegate::InformationList list
= additionalInfo();
732 const bool show
= action
->isChecked();
734 const int index
= list
.indexOf(info
);
735 const bool containsInfo
= (index
>= 0);
736 if (show
&& !containsInfo
) {
738 setAdditionalInfo(list
);
739 } else if (!show
&& containsInfo
) {
740 list
.removeAt(index
);
741 setAdditionalInfo(list
);
742 Q_ASSERT(list
.indexOf(info
) < 0);
746 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
748 QWidget::mouseReleaseEvent(event
);
752 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
754 switch (event
->type()) {
755 case QEvent::FocusIn
:
756 if (watched
== m_viewAccessor
.itemView()) {
757 m_dolphinViewController
->requestActivation();
761 case QEvent::DragEnter
:
762 if (watched
== m_viewAccessor
.itemView()->viewport()) {
767 case QEvent::KeyPress
:
768 if (watched
== m_viewAccessor
.itemView()) {
769 // clear the selection when Escape has been pressed
770 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
771 if (keyEvent
->key() == Qt::Key_Escape
) {
778 if (watched
== m_viewAccessor
.itemView()->viewport()) {
779 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
780 // (the user is probably trying to scroll during a selection in that case)
781 QWheelEvent
* wheelEvent
= static_cast<QWheelEvent
*>(event
);
782 if (wheelEvent
->modifiers() & Qt::ControlModifier
&& !(wheelEvent
->buttons() & Qt::LeftButton
)) {
783 const int delta
= wheelEvent
->delta();
784 const int level
= zoomLevel();
786 setZoomLevel(level
+ 1);
787 } else if (delta
< 0) {
788 setZoomLevel(level
- 1);
799 return QWidget::eventFilter(watched
, event
);
802 void DolphinView::activate()
807 void DolphinView::triggerItem(const KFileItem
& item
)
809 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
810 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
811 // items are selected by the user, hence don't trigger the
812 // item specified by 'index'
816 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
817 if (item
.isNull() || m_isContextMenuOpen
) {
821 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
824 void DolphinView::slotSelectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
826 const int count
= selectedItemsCount();
827 const bool selectionStateChanged
= ((count
> 0) && (selected
.count() == count
)) ||
828 ((count
== 0) && !deselected
.isEmpty());
830 // If nothing has been selected before and something got selected (or if something
831 // was selected before and now nothing is selected) the selectionChangedSignal must
832 // be emitted asynchronously as fast as possible to update the edit-actions.
833 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
834 m_selectionChangedTimer
->start();
837 void DolphinView::emitSelectionChangedSignal()
839 m_selectionChangedTimer
->stop();
840 emit
selectionChanged(selectedItems());
843 void DolphinView::openContextMenu(const QPoint
& pos
,
844 const QList
<QAction
*>& customActions
)
847 const QModelIndex index
= m_viewAccessor
.itemView()->indexAt(pos
);
848 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
849 const QModelIndex dolphinModelIndex
= m_viewAccessor
.proxyModel()->mapToSource(index
);
850 item
= m_viewAccessor
.dirModel()->itemForIndex(dolphinModelIndex
);
853 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
854 emit
requestContextMenu(item
, url(), customActions
);
855 m_isContextMenuOpen
= false;
858 void DolphinView::dropUrls(const KFileItem
& destItem
,
859 const KUrl
& destPath
,
862 addNewFileNames(event
->mimeData());
863 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
866 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
868 ViewProperties
props(rootUrl());
869 props
.setSorting(sorting
);
871 m_viewAccessor
.proxyModel()->setSorting(sorting
);
873 emit
sortingChanged(sorting
);
876 void DolphinView::updateSortOrder(Qt::SortOrder order
)
878 ViewProperties
props(rootUrl());
879 props
.setSortOrder(order
);
881 m_viewAccessor
.proxyModel()->setSortOrder(order
);
883 emit
sortOrderChanged(order
);
886 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
888 ViewProperties
props(rootUrl());
889 props
.setSortFoldersFirst(foldersFirst
);
891 m_viewAccessor
.proxyModel()->setSortFoldersFirst(foldersFirst
);
893 emit
sortFoldersFirstChanged(foldersFirst
);
896 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
898 ViewProperties
props(rootUrl());
899 props
.setAdditionalInfo(info
);
902 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
904 emit
additionalInfoChanged();
907 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
909 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
911 const KFileItemDelegate::InformationList checkedInfo
= m_viewAccessor
.itemDelegate()->showInformation();
912 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
914 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
915 (m_mode
== DolphinView::IconsView
);
917 foreach (const KFileItemDelegate::Information
& info
, infoKeys
) {
918 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::AdditionalInfoType
);
919 QAction
* action
= collection
->action(name
);
920 Q_ASSERT(action
!= 0);
921 action
->setEnabled(enable
);
922 action
->setChecked(checkedInfo
.contains(info
));
926 QPair
<bool, QString
> DolphinView::pasteInfo() const
928 return KonqOperations::pasteInfo(url());
931 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
933 m_tabsForFiles
= tabsForFiles
;
936 bool DolphinView::isTabsForFilesEnabled() const
938 return m_tabsForFiles
;
941 bool DolphinView::itemsExpandable() const
943 return m_viewAccessor
.itemsExpandable();
946 void DolphinView::restoreState(QDataStream
& stream
)
949 stream
>> m_activeItemUrl
;
952 stream
>> m_restoredContentsPosition
;
954 // expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
955 QSet
<KUrl
> urlsToExpand
;
956 stream
>> urlsToExpand
;
957 const DolphinDetailsViewExpander
* expander
= m_viewAccessor
.setExpandedUrls(urlsToExpand
);
959 m_expanderActive
= true;
960 connect (expander
, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
963 m_expanderActive
= false;
967 void DolphinView::saveState(QDataStream
& stream
)
970 KFileItem currentItem
;
971 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
974 const QModelIndex proxyIndex
= view
->currentIndex();
975 const QModelIndex dirModelIndex
= m_viewAccessor
.proxyModel()->mapToSource(proxyIndex
);
976 currentItem
= m_viewAccessor
.dirModel()->itemForIndex(dirModelIndex
);
980 if (!currentItem
.isNull()) {
981 currentUrl
= currentItem
.url();
984 stream
<< currentUrl
;
987 const int x
= view
->horizontalScrollBar()->value();
988 const int y
= view
->verticalScrollBar()->value();
989 stream
<< QPoint(x
, y
);
991 // expanded folders (only relevant for the details view - the set will be empty in other view modes)
992 stream
<< m_viewAccessor
.expandedUrls();
995 void DolphinView::observeCreatedItem(const KUrl
& url
)
997 m_createdItemUrl
= url
;
998 connect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
999 this, SLOT(selectAndScrollToCreatedItem()));
1002 void DolphinView::selectAndScrollToCreatedItem()
1004 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_createdItemUrl
);
1005 if (dirIndex
.isValid()) {
1006 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
1007 m_viewAccessor
.itemView()->setCurrentIndex(proxyIndex
);
1010 disconnect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1011 this, SLOT(selectAndScrollToCreatedItem()));
1012 m_createdItemUrl
= KUrl();
1015 void DolphinView::showHoverInformation(const KFileItem
& item
)
1017 emit
requestItemInfo(item
);
1020 void DolphinView::clearHoverInformation()
1022 emit
requestItemInfo(KFileItem());
1025 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1027 if (job
->error() == 0) {
1028 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1029 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1030 emit
errorMessage(job
->errorString());
1034 void DolphinView::slotDirListerCompleted()
1036 if (!m_expanderActive
) {
1037 slotLoadingCompleted();
1040 if (!m_newFileNames
.isEmpty()) {
1041 // select all newly added items created by a paste operation or
1042 // a drag & drop operation, and clear the previous selection
1043 m_viewAccessor
.itemView()->clearSelection();
1044 const int rowCount
= m_viewAccessor
.proxyModel()->rowCount();
1045 QItemSelection selection
;
1046 for (int row
= 0; row
< rowCount
; ++row
) {
1047 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->index(row
, 0);
1048 const QModelIndex dirIndex
= m_viewAccessor
.proxyModel()->mapToSource(proxyIndex
);
1049 const KUrl url
= m_viewAccessor
.dirModel()->itemForIndex(dirIndex
).url();
1050 if (m_newFileNames
.contains(url
.fileName())) {
1051 selection
.merge(QItemSelection(proxyIndex
, proxyIndex
), QItemSelectionModel::Select
);
1054 m_viewAccessor
.itemView()->selectionModel()->select(selection
, QItemSelectionModel::Select
);
1056 m_newFileNames
.clear();
1060 void DolphinView::slotLoadingCompleted()
1062 m_expanderActive
= false;
1064 if (!m_activeItemUrl
.isEmpty()) {
1065 // assure that the current item remains visible
1066 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_activeItemUrl
);
1067 if (dirIndex
.isValid()) {
1068 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
1069 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1070 const bool clearSelection
= !hasSelection();
1071 view
->setCurrentIndex(proxyIndex
);
1072 if (clearSelection
) {
1073 view
->clearSelection();
1075 m_activeItemUrl
.clear();
1079 if (!m_selectedItems
.isEmpty()) {
1080 const KUrl
& baseUrl
= url();
1082 QItemSelection newSelection
;
1083 foreach(const KFileItem
& item
, m_selectedItems
) {
1084 url
= item
.url().upUrl();
1085 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1086 QModelIndex index
= m_viewAccessor
.proxyModel()->mapFromSource(m_viewAccessor
.dirModel()->indexForItem(item
));
1087 newSelection
.select(index
, index
);
1090 m_viewAccessor
.itemView()->selectionModel()->select(newSelection
,
1091 QItemSelectionModel::ClearAndSelect
1092 | QItemSelectionModel::Current
);
1093 m_selectedItems
.clear();
1096 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1097 // because the view might not be in its final state yet.
1098 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection
);
1100 emit
finishedPathLoading(url());
1103 void DolphinView::slotRefreshItems()
1105 if (m_assureVisibleCurrentIndex
) {
1106 m_assureVisibleCurrentIndex
= false;
1107 m_viewAccessor
.itemView()->scrollTo(m_viewAccessor
.itemView()->currentIndex());
1111 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1113 if (!url
.isValid()) {
1114 const QString
location(url
.pathOrUrl());
1115 if (location
.isEmpty()) {
1116 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1118 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1123 KDirLister
* dirLister
= m_viewAccessor
.dirLister();
1124 dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1127 void DolphinView::applyViewProperties()
1129 const ViewProperties
props(rootUrl());
1131 const Mode mode
= props
.viewMode();
1132 if (m_mode
!= mode
) {
1133 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
1139 updateZoomLevel(oldZoomLevel
);
1141 if (m_viewAccessor
.itemView() == 0) {
1144 Q_ASSERT(m_viewAccessor
.itemView() != 0);
1145 Q_ASSERT(m_viewAccessor
.itemDelegate() != 0);
1147 const bool showHiddenFiles
= props
.showHiddenFiles();
1148 if (showHiddenFiles
!= m_viewAccessor
.dirLister()->showingDotFiles()) {
1149 m_viewAccessor
.dirLister()->setShowingDotFiles(showHiddenFiles
);
1150 emit
showHiddenFilesChanged();
1153 m_storedCategorizedSorting
= props
.categorizedSorting();
1154 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1155 if (categorized
!= m_viewAccessor
.proxyModel()->isCategorizedModel()) {
1156 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
1157 emit
categorizedSortingChanged();
1160 const DolphinView::Sorting sorting
= props
.sorting();
1161 if (sorting
!= m_viewAccessor
.proxyModel()->sorting()) {
1162 m_viewAccessor
.proxyModel()->setSorting(sorting
);
1163 emit
sortingChanged(sorting
);
1166 const Qt::SortOrder sortOrder
= props
.sortOrder();
1167 if (sortOrder
!= m_viewAccessor
.proxyModel()->sortOrder()) {
1168 m_viewAccessor
.proxyModel()->setSortOrder(sortOrder
);
1169 emit
sortOrderChanged(sortOrder
);
1172 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1173 if (sortFoldersFirst
!= m_viewAccessor
.proxyModel()->sortFoldersFirst()) {
1174 m_viewAccessor
.proxyModel()->setSortFoldersFirst(sortFoldersFirst
);
1175 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1178 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1179 if (info
!= m_viewAccessor
.itemDelegate()->showInformation()) {
1180 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
1181 emit
additionalInfoChanged();
1184 const bool showPreview
= props
.showPreview();
1185 if (showPreview
!= m_showPreview
) {
1186 m_showPreview
= showPreview
;
1187 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
1188 emit
showPreviewChanged();
1190 // Enabling or disabling the preview might change the icon size of the view.
1191 // As the view does not emit a signal when the icon size has been changed,
1192 // the used zoom level of the controller must be adjusted manually:
1193 updateZoomLevel(oldZoomLevel
);
1197 void DolphinView::createView()
1201 Q_ASSERT(m_viewAccessor
.itemView() == 0);
1202 Q_ASSERT(m_dolphinViewController
->itemView() == 0);
1203 m_viewAccessor
.createView(this, m_dolphinViewController
, m_viewModeController
, m_mode
);
1205 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1206 Q_ASSERT(view
!= 0);
1207 view
->installEventFilter(this);
1208 view
->viewport()->installEventFilter(this);
1210 m_dolphinViewController
->setItemView(view
);
1212 const int zoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(view
->iconSize());
1213 m_viewModeController
->setZoomLevel(zoomLevel
);
1215 connect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
1216 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
1218 setFocusProxy(m_viewAccessor
.layoutTarget());
1219 m_topLayout
->insertWidget(1, m_viewAccessor
.layoutTarget());
1222 void DolphinView::deleteView()
1224 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1225 Q_ASSERT((m_dolphinViewController
->itemView() == 0) || (m_dolphinViewController
->itemView() == view
));
1226 m_dolphinViewController
->setItemView(0);
1229 if (view
->selectionModel() != 0) {
1230 disconnect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
1231 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
1234 // It's important to set the keyboard focus to the parent
1235 // before deleting the view: Otherwise when having a split
1236 // view the other view will get the focus and will request
1237 // an activation (see DolphinView::eventFilter()).
1241 m_viewModeController
->disconnect(view
);
1243 m_viewAccessor
.deleteView();
1247 void DolphinView::pasteToUrl(const KUrl
& url
)
1249 addNewFileNames(QApplication::clipboard()->mimeData());
1250 KonqOperations::doPaste(this, url
);
1253 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1255 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor
.itemView()->iconSize());
1256 if (oldZoomLevel
!= newZoomLevel
) {
1257 m_viewModeController
->setZoomLevel(newZoomLevel
);
1258 emit
zoomLevelChanged(newZoomLevel
);
1262 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1264 KUrl::List list
= selectedUrls();
1265 if (itemsExpandable() ) {
1266 list
= KDirModel::simplifiedUrlList(list
);
1271 QMimeData
* DolphinView::selectionMimeData() const
1273 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
1274 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1275 const QItemSelection selection
= m_viewAccessor
.proxyModel()->mapSelectionToSource(view
->selectionModel()->selection());
1276 return m_viewAccessor
.dirModel()->mimeData(selection
.indexes());
1279 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1281 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1282 foreach (const KUrl
& url
, urls
) {
1283 m_newFileNames
.insert(url
.fileName());
1287 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel
* proxyModel
) :
1290 m_columnsContainer(0),
1291 m_proxyModel(proxyModel
),
1296 DolphinView::ViewAccessor::~ViewAccessor()
1298 delete m_dragSource
;
1302 void DolphinView::ViewAccessor::createView(QWidget
* parent
,
1303 DolphinViewController
* dolphinViewController
,
1304 const ViewModeController
* viewModeController
,
1307 Q_ASSERT(itemView() == 0);
1311 m_iconsView
= new DolphinIconsView(parent
,
1312 dolphinViewController
,
1318 m_detailsView
= new DolphinDetailsView(parent
,
1319 dolphinViewController
,
1325 m_columnsContainer
= new DolphinColumnViewContainer(parent
,
1326 dolphinViewController
,
1327 viewModeController
);
1335 void DolphinView::ViewAccessor::deleteView()
1337 if (m_columnsContainer
!= 0) {
1338 m_columnsContainer
->close();
1339 m_columnsContainer
->disconnect();
1340 m_columnsContainer
->deleteLater();
1341 m_columnsContainer
= 0;
1343 QAbstractItemView
* view
= itemView();
1348 if (DragAndDropHelper::instance().isDragSource(view
)) {
1349 // The view is a drag source (the feature "Open folders
1350 // during drag operations" is used). Deleting the view
1351 // during an ongoing drag operation is not allowed, so
1352 // this will postponed.
1353 if (m_dragSource
!= 0) {
1354 // the old stored view is obviously not the drag source anymore
1355 m_dragSource
->deleteLater();
1359 m_dragSource
= view
;
1361 view
->deleteLater();
1372 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl
& url
)
1374 if (m_columnsContainer
!= 0) {
1375 m_columnsContainer
->showColumn(url
);
1379 QAbstractItemView
* DolphinView::ViewAccessor::itemView() const
1381 if (m_iconsView
!= 0) {
1385 if (m_detailsView
!= 0) {
1386 return m_detailsView
;
1389 if (m_columnsContainer
!= 0) {
1390 return m_columnsContainer
->activeColumn();
1396 KFileItemDelegate
* DolphinView::ViewAccessor::itemDelegate() const
1398 return static_cast<KFileItemDelegate
*>(itemView()->itemDelegate());
1401 QWidget
* DolphinView::ViewAccessor::layoutTarget() const
1403 if (m_columnsContainer
!= 0) {
1404 return m_columnsContainer
;
1409 KUrl
DolphinView::ViewAccessor::rootUrl() const
1411 return (m_columnsContainer
!= 0) ? m_columnsContainer
->rootUrl() : KUrl();
1414 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1416 return m_iconsView
!= 0;
1419 bool DolphinView::ViewAccessor::itemsExpandable() const
1421 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1425 QSet
<KUrl
> DolphinView::ViewAccessor::expandedUrls() const
1427 if (m_detailsView
!= 0) {
1428 return m_detailsView
->expandedUrls();
1431 return QSet
<KUrl
>();
1434 const DolphinDetailsViewExpander
* DolphinView::ViewAccessor::setExpandedUrls(const QSet
<KUrl
>& urlsToExpand
)
1436 if ((m_detailsView
!= 0) && m_detailsView
->itemsExpandable() && !urlsToExpand
.isEmpty()) {
1437 // Check if another expander is already active and stop it if necessary.
1438 if(!m_detailsViewExpander
.isNull()) {
1439 m_detailsViewExpander
->stop();
1442 m_detailsViewExpander
= new DolphinDetailsViewExpander(m_detailsView
, urlsToExpand
);
1443 return m_detailsViewExpander
;
1450 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1452 // the details view requires no reloading of the directory, as it maps
1453 // the file item delegate info to its columns internally
1454 return m_detailsView
!= 0;
1457 DolphinModel
* DolphinView::ViewAccessor::dirModel() const
1459 return static_cast<DolphinModel
*>(proxyModel()->sourceModel());
1462 DolphinSortFilterProxyModel
* DolphinView::ViewAccessor::proxyModel() const
1464 if (m_columnsContainer
!= 0) {
1465 return static_cast<DolphinSortFilterProxyModel
*>(m_columnsContainer
->activeColumn()->model());
1467 return m_proxyModel
;
1470 KDirLister
* DolphinView::ViewAccessor::dirLister() const
1472 return dirModel()->dirLister();
1475 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1477 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1478 emit
redirection(oldUrl
, newUrl
);
1479 m_viewModeController
->redirectToUrl(newUrl
); // #186947
1483 void DolphinView::restoreContentsPosition()
1485 if (!m_restoredContentsPosition
.isNull()) {
1486 const int x
= m_restoredContentsPosition
.x();
1487 const int y
= m_restoredContentsPosition
.y();
1488 m_restoredContentsPosition
= QPoint();
1490 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1491 Q_ASSERT(view
!= 0);
1492 view
->horizontalScrollBar()->setValue(x
);
1493 view
->verticalScrollBar()->setValue(y
);
1497 #include "dolphinview.moc"