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 "dolphinsettings.h"
61 #include "dolphin_generalsettings.h"
62 #include "draganddrophelper.h"
63 #include "folderexpander.h"
64 #include "renamedialog.h"
65 #include "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_mode(DolphinView::IconsView
),
96 m_fileItemDelegate(0),
98 m_dolphinModel(dolphinModel
),
99 m_dirLister(dirLister
),
100 m_proxyModel(proxyModel
),
101 m_previewGenerator(0),
107 m_topLayout
= new QVBoxLayout(this);
108 m_topLayout
->setSpacing(0);
109 m_topLayout
->setMargin(0);
111 m_controller
= new DolphinController(this);
112 m_controller
->setUrl(url
);
114 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
115 this, SIGNAL(urlChanged(const KUrl
&)));
116 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
117 this, SLOT(slotRequestUrlChange(const KUrl
&)));
119 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
120 this, SLOT(openContextMenu(const QPoint
&)));
121 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
122 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
123 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
124 this, SLOT(updateSorting(DolphinView::Sorting
)));
125 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
126 this, SLOT(updateSortOrder(Qt::SortOrder
)));
127 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
128 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
129 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
130 this, SLOT(triggerItem(const KFileItem
&)));
131 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
132 this, SIGNAL(tabRequested(const KUrl
&)));
133 connect(m_controller
, SIGNAL(activated()),
134 this, SLOT(activate()));
135 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
136 this, SLOT(showHoverInformation(const KFileItem
&)));
137 connect(m_controller
, SIGNAL(viewportEntered()),
138 this, SLOT(clearHoverInformation()));
140 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
141 this, SIGNAL(redirection(KUrl
, KUrl
)));
142 connect(m_dirLister
, SIGNAL(completed()),
143 this, SLOT(restoreCurrentItem()));
145 applyViewProperties(url
);
146 m_topLayout
->addWidget(itemView());
149 DolphinView::~DolphinView()
151 deleteExpandedViews();
154 const KUrl
& DolphinView::url() const
156 return m_controller
->url();
159 KUrl
DolphinView::rootUrl() const
161 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
164 void DolphinView::setActive(bool active
)
166 if (active
== m_active
) {
172 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
174 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
175 // bypasses the problem when having a split view and changing the active view to
176 // update the some URL dependent states. A nicer approach should be no big deal...
177 emit
urlChanged(url());
178 emit
selectionChanged(selectedItems());
183 QWidget
* viewport
= itemView()->viewport();
185 palette
.setColor(viewport
->backgroundRole(), color
);
186 viewport
->setPalette(palette
);
191 itemView()->setFocus();
195 m_controller
->indicateActivationChange(active
);
198 bool DolphinView::isActive() const
203 void DolphinView::setMode(Mode mode
)
205 if (mode
== m_mode
) {
206 return; // the wished mode is already set
209 const int oldZoomLevel
= m_controller
->zoomLevel();
214 const KUrl viewPropsUrl
= viewPropertiesUrl();
215 ViewProperties
props(viewPropsUrl
);
216 props
.setViewMode(m_mode
);
219 // the file item delegate has been recreated, apply the current
220 // additional information manually
221 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
222 m_fileItemDelegate
->setShowInformation(infoList
);
223 emit
additionalInfoChanged();
225 // Not all view modes support categorized sorting. Adjust the sorting model
226 // if changing the view mode results in a change of the categorized sorting
228 m_storedCategorizedSorting
= props
.categorizedSorting();
229 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
230 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
231 m_proxyModel
->setCategorizedModel(categorized
);
232 emit
categorizedSortingChanged();
237 updateZoomLevel(oldZoomLevel
);
239 loadDirectory(viewPropsUrl
);
243 DolphinView::Mode
DolphinView::mode() const
248 bool DolphinView::showPreview() const
250 return m_showPreview
;
253 bool DolphinView::showHiddenFiles() const
255 return m_dirLister
->showingDotFiles();
258 bool DolphinView::categorizedSorting() const
260 // If all view modes would support categorized sorting, returning
261 // m_proxyModel->isCategorizedModel() would be the way to go. As
262 // currently only the icons view supports caterized sorting, we remember
263 // the stored view properties state in m_storedCategorizedSorting and
264 // return this state. The application takes care to disable the corresponding
265 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
266 // that this setting is not applied to the current view mode.
267 return m_storedCategorizedSorting
;
270 bool DolphinView::supportsCategorizedSorting() const
272 return m_iconsView
!= 0;
275 void DolphinView::selectAll()
277 QAbstractItemView
* view
= itemView();
278 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
279 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
280 // selection instead of selecting all items. This is bypassed for KDE 4.0
281 // by invoking clearSelection() first.
282 view
->clearSelection();
286 void DolphinView::invertSelection()
288 if (isColumnViewActive()) {
289 // QAbstractItemView does not offer a virtual method invertSelection()
290 // as counterpart to QAbstractItemView::selectAll(). This makes it
291 // necessary to delegate the inverting of the selection to the
292 // column view, as only the selection of the active column should
294 m_columnView
->invertSelection();
296 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
297 const QAbstractItemModel
* itemModel
= selectionModel
->model();
299 const QModelIndex topLeft
= itemModel
->index(0, 0);
300 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
301 itemModel
->columnCount() - 1);
303 const QItemSelection
selection(topLeft
, bottomRight
);
304 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
308 bool DolphinView::hasSelection() const
310 return itemView()->selectionModel()->hasSelection();
313 void DolphinView::clearSelection()
315 itemView()->selectionModel()->clear();
318 KFileItemList
DolphinView::selectedItems() const
320 if (isColumnViewActive()) {
321 return m_columnView
->selectedItems();
324 const QAbstractItemView
* view
= itemView();
326 // Our view has a selection, we will map them back to the DolphinModel
327 // and then fill the KFileItemList.
328 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
330 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
331 KFileItemList itemList
;
333 const QModelIndexList indexList
= selection
.indexes();
334 foreach (const QModelIndex
&index
, indexList
) {
335 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
336 if (!item
.isNull()) {
337 itemList
.append(item
);
344 KUrl::List
DolphinView::selectedUrls() const
347 const KFileItemList list
= selectedItems();
348 foreach (const KFileItem
&item
, list
) {
349 urls
.append(item
.url());
354 int DolphinView::selectedItemsCount() const
356 if (isColumnViewActive()) {
357 // TODO: get rid of this special case by adjusting the dir lister
358 // to the current column
359 return m_columnView
->selectedItems().count();
362 return itemView()->selectionModel()->selection().count();
365 void DolphinView::setContentsPosition(int x
, int y
)
367 QAbstractItemView
* view
= itemView();
369 // the ColumnView takes care itself for the horizontal scrolling
370 if (!isColumnViewActive()) {
371 view
->horizontalScrollBar()->setValue(x
);
373 view
->verticalScrollBar()->setValue(y
);
375 m_loadingDirectory
= false;
378 QPoint
DolphinView::contentsPosition() const
380 const int x
= itemView()->horizontalScrollBar()->value();
381 const int y
= itemView()->verticalScrollBar()->value();
385 void DolphinView::setZoomLevel(int level
)
387 if (level
< ZoomLevelInfo::minimumLevel()) {
388 level
= ZoomLevelInfo::minimumLevel();
389 } else if (level
> ZoomLevelInfo::maximumLevel()) {
390 level
= ZoomLevelInfo::maximumLevel();
393 if (level
!= zoomLevel()) {
394 m_controller
->setZoomLevel(level
);
395 m_previewGenerator
->updatePreviews();
396 emit
zoomLevelChanged(level
);
400 int DolphinView::zoomLevel() const
402 return m_controller
->zoomLevel();
405 void DolphinView::setSorting(Sorting sorting
)
407 if (sorting
!= this->sorting()) {
408 updateSorting(sorting
);
412 DolphinView::Sorting
DolphinView::sorting() const
414 return m_proxyModel
->sorting();
417 void DolphinView::setSortOrder(Qt::SortOrder order
)
419 if (sortOrder() != order
) {
420 updateSortOrder(order
);
424 Qt::SortOrder
DolphinView::sortOrder() const
426 return m_proxyModel
->sortOrder();
429 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
431 const KUrl viewPropsUrl
= viewPropertiesUrl();
432 ViewProperties
props(viewPropsUrl
);
433 props
.setAdditionalInfo(info
);
434 m_fileItemDelegate
->setShowInformation(info
);
436 emit
additionalInfoChanged();
438 if (itemView() != m_detailsView
) {
439 // the details view requires no reloading of the directory, as it maps
440 // the file item delegate info to its columns internally
441 loadDirectory(viewPropsUrl
);
445 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
447 return m_fileItemDelegate
->showInformation();
450 void DolphinView::reload()
453 loadDirectory(url(), true);
456 void DolphinView::refresh()
458 const bool oldActivationState
= m_active
;
459 const int oldZoomLevel
= m_controller
->zoomLevel();
463 applyViewProperties(m_controller
->url());
466 setActive(oldActivationState
);
467 updateZoomLevel(oldZoomLevel
);
470 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
472 if (m_controller
->url() == url
) {
476 m_previewGenerator
->cancelPreviews();
477 m_controller
->setUrl(url
); // emits urlChanged, which we forward
479 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
480 applyViewProperties(rootUrl
);
481 loadDirectory(rootUrl
);
482 if (itemView() == m_columnView
) {
483 m_columnView
->setRootUrl(rootUrl
);
484 m_columnView
->showColumn(url
);
487 applyViewProperties(url
);
491 emit
startedPathLoading(url
);
494 void DolphinView::setNameFilter(const QString
& nameFilter
)
496 m_proxyModel
->setFilterRegExp(nameFilter
);
498 if (isColumnViewActive()) {
499 // adjusting the directory lister is not enough in the case of the
500 // column view, as each column has its own directory lister internally...
501 m_columnView
->setNameFilter(nameFilter
);
505 void DolphinView::calculateItemCount(int& fileCount
,
507 KIO::filesize_t
& totalFileSize
) const
509 foreach (const KFileItem
& item
, m_dirLister
->items()) {
514 totalFileSize
+= item
.size();
519 QString
DolphinView::statusBarText() const
524 KIO::filesize_t totalFileSize
= 0;
526 if (hasSelection()) {
527 // give a summary of the status of the selected files
528 const KFileItemList list
= selectedItems();
529 if (list
.isEmpty()) {
530 // when an item is triggered, it is temporary selected but selectedItems()
531 // will return an empty list
535 KFileItemList::const_iterator it
= list
.begin();
536 const KFileItemList::const_iterator end
= list
.end();
538 const KFileItem
& item
= *it
;
543 totalFileSize
+= item
.size();
548 if (folderCount
+ fileCount
== 1) {
549 // if only one item is selected, show the filename
550 const QString name
= list
.first().name();
551 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
552 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
553 name
, KIO::convertSize(totalFileSize
));
555 // at least 2 items are selected
556 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
557 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
558 if ((folderCount
> 0) && (fileCount
> 0)) {
559 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
560 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
561 } else if (fileCount
> 0) {
562 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
564 Q_ASSERT(folderCount
> 0);
569 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
570 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
571 fileCount
, folderCount
,
572 totalFileSize
, true);
578 void DolphinView::setUrl(const KUrl
& url
)
580 // remember current item candidate (see restoreCurrentItem())
581 m_currentItemUrl
= url
;
582 updateView(url
, KUrl());
585 void DolphinView::changeSelection(const KFileItemList
& selection
)
588 if (selection
.isEmpty()) {
591 const KUrl
& baseUrl
= url();
593 QItemSelection new_selection
;
594 foreach(const KFileItem
& item
, selection
) {
595 url
= item
.url().upUrl();
596 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
597 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
598 new_selection
.select(index
, index
);
601 itemView()->selectionModel()->select(new_selection
,
602 QItemSelectionModel::ClearAndSelect
603 | QItemSelectionModel::Current
);
606 void DolphinView::renameSelectedItems()
608 KFileItemList items
= selectedItems();
609 const int itemCount
= items
.count();
615 // More than one item has been selected for renaming. Open
616 // a rename dialog and rename all items afterwards.
617 RenameDialog
dialog(this, items
);
618 if (dialog
.exec() == QDialog::Rejected
) {
622 const QString newName
= dialog
.newName();
623 if (newName
.isEmpty()) {
624 emit
errorMessage(dialog
.errorString());
626 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
627 // as one operation instead of n rename operations like it is done now...
628 Q_ASSERT(newName
.contains('#'));
630 // currently the items are sorted by the selection order, resort
631 // them by the file name
632 qSort(items
.begin(), items
.end(), lessThan
);
634 // iterate through all selected items and rename them...
636 foreach (const KFileItem
& item
, items
) {
637 const KUrl
& oldUrl
= item
.url();
639 number
.setNum(index
++);
641 QString name
= newName
;
642 name
.replace('#', number
);
644 if (oldUrl
.fileName() != name
) {
645 KUrl newUrl
= oldUrl
;
646 newUrl
.setFileName(name
);
647 KonqOperations::rename(this, oldUrl
, newUrl
);
651 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
652 Q_ASSERT(itemCount
== 1);
654 if (isColumnViewActive()) {
655 m_columnView
->editItem(items
.first());
657 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
658 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
659 itemView()->edit(proxyIndex
);
662 Q_ASSERT(itemCount
== 1);
664 RenameDialog
dialog(this, items
);
665 if (dialog
.exec() == QDialog::Rejected
) {
669 const QString
& newName
= dialog
.newName();
670 if (newName
.isEmpty()) {
671 emit
errorMessage(dialog
.errorString());
673 const KUrl
& oldUrl
= items
.first().url();
674 KUrl newUrl
= oldUrl
;
675 newUrl
.setFileName(newName
);
676 KonqOperations::rename(this, oldUrl
, newUrl
);
681 void DolphinView::trashSelectedItems()
683 const KUrl::List list
= simplifiedSelectedUrls();
684 KonqOperations::del(this, KonqOperations::TRASH
, list
);
687 void DolphinView::deleteSelectedItems()
689 const KUrl::List list
= simplifiedSelectedUrls();
690 const bool del
= KonqOperations::askDeleteConfirmation(list
,
692 KonqOperations::DEFAULT_CONFIRMATION
,
696 KIO::Job
* job
= KIO::del(list
);
697 connect(job
, SIGNAL(result(KJob
*)),
698 this, SLOT(slotDeleteFileFinished(KJob
*)));
702 void DolphinView::cutSelectedItems()
704 QMimeData
* mimeData
= selectionMimeData();
705 const KUrl::List kdeUrls
= simplifiedSelectedUrls();
706 const KUrl::List mostLocalUrls
;
707 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
708 QApplication::clipboard()->setMimeData(mimeData
);
711 void DolphinView::copySelectedItems()
713 QMimeData
* mimeData
= selectionMimeData();
714 const KUrl::List kdeUrls
= selectedUrls();
715 const KUrl::List mostLocalUrls
;
716 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
717 QApplication::clipboard()->setMimeData(mimeData
);
720 void DolphinView::paste()
725 void DolphinView::pasteIntoFolder()
727 const KFileItemList items
= selectedItems();
728 if ((items
.count() == 1) && items
.first().isDir()) {
729 pasteToUrl(items
.first().url());
733 void DolphinView::setShowPreview(bool show
)
735 if (m_showPreview
== show
) {
739 const KUrl viewPropsUrl
= viewPropertiesUrl();
740 ViewProperties
props(viewPropsUrl
);
741 props
.setShowPreview(show
);
743 m_showPreview
= show
;
744 m_previewGenerator
->setPreviewShown(show
);
746 const int oldZoomLevel
= m_controller
->zoomLevel();
747 emit
showPreviewChanged();
749 // Enabling or disabling the preview might change the icon size of the view.
750 // As the view does not emit a signal when the icon size has been changed,
751 // the used zoom level of the controller must be adjusted manually:
752 updateZoomLevel(oldZoomLevel
);
754 loadDirectory(viewPropsUrl
);
757 void DolphinView::setShowHiddenFiles(bool show
)
759 if (m_dirLister
->showingDotFiles() == show
) {
763 const KUrl viewPropsUrl
= viewPropertiesUrl();
764 ViewProperties
props(viewPropsUrl
);
765 props
.setShowHiddenFiles(show
);
767 m_dirLister
->setShowingDotFiles(show
);
768 emit
showHiddenFilesChanged();
770 loadDirectory(viewPropsUrl
);
773 void DolphinView::setCategorizedSorting(bool categorized
)
775 if (categorized
== categorizedSorting()) {
779 // setCategorizedSorting(true) may only get invoked
780 // if the view supports categorized sorting
781 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
783 ViewProperties
props(viewPropertiesUrl());
784 props
.setCategorizedSorting(categorized
);
787 m_storedCategorizedSorting
= categorized
;
788 m_proxyModel
->setCategorizedModel(categorized
);
790 emit
categorizedSortingChanged();
793 void DolphinView::toggleSortOrder()
795 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
796 Qt::DescendingOrder
:
801 void DolphinView::toggleAdditionalInfo(QAction
* action
)
803 const KFileItemDelegate::Information info
=
804 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
806 KFileItemDelegate::InformationList list
= additionalInfo();
808 const bool show
= action
->isChecked();
810 const int index
= list
.indexOf(info
);
811 const bool containsInfo
= (index
>= 0);
812 if (show
&& !containsInfo
) {
814 setAdditionalInfo(list
);
815 } else if (!show
&& containsInfo
) {
816 list
.removeAt(index
);
817 setAdditionalInfo(list
);
818 Q_ASSERT(list
.indexOf(info
) < 0);
822 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
824 QWidget::mouseReleaseEvent(event
);
828 void DolphinView::wheelEvent(QWheelEvent
* event
)
830 if (event
->modifiers() & Qt::ControlModifier
) {
831 const int delta
= event
->delta();
832 const int level
= zoomLevel();
834 setZoomLevel(level
+ 1);
835 } else if (delta
< 0) {
836 setZoomLevel(level
- 1);
842 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
844 switch (event
->type()) {
845 case QEvent::FocusIn
:
846 if (watched
== itemView()) {
847 m_controller
->requestActivation();
851 case QEvent::MouseButtonPress
:
852 if ((watched
== itemView()->viewport()) && (m_expandedViews
.count() > 0)) {
853 // Listening to a mousebutton press event to delete expanded views is a
854 // workaround, as it seems impossible for the FolderExpander to know when
855 // a dragging outside a view has been finished. However it works quite well:
856 // A mousebutton press event indicates that a drag operation must be
858 deleteExpandedViews();
862 case QEvent::DragEnter
:
863 if (watched
== itemView()->viewport()) {
872 return QWidget::eventFilter(watched
, event
);
875 void DolphinView::activate()
880 void DolphinView::triggerItem(const KFileItem
& item
)
882 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
883 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
884 // items are selected by the user, hence don't trigger the
885 // item specified by 'index'
889 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
890 if (item
.isNull() || m_isContextMenuOpen
) {
894 if (m_toolTipManager
!= 0) {
895 m_toolTipManager
->hideTip();
897 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
900 void DolphinView::emitSelectionChangedSignal()
902 emit
selectionChanged(DolphinView::selectedItems());
905 void DolphinView::openContextMenu(const QPoint
& pos
)
908 if (isColumnViewActive()) {
909 item
= m_columnView
->itemAt(pos
);
911 const QModelIndex index
= itemView()->indexAt(pos
);
912 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
913 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
914 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
918 if (m_toolTipManager
!= 0) {
919 m_toolTipManager
->hideTip();
922 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
923 emit
requestContextMenu(item
, url());
924 m_isContextMenuOpen
= false;
927 void DolphinView::dropUrls(const KFileItem
& destItem
,
928 const KUrl
& destPath
,
931 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
934 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
936 ViewProperties
props(viewPropertiesUrl());
937 props
.setSorting(sorting
);
939 m_proxyModel
->setSorting(sorting
);
941 emit
sortingChanged(sorting
);
944 void DolphinView::updateSortOrder(Qt::SortOrder order
)
946 ViewProperties
props(viewPropertiesUrl());
947 props
.setSortOrder(order
);
949 m_proxyModel
->setSortOrder(order
);
951 emit
sortOrderChanged(order
);
954 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
956 ViewProperties
props(viewPropertiesUrl());
957 props
.setAdditionalInfo(info
);
960 m_fileItemDelegate
->setShowInformation(info
);
962 emit
additionalInfoChanged();
965 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
967 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
968 (m_mode
== DolphinView::IconsView
);
970 QAction
* showSizeInfo
= collection
->action("show_size_info");
971 QAction
* showDateInfo
= collection
->action("show_date_info");
972 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
973 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
974 QAction
* showGroupInfo
= collection
->action("show_group_info");
975 QAction
* showMimeInfo
= collection
->action("show_mime_info");
977 showSizeInfo
->setChecked(false);
978 showDateInfo
->setChecked(false);
979 showPermissionsInfo
->setChecked(false);
980 showOwnerInfo
->setChecked(false);
981 showGroupInfo
->setChecked(false);
982 showMimeInfo
->setChecked(false);
984 showSizeInfo
->setEnabled(enable
);
985 showDateInfo
->setEnabled(enable
);
986 showPermissionsInfo
->setEnabled(enable
);
987 showOwnerInfo
->setEnabled(enable
);
988 showGroupInfo
->setEnabled(enable
);
989 showMimeInfo
->setEnabled(enable
);
991 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
993 case KFileItemDelegate::Size
:
994 showSizeInfo
->setChecked(true);
996 case KFileItemDelegate::ModificationTime
:
997 showDateInfo
->setChecked(true);
999 case KFileItemDelegate::Permissions
:
1000 showPermissionsInfo
->setChecked(true);
1002 case KFileItemDelegate::Owner
:
1003 showOwnerInfo
->setChecked(true);
1005 case KFileItemDelegate::OwnerAndGroup
:
1006 showGroupInfo
->setChecked(true);
1008 case KFileItemDelegate::FriendlyMimeType
:
1009 showMimeInfo
->setChecked(true);
1017 QPair
<bool, QString
> DolphinView::pasteInfo() const
1019 QPair
<bool, QString
> ret
;
1020 QClipboard
* clipboard
= QApplication::clipboard();
1021 const QMimeData
* mimeData
= clipboard
->mimeData();
1023 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1024 if (!urls
.isEmpty()) {
1025 // disable the paste action if no writing is supported
1026 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1027 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1029 if (urls
.count() == 1) {
1030 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1031 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1032 i18nc("@action:inmenu", "Paste One File");
1035 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1039 ret
.second
= i18nc("@action:inmenu", "Paste");
1045 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1047 m_tabsForFiles
= tabsForFiles
;
1050 bool DolphinView::isTabsForFilesEnabled() const
1052 return m_tabsForFiles
;
1055 void DolphinView::emitContentsMoved()
1057 // only emit the contents moved signal if:
1058 // - no directory loading is ongoing (this would reset the contents position
1059 // always to (0, 0))
1060 // - if the Column View is active: the column view does an automatic
1061 // positioning during the loading operation, which must be remembered
1062 if (!m_loadingDirectory
|| isColumnViewActive()) {
1063 const QPoint
pos(contentsPosition());
1064 emit
contentsMoved(pos
.x(), pos
.y());
1068 void DolphinView::showHoverInformation(const KFileItem
& item
)
1070 emit
requestItemInfo(item
);
1073 void DolphinView::clearHoverInformation()
1075 emit
requestItemInfo(KFileItem());
1078 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1080 if (job
->error() == 0) {
1081 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1082 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1083 emit
errorMessage(job
->errorString());
1087 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1089 emit
requestUrlChange(url
);
1090 m_controller
->setUrl(url
);
1093 void DolphinView::restoreCurrentItem()
1095 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1096 if (dirIndex
.isValid()) {
1097 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1098 QAbstractItemView
* view
= itemView();
1099 const bool clearSelection
= !hasSelection();
1100 view
->setCurrentIndex(proxyIndex
);
1101 if (clearSelection
) {
1102 view
->clearSelection();
1107 void DolphinView::enterDir(const QModelIndex
& index
, QAbstractItemView
* view
)
1109 // Deleting a view that is the root of a drag operation is not allowed, otherwise
1110 // the dragging gets automatically cancelled by Qt. So before entering a new
1111 // directory, the current view is remembered in m_expandedViews and deleted
1112 // later when the drag operation has been finished (see DolphinView::eventFilter()).
1113 m_expandedViews
.append(view
);
1114 m_controller
->triggerItem(index
);
1117 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1119 if (!url
.isValid()) {
1120 const QString
location(url
.pathOrUrl());
1121 if (location
.isEmpty()) {
1122 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1124 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1129 m_loadingDirectory
= true;
1131 m_dirLister
->stop();
1132 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1134 if (isColumnViewActive()) {
1135 // adjusting the directory lister is not enough in the case of the
1136 // column view, as each column has its own directory lister internally...
1138 m_columnView
->reload();
1140 m_columnView
->showColumn(url
);
1145 KUrl
DolphinView::viewPropertiesUrl() const
1147 if (isColumnViewActive()) {
1148 return m_columnView
->rootUrl();
1154 void DolphinView::applyViewProperties(const KUrl
& url
)
1156 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1157 // The column view is active, hence don't apply the view properties
1158 // of sub directories (represented by columns) to the view. The
1159 // view always represents the properties of the first column.
1163 const ViewProperties
props(url
);
1165 const Mode mode
= props
.viewMode();
1166 if (m_mode
!= mode
) {
1167 const int oldZoomLevel
= m_controller
->zoomLevel();
1173 updateZoomLevel(oldZoomLevel
);
1175 if (itemView() == 0) {
1178 Q_ASSERT(itemView() != 0);
1179 Q_ASSERT(m_fileItemDelegate
!= 0);
1181 const bool showHiddenFiles
= props
.showHiddenFiles();
1182 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1183 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1184 emit
showHiddenFilesChanged();
1187 m_storedCategorizedSorting
= props
.categorizedSorting();
1188 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1189 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1190 m_proxyModel
->setCategorizedModel(categorized
);
1191 emit
categorizedSortingChanged();
1194 const DolphinView::Sorting sorting
= props
.sorting();
1195 if (sorting
!= m_proxyModel
->sorting()) {
1196 m_proxyModel
->setSorting(sorting
);
1197 emit
sortingChanged(sorting
);
1200 const Qt::SortOrder sortOrder
= props
.sortOrder();
1201 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1202 m_proxyModel
->setSortOrder(sortOrder
);
1203 emit
sortOrderChanged(sortOrder
);
1206 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1207 if (info
!= m_fileItemDelegate
->showInformation()) {
1208 m_fileItemDelegate
->setShowInformation(info
);
1209 emit
additionalInfoChanged();
1212 const bool showPreview
= props
.showPreview();
1213 if (showPreview
!= m_showPreview
) {
1214 m_showPreview
= showPreview
;
1215 m_previewGenerator
->setPreviewShown(showPreview
);
1217 const int oldZoomLevel
= m_controller
->zoomLevel();
1218 emit
showPreviewChanged();
1220 // Enabling or disabling the preview might change the icon size of the view.
1221 // As the view does not emit a signal when the icon size has been changed,
1222 // the used zoom level of the controller must be adjusted manually:
1223 updateZoomLevel(oldZoomLevel
);
1227 void DolphinView::createView()
1230 Q_ASSERT(m_iconsView
== 0);
1231 Q_ASSERT(m_detailsView
== 0);
1232 Q_ASSERT(m_columnView
== 0);
1234 QAbstractItemView
* view
= 0;
1237 m_iconsView
= new DolphinIconsView(this, m_controller
);
1243 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1244 view
= m_detailsView
;
1248 m_columnView
= new DolphinColumnView(this, m_controller
);
1249 view
= m_columnView
;
1253 Q_ASSERT(view
!= 0);
1254 view
->installEventFilter(this);
1255 view
->viewport()->installEventFilter(this);
1257 if (m_mode
!= ColumnView
) {
1258 // Give the view the ability to auto-expand its directories on hovering
1259 // (the column view takes care about this itself). If the details view
1260 // uses expandable folders, the auto-expanding should be used always.
1261 DolphinSettings
& settings
= DolphinSettings::instance();
1262 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1263 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1265 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1266 folderExpander
->setEnabled(enabled
);
1267 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&, QAbstractItemView
*)),
1268 this, SLOT(enterDir(const QModelIndex
&, QAbstractItemView
*)));
1271 m_controller
->setItemView(view
);
1273 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1274 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1275 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1276 view
->setItemDelegate(m_fileItemDelegate
);
1278 view
->setModel(m_proxyModel
);
1279 if (m_selectionModel
!= 0) {
1280 view
->setSelectionModel(m_selectionModel
);
1282 m_selectionModel
= view
->selectionModel();
1285 // reparent the selection model, as it should not be deleted
1286 // when deleting the model
1287 m_selectionModel
->setParent(this);
1289 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1291 m_previewGenerator
= new KFilePreviewGenerator(view
);
1292 m_previewGenerator
->setPreviewShown(m_showPreview
);
1294 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1295 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1296 connect(m_controller
, SIGNAL(hideToolTip()),
1297 m_toolTipManager
, SLOT(hideTip()));
1300 m_topLayout
->insertWidget(1, view
);
1302 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1303 this, SLOT(emitSelectionChangedSignal()));
1304 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1305 this, SLOT(emitContentsMoved()));
1306 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1307 this, SLOT(emitContentsMoved()));
1310 void DolphinView::deleteView()
1312 QAbstractItemView
* view
= itemView();
1314 // It's important to set the keyboard focus to the parent
1315 // before deleting the view: Otherwise when having a split
1316 // view the other view will get the focus and will request
1317 // an activation (see DolphinView::eventFilter()).
1320 m_topLayout
->removeWidget(view
);
1324 m_controller
->disconnect(view
);
1327 bool deleteView
= true;
1328 foreach (const QAbstractItemView
* expandedView
, m_expandedViews
) {
1329 if (view
== expandedView
) {
1330 // the current view got already expanded and must stay alive
1331 // until the dragging has been completed
1337 view
->deleteLater();
1344 m_fileItemDelegate
= 0;
1345 m_previewGenerator
= 0;
1346 m_toolTipManager
= 0;
1350 QAbstractItemView
* DolphinView::itemView() const
1352 if (m_detailsView
!= 0) {
1353 return m_detailsView
;
1354 } else if (m_columnView
!= 0) {
1355 return m_columnView
;
1361 bool DolphinView::isCutItem(const KFileItem
& item
) const
1363 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1364 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1366 const KUrl
& itemUrl
= item
.url();
1367 KUrl::List::const_iterator it
= cutUrls
.begin();
1368 const KUrl::List::const_iterator end
= cutUrls
.end();
1370 if (*it
== itemUrl
) {
1379 void DolphinView::pasteToUrl(const KUrl
& url
)
1381 QClipboard
* clipboard
= QApplication::clipboard();
1382 const QMimeData
* mimeData
= clipboard
->mimeData();
1384 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1385 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1386 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1389 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1393 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1395 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1396 if (oldZoomLevel
!= newZoomLevel
) {
1397 m_controller
->setZoomLevel(newZoomLevel
);
1398 emit
zoomLevelChanged(newZoomLevel
);
1402 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1404 KUrl::List list
= selectedUrls();
1405 if (itemsExpandable() ) {
1406 list
= KonqOperations::simplifiedUrlList(list
);
1411 void DolphinView::deleteExpandedViews()
1413 const QAbstractItemView
* view
= itemView();
1414 foreach (QAbstractItemView
* expandedView
, m_expandedViews
) {
1415 if (expandedView
!= view
) {
1416 expandedView
->deleteLater();
1419 m_expandedViews
.clear();
1422 bool DolphinView::itemsExpandable() const
1424 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1427 QMimeData
* DolphinView::selectionMimeData() const
1429 if (isColumnViewActive()) {
1430 return m_columnView
->selectionMimeData();
1433 const QAbstractItemView
* view
= itemView();
1434 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1435 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1436 return m_dolphinModel
->mimeData(selection
.indexes());
1439 #include "dolphinview.moc"