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 applyViewProperties(url
);
162 m_topLayout
->addWidget(itemView());
165 DolphinView::~DolphinView()
167 delete m_expandedDragSource
;
168 m_expandedDragSource
= 0;
171 const KUrl
& DolphinView::url() const
173 return m_controller
->url();
176 KUrl
DolphinView::rootUrl() const
178 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
181 void DolphinView::setActive(bool active
)
183 if (active
== m_active
) {
189 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
191 emit
selectionChanged(selectedItems());
196 QWidget
* viewport
= itemView()->viewport();
198 palette
.setColor(viewport
->backgroundRole(), color
);
199 viewport
->setPalette(palette
);
204 itemView()->setFocus();
208 m_controller
->indicateActivationChange(active
);
211 bool DolphinView::isActive() const
216 void DolphinView::setMode(Mode mode
)
218 if (mode
== m_mode
) {
219 return; // the wished mode is already set
222 const int oldZoomLevel
= m_controller
->zoomLevel();
227 const KUrl viewPropsUrl
= viewPropertiesUrl();
228 ViewProperties
props(viewPropsUrl
);
229 props
.setViewMode(m_mode
);
232 // the file item delegate has been recreated, apply the current
233 // additional information manually
234 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
235 m_fileItemDelegate
->setShowInformation(infoList
);
236 emit
additionalInfoChanged();
238 // Not all view modes support categorized sorting. Adjust the sorting model
239 // if changing the view mode results in a change of the categorized sorting
241 m_storedCategorizedSorting
= props
.categorizedSorting();
242 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
243 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
244 m_proxyModel
->setCategorizedModel(categorized
);
245 emit
categorizedSortingChanged();
250 updateZoomLevel(oldZoomLevel
);
252 loadDirectory(viewPropsUrl
);
256 DolphinView::Mode
DolphinView::mode() const
261 bool DolphinView::showPreview() const
263 return m_showPreview
;
266 bool DolphinView::showHiddenFiles() const
268 return m_dirLister
->showingDotFiles();
271 bool DolphinView::categorizedSorting() const
273 // If all view modes would support categorized sorting, returning
274 // m_proxyModel->isCategorizedModel() would be the way to go. As
275 // currently only the icons view supports caterized sorting, we remember
276 // the stored view properties state in m_storedCategorizedSorting and
277 // return this state. The application takes care to disable the corresponding
278 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
279 // that this setting is not applied to the current view mode.
280 return m_storedCategorizedSorting
;
283 bool DolphinView::supportsCategorizedSorting() const
285 return m_iconsView
!= 0;
288 void DolphinView::selectAll()
290 QAbstractItemView
* view
= itemView();
291 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
292 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
293 // selection instead of selecting all items. This is bypassed for KDE 4.0
294 // by invoking clearSelection() first.
295 view
->clearSelection();
299 void DolphinView::invertSelection()
301 if (isColumnViewActive()) {
302 // QAbstractItemView does not offer a virtual method invertSelection()
303 // as counterpart to QAbstractItemView::selectAll(). This makes it
304 // necessary to delegate the inverting of the selection to the
305 // column view, as only the selection of the active column should
307 m_columnView
->invertSelection();
309 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
310 const QAbstractItemModel
* itemModel
= selectionModel
->model();
312 const QModelIndex topLeft
= itemModel
->index(0, 0);
313 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
314 itemModel
->columnCount() - 1);
316 const QItemSelection
selection(topLeft
, bottomRight
);
317 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
321 bool DolphinView::hasSelection() const
323 return itemView()->selectionModel()->hasSelection();
326 void DolphinView::clearSelection()
328 QItemSelectionModel
* selModel
= itemView()->selectionModel();
329 const QModelIndex currentIndex
= selModel
->currentIndex();
330 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
331 QItemSelectionModel::Clear
);
334 KFileItemList
DolphinView::selectedItems() const
336 if (isColumnViewActive()) {
337 return m_columnView
->selectedItems();
340 const QAbstractItemView
* view
= itemView();
342 // Our view has a selection, we will map them back to the DolphinModel
343 // and then fill the KFileItemList.
344 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
346 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
347 KFileItemList itemList
;
349 const QModelIndexList indexList
= selection
.indexes();
350 foreach (const QModelIndex
&index
, indexList
) {
351 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
352 if (!item
.isNull()) {
353 itemList
.append(item
);
360 KUrl::List
DolphinView::selectedUrls() const
363 const KFileItemList list
= selectedItems();
364 foreach (const KFileItem
&item
, list
) {
365 urls
.append(item
.url());
370 int DolphinView::selectedItemsCount() const
372 if (isColumnViewActive()) {
373 // TODO: get rid of this special case by adjusting the dir lister
374 // to the current column
375 return m_columnView
->selectedItems().count();
378 return itemView()->selectionModel()->selection().count();
381 void DolphinView::setContentsPosition(int x
, int y
)
383 QAbstractItemView
* view
= itemView();
385 // the ColumnView takes care itself for the horizontal scrolling
386 if (!isColumnViewActive()) {
387 view
->horizontalScrollBar()->setValue(x
);
389 view
->verticalScrollBar()->setValue(y
);
391 m_loadingDirectory
= false;
394 QPoint
DolphinView::contentsPosition() const
396 const int x
= itemView()->horizontalScrollBar()->value();
397 const int y
= itemView()->verticalScrollBar()->value();
401 void DolphinView::setZoomLevel(int level
)
403 if (level
< ZoomLevelInfo::minimumLevel()) {
404 level
= ZoomLevelInfo::minimumLevel();
405 } else if (level
> ZoomLevelInfo::maximumLevel()) {
406 level
= ZoomLevelInfo::maximumLevel();
409 if (level
!= zoomLevel()) {
410 m_controller
->setZoomLevel(level
);
411 m_previewGenerator
->updateIcons();
412 emit
zoomLevelChanged(level
);
416 int DolphinView::zoomLevel() const
418 return m_controller
->zoomLevel();
421 void DolphinView::setSorting(Sorting sorting
)
423 if (sorting
!= this->sorting()) {
424 updateSorting(sorting
);
428 DolphinView::Sorting
DolphinView::sorting() const
430 return m_proxyModel
->sorting();
433 void DolphinView::setSortOrder(Qt::SortOrder order
)
435 if (sortOrder() != order
) {
436 updateSortOrder(order
);
440 Qt::SortOrder
DolphinView::sortOrder() const
442 return m_proxyModel
->sortOrder();
445 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
447 if (sortFoldersFirst() != foldersFirst
) {
448 updateSortFoldersFirst(foldersFirst
);
452 bool DolphinView::sortFoldersFirst() const
454 return m_proxyModel
->sortFoldersFirst();
457 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
459 const KUrl viewPropsUrl
= viewPropertiesUrl();
460 ViewProperties
props(viewPropsUrl
);
461 props
.setAdditionalInfo(info
);
462 m_fileItemDelegate
->setShowInformation(info
);
464 emit
additionalInfoChanged();
466 if (itemView() != m_detailsView
) {
467 // the details view requires no reloading of the directory, as it maps
468 // the file item delegate info to its columns internally
469 loadDirectory(viewPropsUrl
);
473 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
475 return m_fileItemDelegate
->showInformation();
478 void DolphinView::reload()
481 loadDirectory(url(), true);
484 void DolphinView::refresh()
486 m_ignoreViewProperties
= false;
488 const bool oldActivationState
= m_active
;
489 const int oldZoomLevel
= m_controller
->zoomLevel();
493 applyViewProperties(m_controller
->url());
496 setActive(oldActivationState
);
497 updateZoomLevel(oldZoomLevel
);
500 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
502 if (m_controller
->url() == url
) {
506 m_previewGenerator
->cancelPreviews();
507 m_controller
->setUrl(url
); // emits urlChanged, which we forward
509 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
510 applyViewProperties(rootUrl
);
511 loadDirectory(rootUrl
);
512 if (itemView() == m_columnView
) {
513 m_columnView
->setRootUrl(rootUrl
);
514 m_columnView
->showColumn(url
);
517 applyViewProperties(url
);
521 emit
startedPathLoading(url
);
524 void DolphinView::setNameFilter(const QString
& nameFilter
)
526 m_proxyModel
->setFilterRegExp(nameFilter
);
528 if (isColumnViewActive()) {
529 // adjusting the directory lister is not enough in the case of the
530 // column view, as each column has its own directory lister internally...
531 m_columnView
->setNameFilter(nameFilter
);
535 void DolphinView::calculateItemCount(int& fileCount
,
537 KIO::filesize_t
& totalFileSize
) const
539 foreach (const KFileItem
& item
, m_dirLister
->items()) {
544 totalFileSize
+= item
.size();
549 QString
DolphinView::statusBarText() const
554 KIO::filesize_t totalFileSize
= 0;
556 if (hasSelection()) {
557 // give a summary of the status of the selected files
558 const KFileItemList list
= selectedItems();
559 if (list
.isEmpty()) {
560 // when an item is triggered, it is temporary selected but selectedItems()
561 // will return an empty list
565 KFileItemList::const_iterator it
= list
.begin();
566 const KFileItemList::const_iterator end
= list
.end();
568 const KFileItem
& item
= *it
;
573 totalFileSize
+= item
.size();
578 if (folderCount
+ fileCount
== 1) {
579 // if only one item is selected, show the filename
580 const QString name
= list
.first().name();
581 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
582 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
583 name
, KIO::convertSize(totalFileSize
));
585 // at least 2 items are selected
586 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
587 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
588 if ((folderCount
> 0) && (fileCount
> 0)) {
589 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
590 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
591 } else if (fileCount
> 0) {
592 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
594 Q_ASSERT(folderCount
> 0);
599 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
600 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
601 fileCount
, folderCount
,
602 totalFileSize
, true);
608 void DolphinView::setUrl(const KUrl
& url
)
610 // remember current item candidate (see slotDirListerCompleted())
611 m_selectClipboardItems
= false;
612 m_currentItemUrl
= url
;
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 RenameDialog
dialog(this, items
);
649 if (dialog
.exec() == QDialog::Rejected
) {
653 const QString newName
= dialog
.newName();
654 if (newName
.isEmpty()) {
655 emit
errorMessage(dialog
.errorString());
659 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
660 // as one operation instead of n rename operations like it is done now...
661 Q_ASSERT(newName
.contains('#'));
663 // currently the items are sorted by the selection order, resort
664 // them by the file name
665 qSort(items
.begin(), items
.end(), lessThan
);
667 // iterate through all selected items and rename them...
669 foreach (const KFileItem
& item
, items
) {
670 const KUrl
& oldUrl
= item
.url();
672 number
.setNum(index
++);
674 QString name
= newName
;
675 name
.replace('#', number
);
677 if (oldUrl
.fileName() != name
) {
678 KUrl newUrl
= oldUrl
;
679 newUrl
.setFileName(name
);
680 KonqOperations::rename(this, oldUrl
, newUrl
);
683 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
684 Q_ASSERT(itemCount
== 1);
686 if (isColumnViewActive()) {
687 m_columnView
->editItem(items
.first());
689 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
690 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
691 itemView()->edit(proxyIndex
);
694 Q_ASSERT(itemCount
== 1);
696 RenameDialog
dialog(this, items
);
697 if (dialog
.exec() == QDialog::Rejected
) {
701 const QString
& newName
= dialog
.newName();
702 if (newName
.isEmpty()) {
703 emit
errorMessage(dialog
.errorString());
707 const KUrl
& oldUrl
= items
.first().url();
708 KUrl newUrl
= oldUrl
;
709 newUrl
.setFileName(newName
);
710 KonqOperations::rename(this, oldUrl
, newUrl
);
713 // assure that the current index remains visible when KDirLister
714 // will notify the view about changed items
715 m_assureVisibleCurrentIndex
= true;
718 void DolphinView::trashSelectedItems()
720 const KUrl::List list
= simplifiedSelectedUrls();
721 KonqOperations::del(this, KonqOperations::TRASH
, list
);
724 void DolphinView::deleteSelectedItems()
726 const KUrl::List list
= simplifiedSelectedUrls();
727 const bool del
= KonqOperations::askDeleteConfirmation(list
,
729 KonqOperations::DEFAULT_CONFIRMATION
,
733 KIO::Job
* job
= KIO::del(list
);
734 connect(job
, SIGNAL(result(KJob
*)),
735 this, SLOT(slotDeleteFileFinished(KJob
*)));
739 void DolphinView::cutSelectedItems()
741 QMimeData
* mimeData
= selectionMimeData();
742 KonqMimeData::addIsCutSelection(mimeData
, true);
743 QApplication::clipboard()->setMimeData(mimeData
);
746 void DolphinView::copySelectedItems()
748 QMimeData
* mimeData
= selectionMimeData();
749 QApplication::clipboard()->setMimeData(mimeData
);
752 void DolphinView::paste()
757 void DolphinView::pasteIntoFolder()
759 const KFileItemList items
= selectedItems();
760 if ((items
.count() == 1) && items
.first().isDir()) {
761 pasteToUrl(items
.first().url());
765 void DolphinView::setShowPreview(bool show
)
767 if (m_showPreview
== show
) {
771 const KUrl viewPropsUrl
= viewPropertiesUrl();
772 ViewProperties
props(viewPropsUrl
);
773 props
.setShowPreview(show
);
775 m_showPreview
= show
;
776 m_previewGenerator
->setPreviewShown(show
);
778 const int oldZoomLevel
= m_controller
->zoomLevel();
779 emit
showPreviewChanged();
781 // Enabling or disabling the preview might change the icon size of the view.
782 // As the view does not emit a signal when the icon size has been changed,
783 // the used zoom level of the controller must be adjusted manually:
784 updateZoomLevel(oldZoomLevel
);
786 loadDirectory(viewPropsUrl
);
789 void DolphinView::setShowHiddenFiles(bool show
)
791 if (m_dirLister
->showingDotFiles() == show
) {
795 const KUrl viewPropsUrl
= viewPropertiesUrl();
796 ViewProperties
props(viewPropsUrl
);
797 props
.setShowHiddenFiles(show
);
799 m_dirLister
->setShowingDotFiles(show
);
800 emit
showHiddenFilesChanged();
802 loadDirectory(viewPropsUrl
);
805 void DolphinView::setCategorizedSorting(bool categorized
)
807 if (categorized
== categorizedSorting()) {
811 // setCategorizedSorting(true) may only get invoked
812 // if the view supports categorized sorting
813 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
815 ViewProperties
props(viewPropertiesUrl());
816 props
.setCategorizedSorting(categorized
);
819 m_storedCategorizedSorting
= categorized
;
820 m_proxyModel
->setCategorizedModel(categorized
);
822 emit
categorizedSortingChanged();
825 void DolphinView::toggleSortOrder()
827 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
828 Qt::DescendingOrder
:
833 void DolphinView::toggleSortFoldersFirst()
835 setSortFoldersFirst(!sortFoldersFirst());
838 void DolphinView::toggleAdditionalInfo(QAction
* action
)
840 const KFileItemDelegate::Information info
=
841 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
843 KFileItemDelegate::InformationList list
= additionalInfo();
845 const bool show
= action
->isChecked();
847 const int index
= list
.indexOf(info
);
848 const bool containsInfo
= (index
>= 0);
849 if (show
&& !containsInfo
) {
851 setAdditionalInfo(list
);
852 } else if (!show
&& containsInfo
) {
853 list
.removeAt(index
);
854 setAdditionalInfo(list
);
855 Q_ASSERT(list
.indexOf(info
) < 0);
859 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
861 QWidget::mouseReleaseEvent(event
);
865 void DolphinView::wheelEvent(QWheelEvent
* event
)
867 if (event
->modifiers() & Qt::ControlModifier
) {
868 const int delta
= event
->delta();
869 const int level
= zoomLevel();
871 setZoomLevel(level
+ 1);
872 } else if (delta
< 0) {
873 setZoomLevel(level
- 1);
879 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
881 switch (event
->type()) {
882 case QEvent::FocusIn
:
883 if (watched
== itemView()) {
884 m_controller
->requestActivation();
888 case QEvent::MouseButtonPress
:
889 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
890 // Listening to a mousebutton press event to delete expanded views is a
891 // workaround, as it seems impossible for the FolderExpander to know when
892 // a dragging outside a view has been finished. However it works quite well:
893 // A mousebutton press event indicates that a drag operation must be
895 m_expandedDragSource
->deleteLater();
896 m_expandedDragSource
= 0;
900 case QEvent::DragEnter
:
901 if (watched
== itemView()->viewport()) {
906 case QEvent::KeyPress
:
907 if (watched
== itemView()) {
908 if (m_toolTipManager
!= 0) {
909 m_toolTipManager
->hideTip();
912 // clear the selection when Escape has been pressed
913 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
914 if (keyEvent
->key() == Qt::Key_Escape
) {
924 return QWidget::eventFilter(watched
, event
);
927 void DolphinView::activate()
932 void DolphinView::triggerItem(const KFileItem
& item
)
934 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
935 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
936 // items are selected by the user, hence don't trigger the
937 // item specified by 'index'
941 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
942 if (item
.isNull() || m_isContextMenuOpen
) {
946 if (m_toolTipManager
!= 0) {
947 m_toolTipManager
->hideTip();
949 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
952 void DolphinView::emitSelectionChangedSignal()
954 emit
selectionChanged(DolphinView::selectedItems());
957 void DolphinView::openContextMenu(const QPoint
& pos
,
958 const QList
<QAction
*>& customActions
)
961 if (isColumnViewActive()) {
962 item
= m_columnView
->itemAt(pos
);
964 const QModelIndex index
= itemView()->indexAt(pos
);
965 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
966 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
967 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
971 if (m_toolTipManager
!= 0) {
972 m_toolTipManager
->hideTip();
975 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
976 emit
requestContextMenu(item
, url(), customActions
);
977 m_isContextMenuOpen
= false;
980 void DolphinView::dropUrls(const KFileItem
& destItem
,
981 const KUrl
& destPath
,
984 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
987 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
989 ViewProperties
props(viewPropertiesUrl());
990 props
.setSorting(sorting
);
992 m_proxyModel
->setSorting(sorting
);
994 emit
sortingChanged(sorting
);
997 void DolphinView::updateSortOrder(Qt::SortOrder order
)
999 ViewProperties
props(viewPropertiesUrl());
1000 props
.setSortOrder(order
);
1002 m_proxyModel
->setSortOrder(order
);
1004 emit
sortOrderChanged(order
);
1007 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1009 ViewProperties
props(viewPropertiesUrl());
1010 props
.setSortFoldersFirst(foldersFirst
);
1012 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1014 emit
sortFoldersFirstChanged(foldersFirst
);
1017 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1019 ViewProperties
props(viewPropertiesUrl());
1020 props
.setAdditionalInfo(info
);
1023 m_fileItemDelegate
->setShowInformation(info
);
1025 emit
additionalInfoChanged();
1028 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1030 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1031 (m_mode
== DolphinView::IconsView
);
1033 QAction
* showSizeInfo
= collection
->action("show_size_info");
1034 QAction
* showDateInfo
= collection
->action("show_date_info");
1035 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1036 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1037 QAction
* showGroupInfo
= collection
->action("show_group_info");
1038 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1040 showSizeInfo
->setChecked(false);
1041 showDateInfo
->setChecked(false);
1042 showPermissionsInfo
->setChecked(false);
1043 showOwnerInfo
->setChecked(false);
1044 showGroupInfo
->setChecked(false);
1045 showMimeInfo
->setChecked(false);
1047 showSizeInfo
->setEnabled(enable
);
1048 showDateInfo
->setEnabled(enable
);
1049 showPermissionsInfo
->setEnabled(enable
);
1050 showOwnerInfo
->setEnabled(enable
);
1051 showGroupInfo
->setEnabled(enable
);
1052 showMimeInfo
->setEnabled(enable
);
1054 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1056 case KFileItemDelegate::Size
:
1057 showSizeInfo
->setChecked(true);
1059 case KFileItemDelegate::ModificationTime
:
1060 showDateInfo
->setChecked(true);
1062 case KFileItemDelegate::Permissions
:
1063 showPermissionsInfo
->setChecked(true);
1065 case KFileItemDelegate::Owner
:
1066 showOwnerInfo
->setChecked(true);
1068 case KFileItemDelegate::OwnerAndGroup
:
1069 showGroupInfo
->setChecked(true);
1071 case KFileItemDelegate::FriendlyMimeType
:
1072 showMimeInfo
->setChecked(true);
1080 QPair
<bool, QString
> DolphinView::pasteInfo() const
1082 return KonqOperations::pasteInfo(url());
1085 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1087 m_tabsForFiles
= tabsForFiles
;
1090 bool DolphinView::isTabsForFilesEnabled() const
1092 return m_tabsForFiles
;
1095 bool DolphinView::itemsExpandable() const
1097 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1100 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1105 if (DragAndDropHelper::instance().isDragSource(view
)) {
1106 // We must store for later deletion.
1107 if (m_expandedDragSource
!= 0) {
1108 // The old stored view is obviously not the drag source anymore.
1109 m_expandedDragSource
->deleteLater();
1110 m_expandedDragSource
= 0;
1113 m_expandedDragSource
= view
;
1116 view
->deleteLater();
1120 void DolphinView::observeCreatedItem(const KUrl
& url
)
1122 m_createdItemUrl
= url
;
1123 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1124 this, SLOT(selectAndScrollToCreatedItem()));
1127 void DolphinView::selectAndScrollToCreatedItem()
1129 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1130 if (dirIndex
.isValid()) {
1131 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1132 itemView()->setCurrentIndex(proxyIndex
);
1135 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1136 this, SLOT(selectAndScrollToCreatedItem()));
1137 m_createdItemUrl
= KUrl();
1140 void DolphinView::restoreSelection()
1142 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1143 changeSelection(m_selectedItems
);
1146 void DolphinView::emitContentsMoved()
1148 // only emit the contents moved signal if:
1149 // - no directory loading is ongoing (this would reset the contents position
1150 // always to (0, 0))
1151 // - if the Column View is active: the column view does an automatic
1152 // positioning during the loading operation, which must be remembered
1153 if (!m_loadingDirectory
|| isColumnViewActive()) {
1154 const QPoint
pos(contentsPosition());
1155 emit
contentsMoved(pos
.x(), pos
.y());
1159 void DolphinView::showHoverInformation(const KFileItem
& item
)
1161 emit
requestItemInfo(item
);
1164 void DolphinView::clearHoverInformation()
1166 emit
requestItemInfo(KFileItem());
1169 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1171 if (job
->error() == 0) {
1172 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1173 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1174 emit
errorMessage(job
->errorString());
1178 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1180 emit
requestUrlChange(url
);
1181 m_controller
->setUrl(url
);
1184 void DolphinView::slotDirListerCompleted()
1186 if (!m_currentItemUrl
.isEmpty()) {
1187 // assure that the current item remains visible
1188 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1189 if (dirIndex
.isValid()) {
1190 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1191 QAbstractItemView
* view
= itemView();
1192 const bool clearSelection
= !hasSelection();
1193 view
->setCurrentIndex(proxyIndex
);
1194 if (clearSelection
) {
1195 view
->clearSelection();
1198 m_currentItemUrl
.clear();
1201 if (m_selectClipboardItems
) {
1202 m_selectClipboardItems
= false;
1204 // select all items that have been pasted from the clipboard to
1205 // the current directory
1206 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1207 const KUrl::List copiedUrls
= KUrl::List::fromMimeData(mimeData
);
1209 QSet
<QString
> fileNames
;
1210 foreach (const KUrl
& url
, copiedUrls
) {
1211 fileNames
.insert(url
.fileName());
1214 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1215 const int rowCount
= m_proxyModel
->rowCount();
1216 for (int row
= 0; row
< rowCount
; ++row
) {
1217 const QModelIndex proxyIndex
= m_proxyModel
->index(row
, 0);
1218 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(proxyIndex
);
1219 const KUrl url
= m_dolphinModel
->itemForIndex(dirIndex
).url();
1220 if (fileNames
.contains(url
.fileName())) {
1221 selectionModel
->select(proxyIndex
, QItemSelectionModel::Select
);
1227 void DolphinView::slotRefreshItems()
1229 if (m_assureVisibleCurrentIndex
) {
1230 m_assureVisibleCurrentIndex
= false;
1231 itemView()->scrollTo(itemView()->currentIndex());
1235 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1237 if (!url
.isValid()) {
1238 const QString
location(url
.pathOrUrl());
1239 if (location
.isEmpty()) {
1240 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1242 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1247 m_loadingDirectory
= true;
1250 m_selectedItems
= selectedItems();
1251 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1254 m_dirLister
->stop();
1255 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1257 if (isColumnViewActive()) {
1258 // adjusting the directory lister is not enough in the case of the
1259 // column view, as each column has its own directory lister internally...
1261 m_columnView
->reload();
1263 m_columnView
->showColumn(url
);
1268 KUrl
DolphinView::viewPropertiesUrl() const
1270 if (isColumnViewActive()) {
1271 return m_columnView
->rootUrl();
1277 void DolphinView::applyViewProperties(const KUrl
& url
)
1279 if (m_ignoreViewProperties
) {
1283 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1284 // The column view is active, hence don't apply the view properties
1285 // of sub directories (represented by columns) to the view. The
1286 // view always represents the properties of the first column.
1290 const ViewProperties
props(url
);
1292 const Mode mode
= props
.viewMode();
1293 if (m_mode
!= mode
) {
1294 const int oldZoomLevel
= m_controller
->zoomLevel();
1300 updateZoomLevel(oldZoomLevel
);
1302 if (itemView() == 0) {
1305 Q_ASSERT(itemView() != 0);
1306 Q_ASSERT(m_fileItemDelegate
!= 0);
1308 const bool showHiddenFiles
= props
.showHiddenFiles();
1309 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1310 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1311 emit
showHiddenFilesChanged();
1314 m_storedCategorizedSorting
= props
.categorizedSorting();
1315 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1316 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1317 m_proxyModel
->setCategorizedModel(categorized
);
1318 emit
categorizedSortingChanged();
1321 const DolphinView::Sorting sorting
= props
.sorting();
1322 if (sorting
!= m_proxyModel
->sorting()) {
1323 m_proxyModel
->setSorting(sorting
);
1324 emit
sortingChanged(sorting
);
1327 const Qt::SortOrder sortOrder
= props
.sortOrder();
1328 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1329 m_proxyModel
->setSortOrder(sortOrder
);
1330 emit
sortOrderChanged(sortOrder
);
1333 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1334 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1335 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1336 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1339 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1340 if (info
!= m_fileItemDelegate
->showInformation()) {
1341 m_fileItemDelegate
->setShowInformation(info
);
1342 emit
additionalInfoChanged();
1345 const bool showPreview
= props
.showPreview();
1346 if (showPreview
!= m_showPreview
) {
1347 m_showPreview
= showPreview
;
1348 m_previewGenerator
->setPreviewShown(showPreview
);
1350 const int oldZoomLevel
= m_controller
->zoomLevel();
1351 emit
showPreviewChanged();
1353 // Enabling or disabling the preview might change the icon size of the view.
1354 // As the view does not emit a signal when the icon size has been changed,
1355 // the used zoom level of the controller must be adjusted manually:
1356 updateZoomLevel(oldZoomLevel
);
1359 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1360 // During the lifetime of a DolphinView instance the global view properties
1361 // should not be changed. This allows e. g. to split a view and use different
1362 // view properties for each view.
1363 m_ignoreViewProperties
= true;
1367 void DolphinView::createView()
1370 Q_ASSERT(m_iconsView
== 0);
1371 Q_ASSERT(m_detailsView
== 0);
1372 Q_ASSERT(m_columnView
== 0);
1374 QAbstractItemView
* view
= 0;
1377 m_iconsView
= new DolphinIconsView(this, m_controller
);
1383 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1384 view
= m_detailsView
;
1388 m_columnView
= new DolphinColumnView(this, m_controller
);
1389 view
= m_columnView
;
1393 Q_ASSERT(view
!= 0);
1394 view
->installEventFilter(this);
1395 view
->viewport()->installEventFilter(this);
1396 setFocusProxy(view
);
1398 if (m_mode
!= ColumnView
) {
1399 // Give the view the ability to auto-expand its directories on hovering
1400 // (the column view takes care about this itself). If the details view
1401 // uses expandable folders, the auto-expanding should be used always.
1402 DolphinSettings
& settings
= DolphinSettings::instance();
1403 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1404 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1406 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1407 folderExpander
->setEnabled(enabled
);
1408 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1409 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1412 // Listen out for requests to delete the current column.
1413 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1414 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1417 m_controller
->setItemView(view
);
1419 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1420 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1421 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1422 view
->setItemDelegate(m_fileItemDelegate
);
1424 view
->setModel(m_proxyModel
);
1425 if (m_selectionModel
!= 0) {
1426 view
->setSelectionModel(m_selectionModel
);
1428 m_selectionModel
= view
->selectionModel();
1431 // reparent the selection model, as it should not be deleted
1432 // when deleting the model
1433 m_selectionModel
->setParent(this);
1435 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1437 m_previewGenerator
= new KFilePreviewGenerator(view
);
1438 m_previewGenerator
->setPreviewShown(m_showPreview
);
1440 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1441 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1442 connect(m_controller
, SIGNAL(hideToolTip()),
1443 m_toolTipManager
, SLOT(hideTip()));
1446 m_topLayout
->insertWidget(1, view
);
1448 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1449 this, SLOT(emitSelectionChangedSignal()));
1450 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1451 this, SLOT(emitContentsMoved()));
1452 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1453 this, SLOT(emitContentsMoved()));
1456 void DolphinView::deleteView()
1458 QAbstractItemView
* view
= itemView();
1460 // It's important to set the keyboard focus to the parent
1461 // before deleting the view: Otherwise when having a split
1462 // view the other view will get the focus and will request
1463 // an activation (see DolphinView::eventFilter()).
1467 m_topLayout
->removeWidget(view
);
1470 // m_previewGenerator's parent is not always destroyed, and we
1471 // don't want two active at once - manually delete.
1472 delete m_previewGenerator
;
1473 m_previewGenerator
= 0;
1476 m_controller
->disconnect(view
);
1479 deleteWhenNotDragSource(view
);
1485 m_fileItemDelegate
= 0;
1486 m_toolTipManager
= 0;
1490 QAbstractItemView
* DolphinView::itemView() const
1492 if (m_detailsView
!= 0) {
1493 return m_detailsView
;
1494 } else if (m_columnView
!= 0) {
1495 return m_columnView
;
1501 void DolphinView::pasteToUrl(const KUrl
& url
)
1503 m_selectClipboardItems
= true;
1504 KonqOperations::doPaste(this, url
);
1507 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1509 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1510 if (oldZoomLevel
!= newZoomLevel
) {
1511 m_controller
->setZoomLevel(newZoomLevel
);
1512 emit
zoomLevelChanged(newZoomLevel
);
1516 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1518 KUrl::List list
= selectedUrls();
1519 if (itemsExpandable() ) {
1520 list
= KDirModel::simplifiedUrlList(list
);
1525 QMimeData
* DolphinView::selectionMimeData() const
1527 if (isColumnViewActive()) {
1528 return m_columnView
->selectionMimeData();
1531 const QAbstractItemView
* view
= itemView();
1532 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1533 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1534 return m_dolphinModel
->mimeData(selection
.indexes());
1538 #include "dolphinview.moc"