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_dolphinModel(dolphinModel
),
102 m_dirLister(dirLister
),
103 m_proxyModel(proxyModel
),
104 m_previewGenerator(0),
109 m_expandedDragSource(0)
111 m_topLayout
= new QVBoxLayout(this);
112 m_topLayout
->setSpacing(0);
113 m_topLayout
->setMargin(0);
115 m_controller
= new DolphinController(this);
116 m_controller
->setUrl(url
);
118 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
119 this, SIGNAL(urlChanged(const KUrl
&)));
120 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
121 this, SLOT(slotRequestUrlChange(const KUrl
&)));
123 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
124 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
125 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
126 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
127 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
128 this, SLOT(updateSorting(DolphinView::Sorting
)));
129 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
130 this, SLOT(updateSortOrder(Qt::SortOrder
)));
131 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
132 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
133 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
134 this, SLOT(triggerItem(const KFileItem
&)));
135 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
136 this, SIGNAL(tabRequested(const KUrl
&)));
137 connect(m_controller
, SIGNAL(activated()),
138 this, SLOT(activate()));
139 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
140 this, SLOT(showHoverInformation(const KFileItem
&)));
141 connect(m_controller
, SIGNAL(viewportEntered()),
142 this, SLOT(clearHoverInformation()));
144 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
145 this, SIGNAL(redirection(KUrl
, KUrl
)));
146 connect(m_dirLister
, SIGNAL(completed()),
147 this, SLOT(restoreCurrentItem()));
148 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
149 this, SLOT(slotRefreshItems()));
151 // When a new item has been created by the "Create New..." menu, the item should
152 // get selected and it must be assured that the item will get visible. As the
153 // creation is done asynchronously, several signals must be checked:
154 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
155 this, SLOT(observeCreatedItem(const KUrl
&)));
157 applyViewProperties(url
);
158 m_topLayout
->addWidget(itemView());
161 DolphinView::~DolphinView()
163 delete m_expandedDragSource
;
164 m_expandedDragSource
= 0;
167 const KUrl
& DolphinView::url() const
169 return m_controller
->url();
172 KUrl
DolphinView::rootUrl() const
174 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
177 void DolphinView::setActive(bool active
)
179 if (active
== m_active
) {
185 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
187 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
188 // bypasses the problem when having a split view and changing the active view to
189 // update the some URL dependent states. A nicer approach should be no big deal...
190 emit
urlChanged(url());
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
->updatePreviews();
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::setAdditionalInfo(KFileItemDelegate::InformationList info
)
447 const KUrl viewPropsUrl
= viewPropertiesUrl();
448 ViewProperties
props(viewPropsUrl
);
449 props
.setAdditionalInfo(info
);
450 m_fileItemDelegate
->setShowInformation(info
);
452 emit
additionalInfoChanged();
454 if (itemView() != m_detailsView
) {
455 // the details view requires no reloading of the directory, as it maps
456 // the file item delegate info to its columns internally
457 loadDirectory(viewPropsUrl
);
461 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
463 return m_fileItemDelegate
->showInformation();
466 void DolphinView::reload()
469 loadDirectory(url(), true);
472 void DolphinView::refresh()
474 m_ignoreViewProperties
= false;
476 const bool oldActivationState
= m_active
;
477 const int oldZoomLevel
= m_controller
->zoomLevel();
481 applyViewProperties(m_controller
->url());
484 setActive(oldActivationState
);
485 updateZoomLevel(oldZoomLevel
);
488 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
490 if (m_controller
->url() == url
) {
494 m_previewGenerator
->cancelPreviews();
495 m_controller
->setUrl(url
); // emits urlChanged, which we forward
497 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
498 applyViewProperties(rootUrl
);
499 loadDirectory(rootUrl
);
500 if (itemView() == m_columnView
) {
501 m_columnView
->setRootUrl(rootUrl
);
502 m_columnView
->showColumn(url
);
505 applyViewProperties(url
);
509 emit
startedPathLoading(url
);
512 void DolphinView::setNameFilter(const QString
& nameFilter
)
514 m_proxyModel
->setFilterRegExp(nameFilter
);
516 if (isColumnViewActive()) {
517 // adjusting the directory lister is not enough in the case of the
518 // column view, as each column has its own directory lister internally...
519 m_columnView
->setNameFilter(nameFilter
);
523 void DolphinView::calculateItemCount(int& fileCount
,
525 KIO::filesize_t
& totalFileSize
) const
527 foreach (const KFileItem
& item
, m_dirLister
->items()) {
532 totalFileSize
+= item
.size();
537 QString
DolphinView::statusBarText() const
542 KIO::filesize_t totalFileSize
= 0;
544 if (hasSelection()) {
545 // give a summary of the status of the selected files
546 const KFileItemList list
= selectedItems();
547 if (list
.isEmpty()) {
548 // when an item is triggered, it is temporary selected but selectedItems()
549 // will return an empty list
553 KFileItemList::const_iterator it
= list
.begin();
554 const KFileItemList::const_iterator end
= list
.end();
556 const KFileItem
& item
= *it
;
561 totalFileSize
+= item
.size();
566 if (folderCount
+ fileCount
== 1) {
567 // if only one item is selected, show the filename
568 const QString name
= list
.first().name();
569 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
570 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
571 name
, KIO::convertSize(totalFileSize
));
573 // at least 2 items are selected
574 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
575 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
576 if ((folderCount
> 0) && (fileCount
> 0)) {
577 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
578 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
579 } else if (fileCount
> 0) {
580 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
582 Q_ASSERT(folderCount
> 0);
587 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
588 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
589 fileCount
, folderCount
,
590 totalFileSize
, true);
596 void DolphinView::setUrl(const KUrl
& url
)
598 // remember current item candidate (see restoreCurrentItem())
599 m_currentItemUrl
= url
;
600 updateView(url
, KUrl());
603 void DolphinView::changeSelection(const KFileItemList
& selection
)
606 if (selection
.isEmpty()) {
609 const KUrl
& baseUrl
= url();
611 QItemSelection newSelection
;
612 foreach(const KFileItem
& item
, selection
) {
613 url
= item
.url().upUrl();
614 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
615 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
616 newSelection
.select(index
, index
);
619 itemView()->selectionModel()->select(newSelection
,
620 QItemSelectionModel::ClearAndSelect
621 | QItemSelectionModel::Current
);
624 void DolphinView::renameSelectedItems()
626 KFileItemList items
= selectedItems();
627 const int itemCount
= items
.count();
633 // More than one item has been selected for renaming. Open
634 // a rename dialog and rename all items afterwards.
635 RenameDialog
dialog(this, items
);
636 if (dialog
.exec() == QDialog::Rejected
) {
640 const QString newName
= dialog
.newName();
641 if (newName
.isEmpty()) {
642 emit
errorMessage(dialog
.errorString());
646 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
647 // as one operation instead of n rename operations like it is done now...
648 Q_ASSERT(newName
.contains('#'));
650 // currently the items are sorted by the selection order, resort
651 // them by the file name
652 qSort(items
.begin(), items
.end(), lessThan
);
654 // iterate through all selected items and rename them...
656 foreach (const KFileItem
& item
, items
) {
657 const KUrl
& oldUrl
= item
.url();
659 number
.setNum(index
++);
661 QString name
= newName
;
662 name
.replace('#', number
);
664 if (oldUrl
.fileName() != name
) {
665 KUrl newUrl
= oldUrl
;
666 newUrl
.setFileName(name
);
667 KonqOperations::rename(this, oldUrl
, newUrl
);
670 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
671 Q_ASSERT(itemCount
== 1);
673 if (isColumnViewActive()) {
674 m_columnView
->editItem(items
.first());
676 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
677 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
678 itemView()->edit(proxyIndex
);
681 Q_ASSERT(itemCount
== 1);
683 RenameDialog
dialog(this, items
);
684 if (dialog
.exec() == QDialog::Rejected
) {
688 const QString
& newName
= dialog
.newName();
689 if (newName
.isEmpty()) {
690 emit
errorMessage(dialog
.errorString());
694 const KUrl
& oldUrl
= items
.first().url();
695 KUrl newUrl
= oldUrl
;
696 newUrl
.setFileName(newName
);
697 KonqOperations::rename(this, oldUrl
, newUrl
);
700 // assure that the current index remains visible when KDirLister
701 // will notify the view about changed items
702 m_assureVisibleCurrentIndex
= true;
705 void DolphinView::trashSelectedItems()
707 const KUrl::List list
= simplifiedSelectedUrls();
708 KonqOperations::del(this, KonqOperations::TRASH
, list
);
711 void DolphinView::deleteSelectedItems()
713 const KUrl::List list
= simplifiedSelectedUrls();
714 const bool del
= KonqOperations::askDeleteConfirmation(list
,
716 KonqOperations::DEFAULT_CONFIRMATION
,
720 KIO::Job
* job
= KIO::del(list
);
721 connect(job
, SIGNAL(result(KJob
*)),
722 this, SLOT(slotDeleteFileFinished(KJob
*)));
726 void DolphinView::cutSelectedItems()
728 QMimeData
* mimeData
= selectionMimeData();
729 KonqMimeData::addIsCutSelection(mimeData
, true);
730 QApplication::clipboard()->setMimeData(mimeData
);
733 void DolphinView::copySelectedItems()
735 QMimeData
* mimeData
= selectionMimeData();
736 QApplication::clipboard()->setMimeData(mimeData
);
739 void DolphinView::paste()
744 void DolphinView::pasteIntoFolder()
746 const KFileItemList items
= selectedItems();
747 if ((items
.count() == 1) && items
.first().isDir()) {
748 pasteToUrl(items
.first().url());
752 void DolphinView::setShowPreview(bool show
)
754 if (m_showPreview
== show
) {
758 const KUrl viewPropsUrl
= viewPropertiesUrl();
759 ViewProperties
props(viewPropsUrl
);
760 props
.setShowPreview(show
);
762 m_showPreview
= show
;
763 m_previewGenerator
->setPreviewShown(show
);
765 const int oldZoomLevel
= m_controller
->zoomLevel();
766 emit
showPreviewChanged();
768 // Enabling or disabling the preview might change the icon size of the view.
769 // As the view does not emit a signal when the icon size has been changed,
770 // the used zoom level of the controller must be adjusted manually:
771 updateZoomLevel(oldZoomLevel
);
773 loadDirectory(viewPropsUrl
);
776 void DolphinView::setShowHiddenFiles(bool show
)
778 if (m_dirLister
->showingDotFiles() == show
) {
782 const KUrl viewPropsUrl
= viewPropertiesUrl();
783 ViewProperties
props(viewPropsUrl
);
784 props
.setShowHiddenFiles(show
);
786 m_dirLister
->setShowingDotFiles(show
);
787 emit
showHiddenFilesChanged();
789 loadDirectory(viewPropsUrl
);
792 void DolphinView::setCategorizedSorting(bool categorized
)
794 if (categorized
== categorizedSorting()) {
798 // setCategorizedSorting(true) may only get invoked
799 // if the view supports categorized sorting
800 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
802 ViewProperties
props(viewPropertiesUrl());
803 props
.setCategorizedSorting(categorized
);
806 m_storedCategorizedSorting
= categorized
;
807 m_proxyModel
->setCategorizedModel(categorized
);
809 emit
categorizedSortingChanged();
812 void DolphinView::toggleSortOrder()
814 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
815 Qt::DescendingOrder
:
820 void DolphinView::toggleAdditionalInfo(QAction
* action
)
822 const KFileItemDelegate::Information info
=
823 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
825 KFileItemDelegate::InformationList list
= additionalInfo();
827 const bool show
= action
->isChecked();
829 const int index
= list
.indexOf(info
);
830 const bool containsInfo
= (index
>= 0);
831 if (show
&& !containsInfo
) {
833 setAdditionalInfo(list
);
834 } else if (!show
&& containsInfo
) {
835 list
.removeAt(index
);
836 setAdditionalInfo(list
);
837 Q_ASSERT(list
.indexOf(info
) < 0);
841 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
843 QWidget::mouseReleaseEvent(event
);
847 void DolphinView::wheelEvent(QWheelEvent
* event
)
849 if (event
->modifiers() & Qt::ControlModifier
) {
850 const int delta
= event
->delta();
851 const int level
= zoomLevel();
853 setZoomLevel(level
+ 1);
854 } else if (delta
< 0) {
855 setZoomLevel(level
- 1);
861 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
863 switch (event
->type()) {
864 case QEvent::FocusIn
:
865 if (watched
== itemView()) {
866 m_controller
->requestActivation();
870 case QEvent::MouseButtonPress
:
871 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
872 // Listening to a mousebutton press event to delete expanded views is a
873 // workaround, as it seems impossible for the FolderExpander to know when
874 // a dragging outside a view has been finished. However it works quite well:
875 // A mousebutton press event indicates that a drag operation must be
877 m_expandedDragSource
->deleteLater();
878 m_expandedDragSource
= 0;
882 case QEvent::DragEnter
:
883 if (watched
== itemView()->viewport()) {
888 case QEvent::KeyPress
:
889 if (watched
== itemView()) {
890 if (m_toolTipManager
!= 0) {
891 m_toolTipManager
->hideTip();
894 // clear the selection when Escape has been pressed
895 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
896 if (keyEvent
->key() == Qt::Key_Escape
) {
906 return QWidget::eventFilter(watched
, event
);
909 void DolphinView::activate()
914 void DolphinView::triggerItem(const KFileItem
& item
)
916 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
917 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
918 // items are selected by the user, hence don't trigger the
919 // item specified by 'index'
923 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
924 if (item
.isNull() || m_isContextMenuOpen
) {
928 if (m_toolTipManager
!= 0) {
929 m_toolTipManager
->hideTip();
931 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
934 void DolphinView::emitSelectionChangedSignal()
936 emit
selectionChanged(DolphinView::selectedItems());
939 void DolphinView::openContextMenu(const QPoint
& pos
,
940 const QList
<QAction
*>& customActions
)
943 if (isColumnViewActive()) {
944 item
= m_columnView
->itemAt(pos
);
946 const QModelIndex index
= itemView()->indexAt(pos
);
947 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
948 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
949 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
953 if (m_toolTipManager
!= 0) {
954 m_toolTipManager
->hideTip();
957 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
958 emit
requestContextMenu(item
, url(), customActions
);
959 m_isContextMenuOpen
= false;
962 void DolphinView::dropUrls(const KFileItem
& destItem
,
963 const KUrl
& destPath
,
966 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
969 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
971 ViewProperties
props(viewPropertiesUrl());
972 props
.setSorting(sorting
);
974 m_proxyModel
->setSorting(sorting
);
976 emit
sortingChanged(sorting
);
979 void DolphinView::updateSortOrder(Qt::SortOrder order
)
981 ViewProperties
props(viewPropertiesUrl());
982 props
.setSortOrder(order
);
984 m_proxyModel
->setSortOrder(order
);
986 emit
sortOrderChanged(order
);
989 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
991 ViewProperties
props(viewPropertiesUrl());
992 props
.setAdditionalInfo(info
);
995 m_fileItemDelegate
->setShowInformation(info
);
997 emit
additionalInfoChanged();
1000 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1002 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1003 (m_mode
== DolphinView::IconsView
);
1005 QAction
* showSizeInfo
= collection
->action("show_size_info");
1006 QAction
* showDateInfo
= collection
->action("show_date_info");
1007 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1008 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1009 QAction
* showGroupInfo
= collection
->action("show_group_info");
1010 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1012 showSizeInfo
->setChecked(false);
1013 showDateInfo
->setChecked(false);
1014 showPermissionsInfo
->setChecked(false);
1015 showOwnerInfo
->setChecked(false);
1016 showGroupInfo
->setChecked(false);
1017 showMimeInfo
->setChecked(false);
1019 showSizeInfo
->setEnabled(enable
);
1020 showDateInfo
->setEnabled(enable
);
1021 showPermissionsInfo
->setEnabled(enable
);
1022 showOwnerInfo
->setEnabled(enable
);
1023 showGroupInfo
->setEnabled(enable
);
1024 showMimeInfo
->setEnabled(enable
);
1026 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1028 case KFileItemDelegate::Size
:
1029 showSizeInfo
->setChecked(true);
1031 case KFileItemDelegate::ModificationTime
:
1032 showDateInfo
->setChecked(true);
1034 case KFileItemDelegate::Permissions
:
1035 showPermissionsInfo
->setChecked(true);
1037 case KFileItemDelegate::Owner
:
1038 showOwnerInfo
->setChecked(true);
1040 case KFileItemDelegate::OwnerAndGroup
:
1041 showGroupInfo
->setChecked(true);
1043 case KFileItemDelegate::FriendlyMimeType
:
1044 showMimeInfo
->setChecked(true);
1052 QPair
<bool, QString
> DolphinView::pasteInfo() const
1054 return KonqOperations::pasteInfo(url());
1057 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1059 m_tabsForFiles
= tabsForFiles
;
1062 bool DolphinView::isTabsForFilesEnabled() const
1064 return m_tabsForFiles
;
1067 bool DolphinView::itemsExpandable() const
1069 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1072 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1077 if (DragAndDropHelper::instance().isDragSource(view
)) {
1078 // We must store for later deletion.
1079 if (m_expandedDragSource
!= 0) {
1080 // The old stored view is obviously not the drag source anymore.
1081 m_expandedDragSource
->deleteLater();
1082 m_expandedDragSource
= 0;
1085 m_expandedDragSource
= view
;
1088 view
->deleteLater();
1092 void DolphinView::observeCreatedItem(const KUrl
& url
)
1094 m_createdItemUrl
= url
;
1095 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1096 this, SLOT(selectAndScrollToCreatedItem()));
1099 void DolphinView::selectAndScrollToCreatedItem()
1101 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1102 if (dirIndex
.isValid()) {
1103 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1104 itemView()->setCurrentIndex(proxyIndex
);
1107 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1108 this, SLOT(selectAndScrollToCreatedItem()));
1109 m_createdItemUrl
= KUrl();
1112 void DolphinView::emitContentsMoved()
1114 // only emit the contents moved signal if:
1115 // - no directory loading is ongoing (this would reset the contents position
1116 // always to (0, 0))
1117 // - if the Column View is active: the column view does an automatic
1118 // positioning during the loading operation, which must be remembered
1119 if (!m_loadingDirectory
|| isColumnViewActive()) {
1120 const QPoint
pos(contentsPosition());
1121 emit
contentsMoved(pos
.x(), pos
.y());
1125 void DolphinView::showHoverInformation(const KFileItem
& item
)
1127 emit
requestItemInfo(item
);
1130 void DolphinView::clearHoverInformation()
1132 emit
requestItemInfo(KFileItem());
1135 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1137 if (job
->error() == 0) {
1138 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1139 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1140 emit
errorMessage(job
->errorString());
1144 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1146 emit
requestUrlChange(url
);
1147 m_controller
->setUrl(url
);
1150 void DolphinView::restoreCurrentItem()
1152 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1153 if (dirIndex
.isValid()) {
1154 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1155 QAbstractItemView
* view
= itemView();
1156 const bool clearSelection
= !hasSelection();
1157 view
->setCurrentIndex(proxyIndex
);
1158 if (clearSelection
) {
1159 view
->clearSelection();
1164 void DolphinView::slotRefreshItems()
1166 if (m_assureVisibleCurrentIndex
) {
1167 m_assureVisibleCurrentIndex
= false;
1168 itemView()->scrollTo(itemView()->currentIndex());
1172 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1174 if (!url
.isValid()) {
1175 const QString
location(url
.pathOrUrl());
1176 if (location
.isEmpty()) {
1177 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1179 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1184 m_loadingDirectory
= true;
1186 m_dirLister
->stop();
1187 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1189 if (isColumnViewActive()) {
1190 // adjusting the directory lister is not enough in the case of the
1191 // column view, as each column has its own directory lister internally...
1193 m_columnView
->reload();
1195 m_columnView
->showColumn(url
);
1200 KUrl
DolphinView::viewPropertiesUrl() const
1202 if (isColumnViewActive()) {
1203 return m_columnView
->rootUrl();
1209 void DolphinView::applyViewProperties(const KUrl
& url
)
1211 if (m_ignoreViewProperties
) {
1215 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1216 // The column view is active, hence don't apply the view properties
1217 // of sub directories (represented by columns) to the view. The
1218 // view always represents the properties of the first column.
1222 const ViewProperties
props(url
);
1224 const Mode mode
= props
.viewMode();
1225 if (m_mode
!= mode
) {
1226 const int oldZoomLevel
= m_controller
->zoomLevel();
1232 updateZoomLevel(oldZoomLevel
);
1234 if (itemView() == 0) {
1237 Q_ASSERT(itemView() != 0);
1238 Q_ASSERT(m_fileItemDelegate
!= 0);
1240 const bool showHiddenFiles
= props
.showHiddenFiles();
1241 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1242 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1243 emit
showHiddenFilesChanged();
1246 m_storedCategorizedSorting
= props
.categorizedSorting();
1247 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1248 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1249 m_proxyModel
->setCategorizedModel(categorized
);
1250 emit
categorizedSortingChanged();
1253 const DolphinView::Sorting sorting
= props
.sorting();
1254 if (sorting
!= m_proxyModel
->sorting()) {
1255 m_proxyModel
->setSorting(sorting
);
1256 emit
sortingChanged(sorting
);
1259 const Qt::SortOrder sortOrder
= props
.sortOrder();
1260 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1261 m_proxyModel
->setSortOrder(sortOrder
);
1262 emit
sortOrderChanged(sortOrder
);
1265 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1266 if (info
!= m_fileItemDelegate
->showInformation()) {
1267 m_fileItemDelegate
->setShowInformation(info
);
1268 emit
additionalInfoChanged();
1271 const bool showPreview
= props
.showPreview();
1272 if (showPreview
!= m_showPreview
) {
1273 m_showPreview
= showPreview
;
1274 m_previewGenerator
->setPreviewShown(showPreview
);
1276 const int oldZoomLevel
= m_controller
->zoomLevel();
1277 emit
showPreviewChanged();
1279 // Enabling or disabling the preview might change the icon size of the view.
1280 // As the view does not emit a signal when the icon size has been changed,
1281 // the used zoom level of the controller must be adjusted manually:
1282 updateZoomLevel(oldZoomLevel
);
1285 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1286 // During the lifetime of a DolphinView instance the global view properties
1287 // should not be changed. This allows e. g. to split a view and use different
1288 // view properties for each view.
1289 m_ignoreViewProperties
= true;
1293 void DolphinView::createView()
1296 Q_ASSERT(m_iconsView
== 0);
1297 Q_ASSERT(m_detailsView
== 0);
1298 Q_ASSERT(m_columnView
== 0);
1300 QAbstractItemView
* view
= 0;
1303 m_iconsView
= new DolphinIconsView(this, m_controller
);
1309 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1310 view
= m_detailsView
;
1314 m_columnView
= new DolphinColumnView(this, m_controller
);
1315 view
= m_columnView
;
1319 Q_ASSERT(view
!= 0);
1320 view
->installEventFilter(this);
1321 view
->viewport()->installEventFilter(this);
1322 setFocusProxy(view
);
1324 if (m_mode
!= ColumnView
) {
1325 // Give the view the ability to auto-expand its directories on hovering
1326 // (the column view takes care about this itself). If the details view
1327 // uses expandable folders, the auto-expanding should be used always.
1328 DolphinSettings
& settings
= DolphinSettings::instance();
1329 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1330 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1332 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1333 folderExpander
->setEnabled(enabled
);
1334 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1335 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1338 // Listen out for requests to delete the current column.
1339 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1340 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1343 m_controller
->setItemView(view
);
1345 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1346 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1347 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1348 view
->setItemDelegate(m_fileItemDelegate
);
1350 view
->setModel(m_proxyModel
);
1351 if (m_selectionModel
!= 0) {
1352 view
->setSelectionModel(m_selectionModel
);
1354 m_selectionModel
= view
->selectionModel();
1357 // reparent the selection model, as it should not be deleted
1358 // when deleting the model
1359 m_selectionModel
->setParent(this);
1361 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1363 m_previewGenerator
= new KFilePreviewGenerator(view
);
1364 m_previewGenerator
->setPreviewShown(m_showPreview
);
1366 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1367 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1368 connect(m_controller
, SIGNAL(hideToolTip()),
1369 m_toolTipManager
, SLOT(hideTip()));
1372 m_topLayout
->insertWidget(1, view
);
1374 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1375 this, SLOT(emitSelectionChangedSignal()));
1376 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1377 this, SLOT(emitContentsMoved()));
1378 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1379 this, SLOT(emitContentsMoved()));
1382 void DolphinView::deleteView()
1384 QAbstractItemView
* view
= itemView();
1386 // It's important to set the keyboard focus to the parent
1387 // before deleting the view: Otherwise when having a split
1388 // view the other view will get the focus and will request
1389 // an activation (see DolphinView::eventFilter()).
1393 m_topLayout
->removeWidget(view
);
1396 // m_previewGenerator's parent is not always destroyed, and we
1397 // don't want two active at once - manually delete.
1398 delete m_previewGenerator
;
1399 m_previewGenerator
= 0;
1402 m_controller
->disconnect(view
);
1405 deleteWhenNotDragSource(view
);
1411 m_fileItemDelegate
= 0;
1412 m_toolTipManager
= 0;
1416 QAbstractItemView
* DolphinView::itemView() const
1418 if (m_detailsView
!= 0) {
1419 return m_detailsView
;
1420 } else if (m_columnView
!= 0) {
1421 return m_columnView
;
1427 bool DolphinView::isCutItem(const KFileItem
& item
) const
1429 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1430 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1432 const KUrl
& itemUrl
= item
.url();
1433 KUrl::List::const_iterator it
= cutUrls
.begin();
1434 const KUrl::List::const_iterator end
= cutUrls
.end();
1436 if (*it
== itemUrl
) {
1445 void DolphinView::pasteToUrl(const KUrl
& url
)
1447 KonqOperations::doPaste(this, url
);
1450 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1452 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1453 if (oldZoomLevel
!= newZoomLevel
) {
1454 m_controller
->setZoomLevel(newZoomLevel
);
1455 emit
zoomLevelChanged(newZoomLevel
);
1459 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1461 KUrl::List list
= selectedUrls();
1462 if (itemsExpandable() ) {
1463 list
= KDirModel::simplifiedUrlList(list
);
1468 QMimeData
* DolphinView::selectionMimeData() const
1470 if (isColumnViewActive()) {
1471 return m_columnView
->selectionMimeData();
1474 const QAbstractItemView
* view
= itemView();
1475 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1476 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1477 return m_dolphinModel
->mimeData(selection
.indexes());
1480 #include "dolphinview.moc"