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/JobUiDelegate>
51 #include <KIO/NetAccess>
52 #include <KIO/PreviewJob>
55 #include <KMessageBox>
56 #include <konq_fileitemcapabilities.h>
57 #include <konq_operations.h>
58 #include <konqmimedata.h>
59 #include <KToggleAction>
62 #include "dolphinnewfilemenuobserver.h"
63 #include "dolphin_detailsmodesettings.h"
64 #include "dolphin_generalsettings.h"
65 #include "dolphinitemlistcontainer.h"
66 #include "draganddrophelper.h"
67 #include "renamedialog.h"
68 #include "versioncontrol/versioncontrolobserver.h"
69 #include "viewmodecontroller.h"
70 #include "viewproperties.h"
71 #include "views/tooltips/tooltipmanager.h"
72 #include "zoomlevelinfo.h"
75 #include <Nepomuk/ResourceManager>
79 const int MaxModeEnum
= DolphinView::CompactView
;
82 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
85 m_tabsForFiles(false),
86 m_assureVisibleCurrentIndex(false),
87 m_isFolderWritable(true),
90 m_mode(DolphinView::IconsView
),
95 m_selectionChangedTimer(0),
97 m_restoredContentsPosition(),
100 m_versionControlObserver(0)
102 m_topLayout
= new QVBoxLayout(this);
103 m_topLayout
->setSpacing(0);
104 m_topLayout
->setMargin(0);
106 // When a new item has been created by the "Create New..." menu, the item should
107 // get selected and it must be assured that the item will get visible. As the
108 // creation is done asynchronously, several signals must be checked:
109 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
110 this, SLOT(observeCreatedItem(KUrl
)));
112 m_selectionChangedTimer
= new QTimer(this);
113 m_selectionChangedTimer
->setSingleShot(true);
114 m_selectionChangedTimer
->setInterval(300);
115 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
116 this, SLOT(emitSelectionChangedSignal()));
118 m_container
= new DolphinItemListContainer(this);
119 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
120 m_container
->installEventFilter(this);
121 setFocusProxy(m_container
);
122 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
123 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
125 KItemListController
* controller
= m_container
->controller();
126 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
127 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
128 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
129 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
130 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
131 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
132 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
133 connect(controller
, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons
)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons
)));
134 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
135 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
136 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
137 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
139 KFileItemModel
* model
= fileItemModel();
140 connect(model
, SIGNAL(directoryLoadingStarted()), this, SLOT(slotDirectoryLoadingStarted()));
141 connect(model
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
142 connect(model
, SIGNAL(directoryLoadingProgress(int)), this, SIGNAL(directoryLoadingProgress(int)));
143 connect(model
, SIGNAL(directorySortingProgress(int)), this, SIGNAL(directorySortingProgress(int)));
144 connect(model
, SIGNAL(itemsChanged(KItemRangeList
,QSet
<QByteArray
>)),
145 this, SLOT(slotItemsChanged()));
146 connect(model
, SIGNAL(itemsRemoved(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
147 connect(model
, SIGNAL(itemsInserted(KItemRangeList
)), this, SIGNAL(itemCountChanged()));
148 connect(model
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
149 connect(model
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
150 connect(model
, SIGNAL(directoryRedirection(KUrl
,KUrl
)), this, SLOT(slotDirectoryRedirection(KUrl
,KUrl
)));
152 KItemListView
* view
= controller
->view();
153 view
->installEventFilter(this);
154 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
155 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
156 connect(view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
157 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
158 connect(view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
159 this, SLOT(slotVisibleRolesChangedByHeader(QList
<QByteArray
>,QList
<QByteArray
>)));
160 connect(view
, SIGNAL(roleEditingFinished(int,QByteArray
,QVariant
)),
161 this, SLOT(slotRoleEditingFinished(int,QByteArray
,QVariant
)));
162 connect(view
->header(), SIGNAL(columnWidthChanged(QByteArray
,qreal
,qreal
)),
163 this, SLOT(slotHeaderColumnWidthChanged(QByteArray
,qreal
,qreal
)));
165 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
166 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
167 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
169 m_toolTipManager
= new ToolTipManager(this);
171 m_versionControlObserver
= new VersionControlObserver(this);
172 m_versionControlObserver
->setModel(model
);
173 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
174 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
175 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
177 applyViewProperties();
178 m_topLayout
->addWidget(m_container
);
183 DolphinView::~DolphinView()
187 KUrl
DolphinView::url() const
192 void DolphinView::setActive(bool active
)
194 if (active
== m_active
) {
200 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
205 QWidget
* viewport
= m_container
->viewport();
208 palette
.setColor(viewport
->backgroundRole(), color
);
209 viewport
->setPalette(palette
);
215 m_container
->setFocus();
217 emit
writeStateChanged(m_isFolderWritable
);
221 bool DolphinView::isActive() const
226 void DolphinView::setMode(Mode mode
)
228 if (mode
!= m_mode
) {
229 ViewProperties
props(url());
230 props
.setViewMode(mode
);
233 applyViewProperties();
237 DolphinView::Mode
DolphinView::mode() const
242 void DolphinView::setPreviewsShown(bool show
)
244 if (previewsShown() == show
) {
248 ViewProperties
props(url());
249 props
.setPreviewsShown(show
);
251 m_container
->setPreviewsShown(show
);
252 emit
previewsShownChanged(show
);
255 bool DolphinView::previewsShown() const
257 return m_container
->previewsShown();
260 void DolphinView::setHiddenFilesShown(bool show
)
262 KFileItemModel
* model
= fileItemModel();
263 if (model
->showHiddenFiles() == show
) {
267 const KFileItemList itemList
= selectedItems();
268 m_selectedUrls
.clear();
269 m_selectedUrls
= itemList
.urlList();
271 ViewProperties
props(url());
272 props
.setHiddenFilesShown(show
);
274 model
->setShowHiddenFiles(show
);
275 emit
hiddenFilesShownChanged(show
);
278 bool DolphinView::hiddenFilesShown() const
280 return fileItemModel()->showHiddenFiles();
283 void DolphinView::setGroupedSorting(bool grouped
)
285 if (grouped
== groupedSorting()) {
289 ViewProperties
props(url());
290 props
.setGroupedSorting(grouped
);
293 m_container
->controller()->model()->setGroupedSorting(grouped
);
295 emit
groupedSortingChanged(grouped
);
298 bool DolphinView::groupedSorting() const
300 return fileItemModel()->groupedSorting();
303 KFileItemList
DolphinView::items() const
306 const KFileItemModel
* model
= fileItemModel();
307 const int itemCount
= model
->count();
308 list
.reserve(itemCount
);
310 for (int i
= 0; i
< itemCount
; ++i
) {
311 list
.append(model
->fileItem(i
));
317 int DolphinView::itemsCount() const
319 return fileItemModel()->count();
322 KFileItemList
DolphinView::selectedItems() const
324 const KFileItemModel
* model
= fileItemModel();
325 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
326 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
328 KFileItemList selectedItems
;
329 QSetIterator
<int> it(selectedIndexes
);
330 while (it
.hasNext()) {
331 const int index
= it
.next();
332 selectedItems
.append(model
->fileItem(index
));
334 return selectedItems
;
337 int DolphinView::selectedItemsCount() const
339 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
340 return selectionManager
->selectedItems().count();
343 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
345 m_selectedUrls
= urls
;
348 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
350 m_currentItemUrl
= url
;
353 void DolphinView::selectItems(const QRegExp
& pattern
, bool enabled
)
355 const KItemListSelectionManager::SelectionMode mode
= enabled
356 ? KItemListSelectionManager::Select
357 : KItemListSelectionManager::Deselect
;
358 const KFileItemModel
* model
= fileItemModel();
359 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
361 for (int index
= 0; index
< model
->count(); index
++) {
362 const KFileItem item
= model
->fileItem(index
);
363 if (pattern
.exactMatch(item
.text())) {
364 // An alternative approach would be to store the matching items in a QSet<int> and
365 // select them in one go after the loop, but we'd need a new function
366 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
368 selectionManager
->setSelected(index
, 1, mode
);
373 void DolphinView::setZoomLevel(int level
)
375 const int oldZoomLevel
= zoomLevel();
376 m_container
->setZoomLevel(level
);
377 if (zoomLevel() != oldZoomLevel
) {
378 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
382 int DolphinView::zoomLevel() const
384 return m_container
->zoomLevel();
387 void DolphinView::setSortRole(const QByteArray
& role
)
389 if (role
!= sortRole()) {
390 updateSortRole(role
);
394 QByteArray
DolphinView::sortRole() const
396 const KItemModelBase
* model
= m_container
->controller()->model();
397 return model
->sortRole();
400 void DolphinView::setSortOrder(Qt::SortOrder order
)
402 if (sortOrder() != order
) {
403 updateSortOrder(order
);
407 Qt::SortOrder
DolphinView::sortOrder() const
409 KItemModelBase
* model
= fileItemModel();
410 return model
->sortOrder();
413 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
415 if (sortFoldersFirst() != foldersFirst
) {
416 updateSortFoldersFirst(foldersFirst
);
420 bool DolphinView::sortFoldersFirst() const
422 KFileItemModel
* model
= fileItemModel();
423 return model
->sortDirectoriesFirst();
426 void DolphinView::setVisibleRoles(const QList
<QByteArray
>& roles
)
428 const QList
<QByteArray
> previousRoles
= roles
;
430 ViewProperties
props(url());
431 props
.setVisibleRoles(roles
);
433 m_visibleRoles
= roles
;
434 m_container
->setVisibleRoles(roles
);
436 emit
visibleRolesChanged(m_visibleRoles
, previousRoles
);
439 QList
<QByteArray
> DolphinView::visibleRoles() const
441 return m_visibleRoles
;
444 void DolphinView::reload()
446 QByteArray viewState
;
447 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
448 saveState(saveStream
);
450 const KFileItemList itemList
= selectedItems();
451 m_selectedUrls
.clear();
452 m_selectedUrls
= itemList
.urlList();
455 loadDirectory(url(), true);
457 QDataStream
restoreStream(viewState
);
458 restoreState(restoreStream
);
461 void DolphinView::stopLoading()
463 fileItemModel()->cancelDirectoryLoading();
466 void DolphinView::readSettings()
468 const int oldZoomLevel
= m_container
->zoomLevel();
470 GeneralSettings::self()->readConfig();
471 m_container
->readSettings();
472 applyViewProperties();
474 const int newZoomLevel
= m_container
->zoomLevel();
475 if (newZoomLevel
!= oldZoomLevel
) {
476 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
480 void DolphinView::writeSettings()
482 GeneralSettings::self()->writeConfig();
483 m_container
->writeSettings();
486 void DolphinView::setNameFilter(const QString
& nameFilter
)
488 fileItemModel()->setNameFilter(nameFilter
);
491 QString
DolphinView::nameFilter() const
493 return fileItemModel()->nameFilter();
496 QString
DolphinView::statusBarText() const
504 KIO::filesize_t totalFileSize
= 0;
506 if (m_container
->controller()->selectionManager()->hasSelection()) {
507 // Give a summary of the status of the selected files
508 const KFileItemList list
= selectedItems();
509 foreach (const KFileItem
& item
, list
) {
514 totalFileSize
+= item
.size();
518 if (folderCount
+ fileCount
== 1) {
519 // If only one item is selected, show the filename
520 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
522 // At least 2 items are selected
523 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
524 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
527 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
528 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
529 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
532 if (fileCount
> 0 && folderCount
> 0) {
533 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
534 foldersText
, filesText
,
535 KGlobal::locale()->formatByteSize(totalFileSize
));
536 } else if (fileCount
> 0) {
537 summary
= i18nc("@info:status files (size)", "%1 (%2)",
539 KGlobal::locale()->formatByteSize(totalFileSize
));
540 } else if (folderCount
> 0) {
541 summary
= foldersText
;
543 summary
= i18nc("@info:status", "0 Folders, 0 Files");
549 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
551 QList
<QAction
*> actions
;
553 if (items
.isEmpty()) {
554 const KFileItem item
= fileItemModel()->rootItem();
555 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
557 actions
= m_versionControlObserver
->actions(items
);
563 void DolphinView::setUrl(const KUrl
& url
)
569 emit
urlAboutToBeChanged(url
);
574 // It is important to clear the items from the model before
575 // applying the view properties, otherwise expensive operations
576 // might be done on the existing items although they get cleared
577 // anyhow afterwards by loadDirectory().
578 fileItemModel()->clear();
579 applyViewProperties();
582 emit
urlChanged(url
);
585 void DolphinView::selectAll()
587 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
588 selectionManager
->setSelected(0, fileItemModel()->count());
591 void DolphinView::invertSelection()
593 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
594 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
597 void DolphinView::clearSelection()
599 m_container
->controller()->selectionManager()->clearSelection();
602 void DolphinView::renameSelectedItems()
604 const KFileItemList items
= selectedItems();
605 if (items
.isEmpty()) {
609 if (items
.count() == 1) {
610 const int index
= fileItemModel()->index(items
.first());
611 m_container
->controller()->view()->editRole(index
, "name");
613 RenameDialog
* dialog
= new RenameDialog(this, items
);
614 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
617 dialog
->activateWindow();
620 // Assure that the current index remains visible when KFileItemModel
621 // will notify the view about changed items (which might result in
622 // a changed sorting).
623 m_assureVisibleCurrentIndex
= true;
626 void DolphinView::trashSelectedItems()
628 const KUrl::List list
= simplifiedSelectedUrls();
629 KonqOperations::del(this, KonqOperations::TRASH
, list
);
632 void DolphinView::deleteSelectedItems()
634 const KUrl::List list
= simplifiedSelectedUrls();
635 const bool del
= KonqOperations::askDeleteConfirmation(list
,
637 KonqOperations::DEFAULT_CONFIRMATION
,
641 KIO::Job
* job
= KIO::del(list
);
643 job
->ui()->setWindow(this);
645 connect(job
, SIGNAL(result(KJob
*)),
646 this, SLOT(slotDeleteFileFinished(KJob
*)));
650 void DolphinView::cutSelectedItems()
652 QMimeData
* mimeData
= selectionMimeData();
653 KonqMimeData::addIsCutSelection(mimeData
, true);
654 QApplication::clipboard()->setMimeData(mimeData
);
657 void DolphinView::copySelectedItems()
659 QMimeData
* mimeData
= selectionMimeData();
660 QApplication::clipboard()->setMimeData(mimeData
);
663 void DolphinView::paste()
668 void DolphinView::pasteIntoFolder()
670 const KFileItemList items
= selectedItems();
671 if ((items
.count() == 1) && items
.first().isDir()) {
672 pasteToUrl(items
.first().url());
676 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
678 switch (event
->type()) {
679 case QEvent::FocusIn
:
680 if (watched
== m_container
) {
685 case QEvent::GraphicsSceneDragEnter
:
686 if (watched
== m_container
->controller()->view()) {
691 case QEvent::GraphicsSceneDragLeave
:
692 if (watched
== m_container
->controller()->view()) {
697 case QEvent::GraphicsSceneDrop
:
698 if (watched
== m_container
->controller()->view()) {
705 return QWidget::eventFilter(watched
, event
);
708 void DolphinView::wheelEvent(QWheelEvent
* event
)
710 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
711 const int numDegrees
= event
->delta() / 8;
712 const int numSteps
= numDegrees
/ 15;
714 setZoomLevel(zoomLevel() + numSteps
);
721 void DolphinView::hideEvent(QHideEvent
* event
)
724 QWidget::hideEvent(event
);
727 void DolphinView::activate()
732 void DolphinView::slotItemActivated(int index
)
734 const KFileItem item
= fileItemModel()->fileItem(index
);
735 if (!item
.isNull()) {
736 emit
itemActivated(item
);
740 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
742 Q_ASSERT(indexes
.count() >= 2);
746 KFileItemModel
* model
= fileItemModel();
747 QSetIterator
<int> it(indexes
);
748 while (it
.hasNext()) {
749 const int index
= it
.next();
750 items
.append(model
->fileItem(index
));
753 foreach (const KFileItem
& item
, items
) {
755 emit
tabRequested(item
.url());
757 emit
itemActivated(item
);
762 void DolphinView::slotItemMiddleClicked(int index
)
764 const KFileItem item
= fileItemModel()->fileItem(index
);
765 if (item
.isDir() || isTabsForFilesEnabled()) {
766 emit
tabRequested(item
.url());
770 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
772 const KFileItem item
= fileItemModel()->fileItem(index
);
773 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
776 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
778 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
781 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
783 ViewProperties
props(url());
785 QPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
787 KItemListView
* view
= m_container
->controller()->view();
788 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
790 bool nepomukRunning
= false;
791 bool indexingEnabled
= false;
793 nepomukRunning
= (Nepomuk::ResourceManager::instance()->init() == 0);
794 if (nepomukRunning
) {
795 KConfig
config("nepomukserverrc");
796 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
801 QMenu
* groupMenu
= 0;
803 // Add all roles to the menu that can be shown or hidden by the user
804 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
805 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
806 if (info
.role
== "name") {
807 // It should not be possible to hide the "name" role
811 const QString text
= fileItemModel()->roleDescription(info
.role
);
813 if (info
.group
.isEmpty()) {
814 action
= menu
->addAction(text
);
816 if (!groupMenu
|| info
.group
!= groupName
) {
817 groupName
= info
.group
;
818 groupMenu
= menu
->addMenu(groupName
);
821 action
= groupMenu
->addAction(text
);
824 action
->setCheckable(true);
825 action
->setChecked(visibleRolesSet
.contains(info
.role
));
826 action
->setData(info
.role
);
828 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
829 (info
.requiresNepomuk
&& nepomukRunning
) ||
830 (info
.requiresIndexer
&& indexingEnabled
);
831 action
->setEnabled(enable
);
834 menu
->addSeparator();
836 QActionGroup
* widthsGroup
= new QActionGroup(menu
);
837 const bool autoColumnWidths
= props
.headerColumnWidths().isEmpty();
839 QAction
* autoAdjustWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
840 autoAdjustWidthsAction
->setCheckable(true);
841 autoAdjustWidthsAction
->setChecked(autoColumnWidths
);
842 autoAdjustWidthsAction
->setActionGroup(widthsGroup
);
844 QAction
* customWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
845 customWidthsAction
->setCheckable(true);
846 customWidthsAction
->setChecked(!autoColumnWidths
);
847 customWidthsAction
->setActionGroup(widthsGroup
);
849 QAction
* action
= menu
->exec(pos
.toPoint());
850 if (menu
&& action
) {
851 KItemListHeader
* header
= view
->header();
853 if (action
== autoAdjustWidthsAction
) {
854 // Clear the column-widths from the viewproperties and turn on
855 // the automatic resizing of the columns
856 props
.setHeaderColumnWidths(QList
<int>());
857 header
->setAutomaticColumnResizing(true);
858 } else if (action
== customWidthsAction
) {
859 // Apply the current column-widths as custom column-widths and turn
860 // off the automatic resizing of the columns
861 QList
<int> columnWidths
;
862 foreach (const QByteArray
& role
, view
->visibleRoles()) {
863 columnWidths
.append(header
->columnWidth(role
));
865 props
.setHeaderColumnWidths(columnWidths
);
866 header
->setAutomaticColumnResizing(false);
868 // Show or hide the selected role
869 const QByteArray selectedRole
= action
->data().toByteArray();
871 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
872 if (action
->isChecked()) {
873 visibleRoles
.append(selectedRole
);
875 visibleRoles
.removeOne(selectedRole
);
878 view
->setVisibleRoles(visibleRoles
);
879 props
.setVisibleRoles(visibleRoles
);
881 QList
<int> columnWidths
;
882 if (!header
->automaticColumnResizing()) {
883 foreach (const QByteArray
& role
, view
->visibleRoles()) {
884 columnWidths
.append(header
->columnWidth(role
));
887 props
.setHeaderColumnWidths(columnWidths
);
894 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
)
898 const QList
<QByteArray
> visibleRoles
= m_container
->visibleRoles();
900 ViewProperties
props(url());
901 QList
<int> columnWidths
= props
.headerColumnWidths();
902 if (columnWidths
.count() != visibleRoles
.count()) {
903 columnWidths
.clear();
904 columnWidths
.reserve(visibleRoles
.count());
905 const KItemListHeader
* header
= m_container
->controller()->view()->header();
906 foreach (const QByteArray
& role
, visibleRoles
) {
907 const int width
= header
->columnWidth(role
);
908 columnWidths
.append(width
);
912 const int roleIndex
= visibleRoles
.indexOf(role
);
913 Q_ASSERT(roleIndex
>= 0 && roleIndex
< columnWidths
.count());
914 columnWidths
[roleIndex
] = current
;
916 props
.setHeaderColumnWidths(columnWidths
);
919 void DolphinView::slotItemHovered(int index
)
921 const KFileItem item
= fileItemModel()->fileItem(index
);
923 if (GeneralSettings::showToolTips() && !m_dragging
) {
924 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
925 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
926 itemRect
.moveTo(pos
);
928 m_toolTipManager
->showToolTip(item
, itemRect
);
931 emit
requestItemInfo(item
);
934 void DolphinView::slotItemUnhovered(int index
)
938 emit
requestItemInfo(KFileItem());
941 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
944 KFileItem destItem
= fileItemModel()->fileItem(index
);
945 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
946 // Use the URL of the view as drop target if the item is no directory
948 destItem
= fileItemModel()->rootItem();
951 // The item represents a directory or desktop-file
952 destUrl
= destItem
.url();
955 QDropEvent
dropEvent(event
->pos().toPoint(),
956 event
->possibleActions(),
961 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
962 if (!error
.isEmpty()) {
963 emit
errorMessage(error
);
966 if (destUrl
== url()) {
967 // Mark the dropped urls as selected.
968 markPastedUrlsAsSelected(event
->mimeData());
972 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
975 disconnect(previous
, SIGNAL(directoryLoadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
978 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
979 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotDirectoryLoadingCompleted()));
981 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
982 m_versionControlObserver
->setModel(fileItemModel
);
985 void DolphinView::slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
)
990 // Trigger the history navigation only when clicking on the viewport:
991 // Above an item the XButtons provide a simple way to select items in
992 // the singleClick mode.
993 if (buttons
& Qt::XButton1
) {
994 emit
goBackRequested();
995 } else if (buttons
& Qt::XButton2
) {
996 emit
goForwardRequested();
1001 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
1003 const int currentCount
= current
.count();
1004 const int previousCount
= previous
.count();
1005 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
1006 (currentCount
> 0 && previousCount
== 0);
1008 // If nothing has been selected before and something got selected (or if something
1009 // was selected before and now nothing is selected) the selectionChangedSignal must
1010 // be emitted asynchronously as fast as possible to update the edit-actions.
1011 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
1012 m_selectionChangedTimer
->start();
1015 void DolphinView::emitSelectionChangedSignal()
1017 m_selectionChangedTimer
->stop();
1018 emit
selectionChanged(selectedItems());
1021 void DolphinView::updateSortRole(const QByteArray
& role
)
1023 ViewProperties
props(url());
1024 props
.setSortRole(role
);
1026 KItemModelBase
* model
= m_container
->controller()->model();
1027 model
->setSortRole(role
);
1029 emit
sortRoleChanged(role
);
1032 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1034 ViewProperties
props(url());
1035 props
.setSortOrder(order
);
1037 KItemModelBase
* model
= fileItemModel();
1038 model
->setSortOrder(order
);
1040 emit
sortOrderChanged(order
);
1043 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1045 ViewProperties
props(url());
1046 props
.setSortFoldersFirst(foldersFirst
);
1048 KFileItemModel
* model
= fileItemModel();
1049 model
->setSortDirectoriesFirst(foldersFirst
);
1051 emit
sortFoldersFirstChanged(foldersFirst
);
1054 QPair
<bool, QString
> DolphinView::pasteInfo() const
1056 return KonqOperations::pasteInfo(url());
1059 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1061 m_tabsForFiles
= tabsForFiles
;
1064 bool DolphinView::isTabsForFilesEnabled() const
1066 return m_tabsForFiles
;
1069 bool DolphinView::itemsExpandable() const
1071 return m_mode
== DetailsView
;
1074 void DolphinView::restoreState(QDataStream
& stream
)
1076 // Restore the current item that had the keyboard focus
1077 stream
>> m_currentItemUrl
;
1079 // Restore the view position
1080 stream
>> m_restoredContentsPosition
;
1082 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1085 fileItemModel()->restoreExpandedDirectories(urls
);
1088 void DolphinView::saveState(QDataStream
& stream
)
1090 // Save the current item that has the keyboard focus
1091 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
1092 if (currentIndex
!= -1) {
1093 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
1094 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
1095 KUrl currentItemUrl
= item
.url();
1096 stream
<< currentItemUrl
;
1101 // Save view position
1102 const qreal x
= m_container
->horizontalScrollBar()->value();
1103 const qreal y
= m_container
->verticalScrollBar()->value();
1104 stream
<< QPoint(x
, y
);
1106 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1107 stream
<< fileItemModel()->expandedDirectories();
1110 KFileItem
DolphinView::rootItem() const
1112 return fileItemModel()->rootItem();
1115 void DolphinView::observeCreatedItem(const KUrl
& url
)
1117 m_createdItemUrl
= url
;
1118 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1119 // this, SLOT(selectAndScrollToCreatedItem()));
1122 void DolphinView::selectAndScrollToCreatedItem()
1124 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1125 if (dirIndex.isValid()) {
1126 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1127 QAbstractItemView* view = m_viewAccessor.itemView();
1129 view->setCurrentIndex(proxyIndex);
1133 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1134 this, SLOT(selectAndScrollToCreatedItem()));*/
1135 m_createdItemUrl
= KUrl();
1138 void DolphinView::slotDirectoryRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1140 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1141 emit
redirection(oldUrl
, newUrl
);
1142 m_url
= newUrl
; // #186947
1146 void DolphinView::updateViewState()
1148 if (m_currentItemUrl
!= KUrl()) {
1149 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1150 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1151 if (currentIndex
!= -1) {
1152 selectionManager
->setCurrentItem(currentIndex
);
1154 selectionManager
->setCurrentItem(0);
1156 m_currentItemUrl
= KUrl();
1159 if (!m_restoredContentsPosition
.isNull()) {
1160 const int x
= m_restoredContentsPosition
.x();
1161 const int y
= m_restoredContentsPosition
.y();
1162 m_restoredContentsPosition
= QPoint();
1164 m_container
->horizontalScrollBar()->setValue(x
);
1165 m_container
->verticalScrollBar()->setValue(y
);
1168 if (!m_selectedUrls
.isEmpty()) {
1171 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1172 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1173 const KFileItemModel
* model
= fileItemModel();
1175 foreach (const KUrl
& url
, m_selectedUrls
) {
1176 const int index
= model
->index(url
);
1178 selectedItems
.insert(index
);
1182 selectionManager
->setSelectedItems(selectedItems
);
1183 m_selectedUrls
.clear();
1187 void DolphinView::hideToolTip()
1189 if (GeneralSettings::showToolTips()) {
1190 m_toolTipManager
->hideToolTip();
1194 void DolphinView::calculateItemCount(int& fileCount
,
1196 KIO::filesize_t
& totalFileSize
) const
1198 const KFileItemModel
* model
= fileItemModel();
1199 const int itemCount
= model
->count();
1200 for (int i
= 0; i
< itemCount
; ++i
) {
1201 const KFileItem item
= model
->fileItem(i
);
1206 totalFileSize
+= item
.size();
1211 void DolphinView::showHoverInformation(const KFileItem
& item
)
1213 emit
requestItemInfo(item
);
1216 void DolphinView::clearHoverInformation()
1218 emit
requestItemInfo(KFileItem());
1221 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1223 if (job
->error() == 0) {
1224 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1225 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1226 emit
errorMessage(job
->errorString());
1230 void DolphinView::slotDirectoryLoadingStarted()
1232 // Disable the writestate temporary until it can be determined in a fast way
1233 // in DolphinView::slotLoadingCompleted()
1234 if (m_isFolderWritable
) {
1235 m_isFolderWritable
= false;
1236 emit
writeStateChanged(m_isFolderWritable
);
1239 emit
directoryLoadingStarted();
1242 void DolphinView::slotDirectoryLoadingCompleted()
1244 // Update the view-state. This has to be done asynchronously
1245 // because the view might not be in its final state yet.
1246 QTimer::singleShot(0, this, SLOT(updateViewState()));
1248 emit
directoryLoadingCompleted();
1250 updateWritableState();
1253 void DolphinView::slotItemsChanged()
1255 m_assureVisibleCurrentIndex
= false;
1258 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1261 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1263 ViewProperties
props(url());
1264 props
.setSortOrder(current
);
1266 emit
sortOrderChanged(current
);
1269 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1272 Q_ASSERT(fileItemModel()->sortRole() == current
);
1274 ViewProperties
props(url());
1275 props
.setSortRole(current
);
1277 emit
sortRoleChanged(current
);
1280 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1281 const QList
<QByteArray
>& previous
)
1284 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1286 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1288 m_visibleRoles
= current
;
1290 ViewProperties
props(url());
1291 props
.setVisibleRoles(m_visibleRoles
);
1293 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1296 void DolphinView::slotRoleEditingFinished(int index
, const QByteArray
& role
, const QVariant
& value
)
1298 if (role
== "name") {
1299 const KFileItem item
= fileItemModel()->fileItem(index
);
1300 const QString newName
= value
.toString();
1301 if (!newName
.isEmpty() && newName
!= item
.text() && newName
!= QLatin1String(".") && newName
!= QLatin1String("..")) {
1302 KonqOperations::rename(this, item
.url(), newName
);
1307 KFileItemModel
* DolphinView::fileItemModel() const
1309 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1312 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1314 if (!url
.isValid()) {
1315 const QString
location(url
.pathOrUrl());
1316 if (location
.isEmpty()) {
1317 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1319 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1324 KFileItemModel
* model
= fileItemModel();
1326 model
->refreshDirectory(url
);
1328 model
->loadDirectory(url
);
1332 void DolphinView::applyViewProperties()
1334 m_container
->beginTransaction();
1336 const ViewProperties
props(url());
1337 KFileItemModel
* model
= fileItemModel();
1339 const Mode mode
= props
.viewMode();
1340 if (m_mode
!= mode
) {
1341 const Mode previousMode
= m_mode
;
1344 // Changing the mode might result in changing
1345 // the zoom level. Remember the old zoom level so
1346 // that zoomLevelChanged() can get emitted.
1347 const int oldZoomLevel
= m_container
->zoomLevel();
1350 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1351 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1352 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1353 default: Q_ASSERT(false); break;
1356 emit
modeChanged(m_mode
, previousMode
);
1358 if (m_container
->zoomLevel() != oldZoomLevel
) {
1359 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1363 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1364 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1365 model
->setShowHiddenFiles(hiddenFilesShown
);
1366 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1369 const bool groupedSorting
= props
.groupedSorting();
1370 if (groupedSorting
!= model
->groupedSorting()) {
1371 model
->setGroupedSorting(groupedSorting
);
1372 emit
groupedSortingChanged(groupedSorting
);
1375 const QByteArray sortRole
= props
.sortRole();
1376 if (sortRole
!= model
->sortRole()) {
1377 model
->setSortRole(sortRole
);
1378 emit
sortRoleChanged(sortRole
);
1381 const Qt::SortOrder sortOrder
= props
.sortOrder();
1382 if (sortOrder
!= model
->sortOrder()) {
1383 model
->setSortOrder(sortOrder
);
1384 emit
sortOrderChanged(sortOrder
);
1387 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1388 if (sortFoldersFirst
!= model
->sortDirectoriesFirst()) {
1389 model
->setSortDirectoriesFirst(sortFoldersFirst
);
1390 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1393 const QList
<QByteArray
> visibleRoles
= props
.visibleRoles();
1394 if (visibleRoles
!= m_visibleRoles
) {
1395 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1396 m_visibleRoles
= visibleRoles
;
1397 m_container
->setVisibleRoles(visibleRoles
);
1398 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1401 const bool previewsShown
= props
.previewsShown();
1402 if (previewsShown
!= m_container
->previewsShown()) {
1403 const int oldZoomLevel
= zoomLevel();
1405 m_container
->setPreviewsShown(previewsShown
);
1406 emit
previewsShownChanged(previewsShown
);
1408 // Changing the preview-state might result in a changed zoom-level
1409 if (oldZoomLevel
!= zoomLevel()) {
1410 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1414 KItemListView
* itemListView
= m_container
->controller()->view();
1415 if (itemListView
->isHeaderVisible()) {
1416 KItemListHeader
* header
= itemListView
->header();
1417 const QList
<int> headerColumnWidths
= props
.headerColumnWidths();
1418 const int rolesCount
= m_visibleRoles
.count();
1419 if (headerColumnWidths
.count() == rolesCount
) {
1420 header
->setAutomaticColumnResizing(false);
1422 QHash
<QByteArray
, qreal
> columnWidths
;
1423 for (int i
= 0; i
< rolesCount
; ++i
) {
1424 columnWidths
.insert(m_visibleRoles
[i
], headerColumnWidths
[i
]);
1426 header
->setColumnWidths(columnWidths
);
1428 header
->setAutomaticColumnResizing(true);
1432 m_container
->endTransaction();
1435 void DolphinView::pasteToUrl(const KUrl
& url
)
1437 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1438 KonqOperations::doPaste(this, url
);
1441 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1445 const KFileItemList items
= selectedItems();
1446 foreach (const KFileItem
&item
, items
) {
1447 urls
.append(item
.url());
1450 if (itemsExpandable()) {
1451 // TODO: Check if we still need KDirModel for this in KDE 5.0
1452 urls
= KDirModel::simplifiedUrlList(urls
);
1458 QMimeData
* DolphinView::selectionMimeData() const
1460 const KFileItemModel
* model
= fileItemModel();
1461 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1462 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1464 return model
->createMimeData(selectedIndexes
);
1467 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1469 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1470 KUrl::List destUrls
;
1471 foreach (const KUrl
& source
, sourceUrls
) {
1472 KUrl
destination(url().url() + "/" + source
.fileName());
1473 destUrls
<< destination
;
1475 markUrlsAsSelected(destUrls
);
1478 void DolphinView::updateWritableState()
1480 const bool wasFolderWritable
= m_isFolderWritable
;
1481 m_isFolderWritable
= true;
1483 const KFileItem item
= fileItemModel()->rootItem();
1484 if (!item
.isNull()) {
1485 KFileItemListProperties
capabilities(KFileItemList() << item
);
1486 m_isFolderWritable
= capabilities
.supportsWriting();
1488 if (m_isFolderWritable
!= wasFolderWritable
) {
1489 emit
writeStateChanged(m_isFolderWritable
);
1493 #include "dolphinview.moc"