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 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
191 // bypasses the problem when having a split view and changing the active view to
192 // update the some URL dependent states. A nicer approach should be no big deal...
193 emit
urlChanged(url());
194 emit
selectionChanged(selectedItems());
199 QWidget
* viewport
= itemView()->viewport();
201 palette
.setColor(viewport
->backgroundRole(), color
);
202 viewport
->setPalette(palette
);
207 itemView()->setFocus();
211 m_controller
->indicateActivationChange(active
);
214 bool DolphinView::isActive() const
219 void DolphinView::setMode(Mode mode
)
221 if (mode
== m_mode
) {
222 return; // the wished mode is already set
225 const int oldZoomLevel
= m_controller
->zoomLevel();
230 const KUrl viewPropsUrl
= viewPropertiesUrl();
231 ViewProperties
props(viewPropsUrl
);
232 props
.setViewMode(m_mode
);
235 // the file item delegate has been recreated, apply the current
236 // additional information manually
237 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
238 m_fileItemDelegate
->setShowInformation(infoList
);
239 emit
additionalInfoChanged();
241 // Not all view modes support categorized sorting. Adjust the sorting model
242 // if changing the view mode results in a change of the categorized sorting
244 m_storedCategorizedSorting
= props
.categorizedSorting();
245 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
246 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
247 m_proxyModel
->setCategorizedModel(categorized
);
248 emit
categorizedSortingChanged();
253 updateZoomLevel(oldZoomLevel
);
255 loadDirectory(viewPropsUrl
);
259 DolphinView::Mode
DolphinView::mode() const
264 bool DolphinView::showPreview() const
266 return m_showPreview
;
269 bool DolphinView::showHiddenFiles() const
271 return m_dirLister
->showingDotFiles();
274 bool DolphinView::categorizedSorting() const
276 // If all view modes would support categorized sorting, returning
277 // m_proxyModel->isCategorizedModel() would be the way to go. As
278 // currently only the icons view supports caterized sorting, we remember
279 // the stored view properties state in m_storedCategorizedSorting and
280 // return this state. The application takes care to disable the corresponding
281 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
282 // that this setting is not applied to the current view mode.
283 return m_storedCategorizedSorting
;
286 bool DolphinView::supportsCategorizedSorting() const
288 return m_iconsView
!= 0;
291 void DolphinView::selectAll()
293 QAbstractItemView
* view
= itemView();
294 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
295 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
296 // selection instead of selecting all items. This is bypassed for KDE 4.0
297 // by invoking clearSelection() first.
298 view
->clearSelection();
302 void DolphinView::invertSelection()
304 if (isColumnViewActive()) {
305 // QAbstractItemView does not offer a virtual method invertSelection()
306 // as counterpart to QAbstractItemView::selectAll(). This makes it
307 // necessary to delegate the inverting of the selection to the
308 // column view, as only the selection of the active column should
310 m_columnView
->invertSelection();
312 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
313 const QAbstractItemModel
* itemModel
= selectionModel
->model();
315 const QModelIndex topLeft
= itemModel
->index(0, 0);
316 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
317 itemModel
->columnCount() - 1);
319 const QItemSelection
selection(topLeft
, bottomRight
);
320 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
324 bool DolphinView::hasSelection() const
326 return itemView()->selectionModel()->hasSelection();
329 void DolphinView::clearSelection()
331 QItemSelectionModel
* selModel
= itemView()->selectionModel();
332 const QModelIndex currentIndex
= selModel
->currentIndex();
333 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
334 QItemSelectionModel::Clear
);
337 KFileItemList
DolphinView::selectedItems() const
339 if (isColumnViewActive()) {
340 return m_columnView
->selectedItems();
343 const QAbstractItemView
* view
= itemView();
345 // Our view has a selection, we will map them back to the DolphinModel
346 // and then fill the KFileItemList.
347 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
349 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
350 KFileItemList itemList
;
352 const QModelIndexList indexList
= selection
.indexes();
353 foreach (const QModelIndex
&index
, indexList
) {
354 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
355 if (!item
.isNull()) {
356 itemList
.append(item
);
363 KUrl::List
DolphinView::selectedUrls() const
366 const KFileItemList list
= selectedItems();
367 foreach (const KFileItem
&item
, list
) {
368 urls
.append(item
.url());
373 int DolphinView::selectedItemsCount() const
375 if (isColumnViewActive()) {
376 // TODO: get rid of this special case by adjusting the dir lister
377 // to the current column
378 return m_columnView
->selectedItems().count();
381 return itemView()->selectionModel()->selection().count();
384 void DolphinView::setContentsPosition(int x
, int y
)
386 QAbstractItemView
* view
= itemView();
388 // the ColumnView takes care itself for the horizontal scrolling
389 if (!isColumnViewActive()) {
390 view
->horizontalScrollBar()->setValue(x
);
392 view
->verticalScrollBar()->setValue(y
);
394 m_loadingDirectory
= false;
397 QPoint
DolphinView::contentsPosition() const
399 const int x
= itemView()->horizontalScrollBar()->value();
400 const int y
= itemView()->verticalScrollBar()->value();
404 void DolphinView::setZoomLevel(int level
)
406 if (level
< ZoomLevelInfo::minimumLevel()) {
407 level
= ZoomLevelInfo::minimumLevel();
408 } else if (level
> ZoomLevelInfo::maximumLevel()) {
409 level
= ZoomLevelInfo::maximumLevel();
412 if (level
!= zoomLevel()) {
413 m_controller
->setZoomLevel(level
);
414 m_previewGenerator
->updateIcons();
415 emit
zoomLevelChanged(level
);
419 int DolphinView::zoomLevel() const
421 return m_controller
->zoomLevel();
424 void DolphinView::setSorting(Sorting sorting
)
426 if (sorting
!= this->sorting()) {
427 updateSorting(sorting
);
431 DolphinView::Sorting
DolphinView::sorting() const
433 return m_proxyModel
->sorting();
436 void DolphinView::setSortOrder(Qt::SortOrder order
)
438 if (sortOrder() != order
) {
439 updateSortOrder(order
);
443 Qt::SortOrder
DolphinView::sortOrder() const
445 return m_proxyModel
->sortOrder();
448 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
450 if (sortFoldersFirst() != foldersFirst
) {
451 updateSortFoldersFirst(foldersFirst
);
455 bool DolphinView::sortFoldersFirst() const
457 return m_proxyModel
->sortFoldersFirst();
460 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
462 const KUrl viewPropsUrl
= viewPropertiesUrl();
463 ViewProperties
props(viewPropsUrl
);
464 props
.setAdditionalInfo(info
);
465 m_fileItemDelegate
->setShowInformation(info
);
467 emit
additionalInfoChanged();
469 if (itemView() != m_detailsView
) {
470 // the details view requires no reloading of the directory, as it maps
471 // the file item delegate info to its columns internally
472 loadDirectory(viewPropsUrl
);
476 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
478 return m_fileItemDelegate
->showInformation();
481 void DolphinView::reload()
484 loadDirectory(url(), true);
487 void DolphinView::refresh()
489 m_ignoreViewProperties
= false;
491 const bool oldActivationState
= m_active
;
492 const int oldZoomLevel
= m_controller
->zoomLevel();
496 applyViewProperties(m_controller
->url());
499 setActive(oldActivationState
);
500 updateZoomLevel(oldZoomLevel
);
503 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
505 if (m_controller
->url() == url
) {
509 m_previewGenerator
->cancelPreviews();
510 m_controller
->setUrl(url
); // emits urlChanged, which we forward
512 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
513 applyViewProperties(rootUrl
);
514 loadDirectory(rootUrl
);
515 if (itemView() == m_columnView
) {
516 m_columnView
->setRootUrl(rootUrl
);
517 m_columnView
->showColumn(url
);
520 applyViewProperties(url
);
524 emit
startedPathLoading(url
);
527 void DolphinView::setNameFilter(const QString
& nameFilter
)
529 m_proxyModel
->setFilterRegExp(nameFilter
);
531 if (isColumnViewActive()) {
532 // adjusting the directory lister is not enough in the case of the
533 // column view, as each column has its own directory lister internally...
534 m_columnView
->setNameFilter(nameFilter
);
538 void DolphinView::calculateItemCount(int& fileCount
,
540 KIO::filesize_t
& totalFileSize
) const
542 foreach (const KFileItem
& item
, m_dirLister
->items()) {
547 totalFileSize
+= item
.size();
552 QString
DolphinView::statusBarText() const
557 KIO::filesize_t totalFileSize
= 0;
559 if (hasSelection()) {
560 // give a summary of the status of the selected files
561 const KFileItemList list
= selectedItems();
562 if (list
.isEmpty()) {
563 // when an item is triggered, it is temporary selected but selectedItems()
564 // will return an empty list
568 KFileItemList::const_iterator it
= list
.begin();
569 const KFileItemList::const_iterator end
= list
.end();
571 const KFileItem
& item
= *it
;
576 totalFileSize
+= item
.size();
581 if (folderCount
+ fileCount
== 1) {
582 // if only one item is selected, show the filename
583 const QString name
= list
.first().name();
584 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
585 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
586 name
, KIO::convertSize(totalFileSize
));
588 // at least 2 items are selected
589 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
590 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
591 if ((folderCount
> 0) && (fileCount
> 0)) {
592 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
593 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
594 } else if (fileCount
> 0) {
595 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
597 Q_ASSERT(folderCount
> 0);
602 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
603 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
604 fileCount
, folderCount
,
605 totalFileSize
, true);
611 void DolphinView::setUrl(const KUrl
& url
)
613 // remember current item candidate (see restoreCurrentItem())
614 m_currentItemUrl
= url
;
615 updateView(url
, KUrl());
618 void DolphinView::changeSelection(const KFileItemList
& selection
)
621 if (selection
.isEmpty()) {
624 const KUrl
& baseUrl
= url();
626 QItemSelection newSelection
;
627 foreach(const KFileItem
& item
, selection
) {
628 url
= item
.url().upUrl();
629 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
630 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
631 newSelection
.select(index
, index
);
634 itemView()->selectionModel()->select(newSelection
,
635 QItemSelectionModel::ClearAndSelect
636 | QItemSelectionModel::Current
);
639 void DolphinView::renameSelectedItems()
641 KFileItemList items
= selectedItems();
642 const int itemCount
= items
.count();
648 // More than one item has been selected for renaming. Open
649 // a rename dialog and rename all items afterwards.
650 RenameDialog
dialog(this, items
);
651 if (dialog
.exec() == QDialog::Rejected
) {
655 const QString newName
= dialog
.newName();
656 if (newName
.isEmpty()) {
657 emit
errorMessage(dialog
.errorString());
661 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
662 // as one operation instead of n rename operations like it is done now...
663 Q_ASSERT(newName
.contains('#'));
665 // currently the items are sorted by the selection order, resort
666 // them by the file name
667 qSort(items
.begin(), items
.end(), lessThan
);
669 // iterate through all selected items and rename them...
671 foreach (const KFileItem
& item
, items
) {
672 const KUrl
& oldUrl
= item
.url();
674 number
.setNum(index
++);
676 QString name
= newName
;
677 name
.replace('#', number
);
679 if (oldUrl
.fileName() != name
) {
680 KUrl newUrl
= oldUrl
;
681 newUrl
.setFileName(name
);
682 KonqOperations::rename(this, oldUrl
, newUrl
);
685 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
686 Q_ASSERT(itemCount
== 1);
688 if (isColumnViewActive()) {
689 m_columnView
->editItem(items
.first());
691 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
692 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
693 itemView()->edit(proxyIndex
);
696 Q_ASSERT(itemCount
== 1);
698 RenameDialog
dialog(this, items
);
699 if (dialog
.exec() == QDialog::Rejected
) {
703 const QString
& newName
= dialog
.newName();
704 if (newName
.isEmpty()) {
705 emit
errorMessage(dialog
.errorString());
709 const KUrl
& oldUrl
= items
.first().url();
710 KUrl newUrl
= oldUrl
;
711 newUrl
.setFileName(newName
);
712 KonqOperations::rename(this, oldUrl
, newUrl
);
715 // assure that the current index remains visible when KDirLister
716 // will notify the view about changed items
717 m_assureVisibleCurrentIndex
= true;
720 void DolphinView::trashSelectedItems()
722 const KUrl::List list
= simplifiedSelectedUrls();
723 KonqOperations::del(this, KonqOperations::TRASH
, list
);
726 void DolphinView::deleteSelectedItems()
728 const KUrl::List list
= simplifiedSelectedUrls();
729 const bool del
= KonqOperations::askDeleteConfirmation(list
,
731 KonqOperations::DEFAULT_CONFIRMATION
,
735 KIO::Job
* job
= KIO::del(list
);
736 connect(job
, SIGNAL(result(KJob
*)),
737 this, SLOT(slotDeleteFileFinished(KJob
*)));
741 void DolphinView::cutSelectedItems()
743 QMimeData
* mimeData
= selectionMimeData();
744 KonqMimeData::addIsCutSelection(mimeData
, true);
745 QApplication::clipboard()->setMimeData(mimeData
);
748 void DolphinView::copySelectedItems()
750 QMimeData
* mimeData
= selectionMimeData();
751 QApplication::clipboard()->setMimeData(mimeData
);
754 void DolphinView::paste()
759 void DolphinView::pasteIntoFolder()
761 const KFileItemList items
= selectedItems();
762 if ((items
.count() == 1) && items
.first().isDir()) {
763 pasteToUrl(items
.first().url());
767 void DolphinView::setShowPreview(bool show
)
769 if (m_showPreview
== show
) {
773 const KUrl viewPropsUrl
= viewPropertiesUrl();
774 ViewProperties
props(viewPropsUrl
);
775 props
.setShowPreview(show
);
777 m_showPreview
= show
;
778 m_previewGenerator
->setPreviewShown(show
);
780 const int oldZoomLevel
= m_controller
->zoomLevel();
781 emit
showPreviewChanged();
783 // Enabling or disabling the preview might change the icon size of the view.
784 // As the view does not emit a signal when the icon size has been changed,
785 // the used zoom level of the controller must be adjusted manually:
786 updateZoomLevel(oldZoomLevel
);
788 loadDirectory(viewPropsUrl
);
791 void DolphinView::setShowHiddenFiles(bool show
)
793 if (m_dirLister
->showingDotFiles() == show
) {
797 const KUrl viewPropsUrl
= viewPropertiesUrl();
798 ViewProperties
props(viewPropsUrl
);
799 props
.setShowHiddenFiles(show
);
801 m_dirLister
->setShowingDotFiles(show
);
802 emit
showHiddenFilesChanged();
804 loadDirectory(viewPropsUrl
);
807 void DolphinView::setCategorizedSorting(bool categorized
)
809 if (categorized
== categorizedSorting()) {
813 // setCategorizedSorting(true) may only get invoked
814 // if the view supports categorized sorting
815 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
817 ViewProperties
props(viewPropertiesUrl());
818 props
.setCategorizedSorting(categorized
);
821 m_storedCategorizedSorting
= categorized
;
822 m_proxyModel
->setCategorizedModel(categorized
);
824 emit
categorizedSortingChanged();
827 void DolphinView::toggleSortOrder()
829 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
830 Qt::DescendingOrder
:
835 void DolphinView::toggleSortFoldersFirst()
837 setSortFoldersFirst(!sortFoldersFirst());
840 void DolphinView::toggleAdditionalInfo(QAction
* action
)
842 const KFileItemDelegate::Information info
=
843 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
845 KFileItemDelegate::InformationList list
= additionalInfo();
847 const bool show
= action
->isChecked();
849 const int index
= list
.indexOf(info
);
850 const bool containsInfo
= (index
>= 0);
851 if (show
&& !containsInfo
) {
853 setAdditionalInfo(list
);
854 } else if (!show
&& containsInfo
) {
855 list
.removeAt(index
);
856 setAdditionalInfo(list
);
857 Q_ASSERT(list
.indexOf(info
) < 0);
861 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
863 QWidget::mouseReleaseEvent(event
);
867 void DolphinView::wheelEvent(QWheelEvent
* event
)
869 if (event
->modifiers() & Qt::ControlModifier
) {
870 const int delta
= event
->delta();
871 const int level
= zoomLevel();
873 setZoomLevel(level
+ 1);
874 } else if (delta
< 0) {
875 setZoomLevel(level
- 1);
881 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
883 switch (event
->type()) {
884 case QEvent::FocusIn
:
885 if (watched
== itemView()) {
886 m_controller
->requestActivation();
890 case QEvent::MouseButtonPress
:
891 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
892 // Listening to a mousebutton press event to delete expanded views is a
893 // workaround, as it seems impossible for the FolderExpander to know when
894 // a dragging outside a view has been finished. However it works quite well:
895 // A mousebutton press event indicates that a drag operation must be
897 m_expandedDragSource
->deleteLater();
898 m_expandedDragSource
= 0;
902 case QEvent::DragEnter
:
903 if (watched
== itemView()->viewport()) {
908 case QEvent::KeyPress
:
909 if (watched
== itemView()) {
910 if (m_toolTipManager
!= 0) {
911 m_toolTipManager
->hideTip();
914 // clear the selection when Escape has been pressed
915 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
916 if (keyEvent
->key() == Qt::Key_Escape
) {
926 return QWidget::eventFilter(watched
, event
);
929 void DolphinView::activate()
934 void DolphinView::triggerItem(const KFileItem
& item
)
936 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
937 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
938 // items are selected by the user, hence don't trigger the
939 // item specified by 'index'
943 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
944 if (item
.isNull() || m_isContextMenuOpen
) {
948 if (m_toolTipManager
!= 0) {
949 m_toolTipManager
->hideTip();
951 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
954 void DolphinView::emitSelectionChangedSignal()
956 emit
selectionChanged(DolphinView::selectedItems());
959 void DolphinView::openContextMenu(const QPoint
& pos
,
960 const QList
<QAction
*>& customActions
)
963 if (isColumnViewActive()) {
964 item
= m_columnView
->itemAt(pos
);
966 const QModelIndex index
= itemView()->indexAt(pos
);
967 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
968 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
969 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
973 if (m_toolTipManager
!= 0) {
974 m_toolTipManager
->hideTip();
977 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
978 emit
requestContextMenu(item
, url(), customActions
);
979 m_isContextMenuOpen
= false;
982 void DolphinView::dropUrls(const KFileItem
& destItem
,
983 const KUrl
& destPath
,
986 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
989 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
991 ViewProperties
props(viewPropertiesUrl());
992 props
.setSorting(sorting
);
994 m_proxyModel
->setSorting(sorting
);
996 emit
sortingChanged(sorting
);
999 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1001 ViewProperties
props(viewPropertiesUrl());
1002 props
.setSortOrder(order
);
1004 m_proxyModel
->setSortOrder(order
);
1006 emit
sortOrderChanged(order
);
1009 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1011 ViewProperties
props(viewPropertiesUrl());
1012 props
.setSortFoldersFirst(foldersFirst
);
1014 m_proxyModel
->setSortFoldersFirst(foldersFirst
);
1016 emit
sortFoldersFirstChanged(foldersFirst
);
1019 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
1021 ViewProperties
props(viewPropertiesUrl());
1022 props
.setAdditionalInfo(info
);
1025 m_fileItemDelegate
->setShowInformation(info
);
1027 emit
additionalInfoChanged();
1030 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
1032 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
1033 (m_mode
== DolphinView::IconsView
);
1035 QAction
* showSizeInfo
= collection
->action("show_size_info");
1036 QAction
* showDateInfo
= collection
->action("show_date_info");
1037 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1038 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1039 QAction
* showGroupInfo
= collection
->action("show_group_info");
1040 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1042 showSizeInfo
->setChecked(false);
1043 showDateInfo
->setChecked(false);
1044 showPermissionsInfo
->setChecked(false);
1045 showOwnerInfo
->setChecked(false);
1046 showGroupInfo
->setChecked(false);
1047 showMimeInfo
->setChecked(false);
1049 showSizeInfo
->setEnabled(enable
);
1050 showDateInfo
->setEnabled(enable
);
1051 showPermissionsInfo
->setEnabled(enable
);
1052 showOwnerInfo
->setEnabled(enable
);
1053 showGroupInfo
->setEnabled(enable
);
1054 showMimeInfo
->setEnabled(enable
);
1056 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1058 case KFileItemDelegate::Size
:
1059 showSizeInfo
->setChecked(true);
1061 case KFileItemDelegate::ModificationTime
:
1062 showDateInfo
->setChecked(true);
1064 case KFileItemDelegate::Permissions
:
1065 showPermissionsInfo
->setChecked(true);
1067 case KFileItemDelegate::Owner
:
1068 showOwnerInfo
->setChecked(true);
1070 case KFileItemDelegate::OwnerAndGroup
:
1071 showGroupInfo
->setChecked(true);
1073 case KFileItemDelegate::FriendlyMimeType
:
1074 showMimeInfo
->setChecked(true);
1082 QPair
<bool, QString
> DolphinView::pasteInfo() const
1084 return KonqOperations::pasteInfo(url());
1087 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1089 m_tabsForFiles
= tabsForFiles
;
1092 bool DolphinView::isTabsForFilesEnabled() const
1094 return m_tabsForFiles
;
1097 bool DolphinView::itemsExpandable() const
1099 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1102 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1107 if (DragAndDropHelper::instance().isDragSource(view
)) {
1108 // We must store for later deletion.
1109 if (m_expandedDragSource
!= 0) {
1110 // The old stored view is obviously not the drag source anymore.
1111 m_expandedDragSource
->deleteLater();
1112 m_expandedDragSource
= 0;
1115 m_expandedDragSource
= view
;
1118 view
->deleteLater();
1122 void DolphinView::observeCreatedItem(const KUrl
& url
)
1124 m_createdItemUrl
= url
;
1125 connect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1126 this, SLOT(selectAndScrollToCreatedItem()));
1129 void DolphinView::selectAndScrollToCreatedItem()
1131 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_createdItemUrl
);
1132 if (dirIndex
.isValid()) {
1133 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1134 itemView()->setCurrentIndex(proxyIndex
);
1137 disconnect(m_dolphinModel
, SIGNAL(rowsInserted(const QModelIndex
&, int, int)),
1138 this, SLOT(selectAndScrollToCreatedItem()));
1139 m_createdItemUrl
= KUrl();
1142 void DolphinView::restoreSelection()
1144 disconnect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1145 changeSelection(m_selectedItems
);
1148 void DolphinView::emitContentsMoved()
1150 // only emit the contents moved signal if:
1151 // - no directory loading is ongoing (this would reset the contents position
1152 // always to (0, 0))
1153 // - if the Column View is active: the column view does an automatic
1154 // positioning during the loading operation, which must be remembered
1155 if (!m_loadingDirectory
|| isColumnViewActive()) {
1156 const QPoint
pos(contentsPosition());
1157 emit
contentsMoved(pos
.x(), pos
.y());
1161 void DolphinView::showHoverInformation(const KFileItem
& item
)
1163 emit
requestItemInfo(item
);
1166 void DolphinView::clearHoverInformation()
1168 emit
requestItemInfo(KFileItem());
1171 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1173 if (job
->error() == 0) {
1174 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1175 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1176 emit
errorMessage(job
->errorString());
1180 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1182 emit
requestUrlChange(url
);
1183 m_controller
->setUrl(url
);
1186 void DolphinView::restoreCurrentItem()
1188 if (!m_currentItemUrl
.isEmpty()) {
1189 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1190 if (dirIndex
.isValid()) {
1191 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1192 QAbstractItemView
* view
= itemView();
1193 const bool clearSelection
= !hasSelection();
1194 view
->setCurrentIndex(proxyIndex
);
1195 if (clearSelection
) {
1196 view
->clearSelection();
1199 m_currentItemUrl
.clear();
1203 void DolphinView::slotRefreshItems()
1205 if (m_assureVisibleCurrentIndex
) {
1206 m_assureVisibleCurrentIndex
= false;
1207 itemView()->scrollTo(itemView()->currentIndex());
1211 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1213 if (!url
.isValid()) {
1214 const QString
location(url
.pathOrUrl());
1215 if (location
.isEmpty()) {
1216 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1218 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1223 m_loadingDirectory
= true;
1226 m_selectedItems
= selectedItems();
1227 connect(m_dirLister
, SIGNAL(completed()), this, SLOT(restoreSelection()));
1230 m_dirLister
->stop();
1231 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1233 if (isColumnViewActive()) {
1234 // adjusting the directory lister is not enough in the case of the
1235 // column view, as each column has its own directory lister internally...
1237 m_columnView
->reload();
1239 m_columnView
->showColumn(url
);
1244 KUrl
DolphinView::viewPropertiesUrl() const
1246 if (isColumnViewActive()) {
1247 return m_columnView
->rootUrl();
1253 void DolphinView::applyViewProperties(const KUrl
& url
)
1255 if (m_ignoreViewProperties
) {
1259 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1260 // The column view is active, hence don't apply the view properties
1261 // of sub directories (represented by columns) to the view. The
1262 // view always represents the properties of the first column.
1266 const ViewProperties
props(url
);
1268 const Mode mode
= props
.viewMode();
1269 if (m_mode
!= mode
) {
1270 const int oldZoomLevel
= m_controller
->zoomLevel();
1276 updateZoomLevel(oldZoomLevel
);
1278 if (itemView() == 0) {
1281 Q_ASSERT(itemView() != 0);
1282 Q_ASSERT(m_fileItemDelegate
!= 0);
1284 const bool showHiddenFiles
= props
.showHiddenFiles();
1285 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1286 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1287 emit
showHiddenFilesChanged();
1290 m_storedCategorizedSorting
= props
.categorizedSorting();
1291 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1292 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1293 m_proxyModel
->setCategorizedModel(categorized
);
1294 emit
categorizedSortingChanged();
1297 const DolphinView::Sorting sorting
= props
.sorting();
1298 if (sorting
!= m_proxyModel
->sorting()) {
1299 m_proxyModel
->setSorting(sorting
);
1300 emit
sortingChanged(sorting
);
1303 const Qt::SortOrder sortOrder
= props
.sortOrder();
1304 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1305 m_proxyModel
->setSortOrder(sortOrder
);
1306 emit
sortOrderChanged(sortOrder
);
1309 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1310 if (sortFoldersFirst
!= m_proxyModel
->sortFoldersFirst()) {
1311 m_proxyModel
->setSortFoldersFirst(sortFoldersFirst
);
1312 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1315 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1316 if (info
!= m_fileItemDelegate
->showInformation()) {
1317 m_fileItemDelegate
->setShowInformation(info
);
1318 emit
additionalInfoChanged();
1321 const bool showPreview
= props
.showPreview();
1322 if (showPreview
!= m_showPreview
) {
1323 m_showPreview
= showPreview
;
1324 m_previewGenerator
->setPreviewShown(showPreview
);
1326 const int oldZoomLevel
= m_controller
->zoomLevel();
1327 emit
showPreviewChanged();
1329 // Enabling or disabling the preview might change the icon size of the view.
1330 // As the view does not emit a signal when the icon size has been changed,
1331 // the used zoom level of the controller must be adjusted manually:
1332 updateZoomLevel(oldZoomLevel
);
1335 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1336 // During the lifetime of a DolphinView instance the global view properties
1337 // should not be changed. This allows e. g. to split a view and use different
1338 // view properties for each view.
1339 m_ignoreViewProperties
= true;
1343 void DolphinView::createView()
1346 Q_ASSERT(m_iconsView
== 0);
1347 Q_ASSERT(m_detailsView
== 0);
1348 Q_ASSERT(m_columnView
== 0);
1350 QAbstractItemView
* view
= 0;
1353 m_iconsView
= new DolphinIconsView(this, m_controller
);
1359 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1360 view
= m_detailsView
;
1364 m_columnView
= new DolphinColumnView(this, m_controller
);
1365 view
= m_columnView
;
1369 Q_ASSERT(view
!= 0);
1370 view
->installEventFilter(this);
1371 view
->viewport()->installEventFilter(this);
1372 setFocusProxy(view
);
1374 if (m_mode
!= ColumnView
) {
1375 // Give the view the ability to auto-expand its directories on hovering
1376 // (the column view takes care about this itself). If the details view
1377 // uses expandable folders, the auto-expanding should be used always.
1378 DolphinSettings
& settings
= DolphinSettings::instance();
1379 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1380 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1382 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1383 folderExpander
->setEnabled(enabled
);
1384 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1385 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1388 // Listen out for requests to delete the current column.
1389 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1390 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1393 m_controller
->setItemView(view
);
1395 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1396 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1397 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1398 view
->setItemDelegate(m_fileItemDelegate
);
1400 view
->setModel(m_proxyModel
);
1401 if (m_selectionModel
!= 0) {
1402 view
->setSelectionModel(m_selectionModel
);
1404 m_selectionModel
= view
->selectionModel();
1407 // reparent the selection model, as it should not be deleted
1408 // when deleting the model
1409 m_selectionModel
->setParent(this);
1411 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1413 m_previewGenerator
= new KFilePreviewGenerator(view
);
1414 m_previewGenerator
->setPreviewShown(m_showPreview
);
1416 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1417 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1418 connect(m_controller
, SIGNAL(hideToolTip()),
1419 m_toolTipManager
, SLOT(hideTip()));
1422 m_topLayout
->insertWidget(1, view
);
1424 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1425 this, SLOT(emitSelectionChangedSignal()));
1426 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1427 this, SLOT(emitContentsMoved()));
1428 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1429 this, SLOT(emitContentsMoved()));
1432 void DolphinView::deleteView()
1434 QAbstractItemView
* view
= itemView();
1436 // It's important to set the keyboard focus to the parent
1437 // before deleting the view: Otherwise when having a split
1438 // view the other view will get the focus and will request
1439 // an activation (see DolphinView::eventFilter()).
1443 m_topLayout
->removeWidget(view
);
1446 // m_previewGenerator's parent is not always destroyed, and we
1447 // don't want two active at once - manually delete.
1448 delete m_previewGenerator
;
1449 m_previewGenerator
= 0;
1452 m_controller
->disconnect(view
);
1455 deleteWhenNotDragSource(view
);
1461 m_fileItemDelegate
= 0;
1462 m_toolTipManager
= 0;
1466 QAbstractItemView
* DolphinView::itemView() const
1468 if (m_detailsView
!= 0) {
1469 return m_detailsView
;
1470 } else if (m_columnView
!= 0) {
1471 return m_columnView
;
1477 bool DolphinView::isCutItem(const KFileItem
& item
) const
1479 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1480 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1482 const KUrl
& itemUrl
= item
.url();
1483 KUrl::List::const_iterator it
= cutUrls
.begin();
1484 const KUrl::List::const_iterator end
= cutUrls
.end();
1486 if (*it
== itemUrl
) {
1495 void DolphinView::pasteToUrl(const KUrl
& url
)
1497 KonqOperations::doPaste(this, url
);
1500 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1502 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1503 if (oldZoomLevel
!= newZoomLevel
) {
1504 m_controller
->setZoomLevel(newZoomLevel
);
1505 emit
zoomLevelChanged(newZoomLevel
);
1509 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1511 KUrl::List list
= selectedUrls();
1512 if (itemsExpandable() ) {
1513 list
= KDirModel::simplifiedUrlList(list
);
1518 QMimeData
* DolphinView::selectionMimeData() const
1520 if (isColumnViewActive()) {
1521 return m_columnView
->selectionMimeData();
1524 const QAbstractItemView
* view
= itemView();
1525 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1526 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1527 return m_dolphinModel
->mimeData(selection
.indexes());
1531 #include "dolphinview.moc"