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
)));
163 connect(m_model
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
165 m_view
->installEventFilter(this);
166 connect(m_view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
167 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
168 connect(m_view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
169 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
170 connect(m_view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
171 this, SLOT(slotVisibleRolesChangedByHeader(QList
<QByteArray
>,QList
<QByteArray
>)));
172 connect(m_view
, SIGNAL(roleEditingFinished(int,QByteArray
,QVariant
)),
173 this, SLOT(slotRoleEditingFinished(int,QByteArray
,QVariant
)));
174 connect(m_view
->header(), SIGNAL(columnWidthChanged(QByteArray
,qreal
,qreal
)),
175 this, SLOT(slotHeaderColumnWidthChanged(QByteArray
,qreal
,qreal
)));
177 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
178 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
179 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
181 m_toolTipManager
= new ToolTipManager(this);
183 m_versionControlObserver
= new VersionControlObserver(this);
184 m_versionControlObserver
->setModel(m_model
);
185 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
186 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
187 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
189 applyViewProperties();
190 m_topLayout
->addWidget(m_container
);
195 DolphinView::~DolphinView()
199 KUrl
DolphinView::url() const
204 void DolphinView::setActive(bool active
)
206 if (active
== m_active
) {
212 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
217 QWidget
* viewport
= m_container
->viewport();
220 palette
.setColor(viewport
->backgroundRole(), color
);
221 viewport
->setPalette(palette
);
227 m_container
->setFocus();
229 emit
writeStateChanged(m_isFolderWritable
);
233 bool DolphinView::isActive() const
238 void DolphinView::setMode(Mode mode
)
240 if (mode
!= m_mode
) {
241 ViewProperties
props(viewPropertiesUrl());
242 props
.setViewMode(mode
);
245 applyViewProperties();
249 DolphinView::Mode
DolphinView::mode() const
254 void DolphinView::setPreviewsShown(bool show
)
256 if (previewsShown() == show
) {
260 ViewProperties
props(viewPropertiesUrl());
261 props
.setPreviewsShown(show
);
263 const int oldZoomLevel
= m_view
->zoomLevel();
264 m_view
->setPreviewsShown(show
);
265 emit
previewsShownChanged(show
);
267 const int newZoomLevel
= m_view
->zoomLevel();
268 if (newZoomLevel
!= oldZoomLevel
) {
269 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
273 bool DolphinView::previewsShown() const
275 return m_view
->previewsShown();
278 void DolphinView::setHiddenFilesShown(bool show
)
280 if (m_model
->showHiddenFiles() == show
) {
284 const KFileItemList itemList
= selectedItems();
285 m_selectedUrls
.clear();
286 m_selectedUrls
= itemList
.urlList();
288 ViewProperties
props(viewPropertiesUrl());
289 props
.setHiddenFilesShown(show
);
291 m_model
->setShowHiddenFiles(show
);
292 emit
hiddenFilesShownChanged(show
);
295 bool DolphinView::hiddenFilesShown() const
297 return m_model
->showHiddenFiles();
300 void DolphinView::setGroupedSorting(bool grouped
)
302 if (grouped
== groupedSorting()) {
306 ViewProperties
props(viewPropertiesUrl());
307 props
.setGroupedSorting(grouped
);
310 m_container
->controller()->model()->setGroupedSorting(grouped
);
312 emit
groupedSortingChanged(grouped
);
315 bool DolphinView::groupedSorting() const
317 return m_model
->groupedSorting();
320 KFileItemList
DolphinView::items() const
323 const int itemCount
= m_model
->count();
324 list
.reserve(itemCount
);
326 for (int i
= 0; i
< itemCount
; ++i
) {
327 list
.append(m_model
->fileItem(i
));
333 int DolphinView::itemsCount() const
335 return m_model
->count();
338 KFileItemList
DolphinView::selectedItems() const
340 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
341 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
343 KFileItemList selectedItems
;
344 QSetIterator
<int> it(selectedIndexes
);
345 while (it
.hasNext()) {
346 const int index
= it
.next();
347 selectedItems
.append(m_model
->fileItem(index
));
349 return selectedItems
;
352 int DolphinView::selectedItemsCount() const
354 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
355 return selectionManager
->selectedItems().count();
358 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
360 m_selectedUrls
= urls
;
363 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
365 m_currentItemUrl
= url
;
368 void DolphinView::selectItems(const QRegExp
& pattern
, bool enabled
)
370 const KItemListSelectionManager::SelectionMode mode
= enabled
371 ? KItemListSelectionManager::Select
372 : KItemListSelectionManager::Deselect
;
373 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
375 for (int index
= 0; index
< m_model
->count(); index
++) {
376 const KFileItem item
= m_model
->fileItem(index
);
377 if (pattern
.exactMatch(item
.text())) {
378 // An alternative approach would be to store the matching items in a QSet<int> and
379 // select them in one go after the loop, but we'd need a new function
380 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
382 selectionManager
->setSelected(index
, 1, mode
);
387 void DolphinView::setZoomLevel(int level
)
389 const int oldZoomLevel
= zoomLevel();
390 m_view
->setZoomLevel(level
);
391 if (zoomLevel() != oldZoomLevel
) {
393 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
397 int DolphinView::zoomLevel() const
399 return m_view
->zoomLevel();
402 void DolphinView::setSortRole(const QByteArray
& role
)
404 if (role
!= sortRole()) {
405 updateSortRole(role
);
409 QByteArray
DolphinView::sortRole() const
411 const KItemModelBase
* model
= m_container
->controller()->model();
412 return model
->sortRole();
415 void DolphinView::setSortOrder(Qt::SortOrder order
)
417 if (sortOrder() != order
) {
418 updateSortOrder(order
);
422 Qt::SortOrder
DolphinView::sortOrder() const
424 return m_model
->sortOrder();
427 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
429 if (sortFoldersFirst() != foldersFirst
) {
430 updateSortFoldersFirst(foldersFirst
);
434 bool DolphinView::sortFoldersFirst() const
436 return m_model
->sortDirectoriesFirst();
439 void DolphinView::setVisibleRoles(const QList
<QByteArray
>& roles
)
441 const QList
<QByteArray
> previousRoles
= roles
;
443 ViewProperties
props(viewPropertiesUrl());
444 props
.setVisibleRoles(roles
);
446 m_visibleRoles
= roles
;
447 m_view
->setVisibleRoles(roles
);
449 emit
visibleRolesChanged(m_visibleRoles
, previousRoles
);
452 QList
<QByteArray
> DolphinView::visibleRoles() const
454 return m_visibleRoles
;
457 void DolphinView::reload()
459 QByteArray viewState
;
460 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
461 saveState(saveStream
);
463 const KFileItemList itemList
= selectedItems();
464 m_selectedUrls
.clear();
465 m_selectedUrls
= itemList
.urlList();
468 loadDirectory(url(), true);
470 QDataStream
restoreStream(viewState
);
471 restoreState(restoreStream
);
474 void DolphinView::stopLoading()
476 m_model
->cancelDirectoryLoading();
479 void DolphinView::readSettings()
481 const int oldZoomLevel
= m_view
->zoomLevel();
483 GeneralSettings::self()->readConfig();
484 m_view
->readSettings();
485 applyViewProperties();
487 const int delay
= GeneralSettings::autoExpandFolders() ? 750 : -1;
488 m_container
->controller()->setAutoActivationDelay(delay
);
490 const int newZoomLevel
= m_view
->zoomLevel();
491 if (newZoomLevel
!= oldZoomLevel
) {
492 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
496 void DolphinView::writeSettings()
498 GeneralSettings::self()->writeConfig();
499 m_view
->writeSettings();
502 void DolphinView::setNameFilter(const QString
& nameFilter
)
504 m_model
->setNameFilter(nameFilter
);
507 QString
DolphinView::nameFilter() const
509 return m_model
->nameFilter();
512 QString
DolphinView::statusBarText() const
520 KIO::filesize_t totalFileSize
= 0;
522 if (m_container
->controller()->selectionManager()->hasSelection()) {
523 // Give a summary of the status of the selected files
524 const KFileItemList list
= selectedItems();
525 foreach (const KFileItem
& item
, list
) {
530 totalFileSize
+= item
.size();
534 if (folderCount
+ fileCount
== 1) {
535 // If only one item is selected, show the filename
536 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
538 // At least 2 items are selected
539 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
540 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
543 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
544 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
545 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
548 if (fileCount
> 0 && folderCount
> 0) {
549 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
550 foldersText
, filesText
,
551 KGlobal::locale()->formatByteSize(totalFileSize
));
552 } else if (fileCount
> 0) {
553 summary
= i18nc("@info:status files (size)", "%1 (%2)",
555 KGlobal::locale()->formatByteSize(totalFileSize
));
556 } else if (folderCount
> 0) {
557 summary
= foldersText
;
559 summary
= i18nc("@info:status", "0 Folders, 0 Files");
565 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
567 QList
<QAction
*> actions
;
569 if (items
.isEmpty()) {
570 const KFileItem item
= m_model
->rootItem();
571 if (!item
.isNull()) {
572 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
575 actions
= m_versionControlObserver
->actions(items
);
581 void DolphinView::setUrl(const KUrl
& url
)
587 emit
urlAboutToBeChanged(url
);
592 // It is important to clear the items from the model before
593 // applying the view properties, otherwise expensive operations
594 // might be done on the existing items although they get cleared
595 // anyhow afterwards by loadDirectory().
597 applyViewProperties();
600 emit
urlChanged(url
);
603 void DolphinView::selectAll()
605 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
606 selectionManager
->setSelected(0, m_model
->count());
609 void DolphinView::invertSelection()
611 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
612 selectionManager
->setSelected(0, m_model
->count(), KItemListSelectionManager::Toggle
);
615 void DolphinView::clearSelection()
617 m_container
->controller()->selectionManager()->clearSelection();
620 void DolphinView::renameSelectedItems()
622 const KFileItemList items
= selectedItems();
623 if (items
.isEmpty()) {
627 if (items
.count() == 1) {
628 const int index
= m_model
->index(items
.first());
629 m_view
->editRole(index
, "text");
631 RenameDialog
* dialog
= new RenameDialog(this, items
);
632 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
635 dialog
->activateWindow();
638 // Assure that the current index remains visible when KFileItemModel
639 // will notify the view about changed items (which might result in
640 // a changed sorting).
641 m_assureVisibleCurrentIndex
= true;
644 void DolphinView::trashSelectedItems()
646 const KUrl::List list
= simplifiedSelectedUrls();
647 KonqOperations::del(this, KonqOperations::TRASH
, list
);
650 void DolphinView::deleteSelectedItems()
652 const KUrl::List list
= simplifiedSelectedUrls();
653 const bool del
= KonqOperations::askDeleteConfirmation(list
,
655 KonqOperations::DEFAULT_CONFIRMATION
,
659 KIO::Job
* job
= KIO::del(list
);
661 job
->ui()->setWindow(this);
663 connect(job
, SIGNAL(result(KJob
*)),
664 this, SLOT(slotDeleteFileFinished(KJob
*)));
668 void DolphinView::cutSelectedItems()
670 QMimeData
* mimeData
= selectionMimeData();
671 KonqMimeData::addIsCutSelection(mimeData
, true);
672 QApplication::clipboard()->setMimeData(mimeData
);
675 void DolphinView::copySelectedItems()
677 QMimeData
* mimeData
= selectionMimeData();
678 QApplication::clipboard()->setMimeData(mimeData
);
681 void DolphinView::paste()
686 void DolphinView::pasteIntoFolder()
688 const KFileItemList items
= selectedItems();
689 if ((items
.count() == 1) && items
.first().isDir()) {
690 pasteToUrl(items
.first().url());
694 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
696 switch (event
->type()) {
697 case QEvent::FocusIn
:
698 if (watched
== m_container
) {
703 case QEvent::GraphicsSceneDragEnter
:
704 if (watched
== m_view
) {
709 case QEvent::GraphicsSceneDragLeave
:
710 if (watched
== m_view
) {
715 case QEvent::GraphicsSceneDrop
:
716 if (watched
== m_view
) {
723 return QWidget::eventFilter(watched
, event
);
726 void DolphinView::wheelEvent(QWheelEvent
* event
)
728 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
729 const int numDegrees
= event
->delta() / 8;
730 const int numSteps
= numDegrees
/ 15;
732 setZoomLevel(zoomLevel() + numSteps
);
739 void DolphinView::hideEvent(QHideEvent
* event
)
742 QWidget::hideEvent(event
);
745 void DolphinView::activate()
750 void DolphinView::slotItemActivated(int index
)
752 const KFileItem item
= m_model
->fileItem(index
);
753 if (!item
.isNull()) {
754 emit
itemActivated(item
);
758 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
760 Q_ASSERT(indexes
.count() >= 2);
764 QSetIterator
<int> it(indexes
);
765 while (it
.hasNext()) {
766 const int index
= it
.next();
767 items
.append(m_model
->fileItem(index
));
770 foreach (const KFileItem
& item
, items
) {
772 emit
tabRequested(item
.url());
774 emit
itemActivated(item
);
779 void DolphinView::slotItemMiddleClicked(int index
)
781 const KFileItem item
= m_model
->fileItem(index
);
782 if (item
.isDir() || isTabsForFilesEnabled()) {
783 emit
tabRequested(item
.url());
787 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
789 const KFileItem item
= m_model
->fileItem(index
);
790 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
793 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
795 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
798 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
800 ViewProperties
props(viewPropertiesUrl());
802 QPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
804 KItemListView
* view
= m_container
->controller()->view();
805 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
807 bool nepomukRunning
= false;
808 bool indexingEnabled
= false;
810 nepomukRunning
= (Nepomuk::ResourceManager::instance()->initialized());
811 if (nepomukRunning
) {
812 KConfig
config("nepomukserverrc");
813 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
818 QMenu
* groupMenu
= 0;
820 // Add all roles to the menu that can be shown or hidden by the user
821 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
822 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
823 if (info
.role
== "text") {
824 // It should not be possible to hide the "text" role
828 const QString text
= m_model
->roleDescription(info
.role
);
830 if (info
.group
.isEmpty()) {
831 action
= menu
->addAction(text
);
833 if (!groupMenu
|| info
.group
!= groupName
) {
834 groupName
= info
.group
;
835 groupMenu
= menu
->addMenu(groupName
);
838 action
= groupMenu
->addAction(text
);
841 action
->setCheckable(true);
842 action
->setChecked(visibleRolesSet
.contains(info
.role
));
843 action
->setData(info
.role
);
845 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
846 (info
.requiresNepomuk
&& nepomukRunning
) ||
847 (info
.requiresIndexer
&& indexingEnabled
);
848 action
->setEnabled(enable
);
851 menu
->addSeparator();
853 QActionGroup
* widthsGroup
= new QActionGroup(menu
);
854 const bool autoColumnWidths
= props
.headerColumnWidths().isEmpty();
856 QAction
* autoAdjustWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
857 autoAdjustWidthsAction
->setCheckable(true);
858 autoAdjustWidthsAction
->setChecked(autoColumnWidths
);
859 autoAdjustWidthsAction
->setActionGroup(widthsGroup
);
861 QAction
* customWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
862 customWidthsAction
->setCheckable(true);
863 customWidthsAction
->setChecked(!autoColumnWidths
);
864 customWidthsAction
->setActionGroup(widthsGroup
);
866 QAction
* action
= menu
->exec(pos
.toPoint());
867 if (menu
&& action
) {
868 KItemListHeader
* header
= view
->header();
870 if (action
== autoAdjustWidthsAction
) {
871 // Clear the column-widths from the viewproperties and turn on
872 // the automatic resizing of the columns
873 props
.setHeaderColumnWidths(QList
<int>());
874 header
->setAutomaticColumnResizing(true);
875 } else if (action
== customWidthsAction
) {
876 // Apply the current column-widths as custom column-widths and turn
877 // off the automatic resizing of the columns
878 QList
<int> columnWidths
;
879 foreach (const QByteArray
& role
, view
->visibleRoles()) {
880 columnWidths
.append(header
->columnWidth(role
));
882 props
.setHeaderColumnWidths(columnWidths
);
883 header
->setAutomaticColumnResizing(false);
885 // Show or hide the selected role
886 const QByteArray selectedRole
= action
->data().toByteArray();
888 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
889 if (action
->isChecked()) {
890 visibleRoles
.append(selectedRole
);
892 visibleRoles
.removeOne(selectedRole
);
895 view
->setVisibleRoles(visibleRoles
);
896 props
.setVisibleRoles(visibleRoles
);
898 QList
<int> columnWidths
;
899 if (!header
->automaticColumnResizing()) {
900 foreach (const QByteArray
& role
, view
->visibleRoles()) {
901 columnWidths
.append(header
->columnWidth(role
));
904 props
.setHeaderColumnWidths(columnWidths
);
911 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
)
915 const QList
<QByteArray
> visibleRoles
= m_view
->visibleRoles();
917 ViewProperties
props(viewPropertiesUrl());
918 QList
<int> columnWidths
= props
.headerColumnWidths();
919 if (columnWidths
.count() != visibleRoles
.count()) {
920 columnWidths
.clear();
921 columnWidths
.reserve(visibleRoles
.count());
922 const KItemListHeader
* header
= m_view
->header();
923 foreach (const QByteArray
& role
, visibleRoles
) {
924 const int width
= header
->columnWidth(role
);
925 columnWidths
.append(width
);
929 const int roleIndex
= visibleRoles
.indexOf(role
);
930 Q_ASSERT(roleIndex
>= 0 && roleIndex
< columnWidths
.count());
931 columnWidths
[roleIndex
] = current
;
933 props
.setHeaderColumnWidths(columnWidths
);
936 void DolphinView::slotItemHovered(int index
)
938 const KFileItem item
= m_model
->fileItem(index
);
940 if (GeneralSettings::showToolTips() && !m_dragging
) {
941 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
942 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
943 itemRect
.moveTo(pos
);
945 m_toolTipManager
->showToolTip(item
, itemRect
);
948 emit
requestItemInfo(item
);
951 void DolphinView::slotItemUnhovered(int index
)
955 emit
requestItemInfo(KFileItem());
958 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
961 KFileItem destItem
= m_model
->fileItem(index
);
962 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
963 // Use the URL of the view as drop target if the item is no directory
965 destItem
= m_model
->rootItem();
968 // The item represents a directory or desktop-file
969 destUrl
= destItem
.url();
972 QDropEvent
dropEvent(event
->pos().toPoint(),
973 event
->possibleActions(),
978 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
979 if (!error
.isEmpty()) {
980 emit
errorMessage(error
);
983 if (destUrl
== url()) {
984 // Mark the dropped urls as selected.
985 markPastedUrlsAsSelected(event
->mimeData());
989 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
992 disconnect(previous
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
993 m_versionControlObserver
->setModel(0);
997 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
998 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
1000 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
1001 m_versionControlObserver
->setModel(fileItemModel
);
1005 void DolphinView::slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
)
1009 if (itemIndex
< 0) {
1010 // Trigger the history navigation only when clicking on the viewport:
1011 // Above an item the XButtons provide a simple way to select items in
1012 // the singleClick mode.
1013 if (buttons
& Qt::XButton1
) {
1014 emit
goBackRequested();
1015 } else if (buttons
& Qt::XButton2
) {
1016 emit
goForwardRequested();
1021 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
1023 const int currentCount
= current
.count();
1024 const int previousCount
= previous
.count();
1025 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
1026 (currentCount
> 0 && previousCount
== 0);
1028 // If nothing has been selected before and something got selected (or if something
1029 // was selected before and now nothing is selected) the selectionChangedSignal must
1030 // be emitted asynchronously as fast as possible to update the edit-actions.
1031 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
1032 m_selectionChangedTimer
->start();
1035 void DolphinView::emitSelectionChangedSignal()
1037 m_selectionChangedTimer
->stop();
1038 emit
selectionChanged(selectedItems());
1041 void DolphinView::updateSortRole(const QByteArray
& role
)
1043 ViewProperties
props(viewPropertiesUrl());
1044 props
.setSortRole(role
);
1046 KItemModelBase
* model
= m_container
->controller()->model();
1047 model
->setSortRole(role
);
1049 emit
sortRoleChanged(role
);
1052 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1054 ViewProperties
props(viewPropertiesUrl());
1055 props
.setSortOrder(order
);
1057 m_model
->setSortOrder(order
);
1059 emit
sortOrderChanged(order
);
1062 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1064 ViewProperties
props(viewPropertiesUrl());
1065 props
.setSortFoldersFirst(foldersFirst
);
1067 m_model
->setSortDirectoriesFirst(foldersFirst
);
1069 emit
sortFoldersFirstChanged(foldersFirst
);
1072 QPair
<bool, QString
> DolphinView::pasteInfo() const
1074 return KonqOperations::pasteInfo(url());
1077 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1079 m_tabsForFiles
= tabsForFiles
;
1082 bool DolphinView::isTabsForFilesEnabled() const
1084 return m_tabsForFiles
;
1087 bool DolphinView::itemsExpandable() const
1089 return m_mode
== DetailsView
;
1092 void DolphinView::restoreState(QDataStream
& stream
)
1094 // Restore the current item that had the keyboard focus
1095 stream
>> m_currentItemUrl
;
1097 // Restore the view position
1098 stream
>> m_restoredContentsPosition
;
1100 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1103 m_model
->restoreExpandedDirectories(urls
);
1106 void DolphinView::saveState(QDataStream
& stream
)
1108 // Save the current item that has the keyboard focus
1109 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
1110 if (currentIndex
!= -1) {
1111 KFileItem item
= m_model
->fileItem(currentIndex
);
1112 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
1113 KUrl currentItemUrl
= item
.url();
1114 stream
<< currentItemUrl
;
1119 // Save view position
1120 const qreal x
= m_container
->horizontalScrollBar()->value();
1121 const qreal y
= m_container
->verticalScrollBar()->value();
1122 stream
<< QPoint(x
, y
);
1124 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1125 stream
<< m_model
->expandedDirectories();
1128 KFileItem
DolphinView::rootItem() const
1130 return m_model
->rootItem();
1133 void DolphinView::setViewPropertiesContext(const QString
& context
)
1135 m_viewPropertiesContext
= context
;
1138 QString
DolphinView::viewPropertiesContext() const
1140 return m_viewPropertiesContext
;
1143 void DolphinView::observeCreatedItem(const KUrl
& url
)
1145 m_createdItemUrl
= url
;
1146 connect(m_model
, SIGNAL(directoryLoadingCompleted()),
1147 this, SLOT(selectAndScrollToCreatedItem()));
1150 void DolphinView::selectAndScrollToCreatedItem()
1152 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1153 const int index
= m_model
->index(m_createdItemUrl
);
1155 selectionManager
->setCurrentItem(index
);
1156 selectionManager
->clearSelection();
1157 selectionManager
->setSelected(index
);
1158 m_view
->scrollToItem(index
);
1161 disconnect(m_model
, SIGNAL(directoryLoadingCompleted()),
1162 this, SLOT(selectAndScrollToCreatedItem()));
1163 m_createdItemUrl
= KUrl();
1166 void DolphinView::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1168 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1169 emit
redirection(oldUrl
, newUrl
);
1170 m_url
= newUrl
; // #186947
1174 void DolphinView::updateViewState()
1176 if (m_currentItemUrl
!= KUrl()) {
1177 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1178 const int currentIndex
= m_model
->index(m_currentItemUrl
);
1179 if (currentIndex
!= -1) {
1180 selectionManager
->setCurrentItem(currentIndex
);
1182 selectionManager
->setCurrentItem(0);
1184 m_currentItemUrl
= KUrl();
1187 if (!m_restoredContentsPosition
.isNull()) {
1188 const int x
= m_restoredContentsPosition
.x();
1189 const int y
= m_restoredContentsPosition
.y();
1190 m_restoredContentsPosition
= QPoint();
1192 m_container
->horizontalScrollBar()->setValue(x
);
1193 m_container
->verticalScrollBar()->setValue(y
);
1196 if (!m_selectedUrls
.isEmpty()) {
1199 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1200 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1202 foreach (const KUrl
& url
, m_selectedUrls
) {
1203 const int index
= m_model
->index(url
);
1205 selectedItems
.insert(index
);
1209 selectionManager
->setSelectedItems(selectedItems
);
1210 m_selectedUrls
.clear();
1214 void DolphinView::hideToolTip()
1216 if (GeneralSettings::showToolTips()) {
1217 m_toolTipManager
->hideToolTip();
1221 void DolphinView::calculateItemCount(int& fileCount
,
1223 KIO::filesize_t
& totalFileSize
) const
1225 const int itemCount
= m_model
->count();
1226 for (int i
= 0; i
< itemCount
; ++i
) {
1227 const KFileItem item
= m_model
->fileItem(i
);
1232 totalFileSize
+= item
.size();
1237 void DolphinView::showHoverInformation(const KFileItem
& item
)
1239 emit
requestItemInfo(item
);
1242 void DolphinView::clearHoverInformation()
1244 emit
requestItemInfo(KFileItem());
1247 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1249 if (job
->error() == 0) {
1250 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1251 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1252 emit
errorMessage(job
->errorString());
1256 void DolphinView::slotDirectoryLoadingStarted()
1258 // Disable the writestate temporary until it can be determined in a fast way
1259 // in DolphinView::slotLoadingCompleted()
1260 if (m_isFolderWritable
) {
1261 m_isFolderWritable
= false;
1262 emit
writeStateChanged(m_isFolderWritable
);
1265 emit
directoryLoadingStarted();
1268 void DolphinView::slotDirectoryLoadingCompleted()
1270 // Update the view-state. This has to be done asynchronously
1271 // because the view might not be in its final state yet.
1272 QTimer::singleShot(0, this, SLOT(updateViewState()));
1274 emit
directoryLoadingCompleted();
1276 updateWritableState();
1279 void DolphinView::slotItemsChanged()
1281 m_assureVisibleCurrentIndex
= false;
1284 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1287 Q_ASSERT(m_model
->sortOrder() == current
);
1289 ViewProperties
props(viewPropertiesUrl());
1290 props
.setSortOrder(current
);
1292 emit
sortOrderChanged(current
);
1295 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1298 Q_ASSERT(m_model
->sortRole() == current
);
1300 ViewProperties
props(viewPropertiesUrl());
1301 props
.setSortRole(current
);
1303 emit
sortRoleChanged(current
);
1306 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1307 const QList
<QByteArray
>& previous
)
1310 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1312 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1314 m_visibleRoles
= current
;
1316 ViewProperties
props(viewPropertiesUrl());
1317 props
.setVisibleRoles(m_visibleRoles
);
1319 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1322 void DolphinView::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
1324 if (role
== "text") {
1325 const KFileItem oldItem
= m_model
->fileItem(index
);
1326 const QString newName
= value
.toString();
1327 if (!newName
.isEmpty() && newName
!= oldItem
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
1328 const KUrl oldUrl
= oldItem
.url();
1330 QHash
<QByteArray
, QVariant
> data
;
1331 data
.insert(role
, value
);
1332 m_model
->setData(index
, data
);
1334 KonqOperations::rename(this, oldUrl
, newName
);
1339 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1341 if (!url
.isValid()) {
1342 const QString
location(url
.pathOrUrl());
1343 if (location
.isEmpty()) {
1344 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1346 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1352 m_model
->refreshDirectory(url
);
1354 m_model
->loadDirectory(url
);
1358 void DolphinView::applyViewProperties()
1360 m_view
->beginTransaction();
1362 const ViewProperties
props(viewPropertiesUrl());
1364 const Mode mode
= props
.viewMode();
1365 if (m_mode
!= mode
) {
1366 const Mode previousMode
= m_mode
;
1369 // Changing the mode might result in changing
1370 // the zoom level. Remember the old zoom level so
1371 // that zoomLevelChanged() can get emitted.
1372 const int oldZoomLevel
= m_view
->zoomLevel();
1375 emit
modeChanged(m_mode
, previousMode
);
1377 if (m_view
->zoomLevel() != oldZoomLevel
) {
1378 emit
zoomLevelChanged(m_view
->zoomLevel(), oldZoomLevel
);
1382 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1383 if (hiddenFilesShown
!= m_model
->showHiddenFiles()) {
1384 m_model
->setShowHiddenFiles(hiddenFilesShown
);
1385 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1388 const bool groupedSorting
= props
.groupedSorting();
1389 if (groupedSorting
!= m_model
->groupedSorting()) {
1390 m_model
->setGroupedSorting(groupedSorting
);
1391 emit
groupedSortingChanged(groupedSorting
);
1394 const QByteArray sortRole
= props
.sortRole();
1395 if (sortRole
!= m_model
->sortRole()) {
1396 m_model
->setSortRole(sortRole
);
1397 emit
sortRoleChanged(sortRole
);
1400 const Qt::SortOrder sortOrder
= props
.sortOrder();
1401 if (sortOrder
!= m_model
->sortOrder()) {
1402 m_model
->setSortOrder(sortOrder
);
1403 emit
sortOrderChanged(sortOrder
);
1406 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1407 if (sortFoldersFirst
!= m_model
->sortDirectoriesFirst()) {
1408 m_model
->setSortDirectoriesFirst(sortFoldersFirst
);
1409 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1412 const QList
<QByteArray
> visibleRoles
= props
.visibleRoles();
1413 if (visibleRoles
!= m_visibleRoles
) {
1414 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1415 m_visibleRoles
= visibleRoles
;
1416 m_view
->setVisibleRoles(visibleRoles
);
1417 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1420 const bool previewsShown
= props
.previewsShown();
1421 if (previewsShown
!= m_view
->previewsShown()) {
1422 const int oldZoomLevel
= zoomLevel();
1424 m_view
->setPreviewsShown(previewsShown
);
1425 emit
previewsShownChanged(previewsShown
);
1427 // Changing the preview-state might result in a changed zoom-level
1428 if (oldZoomLevel
!= zoomLevel()) {
1429 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1433 KItemListView
* itemListView
= m_container
->controller()->view();
1434 if (itemListView
->isHeaderVisible()) {
1435 KItemListHeader
* header
= itemListView
->header();
1436 const QList
<int> headerColumnWidths
= props
.headerColumnWidths();
1437 const int rolesCount
= m_visibleRoles
.count();
1438 if (headerColumnWidths
.count() == rolesCount
) {
1439 header
->setAutomaticColumnResizing(false);
1441 QHash
<QByteArray
, qreal
> columnWidths
;
1442 for (int i
= 0; i
< rolesCount
; ++i
) {
1443 columnWidths
.insert(m_visibleRoles
[i
], headerColumnWidths
[i
]);
1445 header
->setColumnWidths(columnWidths
);
1447 header
->setAutomaticColumnResizing(true);
1451 m_view
->endTransaction();
1454 void DolphinView::applyModeToView()
1457 case IconsView
: m_view
->setItemLayout(KFileItemListView::IconsLayout
); break;
1458 case CompactView
: m_view
->setItemLayout(KFileItemListView::CompactLayout
); break;
1459 case DetailsView
: m_view
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1460 default: Q_ASSERT(false); break;
1464 void DolphinView::pasteToUrl(const KUrl
& url
)
1466 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1467 KonqOperations::doPaste(this, url
);
1470 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1474 const KFileItemList items
= selectedItems();
1475 foreach (const KFileItem
& item
, items
) {
1476 urls
.append(item
.url());
1479 if (itemsExpandable()) {
1480 // TODO: Check if we still need KDirModel for this in KDE 5.0
1481 urls
= KDirModel::simplifiedUrlList(urls
);
1487 QMimeData
* DolphinView::selectionMimeData() const
1489 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1490 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1492 return m_model
->createMimeData(selectedIndexes
);
1495 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1497 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1498 KUrl::List destUrls
;
1499 foreach (const KUrl
& source
, sourceUrls
) {
1500 KUrl
destination(url().url() + '/' + source
.fileName());
1501 destUrls
<< destination
;
1503 markUrlsAsSelected(destUrls
);
1506 void DolphinView::updateWritableState()
1508 const bool wasFolderWritable
= m_isFolderWritable
;
1509 m_isFolderWritable
= true;
1511 const KFileItem item
= m_model
->rootItem();
1512 if (!item
.isNull()) {
1513 KFileItemListProperties
capabilities(KFileItemList() << item
);
1514 m_isFolderWritable
= capabilities
.supportsWriting();
1516 if (m_isFolderWritable
!= wasFolderWritable
) {
1517 emit
writeStateChanged(m_isFolderWritable
);
1521 KUrl
DolphinView::viewPropertiesUrl() const
1523 if (m_viewPropertiesContext
.isEmpty()) {
1528 url
.setProtocol(m_url
.protocol());
1529 url
.setPath(m_viewPropertiesContext
);
1533 #include "dolphinview.moc"