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 <kstringhandler.h>
49 #include <ktoggleaction.h>
52 #include "dolphinmodel.h"
53 #include "dolphincolumnviewcontainer.h"
54 #include "dolphincontroller.h"
55 #include "dolphindetailsview.h"
56 #include "dolphinfileitemdelegate.h"
57 #include "dolphinnewmenuobserver.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 "viewproperties.h"
66 #include "zoomlevelinfo.h"
69 * Helper function for sorting items with qSort() in
70 * DolphinView::renameSelectedItems().
72 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
74 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
77 DolphinView::DolphinView(QWidget
* parent
,
79 DolphinSortFilterProxyModel
* proxyModel
) :
83 m_loadingDirectory(false),
84 m_storedCategorizedSorting(false),
85 m_tabsForFiles(false),
86 m_isContextMenuOpen(false),
87 m_ignoreViewProperties(false),
88 m_assureVisibleCurrentIndex(false),
89 m_mode(DolphinView::IconsView
),
92 m_viewAccessor(proxyModel
),
94 m_selectionChangedTimer(0),
101 m_topLayout
= new QVBoxLayout(this);
102 m_topLayout
->setSpacing(0);
103 m_topLayout
->setMargin(0);
105 m_controller
= new DolphinController(this);
106 m_controller
->setUrl(url
);
108 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
109 this, SIGNAL(urlChanged(const KUrl
&)));
110 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
111 this, SLOT(slotRequestUrlChange(const KUrl
&)));
113 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
114 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
115 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
116 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
117 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
118 this, SLOT(updateSorting(DolphinView::Sorting
)));
119 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
120 this, SLOT(updateSortOrder(Qt::SortOrder
)));
121 connect(m_controller
, SIGNAL(sortFoldersFirstChanged(bool)),
122 this, SLOT(updateSortFoldersFirst(bool)));
123 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
124 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
125 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
126 this, SLOT(triggerItem(const KFileItem
&)));
127 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
128 this, SIGNAL(tabRequested(const KUrl
&)));
129 connect(m_controller
, SIGNAL(activated()),
130 this, SLOT(activate()));
131 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
132 this, SLOT(showHoverInformation(const KFileItem
&)));
133 connect(m_controller
, SIGNAL(viewportEntered()),
134 this, SLOT(clearHoverInformation()));
136 KDirLister
* dirLister
= m_viewAccessor
.dirLister();
137 connect(dirLister
, SIGNAL(redirection(KUrl
,KUrl
)),
138 this, SLOT(slotRedirection(KUrl
,KUrl
)));
139 connect(dirLister
, SIGNAL(completed()),
140 this, SLOT(slotDirListerCompleted()));
141 connect(dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
142 this, SLOT(slotRefreshItems()));
144 // When a new item has been created by the "Create New..." menu, the item should
145 // get selected and it must be assured that the item will get visible. As the
146 // creation is done asynchronously, several signals must be checked:
147 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
148 this, SLOT(observeCreatedItem(const KUrl
&)));
150 m_selectionChangedTimer
= new QTimer(this);
151 m_selectionChangedTimer
->setSingleShot(true);
152 m_selectionChangedTimer
->setInterval(300);
153 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
154 this, SLOT(emitSelectionChangedSignal()));
156 applyViewProperties();
157 m_topLayout
->addWidget(m_viewAccessor
.itemView());
160 DolphinView::~DolphinView()
164 const KUrl
& DolphinView::url() const
166 return m_controller
->url();
169 KUrl
DolphinView::rootUrl() const
171 const KUrl viewUrl
= url();
172 const KUrl root
= m_viewAccessor
.rootUrl();
173 if (root
.isEmpty() || !root
.isParentOf(viewUrl
)) {
179 void DolphinView::setActive(bool active
)
181 if (active
== m_active
) {
187 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
189 emitSelectionChangedSignal();
194 QWidget
* viewport
= m_viewAccessor
.itemView()->viewport();
196 palette
.setColor(viewport
->backgroundRole(), color
);
197 viewport
->setPalette(palette
);
202 m_viewAccessor
.itemView()->setFocus();
206 m_controller
->indicateActivationChange(active
);
209 bool DolphinView::isActive() const
214 void DolphinView::setMode(Mode mode
)
216 if (mode
== m_mode
) {
217 return; // the wished mode is already set
220 const int oldZoomLevel
= m_controller
->zoomLevel();
225 const KUrl viewPropsUrl
= rootUrl();
226 ViewProperties
props(viewPropsUrl
);
227 props
.setViewMode(m_mode
);
230 // the file item delegate has been recreated, apply the current
231 // additional information manually
232 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
233 m_viewAccessor
.itemDelegate()->setShowInformation(infoList
);
234 emit
additionalInfoChanged();
236 // Not all view modes support categorized sorting. Adjust the sorting model
237 // if changing the view mode results in a change of the categorized sorting
239 m_storedCategorizedSorting
= props
.categorizedSorting();
240 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
241 if (categorized
!= m_viewAccessor
.proxyModel()->isCategorizedModel()) {
242 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
243 emit
categorizedSortingChanged();
248 updateZoomLevel(oldZoomLevel
);
250 loadDirectory(viewPropsUrl
);
254 DolphinView::Mode
DolphinView::mode() const
259 bool DolphinView::showPreview() const
261 return m_showPreview
;
264 bool DolphinView::showHiddenFiles() const
266 return m_viewAccessor
.dirLister()->showingDotFiles();
269 bool DolphinView::categorizedSorting() const
271 // If all view modes would support categorized sorting, returning
272 // m_viewAccessor.proxyModel()->isCategorizedModel() would be the way to go. As
273 // currently only the icons view supports caterized sorting, we remember
274 // the stored view properties state in m_storedCategorizedSorting and
275 // return this state. The application takes care to disable the corresponding
276 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
277 // that this setting is not applied to the current view mode.
278 return m_storedCategorizedSorting
;
281 bool DolphinView::supportsCategorizedSorting() const
283 return m_viewAccessor
.supportsCategorizedSorting();
286 void DolphinView::selectAll()
288 QAbstractItemView
* view
= m_viewAccessor
.itemView();
289 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
290 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
291 // selection instead of selecting all items. This is bypassed for KDE 4.0
292 // by invoking clearSelection() first.
293 view
->clearSelection();
297 void DolphinView::invertSelection()
299 QItemSelectionModel
* selectionModel
= m_viewAccessor
.itemView()->selectionModel();
300 const QAbstractItemModel
* itemModel
= selectionModel
->model();
302 const QModelIndex topLeft
= itemModel
->index(0, 0);
303 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
304 itemModel
->columnCount() - 1);
306 const QItemSelection
selection(topLeft
, bottomRight
);
307 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
310 bool DolphinView::hasSelection() const
312 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
313 return view
&& view
->selectionModel()->hasSelection();
316 void DolphinView::clearSelection()
318 QItemSelectionModel
* selModel
= m_viewAccessor
.itemView()->selectionModel();
319 const QModelIndex currentIndex
= selModel
->currentIndex();
320 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
321 QItemSelectionModel::Clear
);
322 m_selectedItems
.clear();
325 KFileItemList
DolphinView::selectedItems() const
327 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
329 // Our view has a selection, we will map them back to the DolphinModel
330 // and then fill the KFileItemList.
331 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
333 const QItemSelection selection
= m_viewAccessor
.proxyModel()->mapSelectionToSource(view
->selectionModel()->selection());
334 KFileItemList itemList
;
336 const QModelIndexList indexList
= selection
.indexes();
337 foreach (const QModelIndex
&index
, indexList
) {
338 KFileItem item
= m_viewAccessor
.dirModel()->itemForIndex(index
);
339 if (!item
.isNull()) {
340 itemList
.append(item
);
347 KUrl::List
DolphinView::selectedUrls() const
350 const KFileItemList list
= selectedItems();
351 foreach (const KFileItem
&item
, list
) {
352 urls
.append(item
.url());
357 int DolphinView::selectedItemsCount() const
359 return m_viewAccessor
.itemView()->selectionModel()->selectedIndexes().count();
362 void DolphinView::setContentsPosition(int x
, int y
)
364 QAbstractItemView
* view
= m_viewAccessor
.itemView();
366 view
->horizontalScrollBar()->setValue(x
);
367 view
->verticalScrollBar()->setValue(y
);
369 m_loadingDirectory
= false;
372 QPoint
DolphinView::contentsPosition() const
374 QAbstractItemView
* view
= m_viewAccessor
.itemView();
376 const int x
= view
->horizontalScrollBar()->value();
377 const int y
= view
->verticalScrollBar()->value();
381 void DolphinView::setZoomLevel(int level
)
383 if (level
< ZoomLevelInfo::minimumLevel()) {
384 level
= ZoomLevelInfo::minimumLevel();
385 } else if (level
> ZoomLevelInfo::maximumLevel()) {
386 level
= ZoomLevelInfo::maximumLevel();
389 if (level
!= zoomLevel()) {
390 m_controller
->setZoomLevel(level
);
391 emit
zoomLevelChanged(level
);
395 int DolphinView::zoomLevel() const
397 return m_controller
->zoomLevel();
400 void DolphinView::setSorting(Sorting sorting
)
402 if (sorting
!= this->sorting()) {
403 updateSorting(sorting
);
407 DolphinView::Sorting
DolphinView::sorting() const
409 return m_viewAccessor
.proxyModel()->sorting();
412 void DolphinView::setSortOrder(Qt::SortOrder order
)
414 if (sortOrder() != order
) {
415 updateSortOrder(order
);
419 Qt::SortOrder
DolphinView::sortOrder() const
421 return m_viewAccessor
.proxyModel()->sortOrder();
424 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
426 if (sortFoldersFirst() != foldersFirst
) {
427 updateSortFoldersFirst(foldersFirst
);
431 bool DolphinView::sortFoldersFirst() const
433 return m_viewAccessor
.proxyModel()->sortFoldersFirst();
436 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
438 const KUrl viewPropsUrl
= rootUrl();
439 ViewProperties
props(viewPropsUrl
);
440 props
.setAdditionalInfo(info
);
441 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
443 emit
additionalInfoChanged();
445 if (m_viewAccessor
.reloadOnAdditionalInfoChange()) {
446 loadDirectory(viewPropsUrl
);
450 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
452 return m_viewAccessor
.itemDelegate()->showInformation();
455 void DolphinView::reload()
458 loadDirectory(url(), true);
461 void DolphinView::refresh()
463 m_ignoreViewProperties
= false;
465 const bool oldActivationState
= m_active
;
466 const int oldZoomLevel
= m_controller
->zoomLevel();
470 applyViewProperties();
473 setActive(oldActivationState
);
474 updateZoomLevel(oldZoomLevel
);
477 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
479 Q_UNUSED(rootUrl
); // TODO: remove after columnview-cleanup has been finished
481 if (m_controller
->url() == url
) {
485 m_controller
->setUrl(url
); // emits urlChanged, which we forward
486 m_viewAccessor
.prepareUrlChange(url
);
487 applyViewProperties();
490 // When changing the URL there is no need to keep the version
491 // data of the previous URL.
492 m_viewAccessor
.dirModel()->clearVersionData();
494 emit
startedPathLoading(url
);
497 void DolphinView::setNameFilter(const QString
& nameFilter
)
499 m_controller
->setNameFilter(nameFilter
);
502 void DolphinView::calculateItemCount(int& fileCount
,
504 KIO::filesize_t
& totalFileSize
) const
506 foreach (const KFileItem
& item
, m_viewAccessor
.dirLister()->items()) {
511 totalFileSize
+= item
.size();
516 QString
DolphinView::statusBarText() const
521 KIO::filesize_t totalFileSize
= 0;
523 if (hasSelection()) {
524 // give a summary of the status of the selected files
525 const KFileItemList list
= selectedItems();
526 if (list
.isEmpty()) {
527 // when an item is triggered, it is temporary selected but selectedItems()
528 // will return an empty list
532 KFileItemList::const_iterator it
= list
.begin();
533 const KFileItemList::const_iterator end
= list
.end();
535 const KFileItem
& item
= *it
;
540 totalFileSize
+= item
.size();
545 if (folderCount
+ fileCount
== 1) {
546 // if only one item is selected, show the filename
547 const QString name
= list
.first().name();
548 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
549 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
550 name
, KIO::convertSize(totalFileSize
));
552 // at least 2 items are selected
553 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
554 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
555 if ((folderCount
> 0) && (fileCount
> 0)) {
556 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
557 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
558 } else if (fileCount
> 0) {
559 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
561 Q_ASSERT(folderCount
> 0);
566 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
567 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
568 fileCount
, folderCount
,
569 totalFileSize
, true);
575 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
577 return m_controller
->versionControlActions(items
);
580 void DolphinView::setUrl(const KUrl
& url
)
582 m_newFileNames
.clear();
583 updateView(url
, KUrl());
586 void DolphinView::changeSelection(const KFileItemList
& selection
)
589 if (selection
.isEmpty()) {
592 const KUrl
& baseUrl
= url();
594 QItemSelection newSelection
;
595 foreach(const KFileItem
& item
, selection
) {
596 url
= item
.url().upUrl();
597 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
598 QModelIndex index
= m_viewAccessor
.proxyModel()->mapFromSource(m_viewAccessor
.dirModel()->indexForItem(item
));
599 newSelection
.select(index
, index
);
602 m_viewAccessor
.itemView()->selectionModel()->select(newSelection
,
603 QItemSelectionModel::ClearAndSelect
604 | QItemSelectionModel::Current
);
607 void DolphinView::renameSelectedItems()
609 KFileItemList items
= selectedItems();
610 const int itemCount
= items
.count();
616 // More than one item has been selected for renaming. Open
617 // a rename dialog and rename all items afterwards.
618 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
619 if (dialog
->exec() == QDialog::Rejected
) {
624 const QString newName
= dialog
->newName();
625 if (newName
.isEmpty()) {
626 emit
errorMessage(dialog
->errorString());
632 // the selection would be invalid after renaming the items, so just clear
636 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
637 // as one operation instead of n rename operations like it is done now...
638 Q_ASSERT(newName
.contains('#'));
640 // currently the items are sorted by the selection order, resort
641 // them by the file name
642 qSort(items
.begin(), items
.end(), lessThan
);
644 // iterate through all selected items and rename them...
646 foreach (const KFileItem
& item
, items
) {
647 const KUrl
& oldUrl
= item
.url();
649 number
.setNum(index
++);
651 QString name
= newName
;
652 name
.replace('#', number
);
654 if (oldUrl
.fileName() != name
) {
655 KUrl newUrl
= oldUrl
;
656 newUrl
.setFileName(name
);
657 KonqOperations::rename(this, oldUrl
, newUrl
);
660 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
661 Q_ASSERT(itemCount
== 1);
662 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForItem(items
.first());
663 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
664 m_viewAccessor
.itemView()->edit(proxyIndex
);
666 Q_ASSERT(itemCount
== 1);
668 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
669 if (dialog
->exec() == QDialog::Rejected
) {
674 const QString newName
= dialog
->newName();
675 if (newName
.isEmpty()) {
676 emit
errorMessage(dialog
->errorString());
682 const KUrl
& oldUrl
= items
.first().url();
683 KUrl newUrl
= oldUrl
;
684 newUrl
.setFileName(newName
);
685 KonqOperations::rename(this, oldUrl
, newUrl
);
688 // assure that the current index remains visible when KDirLister
689 // will notify the view about changed items
690 m_assureVisibleCurrentIndex
= true;
693 void DolphinView::trashSelectedItems()
695 const KUrl::List list
= simplifiedSelectedUrls();
696 KonqOperations::del(this, KonqOperations::TRASH
, list
);
699 void DolphinView::deleteSelectedItems()
701 const KUrl::List list
= simplifiedSelectedUrls();
702 const bool del
= KonqOperations::askDeleteConfirmation(list
,
704 KonqOperations::DEFAULT_CONFIRMATION
,
708 KIO::Job
* job
= KIO::del(list
);
709 connect(job
, SIGNAL(result(KJob
*)),
710 this, SLOT(slotDeleteFileFinished(KJob
*)));
714 void DolphinView::cutSelectedItems()
716 QMimeData
* mimeData
= selectionMimeData();
717 KonqMimeData::addIsCutSelection(mimeData
, true);
718 QApplication::clipboard()->setMimeData(mimeData
);
721 void DolphinView::copySelectedItems()
723 QMimeData
* mimeData
= selectionMimeData();
724 QApplication::clipboard()->setMimeData(mimeData
);
727 void DolphinView::paste()
732 void DolphinView::pasteIntoFolder()
734 const KFileItemList items
= selectedItems();
735 if ((items
.count() == 1) && items
.first().isDir()) {
736 pasteToUrl(items
.first().url());
740 void DolphinView::setShowPreview(bool show
)
742 if (m_showPreview
== show
) {
746 const KUrl viewPropsUrl
= rootUrl();
747 ViewProperties
props(viewPropsUrl
);
748 props
.setShowPreview(show
);
750 m_showPreview
= show
;
751 const int oldZoomLevel
= m_controller
->zoomLevel();
752 emit
showPreviewChanged();
754 // Enabling or disabling the preview might change the icon size of the view.
755 // As the view does not emit a signal when the icon size has been changed,
756 // the used zoom level of the controller must be adjusted manually:
757 updateZoomLevel(oldZoomLevel
);
760 void DolphinView::setShowHiddenFiles(bool show
)
762 if (m_viewAccessor
.dirLister()->showingDotFiles() == show
) {
766 const KUrl viewPropsUrl
= rootUrl();
767 ViewProperties
props(viewPropsUrl
);
768 props
.setShowHiddenFiles(show
);
770 m_viewAccessor
.dirLister()->setShowingDotFiles(show
);
771 emit
showHiddenFilesChanged();
774 void DolphinView::setCategorizedSorting(bool categorized
)
776 if (categorized
== categorizedSorting()) {
780 // setCategorizedSorting(true) may only get invoked
781 // if the view supports categorized sorting
782 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
784 ViewProperties
props(rootUrl());
785 props
.setCategorizedSorting(categorized
);
788 m_storedCategorizedSorting
= categorized
;
789 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
791 emit
categorizedSortingChanged();
794 void DolphinView::toggleSortOrder()
796 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
797 Qt::DescendingOrder
:
802 void DolphinView::toggleSortFoldersFirst()
804 setSortFoldersFirst(!sortFoldersFirst());
807 void DolphinView::toggleAdditionalInfo(QAction
* action
)
809 const KFileItemDelegate::Information info
=
810 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
812 KFileItemDelegate::InformationList list
= additionalInfo();
814 const bool show
= action
->isChecked();
816 const int index
= list
.indexOf(info
);
817 const bool containsInfo
= (index
>= 0);
818 if (show
&& !containsInfo
) {
820 setAdditionalInfo(list
);
821 } else if (!show
&& containsInfo
) {
822 list
.removeAt(index
);
823 setAdditionalInfo(list
);
824 Q_ASSERT(list
.indexOf(info
) < 0);
828 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
830 QWidget::mouseReleaseEvent(event
);
834 void DolphinView::wheelEvent(QWheelEvent
* event
)
836 if (event
->modifiers() & Qt::ControlModifier
) {
837 const int delta
= event
->delta();
838 const int level
= zoomLevel();
840 setZoomLevel(level
+ 1);
841 } else if (delta
< 0) {
842 setZoomLevel(level
- 1);
848 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
850 switch (event
->type()) {
851 case QEvent::FocusIn
:
852 if (watched
== m_viewAccessor
.itemView()) {
853 m_controller
->requestActivation();
857 case QEvent::DragEnter
:
858 if (watched
== m_viewAccessor
.itemView()->viewport()) {
863 case QEvent::KeyPress
:
864 if (watched
== m_viewAccessor
.itemView()) {
865 // clear the selection when Escape has been pressed
866 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
867 if (keyEvent
->key() == Qt::Key_Escape
) {
877 return QWidget::eventFilter(watched
, event
);
880 void DolphinView::activate()
885 void DolphinView::triggerItem(const KFileItem
& item
)
887 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
888 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
889 // items are selected by the user, hence don't trigger the
890 // item specified by 'index'
894 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
895 if (item
.isNull() || m_isContextMenuOpen
) {
899 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
902 void DolphinView::emitDelayedSelectionChangedSignal()
904 // Invoke emitSelectionChangedSignal() with a delay of 300 ms. This assures
905 // that fast selection changes don't result in expensive operations to
906 // collect all file items for the signal (see DolphinView::selectedItems()).
907 m_selectionChangedTimer
->start();
910 void DolphinView::emitSelectionChangedSignal()
912 emit
selectionChanged(DolphinView::selectedItems());
915 void DolphinView::openContextMenu(const QPoint
& pos
,
916 const QList
<QAction
*>& customActions
)
919 const QModelIndex index
= m_viewAccessor
.itemView()->indexAt(pos
);
920 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
921 const QModelIndex dolphinModelIndex
= m_viewAccessor
.proxyModel()->mapToSource(index
);
922 item
= m_viewAccessor
.dirModel()->itemForIndex(dolphinModelIndex
);
925 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
926 emit
requestContextMenu(item
, url(), customActions
);
927 m_isContextMenuOpen
= false;
930 void DolphinView::dropUrls(const KFileItem
& destItem
,
931 const KUrl
& destPath
,
934 addNewFileNames(event
->mimeData());
935 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
938 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
940 ViewProperties
props(rootUrl());
941 props
.setSorting(sorting
);
943 m_viewAccessor
.proxyModel()->setSorting(sorting
);
945 emit
sortingChanged(sorting
);
948 void DolphinView::updateSortOrder(Qt::SortOrder order
)
950 ViewProperties
props(rootUrl());
951 props
.setSortOrder(order
);
953 m_viewAccessor
.proxyModel()->setSortOrder(order
);
955 emit
sortOrderChanged(order
);
958 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
960 ViewProperties
props(rootUrl());
961 props
.setSortFoldersFirst(foldersFirst
);
963 m_viewAccessor
.proxyModel()->setSortFoldersFirst(foldersFirst
);
965 emit
sortFoldersFirstChanged(foldersFirst
);
968 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
970 ViewProperties
props(rootUrl());
971 props
.setAdditionalInfo(info
);
974 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
976 emit
additionalInfoChanged();
979 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
981 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
982 (m_mode
== DolphinView::IconsView
);
984 QAction
* showSizeInfo
= collection
->action("show_size_info");
985 QAction
* showDateInfo
= collection
->action("show_date_info");
986 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
987 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
988 QAction
* showGroupInfo
= collection
->action("show_group_info");
989 QAction
* showMimeInfo
= collection
->action("show_mime_info");
991 showSizeInfo
->setChecked(false);
992 showDateInfo
->setChecked(false);
993 showPermissionsInfo
->setChecked(false);
994 showOwnerInfo
->setChecked(false);
995 showGroupInfo
->setChecked(false);
996 showMimeInfo
->setChecked(false);
998 showSizeInfo
->setEnabled(enable
);
999 showDateInfo
->setEnabled(enable
);
1000 showPermissionsInfo
->setEnabled(enable
);
1001 showOwnerInfo
->setEnabled(enable
);
1002 showGroupInfo
->setEnabled(enable
);
1003 showMimeInfo
->setEnabled(enable
);
1005 foreach (KFileItemDelegate::Information info
, m_viewAccessor
.itemDelegate()->showInformation()) {
1007 case KFileItemDelegate::Size
:
1008 showSizeInfo
->setChecked(true);
1010 case KFileItemDelegate::ModificationTime
:
1011 showDateInfo
->setChecked(true);
1013 case KFileItemDelegate::Permissions
:
1014 showPermissionsInfo
->setChecked(true);
1016 case KFileItemDelegate::Owner
:
1017 showOwnerInfo
->setChecked(true);
1019 case KFileItemDelegate::OwnerAndGroup
:
1020 showGroupInfo
->setChecked(true);
1022 case KFileItemDelegate::FriendlyMimeType
:
1023 showMimeInfo
->setChecked(true);
1031 QPair
<bool, QString
> DolphinView::pasteInfo() const
1033 return KonqOperations::pasteInfo(url());
1036 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1038 m_tabsForFiles
= tabsForFiles
;
1041 bool DolphinView::isTabsForFilesEnabled() const
1043 return m_tabsForFiles
;
1046 void DolphinView::activateItem(const KUrl
& url
)
1048 m_activeItemUrl
= url
;
1051 bool DolphinView::itemsExpandable() const
1053 return m_viewAccessor
.itemsExpandable();
1056 void DolphinView::observeCreatedItem(const KUrl
& url
)
1058 m_createdItemUrl
= url
;
1059 connect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1060 this, SLOT(selectAndScrollToCreatedItem()));
1063 void DolphinView::selectAndScrollToCreatedItem()
1065 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_createdItemUrl
);
1066 if (dirIndex
.isValid()) {
1067 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
1068 m_viewAccessor
.itemView()->setCurrentIndex(proxyIndex
);
1071 disconnect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1072 this, SLOT(selectAndScrollToCreatedItem()));
1073 m_createdItemUrl
= KUrl();
1076 void DolphinView::restoreSelection()
1078 disconnect(m_viewAccessor
.dirLister(), SIGNAL(completed()), this, SLOT(restoreSelection()));
1079 changeSelection(m_selectedItems
);
1082 void DolphinView::emitContentsMoved()
1084 // only emit the contents moved signal if no directory loading is ongoing
1085 // (this would reset the contents position always to (0, 0))
1086 if (!m_loadingDirectory
) {
1087 const QPoint
pos(contentsPosition());
1088 emit
contentsMoved(pos
.x(), pos
.y());
1092 void DolphinView::showHoverInformation(const KFileItem
& item
)
1094 emit
requestItemInfo(item
);
1097 void DolphinView::clearHoverInformation()
1099 emit
requestItemInfo(KFileItem());
1102 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1104 if (job
->error() == 0) {
1105 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1106 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1107 emit
errorMessage(job
->errorString());
1111 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1113 emit
requestUrlChange(url
);
1114 m_controller
->setUrl(url
);
1117 void DolphinView::slotDirListerCompleted()
1119 if (!m_activeItemUrl
.isEmpty()) {
1120 // assure that the current item remains visible
1121 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_activeItemUrl
);
1122 if (dirIndex
.isValid()) {
1123 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
1124 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1125 const bool clearSelection
= !hasSelection();
1126 view
->setCurrentIndex(proxyIndex
);
1127 if (clearSelection
) {
1128 view
->clearSelection();
1130 m_activeItemUrl
.clear();
1134 if (!m_newFileNames
.isEmpty()) {
1135 // select all newly added items created by a paste operation or
1136 // a drag & drop operation
1137 const int rowCount
= m_viewAccessor
.proxyModel()->rowCount();
1138 QItemSelection selection
;
1139 for (int row
= 0; row
< rowCount
; ++row
) {
1140 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->index(row
, 0);
1141 const QModelIndex dirIndex
= m_viewAccessor
.proxyModel()->mapToSource(proxyIndex
);
1142 const KUrl url
= m_viewAccessor
.dirModel()->itemForIndex(dirIndex
).url();
1143 if (m_newFileNames
.contains(url
.fileName())) {
1144 selection
.merge(QItemSelection(proxyIndex
, proxyIndex
), QItemSelectionModel::Select
);
1147 m_viewAccessor
.itemView()->selectionModel()->select(selection
, QItemSelectionModel::Select
);
1149 m_newFileNames
.clear();
1153 void DolphinView::slotRefreshItems()
1155 if (m_assureVisibleCurrentIndex
) {
1156 m_assureVisibleCurrentIndex
= false;
1157 m_viewAccessor
.itemView()->scrollTo(m_viewAccessor
.itemView()->currentIndex());
1161 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1163 if (!url
.isValid()) {
1164 const QString
location(url
.pathOrUrl());
1165 if (location
.isEmpty()) {
1166 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1168 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1173 m_loadingDirectory
= true;
1176 m_selectedItems
= selectedItems();
1177 connect(m_viewAccessor
.dirLister(), SIGNAL(completed()), this, SLOT(restoreSelection()));
1180 m_viewAccessor
.dirLister()->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1183 void DolphinView::applyViewProperties()
1185 if (m_ignoreViewProperties
) {
1189 const ViewProperties
props(rootUrl());
1191 const Mode mode
= props
.viewMode();
1192 if (m_mode
!= mode
) {
1193 const int oldZoomLevel
= m_controller
->zoomLevel();
1199 updateZoomLevel(oldZoomLevel
);
1201 if (m_viewAccessor
.itemView() == 0) {
1204 Q_ASSERT(m_viewAccessor
.itemView() != 0);
1205 Q_ASSERT(m_viewAccessor
.itemDelegate() != 0);
1207 const bool showHiddenFiles
= props
.showHiddenFiles();
1208 if (showHiddenFiles
!= m_viewAccessor
.dirLister()->showingDotFiles()) {
1209 m_viewAccessor
.dirLister()->setShowingDotFiles(showHiddenFiles
);
1210 emit
showHiddenFilesChanged();
1213 m_storedCategorizedSorting
= props
.categorizedSorting();
1214 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1215 if (categorized
!= m_viewAccessor
.proxyModel()->isCategorizedModel()) {
1216 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
1217 emit
categorizedSortingChanged();
1220 const DolphinView::Sorting sorting
= props
.sorting();
1221 if (sorting
!= m_viewAccessor
.proxyModel()->sorting()) {
1222 m_viewAccessor
.proxyModel()->setSorting(sorting
);
1223 emit
sortingChanged(sorting
);
1226 const Qt::SortOrder sortOrder
= props
.sortOrder();
1227 if (sortOrder
!= m_viewAccessor
.proxyModel()->sortOrder()) {
1228 m_viewAccessor
.proxyModel()->setSortOrder(sortOrder
);
1229 emit
sortOrderChanged(sortOrder
);
1232 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1233 if (sortFoldersFirst
!= m_viewAccessor
.proxyModel()->sortFoldersFirst()) {
1234 m_viewAccessor
.proxyModel()->setSortFoldersFirst(sortFoldersFirst
);
1235 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1238 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1239 if (info
!= m_viewAccessor
.itemDelegate()->showInformation()) {
1240 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
1241 emit
additionalInfoChanged();
1244 const bool showPreview
= props
.showPreview();
1245 if (showPreview
!= m_showPreview
) {
1246 m_showPreview
= showPreview
;
1247 const int oldZoomLevel
= m_controller
->zoomLevel();
1248 emit
showPreviewChanged();
1250 // Enabling or disabling the preview might change the icon size of the view.
1251 // As the view does not emit a signal when the icon size has been changed,
1252 // the used zoom level of the controller must be adjusted manually:
1253 updateZoomLevel(oldZoomLevel
);
1256 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1257 // During the lifetime of a DolphinView instance the global view properties
1258 // should not be changed. This allows e. g. to split a view and use different
1259 // view properties for each view.
1260 m_ignoreViewProperties
= true;
1264 void DolphinView::createView()
1268 Q_ASSERT(m_viewAccessor
.itemView() == 0);
1269 m_viewAccessor
.createView(this, m_controller
, m_mode
);
1271 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1272 Q_ASSERT(view
!= 0);
1273 view
->installEventFilter(this);
1274 view
->viewport()->installEventFilter(this);
1276 m_controller
->setItemView(view
);
1277 connect(m_controller
, SIGNAL(selectionChanged()),
1278 this, SLOT(emitDelayedSelectionChangedSignal()));
1280 // When changing the view mode, the selection is lost due to reinstantiating
1281 // a new item view with a custom selection model. Pass the ownership of the
1282 // selection model to DolphinView, so that it can be shared by all item views.
1283 if (m_selectionModel
!= 0) {
1284 view
->setSelectionModel(m_selectionModel
);
1286 m_selectionModel
= view
->selectionModel();
1288 m_selectionModel
->setParent(this);
1290 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1291 this, SLOT(emitContentsMoved()));
1292 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1293 this, SLOT(emitContentsMoved()));
1295 setFocusProxy(m_viewAccessor
.layoutTarget());
1296 m_topLayout
->insertWidget(1, m_viewAccessor
.layoutTarget());
1299 void DolphinView::deleteView()
1301 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1303 // It's important to set the keyboard focus to the parent
1304 // before deleting the view: Otherwise when having a split
1305 // view the other view will get the focus and will request
1306 // an activation (see DolphinView::eventFilter()).
1310 m_topLayout
->removeWidget(view
);
1313 // disconnect all signal/slots
1315 m_controller
->disconnect(view
);
1317 disconnect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1318 this, SLOT(emitContentsMoved()));
1319 disconnect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1320 this, SLOT(emitContentsMoved()));
1322 m_viewAccessor
.deleteView();
1326 void DolphinView::pasteToUrl(const KUrl
& url
)
1328 addNewFileNames(QApplication::clipboard()->mimeData());
1329 KonqOperations::doPaste(this, url
);
1332 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1334 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor
.itemView()->iconSize());
1335 if (oldZoomLevel
!= newZoomLevel
) {
1336 m_controller
->setZoomLevel(newZoomLevel
);
1337 emit
zoomLevelChanged(newZoomLevel
);
1341 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1343 KUrl::List list
= selectedUrls();
1344 if (itemsExpandable() ) {
1345 list
= KDirModel::simplifiedUrlList(list
);
1350 QMimeData
* DolphinView::selectionMimeData() const
1352 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
1353 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1354 const QItemSelection selection
= m_viewAccessor
.proxyModel()->mapSelectionToSource(view
->selectionModel()->selection());
1355 return m_viewAccessor
.dirModel()->mimeData(selection
.indexes());
1358 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1360 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1361 foreach (const KUrl
& url
, urls
) {
1362 m_newFileNames
.insert(url
.fileName());
1366 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel
* proxyModel
) :
1369 m_columnsContainer(0),
1370 m_proxyModel(proxyModel
),
1375 DolphinView::ViewAccessor::~ViewAccessor()
1377 delete m_dragSource
;
1381 void DolphinView::ViewAccessor::createView(QWidget
* parent
,
1382 DolphinController
* controller
,
1385 Q_ASSERT(itemView() == 0);
1389 m_iconsView
= new DolphinIconsView(parent
, controller
, m_proxyModel
);
1393 m_detailsView
= new DolphinDetailsView(parent
, controller
, m_proxyModel
);
1397 m_columnsContainer
= new DolphinColumnViewContainer(parent
, controller
);
1405 void DolphinView::ViewAccessor::deleteView()
1407 QAbstractItemView
* view
= itemView();
1409 if (DragAndDropHelper::instance().isDragSource(view
)) {
1410 // The view is a drag source (the feature "Open folders
1411 // during drag operations" is used). Deleting the view
1412 // during an ongoing drag operation is not allowed, so
1413 // this will postponed.
1414 if (m_dragSource
!= 0) {
1415 // the old stored view is obviously not the drag source anymore
1416 m_dragSource
->deleteLater();
1420 m_dragSource
= view
;
1422 view
->deleteLater();
1429 if (m_columnsContainer
!= 0) {
1430 m_columnsContainer
->deleteLater();
1432 m_columnsContainer
= 0;
1436 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl
& url
)
1438 if (m_columnsContainer
!= 0) {
1439 m_columnsContainer
->showColumn(url
);
1443 QAbstractItemView
* DolphinView::ViewAccessor::itemView() const
1445 if (m_iconsView
!= 0) {
1449 if (m_detailsView
!= 0) {
1450 return m_detailsView
;
1453 if (m_columnsContainer
!= 0) {
1454 return m_columnsContainer
->activeColumn();
1460 KFileItemDelegate
* DolphinView::ViewAccessor::itemDelegate() const
1462 return static_cast<KFileItemDelegate
*>(itemView()->itemDelegate());
1465 QWidget
* DolphinView::ViewAccessor::layoutTarget() const
1467 if (m_columnsContainer
!= 0) {
1468 return m_columnsContainer
;
1473 KUrl
DolphinView::ViewAccessor::rootUrl() const
1475 return (m_columnsContainer
!= 0) ? m_columnsContainer
->rootUrl() : KUrl();
1478 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1480 return m_iconsView
!= 0;
1483 bool DolphinView::ViewAccessor::itemsExpandable() const
1485 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1488 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1490 // the details view requires no reloading of the directory, as it maps
1491 // the file item delegate info to its columns internally
1492 return m_detailsView
!= 0;
1495 DolphinModel
* DolphinView::ViewAccessor::dirModel() const
1497 return static_cast<DolphinModel
*>(proxyModel()->sourceModel());
1500 DolphinSortFilterProxyModel
* DolphinView::ViewAccessor::proxyModel() const
1502 if (m_columnsContainer
!= 0) {
1503 return static_cast<DolphinSortFilterProxyModel
*>(m_columnsContainer
->activeColumn()->model());
1505 return m_proxyModel
;
1508 KDirLister
* DolphinView::ViewAccessor::dirLister() const
1510 return dirModel()->dirLister();
1513 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1515 emit
redirection(oldUrl
, newUrl
);
1516 m_controller
->redirectToUrl(newUrl
); // #186947
1519 #include "dolphinview.moc"