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),
110 m_expandedDragSource(0)
112 m_topLayout
= new QVBoxLayout(this);
113 m_topLayout
->setSpacing(0);
114 m_topLayout
->setMargin(0);
116 m_controller
= new DolphinController(this);
117 m_controller
->setUrl(url
);
119 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
120 this, SIGNAL(urlChanged(const KUrl
&)));
121 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
122 this, SLOT(slotRequestUrlChange(const KUrl
&)));
124 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
125 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
126 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
127 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
128 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
129 this, SLOT(updateSorting(DolphinView::Sorting
)));
130 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
131 this, SLOT(updateSortOrder(Qt::SortOrder
)));
132 connect(m_controller
, SIGNAL(sortFoldersFirstChanged(bool)),
133 this, SLOT(updateSortFoldersFirst(bool)));
134 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
135 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
136 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
137 this, SLOT(triggerItem(const KFileItem
&)));
138 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
139 this, SIGNAL(tabRequested(const KUrl
&)));
140 connect(m_controller
, SIGNAL(activated()),
141 this, SLOT(activate()));
142 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
143 this, SLOT(showHoverInformation(const KFileItem
&)));
144 connect(m_controller
, SIGNAL(viewportEntered()),
145 this, SLOT(clearHoverInformation()));
147 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
148 this, SIGNAL(redirection(KUrl
, KUrl
)));
149 connect(m_dirLister
, SIGNAL(completed()),
150 this, SLOT(restoreCurrentItem()));
151 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
152 this, SLOT(slotRefreshItems()));
154 // When a new item has been created by the "Create New..." menu, the item should
155 // get selected and it must be assured that the item will get visible. As the
156 // creation is done asynchronously, several signals must be checked:
157 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
158 this, SLOT(observeCreatedItem(const KUrl
&)));
160 applyViewProperties(url
);
161 m_topLayout
->addWidget(itemView());
164 DolphinView::~DolphinView()
166 delete m_expandedDragSource
;
167 m_expandedDragSource
= 0;
170 const KUrl
& DolphinView::url() const
172 return m_controller
->url();
175 KUrl
DolphinView::rootUrl() const
177 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
180 void DolphinView::setActive(bool active
)
182 if (active
== m_active
) {
188 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
190 emit
selectionChanged(selectedItems());
195 QWidget
* viewport
= itemView()->viewport();
197 palette
.setColor(viewport
->backgroundRole(), color
);
198 viewport
->setPalette(palette
);
203 itemView()->setFocus();
207 m_controller
->indicateActivationChange(active
);
210 bool DolphinView::isActive() const
215 void DolphinView::setMode(Mode mode
)
217 if (mode
== m_mode
) {
218 return; // the wished mode is already set
221 const int oldZoomLevel
= m_controller
->zoomLevel();
226 const KUrl viewPropsUrl
= viewPropertiesUrl();
227 ViewProperties
props(viewPropsUrl
);
228 props
.setViewMode(m_mode
);
231 // the file item delegate has been recreated, apply the current
232 // additional information manually
233 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
234 m_fileItemDelegate
->setShowInformation(infoList
);
235 emit
additionalInfoChanged();
237 // Not all view modes support categorized sorting. Adjust the sorting model
238 // if changing the view mode results in a change of the categorized sorting
240 m_storedCategorizedSorting
= props
.categorizedSorting();
241 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
242 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
243 m_proxyModel
->setCategorizedModel(categorized
);
244 emit
categorizedSortingChanged();
249 updateZoomLevel(oldZoomLevel
);
251 loadDirectory(viewPropsUrl
);
255 DolphinView::Mode
DolphinView::mode() const
260 bool DolphinView::showPreview() const
262 return m_showPreview
;
265 bool DolphinView::showHiddenFiles() const
267 return m_dirLister
->showingDotFiles();
270 bool DolphinView::categorizedSorting() const
272 // If all view modes would support categorized sorting, returning
273 // m_proxyModel->isCategorizedModel() would be the way to go. As
274 // currently only the icons view supports caterized sorting, we remember
275 // the stored view properties state in m_storedCategorizedSorting and
276 // return this state. The application takes care to disable the corresponding
277 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
278 // that this setting is not applied to the current view mode.
279 return m_storedCategorizedSorting
;
282 bool DolphinView::supportsCategorizedSorting() const
284 return m_iconsView
!= 0;
287 void DolphinView::selectAll()
289 QAbstractItemView
* view
= itemView();
290 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
291 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
292 // selection instead of selecting all items. This is bypassed for KDE 4.0
293 // by invoking clearSelection() first.
294 view
->clearSelection();
298 void DolphinView::invertSelection()
300 if (isColumnViewActive()) {
301 // QAbstractItemView does not offer a virtual method invertSelection()
302 // as counterpart to QAbstractItemView::selectAll(). This makes it
303 // necessary to delegate the inverting of the selection to the
304 // column view, as only the selection of the active column should
306 m_columnView
->invertSelection();
308 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
309 const QAbstractItemModel
* itemModel
= selectionModel
->model();
311 const QModelIndex topLeft
= itemModel
->index(0, 0);
312 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
313 itemModel
->columnCount() - 1);
315 const QItemSelection
selection(topLeft
, bottomRight
);
316 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
320 bool DolphinView::hasSelection() const
322 return itemView()->selectionModel()->hasSelection();
325 void DolphinView::clearSelection()
327 QItemSelectionModel
* selModel
= itemView()->selectionModel();
328 const QModelIndex currentIndex
= selModel
->currentIndex();
329 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
330 QItemSelectionModel::Clear
);
333 KFileItemList
DolphinView::selectedItems() const
335 if (isColumnViewActive()) {
336 return m_columnView
->selectedItems();
339 const QAbstractItemView
* view
= itemView();
341 // Our view has a selection, we will map them back to the DolphinModel
342 // and then fill the KFileItemList.
343 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
345 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
346 KFileItemList itemList
;
348 const QModelIndexList indexList
= selection
.indexes();
349 foreach (const QModelIndex
&index
, indexList
) {
350 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
351 if (!item
.isNull()) {
352 itemList
.append(item
);
359 KUrl::List
DolphinView::selectedUrls() const
362 const KFileItemList list
= selectedItems();
363 foreach (const KFileItem
&item
, list
) {
364 urls
.append(item
.url());
369 int DolphinView::selectedItemsCount() const
371 if (isColumnViewActive()) {
372 // TODO: get rid of this special case by adjusting the dir lister
373 // to the current column
374 return m_columnView
->selectedItems().count();
377 return itemView()->selectionModel()->selection().count();
380 void DolphinView::setContentsPosition(int x
, int y
)
382 QAbstractItemView
* view
= itemView();
384 // the ColumnView takes care itself for the horizontal scrolling
385 if (!isColumnViewActive()) {
386 view
->horizontalScrollBar()->setValue(x
);
388 view
->verticalScrollBar()->setValue(y
);
390 m_loadingDirectory
= false;
393 QPoint
DolphinView::contentsPosition() const
395 const int x
= itemView()->horizontalScrollBar()->value();
396 const int y
= itemView()->verticalScrollBar()->value();
400 void DolphinView::setZoomLevel(int level
)
402 if (level
< ZoomLevelInfo::minimumLevel()) {
403 level
= ZoomLevelInfo::minimumLevel();
404 } else if (level
> ZoomLevelInfo::maximumLevel()) {
405 level
= ZoomLevelInfo::maximumLevel();
408 if (level
!= zoomLevel()) {
409 m_controller
->setZoomLevel(level
);
410 m_previewGenerator
->updateIcons();
411 emit
zoomLevelChanged(level
);
415 int DolphinView::zoomLevel() const
417 return m_controller
->zoomLevel();
420 void DolphinView::setSorting(Sorting sorting
)
422 if (sorting
!= this->sorting()) {
423 updateSorting(sorting
);
427 DolphinView::Sorting
DolphinView::sorting() const
429 return m_proxyModel
->sorting();
432 void DolphinView::setSortOrder(Qt::SortOrder order
)
434 if (sortOrder() != order
) {
435 updateSortOrder(order
);
439 Qt::SortOrder
DolphinView::sortOrder() const
441 return m_proxyModel
->sortOrder();
444 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
446 if (sortFoldersFirst() != foldersFirst
) {
447 updateSortFoldersFirst(foldersFirst
);
451 bool DolphinView::sortFoldersFirst() const
453 return m_proxyModel
->sortFoldersFirst();
456 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
458 const KUrl viewPropsUrl
= viewPropertiesUrl();
459 ViewProperties
props(viewPropsUrl
);
460 props
.setAdditionalInfo(info
);
461 m_fileItemDelegate
->setShowInformation(info
);
463 emit
additionalInfoChanged();
465 if (itemView() != m_detailsView
) {
466 // the details view requires no reloading of the directory, as it maps
467 // the file item delegate info to its columns internally
468 loadDirectory(viewPropsUrl
);
472 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
474 return m_fileItemDelegate
->showInformation();
477 void DolphinView::reload()
480 loadDirectory(url(), true);
483 void DolphinView::refresh()
485 m_ignoreViewProperties
= false;
487 const bool oldActivationState
= m_active
;
488 const int oldZoomLevel
= m_controller
->zoomLevel();
492 applyViewProperties(m_controller
->url());
495 setActive(oldActivationState
);
496 updateZoomLevel(oldZoomLevel
);
499 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
501 if (m_controller
->url() == url
) {
505 m_previewGenerator
->cancelPreviews();
506 m_controller
->setUrl(url
); // emits urlChanged, which we forward
508 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
509 applyViewProperties(rootUrl
);
510 loadDirectory(rootUrl
);
511 if (itemView() == m_columnView
) {
512 m_columnView
->setRootUrl(rootUrl
);
513 m_columnView
->showColumn(url
);
516 applyViewProperties(url
);
520 emit
startedPathLoading(url
);
523 void DolphinView::setNameFilter(const QString
& nameFilter
)
525 m_proxyModel
->setFilterRegExp(nameFilter
);
527 if (isColumnViewActive()) {
528 // adjusting the directory lister is not enough in the case of the
529 // column view, as each column has its own directory lister internally...
530 m_columnView
->setNameFilter(nameFilter
);
534 void DolphinView::calculateItemCount(int& fileCount
,
536 KIO::filesize_t
& totalFileSize
) const
538 foreach (const KFileItem
& item
, m_dirLister
->items()) {
543 totalFileSize
+= item
.size();
548 QString
DolphinView::statusBarText() const
553 KIO::filesize_t totalFileSize
= 0;
555 if (hasSelection()) {
556 // give a summary of the status of the selected files
557 const KFileItemList list
= selectedItems();
558 if (list
.isEmpty()) {
559 // when an item is triggered, it is temporary selected but selectedItems()
560 // will return an empty list
564 KFileItemList::const_iterator it
= list
.begin();
565 const KFileItemList::const_iterator end
= list
.end();
567 const KFileItem
& item
= *it
;
572 totalFileSize
+= item
.size();
577 if (folderCount
+ fileCount
== 1) {
578 // if only one item is selected, show the filename
579 const QString name
= list
.first().name();
580 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
581 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
582 name
, KIO::convertSize(totalFileSize
));
584 // at least 2 items are selected
585 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
586 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
587 if ((folderCount
> 0) && (fileCount
> 0)) {
588 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
589 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
590 } else if (fileCount
> 0) {
591 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
593 Q_ASSERT(folderCount
> 0);
598 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
599 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
600 fileCount
, folderCount
,
601 totalFileSize
, true);
607 void DolphinView::setUrl(const KUrl
& url
)
609 // remember current item candidate (see restoreCurrentItem())
610 m_currentItemUrl
= url
;
611 updateView(url
, KUrl());
614 void DolphinView::changeSelection(const KFileItemList
& selection
)
617 if (selection
.isEmpty()) {
620 const KUrl
& baseUrl
= url();
622 QItemSelection newSelection
;
623 foreach(const KFileItem
& item
, selection
) {
624 url
= item
.url().upUrl();
625 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
626 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
627 newSelection
.select(index
, index
);
630 itemView()->selectionModel()->select(newSelection
,
631 QItemSelectionModel::ClearAndSelect
632 | QItemSelectionModel::Current
);
635 void DolphinView::renameSelectedItems()
637 KFileItemList items
= selectedItems();
638 const int itemCount
= items
.count();
644 // More than one item has been selected for renaming. Open
645 // a rename dialog and rename all items afterwards.
646 RenameDialog
dialog(this, items
);
647 if (dialog
.exec() == QDialog::Rejected
) {
651 const QString newName
= dialog
.newName();
652 if (newName
.isEmpty()) {
653 emit
errorMessage(dialog
.errorString());
657 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
658 // as one operation instead of n rename operations like it is done now...
659 Q_ASSERT(newName
.contains('#'));
661 // currently the items are sorted by the selection order, resort
662 // them by the file name
663 qSort(items
.begin(), items
.end(), lessThan
);
665 // iterate through all selected items and rename them...
667 foreach (const KFileItem
& item
, items
) {
668 const KUrl
& oldUrl
= item
.url();
670 number
.setNum(index
++);
672 QString name
= newName
;
673 name
.replace('#', number
);
675 if (oldUrl
.fileName() != name
) {
676 KUrl newUrl
= oldUrl
;
677 newUrl
.setFileName(name
);
678 KonqOperations::rename(this, oldUrl
, newUrl
);
681 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
682 Q_ASSERT(itemCount
== 1);
684 if (isColumnViewActive()) {
685 m_columnView
->editItem(items
.first());
687 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
688 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
689 itemView()->edit(proxyIndex
);
692 Q_ASSERT(itemCount
== 1);
694 RenameDialog
dialog(this, items
);
695 if (dialog
.exec() == QDialog::Rejected
) {
699 const QString
& newName
= dialog
.newName();
700 if (newName
.isEmpty()) {
701 emit
errorMessage(dialog
.errorString());
705 const KUrl
& oldUrl
= items
.first().url();
706 KUrl newUrl
= oldUrl
;
707 newUrl
.setFileName(newName
);
708 KonqOperations::rename(this, oldUrl
, newUrl
);
711 // assure that the current index remains visible when KDirLister
712 // will notify the view about changed items
713 m_assureVisibleCurrentIndex
= true;
716 void DolphinView::trashSelectedItems()
718 const KUrl::List list
= simplifiedSelectedUrls();
719 KonqOperations::del(this, KonqOperations::TRASH
, list
);
722 void DolphinView::deleteSelectedItems()
724 const KUrl::List list
= simplifiedSelectedUrls();
725 const bool del
= KonqOperations::askDeleteConfirmation(list
,
727 KonqOperations::DEFAULT_CONFIRMATION
,
731 KIO::Job
* job
= KIO::del(list
);
732 connect(job
, SIGNAL(result(KJob
*)),
733 this, SLOT(slotDeleteFileFinished(KJob
*)));
737 void DolphinView::cutSelectedItems()
739 QMimeData
* mimeData
= selectionMimeData();
740 KonqMimeData::addIsCutSelection(mimeData
, true);
741 QApplication::clipboard()->setMimeData(mimeData
);
744 void DolphinView::copySelectedItems()
746 QMimeData
* mimeData
= selectionMimeData();
747 QApplication::clipboard()->setMimeData(mimeData
);
750 void DolphinView::paste()
755 void DolphinView::pasteIntoFolder()
757 const KFileItemList items
= selectedItems();
758 if ((items
.count() == 1) && items
.first().isDir()) {
759 pasteToUrl(items
.first().url());
763 void DolphinView::setShowPreview(bool show
)
765 if (m_showPreview
== show
) {
769 const KUrl viewPropsUrl
= viewPropertiesUrl();
770 ViewProperties
props(viewPropsUrl
);
771 props
.setShowPreview(show
);
773 m_showPreview
= show
;
774 m_previewGenerator
->setPreviewShown(show
);
776 const int oldZoomLevel
= m_controller
->zoomLevel();
777 emit
showPreviewChanged();
779 // Enabling or disabling the preview might change the icon size of the view.
780 // As the view does not emit a signal when the icon size has been changed,
781 // the used zoom level of the controller must be adjusted manually:
782 updateZoomLevel(oldZoomLevel
);
784 loadDirectory(viewPropsUrl
);
787 void DolphinView::setShowHiddenFiles(bool show
)
789 if (m_dirLister
->showingDotFiles() == show
) {
793 const KUrl viewPropsUrl
= viewPropertiesUrl();
794 ViewProperties
props(viewPropsUrl
);
795 props
.setShowHiddenFiles(show
);
797 m_dirLister
->setShowingDotFiles(show
);
798 emit
showHiddenFilesChanged();
800 loadDirectory(viewPropsUrl
);
803 void DolphinView::setCategorizedSorting(bool categorized
)
805 if (categorized
== categorizedSorting()) {
809 // setCategorizedSorting(true) may only get invoked
810 // if the view supports categorized sorting
811 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
813 ViewProperties
props(viewPropertiesUrl());
814 props
.setCategorizedSorting(categorized
);
817 m_storedCategorizedSorting
= categorized
;
818 m_proxyModel
->setCategorizedModel(categorized
);
820 emit
categorizedSortingChanged();
823 void DolphinView::toggleSortOrder()
825 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
826 Qt::DescendingOrder
:
831 void DolphinView::toggleSortFoldersFirst()
833 setSortFoldersFirst(!sortFoldersFirst());
836 void DolphinView::toggleAdditionalInfo(QAction
* action
)
838 const KFileItemDelegate::Information info
=
839 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
841 KFileItemDelegate::InformationList list
= additionalInfo();
843 const bool show
= action
->isChecked();
845 const int index
= list
.indexOf(info
);
846 const bool containsInfo
= (index
>= 0);
847 if (show
&& !containsInfo
) {
849 setAdditionalInfo(list
);
850 } else if (!show
&& containsInfo
) {
851 list
.removeAt(index
);
852 setAdditionalInfo(list
);
853 Q_ASSERT(list
.indexOf(info
) < 0);
857 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
859 QWidget::mouseReleaseEvent(event
);
863 void DolphinView::wheelEvent(QWheelEvent
* event
)
865 if (event
->modifiers() & Qt::ControlModifier
) {
866 const int delta
= event
->delta();
867 const int level
= zoomLevel();
869 setZoomLevel(level
+ 1);
870 } else if (delta
< 0) {
871 setZoomLevel(level
- 1);
877 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
879 switch (event
->type()) {
880 case QEvent::FocusIn
:
881 if (watched
== itemView()) {
882 m_controller
->requestActivation();
886 case QEvent::MouseButtonPress
:
887 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
888 // Listening to a mousebutton press event to delete expanded views is a
889 // workaround, as it seems impossible for the FolderExpander to know when
890 // a dragging outside a view has been finished. However it works quite well:
891 // A mousebutton press event indicates that a drag operation must be
893 m_expandedDragSource
->deleteLater();
894 m_expandedDragSource
= 0;
898 case QEvent::DragEnter
:
899 if (watched
== itemView()->viewport()) {
904 case QEvent::KeyPress
:
905 if (watched
== itemView()) {
906 if (m_toolTipManager
!= 0) {
907 m_toolTipManager
->hideTip();
910 // clear the selection when Escape has been pressed
911 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
912 if (keyEvent
->key() == Qt::Key_Escape
) {
922 return QWidget::eventFilter(watched
, event
);
925 void DolphinView::activate()
930 void DolphinView::triggerItem(const KFileItem
& item
)
932 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
933 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
934 // items are selected by the user, hence don't trigger the
935 // item specified by 'index'
939 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
940 if (item
.isNull() || m_isContextMenuOpen
) {
944 if (m_toolTipManager
!= 0) {
945 m_toolTipManager
->hideTip();
947 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
950 void DolphinView::emitSelectionChangedSignal()
952 emit
selectionChanged(DolphinView::selectedItems());
955 void DolphinView::openContextMenu(const QPoint
& pos
,
956 const QList
<QAction
*>& customActions
)
959 if (isColumnViewActive()) {
960 item
= m_columnView
->itemAt(pos
);
962 const QModelIndex index
= itemView()->indexAt(pos
);
963 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
964 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
965 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
969 if (m_toolTipManager
!= 0) {
970 m_toolTipManager
->hideTip();
973 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
974 emit
requestContextMenu(item
, url(), customActions
);
975 m_isContextMenuOpen
= false;
978 void DolphinView::dropUrls(const KFileItem
& destItem
,
979 const KUrl
& destPath
,
982 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
985 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
987 ViewProperties
props(viewPropertiesUrl());
988 props
.setSorting(sorting
);
990 m_proxyModel
->setSorting(sorting
);
992 emit
sortingChanged(sorting
);
995 void DolphinView::updateSortOrder(Qt::SortOrder order
)
997 ViewProperties
props(viewPropertiesUrl());
998 props
.setSortOrder(order
);
1000 m_proxyModel
->setSortOrder(order
);
1002 emit
sortOrderChanged(order
);
1005 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1007 ViewProperties
props(viewPropertiesUrl());
1008 props
.setSortFoldersFirst(foldersFirst
);
1010 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1012 emit
sortFoldersFirstChanged(foldersFirst
);
1015 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1017 ViewProperties
props(viewPropertiesUrl());
1018 props
.setAdditionalInfo(info
);
1021 m_fileItemDelegate
->setShowInformation(info
);
1023 emit
additionalInfoChanged();
1026 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1028 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1029 (m_mode
== DolphinView::IconsView
);
1031 QAction
* showSizeInfo
= collection
->action("show_size_info");
1032 QAction
* showDateInfo
= collection
->action("show_date_info");
1033 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1034 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1035 QAction
* showGroupInfo
= collection
->action("show_group_info");
1036 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1038 showSizeInfo
->setChecked(false);
1039 showDateInfo
->setChecked(false);
1040 showPermissionsInfo
->setChecked(false);
1041 showOwnerInfo
->setChecked(false);
1042 showGroupInfo
->setChecked(false);
1043 showMimeInfo
->setChecked(false);
1045 showSizeInfo
->setEnabled(enable
);
1046 showDateInfo
->setEnabled(enable
);
1047 showPermissionsInfo
->setEnabled(enable
);
1048 showOwnerInfo
->setEnabled(enable
);
1049 showGroupInfo
->setEnabled(enable
);
1050 showMimeInfo
->setEnabled(enable
);
1052 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1054 case KFileItemDelegate::Size
:
1055 showSizeInfo
->setChecked(true);
1057 case KFileItemDelegate::ModificationTime
:
1058 showDateInfo
->setChecked(true);
1060 case KFileItemDelegate::Permissions
:
1061 showPermissionsInfo
->setChecked(true);
1063 case KFileItemDelegate::Owner
:
1064 showOwnerInfo
->setChecked(true);
1066 case KFileItemDelegate::OwnerAndGroup
:
1067 showGroupInfo
->setChecked(true);
1069 case KFileItemDelegate::FriendlyMimeType
:
1070 showMimeInfo
->setChecked(true);
1078 QPair
<bool, QString
> DolphinView::pasteInfo() const
1080 return KonqOperations::pasteInfo(url());
1083 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1085 m_tabsForFiles
= tabsForFiles
;
1088 bool DolphinView::isTabsForFilesEnabled() const
1090 return m_tabsForFiles
;
1093 bool DolphinView::itemsExpandable() const
1095 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1098 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1103 if (DragAndDropHelper::instance().isDragSource(view
)) {
1104 // We must store for later deletion.
1105 if (m_expandedDragSource
!= 0) {
1106 // The old stored view is obviously not the drag source anymore.
1107 m_expandedDragSource
->deleteLater();
1108 m_expandedDragSource
= 0;
1111 m_expandedDragSource
= view
;
1114 view
->deleteLater();
1118 void DolphinView::observeCreatedItem(const KUrl
& url
)
1120 m_createdItemUrl
= url
;
1121 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1122 this, SLOT(selectAndScrollToCreatedItem()));
1125 void DolphinView::selectAndScrollToCreatedItem()
1127 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1128 if (dirIndex
.isValid()) {
1129 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1130 itemView()->setCurrentIndex(proxyIndex
);
1133 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1134 this, SLOT(selectAndScrollToCreatedItem()));
1135 m_createdItemUrl
= KUrl();
1138 void DolphinView::restoreSelection()
1140 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1141 changeSelection(m_selectedItems
);
1144 void DolphinView::emitContentsMoved()
1146 // only emit the contents moved signal if:
1147 // - no directory loading is ongoing (this would reset the contents position
1148 // always to (0, 0))
1149 // - if the Column View is active: the column view does an automatic
1150 // positioning during the loading operation, which must be remembered
1151 if (!m_loadingDirectory
|| isColumnViewActive()) {
1152 const QPoint
pos(contentsPosition());
1153 emit
contentsMoved(pos
.x(), pos
.y());
1157 void DolphinView::showHoverInformation(const KFileItem
& item
)
1159 emit
requestItemInfo(item
);
1162 void DolphinView::clearHoverInformation()
1164 emit
requestItemInfo(KFileItem());
1167 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1169 if (job
->error() == 0) {
1170 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1171 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1172 emit
errorMessage(job
->errorString());
1176 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1178 emit
requestUrlChange(url
);
1179 m_controller
->setUrl(url
);
1182 void DolphinView::restoreCurrentItem()
1184 if (!m_currentItemUrl
.isEmpty()) {
1185 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1186 if (dirIndex
.isValid()) {
1187 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1188 QAbstractItemView
* view
= itemView();
1189 const bool clearSelection
= !hasSelection();
1190 view
->setCurrentIndex(proxyIndex
);
1191 if (clearSelection
) {
1192 view
->clearSelection();
1195 m_currentItemUrl
.clear();
1199 void DolphinView::slotRefreshItems()
1201 if (m_assureVisibleCurrentIndex
) {
1202 m_assureVisibleCurrentIndex
= false;
1203 itemView()->scrollTo(itemView()->currentIndex());
1207 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1209 if (!url
.isValid()) {
1210 const QString
location(url
.pathOrUrl());
1211 if (location
.isEmpty()) {
1212 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1214 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1219 m_loadingDirectory
= true;
1222 m_selectedItems
= selectedItems();
1223 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1226 m_dirLister
->stop();
1227 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1229 if (isColumnViewActive()) {
1230 // adjusting the directory lister is not enough in the case of the
1231 // column view, as each column has its own directory lister internally...
1233 m_columnView
->reload();
1235 m_columnView
->showColumn(url
);
1240 KUrl
DolphinView::viewPropertiesUrl() const
1242 if (isColumnViewActive()) {
1243 return m_columnView
->rootUrl();
1249 void DolphinView::applyViewProperties(const KUrl
& url
)
1251 if (m_ignoreViewProperties
) {
1255 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1256 // The column view is active, hence don't apply the view properties
1257 // of sub directories (represented by columns) to the view. The
1258 // view always represents the properties of the first column.
1262 const ViewProperties
props(url
);
1264 const Mode mode
= props
.viewMode();
1265 if (m_mode
!= mode
) {
1266 const int oldZoomLevel
= m_controller
->zoomLevel();
1272 updateZoomLevel(oldZoomLevel
);
1274 if (itemView() == 0) {
1277 Q_ASSERT(itemView() != 0);
1278 Q_ASSERT(m_fileItemDelegate
!= 0);
1280 const bool showHiddenFiles
= props
.showHiddenFiles();
1281 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1282 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1283 emit
showHiddenFilesChanged();
1286 m_storedCategorizedSorting
= props
.categorizedSorting();
1287 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1288 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1289 m_proxyModel
->setCategorizedModel(categorized
);
1290 emit
categorizedSortingChanged();
1293 const DolphinView::Sorting sorting
= props
.sorting();
1294 if (sorting
!= m_proxyModel
->sorting()) {
1295 m_proxyModel
->setSorting(sorting
);
1296 emit
sortingChanged(sorting
);
1299 const Qt::SortOrder sortOrder
= props
.sortOrder();
1300 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1301 m_proxyModel
->setSortOrder(sortOrder
);
1302 emit
sortOrderChanged(sortOrder
);
1305 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1306 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1307 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1308 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1311 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1312 if (info
!= m_fileItemDelegate
->showInformation()) {
1313 m_fileItemDelegate
->setShowInformation(info
);
1314 emit
additionalInfoChanged();
1317 const bool showPreview
= props
.showPreview();
1318 if (showPreview
!= m_showPreview
) {
1319 m_showPreview
= showPreview
;
1320 m_previewGenerator
->setPreviewShown(showPreview
);
1322 const int oldZoomLevel
= m_controller
->zoomLevel();
1323 emit
showPreviewChanged();
1325 // Enabling or disabling the preview might change the icon size of the view.
1326 // As the view does not emit a signal when the icon size has been changed,
1327 // the used zoom level of the controller must be adjusted manually:
1328 updateZoomLevel(oldZoomLevel
);
1331 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1332 // During the lifetime of a DolphinView instance the global view properties
1333 // should not be changed. This allows e. g. to split a view and use different
1334 // view properties for each view.
1335 m_ignoreViewProperties
= true;
1339 void DolphinView::createView()
1342 Q_ASSERT(m_iconsView
== 0);
1343 Q_ASSERT(m_detailsView
== 0);
1344 Q_ASSERT(m_columnView
== 0);
1346 QAbstractItemView
* view
= 0;
1349 m_iconsView
= new DolphinIconsView(this, m_controller
);
1355 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1356 view
= m_detailsView
;
1360 m_columnView
= new DolphinColumnView(this, m_controller
);
1361 view
= m_columnView
;
1365 Q_ASSERT(view
!= 0);
1366 view
->installEventFilter(this);
1367 view
->viewport()->installEventFilter(this);
1368 setFocusProxy(view
);
1370 if (m_mode
!= ColumnView
) {
1371 // Give the view the ability to auto-expand its directories on hovering
1372 // (the column view takes care about this itself). If the details view
1373 // uses expandable folders, the auto-expanding should be used always.
1374 DolphinSettings
& settings
= DolphinSettings::instance();
1375 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1376 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1378 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1379 folderExpander
->setEnabled(enabled
);
1380 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1381 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1384 // Listen out for requests to delete the current column.
1385 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1386 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1389 m_controller
->setItemView(view
);
1391 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1392 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1393 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1394 view
->setItemDelegate(m_fileItemDelegate
);
1396 view
->setModel(m_proxyModel
);
1397 if (m_selectionModel
!= 0) {
1398 view
->setSelectionModel(m_selectionModel
);
1400 m_selectionModel
= view
->selectionModel();
1403 // reparent the selection model, as it should not be deleted
1404 // when deleting the model
1405 m_selectionModel
->setParent(this);
1407 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1409 m_previewGenerator
= new KFilePreviewGenerator(view
);
1410 m_previewGenerator
->setPreviewShown(m_showPreview
);
1412 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1413 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1414 connect(m_controller
, SIGNAL(hideToolTip()),
1415 m_toolTipManager
, SLOT(hideTip()));
1418 m_topLayout
->insertWidget(1, view
);
1420 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1421 this, SLOT(emitSelectionChangedSignal()));
1422 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1423 this, SLOT(emitContentsMoved()));
1424 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1425 this, SLOT(emitContentsMoved()));
1428 void DolphinView::deleteView()
1430 QAbstractItemView
* view
= itemView();
1432 // It's important to set the keyboard focus to the parent
1433 // before deleting the view: Otherwise when having a split
1434 // view the other view will get the focus and will request
1435 // an activation (see DolphinView::eventFilter()).
1439 m_topLayout
->removeWidget(view
);
1442 // m_previewGenerator's parent is not always destroyed, and we
1443 // don't want two active at once - manually delete.
1444 delete m_previewGenerator
;
1445 m_previewGenerator
= 0;
1448 m_controller
->disconnect(view
);
1451 deleteWhenNotDragSource(view
);
1457 m_fileItemDelegate
= 0;
1458 m_toolTipManager
= 0;
1462 QAbstractItemView
* DolphinView::itemView() const
1464 if (m_detailsView
!= 0) {
1465 return m_detailsView
;
1466 } else if (m_columnView
!= 0) {
1467 return m_columnView
;
1473 void DolphinView::pasteToUrl(const KUrl
& url
)
1475 KonqOperations::doPaste(this, url
);
1478 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1480 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1481 if (oldZoomLevel
!= newZoomLevel
) {
1482 m_controller
->setZoomLevel(newZoomLevel
);
1483 emit
zoomLevelChanged(newZoomLevel
);
1487 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1489 KUrl::List list
= selectedUrls();
1490 if (itemsExpandable() ) {
1491 list
= KDirModel::simplifiedUrlList(list
);
1496 QMimeData
* DolphinView::selectionMimeData() const
1498 if (isColumnViewActive()) {
1499 return m_columnView
->selectionMimeData();
1502 const QAbstractItemView
* view
= itemView();
1503 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1504 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1505 return m_dolphinModel
->mimeData(selection
.indexes());
1509 #include "dolphinview.moc"