1 /***************************************************************************
2 * Copyright (C) 2006 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 <QApplication>
26 #include <QItemSelection>
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfilepreviewgenerator.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 "dolphincolumnview.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 "folderexpander.h"
64 #include "renamedialog.h"
65 #include "tooltips/tooltipmanager.h"
66 #include "settings/dolphinsettings.h"
67 #include "viewproperties.h"
68 #include "zoomlevelinfo.h"
73 * Helper function for sorting items with qSort() in
74 * DolphinView::renameSelectedItems().
76 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
78 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
81 DolphinView::DolphinView(QWidget
* parent
,
83 KDirLister
* dirLister
,
84 DolphinModel
* dolphinModel
,
85 DolphinSortFilterProxyModel
* proxyModel
) :
89 m_loadingDirectory(false),
90 m_storedCategorizedSorting(false),
91 m_tabsForFiles(false),
92 m_isContextMenuOpen(false),
93 m_ignoreViewProperties(false),
94 m_assureVisibleCurrentIndex(false),
95 m_mode(DolphinView::IconsView
),
101 m_fileItemDelegate(0),
103 m_dolphinModel(dolphinModel
),
104 m_dirLister(dirLister
),
105 m_proxyModel(proxyModel
),
106 m_previewGenerator(0),
113 m_expandedDragSource(0)
115 m_topLayout
= new QVBoxLayout(this);
116 m_topLayout
->setSpacing(0);
117 m_topLayout
->setMargin(0);
119 m_controller
= new DolphinController(this);
120 m_controller
->setUrl(url
);
122 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
123 this, SIGNAL(urlChanged(const KUrl
&)));
124 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
125 this, SLOT(slotRequestUrlChange(const KUrl
&)));
127 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
128 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
129 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
130 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
131 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
132 this, SLOT(updateSorting(DolphinView::Sorting
)));
133 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
134 this, SLOT(updateSortOrder(Qt::SortOrder
)));
135 connect(m_controller
, SIGNAL(sortFoldersFirstChanged(bool)),
136 this, SLOT(updateSortFoldersFirst(bool)));
137 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
138 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
139 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
140 this, SLOT(triggerItem(const KFileItem
&)));
141 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
142 this, SIGNAL(tabRequested(const KUrl
&)));
143 connect(m_controller
, SIGNAL(activated()),
144 this, SLOT(activate()));
145 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
146 this, SLOT(showHoverInformation(const KFileItem
&)));
147 connect(m_controller
, SIGNAL(viewportEntered()),
148 this, SLOT(clearHoverInformation()));
150 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
151 this, SIGNAL(redirection(KUrl
, KUrl
)));
152 connect(m_dirLister
, SIGNAL(completed()),
153 this, SLOT(slotDirListerCompleted()));
154 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
155 this, SLOT(slotRefreshItems()));
157 // When a new item has been created by the "Create New..." menu, the item should
158 // get selected and it must be assured that the item will get visible. As the
159 // creation is done asynchronously, several signals must be checked:
160 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
161 this, SLOT(observeCreatedItem(const KUrl
&)));
163 applyViewProperties(url
);
164 m_topLayout
->addWidget(itemView());
167 DolphinView::~DolphinView()
169 delete m_expandedDragSource
;
170 m_expandedDragSource
= 0;
173 const KUrl
& DolphinView::url() const
175 return m_controller
->url();
178 KUrl
DolphinView::rootUrl() const
180 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
183 void DolphinView::setActive(bool active
)
185 if (active
== m_active
) {
191 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
193 emit
selectionChanged(selectedItems());
198 QWidget
* viewport
= itemView()->viewport();
200 palette
.setColor(viewport
->backgroundRole(), color
);
201 viewport
->setPalette(palette
);
206 itemView()->setFocus();
210 m_controller
->indicateActivationChange(active
);
213 bool DolphinView::isActive() const
218 void DolphinView::setMode(Mode mode
)
220 if (mode
== m_mode
) {
221 return; // the wished mode is already set
224 const int oldZoomLevel
= m_controller
->zoomLevel();
229 const KUrl viewPropsUrl
= viewPropertiesUrl();
230 ViewProperties
props(viewPropsUrl
);
231 props
.setViewMode(m_mode
);
234 // the file item delegate has been recreated, apply the current
235 // additional information manually
236 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
237 m_fileItemDelegate
->setShowInformation(infoList
);
238 emit
additionalInfoChanged();
240 // Not all view modes support categorized sorting. Adjust the sorting model
241 // if changing the view mode results in a change of the categorized sorting
243 m_storedCategorizedSorting
= props
.categorizedSorting();
244 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
245 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
246 m_proxyModel
->setCategorizedModel(categorized
);
247 emit
categorizedSortingChanged();
252 updateZoomLevel(oldZoomLevel
);
254 loadDirectory(viewPropsUrl
);
258 DolphinView::Mode
DolphinView::mode() const
263 bool DolphinView::showPreview() const
265 return m_showPreview
;
268 bool DolphinView::showHiddenFiles() const
270 return m_dirLister
->showingDotFiles();
273 bool DolphinView::categorizedSorting() const
275 // If all view modes would support categorized sorting, returning
276 // m_proxyModel->isCategorizedModel() would be the way to go. As
277 // currently only the icons view supports caterized sorting, we remember
278 // the stored view properties state in m_storedCategorizedSorting and
279 // return this state. The application takes care to disable the corresponding
280 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
281 // that this setting is not applied to the current view mode.
282 return m_storedCategorizedSorting
;
285 bool DolphinView::supportsCategorizedSorting() const
287 return m_iconsView
!= 0;
290 void DolphinView::selectAll()
292 QAbstractItemView
* view
= itemView();
293 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
294 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
295 // selection instead of selecting all items. This is bypassed for KDE 4.0
296 // by invoking clearSelection() first.
297 view
->clearSelection();
301 void DolphinView::invertSelection()
303 if (isColumnViewActive()) {
304 // QAbstractItemView does not offer a virtual method invertSelection()
305 // as counterpart to QAbstractItemView::selectAll(). This makes it
306 // necessary to delegate the inverting of the selection to the
307 // column view, as only the selection of the active column should
309 m_columnView
->invertSelection();
311 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
312 const QAbstractItemModel
* itemModel
= selectionModel
->model();
314 const QModelIndex topLeft
= itemModel
->index(0, 0);
315 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
316 itemModel
->columnCount() - 1);
318 const QItemSelection
selection(topLeft
, bottomRight
);
319 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
323 bool DolphinView::hasSelection() const
325 return itemView()->selectionModel()->hasSelection();
328 void DolphinView::clearSelection()
330 QItemSelectionModel
* selModel
= itemView()->selectionModel();
331 const QModelIndex currentIndex
= selModel
->currentIndex();
332 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
333 QItemSelectionModel::Clear
);
336 KFileItemList
DolphinView::selectedItems() const
338 if (isColumnViewActive()) {
339 return m_columnView
->selectedItems();
342 const QAbstractItemView
* view
= itemView();
344 // Our view has a selection, we will map them back to the DolphinModel
345 // and then fill the KFileItemList.
346 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
348 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
349 KFileItemList itemList
;
351 const QModelIndexList indexList
= selection
.indexes();
352 foreach (const QModelIndex
&index
, indexList
) {
353 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
354 if (!item
.isNull()) {
355 itemList
.append(item
);
362 KUrl::List
DolphinView::selectedUrls() const
365 const KFileItemList list
= selectedItems();
366 foreach (const KFileItem
&item
, list
) {
367 urls
.append(item
.url());
372 int DolphinView::selectedItemsCount() const
374 if (isColumnViewActive()) {
375 // TODO: get rid of this special case by adjusting the dir lister
376 // to the current column
377 return m_columnView
->selectedItems().count();
380 return itemView()->selectionModel()->selection().count();
383 void DolphinView::setContentsPosition(int x
, int y
)
385 QAbstractItemView
* view
= itemView();
387 // the ColumnView takes care itself for the horizontal scrolling
388 if (!isColumnViewActive()) {
389 view
->horizontalScrollBar()->setValue(x
);
391 view
->verticalScrollBar()->setValue(y
);
393 m_loadingDirectory
= false;
396 QPoint
DolphinView::contentsPosition() const
398 const int x
= itemView()->horizontalScrollBar()->value();
399 const int y
= itemView()->verticalScrollBar()->value();
403 void DolphinView::setZoomLevel(int level
)
405 if (level
< ZoomLevelInfo::minimumLevel()) {
406 level
= ZoomLevelInfo::minimumLevel();
407 } else if (level
> ZoomLevelInfo::maximumLevel()) {
408 level
= ZoomLevelInfo::maximumLevel();
411 if (level
!= zoomLevel()) {
412 m_controller
->setZoomLevel(level
);
413 m_previewGenerator
->updateIcons();
414 emit
zoomLevelChanged(level
);
418 int DolphinView::zoomLevel() const
420 return m_controller
->zoomLevel();
423 void DolphinView::setSorting(Sorting sorting
)
425 if (sorting
!= this->sorting()) {
426 updateSorting(sorting
);
430 DolphinView::Sorting
DolphinView::sorting() const
432 return m_proxyModel
->sorting();
435 void DolphinView::setSortOrder(Qt::SortOrder order
)
437 if (sortOrder() != order
) {
438 updateSortOrder(order
);
442 Qt::SortOrder
DolphinView::sortOrder() const
444 return m_proxyModel
->sortOrder();
447 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
449 if (sortFoldersFirst() != foldersFirst
) {
450 updateSortFoldersFirst(foldersFirst
);
454 bool DolphinView::sortFoldersFirst() const
456 return m_proxyModel
->sortFoldersFirst();
459 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
461 const KUrl viewPropsUrl
= viewPropertiesUrl();
462 ViewProperties
props(viewPropsUrl
);
463 props
.setAdditionalInfo(info
);
464 m_fileItemDelegate
->setShowInformation(info
);
466 emit
additionalInfoChanged();
468 if (itemView() != m_detailsView
) {
469 // the details view requires no reloading of the directory, as it maps
470 // the file item delegate info to its columns internally
471 loadDirectory(viewPropsUrl
);
475 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
477 return m_fileItemDelegate
->showInformation();
480 void DolphinView::reload()
483 loadDirectory(url(), true);
486 void DolphinView::refresh()
488 m_ignoreViewProperties
= false;
490 const bool oldActivationState
= m_active
;
491 const int oldZoomLevel
= m_controller
->zoomLevel();
495 applyViewProperties(m_controller
->url());
498 setActive(oldActivationState
);
499 updateZoomLevel(oldZoomLevel
);
502 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
504 if (m_controller
->url() == url
) {
508 m_previewGenerator
->cancelPreviews();
509 m_controller
->setUrl(url
); // emits urlChanged, which we forward
511 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
512 applyViewProperties(rootUrl
);
513 loadDirectory(rootUrl
);
514 if (itemView() == m_columnView
) {
515 m_columnView
->setRootUrl(rootUrl
);
516 m_columnView
->showColumn(url
);
519 applyViewProperties(url
);
523 emit
startedPathLoading(url
);
526 void DolphinView::setNameFilter(const QString
& nameFilter
)
528 m_proxyModel
->setFilterRegExp(nameFilter
);
530 if (isColumnViewActive()) {
531 // adjusting the directory lister is not enough in the case of the
532 // column view, as each column has its own directory lister internally...
533 m_columnView
->setNameFilter(nameFilter
);
537 void DolphinView::calculateItemCount(int& fileCount
,
539 KIO::filesize_t
& totalFileSize
) const
541 foreach (const KFileItem
& item
, m_dirLister
->items()) {
546 totalFileSize
+= item
.size();
551 QString
DolphinView::statusBarText() const
556 KIO::filesize_t totalFileSize
= 0;
558 if (hasSelection()) {
559 // give a summary of the status of the selected files
560 const KFileItemList list
= selectedItems();
561 if (list
.isEmpty()) {
562 // when an item is triggered, it is temporary selected but selectedItems()
563 // will return an empty list
567 KFileItemList::const_iterator it
= list
.begin();
568 const KFileItemList::const_iterator end
= list
.end();
570 const KFileItem
& item
= *it
;
575 totalFileSize
+= item
.size();
580 if (folderCount
+ fileCount
== 1) {
581 // if only one item is selected, show the filename
582 const QString name
= list
.first().name();
583 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
584 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
585 name
, KIO::convertSize(totalFileSize
));
587 // at least 2 items are selected
588 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
589 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
590 if ((folderCount
> 0) && (fileCount
> 0)) {
591 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
592 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
593 } else if (fileCount
> 0) {
594 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
596 Q_ASSERT(folderCount
> 0);
601 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
602 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
603 fileCount
, folderCount
,
604 totalFileSize
, true);
610 void DolphinView::setUrl(const KUrl
& url
)
612 m_newFileNames
.clear();
614 // remember current item candidate (see slotDirListerCompleted())
615 m_currentItemUrl
= url
;
616 updateView(url
, KUrl());
619 void DolphinView::changeSelection(const KFileItemList
& selection
)
622 if (selection
.isEmpty()) {
625 const KUrl
& baseUrl
= url();
627 QItemSelection newSelection
;
628 foreach(const KFileItem
& item
, selection
) {
629 url
= item
.url().upUrl();
630 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
631 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
632 newSelection
.select(index
, index
);
635 itemView()->selectionModel()->select(newSelection
,
636 QItemSelectionModel::ClearAndSelect
637 | QItemSelectionModel::Current
);
640 void DolphinView::renameSelectedItems()
642 KFileItemList items
= selectedItems();
643 const int itemCount
= items
.count();
649 // More than one item has been selected for renaming. Open
650 // a rename dialog and rename all items afterwards.
651 RenameDialog
dialog(this, items
);
652 if (dialog
.exec() == QDialog::Rejected
) {
656 const QString newName
= dialog
.newName();
657 if (newName
.isEmpty()) {
658 emit
errorMessage(dialog
.errorString());
662 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
663 // as one operation instead of n rename operations like it is done now...
664 Q_ASSERT(newName
.contains('#'));
666 // currently the items are sorted by the selection order, resort
667 // them by the file name
668 qSort(items
.begin(), items
.end(), lessThan
);
670 // iterate through all selected items and rename them...
672 foreach (const KFileItem
& item
, items
) {
673 const KUrl
& oldUrl
= item
.url();
675 number
.setNum(index
++);
677 QString name
= newName
;
678 name
.replace('#', number
);
680 if (oldUrl
.fileName() != name
) {
681 KUrl newUrl
= oldUrl
;
682 newUrl
.setFileName(name
);
683 KonqOperations::rename(this, oldUrl
, newUrl
);
686 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
687 Q_ASSERT(itemCount
== 1);
689 if (isColumnViewActive()) {
690 m_columnView
->editItem(items
.first());
692 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
693 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
694 itemView()->edit(proxyIndex
);
697 Q_ASSERT(itemCount
== 1);
699 RenameDialog
dialog(this, items
);
700 if (dialog
.exec() == QDialog::Rejected
) {
704 const QString
& newName
= dialog
.newName();
705 if (newName
.isEmpty()) {
706 emit
errorMessage(dialog
.errorString());
710 const KUrl
& oldUrl
= items
.first().url();
711 KUrl newUrl
= oldUrl
;
712 newUrl
.setFileName(newName
);
713 KonqOperations::rename(this, oldUrl
, newUrl
);
716 // assure that the current index remains visible when KDirLister
717 // will notify the view about changed items
718 m_assureVisibleCurrentIndex
= true;
721 void DolphinView::trashSelectedItems()
723 const KUrl::List list
= simplifiedSelectedUrls();
724 KonqOperations::del(this, KonqOperations::TRASH
, list
);
727 void DolphinView::deleteSelectedItems()
729 const KUrl::List list
= simplifiedSelectedUrls();
730 const bool del
= KonqOperations::askDeleteConfirmation(list
,
732 KonqOperations::DEFAULT_CONFIRMATION
,
736 KIO::Job
* job
= KIO::del(list
);
737 connect(job
, SIGNAL(result(KJob
*)),
738 this, SLOT(slotDeleteFileFinished(KJob
*)));
742 void DolphinView::cutSelectedItems()
744 QMimeData
* mimeData
= selectionMimeData();
745 KonqMimeData::addIsCutSelection(mimeData
, true);
746 QApplication::clipboard()->setMimeData(mimeData
);
749 void DolphinView::copySelectedItems()
751 QMimeData
* mimeData
= selectionMimeData();
752 QApplication::clipboard()->setMimeData(mimeData
);
755 void DolphinView::paste()
760 void DolphinView::pasteIntoFolder()
762 const KFileItemList items
= selectedItems();
763 if ((items
.count() == 1) && items
.first().isDir()) {
764 pasteToUrl(items
.first().url());
768 void DolphinView::setShowPreview(bool show
)
770 if (m_showPreview
== show
) {
774 const KUrl viewPropsUrl
= viewPropertiesUrl();
775 ViewProperties
props(viewPropsUrl
);
776 props
.setShowPreview(show
);
778 m_showPreview
= show
;
779 m_previewGenerator
->setPreviewShown(show
);
781 const int oldZoomLevel
= m_controller
->zoomLevel();
782 emit
showPreviewChanged();
784 // Enabling or disabling the preview might change the icon size of the view.
785 // As the view does not emit a signal when the icon size has been changed,
786 // the used zoom level of the controller must be adjusted manually:
787 updateZoomLevel(oldZoomLevel
);
789 loadDirectory(viewPropsUrl
);
792 void DolphinView::setShowHiddenFiles(bool show
)
794 if (m_dirLister
->showingDotFiles() == show
) {
798 const KUrl viewPropsUrl
= viewPropertiesUrl();
799 ViewProperties
props(viewPropsUrl
);
800 props
.setShowHiddenFiles(show
);
802 m_dirLister
->setShowingDotFiles(show
);
803 emit
showHiddenFilesChanged();
805 loadDirectory(viewPropsUrl
);
808 void DolphinView::setCategorizedSorting(bool categorized
)
810 if (categorized
== categorizedSorting()) {
814 // setCategorizedSorting(true) may only get invoked
815 // if the view supports categorized sorting
816 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
818 ViewProperties
props(viewPropertiesUrl());
819 props
.setCategorizedSorting(categorized
);
822 m_storedCategorizedSorting
= categorized
;
823 m_proxyModel
->setCategorizedModel(categorized
);
825 emit
categorizedSortingChanged();
828 void DolphinView::toggleSortOrder()
830 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
831 Qt::DescendingOrder
:
836 void DolphinView::toggleSortFoldersFirst()
838 setSortFoldersFirst(!sortFoldersFirst());
841 void DolphinView::toggleAdditionalInfo(QAction
* action
)
843 const KFileItemDelegate::Information info
=
844 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
846 KFileItemDelegate::InformationList list
= additionalInfo();
848 const bool show
= action
->isChecked();
850 const int index
= list
.indexOf(info
);
851 const bool containsInfo
= (index
>= 0);
852 if (show
&& !containsInfo
) {
854 setAdditionalInfo(list
);
855 } else if (!show
&& containsInfo
) {
856 list
.removeAt(index
);
857 setAdditionalInfo(list
);
858 Q_ASSERT(list
.indexOf(info
) < 0);
862 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
864 QWidget::mouseReleaseEvent(event
);
868 void DolphinView::wheelEvent(QWheelEvent
* event
)
870 if (event
->modifiers() & Qt::ControlModifier
) {
871 const int delta
= event
->delta();
872 const int level
= zoomLevel();
874 setZoomLevel(level
+ 1);
875 } else if (delta
< 0) {
876 setZoomLevel(level
- 1);
882 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
884 switch (event
->type()) {
885 case QEvent::FocusIn
:
886 if (watched
== itemView()) {
887 m_controller
->requestActivation();
891 case QEvent::MouseButtonPress
:
892 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
893 // Listening to a mousebutton press event to delete expanded views is a
894 // workaround, as it seems impossible for the FolderExpander to know when
895 // a dragging outside a view has been finished. However it works quite well:
896 // A mousebutton press event indicates that a drag operation must be
898 m_expandedDragSource
->deleteLater();
899 m_expandedDragSource
= 0;
903 case QEvent::DragEnter
:
904 if (watched
== itemView()->viewport()) {
909 case QEvent::KeyPress
:
910 if (watched
== itemView()) {
911 if (m_toolTipManager
!= 0) {
912 m_toolTipManager
->hideTip();
915 // clear the selection when Escape has been pressed
916 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
917 if (keyEvent
->key() == Qt::Key_Escape
) {
927 return QWidget::eventFilter(watched
, event
);
930 void DolphinView::activate()
935 void DolphinView::triggerItem(const KFileItem
& item
)
937 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
938 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
939 // items are selected by the user, hence don't trigger the
940 // item specified by 'index'
944 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
945 if (item
.isNull() || m_isContextMenuOpen
) {
949 if (m_toolTipManager
!= 0) {
950 m_toolTipManager
->hideTip();
952 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
955 void DolphinView::emitSelectionChangedSignal()
957 emit
selectionChanged(DolphinView::selectedItems());
960 void DolphinView::openContextMenu(const QPoint
& pos
,
961 const QList
<QAction
*>& customActions
)
964 if (isColumnViewActive()) {
965 item
= m_columnView
->itemAt(pos
);
967 const QModelIndex index
= itemView()->indexAt(pos
);
968 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
969 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
970 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
974 if (m_toolTipManager
!= 0) {
975 m_toolTipManager
->hideTip();
978 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
979 emit
requestContextMenu(item
, url(), customActions
);
980 m_isContextMenuOpen
= false;
983 void DolphinView::dropUrls(const KFileItem
& destItem
,
984 const KUrl
& destPath
,
987 addNewFileNames(event
->mimeData());
988 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
991 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
993 ViewProperties
props(viewPropertiesUrl());
994 props
.setSorting(sorting
);
996 m_proxyModel
->setSorting(sorting
);
998 emit
sortingChanged(sorting
);
1001 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1003 ViewProperties
props(viewPropertiesUrl());
1004 props
.setSortOrder(order
);
1006 m_proxyModel
->setSortOrder(order
);
1008 emit
sortOrderChanged(order
);
1011 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1013 ViewProperties
props(viewPropertiesUrl());
1014 props
.setSortFoldersFirst(foldersFirst
);
1016 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1018 emit
sortFoldersFirstChanged(foldersFirst
);
1021 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1023 ViewProperties
props(viewPropertiesUrl());
1024 props
.setAdditionalInfo(info
);
1027 m_fileItemDelegate
->setShowInformation(info
);
1029 emit
additionalInfoChanged();
1032 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1034 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1035 (m_mode
== DolphinView::IconsView
);
1037 QAction
* showSizeInfo
= collection
->action("show_size_info");
1038 QAction
* showDateInfo
= collection
->action("show_date_info");
1039 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1040 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1041 QAction
* showGroupInfo
= collection
->action("show_group_info");
1042 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1044 showSizeInfo
->setChecked(false);
1045 showDateInfo
->setChecked(false);
1046 showPermissionsInfo
->setChecked(false);
1047 showOwnerInfo
->setChecked(false);
1048 showGroupInfo
->setChecked(false);
1049 showMimeInfo
->setChecked(false);
1051 showSizeInfo
->setEnabled(enable
);
1052 showDateInfo
->setEnabled(enable
);
1053 showPermissionsInfo
->setEnabled(enable
);
1054 showOwnerInfo
->setEnabled(enable
);
1055 showGroupInfo
->setEnabled(enable
);
1056 showMimeInfo
->setEnabled(enable
);
1058 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1060 case KFileItemDelegate::Size
:
1061 showSizeInfo
->setChecked(true);
1063 case KFileItemDelegate::ModificationTime
:
1064 showDateInfo
->setChecked(true);
1066 case KFileItemDelegate::Permissions
:
1067 showPermissionsInfo
->setChecked(true);
1069 case KFileItemDelegate::Owner
:
1070 showOwnerInfo
->setChecked(true);
1072 case KFileItemDelegate::OwnerAndGroup
:
1073 showGroupInfo
->setChecked(true);
1075 case KFileItemDelegate::FriendlyMimeType
:
1076 showMimeInfo
->setChecked(true);
1084 QPair
<bool, QString
> DolphinView::pasteInfo() const
1086 return KonqOperations::pasteInfo(url());
1089 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1091 m_tabsForFiles
= tabsForFiles
;
1094 bool DolphinView::isTabsForFilesEnabled() const
1096 return m_tabsForFiles
;
1099 bool DolphinView::itemsExpandable() const
1101 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1104 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1109 if (DragAndDropHelper::instance().isDragSource(view
)) {
1110 // We must store for later deletion.
1111 if (m_expandedDragSource
!= 0) {
1112 // The old stored view is obviously not the drag source anymore.
1113 m_expandedDragSource
->deleteLater();
1114 m_expandedDragSource
= 0;
1117 m_expandedDragSource
= view
;
1120 view
->deleteLater();
1124 void DolphinView::observeCreatedItem(const KUrl
& url
)
1126 m_createdItemUrl
= url
;
1127 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1128 this, SLOT(selectAndScrollToCreatedItem()));
1131 void DolphinView::selectAndScrollToCreatedItem()
1133 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1134 if (dirIndex
.isValid()) {
1135 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1136 itemView()->setCurrentIndex(proxyIndex
);
1139 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1140 this, SLOT(selectAndScrollToCreatedItem()));
1141 m_createdItemUrl
= KUrl();
1144 void DolphinView::restoreSelection()
1146 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1147 changeSelection(m_selectedItems
);
1150 void DolphinView::emitContentsMoved()
1152 // only emit the contents moved signal if:
1153 // - no directory loading is ongoing (this would reset the contents position
1154 // always to (0, 0))
1155 // - if the Column View is active: the column view does an automatic
1156 // positioning during the loading operation, which must be remembered
1157 if (!m_loadingDirectory
|| isColumnViewActive()) {
1158 const QPoint
pos(contentsPosition());
1159 emit
contentsMoved(pos
.x(), pos
.y());
1163 void DolphinView::showHoverInformation(const KFileItem
& item
)
1165 emit
requestItemInfo(item
);
1168 void DolphinView::clearHoverInformation()
1170 emit
requestItemInfo(KFileItem());
1173 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1175 if (job
->error() == 0) {
1176 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1177 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1178 emit
errorMessage(job
->errorString());
1182 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1184 emit
requestUrlChange(url
);
1185 m_controller
->setUrl(url
);
1188 void DolphinView::slotDirListerCompleted()
1190 if (!m_currentItemUrl
.isEmpty()) {
1191 // assure that the current item remains visible
1192 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1193 if (dirIndex
.isValid()) {
1194 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1195 QAbstractItemView
* view
= itemView();
1196 const bool clearSelection
= !hasSelection();
1197 view
->setCurrentIndex(proxyIndex
);
1198 if (clearSelection
) {
1199 view
->clearSelection();
1202 m_currentItemUrl
.clear();
1205 if (!m_newFileNames
.isEmpty()) {
1206 // select all newly added items created by a paste operation or
1207 // a drag & drop operation
1208 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1209 const int rowCount
= m_proxyModel
->rowCount();
1210 for (int row
= 0; row
< rowCount
; ++row
) {
1211 const QModelIndex proxyIndex
= m_proxyModel
->index(row
, 0);
1212 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(proxyIndex
);
1213 const KUrl url
= m_dolphinModel
->itemForIndex(dirIndex
).url();
1214 if (m_newFileNames
.contains(url
.fileName())) {
1215 selectionModel
->select(proxyIndex
, QItemSelectionModel::Select
);
1219 m_newFileNames
.clear();
1223 void DolphinView::slotRefreshItems()
1225 if (m_assureVisibleCurrentIndex
) {
1226 m_assureVisibleCurrentIndex
= false;
1227 itemView()->scrollTo(itemView()->currentIndex());
1231 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1233 if (!url
.isValid()) {
1234 const QString
location(url
.pathOrUrl());
1235 if (location
.isEmpty()) {
1236 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1238 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1243 m_loadingDirectory
= true;
1246 m_selectedItems
= selectedItems();
1247 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1250 m_dirLister
->stop();
1251 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1253 if (isColumnViewActive()) {
1254 // adjusting the directory lister is not enough in the case of the
1255 // column view, as each column has its own directory lister internally...
1257 m_columnView
->reload();
1259 m_columnView
->showColumn(url
);
1264 KUrl
DolphinView::viewPropertiesUrl() const
1266 if (isColumnViewActive()) {
1267 return m_columnView
->rootUrl();
1273 void DolphinView::applyViewProperties(const KUrl
& url
)
1275 if (m_ignoreViewProperties
) {
1279 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1280 // The column view is active, hence don't apply the view properties
1281 // of sub directories (represented by columns) to the view. The
1282 // view always represents the properties of the first column.
1286 const ViewProperties
props(url
);
1288 const Mode mode
= props
.viewMode();
1289 if (m_mode
!= mode
) {
1290 const int oldZoomLevel
= m_controller
->zoomLevel();
1296 updateZoomLevel(oldZoomLevel
);
1298 if (itemView() == 0) {
1301 Q_ASSERT(itemView() != 0);
1302 Q_ASSERT(m_fileItemDelegate
!= 0);
1304 const bool showHiddenFiles
= props
.showHiddenFiles();
1305 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1306 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1307 emit
showHiddenFilesChanged();
1310 m_storedCategorizedSorting
= props
.categorizedSorting();
1311 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1312 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1313 m_proxyModel
->setCategorizedModel(categorized
);
1314 emit
categorizedSortingChanged();
1317 const DolphinView::Sorting sorting
= props
.sorting();
1318 if (sorting
!= m_proxyModel
->sorting()) {
1319 m_proxyModel
->setSorting(sorting
);
1320 emit
sortingChanged(sorting
);
1323 const Qt::SortOrder sortOrder
= props
.sortOrder();
1324 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1325 m_proxyModel
->setSortOrder(sortOrder
);
1326 emit
sortOrderChanged(sortOrder
);
1329 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1330 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1331 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1332 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1335 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1336 if (info
!= m_fileItemDelegate
->showInformation()) {
1337 m_fileItemDelegate
->setShowInformation(info
);
1338 emit
additionalInfoChanged();
1341 const bool showPreview
= props
.showPreview();
1342 if (showPreview
!= m_showPreview
) {
1343 m_showPreview
= showPreview
;
1344 m_previewGenerator
->setPreviewShown(showPreview
);
1346 const int oldZoomLevel
= m_controller
->zoomLevel();
1347 emit
showPreviewChanged();
1349 // Enabling or disabling the preview might change the icon size of the view.
1350 // As the view does not emit a signal when the icon size has been changed,
1351 // the used zoom level of the controller must be adjusted manually:
1352 updateZoomLevel(oldZoomLevel
);
1355 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1356 // During the lifetime of a DolphinView instance the global view properties
1357 // should not be changed. This allows e. g. to split a view and use different
1358 // view properties for each view.
1359 m_ignoreViewProperties
= true;
1363 void DolphinView::createView()
1366 Q_ASSERT(m_iconsView
== 0);
1367 Q_ASSERT(m_detailsView
== 0);
1368 Q_ASSERT(m_columnView
== 0);
1370 QAbstractItemView
* view
= 0;
1373 m_iconsView
= new DolphinIconsView(this, m_controller
);
1379 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1380 view
= m_detailsView
;
1384 m_columnView
= new DolphinColumnView(this, m_controller
);
1385 view
= m_columnView
;
1389 Q_ASSERT(view
!= 0);
1390 view
->installEventFilter(this);
1391 view
->viewport()->installEventFilter(this);
1392 setFocusProxy(view
);
1394 if (m_mode
!= ColumnView
) {
1395 // Give the view the ability to auto-expand its directories on hovering
1396 // (the column view takes care about this itself). If the details view
1397 // uses expandable folders, the auto-expanding should be used always.
1398 DolphinSettings
& settings
= DolphinSettings::instance();
1399 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1400 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1402 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1403 folderExpander
->setEnabled(enabled
);
1404 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1405 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1408 // Listen out for requests to delete the current column.
1409 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1410 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1413 m_controller
->setItemView(view
);
1415 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1416 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1417 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1418 view
->setItemDelegate(m_fileItemDelegate
);
1420 view
->setModel(m_proxyModel
);
1421 if (m_selectionModel
!= 0) {
1422 view
->setSelectionModel(m_selectionModel
);
1424 m_selectionModel
= view
->selectionModel();
1427 // reparent the selection model, as it should not be deleted
1428 // when deleting the model
1429 m_selectionModel
->setParent(this);
1431 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1433 m_previewGenerator
= new KFilePreviewGenerator(view
);
1434 m_previewGenerator
->setPreviewShown(m_showPreview
);
1436 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1437 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1438 connect(m_controller
, SIGNAL(hideToolTip()),
1439 m_toolTipManager
, SLOT(hideTip()));
1442 m_topLayout
->insertWidget(1, view
);
1444 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1445 this, SLOT(emitSelectionChangedSignal()));
1446 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1447 this, SLOT(emitContentsMoved()));
1448 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1449 this, SLOT(emitContentsMoved()));
1452 void DolphinView::deleteView()
1454 QAbstractItemView
* view
= itemView();
1456 // It's important to set the keyboard focus to the parent
1457 // before deleting the view: Otherwise when having a split
1458 // view the other view will get the focus and will request
1459 // an activation (see DolphinView::eventFilter()).
1463 m_topLayout
->removeWidget(view
);
1466 // m_previewGenerator's parent is not always destroyed, and we
1467 // don't want two active at once - manually delete.
1468 delete m_previewGenerator
;
1469 m_previewGenerator
= 0;
1472 m_controller
->disconnect(view
);
1475 deleteWhenNotDragSource(view
);
1481 m_fileItemDelegate
= 0;
1482 m_toolTipManager
= 0;
1486 QAbstractItemView
* DolphinView::itemView() const
1488 if (m_detailsView
!= 0) {
1489 return m_detailsView
;
1490 } else if (m_columnView
!= 0) {
1491 return m_columnView
;
1497 void DolphinView::pasteToUrl(const KUrl
& url
)
1499 addNewFileNames(QApplication::clipboard()->mimeData());
1500 KonqOperations::doPaste(this, url
);
1503 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1505 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1506 if (oldZoomLevel
!= newZoomLevel
) {
1507 m_controller
->setZoomLevel(newZoomLevel
);
1508 emit
zoomLevelChanged(newZoomLevel
);
1512 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1514 KUrl::List list
= selectedUrls();
1515 if (itemsExpandable() ) {
1516 list
= KDirModel::simplifiedUrlList(list
);
1521 QMimeData
* DolphinView::selectionMimeData() const
1523 if (isColumnViewActive()) {
1524 return m_columnView
->selectionMimeData();
1527 const QAbstractItemView
* view
= itemView();
1528 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1529 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1530 return m_dolphinModel
->mimeData(selection
.indexes());
1533 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1535 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1536 foreach (const KUrl
& url
, urls
) {
1537 m_newFileNames
.insert(url
.fileName());
1541 #include "dolphinview.moc"