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>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
42 #include <kmessagebox.h>
43 #include <kmimetyperesolver.h>
44 #include <konq_fileitemcapabilities.h>
45 #include <konq_operations.h>
46 #include <konqmimedata.h>
47 #include <ktoggleaction.h>
50 #include "dolphinmodel.h"
51 #include "dolphincolumnview.h"
52 #include "dolphincontroller.h"
53 #include "dolphinfileitemdelegate.h"
54 #include "dolphinsortfilterproxymodel.h"
55 #include "dolphindetailsview.h"
56 #include "dolphin_detailsmodesettings.h"
57 #include "dolphiniconsview.h"
58 #include "dolphinsettings.h"
59 #include "dolphin_generalsettings.h"
60 #include "draganddrophelper.h"
61 #include "folderexpander.h"
62 #include "renamedialog.h"
63 #include "tooltipmanager.h"
64 #include "viewproperties.h"
65 #include "zoomlevelinfo.h"
67 DolphinView::DolphinView(QWidget
* parent
,
69 KDirLister
* dirLister
,
70 DolphinModel
* dolphinModel
,
71 DolphinSortFilterProxyModel
* proxyModel
) :
75 m_loadingDirectory(false),
76 m_storedCategorizedSorting(false),
77 m_tabsForFiles(false),
78 m_isContextMenuOpen(false),
79 m_mode(DolphinView::IconsView
),
85 m_fileItemDelegate(0),
87 m_dolphinModel(dolphinModel
),
88 m_dirLister(dirLister
),
89 m_proxyModel(proxyModel
),
90 m_previewGenerator(0),
96 m_topLayout
= new QVBoxLayout(this);
97 m_topLayout
->setSpacing(0);
98 m_topLayout
->setMargin(0);
100 m_controller
= new DolphinController(this);
101 m_controller
->setUrl(url
);
103 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
104 this, SIGNAL(urlChanged(const KUrl
&)));
105 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
106 this, SLOT(slotRequestUrlChange(const KUrl
&)));
108 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
109 this, SLOT(openContextMenu(const QPoint
&)));
110 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
111 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
112 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
113 this, SLOT(updateSorting(DolphinView::Sorting
)));
114 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
115 this, SLOT(updateSortOrder(Qt::SortOrder
)));
116 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
117 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
118 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
119 this, SLOT(triggerItem(const KFileItem
&)));
120 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
121 this, SIGNAL(tabRequested(const KUrl
&)));
122 connect(m_controller
, SIGNAL(activated()),
123 this, SLOT(activate()));
124 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
125 this, SLOT(showHoverInformation(const KFileItem
&)));
126 connect(m_controller
, SIGNAL(viewportEntered()),
127 this, SLOT(clearHoverInformation()));
129 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
130 this, SIGNAL(redirection(KUrl
, KUrl
)));
131 connect(m_dirLister
, SIGNAL(completed()),
132 this, SLOT(restoreCurrentItem()));
134 applyViewProperties(url
);
135 m_topLayout
->addWidget(itemView());
138 DolphinView::~DolphinView()
140 deleteExpandedViews();
143 const KUrl
& DolphinView::url() const
145 return m_controller
->url();
148 KUrl
DolphinView::rootUrl() const
150 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
153 void DolphinView::setActive(bool active
)
155 if (active
== m_active
) {
161 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
163 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
164 // bypasses the problem when having a split view and changing the active view to
165 // update the some URL dependent states. A nicer approach should be no big deal...
166 emit
urlChanged(url());
167 emit
selectionChanged(selectedItems());
172 QWidget
* viewport
= itemView()->viewport();
174 palette
.setColor(viewport
->backgroundRole(), color
);
175 viewport
->setPalette(palette
);
180 itemView()->setFocus();
184 m_controller
->indicateActivationChange(active
);
187 bool DolphinView::isActive() const
192 void DolphinView::setMode(Mode mode
)
194 if (mode
== m_mode
) {
195 return; // the wished mode is already set
198 const int oldZoomLevel
= m_controller
->zoomLevel();
203 const KUrl viewPropsUrl
= viewPropertiesUrl();
204 ViewProperties
props(viewPropsUrl
);
205 props
.setViewMode(m_mode
);
208 // the file item delegate has been recreated, apply the current
209 // additional information manually
210 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
211 m_fileItemDelegate
->setShowInformation(infoList
);
212 emit
additionalInfoChanged();
214 // Not all view modes support categorized sorting. Adjust the sorting model
215 // if changing the view mode results in a change of the categorized sorting
217 m_storedCategorizedSorting
= props
.categorizedSorting();
218 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
219 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
220 m_proxyModel
->setCategorizedModel(categorized
);
221 emit
categorizedSortingChanged();
225 updateZoomLevel(oldZoomLevel
);
228 DolphinView::Mode
DolphinView::mode() const
233 bool DolphinView::showPreview() const
235 return m_showPreview
;
238 bool DolphinView::showHiddenFiles() const
240 return m_dirLister
->showingDotFiles();
243 bool DolphinView::categorizedSorting() const
245 // If all view modes would support categorized sorting, returning
246 // m_proxyModel->isCategorizedModel() would be the way to go. As
247 // currently only the icons view supports caterized sorting, we remember
248 // the stored view properties state in m_storedCategorizedSorting and
249 // return this state. The application takes care to disable the corresponding
250 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
251 // that this setting is not applied to the current view mode.
252 return m_storedCategorizedSorting
;
255 bool DolphinView::supportsCategorizedSorting() const
257 return m_iconsView
!= 0;
260 void DolphinView::selectAll()
262 QAbstractItemView
* view
= itemView();
263 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
264 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
265 // selection instead of selecting all items. This is bypassed for KDE 4.0
266 // by invoking clearSelection() first.
267 view
->clearSelection();
271 void DolphinView::invertSelection()
273 if (isColumnViewActive()) {
274 // QAbstractItemView does not offer a virtual method invertSelection()
275 // as counterpart to QAbstractItemView::selectAll(). This makes it
276 // necessary to delegate the inverting of the selection to the
277 // column view, as only the selection of the active column should
279 m_columnView
->invertSelection();
281 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
282 const QAbstractItemModel
* itemModel
= selectionModel
->model();
284 const QModelIndex topLeft
= itemModel
->index(0, 0);
285 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
286 itemModel
->columnCount() - 1);
288 const QItemSelection
selection(topLeft
, bottomRight
);
289 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
293 bool DolphinView::hasSelection() const
295 return itemView()->selectionModel()->hasSelection();
298 void DolphinView::clearSelection()
300 itemView()->selectionModel()->clear();
303 KFileItemList
DolphinView::selectedItems() const
305 if (isColumnViewActive()) {
306 return m_columnView
->selectedItems();
309 const QAbstractItemView
* view
= itemView();
311 // Our view has a selection, we will map them back to the DolphinModel
312 // and then fill the KFileItemList.
313 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
315 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
316 KFileItemList itemList
;
318 const QModelIndexList indexList
= selection
.indexes();
319 foreach (const QModelIndex
&index
, indexList
) {
320 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
321 if (!item
.isNull()) {
322 itemList
.append(item
);
329 KUrl::List
DolphinView::selectedUrls() const
332 const KFileItemList list
= selectedItems();
333 foreach (const KFileItem
&item
, list
) {
334 urls
.append(item
.url());
339 int DolphinView::selectedItemsCount() const
341 if (isColumnViewActive()) {
342 // TODO: get rid of this special case by adjusting the dir lister
343 // to the current column
344 return m_columnView
->selectedItems().count();
347 return itemView()->selectionModel()->selection().count();
350 void DolphinView::setContentsPosition(int x
, int y
)
352 QAbstractItemView
* view
= itemView();
354 // the ColumnView takes care itself for the horizontal scrolling
355 if (!isColumnViewActive()) {
356 view
->horizontalScrollBar()->setValue(x
);
358 view
->verticalScrollBar()->setValue(y
);
360 m_loadingDirectory
= false;
363 QPoint
DolphinView::contentsPosition() const
365 const int x
= itemView()->horizontalScrollBar()->value();
366 const int y
= itemView()->verticalScrollBar()->value();
370 void DolphinView::setZoomLevel(int level
)
372 if (level
< ZoomLevelInfo::minimumLevel()) {
373 level
= ZoomLevelInfo::minimumLevel();
374 } else if (level
> ZoomLevelInfo::maximumLevel()) {
375 level
= ZoomLevelInfo::maximumLevel();
378 if (level
!= zoomLevel()) {
379 m_controller
->setZoomLevel(level
);
380 m_previewGenerator
->updatePreviews();
381 emit
zoomLevelChanged(level
);
385 int DolphinView::zoomLevel() const
387 return m_controller
->zoomLevel();
390 void DolphinView::setSorting(Sorting sorting
)
392 if (sorting
!= this->sorting()) {
393 updateSorting(sorting
);
397 DolphinView::Sorting
DolphinView::sorting() const
399 return m_proxyModel
->sorting();
402 void DolphinView::setSortOrder(Qt::SortOrder order
)
404 if (sortOrder() != order
) {
405 updateSortOrder(order
);
409 Qt::SortOrder
DolphinView::sortOrder() const
411 return m_proxyModel
->sortOrder();
414 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
416 const KUrl viewPropsUrl
= viewPropertiesUrl();
417 ViewProperties
props(viewPropsUrl
);
418 props
.setAdditionalInfo(info
);
419 m_fileItemDelegate
->setShowInformation(info
);
421 emit
additionalInfoChanged();
423 if (itemView() != m_detailsView
) {
424 // the details view requires no reloading of the directory, as it maps
425 // the file item delegate info to its columns internally
426 loadDirectory(viewPropsUrl
);
430 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
432 return m_fileItemDelegate
->showInformation();
435 void DolphinView::reload()
438 loadDirectory(url(), true);
441 void DolphinView::refresh()
443 const bool oldActivationState
= m_active
;
444 const int oldZoomLevel
= m_controller
->zoomLevel();
448 applyViewProperties(m_controller
->url());
451 setActive(oldActivationState
);
452 updateZoomLevel(oldZoomLevel
);
455 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
457 if (m_controller
->url() == url
) {
461 m_previewGenerator
->cancelPreviews();
462 m_controller
->setUrl(url
); // emits urlChanged, which we forward
464 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
465 applyViewProperties(rootUrl
);
466 loadDirectory(rootUrl
);
467 if (itemView() == m_columnView
) {
468 m_columnView
->setRootUrl(rootUrl
);
469 m_columnView
->showColumn(url
);
472 applyViewProperties(url
);
476 emit
startedPathLoading(url
);
479 void DolphinView::setNameFilter(const QString
& nameFilter
)
481 m_proxyModel
->setFilterRegExp(nameFilter
);
483 if (isColumnViewActive()) {
484 // adjusting the directory lister is not enough in the case of the
485 // column view, as each column has its own directory lister internally...
486 m_columnView
->setNameFilter(nameFilter
);
490 void DolphinView::calculateItemCount(int& fileCount
,
492 KIO::filesize_t
& totalFileSize
) const
494 foreach (const KFileItem
& item
, m_dirLister
->items()) {
499 totalFileSize
+= item
.size();
504 QString
DolphinView::statusBarText() const
509 KIO::filesize_t totalFileSize
= 0;
511 if (hasSelection()) {
512 // give a summary of the status of the selected files
513 const KFileItemList list
= selectedItems();
514 if (list
.isEmpty()) {
515 // when an item is triggered, it is temporary selected but selectedItems()
516 // will return an empty list
520 KFileItemList::const_iterator it
= list
.begin();
521 const KFileItemList::const_iterator end
= list
.end();
523 const KFileItem
& item
= *it
;
528 totalFileSize
+= item
.size();
533 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
536 if (folderCount
> 0) {
537 text
= hasSelection() ?
538 i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
) :
539 i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
541 text
+= i18nc("@info:status separator between 2 status infos", ", ");
546 const QString sizeText
= KIO::convertSize(totalFileSize
);
547 text
+= hasSelection() ?
548 i18ncp("@info:status", "1 File selected (%2)", "%1 Files selected (%2)", fileCount
, sizeText
) :
549 i18ncp("@info:status", "1 File (%2)", "%1 Files (%2)", fileCount
, sizeText
);
555 void DolphinView::setUrl(const KUrl
& url
)
557 // remember current item candidate (see restoreCurrentItem())
558 m_currentItemUrl
= url
;
559 updateView(url
, KUrl());
562 void DolphinView::changeSelection(const KFileItemList
& selection
)
565 if (selection
.isEmpty()) {
568 const KUrl
& baseUrl
= url();
570 QItemSelection new_selection
;
571 foreach(const KFileItem
& item
, selection
) {
572 url
= item
.url().upUrl();
573 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
574 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
575 new_selection
.select(index
, index
);
578 itemView()->selectionModel()->select(new_selection
,
579 QItemSelectionModel::ClearAndSelect
580 | QItemSelectionModel::Current
);
583 void DolphinView::renameSelectedItems()
585 const KFileItemList items
= selectedItems();
586 const int itemCount
= items
.count();
592 // More than one item has been selected for renaming. Open
593 // a rename dialog and rename all items afterwards.
594 RenameDialog
dialog(this, items
);
595 if (dialog
.exec() == QDialog::Rejected
) {
599 const QString newName
= dialog
.newName();
600 if (newName
.isEmpty()) {
601 emit
errorMessage(dialog
.errorString());
603 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
604 // as one operation instead of n rename operations like it is done now...
605 Q_ASSERT(newName
.contains('#'));
607 // iterate through all selected items and rename them...
609 foreach (const KFileItem
&item
, items
) {
610 const KUrl
& oldUrl
= item
.url();
612 number
.setNum(index
++);
614 QString name
= newName
;
615 name
.replace('#', number
);
617 if (oldUrl
.fileName() != name
) {
618 KUrl newUrl
= oldUrl
;
619 newUrl
.setFileName(name
);
620 KonqOperations::rename(this, oldUrl
, newUrl
);
624 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
625 Q_ASSERT(itemCount
== 1);
627 if (isColumnViewActive()) {
628 m_columnView
->editItem(items
.first());
630 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
631 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
632 itemView()->edit(proxyIndex
);
635 Q_ASSERT(itemCount
== 1);
637 RenameDialog
dialog(this, items
);
638 if (dialog
.exec() == QDialog::Rejected
) {
642 const QString
& newName
= dialog
.newName();
643 if (newName
.isEmpty()) {
644 emit
errorMessage(dialog
.errorString());
646 const KUrl
& oldUrl
= items
.first().url();
647 KUrl newUrl
= oldUrl
;
648 newUrl
.setFileName(newName
);
649 KonqOperations::rename(this, oldUrl
, newUrl
);
654 void DolphinView::trashSelectedItems()
656 const KUrl::List list
= simplifiedSelectedUrls();
657 KonqOperations::del(this, KonqOperations::TRASH
, list
);
660 void DolphinView::deleteSelectedItems()
662 const KUrl::List list
= simplifiedSelectedUrls();
663 const bool del
= KonqOperations::askDeleteConfirmation(list
,
665 KonqOperations::DEFAULT_CONFIRMATION
,
669 KIO::Job
* job
= KIO::del(list
);
670 connect(job
, SIGNAL(result(KJob
*)),
671 this, SLOT(slotDeleteFileFinished(KJob
*)));
675 void DolphinView::cutSelectedItems()
677 QMimeData
* mimeData
= new QMimeData();
678 const KUrl::List kdeUrls
= simplifiedSelectedUrls();
679 const KUrl::List mostLocalUrls
;
680 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
681 QApplication::clipboard()->setMimeData(mimeData
);
684 void DolphinView::copySelectedItems()
686 QMimeData
* mimeData
= new QMimeData();
687 const KUrl::List kdeUrls
= selectedUrls();
688 const KUrl::List mostLocalUrls
;
689 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
690 QApplication::clipboard()->setMimeData(mimeData
);
693 void DolphinView::paste()
698 void DolphinView::pasteIntoFolder()
700 const KFileItemList items
= selectedItems();
701 if ((items
.count() == 1) && items
.first().isDir()) {
702 pasteToUrl(items
.first().url());
706 void DolphinView::setShowPreview(bool show
)
708 if (m_showPreview
== show
) {
712 const KUrl viewPropsUrl
= viewPropertiesUrl();
713 ViewProperties
props(viewPropsUrl
);
714 props
.setShowPreview(show
);
716 m_showPreview
= show
;
717 m_previewGenerator
->setPreviewShown(show
);
719 const int oldZoomLevel
= m_controller
->zoomLevel();
720 emit
showPreviewChanged();
722 // Enabling or disabling the preview might change the icon size of the view.
723 // As the view does not emit a signal when the icon size has been changed,
724 // the used zoom level of the controller must be adjusted manually:
725 updateZoomLevel(oldZoomLevel
);
727 loadDirectory(viewPropsUrl
);
730 void DolphinView::setShowHiddenFiles(bool show
)
732 if (m_dirLister
->showingDotFiles() == show
) {
736 const KUrl viewPropsUrl
= viewPropertiesUrl();
737 ViewProperties
props(viewPropsUrl
);
738 props
.setShowHiddenFiles(show
);
740 m_dirLister
->setShowingDotFiles(show
);
741 emit
showHiddenFilesChanged();
743 loadDirectory(viewPropsUrl
);
746 void DolphinView::setCategorizedSorting(bool categorized
)
748 if (categorized
== categorizedSorting()) {
752 // setCategorizedSorting(true) may only get invoked
753 // if the view supports categorized sorting
754 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
756 ViewProperties
props(viewPropertiesUrl());
757 props
.setCategorizedSorting(categorized
);
760 m_storedCategorizedSorting
= categorized
;
761 m_proxyModel
->setCategorizedModel(categorized
);
763 emit
categorizedSortingChanged();
766 void DolphinView::toggleSortOrder()
768 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
769 Qt::DescendingOrder
:
774 void DolphinView::toggleAdditionalInfo(QAction
* action
)
776 const KFileItemDelegate::Information info
=
777 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
779 KFileItemDelegate::InformationList list
= additionalInfo();
781 const bool show
= action
->isChecked();
783 const int index
= list
.indexOf(info
);
784 const bool containsInfo
= (index
>= 0);
785 if (show
&& !containsInfo
) {
787 setAdditionalInfo(list
);
788 } else if (!show
&& containsInfo
) {
789 list
.removeAt(index
);
790 setAdditionalInfo(list
);
791 Q_ASSERT(list
.indexOf(info
) < 0);
795 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
797 QWidget::mouseReleaseEvent(event
);
801 void DolphinView::wheelEvent(QWheelEvent
* event
)
803 if (event
->modifiers() & Qt::ControlModifier
) {
804 const int delta
= event
->delta();
805 const int level
= zoomLevel();
807 setZoomLevel(level
+ 1);
808 } else if (delta
< 0) {
809 setZoomLevel(level
- 1);
815 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
817 switch (event
->type()) {
818 case QEvent::FocusIn
:
819 if (watched
== itemView()) {
820 m_controller
->requestActivation();
824 case QEvent::MouseButtonPress
:
825 if ((watched
== itemView()->viewport()) && (m_expandedViews
.count() > 0)) {
826 // Listening to a mousebutton press event to delete expanded views is a
827 // workaround, as it seems impossible for the FolderExpander to know when
828 // a dragging outside a view has been finished. However it works quite well:
829 // A mousebutton press event indicates that a drag operation must be
831 deleteExpandedViews();
835 case QEvent::DragEnter
:
836 if (watched
== itemView()->viewport()) {
845 return QWidget::eventFilter(watched
, event
);
848 void DolphinView::activate()
853 void DolphinView::triggerItem(const KFileItem
& item
)
855 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
856 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
857 // items are selected by the user, hence don't trigger the
858 // item specified by 'index'
862 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
863 if (item
.isNull() || m_isContextMenuOpen
) {
867 if (m_toolTipManager
!= 0) {
868 m_toolTipManager
->hideTip();
870 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
873 void DolphinView::emitSelectionChangedSignal()
875 emit
selectionChanged(DolphinView::selectedItems());
878 void DolphinView::openContextMenu(const QPoint
& pos
)
881 if (isColumnViewActive()) {
882 item
= m_columnView
->itemAt(pos
);
884 const QModelIndex index
= itemView()->indexAt(pos
);
885 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
886 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
887 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
891 if (m_toolTipManager
!= 0) {
892 m_toolTipManager
->hideTip();
895 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
896 emit
requestContextMenu(item
, url());
897 m_isContextMenuOpen
= false;
900 void DolphinView::dropUrls(const KFileItem
& destItem
,
901 const KUrl
& destPath
,
904 DragAndDropHelper::dropUrls(destItem
, destPath
, event
, this);
907 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
909 ViewProperties
props(viewPropertiesUrl());
910 props
.setSorting(sorting
);
912 m_proxyModel
->setSorting(sorting
);
914 emit
sortingChanged(sorting
);
917 void DolphinView::updateSortOrder(Qt::SortOrder order
)
919 ViewProperties
props(viewPropertiesUrl());
920 props
.setSortOrder(order
);
922 m_proxyModel
->setSortOrder(order
);
924 emit
sortOrderChanged(order
);
927 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
929 ViewProperties
props(viewPropertiesUrl());
930 props
.setAdditionalInfo(info
);
933 m_fileItemDelegate
->setShowInformation(info
);
935 emit
additionalInfoChanged();
938 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
940 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
941 (m_mode
== DolphinView::IconsView
);
943 QAction
* showSizeInfo
= collection
->action("show_size_info");
944 QAction
* showDateInfo
= collection
->action("show_date_info");
945 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
946 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
947 QAction
* showGroupInfo
= collection
->action("show_group_info");
948 QAction
* showMimeInfo
= collection
->action("show_mime_info");
950 showSizeInfo
->setChecked(false);
951 showDateInfo
->setChecked(false);
952 showPermissionsInfo
->setChecked(false);
953 showOwnerInfo
->setChecked(false);
954 showGroupInfo
->setChecked(false);
955 showMimeInfo
->setChecked(false);
957 showSizeInfo
->setEnabled(enable
);
958 showDateInfo
->setEnabled(enable
);
959 showPermissionsInfo
->setEnabled(enable
);
960 showOwnerInfo
->setEnabled(enable
);
961 showGroupInfo
->setEnabled(enable
);
962 showMimeInfo
->setEnabled(enable
);
964 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
966 case KFileItemDelegate::Size
:
967 showSizeInfo
->setChecked(true);
969 case KFileItemDelegate::ModificationTime
:
970 showDateInfo
->setChecked(true);
972 case KFileItemDelegate::Permissions
:
973 showPermissionsInfo
->setChecked(true);
975 case KFileItemDelegate::Owner
:
976 showOwnerInfo
->setChecked(true);
978 case KFileItemDelegate::OwnerAndGroup
:
979 showGroupInfo
->setChecked(true);
981 case KFileItemDelegate::FriendlyMimeType
:
982 showMimeInfo
->setChecked(true);
990 QPair
<bool, QString
> DolphinView::pasteInfo() const
992 QPair
<bool, QString
> ret
;
993 QClipboard
* clipboard
= QApplication::clipboard();
994 const QMimeData
* mimeData
= clipboard
->mimeData();
996 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
997 if (!urls
.isEmpty()) {
998 // disable the paste action if no writing is supported
999 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1000 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1002 if (urls
.count() == 1) {
1003 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1004 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1005 i18nc("@action:inmenu", "Paste One File");
1008 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1012 ret
.second
= i18nc("@action:inmenu", "Paste");
1018 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1020 m_tabsForFiles
= tabsForFiles
;
1023 bool DolphinView::isTabsForFilesEnabled() const
1025 return m_tabsForFiles
;
1028 void DolphinView::emitContentsMoved()
1030 // only emit the contents moved signal if:
1031 // - no directory loading is ongoing (this would reset the contents position
1032 // always to (0, 0))
1033 // - if the Column View is active: the column view does an automatic
1034 // positioning during the loading operation, which must be remembered
1035 if (!m_loadingDirectory
|| isColumnViewActive()) {
1036 const QPoint
pos(contentsPosition());
1037 emit
contentsMoved(pos
.x(), pos
.y());
1041 void DolphinView::showHoverInformation(const KFileItem
& item
)
1043 emit
requestItemInfo(item
);
1046 void DolphinView::clearHoverInformation()
1048 emit
requestItemInfo(KFileItem());
1051 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1053 if (job
->error() == 0) {
1054 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1055 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1056 emit
errorMessage(job
->errorString());
1060 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1062 emit
requestUrlChange(url
);
1063 m_controller
->setUrl(url
);
1066 void DolphinView::restoreCurrentItem()
1068 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1069 if (dirIndex
.isValid()) {
1070 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1071 QAbstractItemView
* view
= itemView();
1072 const bool clearSelection
= !hasSelection();
1073 view
->setCurrentIndex(proxyIndex
);
1074 if (clearSelection
) {
1075 view
->clearSelection();
1080 void DolphinView::enterDir(const QModelIndex
& index
, QAbstractItemView
* view
)
1082 // Deleting a view that is the root of a drag operation is not allowed, otherwise
1083 // the dragging gets automatically cancelled by Qt. So before entering a new
1084 // directory, the current view is remembered in m_expandedViews and deleted
1085 // later when the drag operation has been finished (see DolphinView::eventFilter()).
1086 m_expandedViews
.append(view
);
1087 m_controller
->triggerItem(index
);
1090 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1092 if (!url
.isValid()) {
1093 const QString
location(url
.pathOrUrl());
1094 if (location
.isEmpty()) {
1095 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1097 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1102 m_loadingDirectory
= true;
1104 m_dirLister
->stop();
1105 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1107 if (isColumnViewActive()) {
1108 // adjusting the directory lister is not enough in the case of the
1109 // column view, as each column has its own directory lister internally...
1111 m_columnView
->reload();
1113 m_columnView
->showColumn(url
);
1118 KUrl
DolphinView::viewPropertiesUrl() const
1120 if (isColumnViewActive()) {
1121 return m_columnView
->rootUrl();
1127 void DolphinView::applyViewProperties(const KUrl
& url
)
1129 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1130 // The column view is active, hence don't apply the view properties
1131 // of sub directories (represented by columns) to the view. The
1132 // view always represents the properties of the first column.
1136 const ViewProperties
props(url
);
1138 const Mode mode
= props
.viewMode();
1139 if (m_mode
!= mode
) {
1140 const int oldZoomLevel
= m_controller
->zoomLevel();
1146 updateZoomLevel(oldZoomLevel
);
1148 if (itemView() == 0) {
1151 Q_ASSERT(itemView() != 0);
1152 Q_ASSERT(m_fileItemDelegate
!= 0);
1154 const bool showHiddenFiles
= props
.showHiddenFiles();
1155 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1156 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1157 emit
showHiddenFilesChanged();
1160 m_storedCategorizedSorting
= props
.categorizedSorting();
1161 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1162 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1163 m_proxyModel
->setCategorizedModel(categorized
);
1164 emit
categorizedSortingChanged();
1167 const DolphinView::Sorting sorting
= props
.sorting();
1168 if (sorting
!= m_proxyModel
->sorting()) {
1169 m_proxyModel
->setSorting(sorting
);
1170 emit
sortingChanged(sorting
);
1173 const Qt::SortOrder sortOrder
= props
.sortOrder();
1174 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1175 m_proxyModel
->setSortOrder(sortOrder
);
1176 emit
sortOrderChanged(sortOrder
);
1179 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1180 if (info
!= m_fileItemDelegate
->showInformation()) {
1181 m_fileItemDelegate
->setShowInformation(info
);
1182 emit
additionalInfoChanged();
1185 const bool showPreview
= props
.showPreview();
1186 if (showPreview
!= m_showPreview
) {
1187 m_showPreview
= showPreview
;
1188 m_previewGenerator
->setPreviewShown(showPreview
);
1190 const int oldZoomLevel
= m_controller
->zoomLevel();
1191 emit
showPreviewChanged();
1193 // Enabling or disabling the preview might change the icon size of the view.
1194 // As the view does not emit a signal when the icon size has been changed,
1195 // the used zoom level of the controller must be adjusted manually:
1196 updateZoomLevel(oldZoomLevel
);
1200 void DolphinView::createView()
1203 Q_ASSERT(m_iconsView
== 0);
1204 Q_ASSERT(m_detailsView
== 0);
1205 Q_ASSERT(m_columnView
== 0);
1207 QAbstractItemView
* view
= 0;
1210 m_iconsView
= new DolphinIconsView(this, m_controller
);
1216 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1217 view
= m_detailsView
;
1221 m_columnView
= new DolphinColumnView(this, m_controller
);
1222 view
= m_columnView
;
1226 Q_ASSERT(view
!= 0);
1227 view
->installEventFilter(this);
1228 view
->viewport()->installEventFilter(this);
1230 if (m_mode
!= ColumnView
) {
1231 // Give the view the ability to auto-expand its directories on hovering
1232 // (the column view takes care about this itself). If the details view
1233 // uses expandable folders, the auto-expanding should be used always.
1234 DolphinSettings
& settings
= DolphinSettings::instance();
1235 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1236 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1238 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1239 folderExpander
->setEnabled(enabled
);
1240 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&, QAbstractItemView
*)),
1241 this, SLOT(enterDir(const QModelIndex
&, QAbstractItemView
*)));
1244 m_controller
->setItemView(view
);
1246 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1247 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1248 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1249 view
->setItemDelegate(m_fileItemDelegate
);
1251 view
->setModel(m_proxyModel
);
1252 if (m_selectionModel
!= 0) {
1253 view
->setSelectionModel(m_selectionModel
);
1255 m_selectionModel
= view
->selectionModel();
1258 // reparent the selection model, as it should not be deleted
1259 // when deleting the model
1260 m_selectionModel
->setParent(this);
1262 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1264 m_previewGenerator
= new KFilePreviewGenerator(view
);
1265 m_previewGenerator
->setPreviewShown(m_showPreview
);
1267 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1268 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1269 connect(m_controller
, SIGNAL(hideToolTip()),
1270 m_toolTipManager
, SLOT(hideTip()));
1273 m_topLayout
->insertWidget(1, view
);
1275 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1276 this, SLOT(emitSelectionChangedSignal()));
1277 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1278 this, SLOT(emitContentsMoved()));
1279 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1280 this, SLOT(emitContentsMoved()));
1283 void DolphinView::deleteView()
1285 QAbstractItemView
* view
= itemView();
1287 // It's important to set the keyboard focus to the parent
1288 // before deleting the view: Otherwise when having a split
1289 // view the other view will get the focus and will request
1290 // an activation (see DolphinView::eventFilter()).
1293 m_topLayout
->removeWidget(view
);
1296 bool deleteView
= true;
1297 foreach (const QAbstractItemView
* expandedView
, m_expandedViews
) {
1298 if (view
== expandedView
) {
1299 // the current view got already expanded and must stay alive
1300 // until the dragging has been completed
1306 view
->deleteLater();
1313 m_fileItemDelegate
= 0;
1314 m_previewGenerator
= 0;
1315 m_toolTipManager
= 0;
1319 QAbstractItemView
* DolphinView::itemView() const
1321 if (m_detailsView
!= 0) {
1322 return m_detailsView
;
1323 } else if (m_columnView
!= 0) {
1324 return m_columnView
;
1330 bool DolphinView::isCutItem(const KFileItem
& item
) const
1332 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1333 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1335 const KUrl
& itemUrl
= item
.url();
1336 KUrl::List::const_iterator it
= cutUrls
.begin();
1337 const KUrl::List::const_iterator end
= cutUrls
.end();
1339 if (*it
== itemUrl
) {
1348 void DolphinView::pasteToUrl(const KUrl
& url
)
1350 QClipboard
* clipboard
= QApplication::clipboard();
1351 const QMimeData
* mimeData
= clipboard
->mimeData();
1353 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1354 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1355 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1358 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1362 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1364 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1365 if (oldZoomLevel
!= newZoomLevel
) {
1366 m_controller
->setZoomLevel(newZoomLevel
);
1367 emit
zoomLevelChanged(newZoomLevel
);
1371 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1373 KUrl::List list
= selectedUrls();
1374 if (itemsExpandable() ) {
1375 list
= KonqOperations::simplifiedUrlList(list
);
1380 void DolphinView::deleteExpandedViews()
1382 const QAbstractItemView
* view
= itemView();
1383 foreach (QAbstractItemView
* expandedView
, m_expandedViews
) {
1384 if (expandedView
!= view
) {
1385 expandedView
->deleteLater();
1388 m_expandedViews
.clear();
1391 bool DolphinView::itemsExpandable() const
1393 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1396 #include "dolphinview.moc"