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 itemView()->selectionModel()->clear();
320 KFileItemList
DolphinView::selectedItems() const
322 if (isColumnViewActive()) {
323 return m_columnView
->selectedItems();
326 const QAbstractItemView
* view
= itemView();
328 // Our view has a selection, we will map them back to the DolphinModel
329 // and then fill the KFileItemList.
330 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
332 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
333 KFileItemList itemList
;
335 const QModelIndexList indexList
= selection
.indexes();
336 foreach (const QModelIndex
&index
, indexList
) {
337 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
338 if (!item
.isNull()) {
339 itemList
.append(item
);
346 KUrl::List
DolphinView::selectedUrls() const
349 const KFileItemList list
= selectedItems();
350 foreach (const KFileItem
&item
, list
) {
351 urls
.append(item
.url());
356 int DolphinView::selectedItemsCount() const
358 if (isColumnViewActive()) {
359 // TODO: get rid of this special case by adjusting the dir lister
360 // to the current column
361 return m_columnView
->selectedItems().count();
364 return itemView()->selectionModel()->selection().count();
367 void DolphinView::setContentsPosition(int x
, int y
)
369 QAbstractItemView
* view
= itemView();
371 // the ColumnView takes care itself for the horizontal scrolling
372 if (!isColumnViewActive()) {
373 view
->horizontalScrollBar()->setValue(x
);
375 view
->verticalScrollBar()->setValue(y
);
377 m_loadingDirectory
= false;
380 QPoint
DolphinView::contentsPosition() const
382 const int x
= itemView()->horizontalScrollBar()->value();
383 const int y
= itemView()->verticalScrollBar()->value();
387 void DolphinView::setZoomLevel(int level
)
389 if (level
< ZoomLevelInfo::minimumLevel()) {
390 level
= ZoomLevelInfo::minimumLevel();
391 } else if (level
> ZoomLevelInfo::maximumLevel()) {
392 level
= ZoomLevelInfo::maximumLevel();
395 if (level
!= zoomLevel()) {
396 m_controller
->setZoomLevel(level
);
397 m_previewGenerator
->updatePreviews();
398 emit
zoomLevelChanged(level
);
402 int DolphinView::zoomLevel() const
404 return m_controller
->zoomLevel();
407 void DolphinView::setSorting(Sorting sorting
)
409 if (sorting
!= this->sorting()) {
410 updateSorting(sorting
);
414 DolphinView::Sorting
DolphinView::sorting() const
416 return m_proxyModel
->sorting();
419 void DolphinView::setSortOrder(Qt::SortOrder order
)
421 if (sortOrder() != order
) {
422 updateSortOrder(order
);
426 Qt::SortOrder
DolphinView::sortOrder() const
428 return m_proxyModel
->sortOrder();
431 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
433 const KUrl viewPropsUrl
= viewPropertiesUrl();
434 ViewProperties
props(viewPropsUrl
);
435 props
.setAdditionalInfo(info
);
436 m_fileItemDelegate
->setShowInformation(info
);
438 emit
additionalInfoChanged();
440 if (itemView() != m_detailsView
) {
441 // the details view requires no reloading of the directory, as it maps
442 // the file item delegate info to its columns internally
443 loadDirectory(viewPropsUrl
);
447 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
449 return m_fileItemDelegate
->showInformation();
452 void DolphinView::reload()
455 loadDirectory(url(), true);
458 void DolphinView::refresh()
460 m_ignoreViewProperties
= false;
462 const bool oldActivationState
= m_active
;
463 const int oldZoomLevel
= m_controller
->zoomLevel();
467 applyViewProperties(m_controller
->url());
470 setActive(oldActivationState
);
471 updateZoomLevel(oldZoomLevel
);
474 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
476 if (m_controller
->url() == url
) {
480 m_previewGenerator
->cancelPreviews();
481 m_controller
->setUrl(url
); // emits urlChanged, which we forward
483 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
484 applyViewProperties(rootUrl
);
485 loadDirectory(rootUrl
);
486 if (itemView() == m_columnView
) {
487 m_columnView
->setRootUrl(rootUrl
);
488 m_columnView
->showColumn(url
);
491 applyViewProperties(url
);
495 emit
startedPathLoading(url
);
498 void DolphinView::setNameFilter(const QString
& nameFilter
)
500 m_proxyModel
->setFilterRegExp(nameFilter
);
502 if (isColumnViewActive()) {
503 // adjusting the directory lister is not enough in the case of the
504 // column view, as each column has its own directory lister internally...
505 m_columnView
->setNameFilter(nameFilter
);
509 void DolphinView::calculateItemCount(int& fileCount
,
511 KIO::filesize_t
& totalFileSize
) const
513 foreach (const KFileItem
& item
, m_dirLister
->items()) {
518 totalFileSize
+= item
.size();
523 QString
DolphinView::statusBarText() const
528 KIO::filesize_t totalFileSize
= 0;
530 if (hasSelection()) {
531 // give a summary of the status of the selected files
532 const KFileItemList list
= selectedItems();
533 if (list
.isEmpty()) {
534 // when an item is triggered, it is temporary selected but selectedItems()
535 // will return an empty list
539 KFileItemList::const_iterator it
= list
.begin();
540 const KFileItemList::const_iterator end
= list
.end();
542 const KFileItem
& item
= *it
;
547 totalFileSize
+= item
.size();
552 if (folderCount
+ fileCount
== 1) {
553 // if only one item is selected, show the filename
554 const QString name
= list
.first().name();
555 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
556 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
557 name
, KIO::convertSize(totalFileSize
));
559 // at least 2 items are selected
560 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
561 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
562 if ((folderCount
> 0) && (fileCount
> 0)) {
563 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
564 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
565 } else if (fileCount
> 0) {
566 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
568 Q_ASSERT(folderCount
> 0);
573 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
574 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
575 fileCount
, folderCount
,
576 totalFileSize
, true);
582 void DolphinView::setUrl(const KUrl
& url
)
584 // remember current item candidate (see restoreCurrentItem())
585 m_currentItemUrl
= url
;
586 updateView(url
, KUrl());
589 void DolphinView::changeSelection(const KFileItemList
& selection
)
592 if (selection
.isEmpty()) {
595 const KUrl
& baseUrl
= url();
597 QItemSelection new_selection
;
598 foreach(const KFileItem
& item
, selection
) {
599 url
= item
.url().upUrl();
600 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
601 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
602 new_selection
.select(index
, index
);
605 itemView()->selectionModel()->select(new_selection
,
606 QItemSelectionModel::ClearAndSelect
607 | QItemSelectionModel::Current
);
610 void DolphinView::renameSelectedItems()
612 KFileItemList items
= selectedItems();
613 const int itemCount
= items
.count();
619 // More than one item has been selected for renaming. Open
620 // a rename dialog and rename all items afterwards.
621 RenameDialog
dialog(this, items
);
622 if (dialog
.exec() == QDialog::Rejected
) {
626 const QString newName
= dialog
.newName();
627 if (newName
.isEmpty()) {
628 emit
errorMessage(dialog
.errorString());
630 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
631 // as one operation instead of n rename operations like it is done now...
632 Q_ASSERT(newName
.contains('#'));
634 // currently the items are sorted by the selection order, resort
635 // them by the file name
636 qSort(items
.begin(), items
.end(), lessThan
);
638 // iterate through all selected items and rename them...
640 foreach (const KFileItem
& item
, items
) {
641 const KUrl
& oldUrl
= item
.url();
643 number
.setNum(index
++);
645 QString name
= newName
;
646 name
.replace('#', number
);
648 if (oldUrl
.fileName() != name
) {
649 KUrl newUrl
= oldUrl
;
650 newUrl
.setFileName(name
);
651 KonqOperations::rename(this, oldUrl
, newUrl
);
655 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
656 Q_ASSERT(itemCount
== 1);
658 if (isColumnViewActive()) {
659 m_columnView
->editItem(items
.first());
661 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
662 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
663 itemView()->edit(proxyIndex
);
666 Q_ASSERT(itemCount
== 1);
668 RenameDialog
dialog(this, items
);
669 if (dialog
.exec() == QDialog::Rejected
) {
673 const QString
& newName
= dialog
.newName();
674 if (newName
.isEmpty()) {
675 emit
errorMessage(dialog
.errorString());
677 const KUrl
& oldUrl
= items
.first().url();
678 KUrl newUrl
= oldUrl
;
679 newUrl
.setFileName(newName
);
680 KonqOperations::rename(this, oldUrl
, newUrl
);
685 void DolphinView::trashSelectedItems()
687 const KUrl::List list
= simplifiedSelectedUrls();
688 KonqOperations::del(this, KonqOperations::TRASH
, list
);
691 void DolphinView::deleteSelectedItems()
693 const KUrl::List list
= simplifiedSelectedUrls();
694 const bool del
= KonqOperations::askDeleteConfirmation(list
,
696 KonqOperations::DEFAULT_CONFIRMATION
,
700 KIO::Job
* job
= KIO::del(list
);
701 connect(job
, SIGNAL(result(KJob
*)),
702 this, SLOT(slotDeleteFileFinished(KJob
*)));
706 void DolphinView::cutSelectedItems()
708 QMimeData
* mimeData
= selectionMimeData();
709 KonqMimeData::addIsCutSelection(mimeData
, true);
710 QApplication::clipboard()->setMimeData(mimeData
);
713 void DolphinView::copySelectedItems()
715 QMimeData
* mimeData
= selectionMimeData();
716 QApplication::clipboard()->setMimeData(mimeData
);
719 void DolphinView::paste()
724 void DolphinView::pasteIntoFolder()
726 const KFileItemList items
= selectedItems();
727 if ((items
.count() == 1) && items
.first().isDir()) {
728 pasteToUrl(items
.first().url());
732 void DolphinView::setShowPreview(bool show
)
734 if (m_showPreview
== show
) {
738 const KUrl viewPropsUrl
= viewPropertiesUrl();
739 ViewProperties
props(viewPropsUrl
);
740 props
.setShowPreview(show
);
742 m_showPreview
= show
;
743 m_previewGenerator
->setPreviewShown(show
);
745 const int oldZoomLevel
= m_controller
->zoomLevel();
746 emit
showPreviewChanged();
748 // Enabling or disabling the preview might change the icon size of the view.
749 // As the view does not emit a signal when the icon size has been changed,
750 // the used zoom level of the controller must be adjusted manually:
751 updateZoomLevel(oldZoomLevel
);
753 loadDirectory(viewPropsUrl
);
756 void DolphinView::setShowHiddenFiles(bool show
)
758 if (m_dirLister
->showingDotFiles() == show
) {
762 const KUrl viewPropsUrl
= viewPropertiesUrl();
763 ViewProperties
props(viewPropsUrl
);
764 props
.setShowHiddenFiles(show
);
766 m_dirLister
->setShowingDotFiles(show
);
767 emit
showHiddenFilesChanged();
769 loadDirectory(viewPropsUrl
);
772 void DolphinView::setCategorizedSorting(bool categorized
)
774 if (categorized
== categorizedSorting()) {
778 // setCategorizedSorting(true) may only get invoked
779 // if the view supports categorized sorting
780 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
782 ViewProperties
props(viewPropertiesUrl());
783 props
.setCategorizedSorting(categorized
);
786 m_storedCategorizedSorting
= categorized
;
787 m_proxyModel
->setCategorizedModel(categorized
);
789 emit
categorizedSortingChanged();
792 void DolphinView::toggleSortOrder()
794 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
795 Qt::DescendingOrder
:
800 void DolphinView::toggleAdditionalInfo(QAction
* action
)
802 const KFileItemDelegate::Information info
=
803 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
805 KFileItemDelegate::InformationList list
= additionalInfo();
807 const bool show
= action
->isChecked();
809 const int index
= list
.indexOf(info
);
810 const bool containsInfo
= (index
>= 0);
811 if (show
&& !containsInfo
) {
813 setAdditionalInfo(list
);
814 } else if (!show
&& containsInfo
) {
815 list
.removeAt(index
);
816 setAdditionalInfo(list
);
817 Q_ASSERT(list
.indexOf(info
) < 0);
821 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
823 QWidget::mouseReleaseEvent(event
);
827 void DolphinView::wheelEvent(QWheelEvent
* event
)
829 if (event
->modifiers() & Qt::ControlModifier
) {
830 const int delta
= event
->delta();
831 const int level
= zoomLevel();
833 setZoomLevel(level
+ 1);
834 } else if (delta
< 0) {
835 setZoomLevel(level
- 1);
841 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
843 switch (event
->type()) {
844 case QEvent::FocusIn
:
845 if (watched
== itemView()) {
846 m_controller
->requestActivation();
850 case QEvent::MouseButtonPress
:
851 if ((watched
== itemView()->viewport()) && (m_expandedDragSource
!= 0)) {
852 // Listening to a mousebutton press event to delete expanded views is a
853 // workaround, as it seems impossible for the FolderExpander to know when
854 // a dragging outside a view has been finished. However it works quite well:
855 // A mousebutton press event indicates that a drag operation must be
857 m_expandedDragSource
->deleteLater();
858 m_expandedDragSource
= 0;
862 case QEvent::DragEnter
:
863 if (watched
== itemView()->viewport()) {
868 case QEvent::KeyPress
:
869 if ((watched
== itemView()) && (m_toolTipManager
!= 0)) {
870 m_toolTipManager
->hideTip();
878 return QWidget::eventFilter(watched
, event
);
881 void DolphinView::activate()
886 void DolphinView::triggerItem(const KFileItem
& item
)
888 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
889 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
890 // items are selected by the user, hence don't trigger the
891 // item specified by 'index'
895 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
896 if (item
.isNull() || m_isContextMenuOpen
) {
900 if (m_toolTipManager
!= 0) {
901 m_toolTipManager
->hideTip();
903 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
906 void DolphinView::emitSelectionChangedSignal()
908 emit
selectionChanged(DolphinView::selectedItems());
911 void DolphinView::openContextMenu(const QPoint
& pos
,
912 const QList
<QAction
*>& customActions
)
915 if (isColumnViewActive()) {
916 item
= m_columnView
->itemAt(pos
);
918 const QModelIndex index
= itemView()->indexAt(pos
);
919 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
920 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
921 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
925 if (m_toolTipManager
!= 0) {
926 m_toolTipManager
->hideTip();
929 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
930 emit
requestContextMenu(item
, url(), customActions
);
931 m_isContextMenuOpen
= false;
934 void DolphinView::dropUrls(const KFileItem
& destItem
,
935 const KUrl
& destPath
,
938 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
941 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
943 ViewProperties
props(viewPropertiesUrl());
944 props
.setSorting(sorting
);
946 m_proxyModel
->setSorting(sorting
);
948 emit
sortingChanged(sorting
);
951 void DolphinView::updateSortOrder(Qt::SortOrder order
)
953 ViewProperties
props(viewPropertiesUrl());
954 props
.setSortOrder(order
);
956 m_proxyModel
->setSortOrder(order
);
958 emit
sortOrderChanged(order
);
961 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
963 ViewProperties
props(viewPropertiesUrl());
964 props
.setAdditionalInfo(info
);
967 m_fileItemDelegate
->setShowInformation(info
);
969 emit
additionalInfoChanged();
972 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
974 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
975 (m_mode
== DolphinView::IconsView
);
977 QAction
* showSizeInfo
= collection
->action("show_size_info");
978 QAction
* showDateInfo
= collection
->action("show_date_info");
979 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
980 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
981 QAction
* showGroupInfo
= collection
->action("show_group_info");
982 QAction
* showMimeInfo
= collection
->action("show_mime_info");
984 showSizeInfo
->setChecked(false);
985 showDateInfo
->setChecked(false);
986 showPermissionsInfo
->setChecked(false);
987 showOwnerInfo
->setChecked(false);
988 showGroupInfo
->setChecked(false);
989 showMimeInfo
->setChecked(false);
991 showSizeInfo
->setEnabled(enable
);
992 showDateInfo
->setEnabled(enable
);
993 showPermissionsInfo
->setEnabled(enable
);
994 showOwnerInfo
->setEnabled(enable
);
995 showGroupInfo
->setEnabled(enable
);
996 showMimeInfo
->setEnabled(enable
);
998 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
1000 case KFileItemDelegate::Size
:
1001 showSizeInfo
->setChecked(true);
1003 case KFileItemDelegate::ModificationTime
:
1004 showDateInfo
->setChecked(true);
1006 case KFileItemDelegate::Permissions
:
1007 showPermissionsInfo
->setChecked(true);
1009 case KFileItemDelegate::Owner
:
1010 showOwnerInfo
->setChecked(true);
1012 case KFileItemDelegate::OwnerAndGroup
:
1013 showGroupInfo
->setChecked(true);
1015 case KFileItemDelegate::FriendlyMimeType
:
1016 showMimeInfo
->setChecked(true);
1024 QPair
<bool, QString
> DolphinView::pasteInfo() const
1026 QPair
<bool, QString
> ret
;
1027 QClipboard
* clipboard
= QApplication::clipboard();
1028 const QMimeData
* mimeData
= clipboard
->mimeData();
1030 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1031 if (!urls
.isEmpty()) {
1032 // disable the paste action if no writing is supported
1033 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1034 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1036 if (urls
.count() == 1) {
1037 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1038 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1039 i18nc("@action:inmenu", "Paste One File");
1042 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1046 ret
.second
= i18nc("@action:inmenu", "Paste");
1052 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1054 m_tabsForFiles
= tabsForFiles
;
1057 bool DolphinView::isTabsForFilesEnabled() const
1059 return m_tabsForFiles
;
1062 bool DolphinView::itemsExpandable() const
1064 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1067 void DolphinView::deleteWhenNotDragSource(QAbstractItemView
*view
)
1072 if (DragAndDropHelper::instance().isDragSource(view
)) {
1073 // We must store for later deletion.
1074 if (m_expandedDragSource
!= 0) {
1075 // The old stored view is obviously not the drag source anymore.
1076 m_expandedDragSource
->deleteLater();
1077 m_expandedDragSource
= 0;
1080 m_expandedDragSource
= view
;
1083 view
->deleteLater();
1087 void DolphinView::emitContentsMoved()
1089 // only emit the contents moved signal if:
1090 // - no directory loading is ongoing (this would reset the contents position
1091 // always to (0, 0))
1092 // - if the Column View is active: the column view does an automatic
1093 // positioning during the loading operation, which must be remembered
1094 if (!m_loadingDirectory
|| isColumnViewActive()) {
1095 const QPoint
pos(contentsPosition());
1096 emit
contentsMoved(pos
.x(), pos
.y());
1100 void DolphinView::showHoverInformation(const KFileItem
& item
)
1102 emit
requestItemInfo(item
);
1105 void DolphinView::clearHoverInformation()
1107 emit
requestItemInfo(KFileItem());
1110 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1112 if (job
->error() == 0) {
1113 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1114 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1115 emit
errorMessage(job
->errorString());
1119 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1121 emit
requestUrlChange(url
);
1122 m_controller
->setUrl(url
);
1125 void DolphinView::restoreCurrentItem()
1127 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1128 if (dirIndex
.isValid()) {
1129 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1130 QAbstractItemView
* view
= itemView();
1131 const bool clearSelection
= !hasSelection();
1132 view
->setCurrentIndex(proxyIndex
);
1133 if (clearSelection
) {
1134 view
->clearSelection();
1139 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1141 if (!url
.isValid()) {
1142 const QString
location(url
.pathOrUrl());
1143 if (location
.isEmpty()) {
1144 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1146 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1151 m_loadingDirectory
= true;
1153 m_dirLister
->stop();
1154 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1156 if (isColumnViewActive()) {
1157 // adjusting the directory lister is not enough in the case of the
1158 // column view, as each column has its own directory lister internally...
1160 m_columnView
->reload();
1162 m_columnView
->showColumn(url
);
1167 KUrl
DolphinView::viewPropertiesUrl() const
1169 if (isColumnViewActive()) {
1170 return m_columnView
->rootUrl();
1176 void DolphinView::applyViewProperties(const KUrl
& url
)
1178 if (m_ignoreViewProperties
) {
1182 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1183 // The column view is active, hence don't apply the view properties
1184 // of sub directories (represented by columns) to the view. The
1185 // view always represents the properties of the first column.
1189 const ViewProperties
props(url
);
1191 const Mode mode
= props
.viewMode();
1192 if (m_mode
!= mode
) {
1193 const int oldZoomLevel
= m_controller
->zoomLevel();
1199 updateZoomLevel(oldZoomLevel
);
1201 if (itemView() == 0) {
1204 Q_ASSERT(itemView() != 0);
1205 Q_ASSERT(m_fileItemDelegate
!= 0);
1207 const bool showHiddenFiles
= props
.showHiddenFiles();
1208 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1209 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1210 emit
showHiddenFilesChanged();
1213 m_storedCategorizedSorting
= props
.categorizedSorting();
1214 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1215 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1216 m_proxyModel
->setCategorizedModel(categorized
);
1217 emit
categorizedSortingChanged();
1220 const DolphinView::Sorting sorting
= props
.sorting();
1221 if (sorting
!= m_proxyModel
->sorting()) {
1222 m_proxyModel
->setSorting(sorting
);
1223 emit
sortingChanged(sorting
);
1226 const Qt::SortOrder sortOrder
= props
.sortOrder();
1227 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1228 m_proxyModel
->setSortOrder(sortOrder
);
1229 emit
sortOrderChanged(sortOrder
);
1232 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1233 if (info
!= m_fileItemDelegate
->showInformation()) {
1234 m_fileItemDelegate
->setShowInformation(info
);
1235 emit
additionalInfoChanged();
1238 const bool showPreview
= props
.showPreview();
1239 if (showPreview
!= m_showPreview
) {
1240 m_showPreview
= showPreview
;
1241 m_previewGenerator
->setPreviewShown(showPreview
);
1243 const int oldZoomLevel
= m_controller
->zoomLevel();
1244 emit
showPreviewChanged();
1246 // Enabling or disabling the preview might change the icon size of the view.
1247 // As the view does not emit a signal when the icon size has been changed,
1248 // the used zoom level of the controller must be adjusted manually:
1249 updateZoomLevel(oldZoomLevel
);
1252 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1253 // During the lifetime of a DolphinView instance the global view properties
1254 // should not be changed. This allows e. g. to split a view and use different
1255 // view properties for each view.
1256 m_ignoreViewProperties
= true;
1260 void DolphinView::createView()
1263 Q_ASSERT(m_iconsView
== 0);
1264 Q_ASSERT(m_detailsView
== 0);
1265 Q_ASSERT(m_columnView
== 0);
1267 QAbstractItemView
* view
= 0;
1270 m_iconsView
= new DolphinIconsView(this, m_controller
);
1276 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1277 view
= m_detailsView
;
1281 m_columnView
= new DolphinColumnView(this, m_controller
);
1282 view
= m_columnView
;
1286 Q_ASSERT(view
!= 0);
1287 view
->installEventFilter(this);
1288 view
->viewport()->installEventFilter(this);
1289 setFocusProxy(view
);
1291 if (m_mode
!= ColumnView
) {
1292 // Give the view the ability to auto-expand its directories on hovering
1293 // (the column view takes care about this itself). If the details view
1294 // uses expandable folders, the auto-expanding should be used always.
1295 DolphinSettings
& settings
= DolphinSettings::instance();
1296 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1297 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1299 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1300 folderExpander
->setEnabled(enabled
);
1301 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&)),
1302 m_controller
, SLOT(triggerItem(const QModelIndex
&)));
1305 // Listen out for requests to delete the current column.
1306 connect(m_columnView
, SIGNAL(requestColumnDeletion(QAbstractItemView
*)),
1307 this, SLOT(deleteWhenNotDragSource(QAbstractItemView
*)));
1310 m_controller
->setItemView(view
);
1312 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1313 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1314 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1315 view
->setItemDelegate(m_fileItemDelegate
);
1317 view
->setModel(m_proxyModel
);
1318 if (m_selectionModel
!= 0) {
1319 view
->setSelectionModel(m_selectionModel
);
1321 m_selectionModel
= view
->selectionModel();
1324 // reparent the selection model, as it should not be deleted
1325 // when deleting the model
1326 m_selectionModel
->setParent(this);
1328 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1330 m_previewGenerator
= new KFilePreviewGenerator(view
);
1331 m_previewGenerator
->setPreviewShown(m_showPreview
);
1333 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1334 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1335 connect(m_controller
, SIGNAL(hideToolTip()),
1336 m_toolTipManager
, SLOT(hideTip()));
1339 m_topLayout
->insertWidget(1, view
);
1341 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1342 this, SLOT(emitSelectionChangedSignal()));
1343 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1344 this, SLOT(emitContentsMoved()));
1345 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1346 this, SLOT(emitContentsMoved()));
1349 void DolphinView::deleteView()
1351 QAbstractItemView
* view
= itemView();
1353 // It's important to set the keyboard focus to the parent
1354 // before deleting the view: Otherwise when having a split
1355 // view the other view will get the focus and will request
1356 // an activation (see DolphinView::eventFilter()).
1360 m_topLayout
->removeWidget(view
);
1363 // m_previewGenerator's parent is not always destroyed, and we
1364 // don't want two active at once - manually delete.
1365 delete m_previewGenerator
;
1366 m_previewGenerator
= 0;
1369 m_controller
->disconnect(view
);
1372 deleteWhenNotDragSource(view
);
1378 m_fileItemDelegate
= 0;
1379 m_toolTipManager
= 0;
1383 QAbstractItemView
* DolphinView::itemView() const
1385 if (m_detailsView
!= 0) {
1386 return m_detailsView
;
1387 } else if (m_columnView
!= 0) {
1388 return m_columnView
;
1394 bool DolphinView::isCutItem(const KFileItem
& item
) const
1396 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1397 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1399 const KUrl
& itemUrl
= item
.url();
1400 KUrl::List::const_iterator it
= cutUrls
.begin();
1401 const KUrl::List::const_iterator end
= cutUrls
.end();
1403 if (*it
== itemUrl
) {
1412 void DolphinView::pasteToUrl(const KUrl
& url
)
1414 QClipboard
* clipboard
= QApplication::clipboard();
1415 const QMimeData
* mimeData
= clipboard
->mimeData();
1417 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1418 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1419 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1422 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1426 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1428 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1429 if (oldZoomLevel
!= newZoomLevel
) {
1430 m_controller
->setZoomLevel(newZoomLevel
);
1431 emit
zoomLevelChanged(newZoomLevel
);
1435 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1437 KUrl::List list
= selectedUrls();
1438 if (itemsExpandable() ) {
1439 list
= KDirModel::simplifiedUrlList(list
);
1444 QMimeData
* DolphinView::selectionMimeData() const
1446 if (isColumnViewActive()) {
1447 return m_columnView
->selectionMimeData();
1450 const QAbstractItemView
* view
= itemView();
1451 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1452 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1453 return m_dolphinModel
->mimeData(selection
.indexes());
1456 #include "dolphinview.moc"