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_viewPropertiesContext(),
92 m_mode(DolphinView::IconsView
),
99 m_selectionChangedTimer(0),
101 m_restoredContentsPosition(),
104 m_versionControlObserver(0)
106 m_topLayout
= new QVBoxLayout(this);
107 m_topLayout
->setSpacing(0);
108 m_topLayout
->setMargin(0);
110 // When a new item has been created by the "Create New..." menu, the item should
111 // get selected and it must be assured that the item will get visible. As the
112 // creation is done asynchronously, several signals must be checked:
113 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
114 this, SLOT(observeCreatedItem(KUrl
)));
116 m_selectionChangedTimer
= new QTimer(this);
117 m_selectionChangedTimer
->setSingleShot(true);
118 m_selectionChangedTimer
->setInterval(300);
119 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
120 this, SLOT(emitSelectionChangedSignal()));
122 m_model
= new KFileItemModel(this);
123 m_view
= new DolphinItemListView();
124 m_view
->setEnabledSelectionToggles(GeneralSettings::showSelectionToggle());
125 m_view
->setEnlargeSmallPreviews(GeneralSettings::enlargeSmallPreviews());
126 m_view
->setVisibleRoles(QList
<QByteArray
>() << "text");
129 KItemListController
* controller
= new KItemListController(m_model
, m_view
, this);
130 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
131 controller
->setAutoActivationDelay(delay
);
133 m_container
= new KItemListContainer(controller
, this);
134 m_container
->installEventFilter(this);
135 setFocusProxy(m_container
);
136 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
137 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
139 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
140 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
141 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
142 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
143 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
144 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
145 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
146 connect(controller
, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons
)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons
)));
147 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
148 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
149 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
150 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
152 connect(m_model
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
153 connect(m_model
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
154 connect(m_model
, SIGNAL(directoryLoadingProgress(int)), this, SIGNAL(directoryLoadingProgress(int)));
155 connect(m_model
, SIGNAL(directorySortingProgress(int)), this, SIGNAL(directorySortingProgress(int)));
156 connect(m_model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
157 this, SLOT(slotItemsChanged()));
158 connect(m_model
, SIGNAL(itemsRemoved(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
159 connect(m_model
, SIGNAL(itemsInserted(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
160 connect(m_model
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
161 connect(m_model
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
162 connect(m_model
, SIGNAL(directoryRedirection(KUrl
,KUrl
)), this, SLOT(slotDirectoryRedirection(KUrl
,KUrl
)));
164 m_view
->installEventFilter(this);
165 connect(m_view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
166 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
167 connect(m_view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
168 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
169 connect(m_view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
170 this, SLOT(slotVisibleRolesChangedByHeader(QList
<QByteArray
>,QList
<QByteArray
>)));
171 connect(m_view
, SIGNAL(roleEditingFinished(int,QByteArray
,QVariant
)),
172 this, SLOT(slotRoleEditingFinished(int,QByteArray
,QVariant
)));
173 connect(m_view
->header(), SIGNAL(columnWidthChanged(QByteArray
,qreal
,qreal
)),
174 this, SLOT(slotHeaderColumnWidthChanged(QByteArray
,qreal
,qreal
)));
176 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
177 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
178 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
180 m_toolTipManager
= new ToolTipManager(this);
182 m_versionControlObserver
= new VersionControlObserver(this);
183 m_versionControlObserver
->setModel(m_model
);
184 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
185 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
186 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
188 applyViewProperties();
189 m_topLayout
->addWidget(m_container
);
194 DolphinView::~DolphinView()
198 KUrl
DolphinView::url() const
203 void DolphinView::setActive(bool active
)
205 if (active
== m_active
) {
211 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
216 QWidget
* viewport
= m_container
->viewport();
219 palette
.setColor(viewport
->backgroundRole(), color
);
220 viewport
->setPalette(palette
);
226 m_container
->setFocus();
228 emit
writeStateChanged(m_isFolderWritable
);
232 bool DolphinView::isActive() const
237 void DolphinView::setMode(Mode mode
)
239 if (mode
!= m_mode
) {
240 ViewProperties
props(viewPropertiesUrl());
241 props
.setViewMode(mode
);
244 applyViewProperties();
248 DolphinView::Mode
DolphinView::mode() const
253 void DolphinView::setPreviewsShown(bool show
)
255 if (previewsShown() == show
) {
259 ViewProperties
props(viewPropertiesUrl());
260 props
.setPreviewsShown(show
);
262 const int oldZoomLevel
= m_view
->zoomLevel();
263 m_view
->setPreviewsShown(show
);
264 emit
previewsShownChanged(show
);
266 const int newZoomLevel
= m_view
->zoomLevel();
267 if (newZoomLevel
!= oldZoomLevel
) {
268 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
272 bool DolphinView::previewsShown() const
274 return m_view
->previewsShown();
277 void DolphinView::setHiddenFilesShown(bool show
)
279 if (m_model
->showHiddenFiles() == show
) {
283 const KFileItemList itemList
= selectedItems();
284 m_selectedUrls
.clear();
285 m_selectedUrls
= itemList
.urlList();
287 ViewProperties
props(viewPropertiesUrl());
288 props
.setHiddenFilesShown(show
);
290 m_model
->setShowHiddenFiles(show
);
291 emit
hiddenFilesShownChanged(show
);
294 bool DolphinView::hiddenFilesShown() const
296 return m_model
->showHiddenFiles();
299 void DolphinView::setGroupedSorting(bool grouped
)
301 if (grouped
== groupedSorting()) {
305 ViewProperties
props(viewPropertiesUrl());
306 props
.setGroupedSorting(grouped
);
309 m_container
->controller()->model()->setGroupedSorting(grouped
);
311 emit
groupedSortingChanged(grouped
);
314 bool DolphinView::groupedSorting() const
316 return m_model
->groupedSorting();
319 KFileItemList
DolphinView::items() const
322 const int itemCount
= m_model
->count();
323 list
.reserve(itemCount
);
325 for (int i
= 0; i
< itemCount
; ++i
) {
326 list
.append(m_model
->fileItem(i
));
332 int DolphinView::itemsCount() const
334 return m_model
->count();
337 KFileItemList
DolphinView::selectedItems() const
339 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
340 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
342 KFileItemList selectedItems
;
343 QSetIterator
<int> it(selectedIndexes
);
344 while (it
.hasNext()) {
345 const int index
= it
.next();
346 selectedItems
.append(m_model
->fileItem(index
));
348 return selectedItems
;
351 int DolphinView::selectedItemsCount() const
353 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
354 return selectionManager
->selectedItems().count();
357 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
359 m_selectedUrls
= urls
;
362 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
364 m_currentItemUrl
= url
;
367 void DolphinView::selectItems(const QRegExp
& pattern
, bool enabled
)
369 const KItemListSelectionManager::SelectionMode mode
= enabled
370 ? KItemListSelectionManager::Select
371 : KItemListSelectionManager::Deselect
;
372 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
374 for (int index
= 0; index
< m_model
->count(); index
++) {
375 const KFileItem item
= m_model
->fileItem(index
);
376 if (pattern
.exactMatch(item
.text())) {
377 // An alternative approach would be to store the matching items in a QSet<int> and
378 // select them in one go after the loop, but we'd need a new function
379 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
381 selectionManager
->setSelected(index
, 1, mode
);
386 void DolphinView::setZoomLevel(int level
)
388 const int oldZoomLevel
= zoomLevel();
389 m_view
->setZoomLevel(level
);
390 if (zoomLevel() != oldZoomLevel
) {
392 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
396 int DolphinView::zoomLevel() const
398 return m_view
->zoomLevel();
401 void DolphinView::setSortRole(const QByteArray
& role
)
403 if (role
!= sortRole()) {
404 updateSortRole(role
);
408 QByteArray
DolphinView::sortRole() const
410 const KItemModelBase
* model
= m_container
->controller()->model();
411 return model
->sortRole();
414 void DolphinView::setSortOrder(Qt::SortOrder order
)
416 if (sortOrder() != order
) {
417 updateSortOrder(order
);
421 Qt::SortOrder
DolphinView::sortOrder() const
423 return m_model
->sortOrder();
426 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
428 if (sortFoldersFirst() != foldersFirst
) {
429 updateSortFoldersFirst(foldersFirst
);
433 bool DolphinView::sortFoldersFirst() const
435 return m_model
->sortDirectoriesFirst();
438 void DolphinView::setVisibleRoles(const QList
<QByteArray
>& roles
)
440 const QList
<QByteArray
> previousRoles
= roles
;
442 ViewProperties
props(viewPropertiesUrl());
443 props
.setVisibleRoles(roles
);
445 m_visibleRoles
= roles
;
446 m_view
->setVisibleRoles(roles
);
448 emit
visibleRolesChanged(m_visibleRoles
, previousRoles
);
451 QList
<QByteArray
> DolphinView::visibleRoles() const
453 return m_visibleRoles
;
456 void DolphinView::reload()
458 QByteArray viewState
;
459 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
460 saveState(saveStream
);
462 const KFileItemList itemList
= selectedItems();
463 m_selectedUrls
.clear();
464 m_selectedUrls
= itemList
.urlList();
467 loadDirectory(url(), true);
469 QDataStream
restoreStream(viewState
);
470 restoreState(restoreStream
);
473 void DolphinView::stopLoading()
475 m_model
->cancelDirectoryLoading();
478 void DolphinView::readSettings()
480 const int oldZoomLevel
= m_view
->zoomLevel();
482 GeneralSettings::self()->readConfig();
483 m_view
->readSettings();
484 applyViewProperties();
486 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
487 m_container
->controller()->setAutoActivationDelay(delay
);
489 const int newZoomLevel
= m_view
->zoomLevel();
490 if (newZoomLevel
!= oldZoomLevel
) {
491 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
495 void DolphinView::writeSettings()
497 GeneralSettings::self()->writeConfig();
498 m_view
->writeSettings();
501 void DolphinView::setNameFilter(const QString
& nameFilter
)
503 m_model
->setNameFilter(nameFilter
);
506 QString
DolphinView::nameFilter() const
508 return m_model
->nameFilter();
511 QString
DolphinView::statusBarText() const
519 KIO::filesize_t totalFileSize
= 0;
521 if (m_container
->controller()->selectionManager()->hasSelection()) {
522 // Give a summary of the status of the selected files
523 const KFileItemList list
= selectedItems();
524 foreach (const KFileItem
& item
, list
) {
529 totalFileSize
+= item
.size();
533 if (folderCount
+ fileCount
== 1) {
534 // If only one item is selected, show the filename
535 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
537 // At least 2 items are selected
538 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
539 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
542 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
543 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
544 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
547 if (fileCount
> 0 && folderCount
> 0) {
548 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
549 foldersText
, filesText
,
550 KGlobal::locale()->formatByteSize(totalFileSize
));
551 } else if (fileCount
> 0) {
552 summary
= i18nc("@info:status files (size)", "%1 (%2)",
554 KGlobal::locale()->formatByteSize(totalFileSize
));
555 } else if (folderCount
> 0) {
556 summary
= foldersText
;
558 summary
= i18nc("@info:status", "0 Folders, 0 Files");
564 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
566 QList
<QAction
*> actions
;
568 if (items
.isEmpty()) {
569 const KFileItem item
= m_model
->rootItem();
570 if (!item
.isNull()) {
571 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
574 actions
= m_versionControlObserver
->actions(items
);
580 void DolphinView::setUrl(const KUrl
& url
)
586 emit
urlAboutToBeChanged(url
);
591 // It is important to clear the items from the model before
592 // applying the view properties, otherwise expensive operations
593 // might be done on the existing items although they get cleared
594 // anyhow afterwards by loadDirectory().
596 applyViewProperties();
599 emit
urlChanged(url
);
602 void DolphinView::selectAll()
604 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
605 selectionManager
->setSelected(0, m_model
->count());
608 void DolphinView::invertSelection()
610 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
611 selectionManager
->setSelected(0, m_model
->count(), KItemListSelectionManager::Toggle
);
614 void DolphinView::clearSelection()
616 m_container
->controller()->selectionManager()->clearSelection();
619 void DolphinView::renameSelectedItems()
621 const KFileItemList items
= selectedItems();
622 if (items
.isEmpty()) {
626 if (items
.count() == 1) {
627 const int index
= m_model
->index(items
.first());
628 m_container
->controller()->view()->editRole(index
, "text");
630 RenameDialog
* dialog
= new RenameDialog(this, items
);
631 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
634 dialog
->activateWindow();
637 // Assure that the current index remains visible when KFileItemModel
638 // will notify the view about changed items (which might result in
639 // a changed sorting).
640 m_assureVisibleCurrentIndex
= true;
643 void DolphinView::trashSelectedItems()
645 const KUrl::List list
= simplifiedSelectedUrls();
646 KonqOperations::del(this, KonqOperations::TRASH
, list
);
649 void DolphinView::deleteSelectedItems()
651 const KUrl::List list
= simplifiedSelectedUrls();
652 const bool del
= KonqOperations::askDeleteConfirmation(list
,
654 KonqOperations::DEFAULT_CONFIRMATION
,
658 KIO::Job
* job
= KIO::del(list
);
660 job
->ui()->setWindow(this);
662 connect(job
, SIGNAL(result(KJob
*)),
663 this, SLOT(slotDeleteFileFinished(KJob
*)));
667 void DolphinView::cutSelectedItems()
669 QMimeData
* mimeData
= selectionMimeData();
670 KonqMimeData::addIsCutSelection(mimeData
, true);
671 QApplication::clipboard()->setMimeData(mimeData
);
674 void DolphinView::copySelectedItems()
676 QMimeData
* mimeData
= selectionMimeData();
677 QApplication::clipboard()->setMimeData(mimeData
);
680 void DolphinView::paste()
685 void DolphinView::pasteIntoFolder()
687 const KFileItemList items
= selectedItems();
688 if ((items
.count() == 1) && items
.first().isDir()) {
689 pasteToUrl(items
.first().url());
693 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
695 switch (event
->type()) {
696 case QEvent::FocusIn
:
697 if (watched
== m_container
) {
702 case QEvent::GraphicsSceneDragEnter
:
703 if (watched
== m_view
) {
708 case QEvent::GraphicsSceneDragLeave
:
709 if (watched
== m_view
) {
714 case QEvent::GraphicsSceneDrop
:
715 if (watched
== m_view
) {
722 return QWidget::eventFilter(watched
, event
);
725 void DolphinView::wheelEvent(QWheelEvent
* event
)
727 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
728 const int numDegrees
= event
->delta() / 8;
729 const int numSteps
= numDegrees
/ 15;
731 setZoomLevel(zoomLevel() + numSteps
);
738 void DolphinView::hideEvent(QHideEvent
* event
)
741 QWidget::hideEvent(event
);
744 void DolphinView::activate()
749 void DolphinView::slotItemActivated(int index
)
751 const KFileItem item
= m_model
->fileItem(index
);
752 if (!item
.isNull()) {
753 emit
itemActivated(item
);
757 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
759 Q_ASSERT(indexes
.count() >= 2);
763 QSetIterator
<int> it(indexes
);
764 while (it
.hasNext()) {
765 const int index
= it
.next();
766 items
.append(m_model
->fileItem(index
));
769 foreach (const KFileItem
& item
, items
) {
771 emit
tabRequested(item
.url());
773 emit
itemActivated(item
);
778 void DolphinView::slotItemMiddleClicked(int index
)
780 const KFileItem item
= m_model
->fileItem(index
);
781 if (item
.isDir() || isTabsForFilesEnabled()) {
782 emit
tabRequested(item
.url());
786 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
788 const KFileItem item
= m_model
->fileItem(index
);
789 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
792 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
794 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
797 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
799 ViewProperties
props(viewPropertiesUrl());
801 QPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
803 KItemListView
* view
= m_container
->controller()->view();
804 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
806 bool nepomukRunning
= false;
807 bool indexingEnabled
= false;
809 nepomukRunning
= (Nepomuk::ResourceManager::instance()->initialized());
810 if (nepomukRunning
) {
811 KConfig
config("nepomukserverrc");
812 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
817 QMenu
* groupMenu
= 0;
819 // Add all roles to the menu that can be shown or hidden by the user
820 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
821 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
822 if (info
.role
== "text") {
823 // It should not be possible to hide the "text" role
827 const QString text
= m_model
->roleDescription(info
.role
);
829 if (info
.group
.isEmpty()) {
830 action
= menu
->addAction(text
);
832 if (!groupMenu
|| info
.group
!= groupName
) {
833 groupName
= info
.group
;
834 groupMenu
= menu
->addMenu(groupName
);
837 action
= groupMenu
->addAction(text
);
840 action
->setCheckable(true);
841 action
->setChecked(visibleRolesSet
.contains(info
.role
));
842 action
->setData(info
.role
);
844 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
845 (info
.requiresNepomuk
&& nepomukRunning
) ||
846 (info
.requiresIndexer
&& indexingEnabled
);
847 action
->setEnabled(enable
);
850 menu
->addSeparator();
852 QActionGroup
* widthsGroup
= new QActionGroup(menu
);
853 const bool autoColumnWidths
= props
.headerColumnWidths().isEmpty();
855 QAction
* autoAdjustWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
856 autoAdjustWidthsAction
->setCheckable(true);
857 autoAdjustWidthsAction
->setChecked(autoColumnWidths
);
858 autoAdjustWidthsAction
->setActionGroup(widthsGroup
);
860 QAction
* customWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
861 customWidthsAction
->setCheckable(true);
862 customWidthsAction
->setChecked(!autoColumnWidths
);
863 customWidthsAction
->setActionGroup(widthsGroup
);
865 QAction
* action
= menu
->exec(pos
.toPoint());
866 if (menu
&& action
) {
867 KItemListHeader
* header
= view
->header();
869 if (action
== autoAdjustWidthsAction
) {
870 // Clear the column-widths from the viewproperties and turn on
871 // the automatic resizing of the columns
872 props
.setHeaderColumnWidths(QList
<int>());
873 header
->setAutomaticColumnResizing(true);
874 } else if (action
== customWidthsAction
) {
875 // Apply the current column-widths as custom column-widths and turn
876 // off the automatic resizing of the columns
877 QList
<int> columnWidths
;
878 foreach (const QByteArray
& role
, view
->visibleRoles()) {
879 columnWidths
.append(header
->columnWidth(role
));
881 props
.setHeaderColumnWidths(columnWidths
);
882 header
->setAutomaticColumnResizing(false);
884 // Show or hide the selected role
885 const QByteArray selectedRole
= action
->data().toByteArray();
887 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
888 if (action
->isChecked()) {
889 visibleRoles
.append(selectedRole
);
891 visibleRoles
.removeOne(selectedRole
);
894 view
->setVisibleRoles(visibleRoles
);
895 props
.setVisibleRoles(visibleRoles
);
897 QList
<int> columnWidths
;
898 if (!header
->automaticColumnResizing()) {
899 foreach (const QByteArray
& role
, view
->visibleRoles()) {
900 columnWidths
.append(header
->columnWidth(role
));
903 props
.setHeaderColumnWidths(columnWidths
);
910 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
)
914 const QList
<QByteArray
> visibleRoles
= m_view
->visibleRoles();
916 ViewProperties
props(viewPropertiesUrl());
917 QList
<int> columnWidths
= props
.headerColumnWidths();
918 if (columnWidths
.count() != visibleRoles
.count()) {
919 columnWidths
.clear();
920 columnWidths
.reserve(visibleRoles
.count());
921 const KItemListHeader
* header
= m_view
->header();
922 foreach (const QByteArray
& role
, visibleRoles
) {
923 const int width
= header
->columnWidth(role
);
924 columnWidths
.append(width
);
928 const int roleIndex
= visibleRoles
.indexOf(role
);
929 Q_ASSERT(roleIndex
>= 0 && roleIndex
< columnWidths
.count());
930 columnWidths
[roleIndex
] = current
;
932 props
.setHeaderColumnWidths(columnWidths
);
935 void DolphinView::slotItemHovered(int index
)
937 const KFileItem item
= m_model
->fileItem(index
);
939 if (GeneralSettings::showToolTips() && !m_dragging
) {
940 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
941 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
942 itemRect
.moveTo(pos
);
944 m_toolTipManager
->showToolTip(item
, itemRect
);
947 emit
requestItemInfo(item
);
950 void DolphinView::slotItemUnhovered(int index
)
954 emit
requestItemInfo(KFileItem());
957 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
960 KFileItem destItem
= m_model
->fileItem(index
);
961 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
962 // Use the URL of the view as drop target if the item is no directory
964 destItem
= m_model
->rootItem();
967 // The item represents a directory or desktop-file
968 destUrl
= destItem
.url();
971 QDropEvent
dropEvent(event
->pos().toPoint(),
972 event
->possibleActions(),
977 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
978 if (!error
.isEmpty()) {
979 emit
errorMessage(error
);
982 if (destUrl
== url()) {
983 // Mark the dropped urls as selected.
984 markPastedUrlsAsSelected(event
->mimeData());
988 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
991 disconnect(previous
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
992 m_versionControlObserver
->setModel(0);
996 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
997 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
999 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
1000 m_versionControlObserver
->setModel(fileItemModel
);
1004 void DolphinView::slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
)
1008 if (itemIndex
< 0) {
1009 // Trigger the history navigation only when clicking on the viewport:
1010 // Above an item the XButtons provide a simple way to select items in
1011 // the singleClick mode.
1012 if (buttons
& Qt::XButton1
) {
1013 emit
goBackRequested();
1014 } else if (buttons
& Qt::XButton2
) {
1015 emit
goForwardRequested();
1020 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
1022 const int currentCount
= current
.count();
1023 const int previousCount
= previous
.count();
1024 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
1025 (currentCount
> 0 && previousCount
== 0);
1027 // If nothing has been selected before and something got selected (or if something
1028 // was selected before and now nothing is selected) the selectionChangedSignal must
1029 // be emitted asynchronously as fast as possible to update the edit-actions.
1030 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
1031 m_selectionChangedTimer
->start();
1034 void DolphinView::emitSelectionChangedSignal()
1036 m_selectionChangedTimer
->stop();
1037 emit
selectionChanged(selectedItems());
1040 void DolphinView::updateSortRole(const QByteArray
& role
)
1042 ViewProperties
props(viewPropertiesUrl());
1043 props
.setSortRole(role
);
1045 KItemModelBase
* model
= m_container
->controller()->model();
1046 model
->setSortRole(role
);
1048 emit
sortRoleChanged(role
);
1051 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1053 ViewProperties
props(viewPropertiesUrl());
1054 props
.setSortOrder(order
);
1056 m_model
->setSortOrder(order
);
1058 emit
sortOrderChanged(order
);
1061 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1063 ViewProperties
props(viewPropertiesUrl());
1064 props
.setSortFoldersFirst(foldersFirst
);
1066 m_model
->setSortDirectoriesFirst(foldersFirst
);
1068 emit
sortFoldersFirstChanged(foldersFirst
);
1071 QPair
<bool, QString
> DolphinView::pasteInfo() const
1073 return KonqOperations::pasteInfo(url());
1076 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1078 m_tabsForFiles
= tabsForFiles
;
1081 bool DolphinView::isTabsForFilesEnabled() const
1083 return m_tabsForFiles
;
1086 bool DolphinView::itemsExpandable() const
1088 return m_mode
== DetailsView
;
1091 void DolphinView::restoreState(QDataStream
& stream
)
1093 // Restore the current item that had the keyboard focus
1094 stream
>> m_currentItemUrl
;
1096 // Restore the view position
1097 stream
>> m_restoredContentsPosition
;
1099 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1102 m_model
->restoreExpandedDirectories(urls
);
1105 void DolphinView::saveState(QDataStream
& stream
)
1107 // Save the current item that has the keyboard focus
1108 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
1109 if (currentIndex
!= -1) {
1110 KFileItem item
= m_model
->fileItem(currentIndex
);
1111 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
1112 KUrl currentItemUrl
= item
.url();
1113 stream
<< currentItemUrl
;
1118 // Save view position
1119 const qreal x
= m_container
->horizontalScrollBar()->value();
1120 const qreal y
= m_container
->verticalScrollBar()->value();
1121 stream
<< QPoint(x
, y
);
1123 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1124 stream
<< m_model
->expandedDirectories();
1127 KFileItem
DolphinView::rootItem() const
1129 return m_model
->rootItem();
1132 void DolphinView::setViewPropertiesContext(const QString
& context
)
1134 m_viewPropertiesContext
= context
;
1137 QString
DolphinView::viewPropertiesContext() const
1139 return m_viewPropertiesContext
;
1142 void DolphinView::observeCreatedItem(const KUrl
& url
)
1144 m_createdItemUrl
= url
;
1145 connect(m_model
, SIGNAL(directoryLoadingCompleted()),
1146 this, SLOT(selectAndScrollToCreatedItem()));
1149 void DolphinView::selectAndScrollToCreatedItem()
1151 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1152 const int index
= m_model
->index(m_createdItemUrl
);
1154 selectionManager
->setCurrentItem(index
);
1155 selectionManager
->clearSelection();
1156 selectionManager
->setSelected(index
);
1157 m_view
->scrollToItem(index
);
1160 disconnect(m_model
, SIGNAL(directoryLoadingCompleted()),
1161 this, SLOT(selectAndScrollToCreatedItem()));
1162 m_createdItemUrl
= KUrl();
1165 void DolphinView::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1167 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1168 emit
redirection(oldUrl
, newUrl
);
1169 m_url
= newUrl
; // #186947
1173 void DolphinView::updateViewState()
1175 if (m_currentItemUrl
!= KUrl()) {
1176 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1177 const int currentIndex
= m_model
->index(m_currentItemUrl
);
1178 if (currentIndex
!= -1) {
1179 selectionManager
->setCurrentItem(currentIndex
);
1181 selectionManager
->setCurrentItem(0);
1183 m_currentItemUrl
= KUrl();
1186 if (!m_restoredContentsPosition
.isNull()) {
1187 const int x
= m_restoredContentsPosition
.x();
1188 const int y
= m_restoredContentsPosition
.y();
1189 m_restoredContentsPosition
= QPoint();
1191 m_container
->horizontalScrollBar()->setValue(x
);
1192 m_container
->verticalScrollBar()->setValue(y
);
1195 if (!m_selectedUrls
.isEmpty()) {
1198 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1199 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1201 foreach (const KUrl
& url
, m_selectedUrls
) {
1202 const int index
= m_model
->index(url
);
1204 selectedItems
.insert(index
);
1208 selectionManager
->setSelectedItems(selectedItems
);
1209 m_selectedUrls
.clear();
1213 void DolphinView::hideToolTip()
1215 if (GeneralSettings::showToolTips()) {
1216 m_toolTipManager
->hideToolTip();
1220 void DolphinView::calculateItemCount(int& fileCount
,
1222 KIO::filesize_t
& totalFileSize
) const
1224 const int itemCount
= m_model
->count();
1225 for (int i
= 0; i
< itemCount
; ++i
) {
1226 const KFileItem item
= m_model
->fileItem(i
);
1231 totalFileSize
+= item
.size();
1236 void DolphinView::showHoverInformation(const KFileItem
& item
)
1238 emit
requestItemInfo(item
);
1241 void DolphinView::clearHoverInformation()
1243 emit
requestItemInfo(KFileItem());
1246 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1248 if (job
->error() == 0) {
1249 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1250 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1251 emit
errorMessage(job
->errorString());
1255 void DolphinView::slotDirectoryLoadingStarted()
1257 // Disable the writestate temporary until it can be determined in a fast way
1258 // in DolphinView::slotLoadingCompleted()
1259 if (m_isFolderWritable
) {
1260 m_isFolderWritable
= false;
1261 emit
writeStateChanged(m_isFolderWritable
);
1264 emit
directoryLoadingStarted();
1267 void DolphinView::slotDirectoryLoadingCompleted()
1269 // Update the view-state. This has to be done asynchronously
1270 // because the view might not be in its final state yet.
1271 QTimer::singleShot(0, this, SLOT(updateViewState()));
1273 emit
directoryLoadingCompleted();
1275 updateWritableState();
1278 void DolphinView::slotItemsChanged()
1280 m_assureVisibleCurrentIndex
= false;
1283 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1286 Q_ASSERT(m_model
->sortOrder() == current
);
1288 ViewProperties
props(viewPropertiesUrl());
1289 props
.setSortOrder(current
);
1291 emit
sortOrderChanged(current
);
1294 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1297 Q_ASSERT(m_model
->sortRole() == current
);
1299 ViewProperties
props(viewPropertiesUrl());
1300 props
.setSortRole(current
);
1302 emit
sortRoleChanged(current
);
1305 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1306 const QList
<QByteArray
>& previous
)
1309 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1311 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1313 m_visibleRoles
= current
;
1315 ViewProperties
props(viewPropertiesUrl());
1316 props
.setVisibleRoles(m_visibleRoles
);
1318 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1321 void DolphinView::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
1323 if (role
== "text") {
1324 const KFileItem item
= m_model
->fileItem(index
);
1325 const QString newName
= value
.toString();
1326 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
1327 KonqOperations::rename(this, item
.url(), newName
);
1332 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1334 if (!url
.isValid()) {
1335 const QString
location(url
.pathOrUrl());
1336 if (location
.isEmpty()) {
1337 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1339 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1345 m_model
->refreshDirectory(url
);
1347 m_model
->loadDirectory(url
);
1351 void DolphinView::applyViewProperties()
1353 m_view
->beginTransaction();
1355 const ViewProperties
props(viewPropertiesUrl());
1357 const Mode mode
= props
.viewMode();
1358 if (m_mode
!= mode
) {
1359 const Mode previousMode
= m_mode
;
1362 // Changing the mode might result in changing
1363 // the zoom level. Remember the old zoom level so
1364 // that zoomLevelChanged() can get emitted.
1365 const int oldZoomLevel
= m_view
->zoomLevel();
1368 emit
modeChanged(m_mode
, previousMode
);
1370 if (m_view
->zoomLevel() != oldZoomLevel
) {
1371 emit
zoomLevelChanged(m_view
->zoomLevel(), oldZoomLevel
);
1375 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1376 if (hiddenFilesShown
!= m_model
->showHiddenFiles()) {
1377 m_model
->setShowHiddenFiles(hiddenFilesShown
);
1378 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1381 const bool groupedSorting
= props
.groupedSorting();
1382 if (groupedSorting
!= m_model
->groupedSorting()) {
1383 m_model
->setGroupedSorting(groupedSorting
);
1384 emit
groupedSortingChanged(groupedSorting
);
1387 const QByteArray sortRole
= props
.sortRole();
1388 if (sortRole
!= m_model
->sortRole()) {
1389 m_model
->setSortRole(sortRole
);
1390 emit
sortRoleChanged(sortRole
);
1393 const Qt::SortOrder sortOrder
= props
.sortOrder();
1394 if (sortOrder
!= m_model
->sortOrder()) {
1395 m_model
->setSortOrder(sortOrder
);
1396 emit
sortOrderChanged(sortOrder
);
1399 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1400 if (sortFoldersFirst
!= m_model
->sortDirectoriesFirst()) {
1401 m_model
->setSortDirectoriesFirst(sortFoldersFirst
);
1402 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1405 const QList
<QByteArray
> visibleRoles
= props
.visibleRoles();
1406 if (visibleRoles
!= m_visibleRoles
) {
1407 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1408 m_visibleRoles
= visibleRoles
;
1409 m_view
->setVisibleRoles(visibleRoles
);
1410 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1413 const bool previewsShown
= props
.previewsShown();
1414 if (previewsShown
!= m_view
->previewsShown()) {
1415 const int oldZoomLevel
= zoomLevel();
1417 m_view
->setPreviewsShown(previewsShown
);
1418 emit
previewsShownChanged(previewsShown
);
1420 // Changing the preview-state might result in a changed zoom-level
1421 if (oldZoomLevel
!= zoomLevel()) {
1422 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1426 KItemListView
* itemListView
= m_container
->controller()->view();
1427 if (itemListView
->isHeaderVisible()) {
1428 KItemListHeader
* header
= itemListView
->header();
1429 const QList
<int> headerColumnWidths
= props
.headerColumnWidths();
1430 const int rolesCount
= m_visibleRoles
.count();
1431 if (headerColumnWidths
.count() == rolesCount
) {
1432 header
->setAutomaticColumnResizing(false);
1434 QHash
<QByteArray
, qreal
> columnWidths
;
1435 for (int i
= 0; i
< rolesCount
; ++i
) {
1436 columnWidths
.insert(m_visibleRoles
[i
], headerColumnWidths
[i
]);
1438 header
->setColumnWidths(columnWidths
);
1440 header
->setAutomaticColumnResizing(true);
1444 m_view
->endTransaction();
1447 void DolphinView::applyModeToView()
1450 case IconsView
: m_view
->setItemLayout(KFileItemListView::IconsLayout
); break;
1451 case CompactView
: m_view
->setItemLayout(KFileItemListView::CompactLayout
); break;
1452 case DetailsView
: m_view
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1453 default: Q_ASSERT(false); break;
1457 void DolphinView::pasteToUrl(const KUrl
& url
)
1459 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1460 KonqOperations::doPaste(this, url
);
1463 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1467 const KFileItemList items
= selectedItems();
1468 foreach (const KFileItem
&item
, items
) {
1469 urls
.append(item
.url());
1472 if (itemsExpandable()) {
1473 // TODO: Check if we still need KDirModel for this in KDE 5.0
1474 urls
= KDirModel::simplifiedUrlList(urls
);
1480 QMimeData
* DolphinView::selectionMimeData() const
1482 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1483 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1485 return m_model
->createMimeData(selectedIndexes
);
1488 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1490 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1491 KUrl::List destUrls
;
1492 foreach (const KUrl
& source
, sourceUrls
) {
1493 KUrl
destination(url().url() + "/" + source
.fileName());
1494 destUrls
<< destination
;
1496 markUrlsAsSelected(destUrls
);
1499 void DolphinView::updateWritableState()
1501 const bool wasFolderWritable
= m_isFolderWritable
;
1502 m_isFolderWritable
= true;
1504 const KFileItem item
= m_model
->rootItem();
1505 if (!item
.isNull()) {
1506 KFileItemListProperties
capabilities(KFileItemList() << item
);
1507 m_isFolderWritable
= capabilities
.supportsWriting();
1509 if (m_isFolderWritable
!= wasFolderWritable
) {
1510 emit
writeStateChanged(m_isFolderWritable
);
1514 KUrl
DolphinView::viewPropertiesUrl() const
1516 if (m_viewPropertiesContext
.isEmpty()) {
1521 url
.setProtocol(m_url
.protocol());
1522 url
.setPath(m_viewPropertiesContext
);
1526 #include "dolphinview.moc"