1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz <peter.penz@gmx.at> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphinview.h"
23 #include <QApplication>
26 #include <QItemSelection>
31 #include <kactioncollection.h>
32 #include <kcolorscheme.h>
33 #include <kdirlister.h>
34 #include <kfilepreviewgenerator.h>
35 #include <kiconeffect.h>
36 #include <kfileitem.h>
38 #include <kio/deletejob.h>
39 #include <kio/netaccess.h>
40 #include <kio/previewjob.h>
43 #include <kmessagebox.h>
44 #include <kmimetyperesolver.h>
45 #include <konq_fileitemcapabilities.h>
46 #include <konq_operations.h>
47 #include <konqmimedata.h>
48 #include <kstringhandler.h>
49 #include <ktoggleaction.h>
52 #include "dolphinmodel.h"
53 #include "dolphincolumnview.h"
54 #include "dolphincontroller.h"
55 #include "dolphindetailsview.h"
56 #include "dolphinfileitemdelegate.h"
57 #include "dolphinnewmenuobserver.h"
58 #include "dolphinsortfilterproxymodel.h"
59 #include "dolphin_detailsmodesettings.h"
60 #include "dolphiniconsview.h"
61 #include "dolphin_generalsettings.h"
62 #include "draganddrophelper.h"
63 #include "folderexpander.h"
64 #include "renamedialog.h"
65 #include "tooltips/tooltipmanager.h"
66 #include "settings/dolphinsettings.h"
67 #include "viewproperties.h"
68 #include "zoomlevelinfo.h"
71 * Helper function for sorting items with qSort() in
72 * DolphinView::renameSelectedItems().
74 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
76 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
79 DolphinView::DolphinView(QWidget
* parent
,
81 KDirLister
* dirLister
,
82 DolphinModel
* dolphinModel
,
83 DolphinSortFilterProxyModel
* proxyModel
) :
87 m_loadingDirectory(false),
88 m_storedCategorizedSorting(false),
89 m_tabsForFiles(false),
90 m_isContextMenuOpen(false),
91 m_ignoreViewProperties(false),
92 m_assureVisibleCurrentIndex(false),
93 m_mode(DolphinView::IconsView
),
99 m_fileItemDelegate(0),
101 m_selectionChangedTimer(0),
102 m_dolphinModel(dolphinModel
),
103 m_dirLister(dirLister
),
104 m_proxyModel(proxyModel
),
105 m_previewGenerator(0),
112 m_expandedDragSource(0)
114 m_topLayout
= new QVBoxLayout(this);
115 m_topLayout
->setSpacing(0);
116 m_topLayout
->setMargin(0);
118 m_controller
= new DolphinController(this);
119 m_controller
->setUrl(url
);
121 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
122 this, SIGNAL(urlChanged(const KUrl
&)));
123 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
124 this, SLOT(slotRequestUrlChange(const KUrl
&)));
126 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
127 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
128 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
129 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
130 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
131 this, SLOT(updateSorting(DolphinView::Sorting
)));
132 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
133 this, SLOT(updateSortOrder(Qt::SortOrder
)));
134 connect(m_controller
, SIGNAL(sortFoldersFirstChanged(bool)),
135 this, SLOT(updateSortFoldersFirst(bool)));
136 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
137 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
138 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
139 this, SLOT(triggerItem(const KFileItem
&)));
140 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
141 this, SIGNAL(tabRequested(const KUrl
&)));
142 connect(m_controller
, SIGNAL(activated()),
143 this, SLOT(activate()));
144 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
145 this, SLOT(showHoverInformation(const KFileItem
&)));
146 connect(m_controller
, SIGNAL(viewportEntered()),
147 this, SLOT(clearHoverInformation()));
149 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
150 this, SIGNAL(redirection(KUrl
, KUrl
)));
151 connect(m_dirLister
, SIGNAL(completed()),
152 this, SLOT(slotDirListerCompleted()));
153 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
154 this, SLOT(slotRefreshItems()));
156 // When a new item has been created by the "Create New..." menu, the item should
157 // get selected and it must be assured that the item will get visible. As the
158 // creation is done asynchronously, several signals must be checked:
159 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
160 this, SLOT(observeCreatedItem(const KUrl
&)));
162 applyViewProperties(url
);
163 m_topLayout
->addWidget(itemView());
166 DolphinView::~DolphinView()
168 delete m_expandedDragSource
;
169 m_expandedDragSource
= 0;
172 const KUrl
& DolphinView::url() const
174 return m_controller
->url();
177 KUrl
DolphinView::rootUrl() const
179 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
182 void DolphinView::setActive(bool active
)
184 if (active
== m_active
) {
190 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
192 emitSelectionChangedSignal();
197 QWidget
* viewport
= itemView()->viewport();
199 palette
.setColor(viewport
->backgroundRole(), color
);
200 viewport
->setPalette(palette
);
205 itemView()->setFocus();
209 m_controller
->indicateActivationChange(active
);
212 bool DolphinView::isActive() const
217 void DolphinView::setMode(Mode mode
)
219 if (mode
== m_mode
) {
220 return; // the wished mode is already set
223 const int oldZoomLevel
= m_controller
->zoomLevel();
228 const KUrl viewPropsUrl
= viewPropertiesUrl();
229 ViewProperties
props(viewPropsUrl
);
230 props
.setViewMode(m_mode
);
233 // the file item delegate has been recreated, apply the current
234 // additional information manually
235 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
236 m_fileItemDelegate
->setShowInformation(infoList
);
237 emit
additionalInfoChanged();
239 // Not all view modes support categorized sorting. Adjust the sorting model
240 // if changing the view mode results in a change of the categorized sorting
242 m_storedCategorizedSorting
= props
.categorizedSorting();
243 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
244 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
245 m_proxyModel
->setCategorizedModel(categorized
);
246 emit
categorizedSortingChanged();
251 updateZoomLevel(oldZoomLevel
);
253 loadDirectory(viewPropsUrl
);
257 DolphinView::Mode
DolphinView::mode() const
262 bool DolphinView::showPreview() const
264 return m_showPreview
;
267 bool DolphinView::showHiddenFiles() const
269 return m_dirLister
->showingDotFiles();
272 bool DolphinView::categorizedSorting() const
274 // If all view modes would support categorized sorting, returning
275 // m_proxyModel->isCategorizedModel() would be the way to go. As
276 // currently only the icons view supports caterized sorting, we remember
277 // the stored view properties state in m_storedCategorizedSorting and
278 // return this state. The application takes care to disable the corresponding
279 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
280 // that this setting is not applied to the current view mode.
281 return m_storedCategorizedSorting
;
284 bool DolphinView::supportsCategorizedSorting() const
286 return m_iconsView
!= 0;
289 void DolphinView::selectAll()
291 QAbstractItemView
* view
= itemView();
292 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
293 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
294 // selection instead of selecting all items. This is bypassed for KDE 4.0
295 // by invoking clearSelection() first.
296 view
->clearSelection();
300 void DolphinView::invertSelection()
302 if (isColumnViewActive()) {
303 // QAbstractItemView does not offer a virtual method invertSelection()
304 // as counterpart to QAbstractItemView::selectAll(). This makes it
305 // necessary to delegate the inverting of the selection to the
306 // column view, as only the selection of the active column should
308 m_columnView
->invertSelection();
310 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
311 const QAbstractItemModel
* itemModel
= selectionModel
->model();
313 const QModelIndex topLeft
= itemModel
->index(0, 0);
314 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
315 itemModel
->columnCount() - 1);
317 const QItemSelection
selection(topLeft
, bottomRight
);
318 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
322 bool DolphinView::hasSelection() const
324 return itemView()->selectionModel()->hasSelection();
327 void DolphinView::clearSelection()
329 QItemSelectionModel
* selModel
= itemView()->selectionModel();
330 const QModelIndex currentIndex
= selModel
->currentIndex();
331 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
332 QItemSelectionModel::Clear
);
333 m_selectedItems
.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()->selectedIndexes().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();
613 updateView(url
, KUrl());
616 void DolphinView::changeSelection(const KFileItemList
& selection
)
619 if (selection
.isEmpty()) {
622 const KUrl
& baseUrl
= url();
624 QItemSelection newSelection
;
625 foreach(const KFileItem
& item
, selection
) {
626 url
= item
.url().upUrl();
627 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
628 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
629 newSelection
.select(index
, index
);
632 itemView()->selectionModel()->select(newSelection
,
633 QItemSelectionModel::ClearAndSelect
634 | QItemSelectionModel::Current
);
637 void DolphinView::renameSelectedItems()
639 KFileItemList items
= selectedItems();
640 const int itemCount
= items
.count();
646 // More than one item has been selected for renaming. Open
647 // a rename dialog and rename all items afterwards.
648 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
649 if (dialog
->exec() == QDialog::Rejected
) {
654 const QString newName
= dialog
->newName();
655 if (newName
.isEmpty()) {
656 emit
errorMessage(dialog
->errorString());
662 // the selection would be invalid after renaming the items, so just clear
666 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
667 // as one operation instead of n rename operations like it is done now...
668 Q_ASSERT(newName
.contains('#'));
670 // currently the items are sorted by the selection order, resort
671 // them by the file name
672 qSort(items
.begin(), items
.end(), lessThan
);
674 // iterate through all selected items and rename them...
676 foreach (const KFileItem
& item
, items
) {
677 const KUrl
& oldUrl
= item
.url();
679 number
.setNum(index
++);
681 QString name
= newName
;
682 name
.replace('#', number
);
684 if (oldUrl
.fileName() != name
) {
685 KUrl newUrl
= oldUrl
;
686 newUrl
.setFileName(name
);
687 KonqOperations::rename(this, oldUrl
, newUrl
);
690 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
691 Q_ASSERT(itemCount
== 1);
693 if (isColumnViewActive()) {
694 m_columnView
->editItem(items
.first());
696 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
697 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
698 itemView()->edit(proxyIndex
);
701 Q_ASSERT(itemCount
== 1);
703 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
704 if (dialog
->exec() == QDialog::Rejected
) {
709 const QString newName
= dialog
->newName();
710 if (newName
.isEmpty()) {
711 emit
errorMessage(dialog
->errorString());
717 const KUrl
& oldUrl
= items
.first().url();
718 KUrl newUrl
= oldUrl
;
719 newUrl
.setFileName(newName
);
720 KonqOperations::rename(this, oldUrl
, newUrl
);
723 // assure that the current index remains visible when KDirLister
724 // will notify the view about changed items
725 m_assureVisibleCurrentIndex
= true;
728 void DolphinView::trashSelectedItems()
730 const KUrl::List list
= simplifiedSelectedUrls();
731 KonqOperations::del(this, KonqOperations::TRASH
, list
);
734 void DolphinView::deleteSelectedItems()
736 const KUrl::List list
= simplifiedSelectedUrls();
737 const bool del
= KonqOperations::askDeleteConfirmation(list
,
739 KonqOperations::DEFAULT_CONFIRMATION
,
743 KIO::Job
* job
= KIO::del(list
);
744 connect(job
, SIGNAL(result(KJob
*)),
745 this, SLOT(slotDeleteFileFinished(KJob
*)));
749 void DolphinView::cutSelectedItems()
751 QMimeData
* mimeData
= selectionMimeData();
752 KonqMimeData::addIsCutSelection(mimeData
, true);
753 QApplication::clipboard()->setMimeData(mimeData
);
756 void DolphinView::copySelectedItems()
758 QMimeData
* mimeData
= selectionMimeData();
759 QApplication::clipboard()->setMimeData(mimeData
);
762 void DolphinView::paste()
767 void DolphinView::pasteIntoFolder()
769 const KFileItemList items
= selectedItems();
770 if ((items
.count() == 1) && items
.first().isDir()) {
771 pasteToUrl(items
.first().url());
775 void DolphinView::setShowPreview(bool show
)
777 if (m_showPreview
== show
) {
781 const KUrl viewPropsUrl
= viewPropertiesUrl();
782 ViewProperties
props(viewPropsUrl
);
783 props
.setShowPreview(show
);
785 m_showPreview
= show
;
786 m_previewGenerator
->setPreviewShown(show
);
788 const int oldZoomLevel
= m_controller
->zoomLevel();
789 emit
showPreviewChanged();
791 // Enabling or disabling the preview might change the icon size of the view.
792 // As the view does not emit a signal when the icon size has been changed,
793 // the used zoom level of the controller must be adjusted manually:
794 updateZoomLevel(oldZoomLevel
);
796 loadDirectory(viewPropsUrl
);
799 void DolphinView::setShowHiddenFiles(bool show
)
801 if (m_dirLister
->showingDotFiles() == show
) {
805 const KUrl viewPropsUrl
= viewPropertiesUrl();
806 ViewProperties
props(viewPropsUrl
);
807 props
.setShowHiddenFiles(show
);
809 m_dirLister
->setShowingDotFiles(show
);
810 emit
showHiddenFilesChanged();
812 loadDirectory(viewPropsUrl
);
815 void DolphinView::setCategorizedSorting(bool categorized
)
817 if (categorized
== categorizedSorting()) {
821 // setCategorizedSorting(true) may only get invoked
822 // if the view supports categorized sorting
823 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
825 ViewProperties
props(viewPropertiesUrl());
826 props
.setCategorizedSorting(categorized
);
829 m_storedCategorizedSorting
= categorized
;
830 m_proxyModel
->setCategorizedModel(categorized
);
832 emit
categorizedSortingChanged();
835 void DolphinView::toggleSortOrder()
837 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
838 Qt::DescendingOrder
:
843 void DolphinView::toggleSortFoldersFirst()
845 setSortFoldersFirst(!sortFoldersFirst());
848 void DolphinView::toggleAdditionalInfo(QAction
* action
)
850 const KFileItemDelegate::Information info
=
851 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
853 KFileItemDelegate::InformationList list
= additionalInfo();
855 const bool show
= action
->isChecked();
857 const int index
= list
.indexOf(info
);
858 const bool containsInfo
= (index
>= 0);
859 if (show
&& !containsInfo
) {
861 setAdditionalInfo(list
);
862 } else if (!show
&& containsInfo
) {
863 list
.removeAt(index
);
864 setAdditionalInfo(list
);
865 Q_ASSERT(list
.indexOf(info
) < 0);
869 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
871 QWidget::mouseReleaseEvent(event
);
875 void DolphinView::wheelEvent(QWheelEvent
* event
)
877 if (event
->modifiers() & Qt::ControlModifier
) {
878 const int delta
= event
->delta();
879 const int level
= zoomLevel();
881 setZoomLevel(level
+ 1);
882 } else if (delta
< 0) {
883 setZoomLevel(level
- 1);
889 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
891 switch (event
->type()) {
892 case QEvent::FocusIn
:
893 if (watched
== itemView()) {
894 m_controller
->requestActivation();
898 case QEvent::MouseButtonPress
:
899 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
900 // Listening to a mousebutton press event to delete expanded views is a
901 // workaround, as it seems impossible for the FolderExpander to know when
902 // a dragging outside a view has been finished. However it works quite well:
903 // A mousebutton press event indicates that a drag operation must be
905 m_expandedDragSource
->deleteLater();
906 m_expandedDragSource
= 0;
910 case QEvent::DragEnter
:
911 if (watched
== itemView()->viewport()) {
916 case QEvent::KeyPress
:
917 if (watched
== itemView()) {
918 if (m_toolTipManager
!= 0) {
919 m_toolTipManager
->hideTip();
922 // clear the selection when Escape has been pressed
923 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
924 if (keyEvent
->key() == Qt::Key_Escape
) {
934 return QWidget::eventFilter(watched
, event
);
937 void DolphinView::activate()
942 void DolphinView::triggerItem(const KFileItem
& item
)
944 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
945 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
946 // items are selected by the user, hence don't trigger the
947 // item specified by 'index'
951 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
952 if (item
.isNull() || m_isContextMenuOpen
) {
956 if (m_toolTipManager
!= 0) {
957 m_toolTipManager
->hideTip();
959 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
962 void DolphinView::emitDelayedSelectionChangedSignal()
964 // Invoke emitSelectionChangedSignal() with a delay of 300 ms. This assures
965 // that fast selection changes don't result in expensive operations to
966 // collect all file items for the signal (see DolphinView::selectedItems()).
967 m_selectionChangedTimer
->start();
970 void DolphinView::emitSelectionChangedSignal()
972 emit
selectionChanged(DolphinView::selectedItems());
975 void DolphinView::openContextMenu(const QPoint
& pos
,
976 const QList
<QAction
*>& customActions
)
979 if (isColumnViewActive()) {
980 item
= m_columnView
->itemAt(pos
);
982 const QModelIndex index
= itemView()->indexAt(pos
);
983 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
984 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
985 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
989 if (m_toolTipManager
!= 0) {
990 m_toolTipManager
->hideTip();
993 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
994 emit
requestContextMenu(item
, url(), customActions
);
995 m_isContextMenuOpen
= false;
998 void DolphinView::dropUrls(const KFileItem
& destItem
,
999 const KUrl
& destPath
,
1002 addNewFileNames(event
->mimeData());
1003 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
1006 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
1008 ViewProperties
props(viewPropertiesUrl());
1009 props
.setSorting(sorting
);
1011 m_proxyModel
->setSorting(sorting
);
1013 emit
sortingChanged(sorting
);
1016 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1018 ViewProperties
props(viewPropertiesUrl());
1019 props
.setSortOrder(order
);
1021 m_proxyModel
->setSortOrder(order
);
1023 emit
sortOrderChanged(order
);
1026 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1028 ViewProperties
props(viewPropertiesUrl());
1029 props
.setSortFoldersFirst(foldersFirst
);
1031 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1033 emit
sortFoldersFirstChanged(foldersFirst
);
1036 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1038 ViewProperties
props(viewPropertiesUrl());
1039 props
.setAdditionalInfo(info
);
1042 m_fileItemDelegate
->setShowInformation(info
);
1044 emit
additionalInfoChanged();
1047 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1049 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1050 (m_mode
== DolphinView::IconsView
);
1052 QAction
* showSizeInfo
= collection
->action("show_size_info");
1053 QAction
* showDateInfo
= collection
->action("show_date_info");
1054 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1055 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1056 QAction
* showGroupInfo
= collection
->action("show_group_info");
1057 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1059 showSizeInfo
->setChecked(false);
1060 showDateInfo
->setChecked(false);
1061 showPermissionsInfo
->setChecked(false);
1062 showOwnerInfo
->setChecked(false);
1063 showGroupInfo
->setChecked(false);
1064 showMimeInfo
->setChecked(false);
1066 showSizeInfo
->setEnabled(enable
);
1067 showDateInfo
->setEnabled(enable
);
1068 showPermissionsInfo
->setEnabled(enable
);
1069 showOwnerInfo
->setEnabled(enable
);
1070 showGroupInfo
->setEnabled(enable
);
1071 showMimeInfo
->setEnabled(enable
);
1073 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1075 case KFileItemDelegate::Size
:
1076 showSizeInfo
->setChecked(true);
1078 case KFileItemDelegate::ModificationTime
:
1079 showDateInfo
->setChecked(true);
1081 case KFileItemDelegate::Permissions
:
1082 showPermissionsInfo
->setChecked(true);
1084 case KFileItemDelegate::Owner
:
1085 showOwnerInfo
->setChecked(true);
1087 case KFileItemDelegate::OwnerAndGroup
:
1088 showGroupInfo
->setChecked(true);
1090 case KFileItemDelegate::FriendlyMimeType
:
1091 showMimeInfo
->setChecked(true);
1099 QPair
<bool, QString
> DolphinView::pasteInfo() const
1101 return KonqOperations::pasteInfo(url());
1104 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1106 m_tabsForFiles
= tabsForFiles
;
1109 bool DolphinView::isTabsForFilesEnabled() const
1111 return m_tabsForFiles
;
1114 void DolphinView::activateItem(const KUrl
& url
)
1116 m_activeItemUrl
= url
;
1119 bool DolphinView::itemsExpandable() const
1121 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1124 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1129 if (DragAndDropHelper::instance().isDragSource(view
)) {
1130 // We must store for later deletion.
1131 if (m_expandedDragSource
!= 0) {
1132 // The old stored view is obviously not the drag source anymore.
1133 m_expandedDragSource
->deleteLater();
1134 m_expandedDragSource
= 0;
1137 m_expandedDragSource
= view
;
1140 view
->deleteLater();
1144 void DolphinView::observeCreatedItem(const KUrl
& url
)
1146 m_createdItemUrl
= url
;
1147 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1148 this, SLOT(selectAndScrollToCreatedItem()));
1151 void DolphinView::selectAndScrollToCreatedItem()
1153 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1154 if (dirIndex
.isValid()) {
1155 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1156 itemView()->setCurrentIndex(proxyIndex
);
1159 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1160 this, SLOT(selectAndScrollToCreatedItem()));
1161 m_createdItemUrl
= KUrl();
1164 void DolphinView::restoreSelection()
1166 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1167 changeSelection(m_selectedItems
);
1170 void DolphinView::emitContentsMoved()
1172 // only emit the contents moved signal if:
1173 // - no directory loading is ongoing (this would reset the contents position
1174 // always to (0, 0))
1175 // - if the Column View is active: the column view does an automatic
1176 // positioning during the loading operation, which must be remembered
1177 if (!m_loadingDirectory
|| isColumnViewActive()) {
1178 const QPoint
pos(contentsPosition());
1179 emit
contentsMoved(pos
.x(), pos
.y());
1183 void DolphinView::showHoverInformation(const KFileItem
& item
)
1185 emit
requestItemInfo(item
);
1188 void DolphinView::clearHoverInformation()
1190 emit
requestItemInfo(KFileItem());
1193 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1195 if (job
->error() == 0) {
1196 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1197 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1198 emit
errorMessage(job
->errorString());
1202 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1204 emit
requestUrlChange(url
);
1205 m_controller
->setUrl(url
);
1208 void DolphinView::slotDirListerCompleted()
1210 if (!m_activeItemUrl
.isEmpty()) {
1211 // assure that the current item remains visible
1212 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_activeItemUrl
);
1213 if (dirIndex
.isValid()) {
1214 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1215 QAbstractItemView
* view
= itemView();
1216 const bool clearSelection
= !hasSelection();
1217 view
->setCurrentIndex(proxyIndex
);
1218 if (clearSelection
) {
1219 view
->clearSelection();
1221 m_activeItemUrl
.clear();
1225 if (!m_newFileNames
.isEmpty()) {
1226 // select all newly added items created by a paste operation or
1227 // a drag & drop operation
1228 const int rowCount
= m_proxyModel
->rowCount();
1229 QItemSelection selection
;
1230 for (int row
= 0; row
< rowCount
; ++row
) {
1231 const QModelIndex proxyIndex
= m_proxyModel
->index(row
, 0);
1232 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(proxyIndex
);
1233 const KUrl url
= m_dolphinModel
->itemForIndex(dirIndex
).url();
1234 if (m_newFileNames
.contains(url
.fileName())) {
1235 selection
.merge(QItemSelection(proxyIndex
, proxyIndex
), QItemSelectionModel::Select
);
1238 itemView()->selectionModel()->select(selection
, QItemSelectionModel::Select
);
1240 m_newFileNames
.clear();
1244 void DolphinView::slotRefreshItems()
1246 if (m_assureVisibleCurrentIndex
) {
1247 m_assureVisibleCurrentIndex
= false;
1248 itemView()->scrollTo(itemView()->currentIndex());
1252 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1254 if (!url
.isValid()) {
1255 const QString
location(url
.pathOrUrl());
1256 if (location
.isEmpty()) {
1257 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1259 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1264 m_loadingDirectory
= true;
1267 m_selectedItems
= selectedItems();
1268 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1271 m_dirLister
->stop();
1272 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1274 if (isColumnViewActive()) {
1275 // adjusting the directory lister is not enough in the case of the
1276 // column view, as each column has its own directory lister internally...
1278 m_columnView
->reload();
1280 m_columnView
->showColumn(url
);
1285 KUrl
DolphinView::viewPropertiesUrl() const
1287 if (isColumnViewActive()) {
1288 return m_columnView
->rootUrl();
1294 void DolphinView::applyViewProperties(const KUrl
& url
)
1296 if (m_ignoreViewProperties
) {
1300 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1301 // The column view is active, hence don't apply the view properties
1302 // of sub directories (represented by columns) to the view. The
1303 // view always represents the properties of the first column.
1307 const ViewProperties
props(url
);
1309 const Mode mode
= props
.viewMode();
1310 if (m_mode
!= mode
) {
1311 const int oldZoomLevel
= m_controller
->zoomLevel();
1317 updateZoomLevel(oldZoomLevel
);
1319 if (itemView() == 0) {
1322 Q_ASSERT(itemView() != 0);
1323 Q_ASSERT(m_fileItemDelegate
!= 0);
1325 const bool showHiddenFiles
= props
.showHiddenFiles();
1326 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1327 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1328 emit
showHiddenFilesChanged();
1331 m_storedCategorizedSorting
= props
.categorizedSorting();
1332 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1333 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1334 m_proxyModel
->setCategorizedModel(categorized
);
1335 emit
categorizedSortingChanged();
1338 const DolphinView::Sorting sorting
= props
.sorting();
1339 if (sorting
!= m_proxyModel
->sorting()) {
1340 m_proxyModel
->setSorting(sorting
);
1341 emit
sortingChanged(sorting
);
1344 const Qt::SortOrder sortOrder
= props
.sortOrder();
1345 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1346 m_proxyModel
->setSortOrder(sortOrder
);
1347 emit
sortOrderChanged(sortOrder
);
1350 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1351 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1352 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1353 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1356 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1357 if (info
!= m_fileItemDelegate
->showInformation()) {
1358 m_fileItemDelegate
->setShowInformation(info
);
1359 emit
additionalInfoChanged();
1362 const bool showPreview
= props
.showPreview();
1363 if (showPreview
!= m_showPreview
) {
1364 m_showPreview
= showPreview
;
1365 m_previewGenerator
->setPreviewShown(showPreview
);
1367 const int oldZoomLevel
= m_controller
->zoomLevel();
1368 emit
showPreviewChanged();
1370 // Enabling or disabling the preview might change the icon size of the view.
1371 // As the view does not emit a signal when the icon size has been changed,
1372 // the used zoom level of the controller must be adjusted manually:
1373 updateZoomLevel(oldZoomLevel
);
1376 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1377 // During the lifetime of a DolphinView instance the global view properties
1378 // should not be changed. This allows e. g. to split a view and use different
1379 // view properties for each view.
1380 m_ignoreViewProperties
= true;
1384 void DolphinView::createView()
1387 Q_ASSERT(m_iconsView
== 0);
1388 Q_ASSERT(m_detailsView
== 0);
1389 Q_ASSERT(m_columnView
== 0);
1391 QAbstractItemView
* view
= 0;
1394 m_iconsView
= new DolphinIconsView(this, m_controller
);
1400 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1401 view
= m_detailsView
;
1405 m_columnView
= new DolphinColumnView(this, m_controller
);
1406 view
= m_columnView
;
1410 Q_ASSERT(view
!= 0);
1411 view
->installEventFilter(this);
1412 view
->viewport()->installEventFilter(this);
1413 setFocusProxy(view
);
1415 if (m_mode
!= ColumnView
) {
1416 // Give the view the ability to auto-expand its directories on hovering
1417 // (the column view takes care about this itself). If the details view
1418 // uses expandable folders, the auto-expanding should be used always.
1419 DolphinSettings
& settings
= DolphinSettings::instance();
1420 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1421 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1423 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1424 folderExpander
->setEnabled(enabled
);
1425 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1426 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1429 // Listen out for requests to delete the current column.
1430 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1431 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1434 m_controller
->setItemView(view
);
1436 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1437 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1438 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1439 view
->setItemDelegate(m_fileItemDelegate
);
1441 view
->setModel(m_proxyModel
);
1442 if (m_selectionModel
!= 0) {
1443 view
->setSelectionModel(m_selectionModel
);
1445 m_selectionModel
= view
->selectionModel();
1448 m_selectionChangedTimer
= new QTimer(this);
1449 m_selectionChangedTimer
->setSingleShot(true);
1450 m_selectionChangedTimer
->setInterval(300);
1451 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
1452 this, SLOT(emitSelectionChangedSignal()));
1454 // reparent the selection model, as it should not be deleted
1455 // when deleting the model
1456 m_selectionModel
->setParent(this);
1458 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1460 m_previewGenerator
= new KFilePreviewGenerator(view
);
1461 m_previewGenerator
->setPreviewShown(m_showPreview
);
1463 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1464 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1465 connect(m_controller
, SIGNAL(hideToolTip()),
1466 m_toolTipManager
, SLOT(hideTip()));
1469 m_topLayout
->insertWidget(1, view
);
1471 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1472 this, SLOT(emitDelayedSelectionChangedSignal()));
1473 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1474 this, SLOT(emitContentsMoved()));
1475 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1476 this, SLOT(emitContentsMoved()));
1479 void DolphinView::deleteView()
1481 QAbstractItemView
* view
= itemView();
1483 // It's important to set the keyboard focus to the parent
1484 // before deleting the view: Otherwise when having a split
1485 // view the other view will get the focus and will request
1486 // an activation (see DolphinView::eventFilter()).
1490 m_topLayout
->removeWidget(view
);
1493 // m_previewGenerator's parent is not always destroyed, and we
1494 // don't want two active at once - manually delete.
1495 delete m_previewGenerator
;
1496 m_previewGenerator
= 0;
1499 m_controller
->disconnect(view
);
1502 deleteWhenNotDragSource(view
);
1508 m_fileItemDelegate
= 0;
1509 m_toolTipManager
= 0;
1513 QAbstractItemView
* DolphinView::itemView() const
1515 if (m_detailsView
!= 0) {
1516 return m_detailsView
;
1517 } else if (m_columnView
!= 0) {
1518 return m_columnView
;
1524 void DolphinView::pasteToUrl(const KUrl
& url
)
1526 addNewFileNames(QApplication::clipboard()->mimeData());
1527 KonqOperations::doPaste(this, url
);
1530 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1532 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1533 if (oldZoomLevel
!= newZoomLevel
) {
1534 m_controller
->setZoomLevel(newZoomLevel
);
1535 emit
zoomLevelChanged(newZoomLevel
);
1539 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1541 KUrl::List list
= selectedUrls();
1542 if (itemsExpandable() ) {
1543 list
= KDirModel::simplifiedUrlList(list
);
1548 QMimeData
* DolphinView::selectionMimeData() const
1550 if (isColumnViewActive()) {
1551 return m_columnView
->selectionMimeData();
1554 const QAbstractItemView
* view
= itemView();
1555 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1556 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1557 return m_dolphinModel
->mimeData(selection
.indexes());
1560 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1562 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1563 foreach (const KUrl
& url
, urls
) {
1564 m_newFileNames
.insert(url
.fileName());
1568 #include "dolphinview.moc"