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/kitemlistcontainer.h>
46 #include <kitemviews/kitemlistheader.h>
47 #include <kitemviews/kitemlistselectionmanager.h>
48 #include <kitemviews/kitemlistview.h>
49 #include <kitemviews/kitemlistcontroller.h>
50 #include <KIO/DeleteJob>
51 #include <KIO/JobUiDelegate>
52 #include <KIO/NetAccess>
53 #include <KIO/PreviewJob>
56 #include <KMessageBox>
57 #include <konq_fileitemcapabilities.h>
58 #include <konq_operations.h>
59 #include <konqmimedata.h>
60 #include <KToggleAction>
63 #include "dolphinnewfilemenuobserver.h"
64 #include "dolphin_detailsmodesettings.h"
65 #include "dolphin_generalsettings.h"
66 #include "dolphinitemlistview.h"
67 #include "draganddrophelper.h"
68 #include "renamedialog.h"
69 #include "versioncontrol/versioncontrolobserver.h"
70 #include "viewmodecontroller.h"
71 #include "viewproperties.h"
72 #include "views/tooltips/tooltipmanager.h"
73 #include "zoomlevelinfo.h"
76 #include <Nepomuk/ResourceManager>
80 const int MaxModeEnum
= DolphinView::CompactView
;
83 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
86 m_tabsForFiles(false),
87 m_assureVisibleCurrentIndex(false),
88 m_isFolderWritable(true),
91 m_mode(DolphinView::IconsView
),
98 m_selectionChangedTimer(0),
100 m_restoredContentsPosition(),
103 m_versionControlObserver(0)
105 m_topLayout
= new QVBoxLayout(this);
106 m_topLayout
->setSpacing(0);
107 m_topLayout
->setMargin(0);
109 // When a new item has been created by the "Create New..." menu, the item should
110 // get selected and it must be assured that the item will get visible. As the
111 // creation is done asynchronously, several signals must be checked:
112 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
113 this, SLOT(observeCreatedItem(KUrl
)));
115 m_selectionChangedTimer
= new QTimer(this);
116 m_selectionChangedTimer
->setSingleShot(true);
117 m_selectionChangedTimer
->setInterval(300);
118 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
119 this, SLOT(emitSelectionChangedSignal()));
121 m_model
= new KFileItemModel(this);
122 m_view
= new DolphinItemListView();
123 m_view
->setEnabledSelectionToggles(GeneralSettings::showSelectionToggle());
124 m_view
->setEnlargeSmallPreviews(GeneralSettings::enlargeSmallPreviews());
125 m_view
->setVisibleRoles(QList
<QByteArray
>() << "text");
128 KItemListController
* controller
= new KItemListController(m_model
, m_view
, this);
129 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
130 controller
->setAutoActivationDelay(delay
);
132 m_container
= new KItemListContainer(controller
, this);
133 m_container
->installEventFilter(this);
134 setFocusProxy(m_container
);
135 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
136 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
138 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
139 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
140 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
141 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
142 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
143 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
144 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
145 connect(controller
, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons
)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons
)));
146 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
147 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
148 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
149 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
151 connect(m_model
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
152 connect(m_model
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
153 connect(m_model
, SIGNAL(directoryLoadingProgress(int)), this, SIGNAL(directoryLoadingProgress(int)));
154 connect(m_model
, SIGNAL(directorySortingProgress(int)), this, SIGNAL(directorySortingProgress(int)));
155 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
156 this, SLOT(slotItemsChanged()));
157 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
158 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
159 connect(m_model
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
160 connect(m_model
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
161 connect(m_model
, SIGNAL(directoryRedirection(KUrl
,KUrl
)), this, SLOT(slotDirectoryRedirection(KUrl
,KUrl
)));
163 m_view
->installEventFilter(this);
164 connect(m_view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
165 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
166 connect(m_view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
167 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
168 connect(m_view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
169 this, SLOT(slotVisibleRolesChangedByHeader(QList
<QByteArray
>,QList
<QByteArray
>)));
170 connect(m_view
, SIGNAL(roleEditingFinished(int,QByteArray
,QVariant
)),
171 this, SLOT(slotRoleEditingFinished(int,QByteArray
,QVariant
)));
172 connect(m_view
->header(), SIGNAL(columnWidthChanged(QByteArray
,qreal
,qreal
)),
173 this, SLOT(slotHeaderColumnWidthChanged(QByteArray
,qreal
,qreal
)));
175 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
176 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
177 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
179 m_toolTipManager
= new ToolTipManager(this);
181 m_versionControlObserver
= new VersionControlObserver(this);
182 m_versionControlObserver
->setModel(m_model
);
183 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
184 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
185 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
187 applyViewProperties();
188 m_topLayout
->addWidget(m_container
);
193 DolphinView::~DolphinView()
197 KUrl
DolphinView::url() const
202 void DolphinView::setActive(bool active
)
204 if (active
== m_active
) {
210 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
215 QWidget
* viewport
= m_container
->viewport();
218 palette
.setColor(viewport
->backgroundRole(), color
);
219 viewport
->setPalette(palette
);
225 m_container
->setFocus();
227 emit
writeStateChanged(m_isFolderWritable
);
231 bool DolphinView::isActive() const
236 void DolphinView::setMode(Mode mode
)
238 if (mode
!= m_mode
) {
239 ViewProperties
props(url());
240 props
.setViewMode(mode
);
243 applyViewProperties();
247 DolphinView::Mode
DolphinView::mode() const
252 void DolphinView::setPreviewsShown(bool show
)
254 if (previewsShown() == show
) {
258 ViewProperties
props(url());
259 props
.setPreviewsShown(show
);
261 m_view
->setPreviewsShown(show
);
262 emit
previewsShownChanged(show
);
265 bool DolphinView::previewsShown() const
267 return m_view
->previewsShown();
270 void DolphinView::setHiddenFilesShown(bool show
)
272 if (m_model
->showHiddenFiles() == show
) {
276 const KFileItemList itemList
= selectedItems();
277 m_selectedUrls
.clear();
278 m_selectedUrls
= itemList
.urlList();
280 ViewProperties
props(url());
281 props
.setHiddenFilesShown(show
);
283 m_model
->setShowHiddenFiles(show
);
284 emit
hiddenFilesShownChanged(show
);
287 bool DolphinView::hiddenFilesShown() const
289 return m_model
->showHiddenFiles();
292 void DolphinView::setGroupedSorting(bool grouped
)
294 if (grouped
== groupedSorting()) {
298 ViewProperties
props(url());
299 props
.setGroupedSorting(grouped
);
302 m_container
->controller()->model()->setGroupedSorting(grouped
);
304 emit
groupedSortingChanged(grouped
);
307 bool DolphinView::groupedSorting() const
309 return m_model
->groupedSorting();
312 KFileItemList
DolphinView::items() const
315 const int itemCount
= m_model
->count();
316 list
.reserve(itemCount
);
318 for (int i
= 0; i
< itemCount
; ++i
) {
319 list
.append(m_model
->fileItem(i
));
325 int DolphinView::itemsCount() const
327 return m_model
->count();
330 KFileItemList
DolphinView::selectedItems() const
332 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
333 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
335 KFileItemList selectedItems
;
336 QSetIterator
<int> it(selectedIndexes
);
337 while (it
.hasNext()) {
338 const int index
= it
.next();
339 selectedItems
.append(m_model
->fileItem(index
));
341 return selectedItems
;
344 int DolphinView::selectedItemsCount() const
346 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
347 return selectionManager
->selectedItems().count();
350 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
352 m_selectedUrls
= urls
;
355 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
357 m_currentItemUrl
= url
;
360 void DolphinView::selectItems(const QRegExp
& pattern
, bool enabled
)
362 const KItemListSelectionManager::SelectionMode mode
= enabled
363 ? KItemListSelectionManager::Select
364 : KItemListSelectionManager::Deselect
;
365 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
367 for (int index
= 0; index
< m_model
->count(); index
++) {
368 const KFileItem item
= m_model
->fileItem(index
);
369 if (pattern
.exactMatch(item
.text())) {
370 // An alternative approach would be to store the matching items in a QSet<int> and
371 // select them in one go after the loop, but we'd need a new function
372 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
374 selectionManager
->setSelected(index
, 1, mode
);
379 void DolphinView::setZoomLevel(int level
)
381 const int oldZoomLevel
= zoomLevel();
382 m_view
->setZoomLevel(level
);
383 if (zoomLevel() != oldZoomLevel
) {
384 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
388 int DolphinView::zoomLevel() const
390 return m_view
->zoomLevel();
393 void DolphinView::setSortRole(const QByteArray
& role
)
395 if (role
!= sortRole()) {
396 updateSortRole(role
);
400 QByteArray
DolphinView::sortRole() const
402 const KItemModelBase
* model
= m_container
->controller()->model();
403 return model
->sortRole();
406 void DolphinView::setSortOrder(Qt::SortOrder order
)
408 if (sortOrder() != order
) {
409 updateSortOrder(order
);
413 Qt::SortOrder
DolphinView::sortOrder() const
415 return m_model
->sortOrder();
418 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
420 if (sortFoldersFirst() != foldersFirst
) {
421 updateSortFoldersFirst(foldersFirst
);
425 bool DolphinView::sortFoldersFirst() const
427 return m_model
->sortDirectoriesFirst();
430 void DolphinView::setVisibleRoles(const QList
<QByteArray
>& roles
)
432 const QList
<QByteArray
> previousRoles
= roles
;
434 ViewProperties
props(url());
435 props
.setVisibleRoles(roles
);
437 m_visibleRoles
= roles
;
438 m_view
->setVisibleRoles(roles
);
440 emit
visibleRolesChanged(m_visibleRoles
, previousRoles
);
443 QList
<QByteArray
> DolphinView::visibleRoles() const
445 return m_visibleRoles
;
448 void DolphinView::reload()
450 QByteArray viewState
;
451 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
452 saveState(saveStream
);
454 const KFileItemList itemList
= selectedItems();
455 m_selectedUrls
.clear();
456 m_selectedUrls
= itemList
.urlList();
459 loadDirectory(url(), true);
461 QDataStream
restoreStream(viewState
);
462 restoreState(restoreStream
);
465 void DolphinView::stopLoading()
467 m_model
->cancelDirectoryLoading();
470 void DolphinView::readSettings()
472 const int oldZoomLevel
= m_view
->zoomLevel();
474 GeneralSettings::self()->readConfig();
475 m_view
->readSettings();
476 applyViewProperties();
478 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
479 m_container
->controller()->setAutoActivationDelay(delay
);
481 const int newZoomLevel
= m_view
->zoomLevel();
482 if (newZoomLevel
!= oldZoomLevel
) {
483 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
487 void DolphinView::writeSettings()
489 GeneralSettings::self()->writeConfig();
490 m_view
->writeSettings();
493 void DolphinView::setNameFilter(const QString
& nameFilter
)
495 m_model
->setNameFilter(nameFilter
);
498 QString
DolphinView::nameFilter() const
500 return m_model
->nameFilter();
503 QString
DolphinView::statusBarText() const
511 KIO::filesize_t totalFileSize
= 0;
513 if (m_container
->controller()->selectionManager()->hasSelection()) {
514 // Give a summary of the status of the selected files
515 const KFileItemList list
= selectedItems();
516 foreach (const KFileItem
& item
, list
) {
521 totalFileSize
+= item
.size();
525 if (folderCount
+ fileCount
== 1) {
526 // If only one item is selected, show the filename
527 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
529 // At least 2 items are selected
530 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
531 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
534 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
535 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
536 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
539 if (fileCount
> 0 && folderCount
> 0) {
540 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
541 foldersText
, filesText
,
542 KGlobal::locale()->formatByteSize(totalFileSize
));
543 } else if (fileCount
> 0) {
544 summary
= i18nc("@info:status files (size)", "%1 (%2)",
546 KGlobal::locale()->formatByteSize(totalFileSize
));
547 } else if (folderCount
> 0) {
548 summary
= foldersText
;
550 summary
= i18nc("@info:status", "0 Folders, 0 Files");
556 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
558 QList
<QAction
*> actions
;
560 if (items
.isEmpty()) {
561 const KFileItem item
= m_model
->rootItem();
562 if (!item
.isNull()) {
563 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
566 actions
= m_versionControlObserver
->actions(items
);
572 void DolphinView::setUrl(const KUrl
& url
)
578 emit
urlAboutToBeChanged(url
);
583 // It is important to clear the items from the model before
584 // applying the view properties, otherwise expensive operations
585 // might be done on the existing items although they get cleared
586 // anyhow afterwards by loadDirectory().
588 applyViewProperties();
591 emit
urlChanged(url
);
594 void DolphinView::selectAll()
596 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
597 selectionManager
->setSelected(0, m_model
->count());
600 void DolphinView::invertSelection()
602 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
603 selectionManager
->setSelected(0, m_model
->count(), KItemListSelectionManager::Toggle
);
606 void DolphinView::clearSelection()
608 m_container
->controller()->selectionManager()->clearSelection();
611 void DolphinView::renameSelectedItems()
613 const KFileItemList items
= selectedItems();
614 if (items
.isEmpty()) {
618 if (items
.count() == 1) {
619 const int index
= m_model
->index(items
.first());
620 m_container
->controller()->view()->editRole(index
, "text");
622 RenameDialog
* dialog
= new RenameDialog(this, items
);
623 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
626 dialog
->activateWindow();
629 // Assure that the current index remains visible when KFileItemModel
630 // will notify the view about changed items (which might result in
631 // a changed sorting).
632 m_assureVisibleCurrentIndex
= true;
635 void DolphinView::trashSelectedItems()
637 const KUrl::List list
= simplifiedSelectedUrls();
638 KonqOperations::del(this, KonqOperations::TRASH
, list
);
641 void DolphinView::deleteSelectedItems()
643 const KUrl::List list
= simplifiedSelectedUrls();
644 const bool del
= KonqOperations::askDeleteConfirmation(list
,
646 KonqOperations::DEFAULT_CONFIRMATION
,
650 KIO::Job
* job
= KIO::del(list
);
652 job
->ui()->setWindow(this);
654 connect(job
, SIGNAL(result(KJob
*)),
655 this, SLOT(slotDeleteFileFinished(KJob
*)));
659 void DolphinView::cutSelectedItems()
661 QMimeData
* mimeData
= selectionMimeData();
662 KonqMimeData::addIsCutSelection(mimeData
, true);
663 QApplication::clipboard()->setMimeData(mimeData
);
666 void DolphinView::copySelectedItems()
668 QMimeData
* mimeData
= selectionMimeData();
669 QApplication::clipboard()->setMimeData(mimeData
);
672 void DolphinView::paste()
677 void DolphinView::pasteIntoFolder()
679 const KFileItemList items
= selectedItems();
680 if ((items
.count() == 1) && items
.first().isDir()) {
681 pasteToUrl(items
.first().url());
685 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
687 switch (event
->type()) {
688 case QEvent::FocusIn
:
689 if (watched
== m_container
) {
694 case QEvent::GraphicsSceneDragEnter
:
695 if (watched
== m_view
) {
700 case QEvent::GraphicsSceneDragLeave
:
701 if (watched
== m_view
) {
706 case QEvent::GraphicsSceneDrop
:
707 if (watched
== m_view
) {
714 return QWidget::eventFilter(watched
, event
);
717 void DolphinView::wheelEvent(QWheelEvent
* event
)
719 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
720 const int numDegrees
= event
->delta() / 8;
721 const int numSteps
= numDegrees
/ 15;
723 setZoomLevel(zoomLevel() + numSteps
);
730 void DolphinView::hideEvent(QHideEvent
* event
)
733 QWidget::hideEvent(event
);
736 void DolphinView::activate()
741 void DolphinView::slotItemActivated(int index
)
743 const KFileItem item
= m_model
->fileItem(index
);
744 if (!item
.isNull()) {
745 emit
itemActivated(item
);
749 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
751 Q_ASSERT(indexes
.count() >= 2);
755 QSetIterator
<int> it(indexes
);
756 while (it
.hasNext()) {
757 const int index
= it
.next();
758 items
.append(m_model
->fileItem(index
));
761 foreach (const KFileItem
& item
, items
) {
763 emit
tabRequested(item
.url());
765 emit
itemActivated(item
);
770 void DolphinView::slotItemMiddleClicked(int index
)
772 const KFileItem item
= m_model
->fileItem(index
);
773 if (item
.isDir() || isTabsForFilesEnabled()) {
774 emit
tabRequested(item
.url());
778 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
780 const KFileItem item
= m_model
->fileItem(index
);
781 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
784 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
786 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
789 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
791 ViewProperties
props(url());
793 QPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
795 KItemListView
* view
= m_container
->controller()->view();
796 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
798 bool nepomukRunning
= false;
799 bool indexingEnabled
= false;
801 nepomukRunning
= (Nepomuk::ResourceManager::instance()->initialized());
802 if (nepomukRunning
) {
803 KConfig
config("nepomukserverrc");
804 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
809 QMenu
* groupMenu
= 0;
811 // Add all roles to the menu that can be shown or hidden by the user
812 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
813 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
814 if (info
.role
== "text") {
815 // It should not be possible to hide the "text" role
819 const QString text
= m_model
->roleDescription(info
.role
);
821 if (info
.group
.isEmpty()) {
822 action
= menu
->addAction(text
);
824 if (!groupMenu
|| info
.group
!= groupName
) {
825 groupName
= info
.group
;
826 groupMenu
= menu
->addMenu(groupName
);
829 action
= groupMenu
->addAction(text
);
832 action
->setCheckable(true);
833 action
->setChecked(visibleRolesSet
.contains(info
.role
));
834 action
->setData(info
.role
);
836 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
837 (info
.requiresNepomuk
&& nepomukRunning
) ||
838 (info
.requiresIndexer
&& indexingEnabled
);
839 action
->setEnabled(enable
);
842 menu
->addSeparator();
844 QActionGroup
* widthsGroup
= new QActionGroup(menu
);
845 const bool autoColumnWidths
= props
.headerColumnWidths().isEmpty();
847 QAction
* autoAdjustWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
848 autoAdjustWidthsAction
->setCheckable(true);
849 autoAdjustWidthsAction
->setChecked(autoColumnWidths
);
850 autoAdjustWidthsAction
->setActionGroup(widthsGroup
);
852 QAction
* customWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
853 customWidthsAction
->setCheckable(true);
854 customWidthsAction
->setChecked(!autoColumnWidths
);
855 customWidthsAction
->setActionGroup(widthsGroup
);
857 QAction
* action
= menu
->exec(pos
.toPoint());
858 if (menu
&& action
) {
859 KItemListHeader
* header
= view
->header();
861 if (action
== autoAdjustWidthsAction
) {
862 // Clear the column-widths from the viewproperties and turn on
863 // the automatic resizing of the columns
864 props
.setHeaderColumnWidths(QList
<int>());
865 header
->setAutomaticColumnResizing(true);
866 } else if (action
== customWidthsAction
) {
867 // Apply the current column-widths as custom column-widths and turn
868 // off the automatic resizing of the columns
869 QList
<int> columnWidths
;
870 foreach (const QByteArray
& role
, view
->visibleRoles()) {
871 columnWidths
.append(header
->columnWidth(role
));
873 props
.setHeaderColumnWidths(columnWidths
);
874 header
->setAutomaticColumnResizing(false);
876 // Show or hide the selected role
877 const QByteArray selectedRole
= action
->data().toByteArray();
879 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
880 if (action
->isChecked()) {
881 visibleRoles
.append(selectedRole
);
883 visibleRoles
.removeOne(selectedRole
);
886 view
->setVisibleRoles(visibleRoles
);
887 props
.setVisibleRoles(visibleRoles
);
889 QList
<int> columnWidths
;
890 if (!header
->automaticColumnResizing()) {
891 foreach (const QByteArray
& role
, view
->visibleRoles()) {
892 columnWidths
.append(header
->columnWidth(role
));
895 props
.setHeaderColumnWidths(columnWidths
);
902 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
)
906 const QList
<QByteArray
> visibleRoles
= m_view
->visibleRoles();
908 ViewProperties
props(url());
909 QList
<int> columnWidths
= props
.headerColumnWidths();
910 if (columnWidths
.count() != visibleRoles
.count()) {
911 columnWidths
.clear();
912 columnWidths
.reserve(visibleRoles
.count());
913 const KItemListHeader
* header
= m_view
->header();
914 foreach (const QByteArray
& role
, visibleRoles
) {
915 const int width
= header
->columnWidth(role
);
916 columnWidths
.append(width
);
920 const int roleIndex
= visibleRoles
.indexOf(role
);
921 Q_ASSERT(roleIndex
>= 0 && roleIndex
< columnWidths
.count());
922 columnWidths
[roleIndex
] = current
;
924 props
.setHeaderColumnWidths(columnWidths
);
927 void DolphinView::slotItemHovered(int index
)
929 const KFileItem item
= m_model
->fileItem(index
);
931 if (GeneralSettings::showToolTips() && !m_dragging
) {
932 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
933 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
934 itemRect
.moveTo(pos
);
936 m_toolTipManager
->showToolTip(item
, itemRect
);
939 emit
requestItemInfo(item
);
942 void DolphinView::slotItemUnhovered(int index
)
946 emit
requestItemInfo(KFileItem());
949 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
952 KFileItem destItem
= m_model
->fileItem(index
);
953 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
954 // Use the URL of the view as drop target if the item is no directory
956 destItem
= m_model
->rootItem();
959 // The item represents a directory or desktop-file
960 destUrl
= destItem
.url();
963 QDropEvent
dropEvent(event
->pos().toPoint(),
964 event
->possibleActions(),
969 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
970 if (!error
.isEmpty()) {
971 emit
errorMessage(error
);
974 if (destUrl
== url()) {
975 // Mark the dropped urls as selected.
976 markPastedUrlsAsSelected(event
->mimeData());
980 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
983 disconnect(previous
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
984 m_versionControlObserver
->setModel(0);
988 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
989 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
991 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
992 m_versionControlObserver
->setModel(fileItemModel
);
996 void DolphinView::slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
)
1000 if (itemIndex
< 0) {
1001 // Trigger the history navigation only when clicking on the viewport:
1002 // Above an item the XButtons provide a simple way to select items in
1003 // the singleClick mode.
1004 if (buttons
& Qt::XButton1
) {
1005 emit
goBackRequested();
1006 } else if (buttons
& Qt::XButton2
) {
1007 emit
goForwardRequested();
1012 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
1014 const int currentCount
= current
.count();
1015 const int previousCount
= previous
.count();
1016 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
1017 (currentCount
> 0 && previousCount
== 0);
1019 // If nothing has been selected before and something got selected (or if something
1020 // was selected before and now nothing is selected) the selectionChangedSignal must
1021 // be emitted asynchronously as fast as possible to update the edit-actions.
1022 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
1023 m_selectionChangedTimer
->start();
1026 void DolphinView::emitSelectionChangedSignal()
1028 m_selectionChangedTimer
->stop();
1029 emit
selectionChanged(selectedItems());
1032 void DolphinView::updateSortRole(const QByteArray
& role
)
1034 ViewProperties
props(url());
1035 props
.setSortRole(role
);
1037 KItemModelBase
* model
= m_container
->controller()->model();
1038 model
->setSortRole(role
);
1040 emit
sortRoleChanged(role
);
1043 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1045 ViewProperties
props(url());
1046 props
.setSortOrder(order
);
1048 m_model
->setSortOrder(order
);
1050 emit
sortOrderChanged(order
);
1053 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1055 ViewProperties
props(url());
1056 props
.setSortFoldersFirst(foldersFirst
);
1058 m_model
->setSortDirectoriesFirst(foldersFirst
);
1060 emit
sortFoldersFirstChanged(foldersFirst
);
1063 QPair
<bool, QString
> DolphinView::pasteInfo() const
1065 return KonqOperations::pasteInfo(url());
1068 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1070 m_tabsForFiles
= tabsForFiles
;
1073 bool DolphinView::isTabsForFilesEnabled() const
1075 return m_tabsForFiles
;
1078 bool DolphinView::itemsExpandable() const
1080 return m_mode
== DetailsView
;
1083 void DolphinView::restoreState(QDataStream
& stream
)
1085 // Restore the current item that had the keyboard focus
1086 stream
>> m_currentItemUrl
;
1088 // Restore the view position
1089 stream
>> m_restoredContentsPosition
;
1091 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1094 m_model
->restoreExpandedDirectories(urls
);
1097 void DolphinView::saveState(QDataStream
& stream
)
1099 // Save the current item that has the keyboard focus
1100 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
1101 if (currentIndex
!= -1) {
1102 KFileItem item
= m_model
->fileItem(currentIndex
);
1103 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
1104 KUrl currentItemUrl
= item
.url();
1105 stream
<< currentItemUrl
;
1110 // Save view position
1111 const qreal x
= m_container
->horizontalScrollBar()->value();
1112 const qreal y
= m_container
->verticalScrollBar()->value();
1113 stream
<< QPoint(x
, y
);
1115 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1116 stream
<< m_model
->expandedDirectories();
1119 KFileItem
DolphinView::rootItem() const
1121 return m_model
->rootItem();
1124 void DolphinView::observeCreatedItem(const KUrl
& url
)
1126 m_createdItemUrl
= url
;
1127 connect(m_model
, SIGNAL(directoryLoadingCompleted()),
1128 this, SLOT(selectAndScrollToCreatedItem()));
1131 void DolphinView::selectAndScrollToCreatedItem()
1133 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1134 const int index
= m_model
->index(m_createdItemUrl
);
1136 selectionManager
->setCurrentItem(index
);
1137 selectionManager
->clearSelection();
1138 selectionManager
->setSelected(index
);
1139 m_view
->scrollToItem(index
);
1142 disconnect(m_model
, SIGNAL(directoryLoadingCompleted()),
1143 this, SLOT(selectAndScrollToCreatedItem()));
1144 m_createdItemUrl
= KUrl();
1147 void DolphinView::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1149 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1150 emit
redirection(oldUrl
, newUrl
);
1151 m_url
= newUrl
; // #186947
1155 void DolphinView::updateViewState()
1157 if (m_currentItemUrl
!= KUrl()) {
1158 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1159 const int currentIndex
= m_model
->index(m_currentItemUrl
);
1160 if (currentIndex
!= -1) {
1161 selectionManager
->setCurrentItem(currentIndex
);
1163 selectionManager
->setCurrentItem(0);
1165 m_currentItemUrl
= KUrl();
1168 if (!m_restoredContentsPosition
.isNull()) {
1169 const int x
= m_restoredContentsPosition
.x();
1170 const int y
= m_restoredContentsPosition
.y();
1171 m_restoredContentsPosition
= QPoint();
1173 m_container
->horizontalScrollBar()->setValue(x
);
1174 m_container
->verticalScrollBar()->setValue(y
);
1177 if (!m_selectedUrls
.isEmpty()) {
1180 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1181 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1183 foreach (const KUrl
& url
, m_selectedUrls
) {
1184 const int index
= m_model
->index(url
);
1186 selectedItems
.insert(index
);
1190 selectionManager
->setSelectedItems(selectedItems
);
1191 m_selectedUrls
.clear();
1195 void DolphinView::hideToolTip()
1197 if (GeneralSettings::showToolTips()) {
1198 m_toolTipManager
->hideToolTip();
1202 void DolphinView::calculateItemCount(int& fileCount
,
1204 KIO::filesize_t
& totalFileSize
) const
1206 const int itemCount
= m_model
->count();
1207 for (int i
= 0; i
< itemCount
; ++i
) {
1208 const KFileItem item
= m_model
->fileItem(i
);
1213 totalFileSize
+= item
.size();
1218 void DolphinView::showHoverInformation(const KFileItem
& item
)
1220 emit
requestItemInfo(item
);
1223 void DolphinView::clearHoverInformation()
1225 emit
requestItemInfo(KFileItem());
1228 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1230 if (job
->error() == 0) {
1231 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1232 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1233 emit
errorMessage(job
->errorString());
1237 void DolphinView::slotDirectoryLoadingStarted()
1239 // Disable the writestate temporary until it can be determined in a fast way
1240 // in DolphinView::slotLoadingCompleted()
1241 if (m_isFolderWritable
) {
1242 m_isFolderWritable
= false;
1243 emit
writeStateChanged(m_isFolderWritable
);
1246 emit
directoryLoadingStarted();
1249 void DolphinView::slotDirectoryLoadingCompleted()
1251 // Update the view-state. This has to be done asynchronously
1252 // because the view might not be in its final state yet.
1253 QTimer::singleShot(0, this, SLOT(updateViewState()));
1255 emit
directoryLoadingCompleted();
1257 updateWritableState();
1260 void DolphinView::slotItemsChanged()
1262 m_assureVisibleCurrentIndex
= false;
1265 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1268 Q_ASSERT(m_model
->sortOrder() == current
);
1270 ViewProperties
props(url());
1271 props
.setSortOrder(current
);
1273 emit
sortOrderChanged(current
);
1276 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1279 Q_ASSERT(m_model
->sortRole() == current
);
1281 ViewProperties
props(url());
1282 props
.setSortRole(current
);
1284 emit
sortRoleChanged(current
);
1287 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1288 const QList
<QByteArray
>& previous
)
1291 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1293 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1295 m_visibleRoles
= current
;
1297 ViewProperties
props(url());
1298 props
.setVisibleRoles(m_visibleRoles
);
1300 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1303 void DolphinView::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
1305 if (role
== "text") {
1306 const KFileItem item
= m_model
->fileItem(index
);
1307 const QString newName
= value
.toString();
1308 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
1309 KonqOperations::rename(this, item
.url(), newName
);
1314 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1316 if (!url
.isValid()) {
1317 const QString
location(url
.pathOrUrl());
1318 if (location
.isEmpty()) {
1319 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1321 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1327 m_model
->refreshDirectory(url
);
1329 m_model
->loadDirectory(url
);
1333 void DolphinView::applyViewProperties()
1335 m_view
->beginTransaction();
1337 const ViewProperties
props(url());
1339 const Mode mode
= props
.viewMode();
1340 if (m_mode
!= mode
) {
1341 const Mode previousMode
= m_mode
;
1344 // Changing the mode might result in changing
1345 // the zoom level. Remember the old zoom level so
1346 // that zoomLevelChanged() can get emitted.
1347 const int oldZoomLevel
= m_view
->zoomLevel();
1350 emit
modeChanged(m_mode
, previousMode
);
1352 if (m_view
->zoomLevel() != oldZoomLevel
) {
1353 emit
zoomLevelChanged(m_view
->zoomLevel(), oldZoomLevel
);
1357 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1358 if (hiddenFilesShown
!= m_model
->showHiddenFiles()) {
1359 m_model
->setShowHiddenFiles(hiddenFilesShown
);
1360 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1363 const bool groupedSorting
= props
.groupedSorting();
1364 if (groupedSorting
!= m_model
->groupedSorting()) {
1365 m_model
->setGroupedSorting(groupedSorting
);
1366 emit
groupedSortingChanged(groupedSorting
);
1369 const QByteArray sortRole
= props
.sortRole();
1370 if (sortRole
!= m_model
->sortRole()) {
1371 m_model
->setSortRole(sortRole
);
1372 emit
sortRoleChanged(sortRole
);
1375 const Qt::SortOrder sortOrder
= props
.sortOrder();
1376 if (sortOrder
!= m_model
->sortOrder()) {
1377 m_model
->setSortOrder(sortOrder
);
1378 emit
sortOrderChanged(sortOrder
);
1381 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1382 if (sortFoldersFirst
!= m_model
->sortDirectoriesFirst()) {
1383 m_model
->setSortDirectoriesFirst(sortFoldersFirst
);
1384 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1387 const QList
<QByteArray
> visibleRoles
= props
.visibleRoles();
1388 if (visibleRoles
!= m_visibleRoles
) {
1389 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1390 m_visibleRoles
= visibleRoles
;
1391 m_view
->setVisibleRoles(visibleRoles
);
1392 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1395 const bool previewsShown
= props
.previewsShown();
1396 if (previewsShown
!= m_view
->previewsShown()) {
1397 const int oldZoomLevel
= zoomLevel();
1399 m_view
->setPreviewsShown(previewsShown
);
1400 emit
previewsShownChanged(previewsShown
);
1402 // Changing the preview-state might result in a changed zoom-level
1403 if (oldZoomLevel
!= zoomLevel()) {
1404 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1408 KItemListView
* itemListView
= m_container
->controller()->view();
1409 if (itemListView
->isHeaderVisible()) {
1410 KItemListHeader
* header
= itemListView
->header();
1411 const QList
<int> headerColumnWidths
= props
.headerColumnWidths();
1412 const int rolesCount
= m_visibleRoles
.count();
1413 if (headerColumnWidths
.count() == rolesCount
) {
1414 header
->setAutomaticColumnResizing(false);
1416 QHash
<QByteArray
, qreal
> columnWidths
;
1417 for (int i
= 0; i
< rolesCount
; ++i
) {
1418 columnWidths
.insert(m_visibleRoles
[i
], headerColumnWidths
[i
]);
1420 header
->setColumnWidths(columnWidths
);
1422 header
->setAutomaticColumnResizing(true);
1426 m_view
->endTransaction();
1429 void DolphinView::applyModeToView()
1432 case IconsView
: m_view
->setItemLayout(KFileItemListView::IconsLayout
); break;
1433 case CompactView
: m_view
->setItemLayout(KFileItemListView::CompactLayout
); break;
1434 case DetailsView
: m_view
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1435 default: Q_ASSERT(false); break;
1439 void DolphinView::pasteToUrl(const KUrl
& url
)
1441 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1442 KonqOperations::doPaste(this, url
);
1445 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1449 const KFileItemList items
= selectedItems();
1450 foreach (const KFileItem
&item
, items
) {
1451 urls
.append(item
.url());
1454 if (itemsExpandable()) {
1455 // TODO: Check if we still need KDirModel for this in KDE 5.0
1456 urls
= KDirModel::simplifiedUrlList(urls
);
1462 QMimeData
* DolphinView::selectionMimeData() const
1464 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1465 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1467 return m_model
->createMimeData(selectedIndexes
);
1470 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1472 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1473 KUrl::List destUrls
;
1474 foreach (const KUrl
& source
, sourceUrls
) {
1475 KUrl
destination(url().url() + "/" + source
.fileName());
1476 destUrls
<< destination
;
1478 markUrlsAsSelected(destUrls
);
1481 void DolphinView::updateWritableState()
1483 const bool wasFolderWritable
= m_isFolderWritable
;
1484 m_isFolderWritable
= true;
1486 const KFileItem item
= m_model
->rootItem();
1487 if (!item
.isNull()) {
1488 KFileItemListProperties
capabilities(KFileItemList() << item
);
1489 m_isFolderWritable
= capabilities
.supportsWriting();
1491 if (m_isFolderWritable
!= wasFolderWritable
) {
1492 emit
writeStateChanged(m_isFolderWritable
);
1496 #include "dolphinview.moc"