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 "revisioncontrolobserver.h"
66 #include "tooltips/tooltipmanager.h"
67 #include "settings/dolphinsettings.h"
68 #include "viewproperties.h"
69 #include "zoomlevelinfo.h"
72 * Helper function for sorting items with qSort() in
73 * DolphinView::renameSelectedItems().
75 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
77 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
80 DolphinView::DolphinView(QWidget
* parent
,
82 KDirLister
* dirLister
,
83 DolphinModel
* dolphinModel
,
84 DolphinSortFilterProxyModel
* proxyModel
) :
88 m_loadingDirectory(false),
89 m_storedCategorizedSorting(false),
90 m_tabsForFiles(false),
91 m_isContextMenuOpen(false),
92 m_ignoreViewProperties(false),
93 m_assureVisibleCurrentIndex(false),
94 m_mode(DolphinView::IconsView
),
100 m_fileItemDelegate(0),
102 m_selectionChangedTimer(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 emitSelectionChangedSignal();
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
);
334 m_selectedItems
.clear();
337 KFileItemList
DolphinView::selectedItems() const
339 if (isColumnViewActive()) {
340 return m_columnView
->selectedItems();
343 const QAbstractItemView
* view
= itemView();
345 // Our view has a selection, we will map them back to the DolphinModel
346 // and then fill the KFileItemList.
347 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
349 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
350 KFileItemList itemList
;
352 const QModelIndexList indexList
= selection
.indexes();
353 foreach (const QModelIndex
&index
, indexList
) {
354 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
355 if (!item
.isNull()) {
356 itemList
.append(item
);
363 KUrl::List
DolphinView::selectedUrls() const
366 const KFileItemList list
= selectedItems();
367 foreach (const KFileItem
&item
, list
) {
368 urls
.append(item
.url());
373 int DolphinView::selectedItemsCount() const
375 if (isColumnViewActive()) {
376 // TODO: get rid of this special case by adjusting the dir lister
377 // to the current column
378 return m_columnView
->selectedItems().count();
381 return itemView()->selectionModel()->selectedIndexes().count();
384 void DolphinView::setContentsPosition(int x
, int y
)
386 QAbstractItemView
* view
= itemView();
388 // the ColumnView takes care itself for the horizontal scrolling
389 if (!isColumnViewActive()) {
390 view
->horizontalScrollBar()->setValue(x
);
392 view
->verticalScrollBar()->setValue(y
);
394 m_loadingDirectory
= false;
397 QPoint
DolphinView::contentsPosition() const
399 const int x
= itemView()->horizontalScrollBar()->value();
400 const int y
= itemView()->verticalScrollBar()->value();
404 void DolphinView::setZoomLevel(int level
)
406 if (level
< ZoomLevelInfo::minimumLevel()) {
407 level
= ZoomLevelInfo::minimumLevel();
408 } else if (level
> ZoomLevelInfo::maximumLevel()) {
409 level
= ZoomLevelInfo::maximumLevel();
412 if (level
!= zoomLevel()) {
413 m_controller
->setZoomLevel(level
);
414 m_previewGenerator
->updateIcons();
415 emit
zoomLevelChanged(level
);
419 int DolphinView::zoomLevel() const
421 return m_controller
->zoomLevel();
424 void DolphinView::setSorting(Sorting sorting
)
426 if (sorting
!= this->sorting()) {
427 updateSorting(sorting
);
431 DolphinView::Sorting
DolphinView::sorting() const
433 return m_proxyModel
->sorting();
436 void DolphinView::setSortOrder(Qt::SortOrder order
)
438 if (sortOrder() != order
) {
439 updateSortOrder(order
);
443 Qt::SortOrder
DolphinView::sortOrder() const
445 return m_proxyModel
->sortOrder();
448 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
450 if (sortFoldersFirst() != foldersFirst
) {
451 updateSortFoldersFirst(foldersFirst
);
455 bool DolphinView::sortFoldersFirst() const
457 return m_proxyModel
->sortFoldersFirst();
460 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
462 const KUrl viewPropsUrl
= viewPropertiesUrl();
463 ViewProperties
props(viewPropsUrl
);
464 props
.setAdditionalInfo(info
);
465 m_fileItemDelegate
->setShowInformation(info
);
467 emit
additionalInfoChanged();
469 if (itemView() != m_detailsView
) {
470 // the details view requires no reloading of the directory, as it maps
471 // the file item delegate info to its columns internally
472 loadDirectory(viewPropsUrl
);
476 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
478 return m_fileItemDelegate
->showInformation();
481 void DolphinView::reload()
484 loadDirectory(url(), true);
487 void DolphinView::refresh()
489 m_ignoreViewProperties
= false;
491 const bool oldActivationState
= m_active
;
492 const int oldZoomLevel
= m_controller
->zoomLevel();
496 applyViewProperties(m_controller
->url());
499 setActive(oldActivationState
);
500 updateZoomLevel(oldZoomLevel
);
503 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
505 if (m_controller
->url() == url
) {
509 m_previewGenerator
->cancelPreviews();
510 m_controller
->setUrl(url
); // emits urlChanged, which we forward
512 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
513 applyViewProperties(rootUrl
);
514 loadDirectory(rootUrl
);
515 if (itemView() == m_columnView
) {
516 m_columnView
->setRootUrl(rootUrl
);
517 m_columnView
->showColumn(url
);
520 applyViewProperties(url
);
524 emit
startedPathLoading(url
);
527 void DolphinView::setNameFilter(const QString
& nameFilter
)
529 m_proxyModel
->setFilterRegExp(nameFilter
);
531 if (isColumnViewActive()) {
532 // adjusting the directory lister is not enough in the case of the
533 // column view, as each column has its own directory lister internally...
534 m_columnView
->setNameFilter(nameFilter
);
538 void DolphinView::calculateItemCount(int& fileCount
,
540 KIO::filesize_t
& totalFileSize
) const
542 foreach (const KFileItem
& item
, m_dirLister
->items()) {
547 totalFileSize
+= item
.size();
552 QString
DolphinView::statusBarText() const
557 KIO::filesize_t totalFileSize
= 0;
559 if (hasSelection()) {
560 // give a summary of the status of the selected files
561 const KFileItemList list
= selectedItems();
562 if (list
.isEmpty()) {
563 // when an item is triggered, it is temporary selected but selectedItems()
564 // will return an empty list
568 KFileItemList::const_iterator it
= list
.begin();
569 const KFileItemList::const_iterator end
= list
.end();
571 const KFileItem
& item
= *it
;
576 totalFileSize
+= item
.size();
581 if (folderCount
+ fileCount
== 1) {
582 // if only one item is selected, show the filename
583 const QString name
= list
.first().name();
584 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
585 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
586 name
, KIO::convertSize(totalFileSize
));
588 // at least 2 items are selected
589 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
590 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
591 if ((folderCount
> 0) && (fileCount
> 0)) {
592 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
593 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
594 } else if (fileCount
> 0) {
595 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
597 Q_ASSERT(folderCount
> 0);
602 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
603 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
604 fileCount
, folderCount
,
605 totalFileSize
, true);
611 void DolphinView::setUrl(const KUrl
& url
)
613 m_newFileNames
.clear();
614 updateView(url
, KUrl());
617 void DolphinView::changeSelection(const KFileItemList
& selection
)
620 if (selection
.isEmpty()) {
623 const KUrl
& baseUrl
= url();
625 QItemSelection newSelection
;
626 foreach(const KFileItem
& item
, selection
) {
627 url
= item
.url().upUrl();
628 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
629 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
630 newSelection
.select(index
, index
);
633 itemView()->selectionModel()->select(newSelection
,
634 QItemSelectionModel::ClearAndSelect
635 | QItemSelectionModel::Current
);
638 void DolphinView::renameSelectedItems()
640 KFileItemList items
= selectedItems();
641 const int itemCount
= items
.count();
647 // More than one item has been selected for renaming. Open
648 // a rename dialog and rename all items afterwards.
649 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
650 if (dialog
->exec() == QDialog::Rejected
) {
655 const QString newName
= dialog
->newName();
656 if (newName
.isEmpty()) {
657 emit
errorMessage(dialog
->errorString());
663 // the selection would be invalid after renaming the items, so just clear
667 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
668 // as one operation instead of n rename operations like it is done now...
669 Q_ASSERT(newName
.contains('#'));
671 // currently the items are sorted by the selection order, resort
672 // them by the file name
673 qSort(items
.begin(), items
.end(), lessThan
);
675 // iterate through all selected items and rename them...
677 foreach (const KFileItem
& item
, items
) {
678 const KUrl
& oldUrl
= item
.url();
680 number
.setNum(index
++);
682 QString name
= newName
;
683 name
.replace('#', number
);
685 if (oldUrl
.fileName() != name
) {
686 KUrl newUrl
= oldUrl
;
687 newUrl
.setFileName(name
);
688 KonqOperations::rename(this, oldUrl
, newUrl
);
691 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
692 Q_ASSERT(itemCount
== 1);
694 if (isColumnViewActive()) {
695 m_columnView
->editItem(items
.first());
697 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
698 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
699 itemView()->edit(proxyIndex
);
702 Q_ASSERT(itemCount
== 1);
704 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
705 if (dialog
->exec() == QDialog::Rejected
) {
710 const QString newName
= dialog
->newName();
711 if (newName
.isEmpty()) {
712 emit
errorMessage(dialog
->errorString());
718 const KUrl
& oldUrl
= items
.first().url();
719 KUrl newUrl
= oldUrl
;
720 newUrl
.setFileName(newName
);
721 KonqOperations::rename(this, oldUrl
, newUrl
);
724 // assure that the current index remains visible when KDirLister
725 // will notify the view about changed items
726 m_assureVisibleCurrentIndex
= true;
729 void DolphinView::trashSelectedItems()
731 const KUrl::List list
= simplifiedSelectedUrls();
732 KonqOperations::del(this, KonqOperations::TRASH
, list
);
735 void DolphinView::deleteSelectedItems()
737 const KUrl::List list
= simplifiedSelectedUrls();
738 const bool del
= KonqOperations::askDeleteConfirmation(list
,
740 KonqOperations::DEFAULT_CONFIRMATION
,
744 KIO::Job
* job
= KIO::del(list
);
745 connect(job
, SIGNAL(result(KJob
*)),
746 this, SLOT(slotDeleteFileFinished(KJob
*)));
750 void DolphinView::cutSelectedItems()
752 QMimeData
* mimeData
= selectionMimeData();
753 KonqMimeData::addIsCutSelection(mimeData
, true);
754 QApplication::clipboard()->setMimeData(mimeData
);
757 void DolphinView::copySelectedItems()
759 QMimeData
* mimeData
= selectionMimeData();
760 QApplication::clipboard()->setMimeData(mimeData
);
763 void DolphinView::paste()
768 void DolphinView::pasteIntoFolder()
770 const KFileItemList items
= selectedItems();
771 if ((items
.count() == 1) && items
.first().isDir()) {
772 pasteToUrl(items
.first().url());
776 void DolphinView::setShowPreview(bool show
)
778 if (m_showPreview
== show
) {
782 const KUrl viewPropsUrl
= viewPropertiesUrl();
783 ViewProperties
props(viewPropsUrl
);
784 props
.setShowPreview(show
);
786 m_showPreview
= show
;
787 m_previewGenerator
->setPreviewShown(show
);
789 const int oldZoomLevel
= m_controller
->zoomLevel();
790 emit
showPreviewChanged();
792 // Enabling or disabling the preview might change the icon size of the view.
793 // As the view does not emit a signal when the icon size has been changed,
794 // the used zoom level of the controller must be adjusted manually:
795 updateZoomLevel(oldZoomLevel
);
797 loadDirectory(viewPropsUrl
);
800 void DolphinView::setShowHiddenFiles(bool show
)
802 if (m_dirLister
->showingDotFiles() == show
) {
806 const KUrl viewPropsUrl
= viewPropertiesUrl();
807 ViewProperties
props(viewPropsUrl
);
808 props
.setShowHiddenFiles(show
);
810 m_dirLister
->setShowingDotFiles(show
);
811 emit
showHiddenFilesChanged();
813 loadDirectory(viewPropsUrl
);
816 void DolphinView::setCategorizedSorting(bool categorized
)
818 if (categorized
== categorizedSorting()) {
822 // setCategorizedSorting(true) may only get invoked
823 // if the view supports categorized sorting
824 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
826 ViewProperties
props(viewPropertiesUrl());
827 props
.setCategorizedSorting(categorized
);
830 m_storedCategorizedSorting
= categorized
;
831 m_proxyModel
->setCategorizedModel(categorized
);
833 emit
categorizedSortingChanged();
836 void DolphinView::toggleSortOrder()
838 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
839 Qt::DescendingOrder
:
844 void DolphinView::toggleSortFoldersFirst()
846 setSortFoldersFirst(!sortFoldersFirst());
849 void DolphinView::toggleAdditionalInfo(QAction
* action
)
851 const KFileItemDelegate::Information info
=
852 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
854 KFileItemDelegate::InformationList list
= additionalInfo();
856 const bool show
= action
->isChecked();
858 const int index
= list
.indexOf(info
);
859 const bool containsInfo
= (index
>= 0);
860 if (show
&& !containsInfo
) {
862 setAdditionalInfo(list
);
863 } else if (!show
&& containsInfo
) {
864 list
.removeAt(index
);
865 setAdditionalInfo(list
);
866 Q_ASSERT(list
.indexOf(info
) < 0);
870 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
872 QWidget::mouseReleaseEvent(event
);
876 void DolphinView::wheelEvent(QWheelEvent
* event
)
878 if (event
->modifiers() & Qt::ControlModifier
) {
879 const int delta
= event
->delta();
880 const int level
= zoomLevel();
882 setZoomLevel(level
+ 1);
883 } else if (delta
< 0) {
884 setZoomLevel(level
- 1);
890 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
892 switch (event
->type()) {
893 case QEvent::FocusIn
:
894 if (watched
== itemView()) {
895 m_controller
->requestActivation();
899 case QEvent::MouseButtonPress
:
900 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
901 // Listening to a mousebutton press event to delete expanded views is a
902 // workaround, as it seems impossible for the FolderExpander to know when
903 // a dragging outside a view has been finished. However it works quite well:
904 // A mousebutton press event indicates that a drag operation must be
906 m_expandedDragSource
->deleteLater();
907 m_expandedDragSource
= 0;
911 case QEvent::DragEnter
:
912 if (watched
== itemView()->viewport()) {
917 case QEvent::KeyPress
:
918 if (watched
== itemView()) {
919 if (m_toolTipManager
!= 0) {
920 m_toolTipManager
->hideTip();
923 // clear the selection when Escape has been pressed
924 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
925 if (keyEvent
->key() == Qt::Key_Escape
) {
935 return QWidget::eventFilter(watched
, event
);
938 void DolphinView::activate()
943 void DolphinView::triggerItem(const KFileItem
& item
)
945 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
946 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
947 // items are selected by the user, hence don't trigger the
948 // item specified by 'index'
952 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
953 if (item
.isNull() || m_isContextMenuOpen
) {
957 if (m_toolTipManager
!= 0) {
958 m_toolTipManager
->hideTip();
960 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
963 void DolphinView::emitDelayedSelectionChangedSignal()
965 // Invoke emitSelectionChangedSignal() with a delay of 300 ms. This assures
966 // that fast selection changes don't result in expensive operations to
967 // collect all file items for the signal (see DolphinView::selectedItems()).
968 m_selectionChangedTimer
->start();
971 void DolphinView::emitSelectionChangedSignal()
973 emit
selectionChanged(DolphinView::selectedItems());
976 void DolphinView::openContextMenu(const QPoint
& pos
,
977 const QList
<QAction
*>& customActions
)
980 if (isColumnViewActive()) {
981 item
= m_columnView
->itemAt(pos
);
983 const QModelIndex index
= itemView()->indexAt(pos
);
984 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
985 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
986 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
990 if (m_toolTipManager
!= 0) {
991 m_toolTipManager
->hideTip();
994 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
995 emit
requestContextMenu(item
, url(), customActions
);
996 m_isContextMenuOpen
= false;
999 void DolphinView::dropUrls(const KFileItem
& destItem
,
1000 const KUrl
& destPath
,
1003 addNewFileNames(event
->mimeData());
1004 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
1007 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1009 ViewProperties
props(viewPropertiesUrl());
1010 props
.setSorting(sorting
);
1012 m_proxyModel
->setSorting(sorting
);
1014 emit
sortingChanged(sorting
);
1017 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1019 ViewProperties
props(viewPropertiesUrl());
1020 props
.setSortOrder(order
);
1022 m_proxyModel
->setSortOrder(order
);
1024 emit
sortOrderChanged(order
);
1027 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1029 ViewProperties
props(viewPropertiesUrl());
1030 props
.setSortFoldersFirst(foldersFirst
);
1032 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1034 emit
sortFoldersFirstChanged(foldersFirst
);
1037 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1039 ViewProperties
props(viewPropertiesUrl());
1040 props
.setAdditionalInfo(info
);
1043 m_fileItemDelegate
->setShowInformation(info
);
1045 emit
additionalInfoChanged();
1048 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1050 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1051 (m_mode
== DolphinView::IconsView
);
1053 QAction
* showSizeInfo
= collection
->action("show_size_info");
1054 QAction
* showDateInfo
= collection
->action("show_date_info");
1055 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1056 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1057 QAction
* showGroupInfo
= collection
->action("show_group_info");
1058 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1060 showSizeInfo
->setChecked(false);
1061 showDateInfo
->setChecked(false);
1062 showPermissionsInfo
->setChecked(false);
1063 showOwnerInfo
->setChecked(false);
1064 showGroupInfo
->setChecked(false);
1065 showMimeInfo
->setChecked(false);
1067 showSizeInfo
->setEnabled(enable
);
1068 showDateInfo
->setEnabled(enable
);
1069 showPermissionsInfo
->setEnabled(enable
);
1070 showOwnerInfo
->setEnabled(enable
);
1071 showGroupInfo
->setEnabled(enable
);
1072 showMimeInfo
->setEnabled(enable
);
1074 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1076 case KFileItemDelegate::Size
:
1077 showSizeInfo
->setChecked(true);
1079 case KFileItemDelegate::ModificationTime
:
1080 showDateInfo
->setChecked(true);
1082 case KFileItemDelegate::Permissions
:
1083 showPermissionsInfo
->setChecked(true);
1085 case KFileItemDelegate::Owner
:
1086 showOwnerInfo
->setChecked(true);
1088 case KFileItemDelegate::OwnerAndGroup
:
1089 showGroupInfo
->setChecked(true);
1091 case KFileItemDelegate::FriendlyMimeType
:
1092 showMimeInfo
->setChecked(true);
1100 QPair
<bool, QString
> DolphinView::pasteInfo() const
1102 return KonqOperations::pasteInfo(url());
1105 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1107 m_tabsForFiles
= tabsForFiles
;
1110 bool DolphinView::isTabsForFilesEnabled() const
1112 return m_tabsForFiles
;
1115 void DolphinView::activateItem(const KUrl
& url
)
1117 m_activeItemUrl
= url
;
1120 bool DolphinView::itemsExpandable() const
1122 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1125 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1130 if (DragAndDropHelper::instance().isDragSource(view
)) {
1131 // We must store for later deletion.
1132 if (m_expandedDragSource
!= 0) {
1133 // The old stored view is obviously not the drag source anymore.
1134 m_expandedDragSource
->deleteLater();
1135 m_expandedDragSource
= 0;
1138 m_expandedDragSource
= view
;
1141 view
->deleteLater();
1145 void DolphinView::observeCreatedItem(const KUrl
& url
)
1147 m_createdItemUrl
= url
;
1148 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1149 this, SLOT(selectAndScrollToCreatedItem()));
1152 void DolphinView::selectAndScrollToCreatedItem()
1154 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1155 if (dirIndex
.isValid()) {
1156 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1157 itemView()->setCurrentIndex(proxyIndex
);
1160 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1161 this, SLOT(selectAndScrollToCreatedItem()));
1162 m_createdItemUrl
= KUrl();
1165 void DolphinView::restoreSelection()
1167 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1168 changeSelection(m_selectedItems
);
1171 void DolphinView::emitContentsMoved()
1173 // only emit the contents moved signal if:
1174 // - no directory loading is ongoing (this would reset the contents position
1175 // always to (0, 0))
1176 // - if the Column View is active: the column view does an automatic
1177 // positioning during the loading operation, which must be remembered
1178 if (!m_loadingDirectory
|| isColumnViewActive()) {
1179 const QPoint
pos(contentsPosition());
1180 emit
contentsMoved(pos
.x(), pos
.y());
1184 void DolphinView::showHoverInformation(const KFileItem
& item
)
1186 emit
requestItemInfo(item
);
1189 void DolphinView::clearHoverInformation()
1191 emit
requestItemInfo(KFileItem());
1194 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1196 if (job
->error() == 0) {
1197 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1198 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1199 emit
errorMessage(job
->errorString());
1203 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1205 emit
requestUrlChange(url
);
1206 m_controller
->setUrl(url
);
1209 void DolphinView::slotDirListerCompleted()
1211 if (!m_activeItemUrl
.isEmpty()) {
1212 // assure that the current item remains visible
1213 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_activeItemUrl
);
1214 if (dirIndex
.isValid()) {
1215 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1216 QAbstractItemView
* view
= itemView();
1217 const bool clearSelection
= !hasSelection();
1218 view
->setCurrentIndex(proxyIndex
);
1219 if (clearSelection
) {
1220 view
->clearSelection();
1222 m_activeItemUrl
.clear();
1226 if (!m_newFileNames
.isEmpty()) {
1227 // select all newly added items created by a paste operation or
1228 // a drag & drop operation
1229 const int rowCount
= m_proxyModel
->rowCount();
1230 QItemSelection selection
;
1231 for (int row
= 0; row
< rowCount
; ++row
) {
1232 const QModelIndex proxyIndex
= m_proxyModel
->index(row
, 0);
1233 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(proxyIndex
);
1234 const KUrl url
= m_dolphinModel
->itemForIndex(dirIndex
).url();
1235 if (m_newFileNames
.contains(url
.fileName())) {
1236 selection
.merge(QItemSelection(proxyIndex
, proxyIndex
), QItemSelectionModel::Select
);
1239 itemView()->selectionModel()->select(selection
, QItemSelectionModel::Select
);
1241 m_newFileNames
.clear();
1245 void DolphinView::slotRefreshItems()
1247 if (m_assureVisibleCurrentIndex
) {
1248 m_assureVisibleCurrentIndex
= false;
1249 itemView()->scrollTo(itemView()->currentIndex());
1253 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1255 if (!url
.isValid()) {
1256 const QString
location(url
.pathOrUrl());
1257 if (location
.isEmpty()) {
1258 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1260 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1265 m_loadingDirectory
= true;
1268 m_selectedItems
= selectedItems();
1269 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1272 m_dirLister
->stop();
1273 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1275 if (isColumnViewActive()) {
1276 // adjusting the directory lister is not enough in the case of the
1277 // column view, as each column has its own directory lister internally...
1279 m_columnView
->reload();
1281 m_columnView
->showColumn(url
);
1286 KUrl
DolphinView::viewPropertiesUrl() const
1288 if (isColumnViewActive()) {
1289 return m_columnView
->rootUrl();
1295 void DolphinView::applyViewProperties(const KUrl
& url
)
1297 if (m_ignoreViewProperties
) {
1301 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1302 // The column view is active, hence don't apply the view properties
1303 // of sub directories (represented by columns) to the view. The
1304 // view always represents the properties of the first column.
1308 const ViewProperties
props(url
);
1310 const Mode mode
= props
.viewMode();
1311 if (m_mode
!= mode
) {
1312 const int oldZoomLevel
= m_controller
->zoomLevel();
1318 updateZoomLevel(oldZoomLevel
);
1320 if (itemView() == 0) {
1323 Q_ASSERT(itemView() != 0);
1324 Q_ASSERT(m_fileItemDelegate
!= 0);
1326 const bool showHiddenFiles
= props
.showHiddenFiles();
1327 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1328 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1329 emit
showHiddenFilesChanged();
1332 m_storedCategorizedSorting
= props
.categorizedSorting();
1333 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1334 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1335 m_proxyModel
->setCategorizedModel(categorized
);
1336 emit
categorizedSortingChanged();
1339 const DolphinView::Sorting sorting
= props
.sorting();
1340 if (sorting
!= m_proxyModel
->sorting()) {
1341 m_proxyModel
->setSorting(sorting
);
1342 emit
sortingChanged(sorting
);
1345 const Qt::SortOrder sortOrder
= props
.sortOrder();
1346 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1347 m_proxyModel
->setSortOrder(sortOrder
);
1348 emit
sortOrderChanged(sortOrder
);
1351 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1352 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1353 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1354 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1357 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1358 if (info
!= m_fileItemDelegate
->showInformation()) {
1359 m_fileItemDelegate
->setShowInformation(info
);
1360 emit
additionalInfoChanged();
1363 const bool showPreview
= props
.showPreview();
1364 if (showPreview
!= m_showPreview
) {
1365 m_showPreview
= showPreview
;
1366 m_previewGenerator
->setPreviewShown(showPreview
);
1368 const int oldZoomLevel
= m_controller
->zoomLevel();
1369 emit
showPreviewChanged();
1371 // Enabling or disabling the preview might change the icon size of the view.
1372 // As the view does not emit a signal when the icon size has been changed,
1373 // the used zoom level of the controller must be adjusted manually:
1374 updateZoomLevel(oldZoomLevel
);
1377 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1378 // During the lifetime of a DolphinView instance the global view properties
1379 // should not be changed. This allows e. g. to split a view and use different
1380 // view properties for each view.
1381 m_ignoreViewProperties
= true;
1385 void DolphinView::createView()
1388 Q_ASSERT(m_iconsView
== 0);
1389 Q_ASSERT(m_detailsView
== 0);
1390 Q_ASSERT(m_columnView
== 0);
1392 QAbstractItemView
* view
= 0;
1395 m_iconsView
= new DolphinIconsView(this, m_controller
);
1401 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1402 view
= m_detailsView
;
1406 m_columnView
= new DolphinColumnView(this, m_controller
);
1407 view
= m_columnView
;
1411 Q_ASSERT(view
!= 0);
1412 view
->installEventFilter(this);
1413 view
->viewport()->installEventFilter(this);
1414 setFocusProxy(view
);
1416 if (m_mode
!= ColumnView
) {
1417 // Give the view the ability to auto-expand its directories on hovering
1418 // (the column view takes care about this itself). If the details view
1419 // uses expandable folders, the auto-expanding should be used always.
1420 DolphinSettings
& settings
= DolphinSettings::instance();
1421 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1422 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1424 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1425 folderExpander
->setEnabled(enabled
);
1426 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1427 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1430 // Listen out for requests to delete the current column.
1431 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1432 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1435 m_controller
->setItemView(view
);
1437 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1438 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1439 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1440 view
->setItemDelegate(m_fileItemDelegate
);
1442 view
->setModel(m_proxyModel
);
1443 if (m_selectionModel
!= 0) {
1444 view
->setSelectionModel(m_selectionModel
);
1446 m_selectionModel
= view
->selectionModel();
1449 m_selectionChangedTimer
= new QTimer(this);
1450 m_selectionChangedTimer
->setSingleShot(true);
1451 m_selectionChangedTimer
->setInterval(300);
1452 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
1453 this, SLOT(emitSelectionChangedSignal()));
1455 // reparent the selection model, as it should not be deleted
1456 // when deleting the model
1457 m_selectionModel
->setParent(this);
1459 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1461 m_previewGenerator
= new KFilePreviewGenerator(view
);
1462 m_previewGenerator
->setPreviewShown(m_showPreview
);
1464 new RevisionControlObserver(view
);
1466 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1467 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1468 connect(m_controller
, SIGNAL(hideToolTip()),
1469 m_toolTipManager
, SLOT(hideTip()));
1472 m_topLayout
->insertWidget(1, view
);
1474 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1475 this, SLOT(emitDelayedSelectionChangedSignal()));
1476 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1477 this, SLOT(emitContentsMoved()));
1478 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1479 this, SLOT(emitContentsMoved()));
1482 void DolphinView::deleteView()
1484 QAbstractItemView
* view
= itemView();
1486 // It's important to set the keyboard focus to the parent
1487 // before deleting the view: Otherwise when having a split
1488 // view the other view will get the focus and will request
1489 // an activation (see DolphinView::eventFilter()).
1493 m_topLayout
->removeWidget(view
);
1496 // m_previewGenerator's parent is not always destroyed, and we
1497 // don't want two active at once - manually delete.
1498 delete m_previewGenerator
;
1499 m_previewGenerator
= 0;
1502 m_controller
->disconnect(view
);
1505 deleteWhenNotDragSource(view
);
1511 m_fileItemDelegate
= 0;
1512 m_toolTipManager
= 0;
1516 QAbstractItemView
* DolphinView::itemView() const
1518 if (m_detailsView
!= 0) {
1519 return m_detailsView
;
1520 } else if (m_columnView
!= 0) {
1521 return m_columnView
;
1527 void DolphinView::pasteToUrl(const KUrl
& url
)
1529 addNewFileNames(QApplication::clipboard()->mimeData());
1530 KonqOperations::doPaste(this, url
);
1533 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1535 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1536 if (oldZoomLevel
!= newZoomLevel
) {
1537 m_controller
->setZoomLevel(newZoomLevel
);
1538 emit
zoomLevelChanged(newZoomLevel
);
1542 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1544 KUrl::List list
= selectedUrls();
1545 if (itemsExpandable() ) {
1546 list
= KDirModel::simplifiedUrlList(list
);
1551 QMimeData
* DolphinView::selectionMimeData() const
1553 if (isColumnViewActive()) {
1554 return m_columnView
->selectionMimeData();
1557 const QAbstractItemView
* view
= itemView();
1558 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1559 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1560 return m_dolphinModel
->mimeData(selection
.indexes());
1563 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1565 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1566 foreach (const KUrl
& url
, urls
) {
1567 m_newFileNames
.insert(url
.fileName());
1571 #include "dolphinview.moc"