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 "dolphinfileitemdelegate.h"
56 #include "dolphinsortfilterproxymodel.h"
57 #include "dolphindetailsview.h"
58 #include "dolphin_detailsmodesettings.h"
59 #include "dolphiniconsview.h"
60 #include "settings/dolphinsettings.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 "viewproperties.h"
67 #include "zoomlevelinfo.h"
70 * Helper function for sorting items with qSort() in
71 * DolphinView::renameSelectedItems().
73 bool lessThan(const KFileItem
& item1
, const KFileItem
& item2
)
75 return KStringHandler::naturalCompare(item1
.name(), item2
.name()) < 0;
78 DolphinView::DolphinView(QWidget
* parent
,
80 KDirLister
* dirLister
,
81 DolphinModel
* dolphinModel
,
82 DolphinSortFilterProxyModel
* proxyModel
) :
86 m_loadingDirectory(false),
87 m_storedCategorizedSorting(false),
88 m_tabsForFiles(false),
89 m_isContextMenuOpen(false),
90 m_ignoreViewProperties(false),
91 m_assureVisibleCurrentIndex(false),
92 m_mode(DolphinView::IconsView
),
98 m_fileItemDelegate(0),
100 m_dolphinModel(dolphinModel
),
101 m_dirLister(dirLister
),
102 m_proxyModel(proxyModel
),
103 m_previewGenerator(0),
107 m_expandedDragSource(0)
109 m_topLayout
= new QVBoxLayout(this);
110 m_topLayout
->setSpacing(0);
111 m_topLayout
->setMargin(0);
113 m_controller
= new DolphinController(this);
114 m_controller
->setUrl(url
);
116 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
117 this, SIGNAL(urlChanged(const KUrl
&)));
118 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
119 this, SLOT(slotRequestUrlChange(const KUrl
&)));
121 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
122 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
123 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
124 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
125 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
126 this, SLOT(updateSorting(DolphinView::Sorting
)));
127 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
128 this, SLOT(updateSortOrder(Qt::SortOrder
)));
129 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
130 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
131 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
132 this, SLOT(triggerItem(const KFileItem
&)));
133 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
134 this, SIGNAL(tabRequested(const KUrl
&)));
135 connect(m_controller
, SIGNAL(activated()),
136 this, SLOT(activate()));
137 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
138 this, SLOT(showHoverInformation(const KFileItem
&)));
139 connect(m_controller
, SIGNAL(viewportEntered()),
140 this, SLOT(clearHoverInformation()));
142 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
143 this, SIGNAL(redirection(KUrl
, KUrl
)));
144 connect(m_dirLister
, SIGNAL(completed()),
145 this, SLOT(restoreCurrentItem()));
146 connect(m_dirLister
, SIGNAL(refreshItems(const QList
<QPair
<KFileItem
,KFileItem
>>&)),
147 this, SLOT(slotRefreshItems()));
149 applyViewProperties(url
);
150 m_topLayout
->addWidget(itemView());
153 DolphinView::~DolphinView()
155 delete m_expandedDragSource
;
156 m_expandedDragSource
= 0;
159 const KUrl
& DolphinView::url() const
161 return m_controller
->url();
164 KUrl
DolphinView::rootUrl() const
166 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
169 void DolphinView::setActive(bool active
)
171 if (active
== m_active
) {
177 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
179 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
180 // bypasses the problem when having a split view and changing the active view to
181 // update the some URL dependent states. A nicer approach should be no big deal...
182 emit
urlChanged(url());
183 emit
selectionChanged(selectedItems());
188 QWidget
* viewport
= itemView()->viewport();
190 palette
.setColor(viewport
->backgroundRole(), color
);
191 viewport
->setPalette(palette
);
196 itemView()->setFocus();
200 m_controller
->indicateActivationChange(active
);
203 bool DolphinView::isActive() const
208 void DolphinView::setMode(Mode mode
)
210 if (mode
== m_mode
) {
211 return; // the wished mode is already set
214 const int oldZoomLevel
= m_controller
->zoomLevel();
219 const KUrl viewPropsUrl
= viewPropertiesUrl();
220 ViewProperties
props(viewPropsUrl
);
221 props
.setViewMode(m_mode
);
224 // the file item delegate has been recreated, apply the current
225 // additional information manually
226 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
227 m_fileItemDelegate
->setShowInformation(infoList
);
228 emit
additionalInfoChanged();
230 // Not all view modes support categorized sorting. Adjust the sorting model
231 // if changing the view mode results in a change of the categorized sorting
233 m_storedCategorizedSorting
= props
.categorizedSorting();
234 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
235 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
236 m_proxyModel
->setCategorizedModel(categorized
);
237 emit
categorizedSortingChanged();
242 updateZoomLevel(oldZoomLevel
);
244 loadDirectory(viewPropsUrl
);
248 DolphinView::Mode
DolphinView::mode() const
253 bool DolphinView::showPreview() const
255 return m_showPreview
;
258 bool DolphinView::showHiddenFiles() const
260 return m_dirLister
->showingDotFiles();
263 bool DolphinView::categorizedSorting() const
265 // If all view modes would support categorized sorting, returning
266 // m_proxyModel->isCategorizedModel() would be the way to go. As
267 // currently only the icons view supports caterized sorting, we remember
268 // the stored view properties state in m_storedCategorizedSorting and
269 // return this state. The application takes care to disable the corresponding
270 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
271 // that this setting is not applied to the current view mode.
272 return m_storedCategorizedSorting
;
275 bool DolphinView::supportsCategorizedSorting() const
277 return m_iconsView
!= 0;
280 void DolphinView::selectAll()
282 QAbstractItemView
* view
= itemView();
283 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
284 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
285 // selection instead of selecting all items. This is bypassed for KDE 4.0
286 // by invoking clearSelection() first.
287 view
->clearSelection();
291 void DolphinView::invertSelection()
293 if (isColumnViewActive()) {
294 // QAbstractItemView does not offer a virtual method invertSelection()
295 // as counterpart to QAbstractItemView::selectAll(). This makes it
296 // necessary to delegate the inverting of the selection to the
297 // column view, as only the selection of the active column should
299 m_columnView
->invertSelection();
301 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
302 const QAbstractItemModel
* itemModel
= selectionModel
->model();
304 const QModelIndex topLeft
= itemModel
->index(0, 0);
305 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
306 itemModel
->columnCount() - 1);
308 const QItemSelection
selection(topLeft
, bottomRight
);
309 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
313 bool DolphinView::hasSelection() const
315 return itemView()->selectionModel()->hasSelection();
318 void DolphinView::clearSelection()
320 QItemSelectionModel
* selModel
= itemView()->selectionModel();
321 const QModelIndex currentIndex
= selModel
->currentIndex();
322 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
323 QItemSelectionModel::Clear
);
326 KFileItemList
DolphinView::selectedItems() const
328 if (isColumnViewActive()) {
329 return m_columnView
->selectedItems();
332 const QAbstractItemView
* view
= itemView();
334 // Our view has a selection, we will map them back to the DolphinModel
335 // and then fill the KFileItemList.
336 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
338 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
339 KFileItemList itemList
;
341 const QModelIndexList indexList
= selection
.indexes();
342 foreach (const QModelIndex
&index
, indexList
) {
343 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
344 if (!item
.isNull()) {
345 itemList
.append(item
);
352 KUrl::List
DolphinView::selectedUrls() const
355 const KFileItemList list
= selectedItems();
356 foreach (const KFileItem
&item
, list
) {
357 urls
.append(item
.url());
362 int DolphinView::selectedItemsCount() const
364 if (isColumnViewActive()) {
365 // TODO: get rid of this special case by adjusting the dir lister
366 // to the current column
367 return m_columnView
->selectedItems().count();
370 return itemView()->selectionModel()->selection().count();
373 void DolphinView::setContentsPosition(int x
, int y
)
375 QAbstractItemView
* view
= itemView();
377 // the ColumnView takes care itself for the horizontal scrolling
378 if (!isColumnViewActive()) {
379 view
->horizontalScrollBar()->setValue(x
);
381 view
->verticalScrollBar()->setValue(y
);
383 m_loadingDirectory
= false;
386 QPoint
DolphinView::contentsPosition() const
388 const int x
= itemView()->horizontalScrollBar()->value();
389 const int y
= itemView()->verticalScrollBar()->value();
393 void DolphinView::setZoomLevel(int level
)
395 if (level
< ZoomLevelInfo::minimumLevel()) {
396 level
= ZoomLevelInfo::minimumLevel();
397 } else if (level
> ZoomLevelInfo::maximumLevel()) {
398 level
= ZoomLevelInfo::maximumLevel();
401 if (level
!= zoomLevel()) {
402 m_controller
->setZoomLevel(level
);
403 m_previewGenerator
->updatePreviews();
404 emit
zoomLevelChanged(level
);
408 int DolphinView::zoomLevel() const
410 return m_controller
->zoomLevel();
413 void DolphinView::setSorting(Sorting sorting
)
415 if (sorting
!= this->sorting()) {
416 updateSorting(sorting
);
420 DolphinView::Sorting
DolphinView::sorting() const
422 return m_proxyModel
->sorting();
425 void DolphinView::setSortOrder(Qt::SortOrder order
)
427 if (sortOrder() != order
) {
428 updateSortOrder(order
);
432 Qt::SortOrder
DolphinView::sortOrder() const
434 return m_proxyModel
->sortOrder();
437 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
439 const KUrl viewPropsUrl
= viewPropertiesUrl();
440 ViewProperties
props(viewPropsUrl
);
441 props
.setAdditionalInfo(info
);
442 m_fileItemDelegate
->setShowInformation(info
);
444 emit
additionalInfoChanged();
446 if (itemView() != m_detailsView
) {
447 // the details view requires no reloading of the directory, as it maps
448 // the file item delegate info to its columns internally
449 loadDirectory(viewPropsUrl
);
453 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
455 return m_fileItemDelegate
->showInformation();
458 void DolphinView::reload()
461 loadDirectory(url(), true);
464 void DolphinView::refresh()
466 m_ignoreViewProperties
= false;
468 const bool oldActivationState
= m_active
;
469 const int oldZoomLevel
= m_controller
->zoomLevel();
473 applyViewProperties(m_controller
->url());
476 setActive(oldActivationState
);
477 updateZoomLevel(oldZoomLevel
);
480 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
482 if (m_controller
->url() == url
) {
486 m_previewGenerator
->cancelPreviews();
487 m_controller
->setUrl(url
); // emits urlChanged, which we forward
489 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
490 applyViewProperties(rootUrl
);
491 loadDirectory(rootUrl
);
492 if (itemView() == m_columnView
) {
493 m_columnView
->setRootUrl(rootUrl
);
494 m_columnView
->showColumn(url
);
497 applyViewProperties(url
);
501 emit
startedPathLoading(url
);
504 void DolphinView::setNameFilter(const QString
& nameFilter
)
506 m_proxyModel
->setFilterRegExp(nameFilter
);
508 if (isColumnViewActive()) {
509 // adjusting the directory lister is not enough in the case of the
510 // column view, as each column has its own directory lister internally...
511 m_columnView
->setNameFilter(nameFilter
);
515 void DolphinView::calculateItemCount(int& fileCount
,
517 KIO::filesize_t
& totalFileSize
) const
519 foreach (const KFileItem
& item
, m_dirLister
->items()) {
524 totalFileSize
+= item
.size();
529 QString
DolphinView::statusBarText() const
534 KIO::filesize_t totalFileSize
= 0;
536 if (hasSelection()) {
537 // give a summary of the status of the selected files
538 const KFileItemList list
= selectedItems();
539 if (list
.isEmpty()) {
540 // when an item is triggered, it is temporary selected but selectedItems()
541 // will return an empty list
545 KFileItemList::const_iterator it
= list
.begin();
546 const KFileItemList::const_iterator end
= list
.end();
548 const KFileItem
& item
= *it
;
553 totalFileSize
+= item
.size();
558 if (folderCount
+ fileCount
== 1) {
559 // if only one item is selected, show the filename
560 const QString name
= list
.first().name();
561 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
562 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
563 name
, KIO::convertSize(totalFileSize
));
565 // at least 2 items are selected
566 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
567 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
568 if ((folderCount
> 0) && (fileCount
> 0)) {
569 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
570 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
571 } else if (fileCount
> 0) {
572 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
574 Q_ASSERT(folderCount
> 0);
579 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
580 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
581 fileCount
, folderCount
,
582 totalFileSize
, true);
588 void DolphinView::setUrl(const KUrl
& url
)
590 // remember current item candidate (see restoreCurrentItem())
591 m_currentItemUrl
= url
;
592 updateView(url
, KUrl());
595 void DolphinView::changeSelection(const KFileItemList
& selection
)
598 if (selection
.isEmpty()) {
601 const KUrl
& baseUrl
= url();
603 QItemSelection new_selection
;
604 foreach(const KFileItem
& item
, selection
) {
605 url
= item
.url().upUrl();
606 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
607 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
608 new_selection
.select(index
, index
);
611 itemView()->selectionModel()->select(new_selection
,
612 QItemSelectionModel::ClearAndSelect
613 | QItemSelectionModel::Current
);
616 void DolphinView::renameSelectedItems()
618 KFileItemList items
= selectedItems();
619 const int itemCount
= items
.count();
625 // More than one item has been selected for renaming. Open
626 // a rename dialog and rename all items afterwards.
627 RenameDialog
dialog(this, items
);
628 if (dialog
.exec() == QDialog::Rejected
) {
632 const QString newName
= dialog
.newName();
633 if (newName
.isEmpty()) {
634 emit
errorMessage(dialog
.errorString());
638 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
639 // as one operation instead of n rename operations like it is done now...
640 Q_ASSERT(newName
.contains('#'));
642 // currently the items are sorted by the selection order, resort
643 // them by the file name
644 qSort(items
.begin(), items
.end(), lessThan
);
646 // iterate through all selected items and rename them...
648 foreach (const KFileItem
& item
, items
) {
649 const KUrl
& oldUrl
= item
.url();
651 number
.setNum(index
++);
653 QString name
= newName
;
654 name
.replace('#', number
);
656 if (oldUrl
.fileName() != name
) {
657 KUrl newUrl
= oldUrl
;
658 newUrl
.setFileName(name
);
659 KonqOperations::rename(this, oldUrl
, newUrl
);
662 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
663 Q_ASSERT(itemCount
== 1);
665 if (isColumnViewActive()) {
666 m_columnView
->editItem(items
.first());
668 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
669 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
670 itemView()->edit(proxyIndex
);
673 Q_ASSERT(itemCount
== 1);
675 RenameDialog
dialog(this, items
);
676 if (dialog
.exec() == QDialog::Rejected
) {
680 const QString
& newName
= dialog
.newName();
681 if (newName
.isEmpty()) {
682 emit
errorMessage(dialog
.errorString());
686 const KUrl
& oldUrl
= items
.first().url();
687 KUrl newUrl
= oldUrl
;
688 newUrl
.setFileName(newName
);
689 KonqOperations::rename(this, oldUrl
, newUrl
);
692 // assure that the current index remains visible when KDirLister
693 // will notify the view about changed items
694 m_assureVisibleCurrentIndex
= true;
697 void DolphinView::trashSelectedItems()
699 const KUrl::List list
= simplifiedSelectedUrls();
700 KonqOperations::del(this, KonqOperations::TRASH
, list
);
703 void DolphinView::deleteSelectedItems()
705 const KUrl::List list
= simplifiedSelectedUrls();
706 const bool del
= KonqOperations::askDeleteConfirmation(list
,
708 KonqOperations::DEFAULT_CONFIRMATION
,
712 KIO::Job
* job
= KIO::del(list
);
713 connect(job
, SIGNAL(result(KJob
*)),
714 this, SLOT(slotDeleteFileFinished(KJob
*)));
718 void DolphinView::cutSelectedItems()
720 QMimeData
* mimeData
= selectionMimeData();
721 KonqMimeData::addIsCutSelection(mimeData
, true);
722 QApplication::clipboard()->setMimeData(mimeData
);
725 void DolphinView::copySelectedItems()
727 QMimeData
* mimeData
= selectionMimeData();
728 QApplication::clipboard()->setMimeData(mimeData
);
731 void DolphinView::paste()
736 void DolphinView::pasteIntoFolder()
738 const KFileItemList items
= selectedItems();
739 if ((items
.count() == 1) && items
.first().isDir()) {
740 pasteToUrl(items
.first().url());
744 void DolphinView::setShowPreview(bool show
)
746 if (m_showPreview
== show
) {
750 const KUrl viewPropsUrl
= viewPropertiesUrl();
751 ViewProperties
props(viewPropsUrl
);
752 props
.setShowPreview(show
);
754 m_showPreview
= show
;
755 m_previewGenerator
->setPreviewShown(show
);
757 const int oldZoomLevel
= m_controller
->zoomLevel();
758 emit
showPreviewChanged();
760 // Enabling or disabling the preview might change the icon size of the view.
761 // As the view does not emit a signal when the icon size has been changed,
762 // the used zoom level of the controller must be adjusted manually:
763 updateZoomLevel(oldZoomLevel
);
765 loadDirectory(viewPropsUrl
);
768 void DolphinView::setShowHiddenFiles(bool show
)
770 if (m_dirLister
->showingDotFiles() == show
) {
774 const KUrl viewPropsUrl
= viewPropertiesUrl();
775 ViewProperties
props(viewPropsUrl
);
776 props
.setShowHiddenFiles(show
);
778 m_dirLister
->setShowingDotFiles(show
);
779 emit
showHiddenFilesChanged();
781 loadDirectory(viewPropsUrl
);
784 void DolphinView::setCategorizedSorting(bool categorized
)
786 if (categorized
== categorizedSorting()) {
790 // setCategorizedSorting(true) may only get invoked
791 // if the view supports categorized sorting
792 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
794 ViewProperties
props(viewPropertiesUrl());
795 props
.setCategorizedSorting(categorized
);
798 m_storedCategorizedSorting
= categorized
;
799 m_proxyModel
->setCategorizedModel(categorized
);
801 emit
categorizedSortingChanged();
804 void DolphinView::toggleSortOrder()
806 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
807 Qt::DescendingOrder
:
812 void DolphinView::toggleAdditionalInfo(QAction
* action
)
814 const KFileItemDelegate::Information info
=
815 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
817 KFileItemDelegate::InformationList list
= additionalInfo();
819 const bool show
= action
->isChecked();
821 const int index
= list
.indexOf(info
);
822 const bool containsInfo
= (index
>= 0);
823 if (show
&& !containsInfo
) {
825 setAdditionalInfo(list
);
826 } else if (!show
&& containsInfo
) {
827 list
.removeAt(index
);
828 setAdditionalInfo(list
);
829 Q_ASSERT(list
.indexOf(info
) < 0);
833 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
835 QWidget::mouseReleaseEvent(event
);
839 void DolphinView::wheelEvent(QWheelEvent
* event
)
841 if (event
->modifiers() & Qt::ControlModifier
) {
842 const int delta
= event
->delta();
843 const int level
= zoomLevel();
845 setZoomLevel(level
+ 1);
846 } else if (delta
< 0) {
847 setZoomLevel(level
- 1);
853 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
855 switch (event
->type()) {
856 case QEvent::FocusIn
:
857 if (watched
== itemView()) {
858 m_controller
->requestActivation();
862 case QEvent::MouseButtonPress
:
863 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
864 // Listening to a mousebutton press event to delete expanded views is a
865 // workaround, as it seems impossible for the FolderExpander to know when
866 // a dragging outside a view has been finished. However it works quite well:
867 // A mousebutton press event indicates that a drag operation must be
869 m_expandedDragSource
->deleteLater();
870 m_expandedDragSource
= 0;
874 case QEvent::DragEnter
:
875 if (watched
== itemView()->viewport()) {
880 case QEvent::KeyPress
:
881 if (watched
== itemView()) {
882 if (m_toolTipManager
!= 0) {
883 m_toolTipManager
->hideTip();
886 // clear the selection when Escape has been pressed
887 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
888 if (keyEvent
->key() == Qt::Key_Escape
) {
898 return QWidget::eventFilter(watched
, event
);
901 void DolphinView::activate()
906 void DolphinView::triggerItem(const KFileItem
& item
)
908 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
909 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
910 // items are selected by the user, hence don't trigger the
911 // item specified by 'index'
915 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
916 if (item
.isNull() || m_isContextMenuOpen
) {
920 if (m_toolTipManager
!= 0) {
921 m_toolTipManager
->hideTip();
923 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
926 void DolphinView::emitSelectionChangedSignal()
928 emit
selectionChanged(DolphinView::selectedItems());
931 void DolphinView::openContextMenu(const QPoint
& pos
,
932 const QList
<QAction
*>& customActions
)
935 if (isColumnViewActive()) {
936 item
= m_columnView
->itemAt(pos
);
938 const QModelIndex index
= itemView()->indexAt(pos
);
939 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
940 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
941 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
945 if (m_toolTipManager
!= 0) {
946 m_toolTipManager
->hideTip();
949 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
950 emit
requestContextMenu(item
, url(), customActions
);
951 m_isContextMenuOpen
= false;
954 void DolphinView::dropUrls(const KFileItem
& destItem
,
955 const KUrl
& destPath
,
958 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
961 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
963 ViewProperties
props(viewPropertiesUrl());
964 props
.setSorting(sorting
);
966 m_proxyModel
->setSorting(sorting
);
968 emit
sortingChanged(sorting
);
971 void DolphinView::updateSortOrder(Qt::SortOrder order
)
973 ViewProperties
props(viewPropertiesUrl());
974 props
.setSortOrder(order
);
976 m_proxyModel
->setSortOrder(order
);
978 emit
sortOrderChanged(order
);
981 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
983 ViewProperties
props(viewPropertiesUrl());
984 props
.setAdditionalInfo(info
);
987 m_fileItemDelegate
->setShowInformation(info
);
989 emit
additionalInfoChanged();
992 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
994 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
995 (m_mode
== DolphinView::IconsView
);
997 QAction
* showSizeInfo
= collection
->action("show_size_info");
998 QAction
* showDateInfo
= collection
->action("show_date_info");
999 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
1000 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
1001 QAction
* showGroupInfo
= collection
->action("show_group_info");
1002 QAction
* showMimeInfo
= collection
->action("show_mime_info");
1004 showSizeInfo
->setChecked(false);
1005 showDateInfo
->setChecked(false);
1006 showPermissionsInfo
->setChecked(false);
1007 showOwnerInfo
->setChecked(false);
1008 showGroupInfo
->setChecked(false);
1009 showMimeInfo
->setChecked(false);
1011 showSizeInfo
->setEnabled(enable
);
1012 showDateInfo
->setEnabled(enable
);
1013 showPermissionsInfo
->setEnabled(enable
);
1014 showOwnerInfo
->setEnabled(enable
);
1015 showGroupInfo
->setEnabled(enable
);
1016 showMimeInfo
->setEnabled(enable
);
1018 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1020 case KFileItemDelegate::Size
:
1021 showSizeInfo
->setChecked(true);
1023 case KFileItemDelegate::ModificationTime
:
1024 showDateInfo
->setChecked(true);
1026 case KFileItemDelegate::Permissions
:
1027 showPermissionsInfo
->setChecked(true);
1029 case KFileItemDelegate::Owner
:
1030 showOwnerInfo
->setChecked(true);
1032 case KFileItemDelegate::OwnerAndGroup
:
1033 showGroupInfo
->setChecked(true);
1035 case KFileItemDelegate::FriendlyMimeType
:
1036 showMimeInfo
->setChecked(true);
1044 QPair
<bool, QString
> DolphinView::pasteInfo() const
1046 QPair
<bool, QString
> ret
;
1047 QClipboard
* clipboard
= QApplication::clipboard();
1048 const QMimeData
* mimeData
= clipboard
->mimeData();
1050 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1051 if (!urls
.isEmpty()) {
1052 // disable the paste action if no writing is supported
1053 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1054 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1056 if (urls
.count() == 1) {
1057 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1058 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1059 i18nc("@action:inmenu", "Paste One File");
1062 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1066 ret
.second
= i18nc("@action:inmenu", "Paste");
1072 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1074 m_tabsForFiles
= tabsForFiles
;
1077 bool DolphinView::isTabsForFilesEnabled() const
1079 return m_tabsForFiles
;
1082 bool DolphinView::itemsExpandable() const
1084 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1087 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1092 if (DragAndDropHelper::instance().isDragSource(view
)) {
1093 // We must store for later deletion.
1094 if (m_expandedDragSource
!= 0) {
1095 // The old stored view is obviously not the drag source anymore.
1096 m_expandedDragSource
->deleteLater();
1097 m_expandedDragSource
= 0;
1100 m_expandedDragSource
= view
;
1103 view
->deleteLater();
1107 void DolphinView::emitContentsMoved()
1109 // only emit the contents moved signal if:
1110 // - no directory loading is ongoing (this would reset the contents position
1111 // always to (0, 0))
1112 // - if the Column View is active: the column view does an automatic
1113 // positioning during the loading operation, which must be remembered
1114 if (!m_loadingDirectory
|| isColumnViewActive()) {
1115 const QPoint
pos(contentsPosition());
1116 emit
contentsMoved(pos
.x(), pos
.y());
1120 void DolphinView::showHoverInformation(const KFileItem
& item
)
1122 emit
requestItemInfo(item
);
1125 void DolphinView::clearHoverInformation()
1127 emit
requestItemInfo(KFileItem());
1130 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1132 if (job
->error() == 0) {
1133 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1134 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1135 emit
errorMessage(job
->errorString());
1139 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1141 emit
requestUrlChange(url
);
1142 m_controller
->setUrl(url
);
1145 void DolphinView::restoreCurrentItem()
1147 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1148 if (dirIndex
.isValid()) {
1149 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1150 QAbstractItemView
* view
= itemView();
1151 const bool clearSelection
= !hasSelection();
1152 view
->setCurrentIndex(proxyIndex
);
1153 if (clearSelection
) {
1154 view
->clearSelection();
1159 void DolphinView::slotRefreshItems()
1161 if (m_assureVisibleCurrentIndex
) {
1162 m_assureVisibleCurrentIndex
= false;
1163 // Invoking itemView()->scrollTo(itemView()->currentIndex()) is
1164 // not sufficient, as QListView and QTreeView have an inconsistent
1165 // default behavior.
1166 m_controller
->triggerScrollToCurrentItem();
1170 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1172 if (!url
.isValid()) {
1173 const QString
location(url
.pathOrUrl());
1174 if (location
.isEmpty()) {
1175 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1177 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1182 m_loadingDirectory
= true;
1184 m_dirLister
->stop();
1185 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1187 if (isColumnViewActive()) {
1188 // adjusting the directory lister is not enough in the case of the
1189 // column view, as each column has its own directory lister internally...
1191 m_columnView
->reload();
1193 m_columnView
->showColumn(url
);
1198 KUrl
DolphinView::viewPropertiesUrl() const
1200 if (isColumnViewActive()) {
1201 return m_columnView
->rootUrl();
1207 void DolphinView::applyViewProperties(const KUrl
& url
)
1209 if (m_ignoreViewProperties
) {
1213 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1214 // The column view is active, hence don't apply the view properties
1215 // of sub directories (represented by columns) to the view. The
1216 // view always represents the properties of the first column.
1220 const ViewProperties
props(url
);
1222 const Mode mode
= props
.viewMode();
1223 if (m_mode
!= mode
) {
1224 const int oldZoomLevel
= m_controller
->zoomLevel();
1230 updateZoomLevel(oldZoomLevel
);
1232 if (itemView() == 0) {
1235 Q_ASSERT(itemView() != 0);
1236 Q_ASSERT(m_fileItemDelegate
!= 0);
1238 const bool showHiddenFiles
= props
.showHiddenFiles();
1239 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1240 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1241 emit
showHiddenFilesChanged();
1244 m_storedCategorizedSorting
= props
.categorizedSorting();
1245 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1246 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1247 m_proxyModel
->setCategorizedModel(categorized
);
1248 emit
categorizedSortingChanged();
1251 const DolphinView::Sorting sorting
= props
.sorting();
1252 if (sorting
!= m_proxyModel
->sorting()) {
1253 m_proxyModel
->setSorting(sorting
);
1254 emit
sortingChanged(sorting
);
1257 const Qt::SortOrder sortOrder
= props
.sortOrder();
1258 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1259 m_proxyModel
->setSortOrder(sortOrder
);
1260 emit
sortOrderChanged(sortOrder
);
1263 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1264 if (info
!= m_fileItemDelegate
->showInformation()) {
1265 m_fileItemDelegate
->setShowInformation(info
);
1266 emit
additionalInfoChanged();
1269 const bool showPreview
= props
.showPreview();
1270 if (showPreview
!= m_showPreview
) {
1271 m_showPreview
= showPreview
;
1272 m_previewGenerator
->setPreviewShown(showPreview
);
1274 const int oldZoomLevel
= m_controller
->zoomLevel();
1275 emit
showPreviewChanged();
1277 // Enabling or disabling the preview might change the icon size of the view.
1278 // As the view does not emit a signal when the icon size has been changed,
1279 // the used zoom level of the controller must be adjusted manually:
1280 updateZoomLevel(oldZoomLevel
);
1283 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1284 // During the lifetime of a DolphinView instance the global view properties
1285 // should not be changed. This allows e. g. to split a view and use different
1286 // view properties for each view.
1287 m_ignoreViewProperties
= true;
1291 void DolphinView::createView()
1294 Q_ASSERT(m_iconsView
== 0);
1295 Q_ASSERT(m_detailsView
== 0);
1296 Q_ASSERT(m_columnView
== 0);
1298 QAbstractItemView
* view
= 0;
1301 m_iconsView
= new DolphinIconsView(this, m_controller
);
1307 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1308 view
= m_detailsView
;
1312 m_columnView
= new DolphinColumnView(this, m_controller
);
1313 view
= m_columnView
;
1317 Q_ASSERT(view
!= 0);
1318 view
->installEventFilter(this);
1319 view
->viewport()->installEventFilter(this);
1320 setFocusProxy(view
);
1322 if (m_mode
!= ColumnView
) {
1323 // Give the view the ability to auto-expand its directories on hovering
1324 // (the column view takes care about this itself). If the details view
1325 // uses expandable folders, the auto-expanding should be used always.
1326 DolphinSettings
& settings
= DolphinSettings::instance();
1327 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1328 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1330 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1331 folderExpander
->setEnabled(enabled
);
1332 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1333 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1336 // Listen out for requests to delete the current column.
1337 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1338 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1341 m_controller
->setItemView(view
);
1343 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1344 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1345 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1346 view
->setItemDelegate(m_fileItemDelegate
);
1348 view
->setModel(m_proxyModel
);
1349 if (m_selectionModel
!= 0) {
1350 view
->setSelectionModel(m_selectionModel
);
1352 m_selectionModel
= view
->selectionModel();
1355 // reparent the selection model, as it should not be deleted
1356 // when deleting the model
1357 m_selectionModel
->setParent(this);
1359 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1361 m_previewGenerator
= new KFilePreviewGenerator(view
);
1362 m_previewGenerator
->setPreviewShown(m_showPreview
);
1364 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1365 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1366 connect(m_controller
, SIGNAL(hideToolTip()),
1367 m_toolTipManager
, SLOT(hideTip()));
1370 m_topLayout
->insertWidget(1, view
);
1372 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1373 this, SLOT(emitSelectionChangedSignal()));
1374 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1375 this, SLOT(emitContentsMoved()));
1376 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1377 this, SLOT(emitContentsMoved()));
1380 void DolphinView::deleteView()
1382 QAbstractItemView
* view
= itemView();
1384 // It's important to set the keyboard focus to the parent
1385 // before deleting the view: Otherwise when having a split
1386 // view the other view will get the focus and will request
1387 // an activation (see DolphinView::eventFilter()).
1391 m_topLayout
->removeWidget(view
);
1394 // m_previewGenerator's parent is not always destroyed, and we
1395 // don't want two active at once - manually delete.
1396 delete m_previewGenerator
;
1397 m_previewGenerator
= 0;
1400 m_controller
->disconnect(view
);
1403 deleteWhenNotDragSource(view
);
1409 m_fileItemDelegate
= 0;
1410 m_toolTipManager
= 0;
1414 QAbstractItemView
* DolphinView::itemView() const
1416 if (m_detailsView
!= 0) {
1417 return m_detailsView
;
1418 } else if (m_columnView
!= 0) {
1419 return m_columnView
;
1425 bool DolphinView::isCutItem(const KFileItem
& item
) const
1427 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1428 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1430 const KUrl
& itemUrl
= item
.url();
1431 KUrl::List::const_iterator it
= cutUrls
.begin();
1432 const KUrl::List::const_iterator end
= cutUrls
.end();
1434 if (*it
== itemUrl
) {
1443 void DolphinView::pasteToUrl(const KUrl
& url
)
1445 QClipboard
* clipboard
= QApplication::clipboard();
1446 const QMimeData
* mimeData
= clipboard
->mimeData();
1448 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1449 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1450 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1453 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1457 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1459 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1460 if (oldZoomLevel
!= newZoomLevel
) {
1461 m_controller
->setZoomLevel(newZoomLevel
);
1462 emit
zoomLevelChanged(newZoomLevel
);
1466 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1468 KUrl::List list
= selectedUrls();
1469 if (itemsExpandable() ) {
1470 list
= KDirModel::simplifiedUrlList(list
);
1475 QMimeData
* DolphinView::selectionMimeData() const
1477 if (isColumnViewActive()) {
1478 return m_columnView
->selectionMimeData();
1481 const QAbstractItemView
* view
= itemView();
1482 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1483 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1484 return m_dolphinModel
->mimeData(selection
.indexes());
1487 #include "dolphinview.moc"