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 <config-nepomuk.h>
25 #include <QAbstractItemView>
26 #include <QApplication>
30 #include <QGraphicsSceneDragDropEvent>
32 #include <QItemSelection>
36 #include <KActionCollection>
37 #include <KColorScheme>
39 #include <KIconEffect>
41 #include <KFileItemListProperties>
43 #include <kitemviews/kfileitemmodel.h>
44 #include <kitemviews/kfileitemlistview.h>
45 #include <kitemviews/kitemlistheader.h>
46 #include <kitemviews/kitemlistselectionmanager.h>
47 #include <kitemviews/kitemlistview.h>
48 #include <kitemviews/kitemlistcontroller.h>
49 #include <KIO/DeleteJob>
50 #include <KIO/NetAccess>
51 #include <KIO/PreviewJob>
54 #include <KMessageBox>
55 #include <konq_fileitemcapabilities.h>
56 #include <konq_operations.h>
57 #include <konqmimedata.h>
58 #include <KToggleAction>
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 #include <Nepomuk/ResourceManager>
78 const int MaxModeEnum
= DolphinView::CompactView
;
81 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
84 m_tabsForFiles(false),
85 m_assureVisibleCurrentIndex(false),
86 m_isFolderWritable(true),
89 m_mode(DolphinView::IconsView
),
94 m_selectionChangedTimer(0),
96 m_restoredContentsPosition(),
99 m_versionControlObserver(0)
101 m_topLayout
= new QVBoxLayout(this);
102 m_topLayout
->setSpacing(0);
103 m_topLayout
->setMargin(0);
105 // When a new item has been created by the "Create New..." menu, the item should
106 // get selected and it must be assured that the item will get visible. As the
107 // creation is done asynchronously, several signals must be checked:
108 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
109 this, SLOT(observeCreatedItem(KUrl
)));
111 m_selectionChangedTimer
= new QTimer(this);
112 m_selectionChangedTimer
->setSingleShot(true);
113 m_selectionChangedTimer
->setInterval(300);
114 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
115 this, SLOT(emitSelectionChangedSignal()));
117 m_container
= new DolphinItemListContainer(this);
118 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
119 m_container
->installEventFilter(this);
120 setFocusProxy(m_container
);
121 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
122 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
124 KItemListController
* controller
= m_container
->controller();
125 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
126 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
127 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
128 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
129 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
130 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
131 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
132 connect(controller
, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons
)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons
)));
133 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
134 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
135 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
136 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
138 KFileItemModel
* model
= fileItemModel();
139 connect(model
, SIGNAL(dirLoadingStarted()), this, SLOT(slotDirLoadingStarted()));
140 connect(model
, SIGNAL(dirLoadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
141 connect(model
, SIGNAL(dirLoadingProgress(int)), this, SIGNAL(dirLoadingProgress(int)));
142 connect(model
, SIGNAL(dirSortingProgress(int)), this, SIGNAL(dirSortingProgress(int)));
143 connect(model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
144 this, SLOT(slotItemsChanged()));
145 connect(model
, SIGNAL(itemsRemoved(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
146 connect(model
, SIGNAL(itemsInserted(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
147 connect(model
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
148 connect(model
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
149 connect(model
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
151 KItemListView
* view
= controller
->view();
152 view
->installEventFilter(this);
153 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
154 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
155 connect(view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
156 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
157 connect(view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
158 this, SLOT(slotVisibleRolesChangedByHeader(QList
<QByteArray
>,QList
<QByteArray
>)));
159 connect(view
->header(), SIGNAL(columnWidthChanged(QByteArray
,qreal
,qreal
)),
160 this, SLOT(slotHeaderColumnWidthChanged(QByteArray
,qreal
,qreal
)));
162 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
163 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
164 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
166 m_toolTipManager
= new ToolTipManager(this);
168 m_versionControlObserver
= new VersionControlObserver(this);
169 m_versionControlObserver
->setModel(model
);
170 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
171 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
172 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
174 applyViewProperties();
175 m_topLayout
->addWidget(m_container
);
180 DolphinView::~DolphinView()
184 KUrl
DolphinView::url() const
189 void DolphinView::setActive(bool active
)
191 if (active
== m_active
) {
197 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
202 QWidget
* viewport
= m_container
->viewport();
205 palette
.setColor(viewport
->backgroundRole(), color
);
206 viewport
->setPalette(palette
);
212 m_container
->setFocus();
214 emit
writeStateChanged(m_isFolderWritable
);
218 bool DolphinView::isActive() const
223 void DolphinView::setMode(Mode mode
)
225 if (mode
!= m_mode
) {
226 ViewProperties
props(url());
227 props
.setViewMode(mode
);
230 applyViewProperties();
234 DolphinView::Mode
DolphinView::mode() const
239 void DolphinView::setPreviewsShown(bool show
)
241 if (previewsShown() == show
) {
245 ViewProperties
props(url());
246 props
.setPreviewsShown(show
);
248 m_container
->setPreviewsShown(show
);
249 emit
previewsShownChanged(show
);
252 bool DolphinView::previewsShown() const
254 return m_container
->previewsShown();
257 void DolphinView::setHiddenFilesShown(bool show
)
259 KFileItemModel
* model
= fileItemModel();
260 if (model
->showHiddenFiles() == show
) {
264 const KFileItemList itemList
= selectedItems();
265 m_selectedUrls
.clear();
266 m_selectedUrls
= itemList
.urlList();
268 ViewProperties
props(url());
269 props
.setHiddenFilesShown(show
);
271 model
->setShowHiddenFiles(show
);
272 emit
hiddenFilesShownChanged(show
);
275 bool DolphinView::hiddenFilesShown() const
277 return fileItemModel()->showHiddenFiles();
280 void DolphinView::setGroupedSorting(bool grouped
)
282 if (grouped
== groupedSorting()) {
286 ViewProperties
props(url());
287 props
.setGroupedSorting(grouped
);
290 m_container
->controller()->model()->setGroupedSorting(grouped
);
292 emit
groupedSortingChanged(grouped
);
295 bool DolphinView::groupedSorting() const
297 return fileItemModel()->groupedSorting();
300 KFileItemList
DolphinView::items() const
303 const KFileItemModel
* model
= fileItemModel();
304 const int itemCount
= model
->count();
305 list
.reserve(itemCount
);
307 for (int i
= 0; i
< itemCount
; ++i
) {
308 list
.append(model
->fileItem(i
));
314 int DolphinView::itemsCount() const
316 return fileItemModel()->count();
319 KFileItemList
DolphinView::selectedItems() const
321 const KFileItemModel
* model
= fileItemModel();
322 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
323 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
325 KFileItemList selectedItems
;
326 QSetIterator
<int> it(selectedIndexes
);
327 while (it
.hasNext()) {
328 const int index
= it
.next();
329 selectedItems
.append(model
->fileItem(index
));
331 return selectedItems
;
334 int DolphinView::selectedItemsCount() const
336 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
337 return selectionManager
->selectedItems().count();
340 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
342 m_selectedUrls
= urls
;
345 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
347 m_currentItemUrl
= url
;
350 void DolphinView::selectItems(const QRegExp
& pattern
, bool enabled
)
352 const KItemListSelectionManager::SelectionMode mode
= enabled
353 ? KItemListSelectionManager::Select
354 : KItemListSelectionManager::Deselect
;
355 const KFileItemModel
* model
= fileItemModel();
356 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
358 for (int index
= 0; index
< model
->count(); index
++) {
359 const KFileItem item
= model
->fileItem(index
);
360 if (pattern
.exactMatch(item
.text())) {
361 // An alternative approach would be to store the matching items in a QSet<int> and
362 // select them in one go after the loop, but we'd need a new function
363 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
365 selectionManager
->setSelected(index
, 1, mode
);
370 void DolphinView::setZoomLevel(int level
)
372 const int oldZoomLevel
= zoomLevel();
373 m_container
->setZoomLevel(level
);
374 if (zoomLevel() != oldZoomLevel
) {
375 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
379 int DolphinView::zoomLevel() const
381 return m_container
->zoomLevel();
384 void DolphinView::setSortRole(const QByteArray
& role
)
386 if (role
!= sortRole()) {
387 updateSortRole(role
);
391 QByteArray
DolphinView::sortRole() const
393 const KItemModelBase
* model
= m_container
->controller()->model();
394 return model
->sortRole();
397 void DolphinView::setSortOrder(Qt::SortOrder order
)
399 if (sortOrder() != order
) {
400 updateSortOrder(order
);
404 Qt::SortOrder
DolphinView::sortOrder() const
406 KItemModelBase
* model
= fileItemModel();
407 return model
->sortOrder();
410 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
412 if (sortFoldersFirst() != foldersFirst
) {
413 updateSortFoldersFirst(foldersFirst
);
417 bool DolphinView::sortFoldersFirst() const
419 KFileItemModel
* model
= fileItemModel();
420 return model
->sortFoldersFirst();
423 void DolphinView::setVisibleRoles(const QList
<QByteArray
>& roles
)
425 const QList
<QByteArray
> previousRoles
= roles
;
427 ViewProperties
props(url());
428 props
.setVisibleRoles(roles
);
430 m_visibleRoles
= roles
;
431 m_container
->setVisibleRoles(roles
);
433 emit
visibleRolesChanged(m_visibleRoles
, previousRoles
);
436 QList
<QByteArray
> DolphinView::visibleRoles() const
438 return m_visibleRoles
;
441 void DolphinView::reload()
443 QByteArray viewState
;
444 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
445 saveState(saveStream
);
447 const KFileItemList itemList
= selectedItems();
448 m_selectedUrls
.clear();
449 m_selectedUrls
= itemList
.urlList();
452 loadDirectory(url(), true);
454 QDataStream
restoreStream(viewState
);
455 restoreState(restoreStream
);
458 void DolphinView::stopLoading()
460 fileItemModel()->cancelDirLoading();
463 void DolphinView::readSettings()
465 const int oldZoomLevel
= m_container
->zoomLevel();
467 GeneralSettings::self()->readConfig();
468 m_container
->readSettings();
469 applyViewProperties();
471 const int newZoomLevel
= m_container
->zoomLevel();
472 if (newZoomLevel
!= oldZoomLevel
) {
473 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
477 void DolphinView::writeSettings()
479 GeneralSettings::self()->writeConfig();
480 m_container
->writeSettings();
483 void DolphinView::setNameFilter(const QString
& nameFilter
)
485 fileItemModel()->setNameFilter(nameFilter
);
488 QString
DolphinView::nameFilter() const
490 return fileItemModel()->nameFilter();
493 QString
DolphinView::statusBarText() const
501 KIO::filesize_t totalFileSize
= 0;
503 if (m_container
->controller()->selectionManager()->hasSelection()) {
504 // Give a summary of the status of the selected files
505 const KFileItemList list
= selectedItems();
506 foreach (const KFileItem
& item
, list
) {
511 totalFileSize
+= item
.size();
515 if (folderCount
+ fileCount
== 1) {
516 // If only one item is selected, show the filename
517 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
519 // At least 2 items are selected
520 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
521 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
524 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
525 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
526 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
529 if (fileCount
> 0 && folderCount
> 0) {
530 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
531 foldersText
, filesText
,
532 KGlobal::locale()->formatByteSize(totalFileSize
));
533 } else if (fileCount
> 0) {
534 summary
= i18nc("@info:status files (size)", "%1 (%2)",
536 KGlobal::locale()->formatByteSize(totalFileSize
));
537 } else if (folderCount
> 0) {
538 summary
= foldersText
;
544 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
546 QList
<QAction
*> actions
;
548 if (items
.isEmpty()) {
549 const KFileItem item
= fileItemModel()->rootItem();
550 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
552 actions
= m_versionControlObserver
->actions(items
);
558 void DolphinView::setUrl(const KUrl
& url
)
564 emit
urlAboutToBeChanged(url
);
569 // It is important to clear the items from the model before
570 // applying the view properties, otherwise expensive operations
571 // might be done on the existing items although they get cleared
572 // anyhow afterwards by loadDirectory().
573 fileItemModel()->clear();
574 applyViewProperties();
577 emit
urlChanged(url
);
580 void DolphinView::selectAll()
582 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
583 selectionManager
->setSelected(0, fileItemModel()->count());
586 void DolphinView::invertSelection()
588 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
589 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
592 void DolphinView::clearSelection()
594 m_container
->controller()->selectionManager()->clearSelection();
597 void DolphinView::renameSelectedItems()
599 KFileItemList items
= selectedItems();
600 const int itemCount
= items
.count();
605 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
607 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
608 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
609 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
610 m_viewAccessor.itemView()->edit(proxyIndex);
612 RenameDialog
* dialog
= new RenameDialog(this, items
);
613 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
616 dialog
->activateWindow();
619 // Assure that the current index remains visible when KFileItemModel
620 // will notify the view about changed items (which might result in
621 // a changed sorting).
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 ViewProperties
props(url());
781 QPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
783 KItemListView
* view
= m_container
->controller()->view();
784 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
786 bool nepomukRunning
= false;
787 bool indexingEnabled
= false;
789 nepomukRunning
= (Nepomuk::ResourceManager::instance()->init() == 0);
790 if (nepomukRunning
) {
791 KConfig
config("nepomukserverrc");
792 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
797 QMenu
* groupMenu
= 0;
799 // Add all roles to the menu that can be shown or hidden by the user
800 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
801 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
802 if (info
.role
== "name") {
803 // It should not be possible to hide the "name" role
807 const QString text
= fileItemModel()->roleDescription(info
.role
);
809 if (info
.group
.isEmpty()) {
810 action
= menu
->addAction(text
);
812 if (!groupMenu
|| info
.group
!= groupName
) {
813 groupName
= info
.group
;
814 groupMenu
= menu
->addMenu(groupName
);
817 action
= groupMenu
->addAction(text
);
820 action
->setCheckable(true);
821 action
->setChecked(visibleRolesSet
.contains(info
.role
));
822 action
->setData(info
.role
);
824 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
825 (info
.requiresNepomuk
&& nepomukRunning
) ||
826 (info
.requiresIndexer
&& indexingEnabled
);
827 action
->setEnabled(enable
);
830 menu
->addSeparator();
832 QActionGroup
* widthsGroup
= new QActionGroup(menu
);
833 const bool autoColumnWidths
= props
.headerColumnWidths().isEmpty();
835 QAction
* autoAdjustWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
836 autoAdjustWidthsAction
->setCheckable(true);
837 autoAdjustWidthsAction
->setChecked(autoColumnWidths
);
838 autoAdjustWidthsAction
->setActionGroup(widthsGroup
);
840 QAction
* customWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
841 customWidthsAction
->setCheckable(true);
842 customWidthsAction
->setChecked(!autoColumnWidths
);
843 customWidthsAction
->setActionGroup(widthsGroup
);
845 QAction
* action
= menu
->exec(pos
.toPoint());
846 if (menu
&& action
) {
847 KItemListHeader
* header
= view
->header();
849 if (action
== autoAdjustWidthsAction
) {
850 // Clear the column-widths from the viewproperties and turn on
851 // the automatic resizing of the columns
852 props
.setHeaderColumnWidths(QList
<int>());
853 header
->setAutomaticColumnResizing(true);
854 } else if (action
== customWidthsAction
) {
855 // Apply the current column-widths as custom column-widths and turn
856 // off the automatic resizing of the columns
857 QList
<int> columnWidths
;
858 foreach (const QByteArray
& role
, view
->visibleRoles()) {
859 columnWidths
.append(header
->columnWidth(role
));
861 props
.setHeaderColumnWidths(columnWidths
);
862 header
->setAutomaticColumnResizing(false);
864 // Show or hide the selected role
865 const QByteArray selectedRole
= action
->data().toByteArray();
867 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
868 if (action
->isChecked()) {
869 visibleRoles
.append(selectedRole
);
871 visibleRoles
.removeOne(selectedRole
);
874 view
->setVisibleRoles(visibleRoles
);
875 props
.setVisibleRoles(visibleRoles
);
877 QList
<int> columnWidths
;
878 if (!header
->automaticColumnResizing()) {
879 foreach (const QByteArray
& role
, view
->visibleRoles()) {
880 columnWidths
.append(header
->columnWidth(role
));
883 props
.setHeaderColumnWidths(columnWidths
);
890 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
)
894 const QList
<QByteArray
> visibleRoles
= m_container
->visibleRoles();
896 ViewProperties
props(url());
897 QList
<int> columnWidths
= props
.headerColumnWidths();
898 if (columnWidths
.count() != visibleRoles
.count()) {
899 columnWidths
.clear();
900 columnWidths
.reserve(visibleRoles
.count());
901 const KItemListHeader
* header
= m_container
->controller()->view()->header();
902 foreach (const QByteArray
& role
, visibleRoles
) {
903 const int width
= header
->columnWidth(role
);
904 columnWidths
.append(width
);
908 const int roleIndex
= visibleRoles
.indexOf(role
);
909 Q_ASSERT(roleIndex
>= 0 && roleIndex
< columnWidths
.count());
910 columnWidths
[roleIndex
] = current
;
912 props
.setHeaderColumnWidths(columnWidths
);
915 void DolphinView::slotItemHovered(int index
)
917 const KFileItem item
= fileItemModel()->fileItem(index
);
919 if (GeneralSettings::showToolTips() && !m_dragging
) {
920 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
921 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
922 itemRect
.moveTo(pos
);
924 m_toolTipManager
->showToolTip(item
, itemRect
);
927 emit
requestItemInfo(item
);
930 void DolphinView::slotItemUnhovered(int index
)
934 emit
requestItemInfo(KFileItem());
937 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
940 KFileItem destItem
= fileItemModel()->fileItem(index
);
941 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
942 // Use the URL of the view as drop target if the item is no directory
944 destItem
= fileItemModel()->rootItem();
947 // The item represents a directory or desktop-file
948 destUrl
= destItem
.url();
951 QDropEvent
dropEvent(event
->pos().toPoint(),
952 event
->possibleActions(),
957 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
958 if (!error
.isEmpty()) {
959 emit
errorMessage(error
);
962 if (destUrl
== url()) {
963 // Mark the dropped urls as selected.
964 markPastedUrlsAsSelected(event
->mimeData());
968 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
971 disconnect(previous
, SIGNAL(dirLoadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
974 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
975 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
977 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
978 m_versionControlObserver
->setModel(fileItemModel
);
981 void DolphinView::slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
)
986 // Trigger the history navigation only when clicking on the viewport:
987 // Above an item the XButtons provide a simple way to select items in
988 // the singleClick mode.
989 if (buttons
& Qt::XButton1
) {
990 emit
goBackRequested();
991 } else if (buttons
& Qt::XButton2
) {
992 emit
goForwardRequested();
997 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
999 const int currentCount
= current
.count();
1000 const int previousCount
= previous
.count();
1001 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
1002 (currentCount
> 0 && previousCount
== 0);
1004 // If nothing has been selected before and something got selected (or if something
1005 // was selected before and now nothing is selected) the selectionChangedSignal must
1006 // be emitted asynchronously as fast as possible to update the edit-actions.
1007 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
1008 m_selectionChangedTimer
->start();
1011 void DolphinView::emitSelectionChangedSignal()
1013 m_selectionChangedTimer
->stop();
1014 emit
selectionChanged(selectedItems());
1017 void DolphinView::updateSortRole(const QByteArray
& role
)
1019 ViewProperties
props(url());
1020 props
.setSortRole(role
);
1022 KItemModelBase
* model
= m_container
->controller()->model();
1023 model
->setSortRole(role
);
1025 emit
sortRoleChanged(role
);
1028 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1030 ViewProperties
props(url());
1031 props
.setSortOrder(order
);
1033 KItemModelBase
* model
= fileItemModel();
1034 model
->setSortOrder(order
);
1036 emit
sortOrderChanged(order
);
1039 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1041 ViewProperties
props(url());
1042 props
.setSortFoldersFirst(foldersFirst
);
1044 KFileItemModel
* model
= fileItemModel();
1045 model
->setSortFoldersFirst(foldersFirst
);
1047 emit
sortFoldersFirstChanged(foldersFirst
);
1050 QPair
<bool, QString
> DolphinView::pasteInfo() const
1052 return KonqOperations::pasteInfo(url());
1055 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1057 m_tabsForFiles
= tabsForFiles
;
1060 bool DolphinView::isTabsForFilesEnabled() const
1062 return m_tabsForFiles
;
1065 bool DolphinView::itemsExpandable() const
1067 return m_mode
== DetailsView
;
1070 void DolphinView::restoreState(QDataStream
& stream
)
1072 // Restore the current item that had the keyboard focus
1073 stream
>> m_currentItemUrl
;
1075 // Restore the view position
1076 stream
>> m_restoredContentsPosition
;
1078 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1081 fileItemModel()->restoreExpandedUrls(urls
);
1084 void DolphinView::saveState(QDataStream
& stream
)
1086 // Save the current item that has the keyboard focus
1087 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
1088 if (currentIndex
!= -1) {
1089 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
1090 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
1091 KUrl currentItemUrl
= item
.url();
1092 stream
<< currentItemUrl
;
1097 // Save view position
1098 const qreal x
= m_container
->horizontalScrollBar()->value();
1099 const qreal y
= m_container
->verticalScrollBar()->value();
1100 stream
<< QPoint(x
, y
);
1102 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1103 stream
<< fileItemModel()->expandedUrls();
1106 KFileItem
DolphinView::rootItem() const
1108 return fileItemModel()->rootItem();
1111 void DolphinView::observeCreatedItem(const KUrl
& url
)
1113 m_createdItemUrl
= url
;
1114 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1115 // this, SLOT(selectAndScrollToCreatedItem()));
1118 void DolphinView::selectAndScrollToCreatedItem()
1120 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1121 if (dirIndex.isValid()) {
1122 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1123 QAbstractItemView* view = m_viewAccessor.itemView();
1125 view->setCurrentIndex(proxyIndex);
1129 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1130 this, SLOT(selectAndScrollToCreatedItem()));*/
1131 m_createdItemUrl
= KUrl();
1134 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1136 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1137 emit
redirection(oldUrl
, newUrl
);
1138 m_url
= newUrl
; // #186947
1142 void DolphinView::updateViewState()
1144 if (m_currentItemUrl
!= KUrl()) {
1145 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1146 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1147 if (currentIndex
!= -1) {
1148 selectionManager
->setCurrentItem(currentIndex
);
1150 selectionManager
->setCurrentItem(0);
1152 m_currentItemUrl
= KUrl();
1155 if (!m_restoredContentsPosition
.isNull()) {
1156 const int x
= m_restoredContentsPosition
.x();
1157 const int y
= m_restoredContentsPosition
.y();
1158 m_restoredContentsPosition
= QPoint();
1160 m_container
->horizontalScrollBar()->setValue(x
);
1161 m_container
->verticalScrollBar()->setValue(y
);
1164 if (!m_selectedUrls
.isEmpty()) {
1167 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1168 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1169 const KFileItemModel
* model
= fileItemModel();
1171 foreach (const KUrl
& url
, m_selectedUrls
) {
1172 const int index
= model
->index(url
);
1174 selectedItems
.insert(index
);
1178 selectionManager
->setSelectedItems(selectedItems
);
1179 m_selectedUrls
.clear();
1183 void DolphinView::hideToolTip()
1185 if (GeneralSettings::showToolTips()) {
1186 m_toolTipManager
->hideToolTip();
1190 void DolphinView::calculateItemCount(int& fileCount
,
1192 KIO::filesize_t
& totalFileSize
) const
1194 const KFileItemModel
* model
= fileItemModel();
1195 const int itemCount
= model
->count();
1196 for (int i
= 0; i
< itemCount
; ++i
) {
1197 const KFileItem item
= model
->fileItem(i
);
1202 totalFileSize
+= item
.size();
1207 void DolphinView::showHoverInformation(const KFileItem
& item
)
1209 emit
requestItemInfo(item
);
1212 void DolphinView::clearHoverInformation()
1214 emit
requestItemInfo(KFileItem());
1217 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1219 if (job
->error() == 0) {
1220 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1221 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1222 emit
errorMessage(job
->errorString());
1226 void DolphinView::slotDirLoadingStarted()
1228 // Disable the writestate temporary until it can be determined in a fast way
1229 // in DolphinView::slotLoadingCompleted()
1230 if (m_isFolderWritable
) {
1231 m_isFolderWritable
= false;
1232 emit
writeStateChanged(m_isFolderWritable
);
1235 emit
dirLoadingStarted();
1238 void DolphinView::slotDirLoadingCompleted()
1240 // Update the view-state. This has to be done asynchronously
1241 // because the view might not be in its final state yet.
1242 QTimer::singleShot(0, this, SLOT(updateViewState()));
1244 emit
dirLoadingCompleted();
1246 updateWritableState();
1249 void DolphinView::slotItemsChanged()
1251 m_assureVisibleCurrentIndex
= false;
1254 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1257 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1259 ViewProperties
props(url());
1260 props
.setSortOrder(current
);
1262 emit
sortOrderChanged(current
);
1265 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1268 Q_ASSERT(fileItemModel()->sortRole() == current
);
1270 ViewProperties
props(url());
1271 props
.setSortRole(current
);
1273 emit
sortRoleChanged(current
);
1276 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1277 const QList
<QByteArray
>& previous
)
1280 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1282 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1284 m_visibleRoles
= current
;
1286 ViewProperties
props(url());
1287 props
.setVisibleRoles(m_visibleRoles
);
1289 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1292 KFileItemModel
* DolphinView::fileItemModel() const
1294 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1297 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1299 if (!url
.isValid()) {
1300 const QString
location(url
.pathOrUrl());
1301 if (location
.isEmpty()) {
1302 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1304 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1309 KFileItemModel
* model
= fileItemModel();
1311 model
->refreshDir(url
);
1313 model
->loadDir(url
);
1317 void DolphinView::applyViewProperties()
1319 m_container
->beginTransaction();
1321 const ViewProperties
props(url());
1322 KFileItemModel
* model
= fileItemModel();
1324 const Mode mode
= props
.viewMode();
1325 if (m_mode
!= mode
) {
1326 const Mode previousMode
= m_mode
;
1329 // Changing the mode might result in changing
1330 // the zoom level. Remember the old zoom level so
1331 // that zoomLevelChanged() can get emitted.
1332 const int oldZoomLevel
= m_container
->zoomLevel();
1335 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1336 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1337 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1338 default: Q_ASSERT(false); break;
1341 emit
modeChanged(m_mode
, previousMode
);
1343 if (m_container
->zoomLevel() != oldZoomLevel
) {
1344 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1348 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1349 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1350 model
->setShowHiddenFiles(hiddenFilesShown
);
1351 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1354 const bool groupedSorting
= props
.groupedSorting();
1355 if (groupedSorting
!= model
->groupedSorting()) {
1356 model
->setGroupedSorting(groupedSorting
);
1357 emit
groupedSortingChanged(groupedSorting
);
1360 const QByteArray sortRole
= props
.sortRole();
1361 if (sortRole
!= model
->sortRole()) {
1362 model
->setSortRole(sortRole
);
1363 emit
sortRoleChanged(sortRole
);
1366 const Qt::SortOrder sortOrder
= props
.sortOrder();
1367 if (sortOrder
!= model
->sortOrder()) {
1368 model
->setSortOrder(sortOrder
);
1369 emit
sortOrderChanged(sortOrder
);
1372 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1373 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1374 model
->setSortFoldersFirst(sortFoldersFirst
);
1375 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1378 const QList
<QByteArray
> visibleRoles
= props
.visibleRoles();
1379 if (visibleRoles
!= m_visibleRoles
) {
1380 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1381 m_visibleRoles
= visibleRoles
;
1382 m_container
->setVisibleRoles(visibleRoles
);
1383 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1386 const bool previewsShown
= props
.previewsShown();
1387 if (previewsShown
!= m_container
->previewsShown()) {
1388 const int oldZoomLevel
= zoomLevel();
1390 m_container
->setPreviewsShown(previewsShown
);
1391 emit
previewsShownChanged(previewsShown
);
1393 // Changing the preview-state might result in a changed zoom-level
1394 if (oldZoomLevel
!= zoomLevel()) {
1395 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1399 KItemListView
* itemListView
= m_container
->controller()->view();
1400 if (itemListView
->isHeaderVisible()) {
1401 KItemListHeader
* header
= itemListView
->header();
1402 const QList
<int> headerColumnWidths
= props
.headerColumnWidths();
1403 const int rolesCount
= m_visibleRoles
.count();
1404 if (headerColumnWidths
.count() == rolesCount
) {
1405 header
->setAutomaticColumnResizing(false);
1407 QHash
<QByteArray
, qreal
> columnWidths
;
1408 for (int i
= 0; i
< rolesCount
; ++i
) {
1409 columnWidths
.insert(m_visibleRoles
[i
], headerColumnWidths
[i
]);
1411 header
->setColumnWidths(columnWidths
);
1413 header
->setAutomaticColumnResizing(true);
1417 m_container
->endTransaction();
1420 void DolphinView::pasteToUrl(const KUrl
& url
)
1422 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1423 KonqOperations::doPaste(this, url
);
1426 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1430 const KFileItemList items
= selectedItems();
1431 foreach (const KFileItem
&item
, items
) {
1432 urls
.append(item
.url());
1435 if (itemsExpandable()) {
1436 // TODO: Check if we still need KDirModel for this in KDE 5.0
1437 urls
= KDirModel::simplifiedUrlList(urls
);
1443 QMimeData
* DolphinView::selectionMimeData() const
1445 const KFileItemModel
* model
= fileItemModel();
1446 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1447 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1449 return model
->createMimeData(selectedIndexes
);
1452 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1454 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1455 KUrl::List destUrls
;
1456 foreach (const KUrl
& source
, sourceUrls
) {
1457 KUrl
destination(url().url() + "/" + source
.fileName());
1458 destUrls
<< destination
;
1460 markUrlsAsSelected(destUrls
);
1463 void DolphinView::updateWritableState()
1465 const bool wasFolderWritable
= m_isFolderWritable
;
1466 m_isFolderWritable
= true;
1468 const KFileItem item
= fileItemModel()->rootItem();
1469 if (!item
.isNull()) {
1470 KFileItemListProperties
capabilities(KFileItemList() << item
);
1471 m_isFolderWritable
= capabilities
.supportsWriting();
1473 if (m_isFolderWritable
!= wasFolderWritable
) {
1474 emit
writeStateChanged(m_isFolderWritable
);
1478 #include "dolphinview.moc"