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),
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 deleteExpandedViews();
155 const KUrl
& DolphinView::url() const
157 return m_controller
->url();
160 KUrl
DolphinView::rootUrl() const
162 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
165 void DolphinView::setActive(bool active
)
167 if (active
== m_active
) {
173 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
175 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
176 // bypasses the problem when having a split view and changing the active view to
177 // update the some URL dependent states. A nicer approach should be no big deal...
178 emit
urlChanged(url());
179 emit
selectionChanged(selectedItems());
184 QWidget
* viewport
= itemView()->viewport();
186 palette
.setColor(viewport
->backgroundRole(), color
);
187 viewport
->setPalette(palette
);
192 itemView()->setFocus();
196 m_controller
->indicateActivationChange(active
);
199 bool DolphinView::isActive() const
204 void DolphinView::setMode(Mode mode
)
206 if (mode
== m_mode
) {
207 return; // the wished mode is already set
210 const int oldZoomLevel
= m_controller
->zoomLevel();
215 const KUrl viewPropsUrl
= viewPropertiesUrl();
216 ViewProperties
props(viewPropsUrl
);
217 props
.setViewMode(m_mode
);
220 // the file item delegate has been recreated, apply the current
221 // additional information manually
222 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
223 m_fileItemDelegate
->setShowInformation(infoList
);
224 emit
additionalInfoChanged();
226 // Not all view modes support categorized sorting. Adjust the sorting model
227 // if changing the view mode results in a change of the categorized sorting
229 m_storedCategorizedSorting
= props
.categorizedSorting();
230 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
231 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
232 m_proxyModel
->setCategorizedModel(categorized
);
233 emit
categorizedSortingChanged();
238 updateZoomLevel(oldZoomLevel
);
240 loadDirectory(viewPropsUrl
);
244 DolphinView::Mode
DolphinView::mode() const
249 bool DolphinView::showPreview() const
251 return m_showPreview
;
254 bool DolphinView::showHiddenFiles() const
256 return m_dirLister
->showingDotFiles();
259 bool DolphinView::categorizedSorting() const
261 // If all view modes would support categorized sorting, returning
262 // m_proxyModel->isCategorizedModel() would be the way to go. As
263 // currently only the icons view supports caterized sorting, we remember
264 // the stored view properties state in m_storedCategorizedSorting and
265 // return this state. The application takes care to disable the corresponding
266 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
267 // that this setting is not applied to the current view mode.
268 return m_storedCategorizedSorting
;
271 bool DolphinView::supportsCategorizedSorting() const
273 return m_iconsView
!= 0;
276 void DolphinView::selectAll()
278 QAbstractItemView
* view
= itemView();
279 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
280 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
281 // selection instead of selecting all items. This is bypassed for KDE 4.0
282 // by invoking clearSelection() first.
283 view
->clearSelection();
287 void DolphinView::invertSelection()
289 if (isColumnViewActive()) {
290 // QAbstractItemView does not offer a virtual method invertSelection()
291 // as counterpart to QAbstractItemView::selectAll(). This makes it
292 // necessary to delegate the inverting of the selection to the
293 // column view, as only the selection of the active column should
295 m_columnView
->invertSelection();
297 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
298 const QAbstractItemModel
* itemModel
= selectionModel
->model();
300 const QModelIndex topLeft
= itemModel
->index(0, 0);
301 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
302 itemModel
->columnCount() - 1);
304 const QItemSelection
selection(topLeft
, bottomRight
);
305 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
309 bool DolphinView::hasSelection() const
311 return itemView()->selectionModel()->hasSelection();
314 void DolphinView::clearSelection()
316 itemView()->selectionModel()->clear();
319 KFileItemList
DolphinView::selectedItems() const
321 if (isColumnViewActive()) {
322 return m_columnView
->selectedItems();
325 const QAbstractItemView
* view
= itemView();
327 // Our view has a selection, we will map them back to the DolphinModel
328 // and then fill the KFileItemList.
329 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
331 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
332 KFileItemList itemList
;
334 const QModelIndexList indexList
= selection
.indexes();
335 foreach (const QModelIndex
&index
, indexList
) {
336 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
337 if (!item
.isNull()) {
338 itemList
.append(item
);
345 KUrl::List
DolphinView::selectedUrls() const
348 const KFileItemList list
= selectedItems();
349 foreach (const KFileItem
&item
, list
) {
350 urls
.append(item
.url());
355 int DolphinView::selectedItemsCount() const
357 if (isColumnViewActive()) {
358 // TODO: get rid of this special case by adjusting the dir lister
359 // to the current column
360 return m_columnView
->selectedItems().count();
363 return itemView()->selectionModel()->selection().count();
366 void DolphinView::setContentsPosition(int x
, int y
)
368 QAbstractItemView
* view
= itemView();
370 // the ColumnView takes care itself for the horizontal scrolling
371 if (!isColumnViewActive()) {
372 view
->horizontalScrollBar()->setValue(x
);
374 view
->verticalScrollBar()->setValue(y
);
376 m_loadingDirectory
= false;
379 QPoint
DolphinView::contentsPosition() const
381 const int x
= itemView()->horizontalScrollBar()->value();
382 const int y
= itemView()->verticalScrollBar()->value();
386 void DolphinView::setZoomLevel(int level
)
388 if (level
< ZoomLevelInfo::minimumLevel()) {
389 level
= ZoomLevelInfo::minimumLevel();
390 } else if (level
> ZoomLevelInfo::maximumLevel()) {
391 level
= ZoomLevelInfo::maximumLevel();
394 if (level
!= zoomLevel()) {
395 m_controller
->setZoomLevel(level
);
396 m_previewGenerator
->updatePreviews();
397 emit
zoomLevelChanged(level
);
401 int DolphinView::zoomLevel() const
403 return m_controller
->zoomLevel();
406 void DolphinView::setSorting(Sorting sorting
)
408 if (sorting
!= this->sorting()) {
409 updateSorting(sorting
);
413 DolphinView::Sorting
DolphinView::sorting() const
415 return m_proxyModel
->sorting();
418 void DolphinView::setSortOrder(Qt::SortOrder order
)
420 if (sortOrder() != order
) {
421 updateSortOrder(order
);
425 Qt::SortOrder
DolphinView::sortOrder() const
427 return m_proxyModel
->sortOrder();
430 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
432 const KUrl viewPropsUrl
= viewPropertiesUrl();
433 ViewProperties
props(viewPropsUrl
);
434 props
.setAdditionalInfo(info
);
435 m_fileItemDelegate
->setShowInformation(info
);
437 emit
additionalInfoChanged();
439 if (itemView() != m_detailsView
) {
440 // the details view requires no reloading of the directory, as it maps
441 // the file item delegate info to its columns internally
442 loadDirectory(viewPropsUrl
);
446 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
448 return m_fileItemDelegate
->showInformation();
451 void DolphinView::reload()
454 loadDirectory(url(), true);
457 void DolphinView::refresh()
459 m_ignoreViewProperties
= false;
461 const bool oldActivationState
= m_active
;
462 const int oldZoomLevel
= m_controller
->zoomLevel();
466 applyViewProperties(m_controller
->url());
469 setActive(oldActivationState
);
470 updateZoomLevel(oldZoomLevel
);
473 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
475 if (m_controller
->url() == url
) {
479 m_previewGenerator
->cancelPreviews();
480 m_controller
->setUrl(url
); // emits urlChanged, which we forward
482 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
483 applyViewProperties(rootUrl
);
484 loadDirectory(rootUrl
);
485 if (itemView() == m_columnView
) {
486 m_columnView
->setRootUrl(rootUrl
);
487 m_columnView
->showColumn(url
);
490 applyViewProperties(url
);
494 emit
startedPathLoading(url
);
497 void DolphinView::setNameFilter(const QString
& nameFilter
)
499 m_proxyModel
->setFilterRegExp(nameFilter
);
501 if (isColumnViewActive()) {
502 // adjusting the directory lister is not enough in the case of the
503 // column view, as each column has its own directory lister internally...
504 m_columnView
->setNameFilter(nameFilter
);
508 void DolphinView::calculateItemCount(int& fileCount
,
510 KIO::filesize_t
& totalFileSize
) const
512 foreach (const KFileItem
& item
, m_dirLister
->items()) {
517 totalFileSize
+= item
.size();
522 QString
DolphinView::statusBarText() const
527 KIO::filesize_t totalFileSize
= 0;
529 if (hasSelection()) {
530 // give a summary of the status of the selected files
531 const KFileItemList list
= selectedItems();
532 if (list
.isEmpty()) {
533 // when an item is triggered, it is temporary selected but selectedItems()
534 // will return an empty list
538 KFileItemList::const_iterator it
= list
.begin();
539 const KFileItemList::const_iterator end
= list
.end();
541 const KFileItem
& item
= *it
;
546 totalFileSize
+= item
.size();
551 if (folderCount
+ fileCount
== 1) {
552 // if only one item is selected, show the filename
553 const QString name
= list
.first().name();
554 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
555 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
556 name
, KIO::convertSize(totalFileSize
));
558 // at least 2 items are selected
559 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
560 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
561 if ((folderCount
> 0) && (fileCount
> 0)) {
562 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
563 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
564 } else if (fileCount
> 0) {
565 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
567 Q_ASSERT(folderCount
> 0);
572 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
573 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
574 fileCount
, folderCount
,
575 totalFileSize
, true);
581 void DolphinView::setUrl(const KUrl
& url
)
583 // remember current item candidate (see restoreCurrentItem())
584 m_currentItemUrl
= url
;
585 updateView(url
, KUrl());
588 void DolphinView::changeSelection(const KFileItemList
& selection
)
591 if (selection
.isEmpty()) {
594 const KUrl
& baseUrl
= url();
596 QItemSelection new_selection
;
597 foreach(const KFileItem
& item
, selection
) {
598 url
= item
.url().upUrl();
599 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
600 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
601 new_selection
.select(index
, index
);
604 itemView()->selectionModel()->select(new_selection
,
605 QItemSelectionModel::ClearAndSelect
606 | QItemSelectionModel::Current
);
609 void DolphinView::renameSelectedItems()
611 KFileItemList items
= selectedItems();
612 const int itemCount
= items
.count();
618 // More than one item has been selected for renaming. Open
619 // a rename dialog and rename all items afterwards.
620 RenameDialog
dialog(this, items
);
621 if (dialog
.exec() == QDialog::Rejected
) {
625 const QString newName
= dialog
.newName();
626 if (newName
.isEmpty()) {
627 emit
errorMessage(dialog
.errorString());
629 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
630 // as one operation instead of n rename operations like it is done now...
631 Q_ASSERT(newName
.contains('#'));
633 // currently the items are sorted by the selection order, resort
634 // them by the file name
635 qSort(items
.begin(), items
.end(), lessThan
);
637 // iterate through all selected items and rename them...
639 foreach (const KFileItem
& item
, items
) {
640 const KUrl
& oldUrl
= item
.url();
642 number
.setNum(index
++);
644 QString name
= newName
;
645 name
.replace('#', number
);
647 if (oldUrl
.fileName() != name
) {
648 KUrl newUrl
= oldUrl
;
649 newUrl
.setFileName(name
);
650 KonqOperations::rename(this, oldUrl
, newUrl
);
654 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
655 Q_ASSERT(itemCount
== 1);
657 if (isColumnViewActive()) {
658 m_columnView
->editItem(items
.first());
660 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
661 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
662 itemView()->edit(proxyIndex
);
665 Q_ASSERT(itemCount
== 1);
667 RenameDialog
dialog(this, items
);
668 if (dialog
.exec() == QDialog::Rejected
) {
672 const QString
& newName
= dialog
.newName();
673 if (newName
.isEmpty()) {
674 emit
errorMessage(dialog
.errorString());
676 const KUrl
& oldUrl
= items
.first().url();
677 KUrl newUrl
= oldUrl
;
678 newUrl
.setFileName(newName
);
679 KonqOperations::rename(this, oldUrl
, newUrl
);
684 void DolphinView::trashSelectedItems()
686 const KUrl::List list
= simplifiedSelectedUrls();
687 KonqOperations::del(this, KonqOperations::TRASH
, list
);
690 void DolphinView::deleteSelectedItems()
692 const KUrl::List list
= simplifiedSelectedUrls();
693 const bool del
= KonqOperations::askDeleteConfirmation(list
,
695 KonqOperations::DEFAULT_CONFIRMATION
,
699 KIO::Job
* job
= KIO::del(list
);
700 connect(job
, SIGNAL(result(KJob
*)),
701 this, SLOT(slotDeleteFileFinished(KJob
*)));
705 void DolphinView::cutSelectedItems()
707 QMimeData
* mimeData
= selectionMimeData();
708 KonqMimeData::addIsCutSelection(mimeData
, true);
709 QApplication::clipboard()->setMimeData(mimeData
);
712 void DolphinView::copySelectedItems()
714 QMimeData
* mimeData
= selectionMimeData();
715 QApplication::clipboard()->setMimeData(mimeData
);
718 void DolphinView::paste()
723 void DolphinView::pasteIntoFolder()
725 const KFileItemList items
= selectedItems();
726 if ((items
.count() == 1) && items
.first().isDir()) {
727 pasteToUrl(items
.first().url());
731 void DolphinView::setShowPreview(bool show
)
733 if (m_showPreview
== show
) {
737 const KUrl viewPropsUrl
= viewPropertiesUrl();
738 ViewProperties
props(viewPropsUrl
);
739 props
.setShowPreview(show
);
741 m_showPreview
= show
;
742 m_previewGenerator
->setPreviewShown(show
);
744 const int oldZoomLevel
= m_controller
->zoomLevel();
745 emit
showPreviewChanged();
747 // Enabling or disabling the preview might change the icon size of the view.
748 // As the view does not emit a signal when the icon size has been changed,
749 // the used zoom level of the controller must be adjusted manually:
750 updateZoomLevel(oldZoomLevel
);
752 loadDirectory(viewPropsUrl
);
755 void DolphinView::setShowHiddenFiles(bool show
)
757 if (m_dirLister
->showingDotFiles() == show
) {
761 const KUrl viewPropsUrl
= viewPropertiesUrl();
762 ViewProperties
props(viewPropsUrl
);
763 props
.setShowHiddenFiles(show
);
765 m_dirLister
->setShowingDotFiles(show
);
766 emit
showHiddenFilesChanged();
768 loadDirectory(viewPropsUrl
);
771 void DolphinView::setCategorizedSorting(bool categorized
)
773 if (categorized
== categorizedSorting()) {
777 // setCategorizedSorting(true) may only get invoked
778 // if the view supports categorized sorting
779 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
781 ViewProperties
props(viewPropertiesUrl());
782 props
.setCategorizedSorting(categorized
);
785 m_storedCategorizedSorting
= categorized
;
786 m_proxyModel
->setCategorizedModel(categorized
);
788 emit
categorizedSortingChanged();
791 void DolphinView::toggleSortOrder()
793 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
794 Qt::DescendingOrder
:
799 void DolphinView::toggleAdditionalInfo(QAction
* action
)
801 const KFileItemDelegate::Information info
=
802 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
804 KFileItemDelegate::InformationList list
= additionalInfo();
806 const bool show
= action
->isChecked();
808 const int index
= list
.indexOf(info
);
809 const bool containsInfo
= (index
>= 0);
810 if (show
&& !containsInfo
) {
812 setAdditionalInfo(list
);
813 } else if (!show
&& containsInfo
) {
814 list
.removeAt(index
);
815 setAdditionalInfo(list
);
816 Q_ASSERT(list
.indexOf(info
) < 0);
820 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
822 QWidget::mouseReleaseEvent(event
);
826 void DolphinView::wheelEvent(QWheelEvent
* event
)
828 if (event
->modifiers() & Qt::ControlModifier
) {
829 const int delta
= event
->delta();
830 const int level
= zoomLevel();
832 setZoomLevel(level
+ 1);
833 } else if (delta
< 0) {
834 setZoomLevel(level
- 1);
840 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
842 switch (event
->type()) {
843 case QEvent::FocusIn
:
844 if (watched
== itemView()) {
845 m_controller
->requestActivation();
849 case QEvent::MouseButtonPress
:
850 if ((watched
== itemView()->viewport()) && (m_expandedViews
.count() > 0)) {
851 // Listening to a mousebutton press event to delete expanded views is a
852 // workaround, as it seems impossible for the FolderExpander to know when
853 // a dragging outside a view has been finished. However it works quite well:
854 // A mousebutton press event indicates that a drag operation must be
856 deleteExpandedViews();
860 case QEvent::DragEnter
:
861 if (watched
== itemView()->viewport()) {
870 return QWidget::eventFilter(watched
, event
);
873 void DolphinView::activate()
878 void DolphinView::triggerItem(const KFileItem
& item
)
880 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
881 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
882 // items are selected by the user, hence don't trigger the
883 // item specified by 'index'
887 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
888 if (item
.isNull() || m_isContextMenuOpen
) {
892 if (m_toolTipManager
!= 0) {
893 m_toolTipManager
->hideTip();
895 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
898 void DolphinView::emitSelectionChangedSignal()
900 emit
selectionChanged(DolphinView::selectedItems());
903 void DolphinView::openContextMenu(const QPoint
& pos
)
906 if (isColumnViewActive()) {
907 item
= m_columnView
->itemAt(pos
);
909 const QModelIndex index
= itemView()->indexAt(pos
);
910 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
911 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
912 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
916 if (m_toolTipManager
!= 0) {
917 m_toolTipManager
->hideTip();
920 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
921 emit
requestContextMenu(item
, url());
922 m_isContextMenuOpen
= false;
925 void DolphinView::dropUrls(const KFileItem
& destItem
,
926 const KUrl
& destPath
,
929 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
932 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
934 ViewProperties
props(viewPropertiesUrl());
935 props
.setSorting(sorting
);
937 m_proxyModel
->setSorting(sorting
);
939 emit
sortingChanged(sorting
);
942 void DolphinView::updateSortOrder(Qt::SortOrder order
)
944 ViewProperties
props(viewPropertiesUrl());
945 props
.setSortOrder(order
);
947 m_proxyModel
->setSortOrder(order
);
949 emit
sortOrderChanged(order
);
952 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
954 ViewProperties
props(viewPropertiesUrl());
955 props
.setAdditionalInfo(info
);
958 m_fileItemDelegate
->setShowInformation(info
);
960 emit
additionalInfoChanged();
963 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
965 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
966 (m_mode
== DolphinView::IconsView
);
968 QAction
* showSizeInfo
= collection
->action("show_size_info");
969 QAction
* showDateInfo
= collection
->action("show_date_info");
970 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
971 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
972 QAction
* showGroupInfo
= collection
->action("show_group_info");
973 QAction
* showMimeInfo
= collection
->action("show_mime_info");
975 showSizeInfo
->setChecked(false);
976 showDateInfo
->setChecked(false);
977 showPermissionsInfo
->setChecked(false);
978 showOwnerInfo
->setChecked(false);
979 showGroupInfo
->setChecked(false);
980 showMimeInfo
->setChecked(false);
982 showSizeInfo
->setEnabled(enable
);
983 showDateInfo
->setEnabled(enable
);
984 showPermissionsInfo
->setEnabled(enable
);
985 showOwnerInfo
->setEnabled(enable
);
986 showGroupInfo
->setEnabled(enable
);
987 showMimeInfo
->setEnabled(enable
);
989 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
991 case KFileItemDelegate::Size
:
992 showSizeInfo
->setChecked(true);
994 case KFileItemDelegate::ModificationTime
:
995 showDateInfo
->setChecked(true);
997 case KFileItemDelegate::Permissions
:
998 showPermissionsInfo
->setChecked(true);
1000 case KFileItemDelegate::Owner
:
1001 showOwnerInfo
->setChecked(true);
1003 case KFileItemDelegate::OwnerAndGroup
:
1004 showGroupInfo
->setChecked(true);
1006 case KFileItemDelegate::FriendlyMimeType
:
1007 showMimeInfo
->setChecked(true);
1015 QPair
<bool, QString
> DolphinView::pasteInfo() const
1017 QPair
<bool, QString
> ret
;
1018 QClipboard
* clipboard
= QApplication::clipboard();
1019 const QMimeData
* mimeData
= clipboard
->mimeData();
1021 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1022 if (!urls
.isEmpty()) {
1023 // disable the paste action if no writing is supported
1024 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1025 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1027 if (urls
.count() == 1) {
1028 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1029 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1030 i18nc("@action:inmenu", "Paste One File");
1033 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1037 ret
.second
= i18nc("@action:inmenu", "Paste");
1043 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1045 m_tabsForFiles
= tabsForFiles
;
1048 bool DolphinView::isTabsForFilesEnabled() const
1050 return m_tabsForFiles
;
1053 bool DolphinView::itemsExpandable() const
1055 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1058 void DolphinView::emitContentsMoved()
1060 // only emit the contents moved signal if:
1061 // - no directory loading is ongoing (this would reset the contents position
1062 // always to (0, 0))
1063 // - if the Column View is active: the column view does an automatic
1064 // positioning during the loading operation, which must be remembered
1065 if (!m_loadingDirectory
|| isColumnViewActive()) {
1066 const QPoint
pos(contentsPosition());
1067 emit
contentsMoved(pos
.x(), pos
.y());
1071 void DolphinView::showHoverInformation(const KFileItem
& item
)
1073 emit
requestItemInfo(item
);
1076 void DolphinView::clearHoverInformation()
1078 emit
requestItemInfo(KFileItem());
1081 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1083 if (job
->error() == 0) {
1084 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1085 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1086 emit
errorMessage(job
->errorString());
1090 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1092 emit
requestUrlChange(url
);
1093 m_controller
->setUrl(url
);
1096 void DolphinView::restoreCurrentItem()
1098 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1099 if (dirIndex
.isValid()) {
1100 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1101 QAbstractItemView
* view
= itemView();
1102 const bool clearSelection
= !hasSelection();
1103 view
->setCurrentIndex(proxyIndex
);
1104 if (clearSelection
) {
1105 view
->clearSelection();
1110 void DolphinView::enterDir(const QModelIndex
& index
, QAbstractItemView
* view
)
1112 // Deleting a view that is the root of a drag operation is not allowed, otherwise
1113 // the dragging gets automatically cancelled by Qt. So before entering a new
1114 // directory, the current view is remembered in m_expandedViews and deleted
1115 // later when the drag operation has been finished (see DolphinView::eventFilter()).
1116 m_expandedViews
.append(view
);
1117 m_controller
->triggerItem(index
);
1120 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1122 if (!url
.isValid()) {
1123 const QString
location(url
.pathOrUrl());
1124 if (location
.isEmpty()) {
1125 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1127 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1132 m_loadingDirectory
= true;
1134 m_dirLister
->stop();
1135 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1137 if (isColumnViewActive()) {
1138 // adjusting the directory lister is not enough in the case of the
1139 // column view, as each column has its own directory lister internally...
1141 m_columnView
->reload();
1143 m_columnView
->showColumn(url
);
1148 KUrl
DolphinView::viewPropertiesUrl() const
1150 if (isColumnViewActive()) {
1151 return m_columnView
->rootUrl();
1157 void DolphinView::applyViewProperties(const KUrl
& url
)
1159 if (m_ignoreViewProperties
) {
1163 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1164 // The column view is active, hence don't apply the view properties
1165 // of sub directories (represented by columns) to the view. The
1166 // view always represents the properties of the first column.
1170 const ViewProperties
props(url
);
1172 const Mode mode
= props
.viewMode();
1173 if (m_mode
!= mode
) {
1174 const int oldZoomLevel
= m_controller
->zoomLevel();
1180 updateZoomLevel(oldZoomLevel
);
1182 if (itemView() == 0) {
1185 Q_ASSERT(itemView() != 0);
1186 Q_ASSERT(m_fileItemDelegate
!= 0);
1188 const bool showHiddenFiles
= props
.showHiddenFiles();
1189 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1190 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1191 emit
showHiddenFilesChanged();
1194 m_storedCategorizedSorting
= props
.categorizedSorting();
1195 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1196 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1197 m_proxyModel
->setCategorizedModel(categorized
);
1198 emit
categorizedSortingChanged();
1201 const DolphinView::Sorting sorting
= props
.sorting();
1202 if (sorting
!= m_proxyModel
->sorting()) {
1203 m_proxyModel
->setSorting(sorting
);
1204 emit
sortingChanged(sorting
);
1207 const Qt::SortOrder sortOrder
= props
.sortOrder();
1208 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1209 m_proxyModel
->setSortOrder(sortOrder
);
1210 emit
sortOrderChanged(sortOrder
);
1213 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1214 if (info
!= m_fileItemDelegate
->showInformation()) {
1215 m_fileItemDelegate
->setShowInformation(info
);
1216 emit
additionalInfoChanged();
1219 const bool showPreview
= props
.showPreview();
1220 if (showPreview
!= m_showPreview
) {
1221 m_showPreview
= showPreview
;
1222 m_previewGenerator
->setPreviewShown(showPreview
);
1224 const int oldZoomLevel
= m_controller
->zoomLevel();
1225 emit
showPreviewChanged();
1227 // Enabling or disabling the preview might change the icon size of the view.
1228 // As the view does not emit a signal when the icon size has been changed,
1229 // the used zoom level of the controller must be adjusted manually:
1230 updateZoomLevel(oldZoomLevel
);
1233 if (DolphinSettings::instance().generalSettings()->globalViewProps()) {
1234 // During the lifetime of a DolphinView instance the global view properties
1235 // should not be changed. This allows e. g. to split a view and use different
1236 // view properties for each view.
1237 m_ignoreViewProperties
= true;
1241 void DolphinView::createView()
1244 Q_ASSERT(m_iconsView
== 0);
1245 Q_ASSERT(m_detailsView
== 0);
1246 Q_ASSERT(m_columnView
== 0);
1248 QAbstractItemView
* view
= 0;
1251 m_iconsView
= new DolphinIconsView(this, m_controller
);
1257 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1258 view
= m_detailsView
;
1262 m_columnView
= new DolphinColumnView(this, m_controller
);
1263 view
= m_columnView
;
1267 Q_ASSERT(view
!= 0);
1268 view
->installEventFilter(this);
1269 view
->viewport()->installEventFilter(this);
1270 setFocusProxy(view
);
1272 if (m_mode
!= ColumnView
) {
1273 // Give the view the ability to auto-expand its directories on hovering
1274 // (the column view takes care about this itself). If the details view
1275 // uses expandable folders, the auto-expanding should be used always.
1276 DolphinSettings
& settings
= DolphinSettings::instance();
1277 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1278 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1280 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1281 folderExpander
->setEnabled(enabled
);
1282 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&, QAbstractItemView
*)),
1283 this, SLOT(enterDir(const QModelIndex
&, QAbstractItemView
*)));
1286 m_controller
->setItemView(view
);
1288 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1289 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1290 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1291 view
->setItemDelegate(m_fileItemDelegate
);
1293 view
->setModel(m_proxyModel
);
1294 if (m_selectionModel
!= 0) {
1295 view
->setSelectionModel(m_selectionModel
);
1297 m_selectionModel
= view
->selectionModel();
1300 // reparent the selection model, as it should not be deleted
1301 // when deleting the model
1302 m_selectionModel
->setParent(this);
1304 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1306 m_previewGenerator
= new KFilePreviewGenerator(view
);
1307 m_previewGenerator
->setPreviewShown(m_showPreview
);
1309 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1310 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1311 connect(m_controller
, SIGNAL(hideToolTip()),
1312 m_toolTipManager
, SLOT(hideTip()));
1315 m_topLayout
->insertWidget(1, view
);
1317 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1318 this, SLOT(emitSelectionChangedSignal()));
1319 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1320 this, SLOT(emitContentsMoved()));
1321 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1322 this, SLOT(emitContentsMoved()));
1325 void DolphinView::deleteView()
1327 QAbstractItemView
* view
= itemView();
1329 // It's important to set the keyboard focus to the parent
1330 // before deleting the view: Otherwise when having a split
1331 // view the other view will get the focus and will request
1332 // an activation (see DolphinView::eventFilter()).
1336 m_topLayout
->removeWidget(view
);
1340 m_controller
->disconnect(view
);
1343 bool deleteView
= true;
1344 foreach (const QAbstractItemView
* expandedView
, m_expandedViews
) {
1345 if (view
== expandedView
) {
1346 // the current view got already expanded and must stay alive
1347 // until the dragging has been completed
1353 view
->deleteLater();
1360 m_fileItemDelegate
= 0;
1361 m_previewGenerator
= 0;
1362 m_toolTipManager
= 0;
1366 QAbstractItemView
* DolphinView::itemView() const
1368 if (m_detailsView
!= 0) {
1369 return m_detailsView
;
1370 } else if (m_columnView
!= 0) {
1371 return m_columnView
;
1377 bool DolphinView::isCutItem(const KFileItem
& item
) const
1379 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1380 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1382 const KUrl
& itemUrl
= item
.url();
1383 KUrl::List::const_iterator it
= cutUrls
.begin();
1384 const KUrl::List::const_iterator end
= cutUrls
.end();
1386 if (*it
== itemUrl
) {
1395 void DolphinView::pasteToUrl(const KUrl
& url
)
1397 QClipboard
* clipboard
= QApplication::clipboard();
1398 const QMimeData
* mimeData
= clipboard
->mimeData();
1400 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1401 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1402 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1405 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1409 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1411 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1412 if (oldZoomLevel
!= newZoomLevel
) {
1413 m_controller
->setZoomLevel(newZoomLevel
);
1414 emit
zoomLevelChanged(newZoomLevel
);
1418 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1420 KUrl::List list
= selectedUrls();
1421 if (itemsExpandable() ) {
1422 list
= KDirModel::simplifiedUrlList(list
);
1427 void DolphinView::deleteExpandedViews()
1429 const QAbstractItemView
* view
= itemView();
1430 foreach (QAbstractItemView
* expandedView
, m_expandedViews
) {
1431 if (expandedView
!= view
) {
1432 expandedView
->deleteLater();
1435 m_expandedViews
.clear();
1438 QMimeData
* DolphinView::selectionMimeData() const
1440 if (isColumnViewActive()) {
1441 return m_columnView
->selectionMimeData();
1444 const QAbstractItemView
* view
= itemView();
1445 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
1446 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
1447 return m_dolphinModel
->mimeData(selection
.indexes());
1450 #include "dolphinview.moc"