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
&)),
121 this, SLOT(openContextMenu(const QPoint
&)));
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 kDebug() << "Deleted view " << m_expandedDragSource
;
153 delete m_expandedDragSource
;
154 m_expandedDragSource
= 0;
157 const KUrl
& DolphinView::url() const
159 return m_controller
->url();
162 KUrl
DolphinView::rootUrl() const
164 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
167 void DolphinView::setActive(bool active
)
169 if (active
== m_active
) {
175 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
177 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
178 // bypasses the problem when having a split view and changing the active view to
179 // update the some URL dependent states. A nicer approach should be no big deal...
180 emit
urlChanged(url());
181 emit
selectionChanged(selectedItems());
186 QWidget
* viewport
= itemView()->viewport();
188 palette
.setColor(viewport
->backgroundRole(), color
);
189 viewport
->setPalette(palette
);
194 itemView()->setFocus();
198 m_controller
->indicateActivationChange(active
);
201 bool DolphinView::isActive() const
206 void DolphinView::setMode(Mode mode
)
208 if (mode
== m_mode
) {
209 return; // the wished mode is already set
212 const int oldZoomLevel
= m_controller
->zoomLevel();
217 const KUrl viewPropsUrl
= viewPropertiesUrl();
218 ViewProperties
props(viewPropsUrl
);
219 props
.setViewMode(m_mode
);
222 // the file item delegate has been recreated, apply the current
223 // additional information manually
224 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
225 m_fileItemDelegate
->setShowInformation(infoList
);
226 emit
additionalInfoChanged();
228 // Not all view modes support categorized sorting. Adjust the sorting model
229 // if changing the view mode results in a change of the categorized sorting
231 m_storedCategorizedSorting
= props
.categorizedSorting();
232 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
233 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
234 m_proxyModel
->setCategorizedModel(categorized
);
235 emit
categorizedSortingChanged();
240 updateZoomLevel(oldZoomLevel
);
242 loadDirectory(viewPropsUrl
);
246 DolphinView::Mode
DolphinView::mode() const
251 bool DolphinView::showPreview() const
253 return m_showPreview
;
256 bool DolphinView::showHiddenFiles() const
258 return m_dirLister
->showingDotFiles();
261 bool DolphinView::categorizedSorting() const
263 // If all view modes would support categorized sorting, returning
264 // m_proxyModel->isCategorizedModel() would be the way to go. As
265 // currently only the icons view supports caterized sorting, we remember
266 // the stored view properties state in m_storedCategorizedSorting and
267 // return this state. The application takes care to disable the corresponding
268 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
269 // that this setting is not applied to the current view mode.
270 return m_storedCategorizedSorting
;
273 bool DolphinView::supportsCategorizedSorting() const
275 return m_iconsView
!= 0;
278 void DolphinView::selectAll()
280 QAbstractItemView
* view
= itemView();
281 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
282 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
283 // selection instead of selecting all items. This is bypassed for KDE 4.0
284 // by invoking clearSelection() first.
285 view
->clearSelection();
289 void DolphinView::invertSelection()
291 if (isColumnViewActive()) {
292 // QAbstractItemView does not offer a virtual method invertSelection()
293 // as counterpart to QAbstractItemView::selectAll(). This makes it
294 // necessary to delegate the inverting of the selection to the
295 // column view, as only the selection of the active column should
297 m_columnView
->invertSelection();
299 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
300 const QAbstractItemModel
* itemModel
= selectionModel
->model();
302 const QModelIndex topLeft
= itemModel
->index(0, 0);
303 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
304 itemModel
->columnCount() - 1);
306 const QItemSelection
selection(topLeft
, bottomRight
);
307 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
311 bool DolphinView::hasSelection() const
313 return itemView()->selectionModel()->hasSelection();
316 void DolphinView::clearSelection()
318 itemView()->selectionModel()->clear();
321 KFileItemList
DolphinView::selectedItems() const
323 if (isColumnViewActive()) {
324 return m_columnView
->selectedItems();
327 const QAbstractItemView
* view
= itemView();
329 // Our view has a selection, we will map them back to the DolphinModel
330 // and then fill the KFileItemList.
331 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
333 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
334 KFileItemList itemList
;
336 const QModelIndexList indexList
= selection
.indexes();
337 foreach (const QModelIndex
&index
, indexList
) {
338 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
339 if (!item
.isNull()) {
340 itemList
.append(item
);
347 KUrl::List
DolphinView::selectedUrls() const
350 const KFileItemList list
= selectedItems();
351 foreach (const KFileItem
&item
, list
) {
352 urls
.append(item
.url());
357 int DolphinView::selectedItemsCount() const
359 if (isColumnViewActive()) {
360 // TODO: get rid of this special case by adjusting the dir lister
361 // to the current column
362 return m_columnView
->selectedItems().count();
365 return itemView()->selectionModel()->selection().count();
368 void DolphinView::setContentsPosition(int x
, int y
)
370 QAbstractItemView
* view
= itemView();
372 // the ColumnView takes care itself for the horizontal scrolling
373 if (!isColumnViewActive()) {
374 view
->horizontalScrollBar()->setValue(x
);
376 view
->verticalScrollBar()->setValue(y
);
378 m_loadingDirectory
= false;
381 QPoint
DolphinView::contentsPosition() const
383 const int x
= itemView()->horizontalScrollBar()->value();
384 const int y
= itemView()->verticalScrollBar()->value();
388 void DolphinView::setZoomLevel(int level
)
390 if (level
< ZoomLevelInfo::minimumLevel()) {
391 level
= ZoomLevelInfo::minimumLevel();
392 } else if (level
> ZoomLevelInfo::maximumLevel()) {
393 level
= ZoomLevelInfo::maximumLevel();
396 if (level
!= zoomLevel()) {
397 m_controller
->setZoomLevel(level
);
398 m_previewGenerator
->updatePreviews();
399 emit
zoomLevelChanged(level
);
403 int DolphinView::zoomLevel() const
405 return m_controller
->zoomLevel();
408 void DolphinView::setSorting(Sorting sorting
)
410 if (sorting
!= this->sorting()) {
411 updateSorting(sorting
);
415 DolphinView::Sorting
DolphinView::sorting() const
417 return m_proxyModel
->sorting();
420 void DolphinView::setSortOrder(Qt::SortOrder order
)
422 if (sortOrder() != order
) {
423 updateSortOrder(order
);
427 Qt::SortOrder
DolphinView::sortOrder() const
429 return m_proxyModel
->sortOrder();
432 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
434 const KUrl viewPropsUrl
= viewPropertiesUrl();
435 ViewProperties
props(viewPropsUrl
);
436 props
.setAdditionalInfo(info
);
437 m_fileItemDelegate
->setShowInformation(info
);
439 emit
additionalInfoChanged();
441 if (itemView() != m_detailsView
) {
442 // the details view requires no reloading of the directory, as it maps
443 // the file item delegate info to its columns internally
444 loadDirectory(viewPropsUrl
);
448 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
450 return m_fileItemDelegate
->showInformation();
453 void DolphinView::reload()
456 loadDirectory(url(), true);
459 void DolphinView::refresh()
461 m_ignoreViewProperties
= false;
463 const bool oldActivationState
= m_active
;
464 const int oldZoomLevel
= m_controller
->zoomLevel();
468 applyViewProperties(m_controller
->url());
471 setActive(oldActivationState
);
472 updateZoomLevel(oldZoomLevel
);
475 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
477 if (m_controller
->url() == url
) {
481 m_previewGenerator
->cancelPreviews();
482 m_controller
->setUrl(url
); // emits urlChanged, which we forward
484 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
485 applyViewProperties(rootUrl
);
486 loadDirectory(rootUrl
);
487 if (itemView() == m_columnView
) {
488 m_columnView
->setRootUrl(rootUrl
);
489 m_columnView
->showColumn(url
);
492 applyViewProperties(url
);
496 emit
startedPathLoading(url
);
499 void DolphinView::setNameFilter(const QString
& nameFilter
)
501 m_proxyModel
->setFilterRegExp(nameFilter
);
503 if (isColumnViewActive()) {
504 // adjusting the directory lister is not enough in the case of the
505 // column view, as each column has its own directory lister internally...
506 m_columnView
->setNameFilter(nameFilter
);
510 void DolphinView::calculateItemCount(int& fileCount
,
512 KIO::filesize_t
& totalFileSize
) const
514 foreach (const KFileItem
& item
, m_dirLister
->items()) {
519 totalFileSize
+= item
.size();
524 QString
DolphinView::statusBarText() const
529 KIO::filesize_t totalFileSize
= 0;
531 if (hasSelection()) {
532 // give a summary of the status of the selected files
533 const KFileItemList list
= selectedItems();
534 if (list
.isEmpty()) {
535 // when an item is triggered, it is temporary selected but selectedItems()
536 // will return an empty list
540 KFileItemList::const_iterator it
= list
.begin();
541 const KFileItemList::const_iterator end
= list
.end();
543 const KFileItem
& item
= *it
;
548 totalFileSize
+= item
.size();
553 if (folderCount
+ fileCount
== 1) {
554 // if only one item is selected, show the filename
555 const QString name
= list
.first().name();
556 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
557 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
558 name
, KIO::convertSize(totalFileSize
));
560 // at least 2 items are selected
561 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
562 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
563 if ((folderCount
> 0) && (fileCount
> 0)) {
564 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
565 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
566 } else if (fileCount
> 0) {
567 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
569 Q_ASSERT(folderCount
> 0);
574 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
575 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
576 fileCount
, folderCount
,
577 totalFileSize
, true);
583 void DolphinView::setUrl(const KUrl
& url
)
585 // remember current item candidate (see restoreCurrentItem())
586 m_currentItemUrl
= url
;
587 updateView(url
, KUrl());
590 void DolphinView::changeSelection(const KFileItemList
& selection
)
593 if (selection
.isEmpty()) {
596 const KUrl
& baseUrl
= url();
598 QItemSelection new_selection
;
599 foreach(const KFileItem
& item
, selection
) {
600 url
= item
.url().upUrl();
601 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
602 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
603 new_selection
.select(index
, index
);
606 itemView()->selectionModel()->select(new_selection
,
607 QItemSelectionModel::ClearAndSelect
608 | QItemSelectionModel::Current
);
611 void DolphinView::renameSelectedItems()
613 KFileItemList items
= selectedItems();
614 const int itemCount
= items
.count();
620 // More than one item has been selected for renaming. Open
621 // a rename dialog and rename all items afterwards.
622 RenameDialog
dialog(this, items
);
623 if (dialog
.exec() == QDialog::Rejected
) {
627 const QString newName
= dialog
.newName();
628 if (newName
.isEmpty()) {
629 emit
errorMessage(dialog
.errorString());
631 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
632 // as one operation instead of n rename operations like it is done now...
633 Q_ASSERT(newName
.contains('#'));
635 // currently the items are sorted by the selection order, resort
636 // them by the file name
637 qSort(items
.begin(), items
.end(), lessThan
);
639 // iterate through all selected items and rename them...
641 foreach (const KFileItem
& item
, items
) {
642 const KUrl
& oldUrl
= item
.url();
644 number
.setNum(index
++);
646 QString name
= newName
;
647 name
.replace('#', number
);
649 if (oldUrl
.fileName() != name
) {
650 KUrl newUrl
= oldUrl
;
651 newUrl
.setFileName(name
);
652 KonqOperations::rename(this, oldUrl
, newUrl
);
656 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
657 Q_ASSERT(itemCount
== 1);
659 if (isColumnViewActive()) {
660 m_columnView
->editItem(items
.first());
662 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
663 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
664 itemView()->edit(proxyIndex
);
667 Q_ASSERT(itemCount
== 1);
669 RenameDialog
dialog(this, items
);
670 if (dialog
.exec() == QDialog::Rejected
) {
674 const QString
& newName
= dialog
.newName();
675 if (newName
.isEmpty()) {
676 emit
errorMessage(dialog
.errorString());
678 const KUrl
& oldUrl
= items
.first().url();
679 KUrl newUrl
= oldUrl
;
680 newUrl
.setFileName(newName
);
681 KonqOperations::rename(this, oldUrl
, newUrl
);
686 void DolphinView::trashSelectedItems()
688 const KUrl::List list
= simplifiedSelectedUrls();
689 KonqOperations::del(this, KonqOperations::TRASH
, list
);
692 void DolphinView::deleteSelectedItems()
694 const KUrl::List list
= simplifiedSelectedUrls();
695 const bool del
= KonqOperations::askDeleteConfirmation(list
,
697 KonqOperations::DEFAULT_CONFIRMATION
,
701 KIO::Job
* job
= KIO::del(list
);
702 connect(job
, SIGNAL(result(KJob
*)),
703 this, SLOT(slotDeleteFileFinished(KJob
*)));
707 void DolphinView::cutSelectedItems()
709 QMimeData
* mimeData
= selectionMimeData();
710 KonqMimeData::addIsCutSelection(mimeData
, true);
711 QApplication::clipboard()->setMimeData(mimeData
);
714 void DolphinView::copySelectedItems()
716 QMimeData
* mimeData
= selectionMimeData();
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 kDebug() << "m_expandedDragSource = " << m_expandedDragSource
;
853 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
854 // Listening to a mousebutton press event to delete expanded views is a
855 // workaround, as it seems impossible for the FolderExpander to know when
856 // a dragging outside a view has been finished. However it works quite well:
857 // A mousebutton press event indicates that a drag operation must be
859 kDebug() << "Deleted view " << m_expandedDragSource
;
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()) && (m_toolTipManager
!= 0)) {
873 m_toolTipManager
->hideTip();
881 return QWidget::eventFilter(watched
, event
);
884 void DolphinView::activate()
889 void DolphinView::triggerItem(const KFileItem
& item
)
891 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
892 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
893 // items are selected by the user, hence don't trigger the
894 // item specified by 'index'
898 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
899 if (item
.isNull() || m_isContextMenuOpen
) {
903 if (m_toolTipManager
!= 0) {
904 m_toolTipManager
->hideTip();
906 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
909 void DolphinView::emitSelectionChangedSignal()
911 emit
selectionChanged(DolphinView::selectedItems());
914 void DolphinView::openContextMenu(const QPoint
& pos
)
917 if (isColumnViewActive()) {
918 item
= m_columnView
->itemAt(pos
);
920 const QModelIndex index
= itemView()->indexAt(pos
);
921 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
922 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
923 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
927 if (m_toolTipManager
!= 0) {
928 m_toolTipManager
->hideTip();
931 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
932 emit
requestContextMenu(item
, url());
933 m_isContextMenuOpen
= false;
936 void DolphinView::dropUrls(const KFileItem
& destItem
,
937 const KUrl
& destPath
,
940 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
943 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
945 ViewProperties
props(viewPropertiesUrl());
946 props
.setSorting(sorting
);
948 m_proxyModel
->setSorting(sorting
);
950 emit
sortingChanged(sorting
);
953 void DolphinView::updateSortOrder(Qt::SortOrder order
)
955 ViewProperties
props(viewPropertiesUrl());
956 props
.setSortOrder(order
);
958 m_proxyModel
->setSortOrder(order
);
960 emit
sortOrderChanged(order
);
963 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
965 ViewProperties
props(viewPropertiesUrl());
966 props
.setAdditionalInfo(info
);
969 m_fileItemDelegate
->setShowInformation(info
);
971 emit
additionalInfoChanged();
974 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
976 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
977 (m_mode
== DolphinView::IconsView
);
979 QAction
* showSizeInfo
= collection
->action("show_size_info");
980 QAction
* showDateInfo
= collection
->action("show_date_info");
981 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
982 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
983 QAction
* showGroupInfo
= collection
->action("show_group_info");
984 QAction
* showMimeInfo
= collection
->action("show_mime_info");
986 showSizeInfo
->setChecked(false);
987 showDateInfo
->setChecked(false);
988 showPermissionsInfo
->setChecked(false);
989 showOwnerInfo
->setChecked(false);
990 showGroupInfo
->setChecked(false);
991 showMimeInfo
->setChecked(false);
993 showSizeInfo
->setEnabled(enable
);
994 showDateInfo
->setEnabled(enable
);
995 showPermissionsInfo
->setEnabled(enable
);
996 showOwnerInfo
->setEnabled(enable
);
997 showGroupInfo
->setEnabled(enable
);
998 showMimeInfo
->setEnabled(enable
);
1000 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1002 case KFileItemDelegate::Size
:
1003 showSizeInfo
->setChecked(true);
1005 case KFileItemDelegate::ModificationTime
:
1006 showDateInfo
->setChecked(true);
1008 case KFileItemDelegate::Permissions
:
1009 showPermissionsInfo
->setChecked(true);
1011 case KFileItemDelegate::Owner
:
1012 showOwnerInfo
->setChecked(true);
1014 case KFileItemDelegate::OwnerAndGroup
:
1015 showGroupInfo
->setChecked(true);
1017 case KFileItemDelegate::FriendlyMimeType
:
1018 showMimeInfo
->setChecked(true);
1026 QPair
<bool, QString
> DolphinView::pasteInfo() const
1028 QPair
<bool, QString
> ret
;
1029 QClipboard
* clipboard
= QApplication::clipboard();
1030 const QMimeData
* mimeData
= clipboard
->mimeData();
1032 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1033 if (!urls
.isEmpty()) {
1034 // disable the paste action if no writing is supported
1035 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1036 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1038 if (urls
.count() == 1) {
1039 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1040 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1041 i18nc("@action:inmenu", "Paste One File");
1044 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1048 ret
.second
= i18nc("@action:inmenu", "Paste");
1054 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1056 m_tabsForFiles
= tabsForFiles
;
1059 bool DolphinView::isTabsForFilesEnabled() const
1061 return m_tabsForFiles
;
1064 bool DolphinView::itemsExpandable() const
1066 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1069 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1074 if (DragAndDropHelper::instance().isDragSource(view
)) {
1075 kDebug() << "Is current drag source";
1076 // We must store for later deletion.
1077 if (m_expandedDragSource
!= 0) {
1078 // The old stored view is obviously not the drag source anymore.
1079 kDebug() << "Deleted old view " << m_expandedDragSource
;
1080 m_expandedDragSource
->deleteLater();
1081 m_expandedDragSource
= 0;
1084 m_expandedDragSource
= view
;
1087 kDebug() << "Deleted new view " << view
;
1088 view
->deleteLater();
1092 void DolphinView::emitContentsMoved()
1094 // only emit the contents moved signal if:
1095 // - no directory loading is ongoing (this would reset the contents position
1096 // always to (0, 0))
1097 // - if the Column View is active: the column view does an automatic
1098 // positioning during the loading operation, which must be remembered
1099 if (!m_loadingDirectory
|| isColumnViewActive()) {
1100 const QPoint
pos(contentsPosition());
1101 emit
contentsMoved(pos
.x(), pos
.y());
1105 void DolphinView::showHoverInformation(const KFileItem
& item
)
1107 emit
requestItemInfo(item
);
1110 void DolphinView::clearHoverInformation()
1112 emit
requestItemInfo(KFileItem());
1115 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1117 if (job
->error() == 0) {
1118 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1119 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1120 emit
errorMessage(job
->errorString());
1124 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1126 emit
requestUrlChange(url
);
1127 m_controller
->setUrl(url
);
1130 void DolphinView::restoreCurrentItem()
1132 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1133 if (dirIndex
.isValid()) {
1134 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1135 QAbstractItemView
* view
= itemView();
1136 const bool clearSelection
= !hasSelection();
1137 view
->setCurrentIndex(proxyIndex
);
1138 if (clearSelection
) {
1139 view
->clearSelection();
1144 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1146 if (!url
.isValid()) {
1147 const QString
location(url
.pathOrUrl());
1148 if (location
.isEmpty()) {
1149 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1151 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1156 m_loadingDirectory
= true;
1158 m_dirLister
->stop();
1159 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1161 if (isColumnViewActive()) {
1162 // adjusting the directory lister is not enough in the case of the
1163 // column view, as each column has its own directory lister internally...
1165 m_columnView
->reload();
1167 m_columnView
->showColumn(url
);
1172 KUrl
DolphinView::viewPropertiesUrl() const
1174 if (isColumnViewActive()) {
1175 return m_columnView
->rootUrl();
1181 void DolphinView::applyViewProperties(const KUrl
& url
)
1183 if (m_ignoreViewProperties
) {
1187 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1188 // The column view is active, hence don't apply the view properties
1189 // of sub directories (represented by columns) to the view. The
1190 // view always represents the properties of the first column.
1194 const ViewProperties
props(url
);
1196 const Mode mode
= props
.viewMode();
1197 if (m_mode
!= mode
) {
1198 const int oldZoomLevel
= m_controller
->zoomLevel();
1204 updateZoomLevel(oldZoomLevel
);
1206 if (itemView() == 0) {
1209 Q_ASSERT(itemView() != 0);
1210 Q_ASSERT(m_fileItemDelegate
!= 0);
1212 const bool showHiddenFiles
= props
.showHiddenFiles();
1213 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1214 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1215 emit
showHiddenFilesChanged();
1218 m_storedCategorizedSorting
= props
.categorizedSorting();
1219 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1220 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1221 m_proxyModel
->setCategorizedModel(categorized
);
1222 emit
categorizedSortingChanged();
1225 const DolphinView::Sorting sorting
= props
.sorting();
1226 if (sorting
!= m_proxyModel
->sorting()) {
1227 m_proxyModel
->setSorting(sorting
);
1228 emit
sortingChanged(sorting
);
1231 const Qt::SortOrder sortOrder
= props
.sortOrder();
1232 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1233 m_proxyModel
->setSortOrder(sortOrder
);
1234 emit
sortOrderChanged(sortOrder
);
1237 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1238 if (info
!= m_fileItemDelegate
->showInformation()) {
1239 m_fileItemDelegate
->setShowInformation(info
);
1240 emit
additionalInfoChanged();
1243 const bool showPreview
= props
.showPreview();
1244 if (showPreview
!= m_showPreview
) {
1245 m_showPreview
= showPreview
;
1246 m_previewGenerator
->setPreviewShown(showPreview
);
1248 const int oldZoomLevel
= m_controller
->zoomLevel();
1249 emit
showPreviewChanged();
1251 // Enabling or disabling the preview might change the icon size of the view.
1252 // As the view does not emit a signal when the icon size has been changed,
1253 // the used zoom level of the controller must be adjusted manually:
1254 updateZoomLevel(oldZoomLevel
);
1257 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1258 // During the lifetime of a DolphinView instance the global view properties
1259 // should not be changed. This allows e. g. to split a view and use different
1260 // view properties for each view.
1261 m_ignoreViewProperties
= true;
1265 void DolphinView::createView()
1268 Q_ASSERT(m_iconsView
== 0);
1269 Q_ASSERT(m_detailsView
== 0);
1270 Q_ASSERT(m_columnView
== 0);
1272 QAbstractItemView
* view
= 0;
1275 m_iconsView
= new DolphinIconsView(this, m_controller
);
1281 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1282 view
= m_detailsView
;
1286 m_columnView
= new DolphinColumnView(this, m_controller
);
1287 view
= m_columnView
;
1291 Q_ASSERT(view
!= 0);
1292 view
->installEventFilter(this);
1293 view
->viewport()->installEventFilter(this);
1294 setFocusProxy(view
);
1296 if (m_mode
!= ColumnView
) {
1297 // Give the view the ability to auto-expand its directories on hovering
1298 // (the column view takes care about this itself). If the details view
1299 // uses expandable folders, the auto-expanding should be used always.
1300 DolphinSettings
& settings
= DolphinSettings::instance();
1301 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1302 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1304 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1305 folderExpander
->setEnabled(enabled
);
1306 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1307 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1310 // Listen out for requests to delete the current column.
1311 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1312 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1315 m_controller
->setItemView(view
);
1317 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1318 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1319 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1320 view
->setItemDelegate(m_fileItemDelegate
);
1322 view
->setModel(m_proxyModel
);
1323 if (m_selectionModel
!= 0) {
1324 view
->setSelectionModel(m_selectionModel
);
1326 m_selectionModel
= view
->selectionModel();
1329 // reparent the selection model, as it should not be deleted
1330 // when deleting the model
1331 m_selectionModel
->setParent(this);
1333 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1335 m_previewGenerator
= new KFilePreviewGenerator(view
);
1336 m_previewGenerator
->setPreviewShown(m_showPreview
);
1338 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1339 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1340 connect(m_controller
, SIGNAL(hideToolTip()),
1341 m_toolTipManager
, SLOT(hideTip()));
1344 m_topLayout
->insertWidget(1, view
);
1346 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1347 this, SLOT(emitSelectionChangedSignal()));
1348 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1349 this, SLOT(emitContentsMoved()));
1350 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1351 this, SLOT(emitContentsMoved()));
1354 void DolphinView::deleteView()
1356 QAbstractItemView
* view
= itemView();
1358 // It's important to set the keyboard focus to the parent
1359 // before deleting the view: Otherwise when having a split
1360 // view the other view will get the focus and will request
1361 // an activation (see DolphinView::eventFilter()).
1365 m_topLayout
->removeWidget(view
);
1368 // m_previewGenerator's parent is not always destroyed, and we
1369 // don't want two active at once - manually delete.
1370 delete m_previewGenerator
;
1371 m_previewGenerator
= 0;
1374 m_controller
->disconnect(view
);
1377 deleteWhenNotDragSource(view
);
1383 m_fileItemDelegate
= 0;
1384 m_toolTipManager
= 0;
1388 QAbstractItemView
* DolphinView::itemView() const
1390 if (m_detailsView
!= 0) {
1391 return m_detailsView
;
1392 } else if (m_columnView
!= 0) {
1393 return m_columnView
;
1399 bool DolphinView::isCutItem(const KFileItem
& item
) const
1401 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1402 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1404 const KUrl
& itemUrl
= item
.url();
1405 KUrl::List::const_iterator it
= cutUrls
.begin();
1406 const KUrl::List::const_iterator end
= cutUrls
.end();
1408 if (*it
== itemUrl
) {
1417 void DolphinView::pasteToUrl(const KUrl
& url
)
1419 QClipboard
* clipboard
= QApplication::clipboard();
1420 const QMimeData
* mimeData
= clipboard
->mimeData();
1422 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1423 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1424 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1427 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1431 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1433 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1434 if (oldZoomLevel
!= newZoomLevel
) {
1435 m_controller
->setZoomLevel(newZoomLevel
);
1436 emit
zoomLevelChanged(newZoomLevel
);
1440 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1442 KUrl::List list
= selectedUrls();
1443 if (itemsExpandable() ) {
1444 list
= KDirModel::simplifiedUrlList(list
);
1449 QMimeData
* DolphinView::selectionMimeData() const
1451 if (isColumnViewActive()) {
1452 return m_columnView
->selectionMimeData();
1455 const QAbstractItemView
* view
= itemView();
1456 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1457 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1458 return m_dolphinModel
->mimeData(selection
.indexes());
1461 #include "dolphinview.moc"