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_mode(DolphinView::IconsView
),
97 m_fileItemDelegate(0),
99 m_dolphinModel(dolphinModel
),
100 m_dirLister(dirLister
),
101 m_proxyModel(proxyModel
),
102 m_previewGenerator(0),
106 m_expandedDragSource(0)
108 m_topLayout
= new QVBoxLayout(this);
109 m_topLayout
->setSpacing(0);
110 m_topLayout
->setMargin(0);
112 m_controller
= new DolphinController(this);
113 m_controller
->setUrl(url
);
115 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
116 this, SIGNAL(urlChanged(const KUrl
&)));
117 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
118 this, SLOT(slotRequestUrlChange(const KUrl
&)));
120 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&, const QList
<QAction
*>&)),
121 this, SLOT(openContextMenu(const QPoint
&, const QList
<QAction
*>&)));
122 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
123 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
124 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
125 this, SLOT(updateSorting(DolphinView::Sorting
)));
126 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
127 this, SLOT(updateSortOrder(Qt::SortOrder
)));
128 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
129 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
130 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
131 this, SLOT(triggerItem(const KFileItem
&)));
132 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
133 this, SIGNAL(tabRequested(const KUrl
&)));
134 connect(m_controller
, SIGNAL(activated()),
135 this, SLOT(activate()));
136 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
137 this, SLOT(showHoverInformation(const KFileItem
&)));
138 connect(m_controller
, SIGNAL(viewportEntered()),
139 this, SLOT(clearHoverInformation()));
141 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
142 this, SIGNAL(redirection(KUrl
, KUrl
)));
143 connect(m_dirLister
, SIGNAL(completed()),
144 this, SLOT(restoreCurrentItem()));
146 applyViewProperties(url
);
147 m_topLayout
->addWidget(itemView());
150 DolphinView::~DolphinView()
152 delete m_expandedDragSource
;
153 m_expandedDragSource
= 0;
156 const KUrl
& DolphinView::url() const
158 return m_controller
->url();
161 KUrl
DolphinView::rootUrl() const
163 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
166 void DolphinView::setActive(bool active
)
168 if (active
== m_active
) {
174 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
176 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
177 // bypasses the problem when having a split view and changing the active view to
178 // update the some URL dependent states. A nicer approach should be no big deal...
179 emit
urlChanged(url());
180 emit
selectionChanged(selectedItems());
185 QWidget
* viewport
= itemView()->viewport();
187 palette
.setColor(viewport
->backgroundRole(), color
);
188 viewport
->setPalette(palette
);
193 itemView()->setFocus();
197 m_controller
->indicateActivationChange(active
);
200 bool DolphinView::isActive() const
205 void DolphinView::setMode(Mode mode
)
207 if (mode
== m_mode
) {
208 return; // the wished mode is already set
211 const int oldZoomLevel
= m_controller
->zoomLevel();
216 const KUrl viewPropsUrl
= viewPropertiesUrl();
217 ViewProperties
props(viewPropsUrl
);
218 props
.setViewMode(m_mode
);
221 // the file item delegate has been recreated, apply the current
222 // additional information manually
223 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
224 m_fileItemDelegate
->setShowInformation(infoList
);
225 emit
additionalInfoChanged();
227 // Not all view modes support categorized sorting. Adjust the sorting model
228 // if changing the view mode results in a change of the categorized sorting
230 m_storedCategorizedSorting
= props
.categorizedSorting();
231 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
232 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
233 m_proxyModel
->setCategorizedModel(categorized
);
234 emit
categorizedSortingChanged();
239 updateZoomLevel(oldZoomLevel
);
241 loadDirectory(viewPropsUrl
);
245 DolphinView::Mode
DolphinView::mode() const
250 bool DolphinView::showPreview() const
252 return m_showPreview
;
255 bool DolphinView::showHiddenFiles() const
257 return m_dirLister
->showingDotFiles();
260 bool DolphinView::categorizedSorting() const
262 // If all view modes would support categorized sorting, returning
263 // m_proxyModel->isCategorizedModel() would be the way to go. As
264 // currently only the icons view supports caterized sorting, we remember
265 // the stored view properties state in m_storedCategorizedSorting and
266 // return this state. The application takes care to disable the corresponding
267 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
268 // that this setting is not applied to the current view mode.
269 return m_storedCategorizedSorting
;
272 bool DolphinView::supportsCategorizedSorting() const
274 return m_iconsView
!= 0;
277 void DolphinView::selectAll()
279 QAbstractItemView
* view
= itemView();
280 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
281 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
282 // selection instead of selecting all items. This is bypassed for KDE 4.0
283 // by invoking clearSelection() first.
284 view
->clearSelection();
288 void DolphinView::invertSelection()
290 if (isColumnViewActive()) {
291 // QAbstractItemView does not offer a virtual method invertSelection()
292 // as counterpart to QAbstractItemView::selectAll(). This makes it
293 // necessary to delegate the inverting of the selection to the
294 // column view, as only the selection of the active column should
296 m_columnView
->invertSelection();
298 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
299 const QAbstractItemModel
* itemModel
= selectionModel
->model();
301 const QModelIndex topLeft
= itemModel
->index(0, 0);
302 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
303 itemModel
->columnCount() - 1);
305 const QItemSelection
selection(topLeft
, bottomRight
);
306 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
310 bool DolphinView::hasSelection() const
312 return itemView()->selectionModel()->hasSelection();
315 void DolphinView::clearSelection()
317 QItemSelectionModel
* selModel
= itemView()->selectionModel();
318 const QModelIndex currentIndex
= selModel
->currentIndex();
319 selModel
->setCurrentIndex(currentIndex
, QItemSelectionModel::Current
|
320 QItemSelectionModel::Clear
);
323 KFileItemList
DolphinView::selectedItems() const
325 if (isColumnViewActive()) {
326 return m_columnView
->selectedItems();
329 const QAbstractItemView
* view
= itemView();
331 // Our view has a selection, we will map them back to the DolphinModel
332 // and then fill the KFileItemList.
333 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
335 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
336 KFileItemList itemList
;
338 const QModelIndexList indexList
= selection
.indexes();
339 foreach (const QModelIndex
&index
, indexList
) {
340 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
341 if (!item
.isNull()) {
342 itemList
.append(item
);
349 KUrl::List
DolphinView::selectedUrls() const
352 const KFileItemList list
= selectedItems();
353 foreach (const KFileItem
&item
, list
) {
354 urls
.append(item
.url());
359 int DolphinView::selectedItemsCount() const
361 if (isColumnViewActive()) {
362 // TODO: get rid of this special case by adjusting the dir lister
363 // to the current column
364 return m_columnView
->selectedItems().count();
367 return itemView()->selectionModel()->selection().count();
370 void DolphinView::setContentsPosition(int x
, int y
)
372 QAbstractItemView
* view
= itemView();
374 // the ColumnView takes care itself for the horizontal scrolling
375 if (!isColumnViewActive()) {
376 view
->horizontalScrollBar()->setValue(x
);
378 view
->verticalScrollBar()->setValue(y
);
380 m_loadingDirectory
= false;
383 QPoint
DolphinView::contentsPosition() const
385 const int x
= itemView()->horizontalScrollBar()->value();
386 const int y
= itemView()->verticalScrollBar()->value();
390 void DolphinView::setZoomLevel(int level
)
392 if (level
< ZoomLevelInfo::minimumLevel()) {
393 level
= ZoomLevelInfo::minimumLevel();
394 } else if (level
> ZoomLevelInfo::maximumLevel()) {
395 level
= ZoomLevelInfo::maximumLevel();
398 if (level
!= zoomLevel()) {
399 m_controller
->setZoomLevel(level
);
400 m_previewGenerator
->updatePreviews();
401 emit
zoomLevelChanged(level
);
405 int DolphinView::zoomLevel() const
407 return m_controller
->zoomLevel();
410 void DolphinView::setSorting(Sorting sorting
)
412 if (sorting
!= this->sorting()) {
413 updateSorting(sorting
);
417 DolphinView::Sorting
DolphinView::sorting() const
419 return m_proxyModel
->sorting();
422 void DolphinView::setSortOrder(Qt::SortOrder order
)
424 if (sortOrder() != order
) {
425 updateSortOrder(order
);
429 Qt::SortOrder
DolphinView::sortOrder() const
431 return m_proxyModel
->sortOrder();
434 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
436 const KUrl viewPropsUrl
= viewPropertiesUrl();
437 ViewProperties
props(viewPropsUrl
);
438 props
.setAdditionalInfo(info
);
439 m_fileItemDelegate
->setShowInformation(info
);
441 emit
additionalInfoChanged();
443 if (itemView() != m_detailsView
) {
444 // the details view requires no reloading of the directory, as it maps
445 // the file item delegate info to its columns internally
446 loadDirectory(viewPropsUrl
);
450 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
452 return m_fileItemDelegate
->showInformation();
455 void DolphinView::reload()
458 loadDirectory(url(), true);
461 void DolphinView::refresh()
463 m_ignoreViewProperties
= false;
465 const bool oldActivationState
= m_active
;
466 const int oldZoomLevel
= m_controller
->zoomLevel();
470 applyViewProperties(m_controller
->url());
473 setActive(oldActivationState
);
474 updateZoomLevel(oldZoomLevel
);
477 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
479 if (m_controller
->url() == url
) {
483 m_previewGenerator
->cancelPreviews();
484 m_controller
->setUrl(url
); // emits urlChanged, which we forward
486 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
487 applyViewProperties(rootUrl
);
488 loadDirectory(rootUrl
);
489 if (itemView() == m_columnView
) {
490 m_columnView
->setRootUrl(rootUrl
);
491 m_columnView
->showColumn(url
);
494 applyViewProperties(url
);
498 emit
startedPathLoading(url
);
501 void DolphinView::setNameFilter(const QString
& nameFilter
)
503 m_proxyModel
->setFilterRegExp(nameFilter
);
505 if (isColumnViewActive()) {
506 // adjusting the directory lister is not enough in the case of the
507 // column view, as each column has its own directory lister internally...
508 m_columnView
->setNameFilter(nameFilter
);
512 void DolphinView::calculateItemCount(int& fileCount
,
514 KIO::filesize_t
& totalFileSize
) const
516 foreach (const KFileItem
& item
, m_dirLister
->items()) {
521 totalFileSize
+= item
.size();
526 QString
DolphinView::statusBarText() const
531 KIO::filesize_t totalFileSize
= 0;
533 if (hasSelection()) {
534 // give a summary of the status of the selected files
535 const KFileItemList list
= selectedItems();
536 if (list
.isEmpty()) {
537 // when an item is triggered, it is temporary selected but selectedItems()
538 // will return an empty list
542 KFileItemList::const_iterator it
= list
.begin();
543 const KFileItemList::const_iterator end
= list
.end();
545 const KFileItem
& item
= *it
;
550 totalFileSize
+= item
.size();
555 if (folderCount
+ fileCount
== 1) {
556 // if only one item is selected, show the filename
557 const QString name
= list
.first().name();
558 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
559 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
560 name
, KIO::convertSize(totalFileSize
));
562 // at least 2 items are selected
563 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
564 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
565 if ((folderCount
> 0) && (fileCount
> 0)) {
566 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
567 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
568 } else if (fileCount
> 0) {
569 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
571 Q_ASSERT(folderCount
> 0);
576 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
577 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
578 fileCount
, folderCount
,
579 totalFileSize
, true);
585 void DolphinView::setUrl(const KUrl
& url
)
587 // remember current item candidate (see restoreCurrentItem())
588 m_currentItemUrl
= url
;
589 updateView(url
, KUrl());
592 void DolphinView::changeSelection(const KFileItemList
& selection
)
595 if (selection
.isEmpty()) {
598 const KUrl
& baseUrl
= url();
600 QItemSelection new_selection
;
601 foreach(const KFileItem
& item
, selection
) {
602 url
= item
.url().upUrl();
603 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
604 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
605 new_selection
.select(index
, index
);
608 itemView()->selectionModel()->select(new_selection
,
609 QItemSelectionModel::ClearAndSelect
610 | QItemSelectionModel::Current
);
613 void DolphinView::renameSelectedItems()
615 KFileItemList items
= selectedItems();
616 const int itemCount
= items
.count();
622 // More than one item has been selected for renaming. Open
623 // a rename dialog and rename all items afterwards.
624 RenameDialog
dialog(this, items
);
625 if (dialog
.exec() == QDialog::Rejected
) {
629 const QString newName
= dialog
.newName();
630 if (newName
.isEmpty()) {
631 emit
errorMessage(dialog
.errorString());
633 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
634 // as one operation instead of n rename operations like it is done now...
635 Q_ASSERT(newName
.contains('#'));
637 // currently the items are sorted by the selection order, resort
638 // them by the file name
639 qSort(items
.begin(), items
.end(), lessThan
);
641 // iterate through all selected items and rename them...
643 foreach (const KFileItem
& item
, items
) {
644 const KUrl
& oldUrl
= item
.url();
646 number
.setNum(index
++);
648 QString name
= newName
;
649 name
.replace('#', number
);
651 if (oldUrl
.fileName() != name
) {
652 KUrl newUrl
= oldUrl
;
653 newUrl
.setFileName(name
);
654 KonqOperations::rename(this, oldUrl
, newUrl
);
658 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
659 Q_ASSERT(itemCount
== 1);
661 if (isColumnViewActive()) {
662 m_columnView
->editItem(items
.first());
664 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
665 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
666 itemView()->edit(proxyIndex
);
669 Q_ASSERT(itemCount
== 1);
671 RenameDialog
dialog(this, items
);
672 if (dialog
.exec() == QDialog::Rejected
) {
676 const QString
& newName
= dialog
.newName();
677 if (newName
.isEmpty()) {
678 emit
errorMessage(dialog
.errorString());
680 const KUrl
& oldUrl
= items
.first().url();
681 KUrl newUrl
= oldUrl
;
682 newUrl
.setFileName(newName
);
683 KonqOperations::rename(this, oldUrl
, newUrl
);
688 void DolphinView::trashSelectedItems()
690 const KUrl::List list
= simplifiedSelectedUrls();
691 KonqOperations::del(this, KonqOperations::TRASH
, list
);
694 void DolphinView::deleteSelectedItems()
696 const KUrl::List list
= simplifiedSelectedUrls();
697 const bool del
= KonqOperations::askDeleteConfirmation(list
,
699 KonqOperations::DEFAULT_CONFIRMATION
,
703 KIO::Job
* job
= KIO::del(list
);
704 connect(job
, SIGNAL(result(KJob
*)),
705 this, SLOT(slotDeleteFileFinished(KJob
*)));
709 void DolphinView::cutSelectedItems()
711 QMimeData
* mimeData
= selectionMimeData();
712 KonqMimeData::addIsCutSelection(mimeData
, true);
713 QApplication::clipboard()->setMimeData(mimeData
);
716 void DolphinView::copySelectedItems()
718 QMimeData
* mimeData
= selectionMimeData();
719 QApplication::clipboard()->setMimeData(mimeData
);
722 void DolphinView::paste()
727 void DolphinView::pasteIntoFolder()
729 const KFileItemList items
= selectedItems();
730 if ((items
.count() == 1) && items
.first().isDir()) {
731 pasteToUrl(items
.first().url());
735 void DolphinView::setShowPreview(bool show
)
737 if (m_showPreview
== show
) {
741 const KUrl viewPropsUrl
= viewPropertiesUrl();
742 ViewProperties
props(viewPropsUrl
);
743 props
.setShowPreview(show
);
745 m_showPreview
= show
;
746 m_previewGenerator
->setPreviewShown(show
);
748 const int oldZoomLevel
= m_controller
->zoomLevel();
749 emit
showPreviewChanged();
751 // Enabling or disabling the preview might change the icon size of the view.
752 // As the view does not emit a signal when the icon size has been changed,
753 // the used zoom level of the controller must be adjusted manually:
754 updateZoomLevel(oldZoomLevel
);
756 loadDirectory(viewPropsUrl
);
759 void DolphinView::setShowHiddenFiles(bool show
)
761 if (m_dirLister
->showingDotFiles() == show
) {
765 const KUrl viewPropsUrl
= viewPropertiesUrl();
766 ViewProperties
props(viewPropsUrl
);
767 props
.setShowHiddenFiles(show
);
769 m_dirLister
->setShowingDotFiles(show
);
770 emit
showHiddenFilesChanged();
772 loadDirectory(viewPropsUrl
);
775 void DolphinView::setCategorizedSorting(bool categorized
)
777 if (categorized
== categorizedSorting()) {
781 // setCategorizedSorting(true) may only get invoked
782 // if the view supports categorized sorting
783 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
785 ViewProperties
props(viewPropertiesUrl());
786 props
.setCategorizedSorting(categorized
);
789 m_storedCategorizedSorting
= categorized
;
790 m_proxyModel
->setCategorizedModel(categorized
);
792 emit
categorizedSortingChanged();
795 void DolphinView::toggleSortOrder()
797 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
798 Qt::DescendingOrder
:
803 void DolphinView::toggleAdditionalInfo(QAction
* action
)
805 const KFileItemDelegate::Information info
=
806 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
808 KFileItemDelegate::InformationList list
= additionalInfo();
810 const bool show
= action
->isChecked();
812 const int index
= list
.indexOf(info
);
813 const bool containsInfo
= (index
>= 0);
814 if (show
&& !containsInfo
) {
816 setAdditionalInfo(list
);
817 } else if (!show
&& containsInfo
) {
818 list
.removeAt(index
);
819 setAdditionalInfo(list
);
820 Q_ASSERT(list
.indexOf(info
) < 0);
824 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
826 QWidget::mouseReleaseEvent(event
);
830 void DolphinView::wheelEvent(QWheelEvent
* event
)
832 if (event
->modifiers() & Qt::ControlModifier
) {
833 const int delta
= event
->delta();
834 const int level
= zoomLevel();
836 setZoomLevel(level
+ 1);
837 } else if (delta
< 0) {
838 setZoomLevel(level
- 1);
844 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
846 switch (event
->type()) {
847 case QEvent::FocusIn
:
848 if (watched
== itemView()) {
849 m_controller
->requestActivation();
853 case QEvent::MouseButtonPress
:
854 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
855 // Listening to a mousebutton press event to delete expanded views is a
856 // workaround, as it seems impossible for the FolderExpander to know when
857 // a dragging outside a view has been finished. However it works quite well:
858 // A mousebutton press event indicates that a drag operation must be
860 m_expandedDragSource
->deleteLater();
861 m_expandedDragSource
= 0;
865 case QEvent::DragEnter
:
866 if (watched
== itemView()->viewport()) {
871 case QEvent::KeyPress
:
872 if (watched
== itemView()) {
873 if (m_toolTipManager
!= 0) {
874 m_toolTipManager
->hideTip();
877 // clear the selection when Escape has been pressed
878 QKeyEvent
* keyEvent
= static_cast<QKeyEvent
*>(event
);
879 if (keyEvent
->key() == Qt::Key_Escape
) {
889 return QWidget::eventFilter(watched
, event
);
892 void DolphinView::activate()
897 void DolphinView::triggerItem(const KFileItem
& item
)
899 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
900 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
901 // items are selected by the user, hence don't trigger the
902 // item specified by 'index'
906 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
907 if (item
.isNull() || m_isContextMenuOpen
) {
911 if (m_toolTipManager
!= 0) {
912 m_toolTipManager
->hideTip();
914 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
917 void DolphinView::emitSelectionChangedSignal()
919 emit
selectionChanged(DolphinView::selectedItems());
922 void DolphinView::openContextMenu(const QPoint
& pos
,
923 const QList
<QAction
*>& customActions
)
926 if (isColumnViewActive()) {
927 item
= m_columnView
->itemAt(pos
);
929 const QModelIndex index
= itemView()->indexAt(pos
);
930 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
931 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
932 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
936 if (m_toolTipManager
!= 0) {
937 m_toolTipManager
->hideTip();
940 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
941 emit
requestContextMenu(item
, url(), customActions
);
942 m_isContextMenuOpen
= false;
945 void DolphinView::dropUrls(const KFileItem
& destItem
,
946 const KUrl
& destPath
,
949 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
952 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
954 ViewProperties
props(viewPropertiesUrl());
955 props
.setSorting(sorting
);
957 m_proxyModel
->setSorting(sorting
);
959 emit
sortingChanged(sorting
);
962 void DolphinView::updateSortOrder(Qt::SortOrder order
)
964 ViewProperties
props(viewPropertiesUrl());
965 props
.setSortOrder(order
);
967 m_proxyModel
->setSortOrder(order
);
969 emit
sortOrderChanged(order
);
972 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
974 ViewProperties
props(viewPropertiesUrl());
975 props
.setAdditionalInfo(info
);
978 m_fileItemDelegate
->setShowInformation(info
);
980 emit
additionalInfoChanged();
983 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
985 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
986 (m_mode
== DolphinView::IconsView
);
988 QAction
* showSizeInfo
= collection
->action("show_size_info");
989 QAction
* showDateInfo
= collection
->action("show_date_info");
990 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
991 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
992 QAction
* showGroupInfo
= collection
->action("show_group_info");
993 QAction
* showMimeInfo
= collection
->action("show_mime_info");
995 showSizeInfo
->setChecked(false);
996 showDateInfo
->setChecked(false);
997 showPermissionsInfo
->setChecked(false);
998 showOwnerInfo
->setChecked(false);
999 showGroupInfo
->setChecked(false);
1000 showMimeInfo
->setChecked(false);
1002 showSizeInfo
->setEnabled(enable
);
1003 showDateInfo
->setEnabled(enable
);
1004 showPermissionsInfo
->setEnabled(enable
);
1005 showOwnerInfo
->setEnabled(enable
);
1006 showGroupInfo
->setEnabled(enable
);
1007 showMimeInfo
->setEnabled(enable
);
1009 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1011 case KFileItemDelegate::Size
:
1012 showSizeInfo
->setChecked(true);
1014 case KFileItemDelegate::ModificationTime
:
1015 showDateInfo
->setChecked(true);
1017 case KFileItemDelegate::Permissions
:
1018 showPermissionsInfo
->setChecked(true);
1020 case KFileItemDelegate::Owner
:
1021 showOwnerInfo
->setChecked(true);
1023 case KFileItemDelegate::OwnerAndGroup
:
1024 showGroupInfo
->setChecked(true);
1026 case KFileItemDelegate::FriendlyMimeType
:
1027 showMimeInfo
->setChecked(true);
1035 QPair
<bool, QString
> DolphinView::pasteInfo() const
1037 QPair
<bool, QString
> ret
;
1038 QClipboard
* clipboard
= QApplication::clipboard();
1039 const QMimeData
* mimeData
= clipboard
->mimeData();
1041 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1042 if (!urls
.isEmpty()) {
1043 // disable the paste action if no writing is supported
1044 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1045 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1047 if (urls
.count() == 1) {
1048 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1049 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1050 i18nc("@action:inmenu", "Paste One File");
1053 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1057 ret
.second
= i18nc("@action:inmenu", "Paste");
1063 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1065 m_tabsForFiles
= tabsForFiles
;
1068 bool DolphinView::isTabsForFilesEnabled() const
1070 return m_tabsForFiles
;
1073 bool DolphinView::itemsExpandable() const
1075 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1078 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1083 if (DragAndDropHelper::instance().isDragSource(view
)) {
1084 // We must store for later deletion.
1085 if (m_expandedDragSource
!= 0) {
1086 // The old stored view is obviously not the drag source anymore.
1087 m_expandedDragSource
->deleteLater();
1088 m_expandedDragSource
= 0;
1091 m_expandedDragSource
= view
;
1094 view
->deleteLater();
1098 void DolphinView::emitContentsMoved()
1100 // only emit the contents moved signal if:
1101 // - no directory loading is ongoing (this would reset the contents position
1102 // always to (0, 0))
1103 // - if the Column View is active: the column view does an automatic
1104 // positioning during the loading operation, which must be remembered
1105 if (!m_loadingDirectory
|| isColumnViewActive()) {
1106 const QPoint
pos(contentsPosition());
1107 emit
contentsMoved(pos
.x(), pos
.y());
1111 void DolphinView::showHoverInformation(const KFileItem
& item
)
1113 emit
requestItemInfo(item
);
1116 void DolphinView::clearHoverInformation()
1118 emit
requestItemInfo(KFileItem());
1121 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1123 if (job
->error() == 0) {
1124 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1125 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1126 emit
errorMessage(job
->errorString());
1130 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1132 emit
requestUrlChange(url
);
1133 m_controller
->setUrl(url
);
1136 void DolphinView::restoreCurrentItem()
1138 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1139 if (dirIndex
.isValid()) {
1140 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1141 QAbstractItemView
* view
= itemView();
1142 const bool clearSelection
= !hasSelection();
1143 view
->setCurrentIndex(proxyIndex
);
1144 if (clearSelection
) {
1145 view
->clearSelection();
1150 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1152 if (!url
.isValid()) {
1153 const QString
location(url
.pathOrUrl());
1154 if (location
.isEmpty()) {
1155 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1157 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1162 m_loadingDirectory
= true;
1164 m_dirLister
->stop();
1165 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1167 if (isColumnViewActive()) {
1168 // adjusting the directory lister is not enough in the case of the
1169 // column view, as each column has its own directory lister internally...
1171 m_columnView
->reload();
1173 m_columnView
->showColumn(url
);
1178 KUrl
DolphinView::viewPropertiesUrl() const
1180 if (isColumnViewActive()) {
1181 return m_columnView
->rootUrl();
1187 void DolphinView::applyViewProperties(const KUrl
& url
)
1189 if (m_ignoreViewProperties
) {
1193 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1194 // The column view is active, hence don't apply the view properties
1195 // of sub directories (represented by columns) to the view. The
1196 // view always represents the properties of the first column.
1200 const ViewProperties
props(url
);
1202 const Mode mode
= props
.viewMode();
1203 if (m_mode
!= mode
) {
1204 const int oldZoomLevel
= m_controller
->zoomLevel();
1210 updateZoomLevel(oldZoomLevel
);
1212 if (itemView() == 0) {
1215 Q_ASSERT(itemView() != 0);
1216 Q_ASSERT(m_fileItemDelegate
!= 0);
1218 const bool showHiddenFiles
= props
.showHiddenFiles();
1219 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1220 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1221 emit
showHiddenFilesChanged();
1224 m_storedCategorizedSorting
= props
.categorizedSorting();
1225 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1226 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1227 m_proxyModel
->setCategorizedModel(categorized
);
1228 emit
categorizedSortingChanged();
1231 const DolphinView::Sorting sorting
= props
.sorting();
1232 if (sorting
!= m_proxyModel
->sorting()) {
1233 m_proxyModel
->setSorting(sorting
);
1234 emit
sortingChanged(sorting
);
1237 const Qt::SortOrder sortOrder
= props
.sortOrder();
1238 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1239 m_proxyModel
->setSortOrder(sortOrder
);
1240 emit
sortOrderChanged(sortOrder
);
1243 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1244 if (info
!= m_fileItemDelegate
->showInformation()) {
1245 m_fileItemDelegate
->setShowInformation(info
);
1246 emit
additionalInfoChanged();
1249 const bool showPreview
= props
.showPreview();
1250 if (showPreview
!= m_showPreview
) {
1251 m_showPreview
= showPreview
;
1252 m_previewGenerator
->setPreviewShown(showPreview
);
1254 const int oldZoomLevel
= m_controller
->zoomLevel();
1255 emit
showPreviewChanged();
1257 // Enabling or disabling the preview might change the icon size of the view.
1258 // As the view does not emit a signal when the icon size has been changed,
1259 // the used zoom level of the controller must be adjusted manually:
1260 updateZoomLevel(oldZoomLevel
);
1263 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1264 // During the lifetime of a DolphinView instance the global view properties
1265 // should not be changed. This allows e. g. to split a view and use different
1266 // view properties for each view.
1267 m_ignoreViewProperties
= true;
1271 void DolphinView::createView()
1274 Q_ASSERT(m_iconsView
== 0);
1275 Q_ASSERT(m_detailsView
== 0);
1276 Q_ASSERT(m_columnView
== 0);
1278 QAbstractItemView
* view
= 0;
1281 m_iconsView
= new DolphinIconsView(this, m_controller
);
1287 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1288 view
= m_detailsView
;
1292 m_columnView
= new DolphinColumnView(this, m_controller
);
1293 view
= m_columnView
;
1297 Q_ASSERT(view
!= 0);
1298 view
->installEventFilter(this);
1299 view
->viewport()->installEventFilter(this);
1300 setFocusProxy(view
);
1302 if (m_mode
!= ColumnView
) {
1303 // Give the view the ability to auto-expand its directories on hovering
1304 // (the column view takes care about this itself). If the details view
1305 // uses expandable folders, the auto-expanding should be used always.
1306 DolphinSettings
& settings
= DolphinSettings::instance();
1307 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1308 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1310 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1311 folderExpander
->setEnabled(enabled
);
1312 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1313 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1316 // Listen out for requests to delete the current column.
1317 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1318 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1321 m_controller
->setItemView(view
);
1323 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1324 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1325 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1326 view
->setItemDelegate(m_fileItemDelegate
);
1328 view
->setModel(m_proxyModel
);
1329 if (m_selectionModel
!= 0) {
1330 view
->setSelectionModel(m_selectionModel
);
1332 m_selectionModel
= view
->selectionModel();
1335 // reparent the selection model, as it should not be deleted
1336 // when deleting the model
1337 m_selectionModel
->setParent(this);
1339 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1341 m_previewGenerator
= new KFilePreviewGenerator(view
);
1342 m_previewGenerator
->setPreviewShown(m_showPreview
);
1344 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1345 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1346 connect(m_controller
, SIGNAL(hideToolTip()),
1347 m_toolTipManager
, SLOT(hideTip()));
1350 m_topLayout
->insertWidget(1, view
);
1352 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1353 this, SLOT(emitSelectionChangedSignal()));
1354 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1355 this, SLOT(emitContentsMoved()));
1356 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1357 this, SLOT(emitContentsMoved()));
1360 void DolphinView::deleteView()
1362 QAbstractItemView
* view
= itemView();
1364 // It's important to set the keyboard focus to the parent
1365 // before deleting the view: Otherwise when having a split
1366 // view the other view will get the focus and will request
1367 // an activation (see DolphinView::eventFilter()).
1371 m_topLayout
->removeWidget(view
);
1374 // m_previewGenerator's parent is not always destroyed, and we
1375 // don't want two active at once - manually delete.
1376 delete m_previewGenerator
;
1377 m_previewGenerator
= 0;
1380 m_controller
->disconnect(view
);
1383 deleteWhenNotDragSource(view
);
1389 m_fileItemDelegate
= 0;
1390 m_toolTipManager
= 0;
1394 QAbstractItemView
* DolphinView::itemView() const
1396 if (m_detailsView
!= 0) {
1397 return m_detailsView
;
1398 } else if (m_columnView
!= 0) {
1399 return m_columnView
;
1405 bool DolphinView::isCutItem(const KFileItem
& item
) const
1407 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1408 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1410 const KUrl
& itemUrl
= item
.url();
1411 KUrl::List::const_iterator it
= cutUrls
.begin();
1412 const KUrl::List::const_iterator end
= cutUrls
.end();
1414 if (*it
== itemUrl
) {
1423 void DolphinView::pasteToUrl(const KUrl
& url
)
1425 QClipboard
* clipboard
= QApplication::clipboard();
1426 const QMimeData
* mimeData
= clipboard
->mimeData();
1428 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1429 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1430 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1433 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1437 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1439 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1440 if (oldZoomLevel
!= newZoomLevel
) {
1441 m_controller
->setZoomLevel(newZoomLevel
);
1442 emit
zoomLevelChanged(newZoomLevel
);
1446 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1448 KUrl::List list
= selectedUrls();
1449 if (itemsExpandable() ) {
1450 list
= KDirModel::simplifiedUrlList(list
);
1455 QMimeData
* DolphinView::selectionMimeData() const
1457 if (isColumnViewActive()) {
1458 return m_columnView
->selectionMimeData();
1461 const QAbstractItemView
* view
= itemView();
1462 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1463 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1464 return m_dolphinModel
->mimeData(selection
.indexes());
1467 #include "dolphinview.moc"