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(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
133 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
134 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
135 this, SLOT(triggerItem(const KFileItem
&)));
136 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
137 this, SIGNAL(tabRequested(const KUrl
&)));
138 connect(m_controller
, SIGNAL(activated()),
139 this, SLOT(activate()));
140 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
141 this, SLOT(showHoverInformation(const KFileItem
&)));
142 connect(m_controller
, SIGNAL(viewportEntered()),
143 this, SLOT(clearHoverInformation()));
145 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
146 this, SIGNAL(redirection(KUrl
, KUrl
)));
147 connect(m_dirLister
, SIGNAL(completed()),
148 this, SLOT(restoreCurrentItem()));
149 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
150 this, SLOT(slotRefreshItems()));
152 // When a new item has been created by the "Create New..." menu, the item should
153 // get selected and it must be assured that the item will get visible. As the
154 // creation is done asynchronously, several signals must be checked:
155 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
156 this, SLOT(observeCreatedItem(const KUrl
&)));
158 applyViewProperties(url
);
159 m_topLayout
->addWidget(itemView());
162 DolphinView::~DolphinView()
164 delete m_expandedDragSource
;
165 m_expandedDragSource
= 0;
168 const KUrl
& DolphinView::url() const
170 return m_controller
->url();
173 KUrl
DolphinView::rootUrl() const
175 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
178 void DolphinView::setActive(bool active
)
180 if (active
== m_active
) {
186 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
188 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
189 // bypasses the problem when having a split view and changing the active view to
190 // update the some URL dependent states. A nicer approach should be no big deal...
191 emit
urlChanged(url());
192 emit
selectionChanged(selectedItems());
197 QWidget
* viewport
= itemView()->viewport();
199 palette
.setColor(viewport
->backgroundRole(), color
);
200 viewport
->setPalette(palette
);
205 itemView()->setFocus();
209 m_controller
->indicateActivationChange(active
);
212 bool DolphinView::isActive() const
217 void DolphinView::setMode(Mode mode
)
219 if (mode
== m_mode
) {
220 return; // the wished mode is already set
223 const int oldZoomLevel
= m_controller
->zoomLevel();
228 const KUrl viewPropsUrl
= viewPropertiesUrl();
229 ViewProperties
props(viewPropsUrl
);
230 props
.setViewMode(m_mode
);
233 // the file item delegate has been recreated, apply the current
234 // additional information manually
235 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
236 m_fileItemDelegate
->setShowInformation(infoList
);
237 emit
additionalInfoChanged();
239 // Not all view modes support categorized sorting. Adjust the sorting model
240 // if changing the view mode results in a change of the categorized sorting
242 m_storedCategorizedSorting
= props
.categorizedSorting();
243 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
244 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
245 m_proxyModel
->setCategorizedModel(categorized
);
246 emit
categorizedSortingChanged();
251 updateZoomLevel(oldZoomLevel
);
253 loadDirectory(viewPropsUrl
);
257 DolphinView::Mode
DolphinView::mode() const
262 bool DolphinView::showPreview() const
264 return m_showPreview
;
267 bool DolphinView::showHiddenFiles() const
269 return m_dirLister
->showingDotFiles();
272 bool DolphinView::categorizedSorting() const
274 // If all view modes would support categorized sorting, returning
275 // m_proxyModel->isCategorizedModel() would be the way to go. As
276 // currently only the icons view supports caterized sorting, we remember
277 // the stored view properties state in m_storedCategorizedSorting and
278 // return this state. The application takes care to disable the corresponding
279 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
280 // that this setting is not applied to the current view mode.
281 return m_storedCategorizedSorting
;
284 bool DolphinView::supportsCategorizedSorting() const
286 return m_iconsView
!= 0;
289 void DolphinView::selectAll()
291 QAbstractItemView
* view
= itemView();
292 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
293 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
294 // selection instead of selecting all items. This is bypassed for KDE 4.0
295 // by invoking clearSelection() first.
296 view
->clearSelection();
300 void DolphinView::invertSelection()
302 if (isColumnViewActive()) {
303 // QAbstractItemView does not offer a virtual method invertSelection()
304 // as counterpart to QAbstractItemView::selectAll(). This makes it
305 // necessary to delegate the inverting of the selection to the
306 // column view, as only the selection of the active column should
308 m_columnView
->invertSelection();
310 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
311 const QAbstractItemModel
* itemModel
= selectionModel
->model();
313 const QModelIndex topLeft
= itemModel
->index(0, 0);
314 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
315 itemModel
->columnCount() - 1);
317 const QItemSelection
selection(topLeft
, bottomRight
);
318 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
322 bool DolphinView::hasSelection() const
324 return itemView()->selectionModel()->hasSelection();
327 void DolphinView::clearSelection()
329 QItemSelectionModel
* selModel
= itemView()->selectionModel();
330 const QModelIndex currentIndex
= selModel
->currentIndex();
331 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
332 QItemSelectionModel::Clear
);
335 KFileItemList
DolphinView::selectedItems() const
337 if (isColumnViewActive()) {
338 return m_columnView
->selectedItems();
341 const QAbstractItemView
* view
= itemView();
343 // Our view has a selection, we will map them back to the DolphinModel
344 // and then fill the KFileItemList.
345 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
347 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
348 KFileItemList itemList
;
350 const QModelIndexList indexList
= selection
.indexes();
351 foreach (const QModelIndex
&index
, indexList
) {
352 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
353 if (!item
.isNull()) {
354 itemList
.append(item
);
361 KUrl::List
DolphinView::selectedUrls() const
364 const KFileItemList list
= selectedItems();
365 foreach (const KFileItem
&item
, list
) {
366 urls
.append(item
.url());
371 int DolphinView::selectedItemsCount() const
373 if (isColumnViewActive()) {
374 // TODO: get rid of this special case by adjusting the dir lister
375 // to the current column
376 return m_columnView
->selectedItems().count();
379 return itemView()->selectionModel()->selection().count();
382 void DolphinView::setContentsPosition(int x
, int y
)
384 QAbstractItemView
* view
= itemView();
386 // the ColumnView takes care itself for the horizontal scrolling
387 if (!isColumnViewActive()) {
388 view
->horizontalScrollBar()->setValue(x
);
390 view
->verticalScrollBar()->setValue(y
);
392 m_loadingDirectory
= false;
395 QPoint
DolphinView::contentsPosition() const
397 const int x
= itemView()->horizontalScrollBar()->value();
398 const int y
= itemView()->verticalScrollBar()->value();
402 void DolphinView::setZoomLevel(int level
)
404 if (level
< ZoomLevelInfo::minimumLevel()) {
405 level
= ZoomLevelInfo::minimumLevel();
406 } else if (level
> ZoomLevelInfo::maximumLevel()) {
407 level
= ZoomLevelInfo::maximumLevel();
410 if (level
!= zoomLevel()) {
411 m_controller
->setZoomLevel(level
);
412 m_previewGenerator
->updateIcons();
413 emit
zoomLevelChanged(level
);
417 int DolphinView::zoomLevel() const
419 return m_controller
->zoomLevel();
422 void DolphinView::setSorting(Sorting sorting
)
424 if (sorting
!= this->sorting()) {
425 updateSorting(sorting
);
429 DolphinView::Sorting
DolphinView::sorting() const
431 return m_proxyModel
->sorting();
434 void DolphinView::setSortOrder(Qt::SortOrder order
)
436 if (sortOrder() != order
) {
437 updateSortOrder(order
);
441 Qt::SortOrder
DolphinView::sortOrder() const
443 return m_proxyModel
->sortOrder();
446 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
448 const KUrl viewPropsUrl
= viewPropertiesUrl();
449 ViewProperties
props(viewPropsUrl
);
450 props
.setAdditionalInfo(info
);
451 m_fileItemDelegate
->setShowInformation(info
);
453 emit
additionalInfoChanged();
455 if (itemView() != m_detailsView
) {
456 // the details view requires no reloading of the directory, as it maps
457 // the file item delegate info to its columns internally
458 loadDirectory(viewPropsUrl
);
462 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
464 return m_fileItemDelegate
->showInformation();
467 void DolphinView::reload()
470 loadDirectory(url(), true);
473 void DolphinView::refresh()
475 m_ignoreViewProperties
= false;
477 const bool oldActivationState
= m_active
;
478 const int oldZoomLevel
= m_controller
->zoomLevel();
482 applyViewProperties(m_controller
->url());
485 setActive(oldActivationState
);
486 updateZoomLevel(oldZoomLevel
);
489 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
491 if (m_controller
->url() == url
) {
495 m_previewGenerator
->cancelPreviews();
496 m_controller
->setUrl(url
); // emits urlChanged, which we forward
498 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
499 applyViewProperties(rootUrl
);
500 loadDirectory(rootUrl
);
501 if (itemView() == m_columnView
) {
502 m_columnView
->setRootUrl(rootUrl
);
503 m_columnView
->showColumn(url
);
506 applyViewProperties(url
);
510 emit
startedPathLoading(url
);
513 void DolphinView::setNameFilter(const QString
& nameFilter
)
515 m_proxyModel
->setFilterRegExp(nameFilter
);
517 if (isColumnViewActive()) {
518 // adjusting the directory lister is not enough in the case of the
519 // column view, as each column has its own directory lister internally...
520 m_columnView
->setNameFilter(nameFilter
);
524 void DolphinView::calculateItemCount(int& fileCount
,
526 KIO::filesize_t
& totalFileSize
) const
528 foreach (const KFileItem
& item
, m_dirLister
->items()) {
533 totalFileSize
+= item
.size();
538 QString
DolphinView::statusBarText() const
543 KIO::filesize_t totalFileSize
= 0;
545 if (hasSelection()) {
546 // give a summary of the status of the selected files
547 const KFileItemList list
= selectedItems();
548 if (list
.isEmpty()) {
549 // when an item is triggered, it is temporary selected but selectedItems()
550 // will return an empty list
554 KFileItemList::const_iterator it
= list
.begin();
555 const KFileItemList::const_iterator end
= list
.end();
557 const KFileItem
& item
= *it
;
562 totalFileSize
+= item
.size();
567 if (folderCount
+ fileCount
== 1) {
568 // if only one item is selected, show the filename
569 const QString name
= list
.first().name();
570 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
571 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
572 name
, KIO::convertSize(totalFileSize
));
574 // at least 2 items are selected
575 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
576 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
577 if ((folderCount
> 0) && (fileCount
> 0)) {
578 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
579 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
580 } else if (fileCount
> 0) {
581 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
583 Q_ASSERT(folderCount
> 0);
588 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
589 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
590 fileCount
, folderCount
,
591 totalFileSize
, true);
597 void DolphinView::setUrl(const KUrl
& url
)
599 // remember current item candidate (see restoreCurrentItem())
600 m_currentItemUrl
= url
;
601 updateView(url
, KUrl());
604 void DolphinView::changeSelection(const KFileItemList
& selection
)
607 if (selection
.isEmpty()) {
610 const KUrl
& baseUrl
= url();
612 QItemSelection newSelection
;
613 foreach(const KFileItem
& item
, selection
) {
614 url
= item
.url().upUrl();
615 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
616 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
617 newSelection
.select(index
, index
);
620 itemView()->selectionModel()->select(newSelection
,
621 QItemSelectionModel::ClearAndSelect
622 | QItemSelectionModel::Current
);
625 void DolphinView::renameSelectedItems()
627 KFileItemList items
= selectedItems();
628 const int itemCount
= items
.count();
634 // More than one item has been selected for renaming. Open
635 // a rename dialog and rename all items afterwards.
636 RenameDialog
dialog(this, items
);
637 if (dialog
.exec() == QDialog::Rejected
) {
641 const QString newName
= dialog
.newName();
642 if (newName
.isEmpty()) {
643 emit
errorMessage(dialog
.errorString());
647 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
648 // as one operation instead of n rename operations like it is done now...
649 Q_ASSERT(newName
.contains('#'));
651 // currently the items are sorted by the selection order, resort
652 // them by the file name
653 qSort(items
.begin(), items
.end(), lessThan
);
655 // iterate through all selected items and rename them...
657 foreach (const KFileItem
& item
, items
) {
658 const KUrl
& oldUrl
= item
.url();
660 number
.setNum(index
++);
662 QString name
= newName
;
663 name
.replace('#', number
);
665 if (oldUrl
.fileName() != name
) {
666 KUrl newUrl
= oldUrl
;
667 newUrl
.setFileName(name
);
668 KonqOperations::rename(this, oldUrl
, newUrl
);
671 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
672 Q_ASSERT(itemCount
== 1);
674 if (isColumnViewActive()) {
675 m_columnView
->editItem(items
.first());
677 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
678 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
679 itemView()->edit(proxyIndex
);
682 Q_ASSERT(itemCount
== 1);
684 RenameDialog
dialog(this, items
);
685 if (dialog
.exec() == QDialog::Rejected
) {
689 const QString
& newName
= dialog
.newName();
690 if (newName
.isEmpty()) {
691 emit
errorMessage(dialog
.errorString());
695 const KUrl
& oldUrl
= items
.first().url();
696 KUrl newUrl
= oldUrl
;
697 newUrl
.setFileName(newName
);
698 KonqOperations::rename(this, oldUrl
, newUrl
);
701 // assure that the current index remains visible when KDirLister
702 // will notify the view about changed items
703 m_assureVisibleCurrentIndex
= true;
706 void DolphinView::trashSelectedItems()
708 const KUrl::List list
= simplifiedSelectedUrls();
709 KonqOperations::del(this, KonqOperations::TRASH
, list
);
712 void DolphinView::deleteSelectedItems()
714 const KUrl::List list
= simplifiedSelectedUrls();
715 const bool del
= KonqOperations::askDeleteConfirmation(list
,
717 KonqOperations::DEFAULT_CONFIRMATION
,
721 KIO::Job
* job
= KIO::del(list
);
722 connect(job
, SIGNAL(result(KJob
*)),
723 this, SLOT(slotDeleteFileFinished(KJob
*)));
727 void DolphinView::cutSelectedItems()
729 QMimeData
* mimeData
= selectionMimeData();
730 KonqMimeData::addIsCutSelection(mimeData
, true);
731 QApplication::clipboard()->setMimeData(mimeData
);
734 void DolphinView::copySelectedItems()
736 QMimeData
* mimeData
= selectionMimeData();
737 QApplication::clipboard()->setMimeData(mimeData
);
740 void DolphinView::paste()
745 void DolphinView::pasteIntoFolder()
747 const KFileItemList items
= selectedItems();
748 if ((items
.count() == 1) && items
.first().isDir()) {
749 pasteToUrl(items
.first().url());
753 void DolphinView::setShowPreview(bool show
)
755 if (m_showPreview
== show
) {
759 const KUrl viewPropsUrl
= viewPropertiesUrl();
760 ViewProperties
props(viewPropsUrl
);
761 props
.setShowPreview(show
);
763 m_showPreview
= show
;
764 m_previewGenerator
->setPreviewShown(show
);
766 const int oldZoomLevel
= m_controller
->zoomLevel();
767 emit
showPreviewChanged();
769 // Enabling or disabling the preview might change the icon size of the view.
770 // As the view does not emit a signal when the icon size has been changed,
771 // the used zoom level of the controller must be adjusted manually:
772 updateZoomLevel(oldZoomLevel
);
774 loadDirectory(viewPropsUrl
);
777 void DolphinView::setShowHiddenFiles(bool show
)
779 if (m_dirLister
->showingDotFiles() == show
) {
783 const KUrl viewPropsUrl
= viewPropertiesUrl();
784 ViewProperties
props(viewPropsUrl
);
785 props
.setShowHiddenFiles(show
);
787 m_dirLister
->setShowingDotFiles(show
);
788 emit
showHiddenFilesChanged();
790 loadDirectory(viewPropsUrl
);
793 void DolphinView::setCategorizedSorting(bool categorized
)
795 if (categorized
== categorizedSorting()) {
799 // setCategorizedSorting(true) may only get invoked
800 // if the view supports categorized sorting
801 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
803 ViewProperties
props(viewPropertiesUrl());
804 props
.setCategorizedSorting(categorized
);
807 m_storedCategorizedSorting
= categorized
;
808 m_proxyModel
->setCategorizedModel(categorized
);
810 emit
categorizedSortingChanged();
813 void DolphinView::toggleSortOrder()
815 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
816 Qt::DescendingOrder
:
821 void DolphinView::toggleAdditionalInfo(QAction
* action
)
823 const KFileItemDelegate::Information info
=
824 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
826 KFileItemDelegate::InformationList list
= additionalInfo();
828 const bool show
= action
->isChecked();
830 const int index
= list
.indexOf(info
);
831 const bool containsInfo
= (index
>= 0);
832 if (show
&& !containsInfo
) {
834 setAdditionalInfo(list
);
835 } else if (!show
&& containsInfo
) {
836 list
.removeAt(index
);
837 setAdditionalInfo(list
);
838 Q_ASSERT(list
.indexOf(info
) < 0);
842 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
844 QWidget::mouseReleaseEvent(event
);
848 void DolphinView::wheelEvent(QWheelEvent
* event
)
850 if (event
->modifiers() & Qt::ControlModifier
) {
851 const int delta
= event
->delta();
852 const int level
= zoomLevel();
854 setZoomLevel(level
+ 1);
855 } else if (delta
< 0) {
856 setZoomLevel(level
- 1);
862 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
864 switch (event
->type()) {
865 case QEvent::FocusIn
:
866 if (watched
== itemView()) {
867 m_controller
->requestActivation();
871 case QEvent::MouseButtonPress
:
872 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
873 // Listening to a mousebutton press event to delete expanded views is a
874 // workaround, as it seems impossible for the FolderExpander to know when
875 // a dragging outside a view has been finished. However it works quite well:
876 // A mousebutton press event indicates that a drag operation must be
878 m_expandedDragSource
->deleteLater();
879 m_expandedDragSource
= 0;
883 case QEvent::DragEnter
:
884 if (watched
== itemView()->viewport()) {
889 case QEvent::KeyPress
:
890 if (watched
== itemView()) {
891 if (m_toolTipManager
!= 0) {
892 m_toolTipManager
->hideTip();
895 // clear the selection when Escape has been pressed
896 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
897 if (keyEvent
->key() == Qt::Key_Escape
) {
907 return QWidget::eventFilter(watched
, event
);
910 void DolphinView::activate()
915 void DolphinView::triggerItem(const KFileItem
& item
)
917 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
918 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
919 // items are selected by the user, hence don't trigger the
920 // item specified by 'index'
924 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
925 if (item
.isNull() || m_isContextMenuOpen
) {
929 if (m_toolTipManager
!= 0) {
930 m_toolTipManager
->hideTip();
932 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
935 void DolphinView::emitSelectionChangedSignal()
937 emit
selectionChanged(DolphinView::selectedItems());
940 void DolphinView::openContextMenu(const QPoint
& pos
,
941 const QList
<QAction
*>& customActions
)
944 if (isColumnViewActive()) {
945 item
= m_columnView
->itemAt(pos
);
947 const QModelIndex index
= itemView()->indexAt(pos
);
948 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
949 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
950 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
954 if (m_toolTipManager
!= 0) {
955 m_toolTipManager
->hideTip();
958 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
959 emit
requestContextMenu(item
, url(), customActions
);
960 m_isContextMenuOpen
= false;
963 void DolphinView::dropUrls(const KFileItem
& destItem
,
964 const KUrl
& destPath
,
967 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
970 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
972 ViewProperties
props(viewPropertiesUrl());
973 props
.setSorting(sorting
);
975 m_proxyModel
->setSorting(sorting
);
977 emit
sortingChanged(sorting
);
980 void DolphinView::updateSortOrder(Qt::SortOrder order
)
982 ViewProperties
props(viewPropertiesUrl());
983 props
.setSortOrder(order
);
985 m_proxyModel
->setSortOrder(order
);
987 emit
sortOrderChanged(order
);
990 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
992 ViewProperties
props(viewPropertiesUrl());
993 props
.setAdditionalInfo(info
);
996 m_fileItemDelegate
->setShowInformation(info
);
998 emit
additionalInfoChanged();
1001 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1003 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1004 (m_mode
== DolphinView::IconsView
);
1006 QAction
* showSizeInfo
= collection
->action("show_size_info");
1007 QAction
* showDateInfo
= collection
->action("show_date_info");
1008 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1009 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1010 QAction
* showGroupInfo
= collection
->action("show_group_info");
1011 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1013 showSizeInfo
->setChecked(false);
1014 showDateInfo
->setChecked(false);
1015 showPermissionsInfo
->setChecked(false);
1016 showOwnerInfo
->setChecked(false);
1017 showGroupInfo
->setChecked(false);
1018 showMimeInfo
->setChecked(false);
1020 showSizeInfo
->setEnabled(enable
);
1021 showDateInfo
->setEnabled(enable
);
1022 showPermissionsInfo
->setEnabled(enable
);
1023 showOwnerInfo
->setEnabled(enable
);
1024 showGroupInfo
->setEnabled(enable
);
1025 showMimeInfo
->setEnabled(enable
);
1027 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1029 case KFileItemDelegate::Size
:
1030 showSizeInfo
->setChecked(true);
1032 case KFileItemDelegate::ModificationTime
:
1033 showDateInfo
->setChecked(true);
1035 case KFileItemDelegate::Permissions
:
1036 showPermissionsInfo
->setChecked(true);
1038 case KFileItemDelegate::Owner
:
1039 showOwnerInfo
->setChecked(true);
1041 case KFileItemDelegate::OwnerAndGroup
:
1042 showGroupInfo
->setChecked(true);
1044 case KFileItemDelegate::FriendlyMimeType
:
1045 showMimeInfo
->setChecked(true);
1053 QPair
<bool, QString
> DolphinView::pasteInfo() const
1055 return KonqOperations::pasteInfo(url());
1058 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1060 m_tabsForFiles
= tabsForFiles
;
1063 bool DolphinView::isTabsForFilesEnabled() const
1065 return m_tabsForFiles
;
1068 bool DolphinView::itemsExpandable() const
1070 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1073 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1078 if (DragAndDropHelper::instance().isDragSource(view
)) {
1079 // We must store for later deletion.
1080 if (m_expandedDragSource
!= 0) {
1081 // The old stored view is obviously not the drag source anymore.
1082 m_expandedDragSource
->deleteLater();
1083 m_expandedDragSource
= 0;
1086 m_expandedDragSource
= view
;
1089 view
->deleteLater();
1093 void DolphinView::observeCreatedItem(const KUrl
& url
)
1095 m_createdItemUrl
= url
;
1096 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1097 this, SLOT(selectAndScrollToCreatedItem()));
1100 void DolphinView::selectAndScrollToCreatedItem()
1102 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1103 if (dirIndex
.isValid()) {
1104 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1105 itemView()->setCurrentIndex(proxyIndex
);
1108 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1109 this, SLOT(selectAndScrollToCreatedItem()));
1110 m_createdItemUrl
= KUrl();
1113 void DolphinView::restoreSelection()
1115 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1116 changeSelection(m_selectedItems
);
1119 void DolphinView::emitContentsMoved()
1121 // only emit the contents moved signal if:
1122 // - no directory loading is ongoing (this would reset the contents position
1123 // always to (0, 0))
1124 // - if the Column View is active: the column view does an automatic
1125 // positioning during the loading operation, which must be remembered
1126 if (!m_loadingDirectory
|| isColumnViewActive()) {
1127 const QPoint
pos(contentsPosition());
1128 emit
contentsMoved(pos
.x(), pos
.y());
1132 void DolphinView::showHoverInformation(const KFileItem
& item
)
1134 emit
requestItemInfo(item
);
1137 void DolphinView::clearHoverInformation()
1139 emit
requestItemInfo(KFileItem());
1142 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1144 if (job
->error() == 0) {
1145 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1146 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1147 emit
errorMessage(job
->errorString());
1151 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1153 emit
requestUrlChange(url
);
1154 m_controller
->setUrl(url
);
1157 void DolphinView::restoreCurrentItem()
1159 if (!m_currentItemUrl
.isEmpty()) {
1160 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1161 if (dirIndex
.isValid()) {
1162 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1163 QAbstractItemView
* view
= itemView();
1164 const bool clearSelection
= !hasSelection();
1165 view
->setCurrentIndex(proxyIndex
);
1166 if (clearSelection
) {
1167 view
->clearSelection();
1170 m_currentItemUrl
.clear();
1174 void DolphinView::slotRefreshItems()
1176 if (m_assureVisibleCurrentIndex
) {
1177 m_assureVisibleCurrentIndex
= false;
1178 itemView()->scrollTo(itemView()->currentIndex());
1182 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1184 if (!url
.isValid()) {
1185 const QString
location(url
.pathOrUrl());
1186 if (location
.isEmpty()) {
1187 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1189 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1194 m_loadingDirectory
= true;
1197 m_selectedItems
= selectedItems();
1198 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1201 m_dirLister
->stop();
1202 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1204 if (isColumnViewActive()) {
1205 // adjusting the directory lister is not enough in the case of the
1206 // column view, as each column has its own directory lister internally...
1208 m_columnView
->reload();
1210 m_columnView
->showColumn(url
);
1215 KUrl
DolphinView::viewPropertiesUrl() const
1217 if (isColumnViewActive()) {
1218 return m_columnView
->rootUrl();
1224 void DolphinView::applyViewProperties(const KUrl
& url
)
1226 if (m_ignoreViewProperties
) {
1230 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1231 // The column view is active, hence don't apply the view properties
1232 // of sub directories (represented by columns) to the view. The
1233 // view always represents the properties of the first column.
1237 const ViewProperties
props(url
);
1239 const Mode mode
= props
.viewMode();
1240 if (m_mode
!= mode
) {
1241 const int oldZoomLevel
= m_controller
->zoomLevel();
1247 updateZoomLevel(oldZoomLevel
);
1249 if (itemView() == 0) {
1252 Q_ASSERT(itemView() != 0);
1253 Q_ASSERT(m_fileItemDelegate
!= 0);
1255 const bool showHiddenFiles
= props
.showHiddenFiles();
1256 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1257 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1258 emit
showHiddenFilesChanged();
1261 m_storedCategorizedSorting
= props
.categorizedSorting();
1262 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1263 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1264 m_proxyModel
->setCategorizedModel(categorized
);
1265 emit
categorizedSortingChanged();
1268 const DolphinView::Sorting sorting
= props
.sorting();
1269 if (sorting
!= m_proxyModel
->sorting()) {
1270 m_proxyModel
->setSorting(sorting
);
1271 emit
sortingChanged(sorting
);
1274 const Qt::SortOrder sortOrder
= props
.sortOrder();
1275 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1276 m_proxyModel
->setSortOrder(sortOrder
);
1277 emit
sortOrderChanged(sortOrder
);
1280 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1281 if (info
!= m_fileItemDelegate
->showInformation()) {
1282 m_fileItemDelegate
->setShowInformation(info
);
1283 emit
additionalInfoChanged();
1286 const bool showPreview
= props
.showPreview();
1287 if (showPreview
!= m_showPreview
) {
1288 m_showPreview
= showPreview
;
1289 m_previewGenerator
->setPreviewShown(showPreview
);
1291 const int oldZoomLevel
= m_controller
->zoomLevel();
1292 emit
showPreviewChanged();
1294 // Enabling or disabling the preview might change the icon size of the view.
1295 // As the view does not emit a signal when the icon size has been changed,
1296 // the used zoom level of the controller must be adjusted manually:
1297 updateZoomLevel(oldZoomLevel
);
1300 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1301 // During the lifetime of a DolphinView instance the global view properties
1302 // should not be changed. This allows e. g. to split a view and use different
1303 // view properties for each view.
1304 m_ignoreViewProperties
= true;
1308 void DolphinView::createView()
1311 Q_ASSERT(m_iconsView
== 0);
1312 Q_ASSERT(m_detailsView
== 0);
1313 Q_ASSERT(m_columnView
== 0);
1315 QAbstractItemView
* view
= 0;
1318 m_iconsView
= new DolphinIconsView(this, m_controller
);
1324 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1325 view
= m_detailsView
;
1329 m_columnView
= new DolphinColumnView(this, m_controller
);
1330 view
= m_columnView
;
1334 Q_ASSERT(view
!= 0);
1335 view
->installEventFilter(this);
1336 view
->viewport()->installEventFilter(this);
1337 setFocusProxy(view
);
1339 if (m_mode
!= ColumnView
) {
1340 // Give the view the ability to auto-expand its directories on hovering
1341 // (the column view takes care about this itself). If the details view
1342 // uses expandable folders, the auto-expanding should be used always.
1343 DolphinSettings
& settings
= DolphinSettings::instance();
1344 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1345 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1347 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1348 folderExpander
->setEnabled(enabled
);
1349 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1350 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1353 // Listen out for requests to delete the current column.
1354 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1355 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1358 m_controller
->setItemView(view
);
1360 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1361 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1362 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1363 view
->setItemDelegate(m_fileItemDelegate
);
1365 view
->setModel(m_proxyModel
);
1366 if (m_selectionModel
!= 0) {
1367 view
->setSelectionModel(m_selectionModel
);
1369 m_selectionModel
= view
->selectionModel();
1372 // reparent the selection model, as it should not be deleted
1373 // when deleting the model
1374 m_selectionModel
->setParent(this);
1376 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1378 m_previewGenerator
= new KFilePreviewGenerator(view
);
1379 m_previewGenerator
->setPreviewShown(m_showPreview
);
1381 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1382 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1383 connect(m_controller
, SIGNAL(hideToolTip()),
1384 m_toolTipManager
, SLOT(hideTip()));
1387 m_topLayout
->insertWidget(1, view
);
1389 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1390 this, SLOT(emitSelectionChangedSignal()));
1391 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1392 this, SLOT(emitContentsMoved()));
1393 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1394 this, SLOT(emitContentsMoved()));
1397 void DolphinView::deleteView()
1399 QAbstractItemView
* view
= itemView();
1401 // It's important to set the keyboard focus to the parent
1402 // before deleting the view: Otherwise when having a split
1403 // view the other view will get the focus and will request
1404 // an activation (see DolphinView::eventFilter()).
1408 m_topLayout
->removeWidget(view
);
1411 // m_previewGenerator's parent is not always destroyed, and we
1412 // don't want two active at once - manually delete.
1413 delete m_previewGenerator
;
1414 m_previewGenerator
= 0;
1417 m_controller
->disconnect(view
);
1420 deleteWhenNotDragSource(view
);
1426 m_fileItemDelegate
= 0;
1427 m_toolTipManager
= 0;
1431 QAbstractItemView
* DolphinView::itemView() const
1433 if (m_detailsView
!= 0) {
1434 return m_detailsView
;
1435 } else if (m_columnView
!= 0) {
1436 return m_columnView
;
1442 bool DolphinView::isCutItem(const KFileItem
& item
) const
1444 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1445 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1447 const KUrl
& itemUrl
= item
.url();
1448 KUrl::List::const_iterator it
= cutUrls
.begin();
1449 const KUrl::List::const_iterator end
= cutUrls
.end();
1451 if (*it
== itemUrl
) {
1460 void DolphinView::pasteToUrl(const KUrl
& url
)
1462 KonqOperations::doPaste(this, url
);
1465 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1467 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1468 if (oldZoomLevel
!= newZoomLevel
) {
1469 m_controller
->setZoomLevel(newZoomLevel
);
1470 emit
zoomLevelChanged(newZoomLevel
);
1474 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1476 KUrl::List list
= selectedUrls();
1477 if (itemsExpandable() ) {
1478 list
= KDirModel::simplifiedUrlList(list
);
1483 QMimeData
* DolphinView::selectionMimeData() const
1485 if (isColumnViewActive()) {
1486 return m_columnView
->selectionMimeData();
1489 const QAbstractItemView
* view
= itemView();
1490 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1491 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1492 return m_dolphinModel
->mimeData(selection
.indexes());
1496 #include "dolphinview.moc"