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 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
652 if (dialog
->exec() == QDialog::Rejected
) {
657 const QString newName
= dialog
->newName();
658 if (newName
.isEmpty()) {
659 emit
errorMessage(dialog
->errorString());
665 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
666 // as one operation instead of n rename operations like it is done now...
667 Q_ASSERT(newName
.contains('#'));
669 // currently the items are sorted by the selection order, resort
670 // them by the file name
671 qSort(items
.begin(), items
.end(), lessThan
);
673 // iterate through all selected items and rename them...
675 foreach (const KFileItem
& item
, items
) {
676 const KUrl
& oldUrl
= item
.url();
678 number
.setNum(index
++);
680 QString name
= newName
;
681 name
.replace('#', number
);
683 if (oldUrl
.fileName() != name
) {
684 KUrl newUrl
= oldUrl
;
685 newUrl
.setFileName(name
);
686 KonqOperations::rename(this, oldUrl
, newUrl
);
689 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
690 Q_ASSERT(itemCount
== 1);
692 if (isColumnViewActive()) {
693 m_columnView
->editItem(items
.first());
695 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
696 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
697 itemView()->edit(proxyIndex
);
700 Q_ASSERT(itemCount
== 1);
702 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
703 if (dialog
->exec() == QDialog::Rejected
) {
708 const QString newName
= dialog
->newName();
709 if (newName
.isEmpty()) {
710 emit
errorMessage(dialog
->errorString());
716 const KUrl
& oldUrl
= items
.first().url();
717 KUrl newUrl
= oldUrl
;
718 newUrl
.setFileName(newName
);
719 KonqOperations::rename(this, oldUrl
, newUrl
);
722 // assure that the current index remains visible when KDirLister
723 // will notify the view about changed items
724 m_assureVisibleCurrentIndex
= true;
727 void DolphinView::trashSelectedItems()
729 const KUrl::List list
= simplifiedSelectedUrls();
730 KonqOperations::del(this, KonqOperations::TRASH
, list
);
733 void DolphinView::deleteSelectedItems()
735 const KUrl::List list
= simplifiedSelectedUrls();
736 const bool del
= KonqOperations::askDeleteConfirmation(list
,
738 KonqOperations::DEFAULT_CONFIRMATION
,
742 KIO::Job
* job
= KIO::del(list
);
743 connect(job
, SIGNAL(result(KJob
*)),
744 this, SLOT(slotDeleteFileFinished(KJob
*)));
748 void DolphinView::cutSelectedItems()
750 QMimeData
* mimeData
= selectionMimeData();
751 KonqMimeData::addIsCutSelection(mimeData
, true);
752 QApplication::clipboard()->setMimeData(mimeData
);
755 void DolphinView::copySelectedItems()
757 QMimeData
* mimeData
= selectionMimeData();
758 QApplication::clipboard()->setMimeData(mimeData
);
761 void DolphinView::paste()
766 void DolphinView::pasteIntoFolder()
768 const KFileItemList items
= selectedItems();
769 if ((items
.count() == 1) && items
.first().isDir()) {
770 pasteToUrl(items
.first().url());
774 void DolphinView::setShowPreview(bool show
)
776 if (m_showPreview
== show
) {
780 const KUrl viewPropsUrl
= viewPropertiesUrl();
781 ViewProperties
props(viewPropsUrl
);
782 props
.setShowPreview(show
);
784 m_showPreview
= show
;
785 m_previewGenerator
->setPreviewShown(show
);
787 const int oldZoomLevel
= m_controller
->zoomLevel();
788 emit
showPreviewChanged();
790 // Enabling or disabling the preview might change the icon size of the view.
791 // As the view does not emit a signal when the icon size has been changed,
792 // the used zoom level of the controller must be adjusted manually:
793 updateZoomLevel(oldZoomLevel
);
795 loadDirectory(viewPropsUrl
);
798 void DolphinView::setShowHiddenFiles(bool show
)
800 if (m_dirLister
->showingDotFiles() == show
) {
804 const KUrl viewPropsUrl
= viewPropertiesUrl();
805 ViewProperties
props(viewPropsUrl
);
806 props
.setShowHiddenFiles(show
);
808 m_dirLister
->setShowingDotFiles(show
);
809 emit
showHiddenFilesChanged();
811 loadDirectory(viewPropsUrl
);
814 void DolphinView::setCategorizedSorting(bool categorized
)
816 if (categorized
== categorizedSorting()) {
820 // setCategorizedSorting(true) may only get invoked
821 // if the view supports categorized sorting
822 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
824 ViewProperties
props(viewPropertiesUrl());
825 props
.setCategorizedSorting(categorized
);
828 m_storedCategorizedSorting
= categorized
;
829 m_proxyModel
->setCategorizedModel(categorized
);
831 emit
categorizedSortingChanged();
834 void DolphinView::toggleSortOrder()
836 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
837 Qt::DescendingOrder
:
842 void DolphinView::toggleSortFoldersFirst()
844 setSortFoldersFirst(!sortFoldersFirst());
847 void DolphinView::toggleAdditionalInfo(QAction
* action
)
849 const KFileItemDelegate::Information info
=
850 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
852 KFileItemDelegate::InformationList list
= additionalInfo();
854 const bool show
= action
->isChecked();
856 const int index
= list
.indexOf(info
);
857 const bool containsInfo
= (index
>= 0);
858 if (show
&& !containsInfo
) {
860 setAdditionalInfo(list
);
861 } else if (!show
&& containsInfo
) {
862 list
.removeAt(index
);
863 setAdditionalInfo(list
);
864 Q_ASSERT(list
.indexOf(info
) < 0);
868 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
870 QWidget::mouseReleaseEvent(event
);
874 void DolphinView::wheelEvent(QWheelEvent
* event
)
876 if (event
->modifiers() & Qt::ControlModifier
) {
877 const int delta
= event
->delta();
878 const int level
= zoomLevel();
880 setZoomLevel(level
+ 1);
881 } else if (delta
< 0) {
882 setZoomLevel(level
- 1);
888 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
890 switch (event
->type()) {
891 case QEvent::FocusIn
:
892 if (watched
== itemView()) {
893 m_controller
->requestActivation();
897 case QEvent::MouseButtonPress
:
898 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
899 // Listening to a mousebutton press event to delete expanded views is a
900 // workaround, as it seems impossible for the FolderExpander to know when
901 // a dragging outside a view has been finished. However it works quite well:
902 // A mousebutton press event indicates that a drag operation must be
904 m_expandedDragSource
->deleteLater();
905 m_expandedDragSource
= 0;
909 case QEvent::DragEnter
:
910 if (watched
== itemView()->viewport()) {
915 case QEvent::KeyPress
:
916 if (watched
== itemView()) {
917 if (m_toolTipManager
!= 0) {
918 m_toolTipManager
->hideTip();
921 // clear the selection when Escape has been pressed
922 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
923 if (keyEvent
->key() == Qt::Key_Escape
) {
933 return QWidget::eventFilter(watched
, event
);
936 void DolphinView::activate()
941 void DolphinView::triggerItem(const KFileItem
& item
)
943 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
944 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
945 // items are selected by the user, hence don't trigger the
946 // item specified by 'index'
950 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
951 if (item
.isNull() || m_isContextMenuOpen
) {
955 if (m_toolTipManager
!= 0) {
956 m_toolTipManager
->hideTip();
958 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
961 void DolphinView::emitSelectionChangedSignal()
963 emit
selectionChanged(DolphinView::selectedItems());
966 void DolphinView::openContextMenu(const QPoint
& pos
,
967 const QList
<QAction
*>& customActions
)
970 if (isColumnViewActive()) {
971 item
= m_columnView
->itemAt(pos
);
973 const QModelIndex index
= itemView()->indexAt(pos
);
974 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
975 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
976 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
980 if (m_toolTipManager
!= 0) {
981 m_toolTipManager
->hideTip();
984 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
985 emit
requestContextMenu(item
, url(), customActions
);
986 m_isContextMenuOpen
= false;
989 void DolphinView::dropUrls(const KFileItem
& destItem
,
990 const KUrl
& destPath
,
993 addNewFileNames(event
->mimeData());
994 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
997 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
999 ViewProperties
props(viewPropertiesUrl());
1000 props
.setSorting(sorting
);
1002 m_proxyModel
->setSorting(sorting
);
1004 emit
sortingChanged(sorting
);
1007 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1009 ViewProperties
props(viewPropertiesUrl());
1010 props
.setSortOrder(order
);
1012 m_proxyModel
->setSortOrder(order
);
1014 emit
sortOrderChanged(order
);
1017 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1019 ViewProperties
props(viewPropertiesUrl());
1020 props
.setSortFoldersFirst(foldersFirst
);
1022 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1024 emit
sortFoldersFirstChanged(foldersFirst
);
1027 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1029 ViewProperties
props(viewPropertiesUrl());
1030 props
.setAdditionalInfo(info
);
1033 m_fileItemDelegate
->setShowInformation(info
);
1035 emit
additionalInfoChanged();
1038 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1040 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1041 (m_mode
== DolphinView::IconsView
);
1043 QAction
* showSizeInfo
= collection
->action("show_size_info");
1044 QAction
* showDateInfo
= collection
->action("show_date_info");
1045 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1046 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1047 QAction
* showGroupInfo
= collection
->action("show_group_info");
1048 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1050 showSizeInfo
->setChecked(false);
1051 showDateInfo
->setChecked(false);
1052 showPermissionsInfo
->setChecked(false);
1053 showOwnerInfo
->setChecked(false);
1054 showGroupInfo
->setChecked(false);
1055 showMimeInfo
->setChecked(false);
1057 showSizeInfo
->setEnabled(enable
);
1058 showDateInfo
->setEnabled(enable
);
1059 showPermissionsInfo
->setEnabled(enable
);
1060 showOwnerInfo
->setEnabled(enable
);
1061 showGroupInfo
->setEnabled(enable
);
1062 showMimeInfo
->setEnabled(enable
);
1064 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1066 case KFileItemDelegate::Size
:
1067 showSizeInfo
->setChecked(true);
1069 case KFileItemDelegate::ModificationTime
:
1070 showDateInfo
->setChecked(true);
1072 case KFileItemDelegate::Permissions
:
1073 showPermissionsInfo
->setChecked(true);
1075 case KFileItemDelegate::Owner
:
1076 showOwnerInfo
->setChecked(true);
1078 case KFileItemDelegate::OwnerAndGroup
:
1079 showGroupInfo
->setChecked(true);
1081 case KFileItemDelegate::FriendlyMimeType
:
1082 showMimeInfo
->setChecked(true);
1090 QPair
<bool, QString
> DolphinView::pasteInfo() const
1092 return KonqOperations::pasteInfo(url());
1095 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1097 m_tabsForFiles
= tabsForFiles
;
1100 bool DolphinView::isTabsForFilesEnabled() const
1102 return m_tabsForFiles
;
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_currentItemUrl
.isEmpty()) {
1197 // assure that the current item remains visible
1198 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
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();
1208 m_currentItemUrl
.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"