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 int DolphinView::selectedItemsCount() const
319 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
324 return view
->selectionModel()->selectedIndexes().count();
327 QItemSelectionModel
* DolphinView::selectionModel() const
329 return m_viewAccessor
.itemView()->selectionModel();
332 void DolphinView::setZoomLevel(int level
)
334 if (level
< ZoomLevelInfo::minimumLevel()) {
335 level
= ZoomLevelInfo::minimumLevel();
336 } else if (level
> ZoomLevelInfo::maximumLevel()) {
337 level
= ZoomLevelInfo::maximumLevel();
340 if (level
!= zoomLevel()) {
341 m_viewModeController
->setZoomLevel(level
);
342 emit
zoomLevelChanged(level
);
346 int DolphinView::zoomLevel() const
348 return m_viewModeController
->zoomLevel();
351 void DolphinView::setSorting(Sorting sorting
)
353 if (sorting
!= this->sorting()) {
354 updateSorting(sorting
);
358 DolphinView::Sorting
DolphinView::sorting() const
360 return m_viewAccessor
.proxyModel()->sorting();
363 void DolphinView::setSortOrder(Qt::SortOrder order
)
365 if (sortOrder() != order
) {
366 updateSortOrder(order
);
370 Qt::SortOrder
DolphinView::sortOrder() const
372 return m_viewAccessor
.proxyModel()->sortOrder();
375 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
377 if (sortFoldersFirst() != foldersFirst
) {
378 updateSortFoldersFirst(foldersFirst
);
382 bool DolphinView::sortFoldersFirst() const
384 return m_viewAccessor
.proxyModel()->sortFoldersFirst();
387 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
389 const KUrl viewPropsUrl
= rootUrl();
390 ViewProperties
props(viewPropsUrl
);
391 props
.setAdditionalInfo(info
);
392 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
394 emit
additionalInfoChanged();
396 if (m_viewAccessor
.reloadOnAdditionalInfoChange()) {
397 loadDirectory(viewPropsUrl
);
401 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
403 return m_viewAccessor
.itemDelegate()->showInformation();
406 void DolphinView::reload()
408 QByteArray viewState
;
409 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
410 saveState(saveStream
);
411 m_selectedItems
= selectedItems();
414 loadDirectory(url(), true);
416 QDataStream
restoreStream(viewState
);
417 restoreState(restoreStream
);
420 void DolphinView::stopLoading()
422 m_viewAccessor
.dirLister()->stop();
425 void DolphinView::refresh()
427 const bool oldActivationState
= m_active
;
428 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
432 applyViewProperties();
435 setActive(oldActivationState
);
436 updateZoomLevel(oldZoomLevel
);
439 void DolphinView::setNameFilter(const QString
& nameFilter
)
441 m_viewModeController
->setNameFilter(nameFilter
);
444 void DolphinView::calculateItemCount(int& fileCount
,
446 KIO::filesize_t
& totalFileSize
) const
448 foreach (const KFileItem
& item
, m_viewAccessor
.dirLister()->items()) {
453 totalFileSize
+= item
.size();
458 QString
DolphinView::statusBarText() const
463 KIO::filesize_t totalFileSize
= 0;
465 if (hasSelection()) {
466 // give a summary of the status of the selected files
467 const KFileItemList list
= selectedItems();
468 if (list
.isEmpty()) {
469 // when an item is triggered, it is temporary selected but selectedItems()
470 // will return an empty list
474 KFileItemList::const_iterator it
= list
.begin();
475 const KFileItemList::const_iterator end
= list
.end();
477 const KFileItem
& item
= *it
;
482 totalFileSize
+= item
.size();
487 if (folderCount
+ fileCount
== 1) {
488 // if only one item is selected, show the filename
489 const QString name
= list
.first().text();
490 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
491 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
492 name
, KIO::convertSize(totalFileSize
));
494 // at least 2 items are selected
495 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
496 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
497 if ((folderCount
> 0) && (fileCount
> 0)) {
498 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
499 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
500 } else if (fileCount
> 0) {
501 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
503 Q_ASSERT(folderCount
> 0);
508 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
509 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
510 fileCount
, folderCount
,
511 totalFileSize
, true);
517 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
519 return m_dolphinViewController
->versionControlActions(items
);
522 void DolphinView::setUrl(const KUrl
& url
)
524 if (m_viewModeController
->url() == url
) {
528 // The selection model might change in the case of the column view. Disconnect
529 // from the current selection model and reconnect later after the URL switch.
530 const bool hadSelection
= hasSelection();
531 QAbstractItemView
* view
= m_viewAccessor
.itemView();
532 disconnect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
533 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
535 m_newFileNames
.clear();
537 m_viewModeController
->setUrl(url
); // emits urlChanged, which we forward
538 m_viewAccessor
.prepareUrlChange(url
);
539 applyViewProperties();
542 // When changing the URL there is no need to keep the version
543 // data of the previous URL.
544 m_viewAccessor
.dirModel()->clearVersionData();
546 emit
startedPathLoading(url
);
548 // Reconnect to the (probably) new selection model
549 view
= m_viewAccessor
.itemView();
550 connect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
551 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
552 if (hadSelection
|| hasSelection()) {
553 emitSelectionChangedSignal();
557 void DolphinView::selectAll()
559 m_viewAccessor
.itemView()->selectAll();
562 void DolphinView::invertSelection()
564 QItemSelectionModel
* selectionModel
= m_viewAccessor
.itemView()->selectionModel();
565 const QAbstractItemModel
* itemModel
= selectionModel
->model();
567 const QModelIndex topLeft
= itemModel
->index(0, 0);
568 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
569 itemModel
->columnCount() - 1);
571 const QItemSelection
selection(topLeft
, bottomRight
);
572 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
575 void DolphinView::clearSelection()
577 m_viewAccessor
.itemView()->clearSelection();
580 void DolphinView::renameSelectedItems()
582 KFileItemList items
= selectedItems();
583 const int itemCount
= items
.count();
588 if ((itemCount
== 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
589 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForItem(items
.first());
590 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
591 m_viewAccessor
.itemView()->edit(proxyIndex
);
593 RenameDialog
* dialog
= new RenameDialog(this, items
);
594 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
597 dialog
->activateWindow();
601 void DolphinView::trashSelectedItems()
603 const KUrl::List list
= simplifiedSelectedUrls();
604 KonqOperations::del(this, KonqOperations::TRASH
, list
);
607 void DolphinView::deleteSelectedItems()
609 const KUrl::List list
= simplifiedSelectedUrls();
610 const bool del
= KonqOperations::askDeleteConfirmation(list
,
612 KonqOperations::DEFAULT_CONFIRMATION
,
616 KIO::Job
* job
= KIO::del(list
);
617 connect(job
, SIGNAL(result(KJob
*)),
618 this, SLOT(slotDeleteFileFinished(KJob
*)));
622 void DolphinView::cutSelectedItems()
624 QMimeData
* mimeData
= selectionMimeData();
625 KonqMimeData::addIsCutSelection(mimeData
, true);
626 QApplication::clipboard()->setMimeData(mimeData
);
629 void DolphinView::copySelectedItems()
631 QMimeData
* mimeData
= selectionMimeData();
632 QApplication::clipboard()->setMimeData(mimeData
);
635 void DolphinView::paste()
640 void DolphinView::pasteIntoFolder()
642 const KFileItemList items
= selectedItems();
643 if ((items
.count() == 1) && items
.first().isDir()) {
644 pasteToUrl(items
.first().url());
648 void DolphinView::setShowPreview(bool show
)
650 if (m_showPreview
== show
) {
654 const KUrl viewPropsUrl
= rootUrl();
655 ViewProperties
props(viewPropsUrl
);
656 props
.setShowPreview(show
);
658 m_showPreview
= show
;
659 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
660 emit
showPreviewChanged();
662 // Enabling or disabling the preview might change the icon size of the view.
663 // As the view does not emit a signal when the icon size has been changed,
664 // the used zoom level of the controller must be adjusted manually:
665 updateZoomLevel(oldZoomLevel
);
668 void DolphinView::setShowHiddenFiles(bool show
)
670 if (m_viewAccessor
.dirLister()->showingDotFiles() == show
) {
674 const KUrl viewPropsUrl
= rootUrl();
675 ViewProperties
props(viewPropsUrl
);
676 props
.setShowHiddenFiles(show
);
678 m_viewAccessor
.dirLister()->setShowingDotFiles(show
);
679 emit
showHiddenFilesChanged();
682 void DolphinView::setCategorizedSorting(bool categorized
)
684 if (categorized
== categorizedSorting()) {
688 // setCategorizedSorting(true) may only get invoked
689 // if the view supports categorized sorting
690 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
692 ViewProperties
props(rootUrl());
693 props
.setCategorizedSorting(categorized
);
696 m_storedCategorizedSorting
= categorized
;
697 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
699 emit
categorizedSortingChanged();
702 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
704 QWidget::mouseReleaseEvent(event
);
708 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
710 switch (event
->type()) {
711 case QEvent::FocusIn
:
712 if (watched
== m_viewAccessor
.itemView()) {
713 m_dolphinViewController
->requestActivation();
717 case QEvent::DragEnter
:
718 if (watched
== m_viewAccessor
.itemView()->viewport()) {
723 case QEvent::KeyPress
:
724 if (watched
== m_viewAccessor
.itemView()) {
725 // clear the selection when Escape has been pressed
726 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
727 if (keyEvent
->key() == Qt::Key_Escape
) {
734 if (watched
== m_viewAccessor
.itemView()->viewport()) {
735 // Ctrl+wheel events should cause icon zooming, but not if the left mouse button is pressed
736 // (the user is probably trying to scroll during a selection in that case)
737 QWheelEvent
* wheelEvent
= static_cast<QWheelEvent
*>(event
);
738 if (wheelEvent
->modifiers() & Qt::ControlModifier
&& !(wheelEvent
->buttons() & Qt::LeftButton
)) {
739 const int delta
= wheelEvent
->delta();
740 const int level
= zoomLevel();
742 setZoomLevel(level
+ 1);
743 } else if (delta
< 0) {
744 setZoomLevel(level
- 1);
755 return QWidget::eventFilter(watched
, event
);
758 void DolphinView::activate()
763 void DolphinView::triggerItem(const KFileItem
& item
)
765 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
766 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
767 // items are selected by the user, hence don't trigger the
768 // item specified by 'index'
772 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
773 if (item
.isNull() || m_isContextMenuOpen
) {
777 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
780 void DolphinView::slotSelectionChanged(const QItemSelection
& selected
, const QItemSelection
& deselected
)
782 const int count
= selectedItemsCount();
783 const bool selectionStateChanged
= ((count
> 0) && (selected
.count() == count
)) ||
784 ((count
== 0) && !deselected
.isEmpty());
786 // If nothing has been selected before and something got selected (or if something
787 // was selected before and now nothing is selected) the selectionChangedSignal must
788 // be emitted asynchronously as fast as possible to update the edit-actions.
789 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
790 m_selectionChangedTimer
->start();
793 void DolphinView::emitSelectionChangedSignal()
795 m_selectionChangedTimer
->stop();
796 emit
selectionChanged(selectedItems());
799 void DolphinView::openContextMenu(const QPoint
& pos
,
800 const QList
<QAction
*>& customActions
)
803 const QModelIndex index
= m_viewAccessor
.itemView()->indexAt(pos
);
804 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
805 const QModelIndex dolphinModelIndex
= m_viewAccessor
.proxyModel()->mapToSource(index
);
806 item
= m_viewAccessor
.dirModel()->itemForIndex(dolphinModelIndex
);
809 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
810 emit
requestContextMenu(item
, url(), customActions
);
811 m_isContextMenuOpen
= false;
814 void DolphinView::dropUrls(const KFileItem
& destItem
,
815 const KUrl
& destPath
,
818 addNewFileNames(event
->mimeData());
819 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
822 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
824 ViewProperties
props(rootUrl());
825 props
.setSorting(sorting
);
827 m_viewAccessor
.proxyModel()->setSorting(sorting
);
829 emit
sortingChanged(sorting
);
832 void DolphinView::updateSortOrder(Qt::SortOrder order
)
834 ViewProperties
props(rootUrl());
835 props
.setSortOrder(order
);
837 m_viewAccessor
.proxyModel()->setSortOrder(order
);
839 emit
sortOrderChanged(order
);
842 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
844 ViewProperties
props(rootUrl());
845 props
.setSortFoldersFirst(foldersFirst
);
847 m_viewAccessor
.proxyModel()->setSortFoldersFirst(foldersFirst
);
849 emit
sortFoldersFirstChanged(foldersFirst
);
852 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
854 ViewProperties
props(rootUrl());
855 props
.setAdditionalInfo(info
);
858 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
860 emit
additionalInfoChanged();
863 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
865 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
867 const KFileItemDelegate::InformationList checkedInfo
= m_viewAccessor
.itemDelegate()->showInformation();
868 const KFileItemDelegate::InformationList infoKeys
= infoAccessor
.keys();
870 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
871 (m_mode
== DolphinView::IconsView
);
873 foreach (const KFileItemDelegate::Information
& info
, infoKeys
) {
874 const QString name
= infoAccessor
.actionCollectionName(info
, AdditionalInfoAccessor::AdditionalInfoType
);
875 QAction
* action
= collection
->action(name
);
876 Q_ASSERT(action
!= 0);
877 action
->setEnabled(enable
);
878 action
->setChecked(checkedInfo
.contains(info
));
882 QPair
<bool, QString
> DolphinView::pasteInfo() const
884 return KonqOperations::pasteInfo(url());
887 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
889 m_tabsForFiles
= tabsForFiles
;
892 bool DolphinView::isTabsForFilesEnabled() const
894 return m_tabsForFiles
;
897 bool DolphinView::itemsExpandable() const
899 return m_viewAccessor
.itemsExpandable();
902 void DolphinView::restoreState(QDataStream
& stream
)
905 stream
>> m_activeItemUrl
;
908 stream
>> m_restoredContentsPosition
;
910 // expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
911 QSet
<KUrl
> urlsToExpand
;
912 stream
>> urlsToExpand
;
913 const DolphinDetailsViewExpander
* expander
= m_viewAccessor
.setExpandedUrls(urlsToExpand
);
915 m_expanderActive
= true;
916 connect (expander
, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
919 m_expanderActive
= false;
923 void DolphinView::saveState(QDataStream
& stream
)
926 KFileItem currentItem
;
927 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
930 const QModelIndex proxyIndex
= view
->currentIndex();
931 const QModelIndex dirModelIndex
= m_viewAccessor
.proxyModel()->mapToSource(proxyIndex
);
932 currentItem
= m_viewAccessor
.dirModel()->itemForIndex(dirModelIndex
);
936 if (!currentItem
.isNull()) {
937 currentUrl
= currentItem
.url();
940 stream
<< currentUrl
;
943 const int x
= view
->horizontalScrollBar()->value();
944 const int y
= view
->verticalScrollBar()->value();
945 stream
<< QPoint(x
, y
);
947 // expanded folders (only relevant for the details view - the set will be empty in other view modes)
948 stream
<< m_viewAccessor
.expandedUrls();
951 void DolphinView::observeCreatedItem(const KUrl
& url
)
953 m_createdItemUrl
= url
;
954 connect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
955 this, SLOT(selectAndScrollToCreatedItem()));
958 void DolphinView::selectAndScrollToCreatedItem()
960 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_createdItemUrl
);
961 if (dirIndex
.isValid()) {
962 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
963 m_viewAccessor
.itemView()->setCurrentIndex(proxyIndex
);
966 disconnect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
967 this, SLOT(selectAndScrollToCreatedItem()));
968 m_createdItemUrl
= KUrl();
971 void DolphinView::showHoverInformation(const KFileItem
& item
)
973 emit
requestItemInfo(item
);
976 void DolphinView::clearHoverInformation()
978 emit
requestItemInfo(KFileItem());
981 void DolphinView::slotDeleteFileFinished(KJob
* job
)
983 if (job
->error() == 0) {
984 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
985 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
986 emit
errorMessage(job
->errorString());
990 void DolphinView::slotDirListerCompleted()
992 if (!m_expanderActive
) {
993 slotLoadingCompleted();
996 if (!m_newFileNames
.isEmpty()) {
997 // select all newly added items created by a paste operation or
998 // a drag & drop operation, and clear the previous selection
999 m_viewAccessor
.itemView()->clearSelection();
1000 const int rowCount
= m_viewAccessor
.proxyModel()->rowCount();
1001 QItemSelection selection
;
1002 for (int row
= 0; row
< rowCount
; ++row
) {
1003 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->index(row
, 0);
1004 const QModelIndex dirIndex
= m_viewAccessor
.proxyModel()->mapToSource(proxyIndex
);
1005 const KUrl url
= m_viewAccessor
.dirModel()->itemForIndex(dirIndex
).url();
1006 if (m_newFileNames
.contains(url
.fileName())) {
1007 selection
.merge(QItemSelection(proxyIndex
, proxyIndex
), QItemSelectionModel::Select
);
1010 m_viewAccessor
.itemView()->selectionModel()->select(selection
, QItemSelectionModel::Select
);
1012 m_newFileNames
.clear();
1016 void DolphinView::slotLoadingCompleted()
1018 m_expanderActive
= false;
1020 if (!m_activeItemUrl
.isEmpty()) {
1021 // assure that the current item remains visible
1022 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_activeItemUrl
);
1023 if (dirIndex
.isValid()) {
1024 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
1025 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1026 const bool clearSelection
= !hasSelection();
1027 view
->setCurrentIndex(proxyIndex
);
1028 if (clearSelection
) {
1029 view
->clearSelection();
1031 m_activeItemUrl
.clear();
1035 if (!m_selectedItems
.isEmpty()) {
1036 const KUrl
& baseUrl
= url();
1038 QItemSelection newSelection
;
1039 foreach(const KFileItem
& item
, m_selectedItems
) {
1040 url
= item
.url().upUrl();
1041 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
1042 QModelIndex index
= m_viewAccessor
.proxyModel()->mapFromSource(m_viewAccessor
.dirModel()->indexForItem(item
));
1043 newSelection
.select(index
, index
);
1046 m_viewAccessor
.itemView()->selectionModel()->select(newSelection
,
1047 QItemSelectionModel::ClearAndSelect
1048 | QItemSelectionModel::Current
);
1049 m_selectedItems
.clear();
1052 // Restore the contents position. This has to be done using a Qt::QueuedConnection
1053 // because the view might not be in its final state yet.
1054 QMetaObject::invokeMethod(this, "restoreContentsPosition", Qt::QueuedConnection
);
1056 emit
finishedPathLoading(url());
1059 void DolphinView::slotRefreshItems()
1061 if (m_assureVisibleCurrentIndex
) {
1062 m_assureVisibleCurrentIndex
= false;
1063 m_viewAccessor
.itemView()->scrollTo(m_viewAccessor
.itemView()->currentIndex());
1067 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1069 if (!url
.isValid()) {
1070 const QString
location(url
.pathOrUrl());
1071 if (location
.isEmpty()) {
1072 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1074 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1079 KDirLister
* dirLister
= m_viewAccessor
.dirLister();
1080 dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1083 void DolphinView::applyViewProperties()
1085 const ViewProperties
props(rootUrl());
1087 const Mode mode
= props
.viewMode();
1088 if (m_mode
!= mode
) {
1089 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
1095 updateZoomLevel(oldZoomLevel
);
1097 if (m_viewAccessor
.itemView() == 0) {
1100 Q_ASSERT(m_viewAccessor
.itemView() != 0);
1101 Q_ASSERT(m_viewAccessor
.itemDelegate() != 0);
1103 const bool showHiddenFiles
= props
.showHiddenFiles();
1104 if (showHiddenFiles
!= m_viewAccessor
.dirLister()->showingDotFiles()) {
1105 m_viewAccessor
.dirLister()->setShowingDotFiles(showHiddenFiles
);
1106 emit
showHiddenFilesChanged();
1109 m_storedCategorizedSorting
= props
.categorizedSorting();
1110 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1111 if (categorized
!= m_viewAccessor
.proxyModel()->isCategorizedModel()) {
1112 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
1113 emit
categorizedSortingChanged();
1116 const DolphinView::Sorting sorting
= props
.sorting();
1117 if (sorting
!= m_viewAccessor
.proxyModel()->sorting()) {
1118 m_viewAccessor
.proxyModel()->setSorting(sorting
);
1119 emit
sortingChanged(sorting
);
1122 const Qt::SortOrder sortOrder
= props
.sortOrder();
1123 if (sortOrder
!= m_viewAccessor
.proxyModel()->sortOrder()) {
1124 m_viewAccessor
.proxyModel()->setSortOrder(sortOrder
);
1125 emit
sortOrderChanged(sortOrder
);
1128 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1129 if (sortFoldersFirst
!= m_viewAccessor
.proxyModel()->sortFoldersFirst()) {
1130 m_viewAccessor
.proxyModel()->setSortFoldersFirst(sortFoldersFirst
);
1131 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1134 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1135 if (info
!= m_viewAccessor
.itemDelegate()->showInformation()) {
1136 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
1137 emit
additionalInfoChanged();
1140 const bool showPreview
= props
.showPreview();
1141 if (showPreview
!= m_showPreview
) {
1142 m_showPreview
= showPreview
;
1143 const int oldZoomLevel
= m_viewModeController
->zoomLevel();
1144 emit
showPreviewChanged();
1146 // Enabling or disabling the preview might change the icon size of the view.
1147 // As the view does not emit a signal when the icon size has been changed,
1148 // the used zoom level of the controller must be adjusted manually:
1149 updateZoomLevel(oldZoomLevel
);
1153 void DolphinView::createView()
1157 Q_ASSERT(m_viewAccessor
.itemView() == 0);
1158 Q_ASSERT(m_dolphinViewController
->itemView() == 0);
1159 m_viewAccessor
.createView(this, m_dolphinViewController
, m_viewModeController
, m_mode
);
1161 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1162 Q_ASSERT(view
!= 0);
1163 view
->installEventFilter(this);
1164 view
->viewport()->installEventFilter(this);
1166 m_dolphinViewController
->setItemView(view
);
1168 const int zoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(view
->iconSize());
1169 m_viewModeController
->setZoomLevel(zoomLevel
);
1171 connect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
1172 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
1174 setFocusProxy(m_viewAccessor
.layoutTarget());
1175 m_topLayout
->insertWidget(1, m_viewAccessor
.layoutTarget());
1178 void DolphinView::deleteView()
1180 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1181 Q_ASSERT((m_dolphinViewController
->itemView() == 0) || (m_dolphinViewController
->itemView() == view
));
1182 m_dolphinViewController
->setItemView(0);
1185 if (view
->selectionModel() != 0) {
1186 disconnect(view
->selectionModel(), SIGNAL(selectionChanged(QItemSelection
, QItemSelection
)),
1187 this, SLOT(slotSelectionChanged(QItemSelection
, QItemSelection
)));
1190 // It's important to set the keyboard focus to the parent
1191 // before deleting the view: Otherwise when having a split
1192 // view the other view will get the focus and will request
1193 // an activation (see DolphinView::eventFilter()).
1197 m_viewModeController
->disconnect(view
);
1199 m_viewAccessor
.deleteView();
1203 void DolphinView::pasteToUrl(const KUrl
& url
)
1205 addNewFileNames(QApplication::clipboard()->mimeData());
1206 KonqOperations::doPaste(this, url
);
1209 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1211 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor
.itemView()->iconSize());
1212 if (oldZoomLevel
!= newZoomLevel
) {
1213 m_viewModeController
->setZoomLevel(newZoomLevel
);
1214 emit
zoomLevelChanged(newZoomLevel
);
1218 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1222 const KFileItemList items
= selectedItems();
1223 foreach (const KFileItem
&item
, items
) {
1224 urls
.append(item
.url());
1228 if (itemsExpandable()) {
1229 urls
= KDirModel::simplifiedUrlList(urls
);
1235 QMimeData
* DolphinView::selectionMimeData() const
1237 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
1238 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1239 const QItemSelection selection
= m_viewAccessor
.proxyModel()->mapSelectionToSource(view
->selectionModel()->selection());
1240 return m_viewAccessor
.dirModel()->mimeData(selection
.indexes());
1243 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1245 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1246 foreach (const KUrl
& url
, urls
) {
1247 m_newFileNames
.insert(url
.fileName());
1251 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel
* proxyModel
) :
1254 m_columnsContainer(0),
1255 m_proxyModel(proxyModel
),
1260 DolphinView::ViewAccessor::~ViewAccessor()
1262 delete m_dragSource
;
1266 void DolphinView::ViewAccessor::createView(QWidget
* parent
,
1267 DolphinViewController
* dolphinViewController
,
1268 const ViewModeController
* viewModeController
,
1271 Q_ASSERT(itemView() == 0);
1275 m_iconsView
= new DolphinIconsView(parent
,
1276 dolphinViewController
,
1282 m_detailsView
= new DolphinDetailsView(parent
,
1283 dolphinViewController
,
1289 m_columnsContainer
= new DolphinColumnViewContainer(parent
,
1290 dolphinViewController
,
1291 viewModeController
);
1299 void DolphinView::ViewAccessor::deleteView()
1301 if (m_columnsContainer
!= 0) {
1302 m_columnsContainer
->close();
1303 m_columnsContainer
->disconnect();
1304 m_columnsContainer
->deleteLater();
1305 m_columnsContainer
= 0;
1307 QAbstractItemView
* view
= itemView();
1312 if (DragAndDropHelper::instance().isDragSource(view
)) {
1313 // The view is a drag source (the feature "Open folders
1314 // during drag operations" is used). Deleting the view
1315 // during an ongoing drag operation is not allowed, so
1316 // this will postponed.
1317 if (m_dragSource
!= 0) {
1318 // the old stored view is obviously not the drag source anymore
1319 m_dragSource
->deleteLater();
1323 m_dragSource
= view
;
1325 view
->deleteLater();
1336 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl
& url
)
1338 if (m_columnsContainer
!= 0) {
1339 m_columnsContainer
->showColumn(url
);
1343 QAbstractItemView
* DolphinView::ViewAccessor::itemView() const
1345 if (m_iconsView
!= 0) {
1349 if (m_detailsView
!= 0) {
1350 return m_detailsView
;
1353 if (m_columnsContainer
!= 0) {
1354 return m_columnsContainer
->activeColumn();
1360 KFileItemDelegate
* DolphinView::ViewAccessor::itemDelegate() const
1362 return static_cast<KFileItemDelegate
*>(itemView()->itemDelegate());
1365 QWidget
* DolphinView::ViewAccessor::layoutTarget() const
1367 if (m_columnsContainer
!= 0) {
1368 return m_columnsContainer
;
1373 KUrl
DolphinView::ViewAccessor::rootUrl() const
1375 return (m_columnsContainer
!= 0) ? m_columnsContainer
->rootUrl() : KUrl();
1378 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1380 return m_iconsView
!= 0;
1383 bool DolphinView::ViewAccessor::itemsExpandable() const
1385 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1389 QSet
<KUrl
> DolphinView::ViewAccessor::expandedUrls() const
1391 if (m_detailsView
!= 0) {
1392 return m_detailsView
->expandedUrls();
1395 return QSet
<KUrl
>();
1398 const DolphinDetailsViewExpander
* DolphinView::ViewAccessor::setExpandedUrls(const QSet
<KUrl
>& urlsToExpand
)
1400 if ((m_detailsView
!= 0) && m_detailsView
->itemsExpandable() && !urlsToExpand
.isEmpty()) {
1401 // Check if another expander is already active and stop it if necessary.
1402 if(!m_detailsViewExpander
.isNull()) {
1403 m_detailsViewExpander
->stop();
1406 m_detailsViewExpander
= new DolphinDetailsViewExpander(m_detailsView
, urlsToExpand
);
1407 return m_detailsViewExpander
;
1414 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1416 // the details view requires no reloading of the directory, as it maps
1417 // the file item delegate info to its columns internally
1418 return m_detailsView
!= 0;
1421 DolphinModel
* DolphinView::ViewAccessor::dirModel() const
1423 return static_cast<DolphinModel
*>(proxyModel()->sourceModel());
1426 DolphinSortFilterProxyModel
* DolphinView::ViewAccessor::proxyModel() const
1428 if (m_columnsContainer
!= 0) {
1429 return static_cast<DolphinSortFilterProxyModel
*>(m_columnsContainer
->activeColumn()->model());
1431 return m_proxyModel
;
1434 KDirLister
* DolphinView::ViewAccessor::dirLister() const
1436 return dirModel()->dirLister();
1439 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1441 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1442 emit
redirection(oldUrl
, newUrl
);
1443 m_viewModeController
->redirectToUrl(newUrl
); // #186947
1447 void DolphinView::restoreContentsPosition()
1449 if (!m_restoredContentsPosition
.isNull()) {
1450 const int x
= m_restoredContentsPosition
.x();
1451 const int y
= m_restoredContentsPosition
.y();
1452 m_restoredContentsPosition
= QPoint();
1454 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1455 Q_ASSERT(view
!= 0);
1456 view
->horizontalScrollBar()->setValue(x
);
1457 view
->verticalScrollBar()->setValue(y
);
1461 #include "dolphinview.moc"