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),
111 m_expandedDragSource(0)
113 m_topLayout
= new QVBoxLayout(this);
114 m_topLayout
->setSpacing(0);
115 m_topLayout
->setMargin(0);
117 m_controller
= new DolphinController(this);
118 m_controller
->setUrl(url
);
120 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
121 this, SIGNAL(urlChanged(const KUrl
&)));
122 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
123 this, SLOT(slotRequestUrlChange(const KUrl
&)));
125 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
126 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
127 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
128 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
129 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
130 this, SLOT(updateSorting(DolphinView::Sorting
)));
131 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
132 this, SLOT(updateSortOrder(Qt::SortOrder
)));
133 connect(m_controller
, SIGNAL(sortFoldersFirstChanged(bool)),
134 this, SLOT(updateSortFoldersFirst(bool)));
135 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
136 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
137 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
138 this, SLOT(triggerItem(const KFileItem
&)));
139 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
140 this, SIGNAL(tabRequested(const KUrl
&)));
141 connect(m_controller
, SIGNAL(activated()),
142 this, SLOT(activate()));
143 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
144 this, SLOT(showHoverInformation(const KFileItem
&)));
145 connect(m_controller
, SIGNAL(viewportEntered()),
146 this, SLOT(clearHoverInformation()));
148 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
149 this, SIGNAL(redirection(KUrl
, KUrl
)));
150 connect(m_dirLister
, SIGNAL(completed()),
151 this, SLOT(slotDirListerCompleted()));
152 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
153 this, SLOT(slotRefreshItems()));
155 // When a new item has been created by the "Create New..." menu, the item should
156 // get selected and it must be assured that the item will get visible. As the
157 // creation is done asynchronously, several signals must be checked:
158 connect(&DolphinNewMenuObserver::instance(), SIGNAL(itemCreated(const KUrl
&)),
159 this, SLOT(observeCreatedItem(const KUrl
&)));
161 applyViewProperties(url
);
162 m_topLayout
->addWidget(itemView());
165 DolphinView::~DolphinView()
167 delete m_expandedDragSource
;
168 m_expandedDragSource
= 0;
171 const KUrl
& DolphinView::url() const
173 return m_controller
->url();
176 KUrl
DolphinView::rootUrl() const
178 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
181 void DolphinView::setActive(bool active
)
183 if (active
== m_active
) {
189 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
191 emit
selectionChanged(selectedItems());
196 QWidget
* viewport
= itemView()->viewport();
198 palette
.setColor(viewport
->backgroundRole(), color
);
199 viewport
->setPalette(palette
);
204 itemView()->setFocus();
208 m_controller
->indicateActivationChange(active
);
211 bool DolphinView::isActive() const
216 void DolphinView::setMode(Mode mode
)
218 if (mode
== m_mode
) {
219 return; // the wished mode is already set
222 const int oldZoomLevel
= m_controller
->zoomLevel();
227 const KUrl viewPropsUrl
= viewPropertiesUrl();
228 ViewProperties
props(viewPropsUrl
);
229 props
.setViewMode(m_mode
);
232 // the file item delegate has been recreated, apply the current
233 // additional information manually
234 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
235 m_fileItemDelegate
->setShowInformation(infoList
);
236 emit
additionalInfoChanged();
238 // Not all view modes support categorized sorting. Adjust the sorting model
239 // if changing the view mode results in a change of the categorized sorting
241 m_storedCategorizedSorting
= props
.categorizedSorting();
242 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
243 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
244 m_proxyModel
->setCategorizedModel(categorized
);
245 emit
categorizedSortingChanged();
250 updateZoomLevel(oldZoomLevel
);
252 loadDirectory(viewPropsUrl
);
256 DolphinView::Mode
DolphinView::mode() const
261 bool DolphinView::showPreview() const
263 return m_showPreview
;
266 bool DolphinView::showHiddenFiles() const
268 return m_dirLister
->showingDotFiles();
271 bool DolphinView::categorizedSorting() const
273 // If all view modes would support categorized sorting, returning
274 // m_proxyModel->isCategorizedModel() would be the way to go. As
275 // currently only the icons view supports caterized sorting, we remember
276 // the stored view properties state in m_storedCategorizedSorting and
277 // return this state. The application takes care to disable the corresponding
278 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
279 // that this setting is not applied to the current view mode.
280 return m_storedCategorizedSorting
;
283 bool DolphinView::supportsCategorizedSorting() const
285 return m_iconsView
!= 0;
288 void DolphinView::selectAll()
290 QAbstractItemView
* view
= itemView();
291 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
292 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
293 // selection instead of selecting all items. This is bypassed for KDE 4.0
294 // by invoking clearSelection() first.
295 view
->clearSelection();
299 void DolphinView::invertSelection()
301 if (isColumnViewActive()) {
302 // QAbstractItemView does not offer a virtual method invertSelection()
303 // as counterpart to QAbstractItemView::selectAll(). This makes it
304 // necessary to delegate the inverting of the selection to the
305 // column view, as only the selection of the active column should
307 m_columnView
->invertSelection();
309 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
310 const QAbstractItemModel
* itemModel
= selectionModel
->model();
312 const QModelIndex topLeft
= itemModel
->index(0, 0);
313 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
314 itemModel
->columnCount() - 1);
316 const QItemSelection
selection(topLeft
, bottomRight
);
317 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
321 bool DolphinView::hasSelection() const
323 return itemView()->selectionModel()->hasSelection();
326 void DolphinView::clearSelection()
328 QItemSelectionModel
* selModel
= itemView()->selectionModel();
329 const QModelIndex currentIndex
= selModel
->currentIndex();
330 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
331 QItemSelectionModel::Clear
);
332 m_selectedItems
.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::setSortFoldersFirst(bool foldersFirst
)
448 if (sortFoldersFirst() != foldersFirst
) {
449 updateSortFoldersFirst(foldersFirst
);
453 bool DolphinView::sortFoldersFirst() const
455 return m_proxyModel
->sortFoldersFirst();
458 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
460 const KUrl viewPropsUrl
= viewPropertiesUrl();
461 ViewProperties
props(viewPropsUrl
);
462 props
.setAdditionalInfo(info
);
463 m_fileItemDelegate
->setShowInformation(info
);
465 emit
additionalInfoChanged();
467 if (itemView() != m_detailsView
) {
468 // the details view requires no reloading of the directory, as it maps
469 // the file item delegate info to its columns internally
470 loadDirectory(viewPropsUrl
);
474 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
476 return m_fileItemDelegate
->showInformation();
479 void DolphinView::reload()
482 loadDirectory(url(), true);
485 void DolphinView::refresh()
487 m_ignoreViewProperties
= false;
489 const bool oldActivationState
= m_active
;
490 const int oldZoomLevel
= m_controller
->zoomLevel();
494 applyViewProperties(m_controller
->url());
497 setActive(oldActivationState
);
498 updateZoomLevel(oldZoomLevel
);
501 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
503 if (m_controller
->url() == url
) {
507 m_previewGenerator
->cancelPreviews();
508 m_controller
->setUrl(url
); // emits urlChanged, which we forward
510 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
511 applyViewProperties(rootUrl
);
512 loadDirectory(rootUrl
);
513 if (itemView() == m_columnView
) {
514 m_columnView
->setRootUrl(rootUrl
);
515 m_columnView
->showColumn(url
);
518 applyViewProperties(url
);
522 emit
startedPathLoading(url
);
525 void DolphinView::setNameFilter(const QString
& nameFilter
)
527 m_proxyModel
->setFilterRegExp(nameFilter
);
529 if (isColumnViewActive()) {
530 // adjusting the directory lister is not enough in the case of the
531 // column view, as each column has its own directory lister internally...
532 m_columnView
->setNameFilter(nameFilter
);
536 void DolphinView::calculateItemCount(int& fileCount
,
538 KIO::filesize_t
& totalFileSize
) const
540 foreach (const KFileItem
& item
, m_dirLister
->items()) {
545 totalFileSize
+= item
.size();
550 QString
DolphinView::statusBarText() const
555 KIO::filesize_t totalFileSize
= 0;
557 if (hasSelection()) {
558 // give a summary of the status of the selected files
559 const KFileItemList list
= selectedItems();
560 if (list
.isEmpty()) {
561 // when an item is triggered, it is temporary selected but selectedItems()
562 // will return an empty list
566 KFileItemList::const_iterator it
= list
.begin();
567 const KFileItemList::const_iterator end
= list
.end();
569 const KFileItem
& item
= *it
;
574 totalFileSize
+= item
.size();
579 if (folderCount
+ fileCount
== 1) {
580 // if only one item is selected, show the filename
581 const QString name
= list
.first().name();
582 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
583 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
584 name
, KIO::convertSize(totalFileSize
));
586 // at least 2 items are selected
587 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
588 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
589 if ((folderCount
> 0) && (fileCount
> 0)) {
590 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
591 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
592 } else if (fileCount
> 0) {
593 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
595 Q_ASSERT(folderCount
> 0);
600 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
601 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
602 fileCount
, folderCount
,
603 totalFileSize
, true);
609 void DolphinView::setUrl(const KUrl
& url
)
611 m_newFileNames
.clear();
612 updateView(url
, KUrl());
615 void DolphinView::changeSelection(const KFileItemList
& selection
)
618 if (selection
.isEmpty()) {
621 const KUrl
& baseUrl
= url();
623 QItemSelection newSelection
;
624 foreach(const KFileItem
& item
, selection
) {
625 url
= item
.url().upUrl();
626 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
627 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
628 newSelection
.select(index
, index
);
631 itemView()->selectionModel()->select(newSelection
,
632 QItemSelectionModel::ClearAndSelect
633 | QItemSelectionModel::Current
);
636 void DolphinView::renameSelectedItems()
638 KFileItemList items
= selectedItems();
639 const int itemCount
= items
.count();
645 // More than one item has been selected for renaming. Open
646 // a rename dialog and rename all items afterwards.
647 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
648 if (dialog
->exec() == QDialog::Rejected
) {
653 const QString newName
= dialog
->newName();
654 if (newName
.isEmpty()) {
655 emit
errorMessage(dialog
->errorString());
661 // the selection would be invalid after renaming the items, so just clear
665 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
666 // as one operation instead of n rename operations like it is done now...
667 Q_ASSERT(newName
.contains('#'));
669 // currently the items are sorted by the selection order, resort
670 // them by the file name
671 qSort(items
.begin(), items
.end(), lessThan
);
673 // iterate through all selected items and rename them...
675 foreach (const KFileItem
& item
, items
) {
676 const KUrl
& oldUrl
= item
.url();
678 number
.setNum(index
++);
680 QString name
= newName
;
681 name
.replace('#', number
);
683 if (oldUrl
.fileName() != name
) {
684 KUrl newUrl
= oldUrl
;
685 newUrl
.setFileName(name
);
686 KonqOperations::rename(this, oldUrl
, newUrl
);
689 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
690 Q_ASSERT(itemCount
== 1);
692 if (isColumnViewActive()) {
693 m_columnView
->editItem(items
.first());
695 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
696 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
697 itemView()->edit(proxyIndex
);
700 Q_ASSERT(itemCount
== 1);
702 QPointer
<RenameDialog
> dialog
= new RenameDialog(this, items
);
703 if (dialog
->exec() == QDialog::Rejected
) {
708 const QString newName
= dialog
->newName();
709 if (newName
.isEmpty()) {
710 emit
errorMessage(dialog
->errorString());
716 const KUrl
& oldUrl
= items
.first().url();
717 KUrl newUrl
= oldUrl
;
718 newUrl
.setFileName(newName
);
719 KonqOperations::rename(this, oldUrl
, newUrl
);
722 // assure that the current index remains visible when KDirLister
723 // will notify the view about changed items
724 m_assureVisibleCurrentIndex
= true;
727 void DolphinView::trashSelectedItems()
729 const KUrl::List list
= simplifiedSelectedUrls();
730 KonqOperations::del(this, KonqOperations::TRASH
, list
);
733 void DolphinView::deleteSelectedItems()
735 const KUrl::List list
= simplifiedSelectedUrls();
736 const bool del
= KonqOperations::askDeleteConfirmation(list
,
738 KonqOperations::DEFAULT_CONFIRMATION
,
742 KIO::Job
* job
= KIO::del(list
);
743 connect(job
, SIGNAL(result(KJob
*)),
744 this, SLOT(slotDeleteFileFinished(KJob
*)));
748 void DolphinView::cutSelectedItems()
750 QMimeData
* mimeData
= selectionMimeData();
751 KonqMimeData::addIsCutSelection(mimeData
, true);
752 QApplication::clipboard()->setMimeData(mimeData
);
755 void DolphinView::copySelectedItems()
757 QMimeData
* mimeData
= selectionMimeData();
758 QApplication::clipboard()->setMimeData(mimeData
);
761 void DolphinView::paste()
766 void DolphinView::pasteIntoFolder()
768 const KFileItemList items
= selectedItems();
769 if ((items
.count() == 1) && items
.first().isDir()) {
770 pasteToUrl(items
.first().url());
774 void DolphinView::setShowPreview(bool show
)
776 if (m_showPreview
== show
) {
780 const KUrl viewPropsUrl
= viewPropertiesUrl();
781 ViewProperties
props(viewPropsUrl
);
782 props
.setShowPreview(show
);
784 m_showPreview
= show
;
785 m_previewGenerator
->setPreviewShown(show
);
787 const int oldZoomLevel
= m_controller
->zoomLevel();
788 emit
showPreviewChanged();
790 // Enabling or disabling the preview might change the icon size of the view.
791 // As the view does not emit a signal when the icon size has been changed,
792 // the used zoom level of the controller must be adjusted manually:
793 updateZoomLevel(oldZoomLevel
);
795 loadDirectory(viewPropsUrl
);
798 void DolphinView::setShowHiddenFiles(bool show
)
800 if (m_dirLister
->showingDotFiles() == show
) {
804 const KUrl viewPropsUrl
= viewPropertiesUrl();
805 ViewProperties
props(viewPropsUrl
);
806 props
.setShowHiddenFiles(show
);
808 m_dirLister
->setShowingDotFiles(show
);
809 emit
showHiddenFilesChanged();
811 loadDirectory(viewPropsUrl
);
814 void DolphinView::setCategorizedSorting(bool categorized
)
816 if (categorized
== categorizedSorting()) {
820 // setCategorizedSorting(true) may only get invoked
821 // if the view supports categorized sorting
822 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
824 ViewProperties
props(viewPropertiesUrl());
825 props
.setCategorizedSorting(categorized
);
828 m_storedCategorizedSorting
= categorized
;
829 m_proxyModel
->setCategorizedModel(categorized
);
831 emit
categorizedSortingChanged();
834 void DolphinView::toggleSortOrder()
836 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
837 Qt::DescendingOrder
:
842 void DolphinView::toggleSortFoldersFirst()
844 setSortFoldersFirst(!sortFoldersFirst());
847 void DolphinView::toggleAdditionalInfo(QAction
* action
)
849 const KFileItemDelegate::Information info
=
850 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
852 KFileItemDelegate::InformationList list
= additionalInfo();
854 const bool show
= action
->isChecked();
856 const int index
= list
.indexOf(info
);
857 const bool containsInfo
= (index
>= 0);
858 if (show
&& !containsInfo
) {
860 setAdditionalInfo(list
);
861 } else if (!show
&& containsInfo
) {
862 list
.removeAt(index
);
863 setAdditionalInfo(list
);
864 Q_ASSERT(list
.indexOf(info
) < 0);
868 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
870 QWidget::mouseReleaseEvent(event
);
874 void DolphinView::wheelEvent(QWheelEvent
* event
)
876 if (event
->modifiers() & Qt::ControlModifier
) {
877 const int delta
= event
->delta();
878 const int level
= zoomLevel();
880 setZoomLevel(level
+ 1);
881 } else if (delta
< 0) {
882 setZoomLevel(level
- 1);
888 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
890 switch (event
->type()) {
891 case QEvent::FocusIn
:
892 if (watched
== itemView()) {
893 m_controller
->requestActivation();
897 case QEvent::MouseButtonPress
:
898 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
899 // Listening to a mousebutton press event to delete expanded views is a
900 // workaround, as it seems impossible for the FolderExpander to know when
901 // a dragging outside a view has been finished. However it works quite well:
902 // A mousebutton press event indicates that a drag operation must be
904 m_expandedDragSource
->deleteLater();
905 m_expandedDragSource
= 0;
909 case QEvent::DragEnter
:
910 if (watched
== itemView()->viewport()) {
915 case QEvent::KeyPress
:
916 if (watched
== itemView()) {
917 if (m_toolTipManager
!= 0) {
918 m_toolTipManager
->hideTip();
921 // clear the selection when Escape has been pressed
922 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
923 if (keyEvent
->key() == Qt::Key_Escape
) {
933 return QWidget::eventFilter(watched
, event
);
936 void DolphinView::activate()
941 void DolphinView::triggerItem(const KFileItem
& item
)
943 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
944 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
945 // items are selected by the user, hence don't trigger the
946 // item specified by 'index'
950 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
951 if (item
.isNull() || m_isContextMenuOpen
) {
955 if (m_toolTipManager
!= 0) {
956 m_toolTipManager
->hideTip();
958 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
961 void DolphinView::emitSelectionChangedSignal()
963 emit
selectionChanged(DolphinView::selectedItems());
966 void DolphinView::openContextMenu(const QPoint
& pos
,
967 const QList
<QAction
*>& customActions
)
970 if (isColumnViewActive()) {
971 item
= m_columnView
->itemAt(pos
);
973 const QModelIndex index
= itemView()->indexAt(pos
);
974 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
975 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
976 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
980 if (m_toolTipManager
!= 0) {
981 m_toolTipManager
->hideTip();
984 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
985 emit
requestContextMenu(item
, url(), customActions
);
986 m_isContextMenuOpen
= false;
989 void DolphinView::dropUrls(const KFileItem
& destItem
,
990 const KUrl
& destPath
,
993 addNewFileNames(event
->mimeData());
994 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
997 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
999 ViewProperties
props(viewPropertiesUrl());
1000 props
.setSorting(sorting
);
1002 m_proxyModel
->setSorting(sorting
);
1004 emit
sortingChanged(sorting
);
1007 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1009 ViewProperties
props(viewPropertiesUrl());
1010 props
.setSortOrder(order
);
1012 m_proxyModel
->setSortOrder(order
);
1014 emit
sortOrderChanged(order
);
1017 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1019 ViewProperties
props(viewPropertiesUrl());
1020 props
.setSortFoldersFirst(foldersFirst
);
1022 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1024 emit
sortFoldersFirstChanged(foldersFirst
);
1027 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1029 ViewProperties
props(viewPropertiesUrl());
1030 props
.setAdditionalInfo(info
);
1033 m_fileItemDelegate
->setShowInformation(info
);
1035 emit
additionalInfoChanged();
1038 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1040 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1041 (m_mode
== DolphinView::IconsView
);
1043 QAction
* showSizeInfo
= collection
->action("show_size_info");
1044 QAction
* showDateInfo
= collection
->action("show_date_info");
1045 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1046 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1047 QAction
* showGroupInfo
= collection
->action("show_group_info");
1048 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1050 showSizeInfo
->setChecked(false);
1051 showDateInfo
->setChecked(false);
1052 showPermissionsInfo
->setChecked(false);
1053 showOwnerInfo
->setChecked(false);
1054 showGroupInfo
->setChecked(false);
1055 showMimeInfo
->setChecked(false);
1057 showSizeInfo
->setEnabled(enable
);
1058 showDateInfo
->setEnabled(enable
);
1059 showPermissionsInfo
->setEnabled(enable
);
1060 showOwnerInfo
->setEnabled(enable
);
1061 showGroupInfo
->setEnabled(enable
);
1062 showMimeInfo
->setEnabled(enable
);
1064 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1066 case KFileItemDelegate::Size
:
1067 showSizeInfo
->setChecked(true);
1069 case KFileItemDelegate::ModificationTime
:
1070 showDateInfo
->setChecked(true);
1072 case KFileItemDelegate::Permissions
:
1073 showPermissionsInfo
->setChecked(true);
1075 case KFileItemDelegate::Owner
:
1076 showOwnerInfo
->setChecked(true);
1078 case KFileItemDelegate::OwnerAndGroup
:
1079 showGroupInfo
->setChecked(true);
1081 case KFileItemDelegate::FriendlyMimeType
:
1082 showMimeInfo
->setChecked(true);
1090 QPair
<bool, QString
> DolphinView::pasteInfo() const
1092 return KonqOperations::pasteInfo(url());
1095 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1097 m_tabsForFiles
= tabsForFiles
;
1100 bool DolphinView::isTabsForFilesEnabled() const
1102 return m_tabsForFiles
;
1105 void DolphinView::activateItem(const KUrl
& url
)
1107 m_activeItemUrl
= url
;
1110 bool DolphinView::itemsExpandable() const
1112 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1115 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1120 if (DragAndDropHelper::instance().isDragSource(view
)) {
1121 // We must store for later deletion.
1122 if (m_expandedDragSource
!= 0) {
1123 // The old stored view is obviously not the drag source anymore.
1124 m_expandedDragSource
->deleteLater();
1125 m_expandedDragSource
= 0;
1128 m_expandedDragSource
= view
;
1131 view
->deleteLater();
1135 void DolphinView::observeCreatedItem(const KUrl
& url
)
1137 m_createdItemUrl
= url
;
1138 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1139 this, SLOT(selectAndScrollToCreatedItem()));
1142 void DolphinView::selectAndScrollToCreatedItem()
1144 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1145 if (dirIndex
.isValid()) {
1146 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1147 itemView()->setCurrentIndex(proxyIndex
);
1150 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1151 this, SLOT(selectAndScrollToCreatedItem()));
1152 m_createdItemUrl
= KUrl();
1155 void DolphinView::restoreSelection()
1157 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1158 changeSelection(m_selectedItems
);
1161 void DolphinView::emitContentsMoved()
1163 // only emit the contents moved signal if:
1164 // - no directory loading is ongoing (this would reset the contents position
1165 // always to (0, 0))
1166 // - if the Column View is active: the column view does an automatic
1167 // positioning during the loading operation, which must be remembered
1168 if (!m_loadingDirectory
|| isColumnViewActive()) {
1169 const QPoint
pos(contentsPosition());
1170 emit
contentsMoved(pos
.x(), pos
.y());
1174 void DolphinView::showHoverInformation(const KFileItem
& item
)
1176 emit
requestItemInfo(item
);
1179 void DolphinView::clearHoverInformation()
1181 emit
requestItemInfo(KFileItem());
1184 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1186 if (job
->error() == 0) {
1187 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1188 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1189 emit
errorMessage(job
->errorString());
1193 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1195 emit
requestUrlChange(url
);
1196 m_controller
->setUrl(url
);
1199 void DolphinView::slotDirListerCompleted()
1201 if (!m_activeItemUrl
.isEmpty()) {
1202 // assure that the current item remains visible
1203 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_activeItemUrl
);
1204 if (dirIndex
.isValid()) {
1205 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1206 QAbstractItemView
* view
= itemView();
1207 const bool clearSelection
= !hasSelection();
1208 view
->setCurrentIndex(proxyIndex
);
1209 if (clearSelection
) {
1210 view
->clearSelection();
1212 m_activeItemUrl
.clear();
1216 if (!m_newFileNames
.isEmpty()) {
1217 // select all newly added items created by a paste operation or
1218 // a drag & drop operation
1219 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
1220 const int rowCount
= m_proxyModel
->rowCount();
1221 for (int row
= 0; row
< rowCount
; ++row
) {
1222 const QModelIndex proxyIndex
= m_proxyModel
->index(row
, 0);
1223 const QModelIndex dirIndex
= m_proxyModel
->mapToSource(proxyIndex
);
1224 const KUrl url
= m_dolphinModel
->itemForIndex(dirIndex
).url();
1225 if (m_newFileNames
.contains(url
.fileName())) {
1226 selectionModel
->select(proxyIndex
, QItemSelectionModel::Select
);
1230 m_newFileNames
.clear();
1234 void DolphinView::slotRefreshItems()
1236 if (m_assureVisibleCurrentIndex
) {
1237 m_assureVisibleCurrentIndex
= false;
1238 itemView()->scrollTo(itemView()->currentIndex());
1242 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1244 if (!url
.isValid()) {
1245 const QString
location(url
.pathOrUrl());
1246 if (location
.isEmpty()) {
1247 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1249 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1254 m_loadingDirectory
= true;
1257 m_selectedItems
= selectedItems();
1258 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1261 m_dirLister
->stop();
1262 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1264 if (isColumnViewActive()) {
1265 // adjusting the directory lister is not enough in the case of the
1266 // column view, as each column has its own directory lister internally...
1268 m_columnView
->reload();
1270 m_columnView
->showColumn(url
);
1275 KUrl
DolphinView::viewPropertiesUrl() const
1277 if (isColumnViewActive()) {
1278 return m_columnView
->rootUrl();
1284 void DolphinView::applyViewProperties(const KUrl
& url
)
1286 if (m_ignoreViewProperties
) {
1290 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1291 // The column view is active, hence don't apply the view properties
1292 // of sub directories (represented by columns) to the view. The
1293 // view always represents the properties of the first column.
1297 const ViewProperties
props(url
);
1299 const Mode mode
= props
.viewMode();
1300 if (m_mode
!= mode
) {
1301 const int oldZoomLevel
= m_controller
->zoomLevel();
1307 updateZoomLevel(oldZoomLevel
);
1309 if (itemView() == 0) {
1312 Q_ASSERT(itemView() != 0);
1313 Q_ASSERT(m_fileItemDelegate
!= 0);
1315 const bool showHiddenFiles
= props
.showHiddenFiles();
1316 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1317 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1318 emit
showHiddenFilesChanged();
1321 m_storedCategorizedSorting
= props
.categorizedSorting();
1322 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1323 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1324 m_proxyModel
->setCategorizedModel(categorized
);
1325 emit
categorizedSortingChanged();
1328 const DolphinView::Sorting sorting
= props
.sorting();
1329 if (sorting
!= m_proxyModel
->sorting()) {
1330 m_proxyModel
->setSorting(sorting
);
1331 emit
sortingChanged(sorting
);
1334 const Qt::SortOrder sortOrder
= props
.sortOrder();
1335 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1336 m_proxyModel
->setSortOrder(sortOrder
);
1337 emit
sortOrderChanged(sortOrder
);
1340 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1341 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1342 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1343 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1346 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1347 if (info
!= m_fileItemDelegate
->showInformation()) {
1348 m_fileItemDelegate
->setShowInformation(info
);
1349 emit
additionalInfoChanged();
1352 const bool showPreview
= props
.showPreview();
1353 if (showPreview
!= m_showPreview
) {
1354 m_showPreview
= showPreview
;
1355 m_previewGenerator
->setPreviewShown(showPreview
);
1357 const int oldZoomLevel
= m_controller
->zoomLevel();
1358 emit
showPreviewChanged();
1360 // Enabling or disabling the preview might change the icon size of the view.
1361 // As the view does not emit a signal when the icon size has been changed,
1362 // the used zoom level of the controller must be adjusted manually:
1363 updateZoomLevel(oldZoomLevel
);
1366 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1367 // During the lifetime of a DolphinView instance the global view properties
1368 // should not be changed. This allows e. g. to split a view and use different
1369 // view properties for each view.
1370 m_ignoreViewProperties
= true;
1374 void DolphinView::createView()
1377 Q_ASSERT(m_iconsView
== 0);
1378 Q_ASSERT(m_detailsView
== 0);
1379 Q_ASSERT(m_columnView
== 0);
1381 QAbstractItemView
* view
= 0;
1384 m_iconsView
= new DolphinIconsView(this, m_controller
);
1390 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1391 view
= m_detailsView
;
1395 m_columnView
= new DolphinColumnView(this, m_controller
);
1396 view
= m_columnView
;
1400 Q_ASSERT(view
!= 0);
1401 view
->installEventFilter(this);
1402 view
->viewport()->installEventFilter(this);
1403 setFocusProxy(view
);
1405 if (m_mode
!= ColumnView
) {
1406 // Give the view the ability to auto-expand its directories on hovering
1407 // (the column view takes care about this itself). If the details view
1408 // uses expandable folders, the auto-expanding should be used always.
1409 DolphinSettings
& settings
= DolphinSettings::instance();
1410 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1411 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1413 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1414 folderExpander
->setEnabled(enabled
);
1415 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1416 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1419 // Listen out for requests to delete the current column.
1420 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1421 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1424 m_controller
->setItemView(view
);
1426 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1427 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1428 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1429 view
->setItemDelegate(m_fileItemDelegate
);
1431 view
->setModel(m_proxyModel
);
1432 if (m_selectionModel
!= 0) {
1433 view
->setSelectionModel(m_selectionModel
);
1435 m_selectionModel
= view
->selectionModel();
1438 // reparent the selection model, as it should not be deleted
1439 // when deleting the model
1440 m_selectionModel
->setParent(this);
1442 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1444 m_previewGenerator
= new KFilePreviewGenerator(view
);
1445 m_previewGenerator
->setPreviewShown(m_showPreview
);
1447 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1448 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1449 connect(m_controller
, SIGNAL(hideToolTip()),
1450 m_toolTipManager
, SLOT(hideTip()));
1453 m_topLayout
->insertWidget(1, view
);
1455 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1456 this, SLOT(emitSelectionChangedSignal()));
1457 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1458 this, SLOT(emitContentsMoved()));
1459 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1460 this, SLOT(emitContentsMoved()));
1463 void DolphinView::deleteView()
1465 QAbstractItemView
* view
= itemView();
1467 // It's important to set the keyboard focus to the parent
1468 // before deleting the view: Otherwise when having a split
1469 // view the other view will get the focus and will request
1470 // an activation (see DolphinView::eventFilter()).
1474 m_topLayout
->removeWidget(view
);
1477 // m_previewGenerator's parent is not always destroyed, and we
1478 // don't want two active at once - manually delete.
1479 delete m_previewGenerator
;
1480 m_previewGenerator
= 0;
1483 m_controller
->disconnect(view
);
1486 deleteWhenNotDragSource(view
);
1492 m_fileItemDelegate
= 0;
1493 m_toolTipManager
= 0;
1497 QAbstractItemView
* DolphinView::itemView() const
1499 if (m_detailsView
!= 0) {
1500 return m_detailsView
;
1501 } else if (m_columnView
!= 0) {
1502 return m_columnView
;
1508 void DolphinView::pasteToUrl(const KUrl
& url
)
1510 addNewFileNames(QApplication::clipboard()->mimeData());
1511 KonqOperations::doPaste(this, url
);
1514 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1516 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1517 if (oldZoomLevel
!= newZoomLevel
) {
1518 m_controller
->setZoomLevel(newZoomLevel
);
1519 emit
zoomLevelChanged(newZoomLevel
);
1523 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1525 KUrl::List list
= selectedUrls();
1526 if (itemsExpandable() ) {
1527 list
= KDirModel::simplifiedUrlList(list
);
1532 QMimeData
* DolphinView::selectionMimeData() const
1534 if (isColumnViewActive()) {
1535 return m_columnView
->selectionMimeData();
1538 const QAbstractItemView
* view
= itemView();
1539 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1540 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1541 return m_dolphinModel
->mimeData(selection
.indexes());
1544 void DolphinView::addNewFileNames(const QMimeData
* mimeData
)
1546 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1547 foreach (const KUrl
& url
, urls
) {
1548 m_newFileNames
.insert(url
.fileName());
1552 #include "dolphinview.moc"