1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
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 <QAbstractItemView>
24 #include <QApplication>
28 #include <QGraphicsSceneDragDropEvent>
30 #include <QItemSelection>
34 #include <KActionCollection>
35 #include <KColorScheme>
38 #include <KIconEffect>
40 #include <KFileItemListProperties>
42 #include <kitemviews/kfileitemmodel.h>
43 #include <kitemviews/kfileitemlistview.h>
44 #include <kitemviews/kitemlistselectionmanager.h>
45 #include <kitemviews/kitemlistview.h>
46 #include <kitemviews/kitemlistcontroller.h>
47 #include <KIO/DeleteJob>
48 #include <KIO/NetAccess>
49 #include <KIO/PreviewJob>
52 #include <KMessageBox>
53 #include <konq_fileitemcapabilities.h>
54 #include <konq_operations.h>
55 #include <konqmimedata.h>
56 #include <KToggleAction>
59 #include "additionalinfoaccessor.h"
60 #include "dolphindirlister.h"
61 #include "dolphinnewfilemenuobserver.h"
62 #include "dolphin_detailsmodesettings.h"
63 #include "dolphin_generalsettings.h"
64 #include "dolphinitemlistcontainer.h"
65 #include "draganddrophelper.h"
66 #include "renamedialog.h"
67 #include "versioncontrol/versioncontrolobserver.h"
68 #include "viewmodecontroller.h"
69 #include "viewproperties.h"
70 #include "views/tooltips/tooltipmanager.h"
71 #include "zoomlevelinfo.h"
74 const int MaxModeEnum
= DolphinView::CompactView
;
75 const int MaxSortingEnum
= DolphinView::SortByPath
;
78 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
81 m_tabsForFiles(false),
82 m_assureVisibleCurrentIndex(false),
83 m_isFolderWritable(true),
86 m_mode(DolphinView::IconsView
),
87 m_additionalInfoList(),
92 m_selectionChangedTimer(0),
94 m_restoredContentsPosition(),
97 m_versionControlObserver(0)
99 m_topLayout
= new QVBoxLayout(this);
100 m_topLayout
->setSpacing(0);
101 m_topLayout
->setMargin(0);
103 // When a new item has been created by the "Create New..." menu, the item should
104 // get selected and it must be assured that the item will get visible. As the
105 // creation is done asynchronously, several signals must be checked:
106 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
107 this, SLOT(observeCreatedItem(KUrl
)));
109 m_selectionChangedTimer
= new QTimer(this);
110 m_selectionChangedTimer
->setSingleShot(true);
111 m_selectionChangedTimer
->setInterval(300);
112 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
113 this, SLOT(emitSelectionChangedSignal()));
115 m_dirLister
= new DolphinDirLister(this);
116 m_dirLister
->setAutoUpdate(true);
117 m_dirLister
->setDelayedMimeTypes(true);
119 connect(m_dirLister
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
120 connect(m_dirLister
, SIGNAL(started(KUrl
)), this, SLOT(slotDirListerStarted(KUrl
)));
121 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
122 this, SLOT(slotRefreshItems()));
124 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
125 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
126 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
127 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
128 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
129 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
130 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
132 m_container
= new DolphinItemListContainer(m_dirLister
, this);
133 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
134 m_container
->installEventFilter(this);
135 setFocusProxy(m_container
);
136 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
137 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
139 KItemListController
* controller
= m_container
->controller();
140 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
141 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
142 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
143 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
144 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
145 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
146 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
147 connect(controller
, SIGNAL(itemPressed(int,Qt::MouseButton
)), this, SLOT(hideToolTip()));
148 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
149 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
150 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
151 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
153 KFileItemModel
* model
= fileItemModel();
155 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
158 KItemListView
* view
= controller
->view();
159 view
->installEventFilter(this);
160 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
161 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
162 connect(view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
163 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
164 connect(view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
165 this, SLOT(slotVisibleRolesChangedByHeader(QList
<QByteArray
>,QList
<QByteArray
>)));
167 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
168 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
169 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
171 m_toolTipManager
= new ToolTipManager(this);
173 m_versionControlObserver
= new VersionControlObserver(this);
174 m_versionControlObserver
->setModel(model
);
175 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
176 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
177 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
179 applyViewProperties();
180 m_topLayout
->addWidget(m_container
);
185 DolphinView::~DolphinView()
189 KUrl
DolphinView::url() const
194 void DolphinView::setActive(bool active
)
196 if (active
== m_active
) {
202 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
207 QWidget
* viewport
= m_container
->viewport();
210 palette
.setColor(viewport
->backgroundRole(), color
);
211 viewport
->setPalette(palette
);
217 m_container
->setFocus();
219 emit
writeStateChanged(m_isFolderWritable
);
223 bool DolphinView::isActive() const
228 void DolphinView::setMode(Mode mode
)
230 if (mode
!= m_mode
) {
231 ViewProperties
props(url());
232 props
.setViewMode(mode
);
235 applyViewProperties();
239 DolphinView::Mode
DolphinView::mode() const
244 void DolphinView::setPreviewsShown(bool show
)
246 if (previewsShown() == show
) {
250 ViewProperties
props(url());
251 props
.setPreviewsShown(show
);
253 m_container
->setPreviewsShown(show
);
254 emit
previewsShownChanged(show
);
257 bool DolphinView::previewsShown() const
259 return m_container
->previewsShown();
262 void DolphinView::setHiddenFilesShown(bool show
)
264 if (m_dirLister
->showingDotFiles() == show
) {
268 const KFileItemList itemList
= selectedItems();
269 m_selectedUrls
.clear();
270 m_selectedUrls
= itemList
.urlList();
272 ViewProperties
props(url());
273 props
.setHiddenFilesShown(show
);
275 fileItemModel()->setShowHiddenFiles(show
);
276 emit
hiddenFilesShownChanged(show
);
279 bool DolphinView::hiddenFilesShown() const
281 return m_dirLister
->showingDotFiles();
284 void DolphinView::setGroupedSorting(bool grouped
)
286 if (grouped
== groupedSorting()) {
290 ViewProperties
props(url());
291 props
.setGroupedSorting(grouped
);
294 m_container
->controller()->model()->setGroupedSorting(grouped
);
296 emit
groupedSortingChanged(grouped
);
299 bool DolphinView::groupedSorting() const
301 return fileItemModel()->groupedSorting();
304 KFileItemList
DolphinView::items() const
306 return m_dirLister
->items();
309 KFileItemList
DolphinView::selectedItems() const
311 const KFileItemModel
* model
= fileItemModel();
312 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
313 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
315 KFileItemList selectedItems
;
316 QSetIterator
<int> it(selectedIndexes
);
317 while (it
.hasNext()) {
318 const int index
= it
.next();
319 selectedItems
.append(model
->fileItem(index
));
321 return selectedItems
;
324 int DolphinView::selectedItemsCount() const
326 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
327 return selectionManager
->selectedItems().count();
330 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
332 m_selectedUrls
= urls
;
335 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
337 m_currentItemUrl
= url
;
340 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
342 const KItemListSelectionManager::SelectionMode mode
= enabled
343 ? KItemListSelectionManager::Select
344 : KItemListSelectionManager::Deselect
;
345 const KFileItemModel
* model
= fileItemModel();
346 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
348 for (int index
= 0; index
< model
->count(); index
++) {
349 const KFileItem item
= model
->fileItem(index
);
350 if (pattern
.exactMatch(item
.text())) {
351 // An alternative approach would be to store the matching items in a QSet<int> and
352 // select them in one go after the loop, but we'd need a new function
353 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
355 selectionManager
->setSelected(index
, 1, mode
);
360 void DolphinView::setZoomLevel(int level
)
362 const int oldZoomLevel
= zoomLevel();
363 m_container
->setZoomLevel(level
);
364 if (zoomLevel() != oldZoomLevel
) {
365 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
369 int DolphinView::zoomLevel() const
371 return m_container
->zoomLevel();
374 void DolphinView::setSorting(Sorting sorting
)
376 if (sorting
!= this->sorting()) {
377 updateSorting(sorting
);
381 DolphinView::Sorting
DolphinView::sorting() const
383 KItemModelBase
* model
= m_container
->controller()->model();
384 return sortingForSortRole(model
->sortRole());
387 void DolphinView::setSortOrder(Qt::SortOrder order
)
389 if (sortOrder() != order
) {
390 updateSortOrder(order
);
394 Qt::SortOrder
DolphinView::sortOrder() const
396 KItemModelBase
* model
= fileItemModel();
397 return model
->sortOrder();
400 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
402 if (sortFoldersFirst() != foldersFirst
) {
403 updateSortFoldersFirst(foldersFirst
);
407 bool DolphinView::sortFoldersFirst() const
409 KFileItemModel
* model
= fileItemModel();
410 return model
->sortFoldersFirst();
413 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
415 const QList
<AdditionalInfo
> previousList
= info
;
417 ViewProperties
props(url());
418 props
.setAdditionalInfoList(info
);
420 m_additionalInfoList
= info
;
421 applyAdditionalInfoListToView();
423 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
426 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
428 return m_additionalInfoList
;
431 void DolphinView::reload()
433 QByteArray viewState
;
434 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
435 saveState(saveStream
);
437 const KFileItemList itemList
= selectedItems();
438 m_selectedUrls
.clear();
439 m_selectedUrls
= itemList
.urlList();
442 loadDirectory(url(), true);
444 QDataStream
restoreStream(viewState
);
445 restoreState(restoreStream
);
448 void DolphinView::stopLoading()
453 void DolphinView::readSettings()
455 const int oldZoomLevel
= m_container
->zoomLevel();
457 GeneralSettings::self()->readConfig();
458 m_container
->readSettings();
459 applyViewProperties();
461 const int newZoomLevel
= m_container
->zoomLevel();
462 if (newZoomLevel
!= oldZoomLevel
) {
463 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
467 void DolphinView::writeSettings()
469 GeneralSettings::self()->writeConfig();
470 m_container
->writeSettings();
473 void DolphinView::setNameFilter(const QString
& nameFilter
)
475 fileItemModel()->setNameFilter(nameFilter
);
478 QString
DolphinView::nameFilter() const
480 return fileItemModel()->nameFilter();
483 void DolphinView::calculateItemCount(int& fileCount
,
485 KIO::filesize_t
& totalFileSize
) const
487 foreach (const KFileItem
& item
, m_dirLister
->items()) {
492 totalFileSize
+= item
.size();
497 QString
DolphinView::statusBarText() const
505 KIO::filesize_t totalFileSize
= 0;
507 if (hasSelection()) {
508 // Give a summary of the status of the selected files
509 const KFileItemList list
= selectedItems();
510 foreach (const KFileItem
& item
, list
) {
515 totalFileSize
+= item
.size();
519 if (folderCount
+ fileCount
== 1) {
520 // If only one item is selected, show the filename
521 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
523 // At least 2 items are selected
524 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
525 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
528 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
529 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
530 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
533 if (fileCount
> 0 && folderCount
> 0) {
534 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
535 foldersText
, filesText
, fileSizeText(totalFileSize
));
536 } else if (fileCount
> 0) {
537 summary
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, fileSizeText(totalFileSize
));
538 } else if (folderCount
> 0) {
539 summary
= foldersText
;
545 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
547 QList
<QAction
*> actions
;
549 if (items
.isEmpty()) {
550 const KFileItem item
= fileItemModel()->rootItem();
551 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
553 actions
= m_versionControlObserver
->actions(items
);
559 void DolphinView::setUrl(const KUrl
& url
)
565 emit
urlAboutToBeChanged(url
);
570 // It is important to clear the items from the model before
571 // applying the view properties, otherwise expensive operations
572 // might be done on the existing items although they get cleared
573 // anyhow afterwards by loadDirectory().
574 fileItemModel()->clear();
575 applyViewProperties();
578 emit
urlChanged(url
);
581 void DolphinView::selectAll()
583 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
584 selectionManager
->setSelected(0, fileItemModel()->count());
587 void DolphinView::invertSelection()
589 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
590 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
593 void DolphinView::clearSelection()
595 m_container
->controller()->selectionManager()->clearSelection();
598 void DolphinView::renameSelectedItems()
600 KFileItemList items
= selectedItems();
601 const int itemCount
= items
.count();
606 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
608 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
609 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
610 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
611 m_viewAccessor.itemView()->edit(proxyIndex);
613 RenameDialog
* dialog
= new RenameDialog(this, items
);
614 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
617 dialog
->activateWindow();
620 // assure that the current index remains visible when KDirLister
621 // will notify the view about changed items
622 m_assureVisibleCurrentIndex
= true;
625 void DolphinView::trashSelectedItems()
627 const KUrl::List list
= simplifiedSelectedUrls();
628 KonqOperations::del(this, KonqOperations::TRASH
, list
);
631 void DolphinView::deleteSelectedItems()
633 const KUrl::List list
= simplifiedSelectedUrls();
634 const bool del
= KonqOperations::askDeleteConfirmation(list
,
636 KonqOperations::DEFAULT_CONFIRMATION
,
640 KIO::Job
* job
= KIO::del(list
);
641 connect(job
, SIGNAL(result(KJob
*)),
642 this, SLOT(slotDeleteFileFinished(KJob
*)));
646 void DolphinView::cutSelectedItems()
648 QMimeData
* mimeData
= selectionMimeData();
649 KonqMimeData::addIsCutSelection(mimeData
, true);
650 QApplication::clipboard()->setMimeData(mimeData
);
653 void DolphinView::copySelectedItems()
655 QMimeData
* mimeData
= selectionMimeData();
656 QApplication::clipboard()->setMimeData(mimeData
);
659 void DolphinView::paste()
664 void DolphinView::pasteIntoFolder()
666 const KFileItemList items
= selectedItems();
667 if ((items
.count() == 1) && items
.first().isDir()) {
668 pasteToUrl(items
.first().url());
672 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
674 switch (event
->type()) {
675 case QEvent::FocusIn
:
676 if (watched
== m_container
) {
681 case QEvent::GraphicsSceneDragEnter
:
682 if (watched
== m_container
->controller()->view()) {
687 case QEvent::GraphicsSceneDragLeave
:
688 if (watched
== m_container
->controller()->view()) {
693 case QEvent::GraphicsSceneDrop
:
694 if (watched
== m_container
->controller()->view()) {
701 return QWidget::eventFilter(watched
, event
);
704 void DolphinView::wheelEvent(QWheelEvent
* event
)
706 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
707 const int numDegrees
= event
->delta() / 8;
708 const int numSteps
= numDegrees
/ 15;
710 setZoomLevel(zoomLevel() + numSteps
);
717 void DolphinView::hideEvent(QHideEvent
* event
)
720 QWidget::hideEvent(event
);
723 void DolphinView::activate()
728 void DolphinView::slotItemActivated(int index
)
730 const KFileItem item
= fileItemModel()->fileItem(index
);
731 if (!item
.isNull()) {
732 emit
itemActivated(item
);
736 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
738 Q_ASSERT(indexes
.count() >= 2);
742 KFileItemModel
* model
= fileItemModel();
743 QSetIterator
<int> it(indexes
);
744 while (it
.hasNext()) {
745 const int index
= it
.next();
746 items
.append(model
->fileItem(index
));
749 foreach (const KFileItem
& item
, items
) {
751 emit
tabRequested(item
.url());
753 emit
itemActivated(item
);
758 void DolphinView::slotItemMiddleClicked(int index
)
760 const KFileItem item
= fileItemModel()->fileItem(index
);
761 if (item
.isDir() || isTabsForFilesEnabled()) {
762 emit
tabRequested(item
.url());
766 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
768 const KFileItem item
= fileItemModel()->fileItem(index
);
769 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
772 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
774 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
777 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
779 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
781 KItemListView
* view
= m_container
->controller()->view();
782 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
784 // Add all roles to the menu that can be shown or hidden by the user
785 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
786 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
787 foreach (const DolphinView::AdditionalInfo info
, keys
) {
788 const QByteArray
& role
= infoAccessor
.role(info
);
789 if (role
!= "name") {
790 const QString text
= fileItemModel()->roleDescription(role
);
792 QAction
* action
= menu
.data()->addAction(text
);
793 action
->setCheckable(true);
794 action
->setChecked(visibleRolesSet
.contains(role
));
795 action
->setData(info
);
799 QAction
* action
= menu
.data()->exec(pos
.toPoint());
801 // Show or hide the selected role
802 const DolphinView::AdditionalInfo info
=
803 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
805 ViewProperties
props(url());
806 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
808 const QByteArray selectedRole
= infoAccessor
.role(info
);
809 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
811 if (action
->isChecked()) {
812 const int index
= keys
.indexOf(info
) + 1;
813 visibleRoles
.insert(index
, selectedRole
);
814 infoList
.insert(index
, info
);
816 visibleRoles
.removeOne(selectedRole
);
817 infoList
.removeOne(info
);
820 view
->setVisibleRoles(visibleRoles
);
821 props
.setAdditionalInfoList(infoList
);
827 void DolphinView::slotItemHovered(int index
)
829 const KFileItem item
= fileItemModel()->fileItem(index
);
831 if (GeneralSettings::showToolTips() && !m_dragging
) {
832 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
833 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
834 itemRect
.moveTo(pos
);
836 m_toolTipManager
->showToolTip(item
, itemRect
);
839 emit
requestItemInfo(item
);
842 void DolphinView::slotItemUnhovered(int index
)
846 emit
requestItemInfo(KFileItem());
849 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
852 KFileItem destItem
= fileItemModel()->fileItem(index
);
853 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
854 // Use the URL of the view as drop target if the item is no directory
856 destItem
= fileItemModel()->rootItem();
859 // The item represents a directory or desktop-file
860 destUrl
= destItem
.url();
863 QDropEvent
dropEvent(event
->pos().toPoint(),
864 event
->possibleActions(),
869 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
870 if (!error
.isEmpty()) {
871 emit
errorMessage(error
);
875 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
878 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
881 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
882 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
884 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
885 m_versionControlObserver
->setModel(fileItemModel
);
888 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
890 const int currentCount
= current
.count();
891 const int previousCount
= previous
.count();
892 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
893 (currentCount
> 0 && previousCount
== 0);
895 // If nothing has been selected before and something got selected (or if something
896 // was selected before and now nothing is selected) the selectionChangedSignal must
897 // be emitted asynchronously as fast as possible to update the edit-actions.
898 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
899 m_selectionChangedTimer
->start();
902 void DolphinView::emitSelectionChangedSignal()
904 m_selectionChangedTimer
->stop();
905 emit
selectionChanged(selectedItems());
908 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
910 ViewProperties
props(url());
911 props
.setSorting(sorting
);
913 KItemModelBase
* model
= m_container
->controller()->model();
914 model
->setSortRole(sortRoleForSorting(sorting
));
916 emit
sortingChanged(sorting
);
919 void DolphinView::updateSortOrder(Qt::SortOrder order
)
921 ViewProperties
props(url());
922 props
.setSortOrder(order
);
924 KItemModelBase
* model
= fileItemModel();
925 model
->setSortOrder(order
);
927 emit
sortOrderChanged(order
);
930 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
932 ViewProperties
props(url());
933 props
.setSortFoldersFirst(foldersFirst
);
935 KFileItemModel
* model
= fileItemModel();
936 model
->setSortFoldersFirst(foldersFirst
);
938 emit
sortFoldersFirstChanged(foldersFirst
);
941 QPair
<bool, QString
> DolphinView::pasteInfo() const
943 return KonqOperations::pasteInfo(url());
946 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
948 m_tabsForFiles
= tabsForFiles
;
951 bool DolphinView::isTabsForFilesEnabled() const
953 return m_tabsForFiles
;
956 bool DolphinView::itemsExpandable() const
958 return m_mode
== DetailsView
;
961 void DolphinView::restoreState(QDataStream
& stream
)
963 // Restore the current item that had the keyboard focus
964 stream
>> m_currentItemUrl
;
966 // Restore the view position
967 stream
>> m_restoredContentsPosition
;
969 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
972 fileItemModel()->restoreExpandedUrls(urls
);
975 void DolphinView::saveState(QDataStream
& stream
)
977 // Save the current item that has the keyboard focus
978 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
979 if (currentIndex
!= -1) {
980 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
981 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
982 KUrl currentItemUrl
= item
.url();
983 stream
<< currentItemUrl
;
988 // Save view position
989 const qreal x
= m_container
->horizontalScrollBar()->value();
990 const qreal y
= m_container
->verticalScrollBar()->value();
991 stream
<< QPoint(x
, y
);
993 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
994 stream
<< fileItemModel()->expandedUrls();
997 bool DolphinView::hasSelection() const
999 return m_container
->controller()->selectionManager()->hasSelection();
1002 KFileItem
DolphinView::rootItem() const
1004 return m_dirLister
->rootItem();
1007 void DolphinView::observeCreatedItem(const KUrl
& url
)
1009 m_createdItemUrl
= url
;
1010 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1011 // this, SLOT(selectAndScrollToCreatedItem()));
1014 void DolphinView::selectAndScrollToCreatedItem()
1016 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1017 if (dirIndex.isValid()) {
1018 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1019 QAbstractItemView* view = m_viewAccessor.itemView();
1021 view->setCurrentIndex(proxyIndex);
1025 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1026 this, SLOT(selectAndScrollToCreatedItem()));*/
1027 m_createdItemUrl
= KUrl();
1030 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1032 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1033 emit
redirection(oldUrl
, newUrl
);
1034 m_url
= newUrl
; // #186947
1038 void DolphinView::updateViewState()
1040 if (m_currentItemUrl
!= KUrl()) {
1041 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1042 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1043 if (currentIndex
!= -1) {
1044 selectionManager
->setCurrentItem(currentIndex
);
1046 selectionManager
->setCurrentItem(0);
1048 m_currentItemUrl
= KUrl();
1051 if (!m_restoredContentsPosition
.isNull()) {
1052 const int x
= m_restoredContentsPosition
.x();
1053 const int y
= m_restoredContentsPosition
.y();
1054 m_restoredContentsPosition
= QPoint();
1056 m_container
->horizontalScrollBar()->setValue(x
);
1057 m_container
->verticalScrollBar()->setValue(y
);
1060 if (!m_selectedUrls
.isEmpty()) {
1061 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1062 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1063 const KFileItemModel
* model
= fileItemModel();
1065 foreach (const KUrl
& url
, m_selectedUrls
) {
1066 const int index
= model
->index(url
);
1068 selectedItems
.insert(index
);
1072 selectionManager
->setSelectedItems(selectedItems
);
1073 m_selectedUrls
.clear();
1077 void DolphinView::hideToolTip()
1079 if (GeneralSettings::showToolTips()) {
1080 m_toolTipManager
->hideToolTip();
1084 void DolphinView::showHoverInformation(const KFileItem
& item
)
1086 emit
requestItemInfo(item
);
1089 void DolphinView::clearHoverInformation()
1091 emit
requestItemInfo(KFileItem());
1094 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1096 if (job
->error() == 0) {
1097 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1098 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1099 emit
errorMessage(job
->errorString());
1103 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1105 // Disable the writestate temporary until it can be determined in a fast way
1106 // in DolphinView::slotLoadingCompleted()
1107 if (m_isFolderWritable
) {
1108 m_isFolderWritable
= false;
1109 emit
writeStateChanged(m_isFolderWritable
);
1112 emit
startedPathLoading(url
);
1115 void DolphinView::slotLoadingCompleted()
1117 // Update the view-state. This has to be done using a Qt::QueuedConnection
1118 // because the view might not be in its final state yet (the view also
1119 // listens to the completed()-signal from KDirLister and the order of
1120 // of slots is undefined).
1121 QTimer::singleShot(0, this, SLOT(updateViewState()));
1123 emit
finishedPathLoading(url());
1125 updateWritableState();
1128 void DolphinView::slotRefreshItems()
1130 if (m_assureVisibleCurrentIndex
) {
1131 m_assureVisibleCurrentIndex
= false;
1132 //QAbstractItemView* view = m_viewAccessor.itemView();
1134 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1139 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1142 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1144 ViewProperties
props(url());
1145 props
.setSortOrder(current
);
1147 emit
sortOrderChanged(current
);
1150 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1153 Q_ASSERT(fileItemModel()->sortRole() == current
);
1155 ViewProperties
props(url());
1156 const Sorting sorting
= sortingForSortRole(current
);
1157 props
.setSorting(sorting
);
1159 emit
sortingChanged(sorting
);
1162 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1163 const QList
<QByteArray
>& previous
)
1166 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1168 const QList
<AdditionalInfo
> previousAdditionalInfoList
= m_additionalInfoList
;
1170 m_additionalInfoList
.clear();
1171 m_additionalInfoList
.reserve(current
.count());
1172 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1173 foreach (const QByteArray
& role
, current
) {
1174 if (role
!= "name") {
1175 m_additionalInfoList
.append(infoAccessor
.additionalInfo(role
));
1179 ViewProperties
props(url());
1180 props
.setAdditionalInfoList(m_additionalInfoList
);
1182 emit
additionalInfoListChanged(m_additionalInfoList
, previousAdditionalInfoList
);
1185 KFileItemModel
* DolphinView::fileItemModel() const
1187 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1190 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1192 if (!url
.isValid()) {
1193 const QString
location(url
.pathOrUrl());
1194 if (location
.isEmpty()) {
1195 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1197 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1202 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1205 void DolphinView::applyViewProperties()
1207 m_container
->beginTransaction();
1209 const ViewProperties
props(url());
1210 KFileItemModel
* model
= fileItemModel();
1212 const Mode mode
= props
.viewMode();
1213 if (m_mode
!= mode
) {
1214 const Mode previousMode
= m_mode
;
1217 // Changing the mode might result in changing
1218 // the zoom level. Remember the old zoom level so
1219 // that zoomLevelChanged() can get emitted.
1220 const int oldZoomLevel
= m_container
->zoomLevel();
1223 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1224 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1225 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1226 default: Q_ASSERT(false); break;
1229 emit
modeChanged(m_mode
, previousMode
);
1231 if (m_container
->zoomLevel() != oldZoomLevel
) {
1232 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1236 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1237 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1238 model
->setShowHiddenFiles(hiddenFilesShown
);
1239 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1242 const bool groupedSorting
= props
.groupedSorting();
1243 if (groupedSorting
!= model
->groupedSorting()) {
1244 model
->setGroupedSorting(groupedSorting
);
1245 emit
groupedSortingChanged(groupedSorting
);
1248 const DolphinView::Sorting sorting
= props
.sorting();
1249 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1250 if (newSortRole
!= model
->sortRole()) {
1251 model
->setSortRole(newSortRole
);
1252 emit
sortingChanged(sorting
);
1255 const Qt::SortOrder sortOrder
= props
.sortOrder();
1256 if (sortOrder
!= model
->sortOrder()) {
1257 model
->setSortOrder(sortOrder
);
1258 emit
sortOrderChanged(sortOrder
);
1261 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1262 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1263 model
->setSortFoldersFirst(sortFoldersFirst
);
1264 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1267 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1268 if (infoList
!= m_additionalInfoList
) {
1269 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1270 m_additionalInfoList
= infoList
;
1271 applyAdditionalInfoListToView();
1272 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1275 const bool previewsShown
= props
.previewsShown();
1276 if (previewsShown
!= m_container
->previewsShown()) {
1277 const int oldZoomLevel
= zoomLevel();
1279 m_container
->setPreviewsShown(previewsShown
);
1280 emit
previewsShownChanged(previewsShown
);
1282 // Changing the preview-state might result in a changed zoom-level
1283 if (oldZoomLevel
!= zoomLevel()) {
1284 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1288 m_container
->endTransaction();
1291 void DolphinView::applyAdditionalInfoListToView()
1293 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1295 QList
<QByteArray
> visibleRoles
;
1296 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1297 visibleRoles
.append("name");
1299 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1300 visibleRoles
.append(infoAccessor
.role(info
));
1303 m_container
->setVisibleRoles(visibleRoles
);
1306 void DolphinView::pasteToUrl(const KUrl
& url
)
1308 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1309 KonqOperations::doPaste(this, url
);
1312 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1316 const KFileItemList items
= selectedItems();
1317 foreach (const KFileItem
&item
, items
) {
1318 urls
.append(item
.url());
1321 if (itemsExpandable()) {
1322 // TODO: Check if we still need KDirModel for this in KDE 5.0
1323 urls
= KDirModel::simplifiedUrlList(urls
);
1329 QMimeData
* DolphinView::selectionMimeData() const
1331 const KFileItemModel
* model
= fileItemModel();
1332 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1333 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1335 return model
->createMimeData(selectedIndexes
);
1338 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1340 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1341 markUrlsAsSelected(urls
);
1344 void DolphinView::updateWritableState()
1346 const bool wasFolderWritable
= m_isFolderWritable
;
1347 m_isFolderWritable
= true;
1349 const KFileItem item
= m_dirLister
->rootItem();
1350 if (!item
.isNull()) {
1351 KFileItemListProperties
capabilities(KFileItemList() << item
);
1352 m_isFolderWritable
= capabilities
.supportsWriting();
1354 if (m_isFolderWritable
!= wasFolderWritable
) {
1355 emit
writeStateChanged(m_isFolderWritable
);
1359 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1362 case SortByName
: return "name";
1363 case SortBySize
: return "size";
1364 case SortByDate
: return "date";
1365 case SortByPermissions
: return "permissions";
1366 case SortByOwner
: return "owner";
1367 case SortByGroup
: return "group";
1368 case SortByType
: return "type";
1369 case SortByDestination
: return "destination";
1370 case SortByPath
: return "path";
1374 return QByteArray();
1377 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1379 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1380 if (sortHash
.isEmpty()) {
1381 sortHash
.insert("name", SortByName
);
1382 sortHash
.insert("size", SortBySize
);
1383 sortHash
.insert("date", SortByDate
);
1384 sortHash
.insert("permissions", SortByPermissions
);
1385 sortHash
.insert("owner", SortByOwner
);
1386 sortHash
.insert("group", SortByGroup
);
1387 sortHash
.insert("type", SortByType
);
1388 sortHash
.insert("destination", SortByDestination
);
1389 sortHash
.insert("path", SortByPath
);
1391 return sortHash
.value(sortRole
);
1394 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1396 const KLocale
* locale
= KGlobal::locale();
1397 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1401 if (fileSize
< multiplier
) {
1402 // Show the size in bytes
1403 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1404 } else if (fileSize
< multiplier
* multiplier
) {
1405 // Show the size in kilobytes and always round up. This is done
1406 // for consistency with the values shown e.g. in the "Size" column
1407 // of the details-view.
1408 fileSize
+= (multiplier
/ 2) - 1;
1409 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1411 // Show the size in the best fitting unit having one decimal
1412 text
= locale
->formatByteSize(fileSize
, 1);
1417 #include "dolphinview.moc"