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::setItemSelectionEnabled(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 void DolphinView::calculateItemCount(int& fileCount
,
495 KIO::filesize_t
& totalFileSize
) const
497 const KFileItemModel
* model
= fileItemModel();
498 const int itemCount
= model
->count();
499 for (int i
= 0; i
< itemCount
; ++i
) {
500 const KFileItem item
= model
->fileItem(i
);
505 totalFileSize
+= item
.size();
510 QString
DolphinView::statusBarText() const
518 KIO::filesize_t totalFileSize
= 0;
520 if (hasSelection()) {
521 // Give a summary of the status of the selected files
522 const KFileItemList list
= selectedItems();
523 foreach (const KFileItem
& item
, list
) {
528 totalFileSize
+= item
.size();
532 if (folderCount
+ fileCount
== 1) {
533 // If only one item is selected, show the filename
534 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
536 // At least 2 items are selected
537 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
538 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
541 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
542 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
543 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
546 if (fileCount
> 0 && folderCount
> 0) {
547 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
548 foldersText
, filesText
,
549 KGlobal::locale()->formatByteSize(totalFileSize
));
550 } else if (fileCount
> 0) {
551 summary
= i18nc("@info:status files (size)", "%1 (%2)",
553 KGlobal::locale()->formatByteSize(totalFileSize
));
554 } else if (folderCount
> 0) {
555 summary
= foldersText
;
561 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
563 QList
<QAction
*> actions
;
565 if (items
.isEmpty()) {
566 const KFileItem item
= fileItemModel()->rootItem();
567 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
569 actions
= m_versionControlObserver
->actions(items
);
575 void DolphinView::setUrl(const KUrl
& url
)
581 emit
urlAboutToBeChanged(url
);
586 // It is important to clear the items from the model before
587 // applying the view properties, otherwise expensive operations
588 // might be done on the existing items although they get cleared
589 // anyhow afterwards by loadDirectory().
590 fileItemModel()->clear();
591 applyViewProperties();
594 emit
urlChanged(url
);
597 void DolphinView::selectAll()
599 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
600 selectionManager
->setSelected(0, fileItemModel()->count());
603 void DolphinView::invertSelection()
605 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
606 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
609 void DolphinView::clearSelection()
611 m_container
->controller()->selectionManager()->clearSelection();
614 void DolphinView::renameSelectedItems()
616 KFileItemList items
= selectedItems();
617 const int itemCount
= items
.count();
622 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
624 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
625 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
626 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
627 m_viewAccessor.itemView()->edit(proxyIndex);
629 RenameDialog
* dialog
= new RenameDialog(this, items
);
630 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
633 dialog
->activateWindow();
636 // Assure that the current index remains visible when KFileItemModel
637 // will notify the view about changed items (which might result in
638 // a changed sorting).
639 m_assureVisibleCurrentIndex
= true;
642 void DolphinView::trashSelectedItems()
644 const KUrl::List list
= simplifiedSelectedUrls();
645 KonqOperations::del(this, KonqOperations::TRASH
, list
);
648 void DolphinView::deleteSelectedItems()
650 const KUrl::List list
= simplifiedSelectedUrls();
651 const bool del
= KonqOperations::askDeleteConfirmation(list
,
653 KonqOperations::DEFAULT_CONFIRMATION
,
657 KIO::Job
* job
= KIO::del(list
);
658 connect(job
, SIGNAL(result(KJob
*)),
659 this, SLOT(slotDeleteFileFinished(KJob
*)));
663 void DolphinView::cutSelectedItems()
665 QMimeData
* mimeData
= selectionMimeData();
666 KonqMimeData::addIsCutSelection(mimeData
, true);
667 QApplication::clipboard()->setMimeData(mimeData
);
670 void DolphinView::copySelectedItems()
672 QMimeData
* mimeData
= selectionMimeData();
673 QApplication::clipboard()->setMimeData(mimeData
);
676 void DolphinView::paste()
681 void DolphinView::pasteIntoFolder()
683 const KFileItemList items
= selectedItems();
684 if ((items
.count() == 1) && items
.first().isDir()) {
685 pasteToUrl(items
.first().url());
689 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
691 switch (event
->type()) {
692 case QEvent::FocusIn
:
693 if (watched
== m_container
) {
698 case QEvent::GraphicsSceneDragEnter
:
699 if (watched
== m_container
->controller()->view()) {
704 case QEvent::GraphicsSceneDragLeave
:
705 if (watched
== m_container
->controller()->view()) {
710 case QEvent::GraphicsSceneDrop
:
711 if (watched
== m_container
->controller()->view()) {
718 return QWidget::eventFilter(watched
, event
);
721 void DolphinView::wheelEvent(QWheelEvent
* event
)
723 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
724 const int numDegrees
= event
->delta() / 8;
725 const int numSteps
= numDegrees
/ 15;
727 setZoomLevel(zoomLevel() + numSteps
);
734 void DolphinView::hideEvent(QHideEvent
* event
)
737 QWidget::hideEvent(event
);
740 void DolphinView::activate()
745 void DolphinView::slotItemActivated(int index
)
747 const KFileItem item
= fileItemModel()->fileItem(index
);
748 if (!item
.isNull()) {
749 emit
itemActivated(item
);
753 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
755 Q_ASSERT(indexes
.count() >= 2);
759 KFileItemModel
* model
= fileItemModel();
760 QSetIterator
<int> it(indexes
);
761 while (it
.hasNext()) {
762 const int index
= it
.next();
763 items
.append(model
->fileItem(index
));
766 foreach (const KFileItem
& item
, items
) {
768 emit
tabRequested(item
.url());
770 emit
itemActivated(item
);
775 void DolphinView::slotItemMiddleClicked(int index
)
777 const KFileItem item
= fileItemModel()->fileItem(index
);
778 if (item
.isDir() || isTabsForFilesEnabled()) {
779 emit
tabRequested(item
.url());
783 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
785 const KFileItem item
= fileItemModel()->fileItem(index
);
786 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
789 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
791 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
794 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
796 ViewProperties
props(url());
798 QPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
800 KItemListView
* view
= m_container
->controller()->view();
801 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
803 bool nepomukRunning
= false;
804 bool indexingEnabled
= false;
806 nepomukRunning
= (Nepomuk::ResourceManager::instance()->init() == 0);
807 if (nepomukRunning
) {
808 KConfig
config("nepomukserverrc");
809 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
814 QMenu
* groupMenu
= 0;
816 // Add all roles to the menu that can be shown or hidden by the user
817 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
818 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
819 if (info
.role
== "name") {
820 // It should not be possible to hide the "name" role
824 const QString text
= fileItemModel()->roleDescription(info
.role
);
826 if (info
.group
.isEmpty()) {
827 action
= menu
->addAction(text
);
829 if (!groupMenu
|| info
.group
!= groupName
) {
830 groupName
= info
.group
;
831 groupMenu
= menu
->addMenu(groupName
);
834 action
= groupMenu
->addAction(text
);
837 action
->setCheckable(true);
838 action
->setChecked(visibleRolesSet
.contains(info
.role
));
839 action
->setData(info
.role
);
841 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
842 (info
.requiresNepomuk
&& nepomukRunning
) ||
843 (info
.requiresIndexer
&& indexingEnabled
);
844 action
->setEnabled(enable
);
847 menu
->addSeparator();
849 QActionGroup
* widthsGroup
= new QActionGroup(menu
);
850 const bool autoColumnWidths
= props
.headerColumnWidths().isEmpty();
852 QAction
* autoAdjustWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
853 autoAdjustWidthsAction
->setCheckable(true);
854 autoAdjustWidthsAction
->setChecked(autoColumnWidths
);
855 autoAdjustWidthsAction
->setActionGroup(widthsGroup
);
857 QAction
* customWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
858 customWidthsAction
->setCheckable(true);
859 customWidthsAction
->setChecked(!autoColumnWidths
);
860 customWidthsAction
->setActionGroup(widthsGroup
);
862 QAction
* action
= menu
->exec(pos
.toPoint());
863 if (menu
&& action
) {
864 KItemListHeader
* header
= view
->header();
866 if (action
== autoAdjustWidthsAction
) {
867 // Clear the column-widths from the viewproperties and turn on
868 // the automatic resizing of the columns
869 props
.setHeaderColumnWidths(QList
<int>());
870 header
->setAutomaticColumnResizing(true);
871 } else if (action
== customWidthsAction
) {
872 // Apply the current column-widths as custom column-widths and turn
873 // off the automatic resizing of the columns
874 QList
<int> columnWidths
;
875 foreach (const QByteArray
& role
, view
->visibleRoles()) {
876 columnWidths
.append(header
->columnWidth(role
));
878 props
.setHeaderColumnWidths(columnWidths
);
879 header
->setAutomaticColumnResizing(false);
881 // Show or hide the selected role
882 const QByteArray selectedRole
= action
->data().toByteArray();
884 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
885 if (action
->isChecked()) {
886 visibleRoles
.append(selectedRole
);
888 visibleRoles
.removeOne(selectedRole
);
891 view
->setVisibleRoles(visibleRoles
);
892 props
.setVisibleRoles(visibleRoles
);
894 QList
<int> columnWidths
;
895 if (!header
->automaticColumnResizing()) {
896 foreach (const QByteArray
& role
, view
->visibleRoles()) {
897 columnWidths
.append(header
->columnWidth(role
));
900 props
.setHeaderColumnWidths(columnWidths
);
907 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
)
911 const QList
<QByteArray
> visibleRoles
= m_container
->visibleRoles();
913 ViewProperties
props(url());
914 QList
<int> columnWidths
= props
.headerColumnWidths();
915 if (columnWidths
.count() != visibleRoles
.count()) {
916 columnWidths
.clear();
917 columnWidths
.reserve(visibleRoles
.count());
918 const KItemListHeader
* header
= m_container
->controller()->view()->header();
919 foreach (const QByteArray
& role
, visibleRoles
) {
920 const int width
= header
->columnWidth(role
);
921 columnWidths
.append(width
);
925 const int roleIndex
= visibleRoles
.indexOf(role
);
926 Q_ASSERT(roleIndex
>= 0 && roleIndex
< columnWidths
.count());
927 columnWidths
[roleIndex
] = current
;
929 props
.setHeaderColumnWidths(columnWidths
);
932 void DolphinView::slotItemHovered(int index
)
934 const KFileItem item
= fileItemModel()->fileItem(index
);
936 if (GeneralSettings::showToolTips() && !m_dragging
) {
937 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
938 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
939 itemRect
.moveTo(pos
);
941 m_toolTipManager
->showToolTip(item
, itemRect
);
944 emit
requestItemInfo(item
);
947 void DolphinView::slotItemUnhovered(int index
)
951 emit
requestItemInfo(KFileItem());
954 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
957 KFileItem destItem
= fileItemModel()->fileItem(index
);
958 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
959 // Use the URL of the view as drop target if the item is no directory
961 destItem
= fileItemModel()->rootItem();
964 // The item represents a directory or desktop-file
965 destUrl
= destItem
.url();
968 QDropEvent
dropEvent(event
->pos().toPoint(),
969 event
->possibleActions(),
974 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
975 if (!error
.isEmpty()) {
976 emit
errorMessage(error
);
979 if (destUrl
== url()) {
980 // Mark the dropped urls as selected.
981 markPastedUrlsAsSelected(event
->mimeData());
985 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
988 disconnect(previous
, SIGNAL(dirLoadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
991 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
992 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotDirLoadingCompleted()));
994 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
995 m_versionControlObserver
->setModel(fileItemModel
);
998 void DolphinView::slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
)
1002 if (itemIndex
< 0) {
1003 // Trigger the history navigation only when clicking on the viewport:
1004 // Above an item the XButtons provide a simple way to select items in
1005 // the singleClick mode.
1006 if (buttons
& Qt::XButton1
) {
1007 emit
goBackRequested();
1008 } else if (buttons
& Qt::XButton2
) {
1009 emit
goForwardRequested();
1014 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
1016 const int currentCount
= current
.count();
1017 const int previousCount
= previous
.count();
1018 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
1019 (currentCount
> 0 && previousCount
== 0);
1021 // If nothing has been selected before and something got selected (or if something
1022 // was selected before and now nothing is selected) the selectionChangedSignal must
1023 // be emitted asynchronously as fast as possible to update the edit-actions.
1024 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
1025 m_selectionChangedTimer
->start();
1028 void DolphinView::emitSelectionChangedSignal()
1030 m_selectionChangedTimer
->stop();
1031 emit
selectionChanged(selectedItems());
1034 void DolphinView::updateSortRole(const QByteArray
& role
)
1036 ViewProperties
props(url());
1037 props
.setSortRole(role
);
1039 KItemModelBase
* model
= m_container
->controller()->model();
1040 model
->setSortRole(role
);
1042 emit
sortRoleChanged(role
);
1045 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1047 ViewProperties
props(url());
1048 props
.setSortOrder(order
);
1050 KItemModelBase
* model
= fileItemModel();
1051 model
->setSortOrder(order
);
1053 emit
sortOrderChanged(order
);
1056 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1058 ViewProperties
props(url());
1059 props
.setSortFoldersFirst(foldersFirst
);
1061 KFileItemModel
* model
= fileItemModel();
1062 model
->setSortFoldersFirst(foldersFirst
);
1064 emit
sortFoldersFirstChanged(foldersFirst
);
1067 QPair
<bool, QString
> DolphinView::pasteInfo() const
1069 return KonqOperations::pasteInfo(url());
1072 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1074 m_tabsForFiles
= tabsForFiles
;
1077 bool DolphinView::isTabsForFilesEnabled() const
1079 return m_tabsForFiles
;
1082 bool DolphinView::itemsExpandable() const
1084 return m_mode
== DetailsView
;
1087 void DolphinView::restoreState(QDataStream
& stream
)
1089 // Restore the current item that had the keyboard focus
1090 stream
>> m_currentItemUrl
;
1092 // Restore the view position
1093 stream
>> m_restoredContentsPosition
;
1095 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1098 fileItemModel()->restoreExpandedUrls(urls
);
1101 void DolphinView::saveState(QDataStream
& stream
)
1103 // Save the current item that has the keyboard focus
1104 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
1105 if (currentIndex
!= -1) {
1106 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
1107 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
1108 KUrl currentItemUrl
= item
.url();
1109 stream
<< currentItemUrl
;
1114 // Save view position
1115 const qreal x
= m_container
->horizontalScrollBar()->value();
1116 const qreal y
= m_container
->verticalScrollBar()->value();
1117 stream
<< QPoint(x
, y
);
1119 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1120 stream
<< fileItemModel()->expandedUrls();
1123 bool DolphinView::hasSelection() const
1125 return m_container
->controller()->selectionManager()->hasSelection();
1128 KFileItem
DolphinView::rootItem() const
1130 return fileItemModel()->rootItem();
1133 void DolphinView::observeCreatedItem(const KUrl
& url
)
1135 m_createdItemUrl
= url
;
1136 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1137 // this, SLOT(selectAndScrollToCreatedItem()));
1140 void DolphinView::selectAndScrollToCreatedItem()
1142 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1143 if (dirIndex.isValid()) {
1144 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1145 QAbstractItemView* view = m_viewAccessor.itemView();
1147 view->setCurrentIndex(proxyIndex);
1151 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1152 this, SLOT(selectAndScrollToCreatedItem()));*/
1153 m_createdItemUrl
= KUrl();
1156 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1158 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1159 emit
redirection(oldUrl
, newUrl
);
1160 m_url
= newUrl
; // #186947
1164 void DolphinView::updateViewState()
1166 if (m_currentItemUrl
!= KUrl()) {
1167 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1168 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1169 if (currentIndex
!= -1) {
1170 selectionManager
->setCurrentItem(currentIndex
);
1172 selectionManager
->setCurrentItem(0);
1174 m_currentItemUrl
= KUrl();
1177 if (!m_restoredContentsPosition
.isNull()) {
1178 const int x
= m_restoredContentsPosition
.x();
1179 const int y
= m_restoredContentsPosition
.y();
1180 m_restoredContentsPosition
= QPoint();
1182 m_container
->horizontalScrollBar()->setValue(x
);
1183 m_container
->verticalScrollBar()->setValue(y
);
1186 if (!m_selectedUrls
.isEmpty()) {
1189 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1190 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1191 const KFileItemModel
* model
= fileItemModel();
1193 foreach (const KUrl
& url
, m_selectedUrls
) {
1194 const int index
= model
->index(url
);
1196 selectedItems
.insert(index
);
1200 selectionManager
->setSelectedItems(selectedItems
);
1201 m_selectedUrls
.clear();
1205 void DolphinView::hideToolTip()
1207 if (GeneralSettings::showToolTips()) {
1208 m_toolTipManager
->hideToolTip();
1212 void DolphinView::showHoverInformation(const KFileItem
& item
)
1214 emit
requestItemInfo(item
);
1217 void DolphinView::clearHoverInformation()
1219 emit
requestItemInfo(KFileItem());
1222 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1224 if (job
->error() == 0) {
1225 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1226 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1227 emit
errorMessage(job
->errorString());
1231 void DolphinView::slotDirLoadingStarted()
1233 // Disable the writestate temporary until it can be determined in a fast way
1234 // in DolphinView::slotLoadingCompleted()
1235 if (m_isFolderWritable
) {
1236 m_isFolderWritable
= false;
1237 emit
writeStateChanged(m_isFolderWritable
);
1240 emit
startedDirLoading(url());
1243 void DolphinView::slotDirLoadingCompleted()
1245 // Update the view-state. This has to be done using a Qt::QueuedConnection
1246 // because the view might not be in its final state yet.
1247 QTimer::singleShot(0, this, SLOT(updateViewState()));
1249 emit
finishedDirLoading(url());
1251 updateWritableState();
1254 void DolphinView::slotItemsChanged()
1256 m_assureVisibleCurrentIndex
= false;
1259 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1262 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1264 ViewProperties
props(url());
1265 props
.setSortOrder(current
);
1267 emit
sortOrderChanged(current
);
1270 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1273 Q_ASSERT(fileItemModel()->sortRole() == current
);
1275 ViewProperties
props(url());
1276 props
.setSortRole(current
);
1278 emit
sortRoleChanged(current
);
1281 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1282 const QList
<QByteArray
>& previous
)
1285 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1287 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1289 m_visibleRoles
= current
;
1291 ViewProperties
props(url());
1292 props
.setVisibleRoles(m_visibleRoles
);
1294 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1297 KFileItemModel
* DolphinView::fileItemModel() const
1299 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1302 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1304 if (!url
.isValid()) {
1305 const QString
location(url
.pathOrUrl());
1306 if (location
.isEmpty()) {
1307 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1309 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1314 KFileItemModel
* model
= fileItemModel();
1316 model
->refreshDir(url
);
1318 model
->loadDir(url
);
1322 void DolphinView::applyViewProperties()
1324 m_container
->beginTransaction();
1326 const ViewProperties
props(url());
1327 KFileItemModel
* model
= fileItemModel();
1329 const Mode mode
= props
.viewMode();
1330 if (m_mode
!= mode
) {
1331 const Mode previousMode
= m_mode
;
1334 // Changing the mode might result in changing
1335 // the zoom level. Remember the old zoom level so
1336 // that zoomLevelChanged() can get emitted.
1337 const int oldZoomLevel
= m_container
->zoomLevel();
1340 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1341 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1342 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1343 default: Q_ASSERT(false); break;
1346 emit
modeChanged(m_mode
, previousMode
);
1348 if (m_container
->zoomLevel() != oldZoomLevel
) {
1349 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1353 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1354 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1355 model
->setShowHiddenFiles(hiddenFilesShown
);
1356 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1359 const bool groupedSorting
= props
.groupedSorting();
1360 if (groupedSorting
!= model
->groupedSorting()) {
1361 model
->setGroupedSorting(groupedSorting
);
1362 emit
groupedSortingChanged(groupedSorting
);
1365 const QByteArray sortRole
= props
.sortRole();
1366 if (sortRole
!= model
->sortRole()) {
1367 model
->setSortRole(sortRole
);
1368 emit
sortRoleChanged(sortRole
);
1371 const Qt::SortOrder sortOrder
= props
.sortOrder();
1372 if (sortOrder
!= model
->sortOrder()) {
1373 model
->setSortOrder(sortOrder
);
1374 emit
sortOrderChanged(sortOrder
);
1377 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1378 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1379 model
->setSortFoldersFirst(sortFoldersFirst
);
1380 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1383 const QList
<QByteArray
> visibleRoles
= props
.visibleRoles();
1384 if (visibleRoles
!= m_visibleRoles
) {
1385 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1386 m_visibleRoles
= visibleRoles
;
1387 m_container
->setVisibleRoles(visibleRoles
);
1388 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1391 const bool previewsShown
= props
.previewsShown();
1392 if (previewsShown
!= m_container
->previewsShown()) {
1393 const int oldZoomLevel
= zoomLevel();
1395 m_container
->setPreviewsShown(previewsShown
);
1396 emit
previewsShownChanged(previewsShown
);
1398 // Changing the preview-state might result in a changed zoom-level
1399 if (oldZoomLevel
!= zoomLevel()) {
1400 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1404 KItemListView
* itemListView
= m_container
->controller()->view();
1405 if (itemListView
->isHeaderVisible()) {
1406 KItemListHeader
* header
= itemListView
->header();
1407 const QList
<int> headerColumnWidths
= props
.headerColumnWidths();
1408 const int rolesCount
= m_visibleRoles
.count();
1409 if (headerColumnWidths
.count() == rolesCount
) {
1410 header
->setAutomaticColumnResizing(false);
1412 QHash
<QByteArray
, qreal
> columnWidths
;
1413 for (int i
= 0; i
< rolesCount
; ++i
) {
1414 columnWidths
.insert(m_visibleRoles
[i
], headerColumnWidths
[i
]);
1416 header
->setColumnWidths(columnWidths
);
1418 header
->setAutomaticColumnResizing(true);
1422 m_container
->endTransaction();
1425 void DolphinView::pasteToUrl(const KUrl
& url
)
1427 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1428 KonqOperations::doPaste(this, url
);
1431 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1435 const KFileItemList items
= selectedItems();
1436 foreach (const KFileItem
&item
, items
) {
1437 urls
.append(item
.url());
1440 if (itemsExpandable()) {
1441 // TODO: Check if we still need KDirModel for this in KDE 5.0
1442 urls
= KDirModel::simplifiedUrlList(urls
);
1448 QMimeData
* DolphinView::selectionMimeData() const
1450 const KFileItemModel
* model
= fileItemModel();
1451 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1452 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1454 return model
->createMimeData(selectedIndexes
);
1457 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1459 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1460 KUrl::List destUrls
;
1461 foreach (const KUrl
& source
, sourceUrls
) {
1462 KUrl
destination(url().url() + "/" + source
.fileName());
1463 destUrls
<< destination
;
1465 markUrlsAsSelected(destUrls
);
1468 void DolphinView::updateWritableState()
1470 const bool wasFolderWritable
= m_isFolderWritable
;
1471 m_isFolderWritable
= true;
1473 const KFileItem item
= fileItemModel()->rootItem();
1474 if (!item
.isNull()) {
1475 KFileItemListProperties
capabilities(KFileItemList() << item
);
1476 m_isFolderWritable
= capabilities
.supportsWriting();
1478 if (m_isFolderWritable
!= wasFolderWritable
) {
1479 emit
writeStateChanged(m_isFolderWritable
);
1483 #include "dolphinview.moc"