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_selectClipboardItems(false),
94 m_mode(DolphinView::IconsView
),
100 m_fileItemDelegate(0),
102 m_dolphinModel(dolphinModel
),
103 m_dirLister(dirLister
),
104 m_proxyModel(proxyModel
),
105 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 // when a copy/move-operation has been finished, the pasted items should get selected
162 connect(KIO::FileUndoManager::self(), SIGNAL(jobRecordingFinished(CommandType
)),
163 this, SLOT(slotJobRecordingFinished(CommandType
)));
165 applyViewProperties(url
);
166 m_topLayout
->addWidget(itemView());
169 DolphinView::~DolphinView()
171 delete m_expandedDragSource
;
172 m_expandedDragSource
= 0;
175 const KUrl
& DolphinView::url() const
177 return m_controller
->url();
180 KUrl
DolphinView::rootUrl() const
182 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
185 void DolphinView::setActive(bool active
)
187 if (active
== m_active
) {
193 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
195 emit
selectionChanged(selectedItems());
200 QWidget
* viewport
= itemView()->viewport();
202 palette
.setColor(viewport
->backgroundRole(), color
);
203 viewport
->setPalette(palette
);
208 itemView()->setFocus();
212 m_controller
->indicateActivationChange(active
);
215 bool DolphinView::isActive() const
220 void DolphinView::setMode(Mode mode
)
222 if (mode
== m_mode
) {
223 return; // the wished mode is already set
226 const int oldZoomLevel
= m_controller
->zoomLevel();
231 const KUrl viewPropsUrl
= viewPropertiesUrl();
232 ViewProperties
props(viewPropsUrl
);
233 props
.setViewMode(m_mode
);
236 // the file item delegate has been recreated, apply the current
237 // additional information manually
238 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
239 m_fileItemDelegate
->setShowInformation(infoList
);
240 emit
additionalInfoChanged();
242 // Not all view modes support categorized sorting. Adjust the sorting model
243 // if changing the view mode results in a change of the categorized sorting
245 m_storedCategorizedSorting
= props
.categorizedSorting();
246 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
247 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
248 m_proxyModel
->setCategorizedModel(categorized
);
249 emit
categorizedSortingChanged();
254 updateZoomLevel(oldZoomLevel
);
256 loadDirectory(viewPropsUrl
);
260 DolphinView::Mode
DolphinView::mode() const
265 bool DolphinView::showPreview() const
267 return m_showPreview
;
270 bool DolphinView::showHiddenFiles() const
272 return m_dirLister
->showingDotFiles();
275 bool DolphinView::categorizedSorting() const
277 // If all view modes would support categorized sorting, returning
278 // m_proxyModel->isCategorizedModel() would be the way to go. As
279 // currently only the icons view supports caterized sorting, we remember
280 // the stored view properties state in m_storedCategorizedSorting and
281 // return this state. The application takes care to disable the corresponding
282 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
283 // that this setting is not applied to the current view mode.
284 return m_storedCategorizedSorting
;
287 bool DolphinView::supportsCategorizedSorting() const
289 return m_iconsView
!= 0;
292 void DolphinView::selectAll()
294 QAbstractItemView
* view
= itemView();
295 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
296 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
297 // selection instead of selecting all items. This is bypassed for KDE 4.0
298 // by invoking clearSelection() first.
299 view
->clearSelection();
303 void DolphinView::invertSelection()
305 if (isColumnViewActive()) {
306 // QAbstractItemView does not offer a virtual method invertSelection()
307 // as counterpart to QAbstractItemView::selectAll(). This makes it
308 // necessary to delegate the inverting of the selection to the
309 // column view, as only the selection of the active column should
311 m_columnView
->invertSelection();
313 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
314 const QAbstractItemModel
* itemModel
= selectionModel
->model();
316 const QModelIndex topLeft
= itemModel
->index(0, 0);
317 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
318 itemModel
->columnCount() - 1);
320 const QItemSelection
selection(topLeft
, bottomRight
);
321 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
325 bool DolphinView::hasSelection() const
327 return itemView()->selectionModel()->hasSelection();
330 void DolphinView::clearSelection()
332 QItemSelectionModel
* selModel
= itemView()->selectionModel();
333 const QModelIndex currentIndex
= selModel
->currentIndex();
334 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
335 QItemSelectionModel::Clear
);
338 KFileItemList
DolphinView::selectedItems() const
340 if (isColumnViewActive()) {
341 return m_columnView
->selectedItems();
344 const QAbstractItemView
* view
= itemView();
346 // Our view has a selection, we will map them back to the DolphinModel
347 // and then fill the KFileItemList.
348 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
350 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
351 KFileItemList itemList
;
353 const QModelIndexList indexList
= selection
.indexes();
354 foreach (const QModelIndex
&index
, indexList
) {
355 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
356 if (!item
.isNull()) {
357 itemList
.append(item
);
364 KUrl::List
DolphinView::selectedUrls() const
367 const KFileItemList list
= selectedItems();
368 foreach (const KFileItem
&item
, list
) {
369 urls
.append(item
.url());
374 int DolphinView::selectedItemsCount() const
376 if (isColumnViewActive()) {
377 // TODO: get rid of this special case by adjusting the dir lister
378 // to the current column
379 return m_columnView
->selectedItems().count();
382 return itemView()->selectionModel()->selection().count();
385 void DolphinView::setContentsPosition(int x
, int y
)
387 QAbstractItemView
* view
= itemView();
389 // the ColumnView takes care itself for the horizontal scrolling
390 if (!isColumnViewActive()) {
391 view
->horizontalScrollBar()->setValue(x
);
393 view
->verticalScrollBar()->setValue(y
);
395 m_loadingDirectory
= false;
398 QPoint
DolphinView::contentsPosition() const
400 const int x
= itemView()->horizontalScrollBar()->value();
401 const int y
= itemView()->verticalScrollBar()->value();
405 void DolphinView::setZoomLevel(int level
)
407 if (level
< ZoomLevelInfo::minimumLevel()) {
408 level
= ZoomLevelInfo::minimumLevel();
409 } else if (level
> ZoomLevelInfo::maximumLevel()) {
410 level
= ZoomLevelInfo::maximumLevel();
413 if (level
!= zoomLevel()) {
414 m_controller
->setZoomLevel(level
);
415 m_previewGenerator
->updateIcons();
416 emit
zoomLevelChanged(level
);
420 int DolphinView::zoomLevel() const
422 return m_controller
->zoomLevel();
425 void DolphinView::setSorting(Sorting sorting
)
427 if (sorting
!= this->sorting()) {
428 updateSorting(sorting
);
432 DolphinView::Sorting
DolphinView::sorting() const
434 return m_proxyModel
->sorting();
437 void DolphinView::setSortOrder(Qt::SortOrder order
)
439 if (sortOrder() != order
) {
440 updateSortOrder(order
);
444 Qt::SortOrder
DolphinView::sortOrder() const
446 return m_proxyModel
->sortOrder();
449 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
451 if (sortFoldersFirst() != foldersFirst
) {
452 updateSortFoldersFirst(foldersFirst
);
456 bool DolphinView::sortFoldersFirst() const
458 return m_proxyModel
->sortFoldersFirst();
461 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
463 const KUrl viewPropsUrl
= viewPropertiesUrl();
464 ViewProperties
props(viewPropsUrl
);
465 props
.setAdditionalInfo(info
);
466 m_fileItemDelegate
->setShowInformation(info
);
468 emit
additionalInfoChanged();
470 if (itemView() != m_detailsView
) {
471 // the details view requires no reloading of the directory, as it maps
472 // the file item delegate info to its columns internally
473 loadDirectory(viewPropsUrl
);
477 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
479 return m_fileItemDelegate
->showInformation();
482 void DolphinView::reload()
485 loadDirectory(url(), true);
488 void DolphinView::refresh()
490 m_ignoreViewProperties
= false;
492 const bool oldActivationState
= m_active
;
493 const int oldZoomLevel
= m_controller
->zoomLevel();
497 applyViewProperties(m_controller
->url());
500 setActive(oldActivationState
);
501 updateZoomLevel(oldZoomLevel
);
504 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
506 if (m_controller
->url() == url
) {
510 m_previewGenerator
->cancelPreviews();
511 m_controller
->setUrl(url
); // emits urlChanged, which we forward
513 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
514 applyViewProperties(rootUrl
);
515 loadDirectory(rootUrl
);
516 if (itemView() == m_columnView
) {
517 m_columnView
->setRootUrl(rootUrl
);
518 m_columnView
->showColumn(url
);
521 applyViewProperties(url
);
525 emit
startedPathLoading(url
);
528 void DolphinView::setNameFilter(const QString
& nameFilter
)
530 m_proxyModel
->setFilterRegExp(nameFilter
);
532 if (isColumnViewActive()) {
533 // adjusting the directory lister is not enough in the case of the
534 // column view, as each column has its own directory lister internally...
535 m_columnView
->setNameFilter(nameFilter
);
539 void DolphinView::calculateItemCount(int& fileCount
,
541 KIO::filesize_t
& totalFileSize
) const
543 foreach (const KFileItem
& item
, m_dirLister
->items()) {
548 totalFileSize
+= item
.size();
553 QString
DolphinView::statusBarText() const
558 KIO::filesize_t totalFileSize
= 0;
560 if (hasSelection()) {
561 // give a summary of the status of the selected files
562 const KFileItemList list
= selectedItems();
563 if (list
.isEmpty()) {
564 // when an item is triggered, it is temporary selected but selectedItems()
565 // will return an empty list
569 KFileItemList::const_iterator it
= list
.begin();
570 const KFileItemList::const_iterator end
= list
.end();
572 const KFileItem
& item
= *it
;
577 totalFileSize
+= item
.size();
582 if (folderCount
+ fileCount
== 1) {
583 // if only one item is selected, show the filename
584 const QString name
= list
.first().name();
585 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
586 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
587 name
, KIO::convertSize(totalFileSize
));
589 // at least 2 items are selected
590 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
591 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
592 if ((folderCount
> 0) && (fileCount
> 0)) {
593 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
594 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
595 } else if (fileCount
> 0) {
596 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
598 Q_ASSERT(folderCount
> 0);
603 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
604 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
605 fileCount
, folderCount
,
606 totalFileSize
, true);
612 void DolphinView::setUrl(const KUrl
& url
)
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 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
990 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
992 ViewProperties
props(viewPropertiesUrl());
993 props
.setSorting(sorting
);
995 m_proxyModel
->setSorting(sorting
);
997 emit
sortingChanged(sorting
);
1000 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1002 ViewProperties
props(viewPropertiesUrl());
1003 props
.setSortOrder(order
);
1005 m_proxyModel
->setSortOrder(order
);
1007 emit
sortOrderChanged(order
);
1010 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1012 ViewProperties
props(viewPropertiesUrl());
1013 props
.setSortFoldersFirst(foldersFirst
);
1015 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1017 emit
sortFoldersFirstChanged(foldersFirst
);
1020 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1022 ViewProperties
props(viewPropertiesUrl());
1023 props
.setAdditionalInfo(info
);
1026 m_fileItemDelegate
->setShowInformation(info
);
1028 emit
additionalInfoChanged();
1031 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1033 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1034 (m_mode
== DolphinView::IconsView
);
1036 QAction
* showSizeInfo
= collection
->action("show_size_info");
1037 QAction
* showDateInfo
= collection
->action("show_date_info");
1038 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1039 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1040 QAction
* showGroupInfo
= collection
->action("show_group_info");
1041 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1043 showSizeInfo
->setChecked(false);
1044 showDateInfo
->setChecked(false);
1045 showPermissionsInfo
->setChecked(false);
1046 showOwnerInfo
->setChecked(false);
1047 showGroupInfo
->setChecked(false);
1048 showMimeInfo
->setChecked(false);
1050 showSizeInfo
->setEnabled(enable
);
1051 showDateInfo
->setEnabled(enable
);
1052 showPermissionsInfo
->setEnabled(enable
);
1053 showOwnerInfo
->setEnabled(enable
);
1054 showGroupInfo
->setEnabled(enable
);
1055 showMimeInfo
->setEnabled(enable
);
1057 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1059 case KFileItemDelegate::Size
:
1060 showSizeInfo
->setChecked(true);
1062 case KFileItemDelegate::ModificationTime
:
1063 showDateInfo
->setChecked(true);
1065 case KFileItemDelegate::Permissions
:
1066 showPermissionsInfo
->setChecked(true);
1068 case KFileItemDelegate::Owner
:
1069 showOwnerInfo
->setChecked(true);
1071 case KFileItemDelegate::OwnerAndGroup
:
1072 showGroupInfo
->setChecked(true);
1074 case KFileItemDelegate::FriendlyMimeType
:
1075 showMimeInfo
->setChecked(true);
1083 QPair
<bool, QString
> DolphinView::pasteInfo() const
1085 return KonqOperations::pasteInfo(url());
1088 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1090 m_tabsForFiles
= tabsForFiles
;
1093 bool DolphinView::isTabsForFilesEnabled() const
1095 return m_tabsForFiles
;
1098 bool DolphinView::itemsExpandable() const
1100 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1103 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1108 if (DragAndDropHelper::instance().isDragSource(view
)) {
1109 // We must store for later deletion.
1110 if (m_expandedDragSource
!= 0) {
1111 // The old stored view is obviously not the drag source anymore.
1112 m_expandedDragSource
->deleteLater();
1113 m_expandedDragSource
= 0;
1116 m_expandedDragSource
= view
;
1119 view
->deleteLater();
1123 void DolphinView::observeCreatedItem(const KUrl
& url
)
1125 m_createdItemUrl
= url
;
1126 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1127 this, SLOT(selectAndScrollToCreatedItem()));
1130 void DolphinView::selectAndScrollToCreatedItem()
1132 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1133 if (dirIndex
.isValid()) {
1134 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1135 itemView()->setCurrentIndex(proxyIndex
);
1138 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1139 this, SLOT(selectAndScrollToCreatedItem()));
1140 m_createdItemUrl
= KUrl();
1143 void DolphinView::restoreSelection()
1145 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1146 changeSelection(m_selectedItems
);
1149 void DolphinView::slotJobRecordingFinished(CommandType command
)
1151 // Assure that the pasted items get selected. This must be done
1152 // asynchronously in slotDirListerCompleted().
1153 m_selectClipboardItems
= ((command
== KIO::FileUndoManager::Copy
) ||
1154 (command
== KIO::FileUndoManager::Move
)) &&
1158 void DolphinView::emitContentsMoved()
1160 // only emit the contents moved signal if:
1161 // - no directory loading is ongoing (this would reset the contents position
1162 // always to (0, 0))
1163 // - if the Column View is active: the column view does an automatic
1164 // positioning during the loading operation, which must be remembered
1165 if (!m_loadingDirectory
|| isColumnViewActive()) {
1166 const QPoint
pos(contentsPosition());
1167 emit
contentsMoved(pos
.x(), pos
.y());
1171 void DolphinView::showHoverInformation(const KFileItem
& item
)
1173 emit
requestItemInfo(item
);
1176 void DolphinView::clearHoverInformation()
1178 emit
requestItemInfo(KFileItem());
1181 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1183 if (job
->error() == 0) {
1184 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1185 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1186 emit
errorMessage(job
->errorString());
1190 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1192 emit
requestUrlChange(url
);
1193 m_controller
->setUrl(url
);
1196 void DolphinView::slotDirListerCompleted()
1198 if (!m_currentItemUrl
.isEmpty()) {
1199 // assure that the current item remains visible
1200 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1201 if (dirIndex
.isValid()) {
1202 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1203 QAbstractItemView
* view
= itemView();
1204 const bool clearSelection
= !hasSelection();
1205 view
->setCurrentIndex(proxyIndex
);
1206 if (clearSelection
) {
1207 view
->clearSelection();
1210 m_currentItemUrl
.clear();
1213 if (m_selectClipboardItems
) {
1214 m_selectClipboardItems
= false;
1216 // select all items that have been pasted from the clipboard to
1217 // the current directory
1218 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1219 const KUrl::List copiedUrls
= KUrl::List::fromMimeData(mimeData
);
1221 QSet
<QString
> fileNames
;
1222 foreach (const KUrl
& url
, copiedUrls
) {
1223 fileNames
.insert(url
.fileName());
1226 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1227 const int rowCount
= m_proxyModel
->rowCount();
1228 for (int row
= 0; row
< rowCount
; ++row
) {
1229 const QModelIndex proxyIndex
= m_proxyModel
->index(row
, 0);
1230 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(proxyIndex
);
1231 const KUrl url
= m_dolphinModel
->itemForIndex(dirIndex
).url();
1232 if (fileNames
.contains(url
.fileName())) {
1233 selectionModel
->select(proxyIndex
, QItemSelectionModel::Select
);
1239 void DolphinView::slotRefreshItems()
1241 if (m_assureVisibleCurrentIndex
) {
1242 m_assureVisibleCurrentIndex
= false;
1243 itemView()->scrollTo(itemView()->currentIndex());
1247 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1249 if (!url
.isValid()) {
1250 const QString
location(url
.pathOrUrl());
1251 if (location
.isEmpty()) {
1252 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1254 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1259 m_loadingDirectory
= true;
1262 m_selectedItems
= selectedItems();
1263 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1266 m_dirLister
->stop();
1267 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1269 if (isColumnViewActive()) {
1270 // adjusting the directory lister is not enough in the case of the
1271 // column view, as each column has its own directory lister internally...
1273 m_columnView
->reload();
1275 m_columnView
->showColumn(url
);
1280 KUrl
DolphinView::viewPropertiesUrl() const
1282 if (isColumnViewActive()) {
1283 return m_columnView
->rootUrl();
1289 void DolphinView::applyViewProperties(const KUrl
& url
)
1291 if (m_ignoreViewProperties
) {
1295 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1296 // The column view is active, hence don't apply the view properties
1297 // of sub directories (represented by columns) to the view. The
1298 // view always represents the properties of the first column.
1302 const ViewProperties
props(url
);
1304 const Mode mode
= props
.viewMode();
1305 if (m_mode
!= mode
) {
1306 const int oldZoomLevel
= m_controller
->zoomLevel();
1312 updateZoomLevel(oldZoomLevel
);
1314 if (itemView() == 0) {
1317 Q_ASSERT(itemView() != 0);
1318 Q_ASSERT(m_fileItemDelegate
!= 0);
1320 const bool showHiddenFiles
= props
.showHiddenFiles();
1321 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1322 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1323 emit
showHiddenFilesChanged();
1326 m_storedCategorizedSorting
= props
.categorizedSorting();
1327 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1328 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1329 m_proxyModel
->setCategorizedModel(categorized
);
1330 emit
categorizedSortingChanged();
1333 const DolphinView::Sorting sorting
= props
.sorting();
1334 if (sorting
!= m_proxyModel
->sorting()) {
1335 m_proxyModel
->setSorting(sorting
);
1336 emit
sortingChanged(sorting
);
1339 const Qt::SortOrder sortOrder
= props
.sortOrder();
1340 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1341 m_proxyModel
->setSortOrder(sortOrder
);
1342 emit
sortOrderChanged(sortOrder
);
1345 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1346 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1347 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1348 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1351 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1352 if (info
!= m_fileItemDelegate
->showInformation()) {
1353 m_fileItemDelegate
->setShowInformation(info
);
1354 emit
additionalInfoChanged();
1357 const bool showPreview
= props
.showPreview();
1358 if (showPreview
!= m_showPreview
) {
1359 m_showPreview
= showPreview
;
1360 m_previewGenerator
->setPreviewShown(showPreview
);
1362 const int oldZoomLevel
= m_controller
->zoomLevel();
1363 emit
showPreviewChanged();
1365 // Enabling or disabling the preview might change the icon size of the view.
1366 // As the view does not emit a signal when the icon size has been changed,
1367 // the used zoom level of the controller must be adjusted manually:
1368 updateZoomLevel(oldZoomLevel
);
1371 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1372 // During the lifetime of a DolphinView instance the global view properties
1373 // should not be changed. This allows e. g. to split a view and use different
1374 // view properties for each view.
1375 m_ignoreViewProperties
= true;
1379 void DolphinView::createView()
1382 Q_ASSERT(m_iconsView
== 0);
1383 Q_ASSERT(m_detailsView
== 0);
1384 Q_ASSERT(m_columnView
== 0);
1386 QAbstractItemView
* view
= 0;
1389 m_iconsView
= new DolphinIconsView(this, m_controller
);
1395 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1396 view
= m_detailsView
;
1400 m_columnView
= new DolphinColumnView(this, m_controller
);
1401 view
= m_columnView
;
1405 Q_ASSERT(view
!= 0);
1406 view
->installEventFilter(this);
1407 view
->viewport()->installEventFilter(this);
1408 setFocusProxy(view
);
1410 if (m_mode
!= ColumnView
) {
1411 // Give the view the ability to auto-expand its directories on hovering
1412 // (the column view takes care about this itself). If the details view
1413 // uses expandable folders, the auto-expanding should be used always.
1414 DolphinSettings
& settings
= DolphinSettings::instance();
1415 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1416 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1418 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1419 folderExpander
->setEnabled(enabled
);
1420 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1421 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1424 // Listen out for requests to delete the current column.
1425 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1426 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1429 m_controller
->setItemView(view
);
1431 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1432 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1433 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1434 view
->setItemDelegate(m_fileItemDelegate
);
1436 view
->setModel(m_proxyModel
);
1437 if (m_selectionModel
!= 0) {
1438 view
->setSelectionModel(m_selectionModel
);
1440 m_selectionModel
= view
->selectionModel();
1443 // reparent the selection model, as it should not be deleted
1444 // when deleting the model
1445 m_selectionModel
->setParent(this);
1447 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1449 m_previewGenerator
= new KFilePreviewGenerator(view
);
1450 m_previewGenerator
->setPreviewShown(m_showPreview
);
1452 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1453 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1454 connect(m_controller
, SIGNAL(hideToolTip()),
1455 m_toolTipManager
, SLOT(hideTip()));
1458 m_topLayout
->insertWidget(1, view
);
1460 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1461 this, SLOT(emitSelectionChangedSignal()));
1462 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1463 this, SLOT(emitContentsMoved()));
1464 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1465 this, SLOT(emitContentsMoved()));
1468 void DolphinView::deleteView()
1470 QAbstractItemView
* view
= itemView();
1472 // It's important to set the keyboard focus to the parent
1473 // before deleting the view: Otherwise when having a split
1474 // view the other view will get the focus and will request
1475 // an activation (see DolphinView::eventFilter()).
1479 m_topLayout
->removeWidget(view
);
1482 // m_previewGenerator's parent is not always destroyed, and we
1483 // don't want two active at once - manually delete.
1484 delete m_previewGenerator
;
1485 m_previewGenerator
= 0;
1488 m_controller
->disconnect(view
);
1491 deleteWhenNotDragSource(view
);
1497 m_fileItemDelegate
= 0;
1498 m_toolTipManager
= 0;
1502 QAbstractItemView
* DolphinView::itemView() const
1504 if (m_detailsView
!= 0) {
1505 return m_detailsView
;
1506 } else if (m_columnView
!= 0) {
1507 return m_columnView
;
1513 void DolphinView::pasteToUrl(const KUrl
& url
)
1515 KonqOperations::doPaste(this, url
);
1518 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1520 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1521 if (oldZoomLevel
!= newZoomLevel
) {
1522 m_controller
->setZoomLevel(newZoomLevel
);
1523 emit
zoomLevelChanged(newZoomLevel
);
1527 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1529 KUrl::List list
= selectedUrls();
1530 if (itemsExpandable() ) {
1531 list
= KDirModel::simplifiedUrlList(list
);
1536 QMimeData
* DolphinView::selectionMimeData() const
1538 if (isColumnViewActive()) {
1539 return m_columnView
->selectionMimeData();
1542 const QAbstractItemView
* view
= itemView();
1543 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1544 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1545 return m_dolphinModel
->mimeData(selection
.indexes());
1549 #include "dolphinview.moc"