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>
40 #include <KIconEffect>
42 #include <KFileItemListProperties>
44 #include <kitemviews/kfileitemmodel.h>
45 #include <kitemviews/kfileitemlistview.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/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 "dolphindirlister.h"
63 #include "dolphinnewfilemenuobserver.h"
64 #include "dolphin_detailsmodesettings.h"
65 #include "dolphin_generalsettings.h"
66 #include "dolphinitemlistcontainer.h"
67 #include "draganddrophelper.h"
68 #include "renamedialog.h"
69 #include "versioncontrol/versioncontrolobserver.h"
70 #include "viewmodecontroller.h"
71 #include "viewproperties.h"
72 #include "views/tooltips/tooltipmanager.h"
73 #include "zoomlevelinfo.h"
76 #include <Nepomuk/ResourceManager>
80 const int MaxModeEnum
= DolphinView::CompactView
;
83 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
86 m_tabsForFiles(false),
87 m_assureVisibleCurrentIndex(false),
88 m_isFolderWritable(true),
91 m_mode(DolphinView::IconsView
),
97 m_selectionChangedTimer(0),
99 m_restoredContentsPosition(),
102 m_versionControlObserver(0)
104 m_topLayout
= new QVBoxLayout(this);
105 m_topLayout
->setSpacing(0);
106 m_topLayout
->setMargin(0);
108 // When a new item has been created by the "Create New..." menu, the item should
109 // get selected and it must be assured that the item will get visible. As the
110 // creation is done asynchronously, several signals must be checked:
111 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
112 this, SLOT(observeCreatedItem(KUrl
)));
114 m_selectionChangedTimer
= new QTimer(this);
115 m_selectionChangedTimer
->setSingleShot(true);
116 m_selectionChangedTimer
->setInterval(300);
117 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
118 this, SLOT(emitSelectionChangedSignal()));
120 m_dirLister
= new DolphinDirLister(this);
121 m_dirLister
->setAutoUpdate(true);
122 m_dirLister
->setDelayedMimeTypes(true);
124 connect(m_dirLister
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
125 connect(m_dirLister
, SIGNAL(started(KUrl
)), this, SLOT(slotDirListerStarted(KUrl
)));
126 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
127 this, SLOT(slotRefreshItems()));
129 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
130 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
131 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
132 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
133 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
134 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
135 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
137 m_container
= new DolphinItemListContainer(m_dirLister
, this);
138 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
139 m_container
->installEventFilter(this);
140 setFocusProxy(m_container
);
141 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
142 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
144 KItemListController
* controller
= m_container
->controller();
145 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
146 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
147 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
148 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
149 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
150 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
151 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
152 connect(controller
, SIGNAL(mouseButtonPressed(int,Qt::MouseButtons
)), this, SLOT(slotMouseButtonPressed(int,Qt::MouseButtons
)));
153 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
154 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
155 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
156 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
158 KFileItemModel
* model
= fileItemModel();
160 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
163 KItemListView
* view
= controller
->view();
164 view
->installEventFilter(this);
165 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
166 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
167 connect(view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
168 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
169 connect(view
, SIGNAL(visibleRolesChanged(QList
<QByteArray
>,QList
<QByteArray
>)),
170 this, SLOT(slotVisibleRolesChangedByHeader(QList
<QByteArray
>,QList
<QByteArray
>)));
171 connect(view
->header(), SIGNAL(columnWidthChanged(QByteArray
,qreal
,qreal
)),
172 this, SLOT(slotHeaderColumnWidthChanged(QByteArray
,qreal
,qreal
)));
174 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
175 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
176 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
178 m_toolTipManager
= new ToolTipManager(this);
180 m_versionControlObserver
= new VersionControlObserver(this);
181 m_versionControlObserver
->setModel(model
);
182 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
183 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
184 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
186 applyViewProperties();
187 m_topLayout
->addWidget(m_container
);
192 DolphinView::~DolphinView()
196 KUrl
DolphinView::url() const
201 void DolphinView::setActive(bool active
)
203 if (active
== m_active
) {
209 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
214 QWidget
* viewport
= m_container
->viewport();
217 palette
.setColor(viewport
->backgroundRole(), color
);
218 viewport
->setPalette(palette
);
224 m_container
->setFocus();
226 emit
writeStateChanged(m_isFolderWritable
);
230 bool DolphinView::isActive() const
235 void DolphinView::setMode(Mode mode
)
237 if (mode
!= m_mode
) {
238 ViewProperties
props(url());
239 props
.setViewMode(mode
);
242 applyViewProperties();
246 DolphinView::Mode
DolphinView::mode() const
251 void DolphinView::setPreviewsShown(bool show
)
253 if (previewsShown() == show
) {
257 ViewProperties
props(url());
258 props
.setPreviewsShown(show
);
260 m_container
->setPreviewsShown(show
);
261 emit
previewsShownChanged(show
);
264 bool DolphinView::previewsShown() const
266 return m_container
->previewsShown();
269 void DolphinView::setHiddenFilesShown(bool show
)
271 if (m_dirLister
->showingDotFiles() == show
) {
275 const KFileItemList itemList
= selectedItems();
276 m_selectedUrls
.clear();
277 m_selectedUrls
= itemList
.urlList();
279 ViewProperties
props(url());
280 props
.setHiddenFilesShown(show
);
282 fileItemModel()->setShowHiddenFiles(show
);
283 emit
hiddenFilesShownChanged(show
);
286 bool DolphinView::hiddenFilesShown() const
288 return m_dirLister
->showingDotFiles();
291 void DolphinView::setGroupedSorting(bool grouped
)
293 if (grouped
== groupedSorting()) {
297 ViewProperties
props(url());
298 props
.setGroupedSorting(grouped
);
301 m_container
->controller()->model()->setGroupedSorting(grouped
);
303 emit
groupedSortingChanged(grouped
);
306 bool DolphinView::groupedSorting() const
308 return fileItemModel()->groupedSorting();
311 KFileItemList
DolphinView::items() const
313 return m_dirLister
->items();
316 KFileItemList
DolphinView::selectedItems() const
318 const KFileItemModel
* model
= fileItemModel();
319 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
320 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
322 KFileItemList selectedItems
;
323 QSetIterator
<int> it(selectedIndexes
);
324 while (it
.hasNext()) {
325 const int index
= it
.next();
326 selectedItems
.append(model
->fileItem(index
));
328 return selectedItems
;
331 int DolphinView::selectedItemsCount() const
333 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
334 return selectionManager
->selectedItems().count();
337 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
339 m_selectedUrls
= urls
;
342 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
344 m_currentItemUrl
= url
;
347 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
349 const KItemListSelectionManager::SelectionMode mode
= enabled
350 ? KItemListSelectionManager::Select
351 : KItemListSelectionManager::Deselect
;
352 const KFileItemModel
* model
= fileItemModel();
353 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
355 for (int index
= 0; index
< model
->count(); index
++) {
356 const KFileItem item
= model
->fileItem(index
);
357 if (pattern
.exactMatch(item
.text())) {
358 // An alternative approach would be to store the matching items in a QSet<int> and
359 // select them in one go after the loop, but we'd need a new function
360 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
362 selectionManager
->setSelected(index
, 1, mode
);
367 void DolphinView::setZoomLevel(int level
)
369 const int oldZoomLevel
= zoomLevel();
370 m_container
->setZoomLevel(level
);
371 if (zoomLevel() != oldZoomLevel
) {
372 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
376 int DolphinView::zoomLevel() const
378 return m_container
->zoomLevel();
381 void DolphinView::setSortRole(const QByteArray
& role
)
383 if (role
!= sortRole()) {
384 updateSortRole(role
);
388 QByteArray
DolphinView::sortRole() const
390 const KItemModelBase
* model
= m_container
->controller()->model();
391 return model
->sortRole();
394 void DolphinView::setSortOrder(Qt::SortOrder order
)
396 if (sortOrder() != order
) {
397 updateSortOrder(order
);
401 Qt::SortOrder
DolphinView::sortOrder() const
403 KItemModelBase
* model
= fileItemModel();
404 return model
->sortOrder();
407 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
409 if (sortFoldersFirst() != foldersFirst
) {
410 updateSortFoldersFirst(foldersFirst
);
414 bool DolphinView::sortFoldersFirst() const
416 KFileItemModel
* model
= fileItemModel();
417 return model
->sortFoldersFirst();
420 void DolphinView::setVisibleRoles(const QList
<QByteArray
>& roles
)
422 const QList
<QByteArray
> previousRoles
= roles
;
424 ViewProperties
props(url());
425 props
.setVisibleRoles(roles
);
427 m_visibleRoles
= roles
;
428 m_container
->setVisibleRoles(roles
);
430 emit
visibleRolesChanged(m_visibleRoles
, previousRoles
);
433 QList
<QByteArray
> DolphinView::visibleRoles() const
435 return m_visibleRoles
;
438 void DolphinView::reload()
440 QByteArray viewState
;
441 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
442 saveState(saveStream
);
444 const KFileItemList itemList
= selectedItems();
445 m_selectedUrls
.clear();
446 m_selectedUrls
= itemList
.urlList();
449 loadDirectory(url(), true);
451 QDataStream
restoreStream(viewState
);
452 restoreState(restoreStream
);
455 void DolphinView::stopLoading()
460 void DolphinView::readSettings()
462 const int oldZoomLevel
= m_container
->zoomLevel();
464 GeneralSettings::self()->readConfig();
465 m_container
->readSettings();
466 applyViewProperties();
468 const int newZoomLevel
= m_container
->zoomLevel();
469 if (newZoomLevel
!= oldZoomLevel
) {
470 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
474 void DolphinView::writeSettings()
476 GeneralSettings::self()->writeConfig();
477 m_container
->writeSettings();
480 void DolphinView::setNameFilter(const QString
& nameFilter
)
482 fileItemModel()->setNameFilter(nameFilter
);
485 QString
DolphinView::nameFilter() const
487 return fileItemModel()->nameFilter();
490 void DolphinView::calculateItemCount(int& fileCount
,
492 KIO::filesize_t
& totalFileSize
) const
494 foreach (const KFileItem
& item
, m_dirLister
->items()) {
499 totalFileSize
+= item
.size();
504 QString
DolphinView::statusBarText() const
512 KIO::filesize_t totalFileSize
= 0;
514 if (hasSelection()) {
515 // Give a summary of the status of the selected files
516 const KFileItemList list
= selectedItems();
517 foreach (const KFileItem
& item
, list
) {
522 totalFileSize
+= item
.size();
526 if (folderCount
+ fileCount
== 1) {
527 // If only one item is selected, show the filename
528 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
530 // At least 2 items are selected
531 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
532 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
535 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
536 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
537 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
540 if (fileCount
> 0 && folderCount
> 0) {
541 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
542 foldersText
, filesText
, fileSizeText(totalFileSize
));
543 } else if (fileCount
> 0) {
544 summary
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, fileSizeText(totalFileSize
));
545 } else if (folderCount
> 0) {
546 summary
= foldersText
;
552 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
554 QList
<QAction
*> actions
;
556 if (items
.isEmpty()) {
557 const KFileItem item
= fileItemModel()->rootItem();
558 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
560 actions
= m_versionControlObserver
->actions(items
);
566 void DolphinView::setUrl(const KUrl
& url
)
572 emit
urlAboutToBeChanged(url
);
577 // It is important to clear the items from the model before
578 // applying the view properties, otherwise expensive operations
579 // might be done on the existing items although they get cleared
580 // anyhow afterwards by loadDirectory().
581 fileItemModel()->clear();
582 applyViewProperties();
585 emit
urlChanged(url
);
588 void DolphinView::selectAll()
590 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
591 selectionManager
->setSelected(0, fileItemModel()->count());
594 void DolphinView::invertSelection()
596 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
597 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
600 void DolphinView::clearSelection()
602 m_container
->controller()->selectionManager()->clearSelection();
605 void DolphinView::renameSelectedItems()
607 KFileItemList items
= selectedItems();
608 const int itemCount
= items
.count();
613 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
615 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
616 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
617 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
618 m_viewAccessor.itemView()->edit(proxyIndex);
620 RenameDialog
* dialog
= new RenameDialog(this, items
);
621 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
624 dialog
->activateWindow();
627 // assure that the current index remains visible when KDirLister
628 // will notify the view about changed items
629 m_assureVisibleCurrentIndex
= true;
632 void DolphinView::trashSelectedItems()
634 const KUrl::List list
= simplifiedSelectedUrls();
635 KonqOperations::del(this, KonqOperations::TRASH
, list
);
638 void DolphinView::deleteSelectedItems()
640 const KUrl::List list
= simplifiedSelectedUrls();
641 const bool del
= KonqOperations::askDeleteConfirmation(list
,
643 KonqOperations::DEFAULT_CONFIRMATION
,
647 KIO::Job
* job
= KIO::del(list
);
648 connect(job
, SIGNAL(result(KJob
*)),
649 this, SLOT(slotDeleteFileFinished(KJob
*)));
653 void DolphinView::cutSelectedItems()
655 QMimeData
* mimeData
= selectionMimeData();
656 KonqMimeData::addIsCutSelection(mimeData
, true);
657 QApplication::clipboard()->setMimeData(mimeData
);
660 void DolphinView::copySelectedItems()
662 QMimeData
* mimeData
= selectionMimeData();
663 QApplication::clipboard()->setMimeData(mimeData
);
666 void DolphinView::paste()
671 void DolphinView::pasteIntoFolder()
673 const KFileItemList items
= selectedItems();
674 if ((items
.count() == 1) && items
.first().isDir()) {
675 pasteToUrl(items
.first().url());
679 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
681 switch (event
->type()) {
682 case QEvent::FocusIn
:
683 if (watched
== m_container
) {
688 case QEvent::GraphicsSceneDragEnter
:
689 if (watched
== m_container
->controller()->view()) {
694 case QEvent::GraphicsSceneDragLeave
:
695 if (watched
== m_container
->controller()->view()) {
700 case QEvent::GraphicsSceneDrop
:
701 if (watched
== m_container
->controller()->view()) {
708 return QWidget::eventFilter(watched
, event
);
711 void DolphinView::wheelEvent(QWheelEvent
* event
)
713 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
714 const int numDegrees
= event
->delta() / 8;
715 const int numSteps
= numDegrees
/ 15;
717 setZoomLevel(zoomLevel() + numSteps
);
724 void DolphinView::hideEvent(QHideEvent
* event
)
727 QWidget::hideEvent(event
);
730 void DolphinView::activate()
735 void DolphinView::slotItemActivated(int index
)
737 const KFileItem item
= fileItemModel()->fileItem(index
);
738 if (!item
.isNull()) {
739 emit
itemActivated(item
);
743 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
745 Q_ASSERT(indexes
.count() >= 2);
749 KFileItemModel
* model
= fileItemModel();
750 QSetIterator
<int> it(indexes
);
751 while (it
.hasNext()) {
752 const int index
= it
.next();
753 items
.append(model
->fileItem(index
));
756 foreach (const KFileItem
& item
, items
) {
758 emit
tabRequested(item
.url());
760 emit
itemActivated(item
);
765 void DolphinView::slotItemMiddleClicked(int index
)
767 const KFileItem item
= fileItemModel()->fileItem(index
);
768 if (item
.isDir() || isTabsForFilesEnabled()) {
769 emit
tabRequested(item
.url());
773 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
775 const KFileItem item
= fileItemModel()->fileItem(index
);
776 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
779 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
781 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
784 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
786 ViewProperties
props(url());
788 QPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
790 KItemListView
* view
= m_container
->controller()->view();
791 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
793 bool nepomukRunning
= false;
794 bool indexingEnabled
= false;
796 nepomukRunning
= (Nepomuk::ResourceManager::instance()->init() == 0);
797 if (nepomukRunning
) {
798 KConfig
config("nepomukserverrc");
799 indexingEnabled
= config
.group("Service-nepomukfileindexer").readEntry("autostart", false);
804 QMenu
* groupMenu
= 0;
806 // Add all roles to the menu that can be shown or hidden by the user
807 const QList
<KFileItemModel::RoleInfo
> rolesInfo
= KFileItemModel::rolesInformation();
808 foreach (const KFileItemModel::RoleInfo
& info
, rolesInfo
) {
809 if (info
.role
== "name") {
810 // It should not be possible to hide the "name" role
814 const QString text
= fileItemModel()->roleDescription(info
.role
);
816 if (info
.group
.isEmpty()) {
817 action
= menu
->addAction(text
);
819 if (!groupMenu
|| info
.group
!= groupName
) {
820 groupName
= info
.group
;
821 groupMenu
= menu
->addMenu(groupName
);
824 action
= groupMenu
->addAction(text
);
827 action
->setCheckable(true);
828 action
->setChecked(visibleRolesSet
.contains(info
.role
));
829 action
->setData(info
.role
);
831 const bool enable
= (!info
.requiresNepomuk
&& !info
.requiresIndexer
) ||
832 (info
.requiresNepomuk
&& nepomukRunning
) ||
833 (info
.requiresIndexer
&& indexingEnabled
);
834 action
->setEnabled(enable
);
837 menu
->addSeparator();
839 QActionGroup
* widthsGroup
= new QActionGroup(menu
);
840 const bool autoColumnWidths
= props
.headerColumnWidths().isEmpty();
842 QAction
* autoAdjustWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Automatic Column Widths"));
843 autoAdjustWidthsAction
->setCheckable(true);
844 autoAdjustWidthsAction
->setChecked(autoColumnWidths
);
845 autoAdjustWidthsAction
->setActionGroup(widthsGroup
);
847 QAction
* customWidthsAction
= menu
->addAction(i18nc("@action:inmenu", "Custom Column Widths"));
848 customWidthsAction
->setCheckable(true);
849 customWidthsAction
->setChecked(!autoColumnWidths
);
850 customWidthsAction
->setActionGroup(widthsGroup
);
852 QAction
* action
= menu
->exec(pos
.toPoint());
853 if (menu
&& action
) {
854 KItemListHeader
* header
= view
->header();
856 if (action
== autoAdjustWidthsAction
) {
857 // Clear the column-widths from the viewproperties and turn on
858 // the automatic resizing of the columns
859 props
.setHeaderColumnWidths(QList
<int>());
860 header
->setAutomaticColumnResizing(true);
861 } else if (action
== customWidthsAction
) {
862 // Apply the current column-widths as custom column-widths and turn
863 // off the automatic resizing of the columns
864 QList
<int> columnWidths
;
865 foreach (const QByteArray
& role
, view
->visibleRoles()) {
866 columnWidths
.append(header
->columnWidth(role
));
868 props
.setHeaderColumnWidths(columnWidths
);
869 header
->setAutomaticColumnResizing(false);
871 // Show or hide the selected role
872 const QByteArray selectedRole
= action
->data().toByteArray();
874 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
875 if (action
->isChecked()) {
876 visibleRoles
.append(selectedRole
);
878 visibleRoles
.removeOne(selectedRole
);
881 view
->setVisibleRoles(visibleRoles
);
882 props
.setVisibleRoles(visibleRoles
);
884 QList
<int> columnWidths
;
885 if (!header
->automaticColumnResizing()) {
886 foreach (const QByteArray
& role
, view
->visibleRoles()) {
887 columnWidths
.append(header
->columnWidth(role
));
890 props
.setHeaderColumnWidths(columnWidths
);
897 void DolphinView::slotHeaderColumnWidthChanged(const QByteArray
& role
, qreal current
, qreal previous
)
901 const QList
<QByteArray
> visibleRoles
= m_container
->visibleRoles();
903 ViewProperties
props(url());
904 QList
<int> columnWidths
= props
.headerColumnWidths();
905 if (columnWidths
.count() != visibleRoles
.count()) {
906 columnWidths
.clear();
907 columnWidths
.reserve(visibleRoles
.count());
908 const KItemListHeader
* header
= m_container
->controller()->view()->header();
909 foreach (const QByteArray
& role
, visibleRoles
) {
910 const int width
= header
->columnWidth(role
);
911 columnWidths
.append(width
);
915 const int roleIndex
= visibleRoles
.indexOf(role
);
916 Q_ASSERT(roleIndex
>= 0 && roleIndex
< columnWidths
.count());
917 columnWidths
[roleIndex
] = current
;
919 props
.setHeaderColumnWidths(columnWidths
);
922 void DolphinView::slotItemHovered(int index
)
924 const KFileItem item
= fileItemModel()->fileItem(index
);
926 if (GeneralSettings::showToolTips() && !m_dragging
) {
927 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
928 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
929 itemRect
.moveTo(pos
);
931 m_toolTipManager
->showToolTip(item
, itemRect
);
934 emit
requestItemInfo(item
);
937 void DolphinView::slotItemUnhovered(int index
)
941 emit
requestItemInfo(KFileItem());
944 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
947 KFileItem destItem
= fileItemModel()->fileItem(index
);
948 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
949 // Use the URL of the view as drop target if the item is no directory
951 destItem
= fileItemModel()->rootItem();
954 // The item represents a directory or desktop-file
955 destUrl
= destItem
.url();
958 QDropEvent
dropEvent(event
->pos().toPoint(),
959 event
->possibleActions(),
964 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
965 if (!error
.isEmpty()) {
966 emit
errorMessage(error
);
969 if (destUrl
== url()) {
970 // Mark the dropped urls as selected.
971 markPastedUrlsAsSelected(event
->mimeData());
975 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
978 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
981 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
982 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
984 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
985 m_versionControlObserver
->setModel(fileItemModel
);
988 void DolphinView::slotMouseButtonPressed(int itemIndex
, Qt::MouseButtons buttons
)
993 // Trigger the history navigation only when clicking on the viewport:
994 // Above an item the XButtons provide a simple way to select items in
995 // the singleClick mode.
996 if (buttons
& Qt::XButton1
) {
997 emit
goBackRequested();
998 } else if (buttons
& Qt::XButton2
) {
999 emit
goForwardRequested();
1004 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
1006 const int currentCount
= current
.count();
1007 const int previousCount
= previous
.count();
1008 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
1009 (currentCount
> 0 && previousCount
== 0);
1011 // If nothing has been selected before and something got selected (or if something
1012 // was selected before and now nothing is selected) the selectionChangedSignal must
1013 // be emitted asynchronously as fast as possible to update the edit-actions.
1014 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
1015 m_selectionChangedTimer
->start();
1018 void DolphinView::emitSelectionChangedSignal()
1020 m_selectionChangedTimer
->stop();
1021 emit
selectionChanged(selectedItems());
1024 void DolphinView::updateSortRole(const QByteArray
& role
)
1026 ViewProperties
props(url());
1027 props
.setSortRole(role
);
1029 KItemModelBase
* model
= m_container
->controller()->model();
1030 model
->setSortRole(role
);
1032 emit
sortRoleChanged(role
);
1035 void DolphinView::updateSortOrder(Qt::SortOrder order
)
1037 ViewProperties
props(url());
1038 props
.setSortOrder(order
);
1040 KItemModelBase
* model
= fileItemModel();
1041 model
->setSortOrder(order
);
1043 emit
sortOrderChanged(order
);
1046 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
1048 ViewProperties
props(url());
1049 props
.setSortFoldersFirst(foldersFirst
);
1051 KFileItemModel
* model
= fileItemModel();
1052 model
->setSortFoldersFirst(foldersFirst
);
1054 emit
sortFoldersFirstChanged(foldersFirst
);
1057 QPair
<bool, QString
> DolphinView::pasteInfo() const
1059 return KonqOperations::pasteInfo(url());
1062 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
1064 m_tabsForFiles
= tabsForFiles
;
1067 bool DolphinView::isTabsForFilesEnabled() const
1069 return m_tabsForFiles
;
1072 bool DolphinView::itemsExpandable() const
1074 return m_mode
== DetailsView
;
1077 void DolphinView::restoreState(QDataStream
& stream
)
1079 // Restore the current item that had the keyboard focus
1080 stream
>> m_currentItemUrl
;
1082 // Restore the view position
1083 stream
>> m_restoredContentsPosition
;
1085 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
1088 fileItemModel()->restoreExpandedUrls(urls
);
1091 void DolphinView::saveState(QDataStream
& stream
)
1093 // Save the current item that has the keyboard focus
1094 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
1095 if (currentIndex
!= -1) {
1096 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
1097 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
1098 KUrl currentItemUrl
= item
.url();
1099 stream
<< currentItemUrl
;
1104 // Save view position
1105 const qreal x
= m_container
->horizontalScrollBar()->value();
1106 const qreal y
= m_container
->verticalScrollBar()->value();
1107 stream
<< QPoint(x
, y
);
1109 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1110 stream
<< fileItemModel()->expandedUrls();
1113 bool DolphinView::hasSelection() const
1115 return m_container
->controller()->selectionManager()->hasSelection();
1118 KFileItem
DolphinView::rootItem() const
1120 return m_dirLister
->rootItem();
1123 void DolphinView::observeCreatedItem(const KUrl
& url
)
1125 m_createdItemUrl
= url
;
1126 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1127 // this, SLOT(selectAndScrollToCreatedItem()));
1130 void DolphinView::selectAndScrollToCreatedItem()
1132 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1133 if (dirIndex.isValid()) {
1134 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1135 QAbstractItemView* view = m_viewAccessor.itemView();
1137 view->setCurrentIndex(proxyIndex);
1141 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1142 this, SLOT(selectAndScrollToCreatedItem()));*/
1143 m_createdItemUrl
= KUrl();
1146 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1148 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1149 emit
redirection(oldUrl
, newUrl
);
1150 m_url
= newUrl
; // #186947
1154 void DolphinView::updateViewState()
1156 if (m_currentItemUrl
!= KUrl()) {
1157 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1158 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1159 if (currentIndex
!= -1) {
1160 selectionManager
->setCurrentItem(currentIndex
);
1162 selectionManager
->setCurrentItem(0);
1164 m_currentItemUrl
= KUrl();
1167 if (!m_restoredContentsPosition
.isNull()) {
1168 const int x
= m_restoredContentsPosition
.x();
1169 const int y
= m_restoredContentsPosition
.y();
1170 m_restoredContentsPosition
= QPoint();
1172 m_container
->horizontalScrollBar()->setValue(x
);
1173 m_container
->verticalScrollBar()->setValue(y
);
1176 if (!m_selectedUrls
.isEmpty()) {
1179 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1180 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1181 const KFileItemModel
* model
= fileItemModel();
1183 foreach (const KUrl
& url
, m_selectedUrls
) {
1184 const int index
= model
->index(url
);
1186 selectedItems
.insert(index
);
1190 selectionManager
->setSelectedItems(selectedItems
);
1191 m_selectedUrls
.clear();
1195 void DolphinView::hideToolTip()
1197 if (GeneralSettings::showToolTips()) {
1198 m_toolTipManager
->hideToolTip();
1202 void DolphinView::showHoverInformation(const KFileItem
& item
)
1204 emit
requestItemInfo(item
);
1207 void DolphinView::clearHoverInformation()
1209 emit
requestItemInfo(KFileItem());
1212 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1214 if (job
->error() == 0) {
1215 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1216 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1217 emit
errorMessage(job
->errorString());
1221 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1223 // Disable the writestate temporary until it can be determined in a fast way
1224 // in DolphinView::slotLoadingCompleted()
1225 if (m_isFolderWritable
) {
1226 m_isFolderWritable
= false;
1227 emit
writeStateChanged(m_isFolderWritable
);
1230 emit
startedPathLoading(url
);
1233 void DolphinView::slotLoadingCompleted()
1235 // Update the view-state. This has to be done using a Qt::QueuedConnection
1236 // because the view might not be in its final state yet.
1237 QTimer::singleShot(0, this, SLOT(updateViewState()));
1239 emit
finishedPathLoading(url());
1241 updateWritableState();
1244 void DolphinView::slotRefreshItems()
1246 if (m_assureVisibleCurrentIndex
) {
1247 m_assureVisibleCurrentIndex
= false;
1248 //QAbstractItemView* view = m_viewAccessor.itemView();
1250 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1255 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1258 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1260 ViewProperties
props(url());
1261 props
.setSortOrder(current
);
1263 emit
sortOrderChanged(current
);
1266 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1269 Q_ASSERT(fileItemModel()->sortRole() == current
);
1271 ViewProperties
props(url());
1272 props
.setSortRole(current
);
1274 emit
sortRoleChanged(current
);
1277 void DolphinView::slotVisibleRolesChangedByHeader(const QList
<QByteArray
>& current
,
1278 const QList
<QByteArray
>& previous
)
1281 Q_ASSERT(m_container
->controller()->view()->visibleRoles() == current
);
1283 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1285 m_visibleRoles
= current
;
1287 ViewProperties
props(url());
1288 props
.setVisibleRoles(m_visibleRoles
);
1290 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1293 KFileItemModel
* DolphinView::fileItemModel() const
1295 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1298 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1300 if (!url
.isValid()) {
1301 const QString
location(url
.pathOrUrl());
1302 if (location
.isEmpty()) {
1303 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1305 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1310 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1313 void DolphinView::applyViewProperties()
1315 m_container
->beginTransaction();
1317 const ViewProperties
props(url());
1318 KFileItemModel
* model
= fileItemModel();
1320 const Mode mode
= props
.viewMode();
1321 if (m_mode
!= mode
) {
1322 const Mode previousMode
= m_mode
;
1325 // Changing the mode might result in changing
1326 // the zoom level. Remember the old zoom level so
1327 // that zoomLevelChanged() can get emitted.
1328 const int oldZoomLevel
= m_container
->zoomLevel();
1331 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1332 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1333 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1334 default: Q_ASSERT(false); break;
1337 emit
modeChanged(m_mode
, previousMode
);
1339 if (m_container
->zoomLevel() != oldZoomLevel
) {
1340 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1344 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1345 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1346 model
->setShowHiddenFiles(hiddenFilesShown
);
1347 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1350 const bool groupedSorting
= props
.groupedSorting();
1351 if (groupedSorting
!= model
->groupedSorting()) {
1352 model
->setGroupedSorting(groupedSorting
);
1353 emit
groupedSortingChanged(groupedSorting
);
1356 const QByteArray sortRole
= props
.sortRole();
1357 if (sortRole
!= model
->sortRole()) {
1358 model
->setSortRole(sortRole
);
1359 emit
sortRoleChanged(sortRole
);
1362 const Qt::SortOrder sortOrder
= props
.sortOrder();
1363 if (sortOrder
!= model
->sortOrder()) {
1364 model
->setSortOrder(sortOrder
);
1365 emit
sortOrderChanged(sortOrder
);
1368 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1369 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1370 model
->setSortFoldersFirst(sortFoldersFirst
);
1371 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1374 const QList
<QByteArray
> visibleRoles
= props
.visibleRoles();
1375 if (visibleRoles
!= m_visibleRoles
) {
1376 const QList
<QByteArray
> previousVisibleRoles
= m_visibleRoles
;
1377 m_visibleRoles
= visibleRoles
;
1378 m_container
->setVisibleRoles(visibleRoles
);
1379 emit
visibleRolesChanged(m_visibleRoles
, previousVisibleRoles
);
1382 const bool previewsShown
= props
.previewsShown();
1383 if (previewsShown
!= m_container
->previewsShown()) {
1384 const int oldZoomLevel
= zoomLevel();
1386 m_container
->setPreviewsShown(previewsShown
);
1387 emit
previewsShownChanged(previewsShown
);
1389 // Changing the preview-state might result in a changed zoom-level
1390 if (oldZoomLevel
!= zoomLevel()) {
1391 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1395 KItemListView
* itemListView
= m_container
->controller()->view();
1396 if (itemListView
->isHeaderVisible()) {
1397 KItemListHeader
* header
= itemListView
->header();
1398 const QList
<int> headerColumnWidths
= props
.headerColumnWidths();
1399 const int rolesCount
= m_visibleRoles
.count();
1400 if (headerColumnWidths
.count() == rolesCount
) {
1401 header
->setAutomaticColumnResizing(false);
1403 QHash
<QByteArray
, qreal
> columnWidths
;
1404 for (int i
= 0; i
< rolesCount
; ++i
) {
1405 columnWidths
.insert(m_visibleRoles
[i
], headerColumnWidths
[i
]);
1407 header
->setColumnWidths(columnWidths
);
1409 header
->setAutomaticColumnResizing(true);
1413 m_container
->endTransaction();
1416 void DolphinView::pasteToUrl(const KUrl
& url
)
1418 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1419 KonqOperations::doPaste(this, url
);
1422 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1426 const KFileItemList items
= selectedItems();
1427 foreach (const KFileItem
&item
, items
) {
1428 urls
.append(item
.url());
1431 if (itemsExpandable()) {
1432 // TODO: Check if we still need KDirModel for this in KDE 5.0
1433 urls
= KDirModel::simplifiedUrlList(urls
);
1439 QMimeData
* DolphinView::selectionMimeData() const
1441 const KFileItemModel
* model
= fileItemModel();
1442 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1443 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1445 return model
->createMimeData(selectedIndexes
);
1448 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1450 const KUrl::List sourceUrls
= KUrl::List::fromMimeData(mimeData
);
1451 KUrl::List destUrls
;
1452 foreach (const KUrl
& source
, sourceUrls
) {
1453 KUrl
destination(url().url() + "/" + source
.fileName());
1454 destUrls
<< destination
;
1456 markUrlsAsSelected(destUrls
);
1459 void DolphinView::updateWritableState()
1461 const bool wasFolderWritable
= m_isFolderWritable
;
1462 m_isFolderWritable
= true;
1464 const KFileItem item
= m_dirLister
->rootItem();
1465 if (!item
.isNull()) {
1466 KFileItemListProperties
capabilities(KFileItemList() << item
);
1467 m_isFolderWritable
= capabilities
.supportsWriting();
1469 if (m_isFolderWritable
!= wasFolderWritable
) {
1470 emit
writeStateChanged(m_isFolderWritable
);
1474 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1476 const KLocale
* locale
= KGlobal::locale();
1477 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1481 if (fileSize
< multiplier
) {
1482 // Show the size in bytes
1483 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1484 } else if (fileSize
< multiplier
* multiplier
) {
1485 // Show the size in kilobytes and always round up. This is done
1486 // for consistency with the values shown e.g. in the "Size" column
1487 // of the details-view.
1488 fileSize
+= (multiplier
/ 2) - 1;
1489 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1491 // Show the size in the best fitting unit having one decimal
1492 text
= locale
->formatByteSize(fileSize
, 1);
1497 #include "dolphinview.moc"