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>
37 #include <kio/deletejob.h>
38 #include <kio/netaccess.h>
39 #include <kio/previewjob.h>
42 #include <kmessagebox.h>
43 #include <kmimetyperesolver.h>
44 #include <konq_fileitemcapabilities.h>
45 #include <konq_operations.h>
46 #include <konqmimedata.h>
47 #include <ktoggleaction.h>
50 #include "dolphinmodel.h"
51 #include "dolphincolumnview.h"
52 #include "dolphincontroller.h"
53 #include "dolphinfileitemdelegate.h"
54 #include "dolphinsortfilterproxymodel.h"
55 #include "dolphindetailsview.h"
56 #include "dolphin_detailsmodesettings.h"
57 #include "dolphiniconsview.h"
58 #include "dolphinsettings.h"
59 #include "dolphin_generalsettings.h"
60 #include "draganddrophelper.h"
61 #include "folderexpander.h"
62 #include "renamedialog.h"
63 #include "tooltipmanager.h"
64 #include "viewproperties.h"
65 #include "zoomlevelinfo.h"
67 DolphinView::DolphinView(QWidget
* parent
,
69 KDirLister
* dirLister
,
70 DolphinModel
* dolphinModel
,
71 DolphinSortFilterProxyModel
* proxyModel
) :
75 m_loadingDirectory(false),
76 m_storedCategorizedSorting(false),
77 m_tabsForFiles(false),
78 m_isContextMenuOpen(false),
79 m_mode(DolphinView::IconsView
),
85 m_fileItemDelegate(0),
87 m_dolphinModel(dolphinModel
),
88 m_dirLister(dirLister
),
89 m_proxyModel(proxyModel
),
90 m_previewGenerator(0),
96 m_topLayout
= new QVBoxLayout(this);
97 m_topLayout
->setSpacing(0);
98 m_topLayout
->setMargin(0);
100 m_controller
= new DolphinController(this);
101 m_controller
->setUrl(url
);
103 connect(m_controller
, SIGNAL(urlChanged(const KUrl
&)),
104 this, SIGNAL(urlChanged(const KUrl
&)));
105 connect(m_controller
, SIGNAL(requestUrlChange(const KUrl
&)),
106 this, SLOT(slotRequestUrlChange(const KUrl
&)));
108 connect(m_controller
, SIGNAL(requestContextMenu(const QPoint
&)),
109 this, SLOT(openContextMenu(const QPoint
&)));
110 connect(m_controller
, SIGNAL(urlsDropped(const KFileItem
&, const KUrl
&, QDropEvent
*)),
111 this, SLOT(dropUrls(const KFileItem
&, const KUrl
&, QDropEvent
*)));
112 connect(m_controller
, SIGNAL(sortingChanged(DolphinView::Sorting
)),
113 this, SLOT(updateSorting(DolphinView::Sorting
)));
114 connect(m_controller
, SIGNAL(sortOrderChanged(Qt::SortOrder
)),
115 this, SLOT(updateSortOrder(Qt::SortOrder
)));
116 connect(m_controller
, SIGNAL(additionalInfoChanged(const KFileItemDelegate::InformationList
&)),
117 this, SLOT(updateAdditionalInfo(const KFileItemDelegate::InformationList
&)));
118 connect(m_controller
, SIGNAL(itemTriggered(const KFileItem
&)),
119 this, SLOT(triggerItem(const KFileItem
&)));
120 connect(m_controller
, SIGNAL(tabRequested(const KUrl
&)),
121 this, SIGNAL(tabRequested(const KUrl
&)));
122 connect(m_controller
, SIGNAL(activated()),
123 this, SLOT(activate()));
124 connect(m_controller
, SIGNAL(itemEntered(const KFileItem
&)),
125 this, SLOT(showHoverInformation(const KFileItem
&)));
126 connect(m_controller
, SIGNAL(viewportEntered()),
127 this, SLOT(clearHoverInformation()));
129 connect(m_dirLister
, SIGNAL(redirection(KUrl
, KUrl
)),
130 this, SIGNAL(redirection(KUrl
, KUrl
)));
131 connect(m_dirLister
, SIGNAL(completed()),
132 this, SLOT(restoreCurrentItem()));
134 applyViewProperties(url
);
135 m_topLayout
->addWidget(itemView());
138 DolphinView::~DolphinView()
140 deleteExpandedViews();
143 const KUrl
& DolphinView::url() const
145 return m_controller
->url();
148 KUrl
DolphinView::rootUrl() const
150 return isColumnViewActive() ? m_columnView
->rootUrl() : url();
153 void DolphinView::setActive(bool active
)
155 if (active
== m_active
) {
161 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
163 // TODO: emitting urlChanged() is a hack, as the URL hasn't really changed. It
164 // bypasses the problem when having a split view and changing the active view to
165 // update the some URL dependent states. A nicer approach should be no big deal...
166 emit
urlChanged(url());
167 emit
selectionChanged(selectedItems());
172 QWidget
* viewport
= itemView()->viewport();
174 palette
.setColor(viewport
->backgroundRole(), color
);
175 viewport
->setPalette(palette
);
180 itemView()->setFocus();
184 m_controller
->indicateActivationChange(active
);
187 bool DolphinView::isActive() const
192 void DolphinView::setMode(Mode mode
)
194 if (mode
== m_mode
) {
195 return; // the wished mode is already set
198 const int oldZoomLevel
= m_controller
->zoomLevel();
203 const KUrl viewPropsUrl
= viewPropertiesUrl();
204 ViewProperties
props(viewPropsUrl
);
205 props
.setViewMode(m_mode
);
208 // the file item delegate has been recreated, apply the current
209 // additional information manually
210 const KFileItemDelegate::InformationList infoList
= props
.additionalInfo();
211 m_fileItemDelegate
->setShowInformation(infoList
);
212 emit
additionalInfoChanged();
214 // Not all view modes support categorized sorting. Adjust the sorting model
215 // if changing the view mode results in a change of the categorized sorting
217 m_storedCategorizedSorting
= props
.categorizedSorting();
218 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
219 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
220 m_proxyModel
->setCategorizedModel(categorized
);
221 emit
categorizedSortingChanged();
226 updateZoomLevel(oldZoomLevel
);
228 loadDirectory(viewPropsUrl
);
232 DolphinView::Mode
DolphinView::mode() const
237 bool DolphinView::showPreview() const
239 return m_showPreview
;
242 bool DolphinView::showHiddenFiles() const
244 return m_dirLister
->showingDotFiles();
247 bool DolphinView::categorizedSorting() const
249 // If all view modes would support categorized sorting, returning
250 // m_proxyModel->isCategorizedModel() would be the way to go. As
251 // currently only the icons view supports caterized sorting, we remember
252 // the stored view properties state in m_storedCategorizedSorting and
253 // return this state. The application takes care to disable the corresponding
254 // checkbox by checking DolphinView::supportsCategorizedSorting() to indicate
255 // that this setting is not applied to the current view mode.
256 return m_storedCategorizedSorting
;
259 bool DolphinView::supportsCategorizedSorting() const
261 return m_iconsView
!= 0;
264 void DolphinView::selectAll()
266 QAbstractItemView
* view
= itemView();
267 // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if
268 // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the
269 // selection instead of selecting all items. This is bypassed for KDE 4.0
270 // by invoking clearSelection() first.
271 view
->clearSelection();
275 void DolphinView::invertSelection()
277 if (isColumnViewActive()) {
278 // QAbstractItemView does not offer a virtual method invertSelection()
279 // as counterpart to QAbstractItemView::selectAll(). This makes it
280 // necessary to delegate the inverting of the selection to the
281 // column view, as only the selection of the active column should
283 m_columnView
->invertSelection();
285 QItemSelectionModel
* selectionModel
= itemView()->selectionModel();
286 const QAbstractItemModel
* itemModel
= selectionModel
->model();
288 const QModelIndex topLeft
= itemModel
->index(0, 0);
289 const QModelIndex bottomRight
= itemModel
->index(itemModel
->rowCount() - 1,
290 itemModel
->columnCount() - 1);
292 const QItemSelection
selection(topLeft
, bottomRight
);
293 selectionModel
->select(selection
, QItemSelectionModel::Toggle
);
297 bool DolphinView::hasSelection() const
299 return itemView()->selectionModel()->hasSelection();
302 void DolphinView::clearSelection()
304 itemView()->selectionModel()->clear();
307 KFileItemList
DolphinView::selectedItems() const
309 if (isColumnViewActive()) {
310 return m_columnView
->selectedItems();
313 const QAbstractItemView
* view
= itemView();
315 // Our view has a selection, we will map them back to the DolphinModel
316 // and then fill the KFileItemList.
317 Q_ASSERT((view
!= 0) && (view
->selectionModel() != 0));
319 const QItemSelection selection
= m_proxyModel
->mapSelectionToSource(view
->selectionModel()->selection());
320 KFileItemList itemList
;
322 const QModelIndexList indexList
= selection
.indexes();
323 foreach (const QModelIndex
&index
, indexList
) {
324 KFileItem item
= m_dolphinModel
->itemForIndex(index
);
325 if (!item
.isNull()) {
326 itemList
.append(item
);
333 KUrl::List
DolphinView::selectedUrls() const
336 const KFileItemList list
= selectedItems();
337 foreach (const KFileItem
&item
, list
) {
338 urls
.append(item
.url());
343 int DolphinView::selectedItemsCount() const
345 if (isColumnViewActive()) {
346 // TODO: get rid of this special case by adjusting the dir lister
347 // to the current column
348 return m_columnView
->selectedItems().count();
351 return itemView()->selectionModel()->selection().count();
354 void DolphinView::setContentsPosition(int x
, int y
)
356 QAbstractItemView
* view
= itemView();
358 // the ColumnView takes care itself for the horizontal scrolling
359 if (!isColumnViewActive()) {
360 view
->horizontalScrollBar()->setValue(x
);
362 view
->verticalScrollBar()->setValue(y
);
364 m_loadingDirectory
= false;
367 QPoint
DolphinView::contentsPosition() const
369 const int x
= itemView()->horizontalScrollBar()->value();
370 const int y
= itemView()->verticalScrollBar()->value();
374 void DolphinView::setZoomLevel(int level
)
376 if (level
< ZoomLevelInfo::minimumLevel()) {
377 level
= ZoomLevelInfo::minimumLevel();
378 } else if (level
> ZoomLevelInfo::maximumLevel()) {
379 level
= ZoomLevelInfo::maximumLevel();
382 if (level
!= zoomLevel()) {
383 m_controller
->setZoomLevel(level
);
384 m_previewGenerator
->updatePreviews();
385 emit
zoomLevelChanged(level
);
389 int DolphinView::zoomLevel() const
391 return m_controller
->zoomLevel();
394 void DolphinView::setSorting(Sorting sorting
)
396 if (sorting
!= this->sorting()) {
397 updateSorting(sorting
);
401 DolphinView::Sorting
DolphinView::sorting() const
403 return m_proxyModel
->sorting();
406 void DolphinView::setSortOrder(Qt::SortOrder order
)
408 if (sortOrder() != order
) {
409 updateSortOrder(order
);
413 Qt::SortOrder
DolphinView::sortOrder() const
415 return m_proxyModel
->sortOrder();
418 void DolphinView::setAdditionalInfo(KFileItemDelegate::InformationList info
)
420 const KUrl viewPropsUrl
= viewPropertiesUrl();
421 ViewProperties
props(viewPropsUrl
);
422 props
.setAdditionalInfo(info
);
423 m_fileItemDelegate
->setShowInformation(info
);
425 emit
additionalInfoChanged();
427 if (itemView() != m_detailsView
) {
428 // the details view requires no reloading of the directory, as it maps
429 // the file item delegate info to its columns internally
430 loadDirectory(viewPropsUrl
);
434 KFileItemDelegate::InformationList
DolphinView::additionalInfo() const
436 return m_fileItemDelegate
->showInformation();
439 void DolphinView::reload()
442 loadDirectory(url(), true);
445 void DolphinView::refresh()
447 const bool oldActivationState
= m_active
;
448 const int oldZoomLevel
= m_controller
->zoomLevel();
452 applyViewProperties(m_controller
->url());
455 setActive(oldActivationState
);
456 updateZoomLevel(oldZoomLevel
);
459 void DolphinView::updateView(const KUrl
& url
, const KUrl
& rootUrl
)
461 if (m_controller
->url() == url
) {
465 m_previewGenerator
->cancelPreviews();
466 m_controller
->setUrl(url
); // emits urlChanged, which we forward
468 if (!rootUrl
.isEmpty() && rootUrl
.isParentOf(url
)) {
469 applyViewProperties(rootUrl
);
470 loadDirectory(rootUrl
);
471 if (itemView() == m_columnView
) {
472 m_columnView
->setRootUrl(rootUrl
);
473 m_columnView
->showColumn(url
);
476 applyViewProperties(url
);
480 emit
startedPathLoading(url
);
483 void DolphinView::setNameFilter(const QString
& nameFilter
)
485 m_proxyModel
->setFilterRegExp(nameFilter
);
487 if (isColumnViewActive()) {
488 // adjusting the directory lister is not enough in the case of the
489 // column view, as each column has its own directory lister internally...
490 m_columnView
->setNameFilter(nameFilter
);
494 void DolphinView::calculateItemCount(int& fileCount
,
496 KIO::filesize_t
& totalFileSize
) const
498 foreach (const KFileItem
& item
, m_dirLister
->items()) {
503 totalFileSize
+= item
.size();
508 QString
DolphinView::statusBarText() const
513 KIO::filesize_t totalFileSize
= 0;
515 if (hasSelection()) {
516 // give a summary of the status of the selected files
517 const KFileItemList list
= selectedItems();
518 if (list
.isEmpty()) {
519 // when an item is triggered, it is temporary selected but selectedItems()
520 // will return an empty list
524 KFileItemList::const_iterator it
= list
.begin();
525 const KFileItemList::const_iterator end
= list
.end();
527 const KFileItem
& item
= *it
;
532 totalFileSize
+= item
.size();
537 if (folderCount
+ fileCount
== 1) {
538 // if only one item is selected, show the filename
539 const QString name
= list
.first().name();
540 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
541 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
542 name
, KIO::convertSize(totalFileSize
));
544 // at least 2 items are selected
545 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
546 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
547 if ((folderCount
> 0) && (fileCount
> 0)) {
548 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
549 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
550 } else if (fileCount
> 0) {
551 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
553 Q_ASSERT(folderCount
> 0);
558 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
559 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
560 fileCount
, folderCount
,
561 totalFileSize
, true);
567 void DolphinView::setUrl(const KUrl
& url
)
569 // remember current item candidate (see restoreCurrentItem())
570 m_currentItemUrl
= url
;
571 updateView(url
, KUrl());
574 void DolphinView::changeSelection(const KFileItemList
& selection
)
577 if (selection
.isEmpty()) {
580 const KUrl
& baseUrl
= url();
582 QItemSelection new_selection
;
583 foreach(const KFileItem
& item
, selection
) {
584 url
= item
.url().upUrl();
585 if (baseUrl
.equals(url
, KUrl::CompareWithoutTrailingSlash
)) {
586 QModelIndex index
= m_proxyModel
->mapFromSource(m_dolphinModel
->indexForItem(item
));
587 new_selection
.select(index
, index
);
590 itemView()->selectionModel()->select(new_selection
,
591 QItemSelectionModel::ClearAndSelect
592 | QItemSelectionModel::Current
);
595 void DolphinView::renameSelectedItems()
597 const KFileItemList items
= selectedItems();
598 const int itemCount
= items
.count();
604 // More than one item has been selected for renaming. Open
605 // a rename dialog and rename all items afterwards.
606 RenameDialog
dialog(this, items
);
607 if (dialog
.exec() == QDialog::Rejected
) {
611 const QString newName
= dialog
.newName();
612 if (newName
.isEmpty()) {
613 emit
errorMessage(dialog
.errorString());
615 // TODO: check how this can be integrated into KIO::FileUndoManager/KonqOperations
616 // as one operation instead of n rename operations like it is done now...
617 Q_ASSERT(newName
.contains('#'));
619 // iterate through all selected items and rename them...
621 foreach (const KFileItem
&item
, items
) {
622 const KUrl
& oldUrl
= item
.url();
624 number
.setNum(index
++);
626 QString name
= newName
;
627 name
.replace('#', number
);
629 if (oldUrl
.fileName() != name
) {
630 KUrl newUrl
= oldUrl
;
631 newUrl
.setFileName(name
);
632 KonqOperations::rename(this, oldUrl
, newUrl
);
636 } else if (DolphinSettings::instance().generalSettings()->renameInline()) {
637 Q_ASSERT(itemCount
== 1);
639 if (isColumnViewActive()) {
640 m_columnView
->editItem(items
.first());
642 const QModelIndex dirIndex
= m_dolphinModel
->indexForItem(items
.first());
643 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
644 itemView()->edit(proxyIndex
);
647 Q_ASSERT(itemCount
== 1);
649 RenameDialog
dialog(this, items
);
650 if (dialog
.exec() == QDialog::Rejected
) {
654 const QString
& newName
= dialog
.newName();
655 if (newName
.isEmpty()) {
656 emit
errorMessage(dialog
.errorString());
658 const KUrl
& oldUrl
= items
.first().url();
659 KUrl newUrl
= oldUrl
;
660 newUrl
.setFileName(newName
);
661 KonqOperations::rename(this, oldUrl
, newUrl
);
666 void DolphinView::trashSelectedItems()
668 const KUrl::List list
= simplifiedSelectedUrls();
669 KonqOperations::del(this, KonqOperations::TRASH
, list
);
672 void DolphinView::deleteSelectedItems()
674 const KUrl::List list
= simplifiedSelectedUrls();
675 const bool del
= KonqOperations::askDeleteConfirmation(list
,
677 KonqOperations::DEFAULT_CONFIRMATION
,
681 KIO::Job
* job
= KIO::del(list
);
682 connect(job
, SIGNAL(result(KJob
*)),
683 this, SLOT(slotDeleteFileFinished(KJob
*)));
687 void DolphinView::cutSelectedItems()
689 QMimeData
* mimeData
= new QMimeData();
690 const KUrl::List kdeUrls
= simplifiedSelectedUrls();
691 const KUrl::List mostLocalUrls
;
692 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, true);
693 QApplication::clipboard()->setMimeData(mimeData
);
696 void DolphinView::copySelectedItems()
698 QMimeData
* mimeData
= new QMimeData();
699 const KUrl::List kdeUrls
= selectedUrls();
700 const KUrl::List mostLocalUrls
;
701 KonqMimeData::populateMimeData(mimeData
, kdeUrls
, mostLocalUrls
, false);
702 QApplication::clipboard()->setMimeData(mimeData
);
705 void DolphinView::paste()
710 void DolphinView::pasteIntoFolder()
712 const KFileItemList items
= selectedItems();
713 if ((items
.count() == 1) && items
.first().isDir()) {
714 pasteToUrl(items
.first().url());
718 void DolphinView::setShowPreview(bool show
)
720 if (m_showPreview
== show
) {
724 const KUrl viewPropsUrl
= viewPropertiesUrl();
725 ViewProperties
props(viewPropsUrl
);
726 props
.setShowPreview(show
);
728 m_showPreview
= show
;
729 m_previewGenerator
->setPreviewShown(show
);
731 const int oldZoomLevel
= m_controller
->zoomLevel();
732 emit
showPreviewChanged();
734 // Enabling or disabling the preview might change the icon size of the view.
735 // As the view does not emit a signal when the icon size has been changed,
736 // the used zoom level of the controller must be adjusted manually:
737 updateZoomLevel(oldZoomLevel
);
739 loadDirectory(viewPropsUrl
);
742 void DolphinView::setShowHiddenFiles(bool show
)
744 if (m_dirLister
->showingDotFiles() == show
) {
748 const KUrl viewPropsUrl
= viewPropertiesUrl();
749 ViewProperties
props(viewPropsUrl
);
750 props
.setShowHiddenFiles(show
);
752 m_dirLister
->setShowingDotFiles(show
);
753 emit
showHiddenFilesChanged();
755 loadDirectory(viewPropsUrl
);
758 void DolphinView::setCategorizedSorting(bool categorized
)
760 if (categorized
== categorizedSorting()) {
764 // setCategorizedSorting(true) may only get invoked
765 // if the view supports categorized sorting
766 Q_ASSERT(!categorized
|| supportsCategorizedSorting());
768 ViewProperties
props(viewPropertiesUrl());
769 props
.setCategorizedSorting(categorized
);
772 m_storedCategorizedSorting
= categorized
;
773 m_proxyModel
->setCategorizedModel(categorized
);
775 emit
categorizedSortingChanged();
778 void DolphinView::toggleSortOrder()
780 const Qt::SortOrder order
= (sortOrder() == Qt::AscendingOrder
) ?
781 Qt::DescendingOrder
:
786 void DolphinView::toggleAdditionalInfo(QAction
* action
)
788 const KFileItemDelegate::Information info
=
789 static_cast<KFileItemDelegate::Information
>(action
->data().toInt());
791 KFileItemDelegate::InformationList list
= additionalInfo();
793 const bool show
= action
->isChecked();
795 const int index
= list
.indexOf(info
);
796 const bool containsInfo
= (index
>= 0);
797 if (show
&& !containsInfo
) {
799 setAdditionalInfo(list
);
800 } else if (!show
&& containsInfo
) {
801 list
.removeAt(index
);
802 setAdditionalInfo(list
);
803 Q_ASSERT(list
.indexOf(info
) < 0);
807 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
809 QWidget::mouseReleaseEvent(event
);
813 void DolphinView::wheelEvent(QWheelEvent
* event
)
815 if (event
->modifiers() & Qt::ControlModifier
) {
816 const int delta
= event
->delta();
817 const int level
= zoomLevel();
819 setZoomLevel(level
+ 1);
820 } else if (delta
< 0) {
821 setZoomLevel(level
- 1);
827 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
829 switch (event
->type()) {
830 case QEvent::FocusIn
:
831 if (watched
== itemView()) {
832 m_controller
->requestActivation();
836 case QEvent::MouseButtonPress
:
837 if ((watched
== itemView()->viewport()) && (m_expandedViews
.count() > 0)) {
838 // Listening to a mousebutton press event to delete expanded views is a
839 // workaround, as it seems impossible for the FolderExpander to know when
840 // a dragging outside a view has been finished. However it works quite well:
841 // A mousebutton press event indicates that a drag operation must be
843 deleteExpandedViews();
847 case QEvent::DragEnter
:
848 if (watched
== itemView()->viewport()) {
857 return QWidget::eventFilter(watched
, event
);
860 void DolphinView::activate()
865 void DolphinView::triggerItem(const KFileItem
& item
)
867 const Qt::KeyboardModifiers modifier
= QApplication::keyboardModifiers();
868 if ((modifier
& Qt::ShiftModifier
) || (modifier
& Qt::ControlModifier
)) {
869 // items are selected by the user, hence don't trigger the
870 // item specified by 'index'
874 // TODO: the m_isContextMenuOpen check is a workaround for Qt-issue 207192
875 if (item
.isNull() || m_isContextMenuOpen
) {
879 if (m_toolTipManager
!= 0) {
880 m_toolTipManager
->hideTip();
882 emit
itemTriggered(item
); // caught by DolphinViewContainer or DolphinPart
885 void DolphinView::emitSelectionChangedSignal()
887 emit
selectionChanged(DolphinView::selectedItems());
890 void DolphinView::openContextMenu(const QPoint
& pos
)
893 if (isColumnViewActive()) {
894 item
= m_columnView
->itemAt(pos
);
896 const QModelIndex index
= itemView()->indexAt(pos
);
897 if (index
.isValid() && (index
.column() == DolphinModel::Name
)) {
898 const QModelIndex dolphinModelIndex
= m_proxyModel
->mapToSource(index
);
899 item
= m_dolphinModel
->itemForIndex(dolphinModelIndex
);
903 if (m_toolTipManager
!= 0) {
904 m_toolTipManager
->hideTip();
907 m_isContextMenuOpen
= true; // TODO: workaround for Qt-issue 207192
908 emit
requestContextMenu(item
, url());
909 m_isContextMenuOpen
= false;
912 void DolphinView::dropUrls(const KFileItem
& destItem
,
913 const KUrl
& destPath
,
916 DragAndDropHelper::instance().dropUrls(destItem
, destPath
, event
, this);
919 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
921 ViewProperties
props(viewPropertiesUrl());
922 props
.setSorting(sorting
);
924 m_proxyModel
->setSorting(sorting
);
926 emit
sortingChanged(sorting
);
929 void DolphinView::updateSortOrder(Qt::SortOrder order
)
931 ViewProperties
props(viewPropertiesUrl());
932 props
.setSortOrder(order
);
934 m_proxyModel
->setSortOrder(order
);
936 emit
sortOrderChanged(order
);
939 void DolphinView::updateAdditionalInfo(const KFileItemDelegate::InformationList
& info
)
941 ViewProperties
props(viewPropertiesUrl());
942 props
.setAdditionalInfo(info
);
945 m_fileItemDelegate
->setShowInformation(info
);
947 emit
additionalInfoChanged();
950 void DolphinView::updateAdditionalInfoActions(KActionCollection
* collection
)
952 const bool enable
= (m_mode
== DolphinView::DetailsView
) ||
953 (m_mode
== DolphinView::IconsView
);
955 QAction
* showSizeInfo
= collection
->action("show_size_info");
956 QAction
* showDateInfo
= collection
->action("show_date_info");
957 QAction
* showPermissionsInfo
= collection
->action("show_permissions_info");
958 QAction
* showOwnerInfo
= collection
->action("show_owner_info");
959 QAction
* showGroupInfo
= collection
->action("show_group_info");
960 QAction
* showMimeInfo
= collection
->action("show_mime_info");
962 showSizeInfo
->setChecked(false);
963 showDateInfo
->setChecked(false);
964 showPermissionsInfo
->setChecked(false);
965 showOwnerInfo
->setChecked(false);
966 showGroupInfo
->setChecked(false);
967 showMimeInfo
->setChecked(false);
969 showSizeInfo
->setEnabled(enable
);
970 showDateInfo
->setEnabled(enable
);
971 showPermissionsInfo
->setEnabled(enable
);
972 showOwnerInfo
->setEnabled(enable
);
973 showGroupInfo
->setEnabled(enable
);
974 showMimeInfo
->setEnabled(enable
);
976 foreach (KFileItemDelegate::Information info
, m_fileItemDelegate
->showInformation()) {
978 case KFileItemDelegate::Size
:
979 showSizeInfo
->setChecked(true);
981 case KFileItemDelegate::ModificationTime
:
982 showDateInfo
->setChecked(true);
984 case KFileItemDelegate::Permissions
:
985 showPermissionsInfo
->setChecked(true);
987 case KFileItemDelegate::Owner
:
988 showOwnerInfo
->setChecked(true);
990 case KFileItemDelegate::OwnerAndGroup
:
991 showGroupInfo
->setChecked(true);
993 case KFileItemDelegate::FriendlyMimeType
:
994 showMimeInfo
->setChecked(true);
1002 QPair
<bool, QString
> DolphinView::pasteInfo() const
1004 QPair
<bool, QString
> ret
;
1005 QClipboard
* clipboard
= QApplication::clipboard();
1006 const QMimeData
* mimeData
= clipboard
->mimeData();
1008 KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1009 if (!urls
.isEmpty()) {
1010 // disable the paste action if no writing is supported
1011 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url());
1012 ret
.first
= KonqFileItemCapabilities(KFileItemList() << item
).supportsWriting();
1014 if (urls
.count() == 1) {
1015 const KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, urls
.first(), true);
1016 ret
.second
= item
.isDir() ? i18nc("@action:inmenu", "Paste One Folder") :
1017 i18nc("@action:inmenu", "Paste One File");
1020 ret
.second
= i18ncp("@action:inmenu", "Paste One Item", "Paste %1 Items", urls
.count());
1024 ret
.second
= i18nc("@action:inmenu", "Paste");
1030 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1032 m_tabsForFiles
= tabsForFiles
;
1035 bool DolphinView::isTabsForFilesEnabled() const
1037 return m_tabsForFiles
;
1040 void DolphinView::emitContentsMoved()
1042 // only emit the contents moved signal if:
1043 // - no directory loading is ongoing (this would reset the contents position
1044 // always to (0, 0))
1045 // - if the Column View is active: the column view does an automatic
1046 // positioning during the loading operation, which must be remembered
1047 if (!m_loadingDirectory
|| isColumnViewActive()) {
1048 const QPoint
pos(contentsPosition());
1049 emit
contentsMoved(pos
.x(), pos
.y());
1053 void DolphinView::showHoverInformation(const KFileItem
& item
)
1055 emit
requestItemInfo(item
);
1058 void DolphinView::clearHoverInformation()
1060 emit
requestItemInfo(KFileItem());
1063 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1065 if (job
->error() == 0) {
1066 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1067 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1068 emit
errorMessage(job
->errorString());
1072 void DolphinView::slotRequestUrlChange(const KUrl
& url
)
1074 emit
requestUrlChange(url
);
1075 m_controller
->setUrl(url
);
1078 void DolphinView::restoreCurrentItem()
1080 const QModelIndex dirIndex
= m_dolphinModel
->indexForUrl(m_currentItemUrl
);
1081 if (dirIndex
.isValid()) {
1082 const QModelIndex proxyIndex
= m_proxyModel
->mapFromSource(dirIndex
);
1083 QAbstractItemView
* view
= itemView();
1084 const bool clearSelection
= !hasSelection();
1085 view
->setCurrentIndex(proxyIndex
);
1086 if (clearSelection
) {
1087 view
->clearSelection();
1092 void DolphinView::enterDir(const QModelIndex
& index
, QAbstractItemView
* view
)
1094 // Deleting a view that is the root of a drag operation is not allowed, otherwise
1095 // the dragging gets automatically cancelled by Qt. So before entering a new
1096 // directory, the current view is remembered in m_expandedViews and deleted
1097 // later when the drag operation has been finished (see DolphinView::eventFilter()).
1098 m_expandedViews
.append(view
);
1099 m_controller
->triggerItem(index
);
1102 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1104 if (!url
.isValid()) {
1105 const QString
location(url
.pathOrUrl());
1106 if (location
.isEmpty()) {
1107 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1109 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1114 m_loadingDirectory
= true;
1116 m_dirLister
->stop();
1117 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1119 if (isColumnViewActive()) {
1120 // adjusting the directory lister is not enough in the case of the
1121 // column view, as each column has its own directory lister internally...
1123 m_columnView
->reload();
1125 m_columnView
->showColumn(url
);
1130 KUrl
DolphinView::viewPropertiesUrl() const
1132 if (isColumnViewActive()) {
1133 return m_columnView
->rootUrl();
1139 void DolphinView::applyViewProperties(const KUrl
& url
)
1141 if (isColumnViewActive() && rootUrl().isParentOf(url
)) {
1142 // The column view is active, hence don't apply the view properties
1143 // of sub directories (represented by columns) to the view. The
1144 // view always represents the properties of the first column.
1148 const ViewProperties
props(url
);
1150 const Mode mode
= props
.viewMode();
1151 if (m_mode
!= mode
) {
1152 const int oldZoomLevel
= m_controller
->zoomLevel();
1158 updateZoomLevel(oldZoomLevel
);
1160 if (itemView() == 0) {
1163 Q_ASSERT(itemView() != 0);
1164 Q_ASSERT(m_fileItemDelegate
!= 0);
1166 const bool showHiddenFiles
= props
.showHiddenFiles();
1167 if (showHiddenFiles
!= m_dirLister
->showingDotFiles()) {
1168 m_dirLister
->setShowingDotFiles(showHiddenFiles
);
1169 emit
showHiddenFilesChanged();
1172 m_storedCategorizedSorting
= props
.categorizedSorting();
1173 const bool categorized
= m_storedCategorizedSorting
&& supportsCategorizedSorting();
1174 if (categorized
!= m_proxyModel
->isCategorizedModel()) {
1175 m_proxyModel
->setCategorizedModel(categorized
);
1176 emit
categorizedSortingChanged();
1179 const DolphinView::Sorting sorting
= props
.sorting();
1180 if (sorting
!= m_proxyModel
->sorting()) {
1181 m_proxyModel
->setSorting(sorting
);
1182 emit
sortingChanged(sorting
);
1185 const Qt::SortOrder sortOrder
= props
.sortOrder();
1186 if (sortOrder
!= m_proxyModel
->sortOrder()) {
1187 m_proxyModel
->setSortOrder(sortOrder
);
1188 emit
sortOrderChanged(sortOrder
);
1191 KFileItemDelegate::InformationList info
= props
.additionalInfo();
1192 if (info
!= m_fileItemDelegate
->showInformation()) {
1193 m_fileItemDelegate
->setShowInformation(info
);
1194 emit
additionalInfoChanged();
1197 const bool showPreview
= props
.showPreview();
1198 if (showPreview
!= m_showPreview
) {
1199 m_showPreview
= showPreview
;
1200 m_previewGenerator
->setPreviewShown(showPreview
);
1202 const int oldZoomLevel
= m_controller
->zoomLevel();
1203 emit
showPreviewChanged();
1205 // Enabling or disabling the preview might change the icon size of the view.
1206 // As the view does not emit a signal when the icon size has been changed,
1207 // the used zoom level of the controller must be adjusted manually:
1208 updateZoomLevel(oldZoomLevel
);
1212 void DolphinView::createView()
1215 Q_ASSERT(m_iconsView
== 0);
1216 Q_ASSERT(m_detailsView
== 0);
1217 Q_ASSERT(m_columnView
== 0);
1219 QAbstractItemView
* view
= 0;
1222 m_iconsView
= new DolphinIconsView(this, m_controller
);
1228 m_detailsView
= new DolphinDetailsView(this, m_controller
);
1229 view
= m_detailsView
;
1233 m_columnView
= new DolphinColumnView(this, m_controller
);
1234 view
= m_columnView
;
1238 Q_ASSERT(view
!= 0);
1239 view
->installEventFilter(this);
1240 view
->viewport()->installEventFilter(this);
1242 if (m_mode
!= ColumnView
) {
1243 // Give the view the ability to auto-expand its directories on hovering
1244 // (the column view takes care about this itself). If the details view
1245 // uses expandable folders, the auto-expanding should be used always.
1246 DolphinSettings
& settings
= DolphinSettings::instance();
1247 const bool enabled
= settings
.generalSettings()->autoExpandFolders() ||
1248 ((m_detailsView
!= 0) && settings
.detailsModeSettings()->expandableFolders());
1250 FolderExpander
* folderExpander
= new FolderExpander(view
, m_proxyModel
);
1251 folderExpander
->setEnabled(enabled
);
1252 connect(folderExpander
, SIGNAL(enterDir(const QModelIndex
&, QAbstractItemView
*)),
1253 this, SLOT(enterDir(const QModelIndex
&, QAbstractItemView
*)));
1256 m_controller
->setItemView(view
);
1258 m_fileItemDelegate
= new DolphinFileItemDelegate(view
);
1259 m_fileItemDelegate
->setShowToolTipWhenElided(false);
1260 m_fileItemDelegate
->setMinimizedNameColumn(m_mode
== DetailsView
);
1261 view
->setItemDelegate(m_fileItemDelegate
);
1263 view
->setModel(m_proxyModel
);
1264 if (m_selectionModel
!= 0) {
1265 view
->setSelectionModel(m_selectionModel
);
1267 m_selectionModel
= view
->selectionModel();
1270 // reparent the selection model, as it should not be deleted
1271 // when deleting the model
1272 m_selectionModel
->setParent(this);
1274 view
->setSelectionMode(QAbstractItemView::ExtendedSelection
);
1276 m_previewGenerator
= new KFilePreviewGenerator(view
);
1277 m_previewGenerator
->setPreviewShown(m_showPreview
);
1279 if (DolphinSettings::instance().generalSettings()->showToolTips()) {
1280 m_toolTipManager
= new ToolTipManager(view
, m_proxyModel
);
1281 connect(m_controller
, SIGNAL(hideToolTip()),
1282 m_toolTipManager
, SLOT(hideTip()));
1285 m_topLayout
->insertWidget(1, view
);
1287 connect(view
->selectionModel(), SIGNAL(selectionChanged(const QItemSelection
&, const QItemSelection
&)),
1288 this, SLOT(emitSelectionChangedSignal()));
1289 connect(view
->verticalScrollBar(), SIGNAL(valueChanged(int)),
1290 this, SLOT(emitContentsMoved()));
1291 connect(view
->horizontalScrollBar(), SIGNAL(valueChanged(int)),
1292 this, SLOT(emitContentsMoved()));
1295 void DolphinView::deleteView()
1297 QAbstractItemView
* view
= itemView();
1299 // It's important to set the keyboard focus to the parent
1300 // before deleting the view: Otherwise when having a split
1301 // view the other view will get the focus and will request
1302 // an activation (see DolphinView::eventFilter()).
1305 m_topLayout
->removeWidget(view
);
1309 m_controller
->disconnect(view
);
1312 bool deleteView
= true;
1313 foreach (const QAbstractItemView
* expandedView
, m_expandedViews
) {
1314 if (view
== expandedView
) {
1315 // the current view got already expanded and must stay alive
1316 // until the dragging has been completed
1322 view
->deleteLater();
1329 m_fileItemDelegate
= 0;
1330 m_previewGenerator
= 0;
1331 m_toolTipManager
= 0;
1335 QAbstractItemView
* DolphinView::itemView() const
1337 if (m_detailsView
!= 0) {
1338 return m_detailsView
;
1339 } else if (m_columnView
!= 0) {
1340 return m_columnView
;
1346 bool DolphinView::isCutItem(const KFileItem
& item
) const
1348 const QMimeData
* mimeData
= QApplication::clipboard()->mimeData();
1349 const KUrl::List cutUrls
= KUrl::List::fromMimeData(mimeData
);
1351 const KUrl
& itemUrl
= item
.url();
1352 KUrl::List::const_iterator it
= cutUrls
.begin();
1353 const KUrl::List::const_iterator end
= cutUrls
.end();
1355 if (*it
== itemUrl
) {
1364 void DolphinView::pasteToUrl(const KUrl
& url
)
1366 QClipboard
* clipboard
= QApplication::clipboard();
1367 const QMimeData
* mimeData
= clipboard
->mimeData();
1369 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1370 if (KonqMimeData::decodeIsCutSelection(mimeData
)) {
1371 KonqOperations::copy(this, KonqOperations::MOVE
, sourceUrls
, url
);
1374 KonqOperations::copy(this, KonqOperations::COPY
, sourceUrls
, url
);
1378 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1380 const int newZoomLevel
= ZoomLevelInfo::zoomLevelForIconSize(itemView()->iconSize());
1381 if (oldZoomLevel
!= newZoomLevel
) {
1382 m_controller
->setZoomLevel(newZoomLevel
);
1383 emit
zoomLevelChanged(newZoomLevel
);
1387 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1389 KUrl::List list
= selectedUrls();
1390 if (itemsExpandable() ) {
1391 list
= KonqOperations::simplifiedUrlList(list
);
1396 void DolphinView::deleteExpandedViews()
1398 const QAbstractItemView
* view
= itemView();
1399 foreach (QAbstractItemView
* expandedView
, m_expandedViews
) {
1400 if (expandedView
!= view
) {
1401 expandedView
->deleteLater();
1404 m_expandedViews
.clear();
1407 bool DolphinView::itemsExpandable() const
1409 return (m_detailsView
!= 0) && m_detailsView
->itemsExpandable();
1412 #include "dolphinview.moc"