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"
71 * Helper function for sorting items with qSort() in
72 * DolphinView::renameSelectedItems().
74 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
76 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
79 DolphinView::DolphinView(QWidget
* parent
,
81 KDirLister
* dirLister
,
82 DolphinModel
* dolphinModel
,
83 DolphinSortFilterProxyModel
* proxyModel
) :
87 m_loadingDirectory(false),
88 m_storedCategorizedSorting(false),
89 m_tabsForFiles(false),
90 m_isContextMenuOpen(false),
91 m_ignoreViewProperties(false),
92 m_assureVisibleCurrentIndex(false),
93 m_mode(DolphinView::IconsView
),
99 m_fileItemDelegate(0),
101 m_dolphinModel(dolphinModel
),
102 m_dirLister(dirLister
),
103 m_proxyModel(proxyModel
),
104 m_previewGenerator(0),
111 m_expandedDragSource(0)
113 m_topLayout
= new QVBoxLayout(this);
114 m_topLayout
->setSpacing(0);
115 m_topLayout
->setMargin(0);
117 m_controller
= new DolphinController(this);
118 m_controller
->setUrl(url
);
120 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
121 this, SIGNAL(urlChanged(const KUrl
&)));
122 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
123 this, SLOT(slotRequestUrlChange(const KUrl
&)));
125 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
126 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
127 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
128 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
129 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
130 this, SLOT(updateSorting(DolphinView::Sorting
)));
131 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
132 this, SLOT(updateSortOrder(Qt::SortOrder
)));
133 connect(m_controller
, SIGNAL(sortFoldersFirstChanged(bool)),
134 this, SLOT(updateSortFoldersFirst(bool)));
135 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
136 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
137 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
138 this, SLOT(triggerItem(const KFileItem
&)));
139 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
140 this, SIGNAL(tabRequested(const KUrl
&)));
141 connect(m_controller
, SIGNAL(activated()),
142 this, SLOT(activate()));
143 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
144 this, SLOT(showHoverInformation(const KFileItem
&)));
145 connect(m_controller
, SIGNAL(viewportEntered()),
146 this, SLOT(clearHoverInformation()));
148 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
149 this, SIGNAL(redirection(KUrl
, KUrl
)));
150 connect(m_dirLister
, SIGNAL(completed()),
151 this, SLOT(slotDirListerCompleted()));
152 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
153 this, SLOT(slotRefreshItems()));
155 // When a new item has been created by the "Create New..." menu, the item should
156 // get selected and it must be assured that the item will get visible. As the
157 // creation is done asynchronously, several signals must be checked:
158 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
159 this, SLOT(observeCreatedItem(const KUrl
&)));
161 applyViewProperties(url
);
162 m_topLayout
->addWidget(itemView());
165 DolphinView::~DolphinView()
167 delete m_expandedDragSource
;
168 m_expandedDragSource
= 0;
171 const KUrl
& DolphinView::url() const
173 return m_controller
->url();
176 KUrl
DolphinView::rootUrl() const
178 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
181 void DolphinView::setActive(bool active
)
183 if (active
== m_active
) {
189 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
191 emit
selectionChanged(selectedItems());
196 QWidget
* viewport
= itemView()->viewport();
198 palette
.setColor(viewport
->backgroundRole(), color
);
199 viewport
->setPalette(palette
);
204 itemView()->setFocus();
208 m_controller
->indicateActivationChange(active
);
211 bool DolphinView::isActive() const
216 void DolphinView::setMode(Mode mode
)
218 if (mode
== m_mode
) {
219 return; // the wished mode is already set
222 const int oldZoomLevel
= m_controller
->zoomLevel();
227 const KUrl viewPropsUrl
= viewPropertiesUrl();
228 ViewProperties
props(viewPropsUrl
);
229 props
.setViewMode(m_mode
);
232 // the file item delegate has been recreated, apply the current
233 // additional information manually
234 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
235 m_fileItemDelegate
->setShowInformation(infoList
);
236 emit
additionalInfoChanged();
238 // Not all view modes support categorized sorting. Adjust the sorting model
239 // if changing the view mode results in a change of the categorized sorting
241 m_storedCategorizedSorting
= props
.categorizedSorting();
242 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
243 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
244 m_proxyModel
->setCategorizedModel(categorized
);
245 emit
categorizedSortingChanged();
250 updateZoomLevel(oldZoomLevel
);
252 loadDirectory(viewPropsUrl
);
256 DolphinView::Mode
DolphinView::mode() const
261 bool DolphinView::showPreview() const
263 return m_showPreview
;
266 bool DolphinView::showHiddenFiles() const
268 return m_dirLister
->showingDotFiles();
271 bool DolphinView::categorizedSorting() const
273 // If all view modes would support categorized sorting, returning
274 // m_proxyModel->isCategorizedModel() would be the way to go. As
275 // currently only the icons view supports caterized sorting, we remember
276 // the stored view properties state in m_storedCategorizedSorting and
277 // return this state. The application takes care to disable the corresponding
278 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
279 // that this setting is not applied to the current view mode.
280 return m_storedCategorizedSorting
;
283 bool DolphinView::supportsCategorizedSorting() const
285 return m_iconsView
!= 0;
288 void DolphinView::selectAll()
290 QAbstractItemView
* view
= itemView();
291 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
292 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
293 // selection instead of selecting all items. This is bypassed for KDE 4.0
294 // by invoking clearSelection() first.
295 view
->clearSelection();
299 void DolphinView::invertSelection()
301 if (isColumnViewActive()) {
302 // QAbstractItemView does not offer a virtual method invertSelection()
303 // as counterpart to QAbstractItemView::selectAll(). This makes it
304 // necessary to delegate the inverting of the selection to the
305 // column view, as only the selection of the active column should
307 m_columnView
->invertSelection();
309 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
310 const QAbstractItemModel
* itemModel
= selectionModel
->model();
312 const QModelIndex topLeft
= itemModel
->index(0, 0);
313 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
314 itemModel
->columnCount() - 1);
316 const QItemSelection
selection(topLeft
, bottomRight
);
317 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
321 bool DolphinView::hasSelection() const
323 return itemView()->selectionModel()->hasSelection();
326 void DolphinView::clearSelection()
328 QItemSelectionModel
* selModel
= itemView()->selectionModel();
329 const QModelIndex currentIndex
= selModel
->currentIndex();
330 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
331 QItemSelectionModel::Clear
);
334 KFileItemList
DolphinView::selectedItems() const
336 if (isColumnViewActive()) {
337 return m_columnView
->selectedItems();
340 const QAbstractItemView
* view
= itemView();
342 // Our view has a selection, we will map them back to the DolphinModel
343 // and then fill the KFileItemList.
344 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
346 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
347 KFileItemList itemList
;
349 const QModelIndexList indexList
= selection
.indexes();
350 foreach (const QModelIndex
&index
, indexList
) {
351 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
352 if (!item
.isNull()) {
353 itemList
.append(item
);
360 KUrl::List
DolphinView::selectedUrls() const
363 const KFileItemList list
= selectedItems();
364 foreach (const KFileItem
&item
, list
) {
365 urls
.append(item
.url());
370 int DolphinView::selectedItemsCount() const
372 if (isColumnViewActive()) {
373 // TODO: get rid of this special case by adjusting the dir lister
374 // to the current column
375 return m_columnView
->selectedItems().count();
378 return itemView()->selectionModel()->selection().count();
381 void DolphinView::setContentsPosition(int x
, int y
)
383 QAbstractItemView
* view
= itemView();
385 // the ColumnView takes care itself for the horizontal scrolling
386 if (!isColumnViewActive()) {
387 view
->horizontalScrollBar()->setValue(x
);
389 view
->verticalScrollBar()->setValue(y
);
391 m_loadingDirectory
= false;
394 QPoint
DolphinView::contentsPosition() const
396 const int x
= itemView()->horizontalScrollBar()->value();
397 const int y
= itemView()->verticalScrollBar()->value();
401 void DolphinView::setZoomLevel(int level
)
403 if (level
< ZoomLevelInfo::minimumLevel()) {
404 level
= ZoomLevelInfo::minimumLevel();
405 } else if (level
> ZoomLevelInfo::maximumLevel()) {
406 level
= ZoomLevelInfo::maximumLevel();
409 if (level
!= zoomLevel()) {
410 m_controller
->setZoomLevel(level
);
411 m_previewGenerator
->updateIcons();
412 emit
zoomLevelChanged(level
);
416 int DolphinView::zoomLevel() const
418 return m_controller
->zoomLevel();
421 void DolphinView::setSorting(Sorting sorting
)
423 if (sorting
!= this->sorting()) {
424 updateSorting(sorting
);
428 DolphinView::Sorting
DolphinView::sorting() const
430 return m_proxyModel
->sorting();
433 void DolphinView::setSortOrder(Qt::SortOrder order
)
435 if (sortOrder() != order
) {
436 updateSortOrder(order
);
440 Qt::SortOrder
DolphinView::sortOrder() const
442 return m_proxyModel
->sortOrder();
445 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
447 if (sortFoldersFirst() != foldersFirst
) {
448 updateSortFoldersFirst(foldersFirst
);
452 bool DolphinView::sortFoldersFirst() const
454 return m_proxyModel
->sortFoldersFirst();
457 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
459 const KUrl viewPropsUrl
= viewPropertiesUrl();
460 ViewProperties
props(viewPropsUrl
);
461 props
.setAdditionalInfo(info
);
462 m_fileItemDelegate
->setShowInformation(info
);
464 emit
additionalInfoChanged();
466 if (itemView() != m_detailsView
) {
467 // the details view requires no reloading of the directory, as it maps
468 // the file item delegate info to its columns internally
469 loadDirectory(viewPropsUrl
);
473 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
475 return m_fileItemDelegate
->showInformation();
478 void DolphinView::reload()
481 loadDirectory(url(), true);
484 void DolphinView::refresh()
486 m_ignoreViewProperties
= false;
488 const bool oldActivationState
= m_active
;
489 const int oldZoomLevel
= m_controller
->zoomLevel();
493 applyViewProperties(m_controller
->url());
496 setActive(oldActivationState
);
497 updateZoomLevel(oldZoomLevel
);
500 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
502 if (m_controller
->url() == url
) {
506 m_previewGenerator
->cancelPreviews();
507 m_controller
->setUrl(url
); // emits urlChanged, which we forward
509 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
510 applyViewProperties(rootUrl
);
511 loadDirectory(rootUrl
);
512 if (itemView() == m_columnView
) {
513 m_columnView
->setRootUrl(rootUrl
);
514 m_columnView
->showColumn(url
);
517 applyViewProperties(url
);
521 emit
startedPathLoading(url
);
524 void DolphinView::setNameFilter(const QString
& nameFilter
)
526 m_proxyModel
->setFilterRegExp(nameFilter
);
528 if (isColumnViewActive()) {
529 // adjusting the directory lister is not enough in the case of the
530 // column view, as each column has its own directory lister internally...
531 m_columnView
->setNameFilter(nameFilter
);
535 void DolphinView::calculateItemCount(int& fileCount
,
537 KIO::filesize_t
& totalFileSize
) const
539 foreach (const KFileItem
& item
, m_dirLister
->items()) {
544 totalFileSize
+= item
.size();
549 QString
DolphinView::statusBarText() const
554 KIO::filesize_t totalFileSize
= 0;
556 if (hasSelection()) {
557 // give a summary of the status of the selected files
558 const KFileItemList list
= selectedItems();
559 if (list
.isEmpty()) {
560 // when an item is triggered, it is temporary selected but selectedItems()
561 // will return an empty list
565 KFileItemList::const_iterator it
= list
.begin();
566 const KFileItemList::const_iterator end
= list
.end();
568 const KFileItem
& item
= *it
;
573 totalFileSize
+= item
.size();
578 if (folderCount
+ fileCount
== 1) {
579 // if only one item is selected, show the filename
580 const QString name
= list
.first().name();
581 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
582 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
583 name
, KIO::convertSize(totalFileSize
));
585 // at least 2 items are selected
586 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
587 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
588 if ((folderCount
> 0) && (fileCount
> 0)) {
589 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
590 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
591 } else if (fileCount
> 0) {
592 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
594 Q_ASSERT(folderCount
> 0);
599 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
600 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
601 fileCount
, folderCount
,
602 totalFileSize
, true);
608 void DolphinView::setUrl(const KUrl
& url
)
610 m_newFileNames
.clear();
611 updateView(url
, KUrl());
614 void DolphinView::changeSelection(const KFileItemList
& selection
)
617 if (selection
.isEmpty()) {
620 const KUrl
& baseUrl
= url();
622 QItemSelection newSelection
;
623 foreach(const KFileItem
& item
, selection
) {
624 url
= item
.url().upUrl();
625 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
626 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
627 newSelection
.select(index
, index
);
630 itemView()->selectionModel()->select(newSelection
,
631 QItemSelectionModel::ClearAndSelect
632 | QItemSelectionModel::Current
);
635 void DolphinView::renameSelectedItems()
637 KFileItemList items
= selectedItems();
638 const int itemCount
= items
.count();
644 // More than one item has been selected for renaming. Open
645 // a rename dialog and rename all items afterwards.
646 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
647 if (dialog
->exec() == QDialog::Rejected
) {
652 const QString newName
= dialog
->newName();
653 if (newName
.isEmpty()) {
654 emit
errorMessage(dialog
->errorString());
660 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
661 // as one operation instead of n rename operations like it is done now...
662 Q_ASSERT(newName
.contains('#'));
664 // currently the items are sorted by the selection order, resort
665 // them by the file name
666 qSort(items
.begin(), items
.end(), lessThan
);
668 // iterate through all selected items and rename them...
670 foreach (const KFileItem
& item
, items
) {
671 const KUrl
& oldUrl
= item
.url();
673 number
.setNum(index
++);
675 QString name
= newName
;
676 name
.replace('#', number
);
678 if (oldUrl
.fileName() != name
) {
679 KUrl newUrl
= oldUrl
;
680 newUrl
.setFileName(name
);
681 KonqOperations::rename(this, oldUrl
, newUrl
);
684 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
685 Q_ASSERT(itemCount
== 1);
687 if (isColumnViewActive()) {
688 m_columnView
->editItem(items
.first());
690 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
691 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
692 itemView()->edit(proxyIndex
);
695 Q_ASSERT(itemCount
== 1);
697 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
698 if (dialog
->exec() == QDialog::Rejected
) {
703 const QString newName
= dialog
->newName();
704 if (newName
.isEmpty()) {
705 emit
errorMessage(dialog
->errorString());
711 const KUrl
& oldUrl
= items
.first().url();
712 KUrl newUrl
= oldUrl
;
713 newUrl
.setFileName(newName
);
714 KonqOperations::rename(this, oldUrl
, newUrl
);
717 // assure that the current index remains visible when KDirLister
718 // will notify the view about changed items
719 m_assureVisibleCurrentIndex
= true;
722 void DolphinView::trashSelectedItems()
724 const KUrl::List list
= simplifiedSelectedUrls();
725 KonqOperations::del(this, KonqOperations::TRASH
, list
);
728 void DolphinView::deleteSelectedItems()
730 const KUrl::List list
= simplifiedSelectedUrls();
731 const bool del
= KonqOperations::askDeleteConfirmation(list
,
733 KonqOperations::DEFAULT_CONFIRMATION
,
737 KIO::Job
* job
= KIO::del(list
);
738 connect(job
, SIGNAL(result(KJob
*)),
739 this, SLOT(slotDeleteFileFinished(KJob
*)));
743 void DolphinView::cutSelectedItems()
745 QMimeData
* mimeData
= selectionMimeData();
746 KonqMimeData::addIsCutSelection(mimeData
, true);
747 QApplication::clipboard()->setMimeData(mimeData
);
750 void DolphinView::copySelectedItems()
752 QMimeData
* mimeData
= selectionMimeData();
753 QApplication::clipboard()->setMimeData(mimeData
);
756 void DolphinView::paste()
761 void DolphinView::pasteIntoFolder()
763 const KFileItemList items
= selectedItems();
764 if ((items
.count() == 1) && items
.first().isDir()) {
765 pasteToUrl(items
.first().url());
769 void DolphinView::setShowPreview(bool show
)
771 if (m_showPreview
== show
) {
775 const KUrl viewPropsUrl
= viewPropertiesUrl();
776 ViewProperties
props(viewPropsUrl
);
777 props
.setShowPreview(show
);
779 m_showPreview
= show
;
780 m_previewGenerator
->setPreviewShown(show
);
782 const int oldZoomLevel
= m_controller
->zoomLevel();
783 emit
showPreviewChanged();
785 // Enabling or disabling the preview might change the icon size of the view.
786 // As the view does not emit a signal when the icon size has been changed,
787 // the used zoom level of the controller must be adjusted manually:
788 updateZoomLevel(oldZoomLevel
);
790 loadDirectory(viewPropsUrl
);
793 void DolphinView::setShowHiddenFiles(bool show
)
795 if (m_dirLister
->showingDotFiles() == show
) {
799 const KUrl viewPropsUrl
= viewPropertiesUrl();
800 ViewProperties
props(viewPropsUrl
);
801 props
.setShowHiddenFiles(show
);
803 m_dirLister
->setShowingDotFiles(show
);
804 emit
showHiddenFilesChanged();
806 loadDirectory(viewPropsUrl
);
809 void DolphinView::setCategorizedSorting(bool categorized
)
811 if (categorized
== categorizedSorting()) {
815 // setCategorizedSorting(true) may only get invoked
816 // if the view supports categorized sorting
817 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
819 ViewProperties
props(viewPropertiesUrl());
820 props
.setCategorizedSorting(categorized
);
823 m_storedCategorizedSorting
= categorized
;
824 m_proxyModel
->setCategorizedModel(categorized
);
826 emit
categorizedSortingChanged();
829 void DolphinView::toggleSortOrder()
831 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
832 Qt::DescendingOrder
:
837 void DolphinView::toggleSortFoldersFirst()
839 setSortFoldersFirst(!sortFoldersFirst());
842 void DolphinView::toggleAdditionalInfo(QAction
* action
)
844 const KFileItemDelegate::Information info
=
845 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
847 KFileItemDelegate::InformationList list
= additionalInfo();
849 const bool show
= action
->isChecked();
851 const int index
= list
.indexOf(info
);
852 const bool containsInfo
= (index
>= 0);
853 if (show
&& !containsInfo
) {
855 setAdditionalInfo(list
);
856 } else if (!show
&& containsInfo
) {
857 list
.removeAt(index
);
858 setAdditionalInfo(list
);
859 Q_ASSERT(list
.indexOf(info
) < 0);
863 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
865 QWidget::mouseReleaseEvent(event
);
869 void DolphinView::wheelEvent(QWheelEvent
* event
)
871 if (event
->modifiers() & Qt::ControlModifier
) {
872 const int delta
= event
->delta();
873 const int level
= zoomLevel();
875 setZoomLevel(level
+ 1);
876 } else if (delta
< 0) {
877 setZoomLevel(level
- 1);
883 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
885 switch (event
->type()) {
886 case QEvent::FocusIn
:
887 if (watched
== itemView()) {
888 m_controller
->requestActivation();
892 case QEvent::MouseButtonPress
:
893 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
894 // Listening to a mousebutton press event to delete expanded views is a
895 // workaround, as it seems impossible for the FolderExpander to know when
896 // a dragging outside a view has been finished. However it works quite well:
897 // A mousebutton press event indicates that a drag operation must be
899 m_expandedDragSource
->deleteLater();
900 m_expandedDragSource
= 0;
904 case QEvent::DragEnter
:
905 if (watched
== itemView()->viewport()) {
910 case QEvent::KeyPress
:
911 if (watched
== itemView()) {
912 if (m_toolTipManager
!= 0) {
913 m_toolTipManager
->hideTip();
916 // clear the selection when Escape has been pressed
917 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
918 if (keyEvent
->key() == Qt::Key_Escape
) {
928 return QWidget::eventFilter(watched
, event
);
931 void DolphinView::activate()
936 void DolphinView::triggerItem(const KFileItem
& item
)
938 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
939 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
940 // items are selected by the user, hence don't trigger the
941 // item specified by 'index'
945 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
946 if (item
.isNull() || m_isContextMenuOpen
) {
950 if (m_toolTipManager
!= 0) {
951 m_toolTipManager
->hideTip();
953 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
956 void DolphinView::emitSelectionChangedSignal()
958 emit
selectionChanged(DolphinView::selectedItems());
961 void DolphinView::openContextMenu(const QPoint
& pos
,
962 const QList
<QAction
*>& customActions
)
965 if (isColumnViewActive()) {
966 item
= m_columnView
->itemAt(pos
);
968 const QModelIndex index
= itemView()->indexAt(pos
);
969 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
970 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
971 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
975 if (m_toolTipManager
!= 0) {
976 m_toolTipManager
->hideTip();
979 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
980 emit
requestContextMenu(item
, url(), customActions
);
981 m_isContextMenuOpen
= false;
984 void DolphinView::dropUrls(const KFileItem
& destItem
,
985 const KUrl
& destPath
,
988 addNewFileNames(event
->mimeData());
989 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
992 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
994 ViewProperties
props(viewPropertiesUrl());
995 props
.setSorting(sorting
);
997 m_proxyModel
->setSorting(sorting
);
999 emit
sortingChanged(sorting
);
1002 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1004 ViewProperties
props(viewPropertiesUrl());
1005 props
.setSortOrder(order
);
1007 m_proxyModel
->setSortOrder(order
);
1009 emit
sortOrderChanged(order
);
1012 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1014 ViewProperties
props(viewPropertiesUrl());
1015 props
.setSortFoldersFirst(foldersFirst
);
1017 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1019 emit
sortFoldersFirstChanged(foldersFirst
);
1022 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1024 ViewProperties
props(viewPropertiesUrl());
1025 props
.setAdditionalInfo(info
);
1028 m_fileItemDelegate
->setShowInformation(info
);
1030 emit
additionalInfoChanged();
1033 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1035 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1036 (m_mode
== DolphinView::IconsView
);
1038 QAction
* showSizeInfo
= collection
->action("show_size_info");
1039 QAction
* showDateInfo
= collection
->action("show_date_info");
1040 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1041 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1042 QAction
* showGroupInfo
= collection
->action("show_group_info");
1043 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1045 showSizeInfo
->setChecked(false);
1046 showDateInfo
->setChecked(false);
1047 showPermissionsInfo
->setChecked(false);
1048 showOwnerInfo
->setChecked(false);
1049 showGroupInfo
->setChecked(false);
1050 showMimeInfo
->setChecked(false);
1052 showSizeInfo
->setEnabled(enable
);
1053 showDateInfo
->setEnabled(enable
);
1054 showPermissionsInfo
->setEnabled(enable
);
1055 showOwnerInfo
->setEnabled(enable
);
1056 showGroupInfo
->setEnabled(enable
);
1057 showMimeInfo
->setEnabled(enable
);
1059 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1061 case KFileItemDelegate::Size
:
1062 showSizeInfo
->setChecked(true);
1064 case KFileItemDelegate::ModificationTime
:
1065 showDateInfo
->setChecked(true);
1067 case KFileItemDelegate::Permissions
:
1068 showPermissionsInfo
->setChecked(true);
1070 case KFileItemDelegate::Owner
:
1071 showOwnerInfo
->setChecked(true);
1073 case KFileItemDelegate::OwnerAndGroup
:
1074 showGroupInfo
->setChecked(true);
1076 case KFileItemDelegate::FriendlyMimeType
:
1077 showMimeInfo
->setChecked(true);
1085 QPair
<bool, QString
> DolphinView::pasteInfo() const
1087 return KonqOperations::pasteInfo(url());
1090 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1092 m_tabsForFiles
= tabsForFiles
;
1095 bool DolphinView::isTabsForFilesEnabled() const
1097 return m_tabsForFiles
;
1100 void DolphinView::activateItem(const KUrl
& url
)
1102 m_activeItemUrl
= url
;
1105 bool DolphinView::itemsExpandable() const
1107 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1110 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1115 if (DragAndDropHelper::instance().isDragSource(view
)) {
1116 // We must store for later deletion.
1117 if (m_expandedDragSource
!= 0) {
1118 // The old stored view is obviously not the drag source anymore.
1119 m_expandedDragSource
->deleteLater();
1120 m_expandedDragSource
= 0;
1123 m_expandedDragSource
= view
;
1126 view
->deleteLater();
1130 void DolphinView::observeCreatedItem(const KUrl
& url
)
1132 m_createdItemUrl
= url
;
1133 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1134 this, SLOT(selectAndScrollToCreatedItem()));
1137 void DolphinView::selectAndScrollToCreatedItem()
1139 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1140 if (dirIndex
.isValid()) {
1141 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1142 itemView()->setCurrentIndex(proxyIndex
);
1145 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1146 this, SLOT(selectAndScrollToCreatedItem()));
1147 m_createdItemUrl
= KUrl();
1150 void DolphinView::restoreSelection()
1152 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1153 changeSelection(m_selectedItems
);
1156 void DolphinView::emitContentsMoved()
1158 // only emit the contents moved signal if:
1159 // - no directory loading is ongoing (this would reset the contents position
1160 // always to (0, 0))
1161 // - if the Column View is active: the column view does an automatic
1162 // positioning during the loading operation, which must be remembered
1163 if (!m_loadingDirectory
|| isColumnViewActive()) {
1164 const QPoint
pos(contentsPosition());
1165 emit
contentsMoved(pos
.x(), pos
.y());
1169 void DolphinView::showHoverInformation(const KFileItem
& item
)
1171 emit
requestItemInfo(item
);
1174 void DolphinView::clearHoverInformation()
1176 emit
requestItemInfo(KFileItem());
1179 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1181 if (job
->error() == 0) {
1182 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1183 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1184 emit
errorMessage(job
->errorString());
1188 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1190 emit
requestUrlChange(url
);
1191 m_controller
->setUrl(url
);
1194 void DolphinView::slotDirListerCompleted()
1196 if (!m_activeItemUrl
.isEmpty()) {
1197 // assure that the current item remains visible
1198 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_activeItemUrl
);
1199 if (dirIndex
.isValid()) {
1200 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1201 QAbstractItemView
* view
= itemView();
1202 const bool clearSelection
= !hasSelection();
1203 view
->setCurrentIndex(proxyIndex
);
1204 if (clearSelection
) {
1205 view
->clearSelection();
1207 m_activeItemUrl
.clear();
1211 if (!m_newFileNames
.isEmpty()) {
1212 // select all newly added items created by a paste operation or
1213 // a drag & drop operation
1214 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1215 const int rowCount
= m_proxyModel
->rowCount();
1216 for (int row
= 0; row
< rowCount
; ++row
) {
1217 const QModelIndex proxyIndex
= m_proxyModel
->index(row
, 0);
1218 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(proxyIndex
);
1219 const KUrl url
= m_dolphinModel
->itemForIndex(dirIndex
).url();
1220 if (m_newFileNames
.contains(url
.fileName())) {
1221 selectionModel
->select(proxyIndex
, QItemSelectionModel::Select
);
1225 m_newFileNames
.clear();
1229 void DolphinView::slotRefreshItems()
1231 if (m_assureVisibleCurrentIndex
) {
1232 m_assureVisibleCurrentIndex
= false;
1233 itemView()->scrollTo(itemView()->currentIndex());
1237 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1239 if (!url
.isValid()) {
1240 const QString
location(url
.pathOrUrl());
1241 if (location
.isEmpty()) {
1242 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1244 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1249 m_loadingDirectory
= true;
1252 m_selectedItems
= selectedItems();
1253 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1256 m_dirLister
->stop();
1257 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1259 if (isColumnViewActive()) {
1260 // adjusting the directory lister is not enough in the case of the
1261 // column view, as each column has its own directory lister internally...
1263 m_columnView
->reload();
1265 m_columnView
->showColumn(url
);
1270 KUrl
DolphinView::viewPropertiesUrl() const
1272 if (isColumnViewActive()) {
1273 return m_columnView
->rootUrl();
1279 void DolphinView::applyViewProperties(const KUrl
& url
)
1281 if (m_ignoreViewProperties
) {
1285 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1286 // The column view is active, hence don't apply the view properties
1287 // of sub directories (represented by columns) to the view. The
1288 // view always represents the properties of the first column.
1292 const ViewProperties
props(url
);
1294 const Mode mode
= props
.viewMode();
1295 if (m_mode
!= mode
) {
1296 const int oldZoomLevel
= m_controller
->zoomLevel();
1302 updateZoomLevel(oldZoomLevel
);
1304 if (itemView() == 0) {
1307 Q_ASSERT(itemView() != 0);
1308 Q_ASSERT(m_fileItemDelegate
!= 0);
1310 const bool showHiddenFiles
= props
.showHiddenFiles();
1311 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1312 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1313 emit
showHiddenFilesChanged();
1316 m_storedCategorizedSorting
= props
.categorizedSorting();
1317 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1318 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1319 m_proxyModel
->setCategorizedModel(categorized
);
1320 emit
categorizedSortingChanged();
1323 const DolphinView::Sorting sorting
= props
.sorting();
1324 if (sorting
!= m_proxyModel
->sorting()) {
1325 m_proxyModel
->setSorting(sorting
);
1326 emit
sortingChanged(sorting
);
1329 const Qt::SortOrder sortOrder
= props
.sortOrder();
1330 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1331 m_proxyModel
->setSortOrder(sortOrder
);
1332 emit
sortOrderChanged(sortOrder
);
1335 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1336 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1337 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1338 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1341 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1342 if (info
!= m_fileItemDelegate
->showInformation()) {
1343 m_fileItemDelegate
->setShowInformation(info
);
1344 emit
additionalInfoChanged();
1347 const bool showPreview
= props
.showPreview();
1348 if (showPreview
!= m_showPreview
) {
1349 m_showPreview
= showPreview
;
1350 m_previewGenerator
->setPreviewShown(showPreview
);
1352 const int oldZoomLevel
= m_controller
->zoomLevel();
1353 emit
showPreviewChanged();
1355 // Enabling or disabling the preview might change the icon size of the view.
1356 // As the view does not emit a signal when the icon size has been changed,
1357 // the used zoom level of the controller must be adjusted manually:
1358 updateZoomLevel(oldZoomLevel
);
1361 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1362 // During the lifetime of a DolphinView instance the global view properties
1363 // should not be changed. This allows e. g. to split a view and use different
1364 // view properties for each view.
1365 m_ignoreViewProperties
= true;
1369 void DolphinView::createView()
1372 Q_ASSERT(m_iconsView
== 0);
1373 Q_ASSERT(m_detailsView
== 0);
1374 Q_ASSERT(m_columnView
== 0);
1376 QAbstractItemView
* view
= 0;
1379 m_iconsView
= new DolphinIconsView(this, m_controller
);
1385 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1386 view
= m_detailsView
;
1390 m_columnView
= new DolphinColumnView(this, m_controller
);
1391 view
= m_columnView
;
1395 Q_ASSERT(view
!= 0);
1396 view
->installEventFilter(this);
1397 view
->viewport()->installEventFilter(this);
1398 setFocusProxy(view
);
1400 if (m_mode
!= ColumnView
) {
1401 // Give the view the ability to auto-expand its directories on hovering
1402 // (the column view takes care about this itself). If the details view
1403 // uses expandable folders, the auto-expanding should be used always.
1404 DolphinSettings
& settings
= DolphinSettings::instance();
1405 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1406 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1408 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1409 folderExpander
->setEnabled(enabled
);
1410 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1411 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1414 // Listen out for requests to delete the current column.
1415 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1416 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1419 m_controller
->setItemView(view
);
1421 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1422 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1423 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1424 view
->setItemDelegate(m_fileItemDelegate
);
1426 view
->setModel(m_proxyModel
);
1427 if (m_selectionModel
!= 0) {
1428 view
->setSelectionModel(m_selectionModel
);
1430 m_selectionModel
= view
->selectionModel();
1433 // reparent the selection model, as it should not be deleted
1434 // when deleting the model
1435 m_selectionModel
->setParent(this);
1437 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1439 m_previewGenerator
= new KFilePreviewGenerator(view
);
1440 m_previewGenerator
->setPreviewShown(m_showPreview
);
1442 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1443 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1444 connect(m_controller
, SIGNAL(hideToolTip()),
1445 m_toolTipManager
, SLOT(hideTip()));
1448 m_topLayout
->insertWidget(1, view
);
1450 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1451 this, SLOT(emitSelectionChangedSignal()));
1452 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1453 this, SLOT(emitContentsMoved()));
1454 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1455 this, SLOT(emitContentsMoved()));
1458 void DolphinView::deleteView()
1460 QAbstractItemView
* view
= itemView();
1462 // It's important to set the keyboard focus to the parent
1463 // before deleting the view: Otherwise when having a split
1464 // view the other view will get the focus and will request
1465 // an activation (see DolphinView::eventFilter()).
1469 m_topLayout
->removeWidget(view
);
1472 // m_previewGenerator's parent is not always destroyed, and we
1473 // don't want two active at once - manually delete.
1474 delete m_previewGenerator
;
1475 m_previewGenerator
= 0;
1478 m_controller
->disconnect(view
);
1481 deleteWhenNotDragSource(view
);
1487 m_fileItemDelegate
= 0;
1488 m_toolTipManager
= 0;
1492 QAbstractItemView
* DolphinView::itemView() const
1494 if (m_detailsView
!= 0) {
1495 return m_detailsView
;
1496 } else if (m_columnView
!= 0) {
1497 return m_columnView
;
1503 void DolphinView::pasteToUrl(const KUrl
& url
)
1505 addNewFileNames(QApplication::clipboard()->mimeData());
1506 KonqOperations::doPaste(this, url
);
1509 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1511 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1512 if (oldZoomLevel
!= newZoomLevel
) {
1513 m_controller
->setZoomLevel(newZoomLevel
);
1514 emit
zoomLevelChanged(newZoomLevel
);
1518 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1520 KUrl::List list
= selectedUrls();
1521 if (itemsExpandable() ) {
1522 list
= KDirModel::simplifiedUrlList(list
);
1527 QMimeData
* DolphinView::selectionMimeData() const
1529 if (isColumnViewActive()) {
1530 return m_columnView
->selectionMimeData();
1533 const QAbstractItemView
* view
= itemView();
1534 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1535 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1536 return m_dolphinModel
->mimeData(selection
.indexes());
1539 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1541 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1542 foreach (const KUrl
& url
, urls
) {
1543 m_newFileNames
.insert(url
.fileName());
1547 #include "dolphinview.moc"