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, SIGNAL(redirection(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 return m_viewAccessor
.itemView()->selectionModel()->hasSelection();
315 void DolphinView::clearSelection()
317 QItemSelectionModel
* selModel
= m_viewAccessor
.itemView()->selectionModel();
318 const QModelIndex currentIndex
= selModel
->currentIndex();
319 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
320 QItemSelectionModel::Clear
);
321 m_selectedItems
.clear();
324 KFileItemList
DolphinView::selectedItems() const
326 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
328 // Our view has a selection, we will map them back to the DolphinModel
329 // and then fill the KFileItemList.
330 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
332 const QItemSelection selection
= m_viewAccessor
.proxyModel()->mapSelectionToSource(view
->selectionModel()->selection());
333 KFileItemList itemList
;
335 const QModelIndexList indexList
= selection
.indexes();
336 foreach (const QModelIndex
&index
, indexList
) {
337 KFileItem item
= m_viewAccessor
.dirModel()->itemForIndex(index
);
338 if (!item
.isNull()) {
339 itemList
.append(item
);
346 KUrl::List
DolphinView::selectedUrls() const
349 const KFileItemList list
= selectedItems();
350 foreach (const KFileItem
&item
, list
) {
351 urls
.append(item
.url());
356 int DolphinView::selectedItemsCount() const
358 return m_viewAccessor
.itemView()->selectionModel()->selectedIndexes().count();
361 void DolphinView::setContentsPosition(int x
, int y
)
363 QAbstractItemView
* view
= m_viewAccessor
.itemView();
364 view
->horizontalScrollBar()->setValue(x
);
365 view
->verticalScrollBar()->setValue(y
);
367 m_loadingDirectory
= false;
370 QPoint
DolphinView::contentsPosition() const
372 const int x
= m_viewAccessor
.itemView()->horizontalScrollBar()->value();
373 const int y
= m_viewAccessor
.itemView()->verticalScrollBar()->value();
377 void DolphinView::setZoomLevel(int level
)
379 if (level
< ZoomLevelInfo::minimumLevel()) {
380 level
= ZoomLevelInfo::minimumLevel();
381 } else if (level
> ZoomLevelInfo::maximumLevel()) {
382 level
= ZoomLevelInfo::maximumLevel();
385 if (level
!= zoomLevel()) {
386 m_controller
->setZoomLevel(level
);
387 emit
zoomLevelChanged(level
);
391 int DolphinView::zoomLevel() const
393 return m_controller
->zoomLevel();
396 void DolphinView::setSorting(Sorting sorting
)
398 if (sorting
!= this->sorting()) {
399 updateSorting(sorting
);
403 DolphinView::Sorting
DolphinView::sorting() const
405 return m_viewAccessor
.proxyModel()->sorting();
408 void DolphinView::setSortOrder(Qt::SortOrder order
)
410 if (sortOrder() != order
) {
411 updateSortOrder(order
);
415 Qt::SortOrder
DolphinView::sortOrder() const
417 return m_viewAccessor
.proxyModel()->sortOrder();
420 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
422 if (sortFoldersFirst() != foldersFirst
) {
423 updateSortFoldersFirst(foldersFirst
);
427 bool DolphinView::sortFoldersFirst() const
429 return m_viewAccessor
.proxyModel()->sortFoldersFirst();
432 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
434 const KUrl viewPropsUrl
= rootUrl();
435 ViewProperties
props(viewPropsUrl
);
436 props
.setAdditionalInfo(info
);
437 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
439 emit
additionalInfoChanged();
441 if (m_viewAccessor
.reloadOnAdditionalInfoChange()) {
442 loadDirectory(viewPropsUrl
);
446 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
448 return m_viewAccessor
.itemDelegate()->showInformation();
451 void DolphinView::reload()
454 loadDirectory(url(), true);
457 void DolphinView::refresh()
459 m_ignoreViewProperties
= false;
461 const bool oldActivationState
= m_active
;
462 const int oldZoomLevel
= m_controller
->zoomLevel();
466 applyViewProperties();
469 setActive(oldActivationState
);
470 updateZoomLevel(oldZoomLevel
);
473 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
475 Q_UNUSED(rootUrl
); // TODO: remove after columnview-cleanup has been finished
477 if (m_controller
->url() == url
) {
481 m_controller
->setUrl(url
); // emits urlChanged, which we forward
482 m_viewAccessor
.prepareUrlChange(url
);
483 applyViewProperties();
486 // When changing the URL there is no need to keep the version
487 // data of the previous URL.
488 m_viewAccessor
.dirModel()->clearVersionData();
490 emit
startedPathLoading(url
);
493 void DolphinView::setNameFilter(const QString
& nameFilter
)
495 m_controller
->setNameFilter(nameFilter
);
498 void DolphinView::calculateItemCount(int& fileCount
,
500 KIO::filesize_t
& totalFileSize
) const
502 foreach (const KFileItem
& item
, m_viewAccessor
.dirLister()->items()) {
507 totalFileSize
+= item
.size();
512 QString
DolphinView::statusBarText() const
517 KIO::filesize_t totalFileSize
= 0;
519 if (hasSelection()) {
520 // give a summary of the status of the selected files
521 const KFileItemList list
= selectedItems();
522 if (list
.isEmpty()) {
523 // when an item is triggered, it is temporary selected but selectedItems()
524 // will return an empty list
528 KFileItemList::const_iterator it
= list
.begin();
529 const KFileItemList::const_iterator end
= list
.end();
531 const KFileItem
& item
= *it
;
536 totalFileSize
+= item
.size();
541 if (folderCount
+ fileCount
== 1) {
542 // if only one item is selected, show the filename
543 const QString name
= list
.first().name();
544 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
545 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
546 name
, KIO::convertSize(totalFileSize
));
548 // at least 2 items are selected
549 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
550 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
551 if ((folderCount
> 0) && (fileCount
> 0)) {
552 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
553 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
554 } else if (fileCount
> 0) {
555 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
557 Q_ASSERT(folderCount
> 0);
562 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
563 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
564 fileCount
, folderCount
,
565 totalFileSize
, true);
571 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
573 return m_controller
->versionControlActions(items
);
576 void DolphinView::setUrl(const KUrl
& url
)
578 m_newFileNames
.clear();
579 updateView(url
, KUrl());
582 void DolphinView::changeSelection(const KFileItemList
& selection
)
585 if (selection
.isEmpty()) {
588 const KUrl
& baseUrl
= url();
590 QItemSelection newSelection
;
591 foreach(const KFileItem
& item
, selection
) {
592 url
= item
.url().upUrl();
593 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
594 QModelIndex index
= m_viewAccessor
.proxyModel()->mapFromSource(m_viewAccessor
.dirModel()->indexForItem(item
));
595 newSelection
.select(index
, index
);
598 m_viewAccessor
.itemView()->selectionModel()->select(newSelection
,
599 QItemSelectionModel::ClearAndSelect
600 | QItemSelectionModel::Current
);
603 void DolphinView::renameSelectedItems()
605 KFileItemList items
= selectedItems();
606 const int itemCount
= items
.count();
612 // More than one item has been selected for renaming. Open
613 // a rename dialog and rename all items afterwards.
614 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
615 if (dialog
->exec() == QDialog::Rejected
) {
620 const QString newName
= dialog
->newName();
621 if (newName
.isEmpty()) {
622 emit
errorMessage(dialog
->errorString());
628 // the selection would be invalid after renaming the items, so just clear
632 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
633 // as one operation instead of n rename operations like it is done now...
634 Q_ASSERT(newName
.contains('#'));
636 // currently the items are sorted by the selection order, resort
637 // them by the file name
638 qSort(items
.begin(), items
.end(), lessThan
);
640 // iterate through all selected items and rename them...
642 foreach (const KFileItem
& item
, items
) {
643 const KUrl
& oldUrl
= item
.url();
645 number
.setNum(index
++);
647 QString name
= newName
;
648 name
.replace('#', number
);
650 if (oldUrl
.fileName() != name
) {
651 KUrl newUrl
= oldUrl
;
652 newUrl
.setFileName(name
);
653 KonqOperations::rename(this, oldUrl
, newUrl
);
656 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
657 Q_ASSERT(itemCount
== 1);
658 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForItem(items
.first());
659 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
660 m_viewAccessor
.itemView()->edit(proxyIndex
);
662 Q_ASSERT(itemCount
== 1);
664 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
665 if (dialog
->exec() == QDialog::Rejected
) {
670 const QString newName
= dialog
->newName();
671 if (newName
.isEmpty()) {
672 emit
errorMessage(dialog
->errorString());
678 const KUrl
& oldUrl
= items
.first().url();
679 KUrl newUrl
= oldUrl
;
680 newUrl
.setFileName(newName
);
681 KonqOperations::rename(this, oldUrl
, newUrl
);
684 // assure that the current index remains visible when KDirLister
685 // will notify the view about changed items
686 m_assureVisibleCurrentIndex
= true;
689 void DolphinView::trashSelectedItems()
691 const KUrl::List list
= simplifiedSelectedUrls();
692 KonqOperations::del(this, KonqOperations::TRASH
, list
);
695 void DolphinView::deleteSelectedItems()
697 const KUrl::List list
= simplifiedSelectedUrls();
698 const bool del
= KonqOperations::askDeleteConfirmation(list
,
700 KonqOperations::DEFAULT_CONFIRMATION
,
704 KIO::Job
* job
= KIO::del(list
);
705 connect(job
, SIGNAL(result(KJob
*)),
706 this, SLOT(slotDeleteFileFinished(KJob
*)));
710 void DolphinView::cutSelectedItems()
712 QMimeData
* mimeData
= selectionMimeData();
713 KonqMimeData::addIsCutSelection(mimeData
, true);
714 QApplication::clipboard()->setMimeData(mimeData
);
717 void DolphinView::copySelectedItems()
719 QMimeData
* mimeData
= selectionMimeData();
720 QApplication::clipboard()->setMimeData(mimeData
);
723 void DolphinView::paste()
728 void DolphinView::pasteIntoFolder()
730 const KFileItemList items
= selectedItems();
731 if ((items
.count() == 1) && items
.first().isDir()) {
732 pasteToUrl(items
.first().url());
736 void DolphinView::setShowPreview(bool show
)
738 if (m_showPreview
== show
) {
742 const KUrl viewPropsUrl
= rootUrl();
743 ViewProperties
props(viewPropsUrl
);
744 props
.setShowPreview(show
);
746 m_showPreview
= show
;
747 const int oldZoomLevel
= m_controller
->zoomLevel();
748 emit
showPreviewChanged();
750 // Enabling or disabling the preview might change the icon size of the view.
751 // As the view does not emit a signal when the icon size has been changed,
752 // the used zoom level of the controller must be adjusted manually:
753 updateZoomLevel(oldZoomLevel
);
756 void DolphinView::setShowHiddenFiles(bool show
)
758 if (m_viewAccessor
.dirLister()->showingDotFiles() == show
) {
762 const KUrl viewPropsUrl
= rootUrl();
763 ViewProperties
props(viewPropsUrl
);
764 props
.setShowHiddenFiles(show
);
766 m_viewAccessor
.dirLister()->setShowingDotFiles(show
);
767 emit
showHiddenFilesChanged();
770 void DolphinView::setCategorizedSorting(bool categorized
)
772 if (categorized
== categorizedSorting()) {
776 // setCategorizedSorting(true) may only get invoked
777 // if the view supports categorized sorting
778 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
780 ViewProperties
props(rootUrl());
781 props
.setCategorizedSorting(categorized
);
784 m_storedCategorizedSorting
= categorized
;
785 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
787 emit
categorizedSortingChanged();
790 void DolphinView::toggleSortOrder()
792 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
793 Qt::DescendingOrder
:
798 void DolphinView::toggleSortFoldersFirst()
800 setSortFoldersFirst(!sortFoldersFirst());
803 void DolphinView::toggleAdditionalInfo(QAction
* action
)
805 const KFileItemDelegate::Information info
=
806 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
808 KFileItemDelegate::InformationList list
= additionalInfo();
810 const bool show
= action
->isChecked();
812 const int index
= list
.indexOf(info
);
813 const bool containsInfo
= (index
>= 0);
814 if (show
&& !containsInfo
) {
816 setAdditionalInfo(list
);
817 } else if (!show
&& containsInfo
) {
818 list
.removeAt(index
);
819 setAdditionalInfo(list
);
820 Q_ASSERT(list
.indexOf(info
) < 0);
824 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
826 QWidget::mouseReleaseEvent(event
);
830 void DolphinView::wheelEvent(QWheelEvent
* event
)
832 if (event
->modifiers() & Qt::ControlModifier
) {
833 const int delta
= event
->delta();
834 const int level
= zoomLevel();
836 setZoomLevel(level
+ 1);
837 } else if (delta
< 0) {
838 setZoomLevel(level
- 1);
844 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
846 switch (event
->type()) {
847 case QEvent::FocusIn
:
848 if (watched
== m_viewAccessor
.itemView()) {
849 m_controller
->requestActivation();
853 case QEvent::DragEnter
:
854 if (watched
== m_viewAccessor
.itemView()->viewport()) {
859 case QEvent::KeyPress
:
860 if (watched
== m_viewAccessor
.itemView()) {
861 // clear the selection when Escape has been pressed
862 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
863 if (keyEvent
->key() == Qt::Key_Escape
) {
873 return QWidget::eventFilter(watched
, event
);
876 void DolphinView::activate()
881 void DolphinView::triggerItem(const KFileItem
& item
)
883 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
884 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
885 // items are selected by the user, hence don't trigger the
886 // item specified by 'index'
890 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
891 if (item
.isNull() || m_isContextMenuOpen
) {
895 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
898 void DolphinView::emitDelayedSelectionChangedSignal()
900 // Invoke emitSelectionChangedSignal() with a delay of 300 ms. This assures
901 // that fast selection changes don't result in expensive operations to
902 // collect all file items for the signal (see DolphinView::selectedItems()).
903 m_selectionChangedTimer
->start();
906 void DolphinView::emitSelectionChangedSignal()
908 emit
selectionChanged(DolphinView::selectedItems());
911 void DolphinView::openContextMenu(const QPoint
& pos
,
912 const QList
<QAction
*>& customActions
)
915 const QModelIndex index
= m_viewAccessor
.itemView()->indexAt(pos
);
916 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
917 const QModelIndex dolphinModelIndex
= m_viewAccessor
.proxyModel()->mapToSource(index
);
918 item
= m_viewAccessor
.dirModel()->itemForIndex(dolphinModelIndex
);
921 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
922 emit
requestContextMenu(item
, url(), customActions
);
923 m_isContextMenuOpen
= false;
926 void DolphinView::dropUrls(const KFileItem
& destItem
,
927 const KUrl
& destPath
,
930 addNewFileNames(event
->mimeData());
931 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
934 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
936 ViewProperties
props(rootUrl());
937 props
.setSorting(sorting
);
939 m_viewAccessor
.proxyModel()->setSorting(sorting
);
941 emit
sortingChanged(sorting
);
944 void DolphinView::updateSortOrder(Qt::SortOrder order
)
946 ViewProperties
props(rootUrl());
947 props
.setSortOrder(order
);
949 m_viewAccessor
.proxyModel()->setSortOrder(order
);
951 emit
sortOrderChanged(order
);
954 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
956 ViewProperties
props(rootUrl());
957 props
.setSortFoldersFirst(foldersFirst
);
959 m_viewAccessor
.proxyModel()->setSortFoldersFirst(foldersFirst
);
961 emit
sortFoldersFirstChanged(foldersFirst
);
964 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
966 ViewProperties
props(rootUrl());
967 props
.setAdditionalInfo(info
);
970 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
972 emit
additionalInfoChanged();
975 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
977 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
978 (m_mode
== DolphinView::IconsView
);
980 QAction
* showSizeInfo
= collection
->action("show_size_info");
981 QAction
* showDateInfo
= collection
->action("show_date_info");
982 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
983 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
984 QAction
* showGroupInfo
= collection
->action("show_group_info");
985 QAction
* showMimeInfo
= collection
->action("show_mime_info");
987 showSizeInfo
->setChecked(false);
988 showDateInfo
->setChecked(false);
989 showPermissionsInfo
->setChecked(false);
990 showOwnerInfo
->setChecked(false);
991 showGroupInfo
->setChecked(false);
992 showMimeInfo
->setChecked(false);
994 showSizeInfo
->setEnabled(enable
);
995 showDateInfo
->setEnabled(enable
);
996 showPermissionsInfo
->setEnabled(enable
);
997 showOwnerInfo
->setEnabled(enable
);
998 showGroupInfo
->setEnabled(enable
);
999 showMimeInfo
->setEnabled(enable
);
1001 foreach (KFileItemDelegate::Information info
, m_viewAccessor
.itemDelegate()->showInformation()) {
1003 case KFileItemDelegate::Size
:
1004 showSizeInfo
->setChecked(true);
1006 case KFileItemDelegate::ModificationTime
:
1007 showDateInfo
->setChecked(true);
1009 case KFileItemDelegate::Permissions
:
1010 showPermissionsInfo
->setChecked(true);
1012 case KFileItemDelegate::Owner
:
1013 showOwnerInfo
->setChecked(true);
1015 case KFileItemDelegate::OwnerAndGroup
:
1016 showGroupInfo
->setChecked(true);
1018 case KFileItemDelegate::FriendlyMimeType
:
1019 showMimeInfo
->setChecked(true);
1027 QPair
<bool, QString
> DolphinView::pasteInfo() const
1029 return KonqOperations::pasteInfo(url());
1032 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1034 m_tabsForFiles
= tabsForFiles
;
1037 bool DolphinView::isTabsForFilesEnabled() const
1039 return m_tabsForFiles
;
1042 void DolphinView::activateItem(const KUrl
& url
)
1044 m_activeItemUrl
= url
;
1047 bool DolphinView::itemsExpandable() const
1049 return m_viewAccessor
.itemsExpandable();
1052 void DolphinView::observeCreatedItem(const KUrl
& url
)
1054 m_createdItemUrl
= url
;
1055 connect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1056 this, SLOT(selectAndScrollToCreatedItem()));
1059 void DolphinView::selectAndScrollToCreatedItem()
1061 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_createdItemUrl
);
1062 if (dirIndex
.isValid()) {
1063 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
1064 m_viewAccessor
.itemView()->setCurrentIndex(proxyIndex
);
1067 disconnect(m_viewAccessor
.dirModel(), SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1068 this, SLOT(selectAndScrollToCreatedItem()));
1069 m_createdItemUrl
= KUrl();
1072 void DolphinView::restoreSelection()
1074 disconnect(m_viewAccessor
.dirLister(), SIGNAL(completed()), this, SLOT(restoreSelection()));
1075 changeSelection(m_selectedItems
);
1078 void DolphinView::emitContentsMoved()
1080 // only emit the contents moved signal if no directory loading is ongoing
1081 // (this would reset the contents position always to (0, 0))
1082 if (!m_loadingDirectory
) {
1083 const QPoint
pos(contentsPosition());
1084 emit
contentsMoved(pos
.x(), pos
.y());
1088 void DolphinView::showHoverInformation(const KFileItem
& item
)
1090 emit
requestItemInfo(item
);
1093 void DolphinView::clearHoverInformation()
1095 emit
requestItemInfo(KFileItem());
1098 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1100 if (job
->error() == 0) {
1101 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1102 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1103 emit
errorMessage(job
->errorString());
1107 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1109 emit
requestUrlChange(url
);
1110 m_controller
->setUrl(url
);
1113 void DolphinView::slotDirListerCompleted()
1115 if (!m_activeItemUrl
.isEmpty()) {
1116 // assure that the current item remains visible
1117 const QModelIndex dirIndex
= m_viewAccessor
.dirModel()->indexForUrl(m_activeItemUrl
);
1118 if (dirIndex
.isValid()) {
1119 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->mapFromSource(dirIndex
);
1120 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1121 const bool clearSelection
= !hasSelection();
1122 view
->setCurrentIndex(proxyIndex
);
1123 if (clearSelection
) {
1124 view
->clearSelection();
1126 m_activeItemUrl
.clear();
1130 if (!m_newFileNames
.isEmpty()) {
1131 // select all newly added items created by a paste operation or
1132 // a drag & drop operation
1133 const int rowCount
= m_viewAccessor
.proxyModel()->rowCount();
1134 QItemSelection selection
;
1135 for (int row
= 0; row
< rowCount
; ++row
) {
1136 const QModelIndex proxyIndex
= m_viewAccessor
.proxyModel()->index(row
, 0);
1137 const QModelIndex dirIndex
= m_viewAccessor
.proxyModel()->mapToSource(proxyIndex
);
1138 const KUrl url
= m_viewAccessor
.dirModel()->itemForIndex(dirIndex
).url();
1139 if (m_newFileNames
.contains(url
.fileName())) {
1140 selection
.merge(QItemSelection(proxyIndex
, proxyIndex
), QItemSelectionModel::Select
);
1143 m_viewAccessor
.itemView()->selectionModel()->select(selection
, QItemSelectionModel::Select
);
1145 m_newFileNames
.clear();
1149 void DolphinView::slotRefreshItems()
1151 if (m_assureVisibleCurrentIndex
) {
1152 m_assureVisibleCurrentIndex
= false;
1153 m_viewAccessor
.itemView()->scrollTo(m_viewAccessor
.itemView()->currentIndex());
1157 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1159 if (!url
.isValid()) {
1160 const QString
location(url
.pathOrUrl());
1161 if (location
.isEmpty()) {
1162 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1164 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1169 m_loadingDirectory
= true;
1172 m_selectedItems
= selectedItems();
1173 connect(m_viewAccessor
.dirLister(), SIGNAL(completed()), this, SLOT(restoreSelection()));
1176 m_viewAccessor
.dirLister()->stop();
1177 m_viewAccessor
.dirLister()->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1180 void DolphinView::applyViewProperties()
1182 if (m_ignoreViewProperties
) {
1186 const ViewProperties
props(rootUrl());
1188 const Mode mode
= props
.viewMode();
1189 if (m_mode
!= mode
) {
1190 const int oldZoomLevel
= m_controller
->zoomLevel();
1196 updateZoomLevel(oldZoomLevel
);
1198 if (m_viewAccessor
.itemView() == 0) {
1201 Q_ASSERT(m_viewAccessor
.itemView() != 0);
1202 Q_ASSERT(m_viewAccessor
.itemDelegate() != 0);
1204 const bool showHiddenFiles
= props
.showHiddenFiles();
1205 if (showHiddenFiles
!= m_viewAccessor
.dirLister()->showingDotFiles()) {
1206 m_viewAccessor
.dirLister()->setShowingDotFiles(showHiddenFiles
);
1207 emit
showHiddenFilesChanged();
1210 m_storedCategorizedSorting
= props
.categorizedSorting();
1211 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1212 if (categorized
!= m_viewAccessor
.proxyModel()->isCategorizedModel()) {
1213 m_viewAccessor
.proxyModel()->setCategorizedModel(categorized
);
1214 emit
categorizedSortingChanged();
1217 const DolphinView::Sorting sorting
= props
.sorting();
1218 if (sorting
!= m_viewAccessor
.proxyModel()->sorting()) {
1219 m_viewAccessor
.proxyModel()->setSorting(sorting
);
1220 emit
sortingChanged(sorting
);
1223 const Qt::SortOrder sortOrder
= props
.sortOrder();
1224 if (sortOrder
!= m_viewAccessor
.proxyModel()->sortOrder()) {
1225 m_viewAccessor
.proxyModel()->setSortOrder(sortOrder
);
1226 emit
sortOrderChanged(sortOrder
);
1229 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1230 if (sortFoldersFirst
!= m_viewAccessor
.proxyModel()->sortFoldersFirst()) {
1231 m_viewAccessor
.proxyModel()->setSortFoldersFirst(sortFoldersFirst
);
1232 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1235 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1236 if (info
!= m_viewAccessor
.itemDelegate()->showInformation()) {
1237 m_viewAccessor
.itemDelegate()->setShowInformation(info
);
1238 emit
additionalInfoChanged();
1241 const bool showPreview
= props
.showPreview();
1242 if (showPreview
!= m_showPreview
) {
1243 m_showPreview
= showPreview
;
1244 const int oldZoomLevel
= m_controller
->zoomLevel();
1245 emit
showPreviewChanged();
1247 // Enabling or disabling the preview might change the icon size of the view.
1248 // As the view does not emit a signal when the icon size has been changed,
1249 // the used zoom level of the controller must be adjusted manually:
1250 updateZoomLevel(oldZoomLevel
);
1253 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1254 // During the lifetime of a DolphinView instance the global view properties
1255 // should not be changed. This allows e. g. to split a view and use different
1256 // view properties for each view.
1257 m_ignoreViewProperties
= true;
1261 void DolphinView::createView()
1264 Q_ASSERT(m_viewAccessor
.itemView() == 0);
1265 m_viewAccessor
.createView(this, m_controller
, m_mode
);
1267 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1268 Q_ASSERT(view
!= 0);
1269 view
->installEventFilter(this);
1270 view
->viewport()->installEventFilter(this);
1272 m_controller
->setItemView(view
);
1273 connect(m_controller
, SIGNAL(selectionChanged()),
1274 this, SLOT(emitDelayedSelectionChangedSignal()));
1276 // When changing the view mode, the selection is lost due to reinstantiating
1277 // a new item view with a custom selection model. Pass the ownership of the
1278 // selection model to DolphinView, so that it can be shared by all item views.
1279 if (m_selectionModel
!= 0) {
1280 view
->setSelectionModel(m_selectionModel
);
1282 m_selectionModel
= view
->selectionModel();
1284 m_selectionModel
->setParent(this);
1286 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1287 this, SLOT(emitContentsMoved()));
1288 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1289 this, SLOT(emitContentsMoved()));
1291 setFocusProxy(m_viewAccessor
.layoutTarget());
1292 m_topLayout
->insertWidget(1, m_viewAccessor
.layoutTarget());
1295 void DolphinView::deleteView()
1297 QAbstractItemView
* view
= m_viewAccessor
.itemView();
1299 // It's important to set the keyboard focus to the parent
1300 // before deleting the view: Otherwise when having a split
1301 // view the other view will get the focus and will request
1302 // an activation (see DolphinView::eventFilter()).
1306 m_topLayout
->removeWidget(view
);
1310 m_controller
->disconnect(view
);
1313 m_viewAccessor
.deleteView();
1317 void DolphinView::pasteToUrl(const KUrl
& url
)
1319 addNewFileNames(QApplication::clipboard()->mimeData());
1320 KonqOperations::doPaste(this, url
);
1323 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1325 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor
.itemView()->iconSize());
1326 if (oldZoomLevel
!= newZoomLevel
) {
1327 m_controller
->setZoomLevel(newZoomLevel
);
1328 emit
zoomLevelChanged(newZoomLevel
);
1332 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1334 KUrl::List list
= selectedUrls();
1335 if (itemsExpandable() ) {
1336 list
= KDirModel::simplifiedUrlList(list
);
1341 QMimeData
* DolphinView::selectionMimeData() const
1343 const QAbstractItemView
* view
= m_viewAccessor
.itemView();
1344 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1345 const QItemSelection selection
= m_viewAccessor
.proxyModel()->mapSelectionToSource(view
->selectionModel()->selection());
1346 return m_viewAccessor
.dirModel()->mimeData(selection
.indexes());
1349 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1351 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1352 foreach (const KUrl
& url
, urls
) {
1353 m_newFileNames
.insert(url
.fileName());
1357 DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel
* proxyModel
) :
1360 m_columnsContainer(0),
1361 m_proxyModel(proxyModel
),
1366 DolphinView::ViewAccessor::~ViewAccessor()
1368 delete m_dragSource
;
1372 void DolphinView::ViewAccessor::createView(QWidget
* parent
,
1373 DolphinController
* controller
,
1376 Q_ASSERT(itemView() == 0);
1380 m_iconsView
= new DolphinIconsView(parent
, controller
, m_proxyModel
);
1384 m_detailsView
= new DolphinDetailsView(parent
, controller
, m_proxyModel
);
1388 m_columnsContainer
= new DolphinColumnViewContainer(parent
, controller
);
1396 void DolphinView::ViewAccessor::deleteView()
1398 QAbstractItemView
* view
= itemView();
1400 if (DragAndDropHelper::instance().isDragSource(view
)) {
1401 // The view is a drag source (the feature "Open folders
1402 // during drag operations" is used). Deleting the view
1403 // during an ongoing drag operation is not allowed, so
1404 // this will postponed.
1405 if (m_dragSource
!= 0) {
1406 // the old stored view is obviously not the drag source anymore
1407 m_dragSource
->deleteLater();
1411 m_dragSource
= view
;
1413 view
->deleteLater();
1420 m_columnsContainer
->deleteLater();
1421 m_columnsContainer
= 0;
1425 void DolphinView::ViewAccessor::prepareUrlChange(const KUrl
& url
)
1427 if (m_columnsContainer
!= 0) {
1428 m_columnsContainer
->showColumn(url
);
1432 QAbstractItemView
* DolphinView::ViewAccessor::itemView() const
1434 if (m_iconsView
!= 0) {
1438 if (m_detailsView
!= 0) {
1439 return m_detailsView
;
1442 if (m_columnsContainer
!= 0) {
1443 return m_columnsContainer
->activeColumn();
1449 KFileItemDelegate
* DolphinView::ViewAccessor::itemDelegate() const
1451 return static_cast<KFileItemDelegate
*>(itemView()->itemDelegate());
1454 QWidget
* DolphinView::ViewAccessor::layoutTarget() const
1456 if (m_columnsContainer
!= 0) {
1457 return m_columnsContainer
;
1462 KUrl
DolphinView::ViewAccessor::rootUrl() const
1464 return (m_columnsContainer
!= 0) ? m_columnsContainer
->rootUrl() : KUrl();
1467 bool DolphinView::ViewAccessor::supportsCategorizedSorting() const
1469 return m_iconsView
!= 0;
1472 bool DolphinView::ViewAccessor::itemsExpandable() const
1474 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1477 bool DolphinView::ViewAccessor::reloadOnAdditionalInfoChange() const
1479 // the details view requires no reloading of the directory, as it maps
1480 // the file item delegate info to its columns internally
1481 return m_detailsView
!= 0;
1484 DolphinModel
* DolphinView::ViewAccessor::dirModel() const
1486 return static_cast<DolphinModel
*>(proxyModel()->sourceModel());
1489 DolphinSortFilterProxyModel
* DolphinView::ViewAccessor::proxyModel() const
1491 if (m_columnsContainer
!= 0) {
1492 return static_cast<DolphinSortFilterProxyModel
*>(m_columnsContainer
->activeColumn()->model());
1494 return m_proxyModel
;
1497 KDirLister
* DolphinView::ViewAccessor::dirLister() const
1499 return dirModel()->dirLister();
1502 #include "dolphinview.moc"