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 <QAbstractItemView>
24 #include <QApplication>
28 #include <QGraphicsSceneDragDropEvent>
30 #include <QItemSelection>
34 #include <KActionCollection>
35 #include <KColorScheme>
38 #include <KIconEffect>
40 #include <KFileItemListProperties>
42 #include <kitemviews/kfileitemmodel.h>
43 #include <kitemviews/kfileitemlistview.h>
44 #include <kitemviews/kitemlistselectionmanager.h>
45 #include <kitemviews/kitemlistview.h>
46 #include <kitemviews/kitemlistcontroller.h>
47 #include <KIO/DeleteJob>
48 #include <KIO/NetAccess>
49 #include <KIO/PreviewJob>
52 #include <KMessageBox>
53 #include <konq_fileitemcapabilities.h>
54 #include <konq_operations.h>
55 #include <konqmimedata.h>
56 #include <KToggleAction>
59 #include "additionalinfoaccessor.h"
60 #include "dolphindirlister.h"
61 #include "dolphinnewfilemenuobserver.h"
62 #include "dolphin_detailsmodesettings.h"
63 #include "dolphin_generalsettings.h"
64 #include "dolphinitemlistcontainer.h"
65 #include "draganddrophelper.h"
66 #include "renamedialog.h"
67 #include "versioncontrol/versioncontrolobserver.h"
68 #include "viewmodecontroller.h"
69 #include "viewproperties.h"
70 #include "views/tooltips/tooltipmanager.h"
71 #include "zoomlevelinfo.h"
74 const int MaxModeEnum
= DolphinView::CompactView
;
75 const int MaxSortingEnum
= DolphinView::SortByPath
;
78 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
81 m_tabsForFiles(false),
82 m_assureVisibleCurrentIndex(false),
83 m_isFolderWritable(true),
85 m_mode(DolphinView::IconsView
),
86 m_additionalInfoList(),
91 m_selectionChangedTimer(0),
93 m_restoredContentsPosition(),
96 m_versionControlObserver(0)
98 m_topLayout
= new QVBoxLayout(this);
99 m_topLayout
->setSpacing(0);
100 m_topLayout
->setMargin(0);
102 // When a new item has been created by the "Create New..." menu, the item should
103 // get selected and it must be assured that the item will get visible. As the
104 // creation is done asynchronously, several signals must be checked:
105 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
106 this, SLOT(observeCreatedItem(KUrl
)));
108 m_selectionChangedTimer
= new QTimer(this);
109 m_selectionChangedTimer
->setSingleShot(true);
110 m_selectionChangedTimer
->setInterval(300);
111 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
112 this, SLOT(emitSelectionChangedSignal()));
114 m_dirLister
= new DolphinDirLister(this);
115 m_dirLister
->setAutoUpdate(true);
116 m_dirLister
->setDelayedMimeTypes(true);
118 connect(m_dirLister
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
119 connect(m_dirLister
, SIGNAL(started(KUrl
)), this, SLOT(slotDirListerStarted(KUrl
)));
120 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
121 this, SLOT(slotRefreshItems()));
123 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
124 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
125 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
126 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
127 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
128 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
129 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
131 m_container
= new DolphinItemListContainer(m_dirLister
, this);
132 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
133 m_container
->installEventFilter(this);
134 setFocusProxy(m_container
);
135 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
136 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
138 KItemListController
* controller
= m_container
->controller();
139 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
140 if (GeneralSettings::autoExpandFolders()) {
141 controller
->setAutoActivationDelay(750);
143 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
144 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
145 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
146 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
147 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
148 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
149 connect(controller
, SIGNAL(itemPressed(int,Qt::MouseButton
)), this, SLOT(hideToolTip()));
150 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
151 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
152 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
153 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
155 KFileItemModel
* model
= fileItemModel();
157 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
160 KItemListView
* view
= controller
->view();
161 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
162 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
163 connect(view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
164 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
166 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
167 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
168 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
170 m_toolTipManager
= new ToolTipManager(this);
172 m_versionControlObserver
= new VersionControlObserver(this);
173 m_versionControlObserver
->setModel(model
);
174 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
175 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
176 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
178 applyViewProperties();
179 m_topLayout
->addWidget(m_container
);
184 DolphinView::~DolphinView()
188 KUrl
DolphinView::url() const
193 void DolphinView::setActive(bool active
)
195 if (active
== m_active
) {
201 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
206 QWidget
* viewport
= m_container
->viewport();
209 palette
.setColor(viewport
->backgroundRole(), color
);
210 viewport
->setPalette(palette
);
216 m_container
->setFocus();
218 emit
writeStateChanged(m_isFolderWritable
);
222 bool DolphinView::isActive() const
227 void DolphinView::setMode(Mode mode
)
229 if (mode
!= m_mode
) {
230 ViewProperties
props(url());
231 props
.setViewMode(mode
);
234 applyViewProperties();
238 DolphinView::Mode
DolphinView::mode() const
243 void DolphinView::setPreviewsShown(bool show
)
245 if (previewsShown() == show
) {
249 ViewProperties
props(url());
250 props
.setPreviewsShown(show
);
252 m_container
->setPreviewsShown(show
);
253 emit
previewsShownChanged(show
);
256 bool DolphinView::previewsShown() const
258 return m_container
->previewsShown();
261 void DolphinView::setHiddenFilesShown(bool show
)
263 if (m_dirLister
->showingDotFiles() == show
) {
267 const KFileItemList itemList
= selectedItems();
268 m_selectedUrls
.clear();
269 m_selectedUrls
= itemList
.urlList();
271 ViewProperties
props(url());
272 props
.setHiddenFilesShown(show
);
274 fileItemModel()->setShowHiddenFiles(show
);
275 emit
hiddenFilesShownChanged(show
);
278 bool DolphinView::hiddenFilesShown() const
280 return m_dirLister
->showingDotFiles();
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
305 return m_dirLister
->items();
308 KFileItemList
DolphinView::selectedItems() const
310 const KFileItemModel
* model
= fileItemModel();
311 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
312 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
314 KFileItemList selectedItems
;
315 QSetIterator
<int> it(selectedIndexes
);
316 while (it
.hasNext()) {
317 const int index
= it
.next();
318 selectedItems
.append(model
->fileItem(index
));
320 return selectedItems
;
323 int DolphinView::selectedItemsCount() const
325 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
326 return selectionManager
->selectedItems().count();
329 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
331 m_selectedUrls
= urls
;
334 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
336 m_currentItemUrl
= url
;
339 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
341 const KItemListSelectionManager::SelectionMode mode
= enabled
342 ? KItemListSelectionManager::Select
343 : KItemListSelectionManager::Deselect
;
344 const KFileItemModel
* model
= fileItemModel();
345 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
347 for (int index
= 0; index
< model
->count(); index
++) {
348 const KFileItem item
= model
->fileItem(index
);
349 if (pattern
.exactMatch(item
.text())) {
350 // An alternative approach would be to store the matching items in a QSet<int> and
351 // select them in one go after the loop, but we'd need a new function
352 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
354 selectionManager
->setSelected(index
, 1, mode
);
359 void DolphinView::setZoomLevel(int level
)
361 const int oldZoomLevel
= zoomLevel();
362 m_container
->setZoomLevel(level
);
363 if (zoomLevel() != oldZoomLevel
) {
364 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
368 int DolphinView::zoomLevel() const
370 return m_container
->zoomLevel();
373 void DolphinView::setSorting(Sorting sorting
)
375 if (sorting
!= this->sorting()) {
376 updateSorting(sorting
);
380 DolphinView::Sorting
DolphinView::sorting() const
382 KItemModelBase
* model
= m_container
->controller()->model();
383 return sortingForSortRole(model
->sortRole());
386 void DolphinView::setSortOrder(Qt::SortOrder order
)
388 if (sortOrder() != order
) {
389 updateSortOrder(order
);
393 Qt::SortOrder
DolphinView::sortOrder() const
395 KItemModelBase
* model
= fileItemModel();
396 return model
->sortOrder();
399 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
401 if (sortFoldersFirst() != foldersFirst
) {
402 updateSortFoldersFirst(foldersFirst
);
406 bool DolphinView::sortFoldersFirst() const
408 KFileItemModel
* model
= fileItemModel();
409 return model
->sortFoldersFirst();
412 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
414 const QList
<AdditionalInfo
> previousList
= info
;
416 ViewProperties
props(url());
417 props
.setAdditionalInfoList(info
);
419 m_additionalInfoList
= info
;
420 applyAdditionalInfoListToView();
422 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
425 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
427 return m_additionalInfoList
;
430 void DolphinView::reload()
432 QByteArray viewState
;
433 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
434 saveState(saveStream
);
436 const KFileItemList itemList
= selectedItems();
437 m_selectedUrls
.clear();
438 m_selectedUrls
= itemList
.urlList();
441 loadDirectory(url(), true);
443 QDataStream
restoreStream(viewState
);
444 restoreState(restoreStream
);
447 void DolphinView::stopLoading()
452 void DolphinView::refresh()
454 GeneralSettings::self()->readConfig();
455 m_container
->refresh();
456 applyViewProperties();
459 void DolphinView::setNameFilter(const QString
& nameFilter
)
461 fileItemModel()->setNameFilter(nameFilter
);
464 QString
DolphinView::nameFilter() const
466 return fileItemModel()->nameFilter();
469 void DolphinView::calculateItemCount(int& fileCount
,
471 KIO::filesize_t
& totalFileSize
) const
473 foreach (const KFileItem
& item
, m_dirLister
->items()) {
478 totalFileSize
+= item
.size();
483 QString
DolphinView::statusBarText() const
491 KIO::filesize_t totalFileSize
= 0;
493 if (hasSelection()) {
494 // Give a summary of the status of the selected files
495 const KFileItemList list
= selectedItems();
496 foreach (const KFileItem
& item
, list
) {
501 totalFileSize
+= item
.size();
505 if (folderCount
+ fileCount
== 1) {
506 // If only one item is selected, show the filename
507 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
509 // At least 2 items are selected
510 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
511 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
514 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
515 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
516 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
519 if (fileCount
> 0 && folderCount
> 0) {
520 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
521 foldersText
, filesText
, fileSizeText(totalFileSize
));
522 } else if (fileCount
> 0) {
523 summary
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, fileSizeText(totalFileSize
));
524 } else if (folderCount
> 0) {
525 summary
= foldersText
;
531 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
533 QList
<QAction
*> actions
;
535 if (items
.isEmpty()) {
536 const KFileItem item
= fileItemModel()->rootItem();
537 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
539 actions
= m_versionControlObserver
->actions(items
);
545 void DolphinView::setUrl(const KUrl
& url
)
551 emit
urlAboutToBeChanged(url
);
556 // It is important to clear the items from the model before
557 // applying the view properties, otherwise expensive operations
558 // might be done on the existing items although they get cleared
559 // anyhow afterwards by loadDirectory().
560 fileItemModel()->clear();
561 applyViewProperties();
564 emit
urlChanged(url
);
567 void DolphinView::selectAll()
569 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
570 selectionManager
->setSelected(0, fileItemModel()->count());
573 void DolphinView::invertSelection()
575 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
576 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
579 void DolphinView::clearSelection()
581 m_container
->controller()->selectionManager()->clearSelection();
584 void DolphinView::renameSelectedItems()
586 KFileItemList items
= selectedItems();
587 const int itemCount
= items
.count();
592 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
594 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
595 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
596 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
597 m_viewAccessor.itemView()->edit(proxyIndex);
599 RenameDialog
* dialog
= new RenameDialog(this, items
);
600 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
603 dialog
->activateWindow();
606 // assure that the current index remains visible when KDirLister
607 // will notify the view about changed items
608 m_assureVisibleCurrentIndex
= true;
611 void DolphinView::trashSelectedItems()
613 const KUrl::List list
= simplifiedSelectedUrls();
614 KonqOperations::del(this, KonqOperations::TRASH
, list
);
617 void DolphinView::deleteSelectedItems()
619 const KUrl::List list
= simplifiedSelectedUrls();
620 const bool del
= KonqOperations::askDeleteConfirmation(list
,
622 KonqOperations::DEFAULT_CONFIRMATION
,
626 KIO::Job
* job
= KIO::del(list
);
627 connect(job
, SIGNAL(result(KJob
*)),
628 this, SLOT(slotDeleteFileFinished(KJob
*)));
632 void DolphinView::cutSelectedItems()
634 QMimeData
* mimeData
= selectionMimeData();
635 KonqMimeData::addIsCutSelection(mimeData
, true);
636 QApplication::clipboard()->setMimeData(mimeData
);
639 void DolphinView::copySelectedItems()
641 QMimeData
* mimeData
= selectionMimeData();
642 QApplication::clipboard()->setMimeData(mimeData
);
645 void DolphinView::paste()
650 void DolphinView::pasteIntoFolder()
652 const KFileItemList items
= selectedItems();
653 if ((items
.count() == 1) && items
.first().isDir()) {
654 pasteToUrl(items
.first().url());
658 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
660 switch (event
->type()) {
661 case QEvent::FocusIn
:
662 if (watched
== m_container
) {
671 return QWidget::eventFilter(watched
, event
);
674 void DolphinView::wheelEvent(QWheelEvent
* event
)
676 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
677 const int numDegrees
= event
->delta() / 8;
678 const int numSteps
= numDegrees
/ 15;
680 setZoomLevel(zoomLevel() + numSteps
);
687 void DolphinView::activate()
692 void DolphinView::slotItemActivated(int index
)
694 const KFileItem item
= fileItemModel()->fileItem(index
);
695 if (!item
.isNull()) {
696 emit
itemActivated(item
);
700 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
702 Q_ASSERT(indexes
.count() >= 2);
706 KFileItemModel
* model
= fileItemModel();
707 QSetIterator
<int> it(indexes
);
708 while (it
.hasNext()) {
709 const int index
= it
.next();
710 items
.append(model
->fileItem(index
));
713 foreach (const KFileItem
& item
, items
) {
715 emit
tabRequested(item
.url());
717 emit
itemActivated(item
);
722 void DolphinView::slotItemMiddleClicked(int index
)
724 const KFileItem item
= fileItemModel()->fileItem(index
);
725 if (item
.isDir() || isTabsForFilesEnabled()) {
726 emit
tabRequested(item
.url());
730 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
732 const KFileItem item
= fileItemModel()->fileItem(index
);
733 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
736 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
738 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
741 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
743 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
745 KItemListView
* view
= m_container
->controller()->view();
746 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
748 // Add all roles to the menu that can be shown or hidden by the user
749 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
750 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
751 foreach (const DolphinView::AdditionalInfo info
, keys
) {
752 const QByteArray
& role
= infoAccessor
.role(info
);
753 if (role
!= "name") {
754 const QString text
= fileItemModel()->roleDescription(role
);
756 QAction
* action
= menu
.data()->addAction(text
);
757 action
->setCheckable(true);
758 action
->setChecked(visibleRolesSet
.contains(role
));
759 action
->setData(info
);
763 QAction
* action
= menu
.data()->exec(pos
.toPoint());
765 // Show or hide the selected role
766 const DolphinView::AdditionalInfo info
=
767 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
769 ViewProperties
props(url());
770 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
772 const QByteArray selectedRole
= infoAccessor
.role(info
);
773 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
775 if (action
->isChecked()) {
776 const int index
= keys
.indexOf(info
) + 1;
777 visibleRoles
.insert(index
, selectedRole
);
778 infoList
.insert(index
, info
);
780 visibleRoles
.removeOne(selectedRole
);
781 infoList
.removeOne(info
);
784 view
->setVisibleRoles(visibleRoles
);
785 props
.setAdditionalInfoList(infoList
);
791 void DolphinView::slotItemHovered(int index
)
793 const KFileItem item
= fileItemModel()->fileItem(index
);
795 if (GeneralSettings::showToolTips() && QApplication::mouseButtons() == Qt::NoButton
) {
796 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
797 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
798 itemRect
.moveTo(pos
);
800 m_toolTipManager
->showToolTip(item
, itemRect
);
803 emit
requestItemInfo(item
);
806 void DolphinView::slotItemUnhovered(int index
)
810 emit
requestItemInfo(KFileItem());
813 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
815 KFileItem destItem
= fileItemModel()->fileItem(index
);
816 if (destItem
.isNull()) {
817 destItem
= fileItemModel()->rootItem();
818 if (destItem
.isNull()) {
819 kWarning() << "No destination item available for drop operation.";
824 QDropEvent
dropEvent(event
->pos().toPoint(),
825 event
->possibleActions(),
830 const QString error
= DragAndDropHelper::dropUrls(destItem
, &dropEvent
);
831 if (!error
.isEmpty()) {
832 emit
errorMessage(error
);
836 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
839 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
842 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
843 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
845 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
846 m_versionControlObserver
->setModel(fileItemModel
);
849 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
851 const int currentCount
= current
.count();
852 const int previousCount
= previous
.count();
853 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
854 (currentCount
> 0 && previousCount
== 0);
856 // If nothing has been selected before and something got selected (or if something
857 // was selected before and now nothing is selected) the selectionChangedSignal must
858 // be emitted asynchronously as fast as possible to update the edit-actions.
859 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
860 m_selectionChangedTimer
->start();
863 void DolphinView::emitSelectionChangedSignal()
865 m_selectionChangedTimer
->stop();
866 emit
selectionChanged(selectedItems());
869 void DolphinView::dropUrls(const KFileItem
& destItem
,
870 const KUrl
& destPath
,
875 markPastedUrlsAsSelected(event
->mimeData());
876 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
879 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
881 ViewProperties
props(url());
882 props
.setSorting(sorting
);
884 KItemModelBase
* model
= m_container
->controller()->model();
885 model
->setSortRole(sortRoleForSorting(sorting
));
887 emit
sortingChanged(sorting
);
890 void DolphinView::updateSortOrder(Qt::SortOrder order
)
892 ViewProperties
props(url());
893 props
.setSortOrder(order
);
895 KItemModelBase
* model
= fileItemModel();
896 model
->setSortOrder(order
);
898 emit
sortOrderChanged(order
);
901 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
903 ViewProperties
props(url());
904 props
.setSortFoldersFirst(foldersFirst
);
906 KFileItemModel
* model
= fileItemModel();
907 model
->setSortFoldersFirst(foldersFirst
);
909 emit
sortFoldersFirstChanged(foldersFirst
);
912 QPair
<bool, QString
> DolphinView::pasteInfo() const
914 return KonqOperations::pasteInfo(url());
917 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
919 m_tabsForFiles
= tabsForFiles
;
922 bool DolphinView::isTabsForFilesEnabled() const
924 return m_tabsForFiles
;
927 bool DolphinView::itemsExpandable() const
929 return m_mode
== DetailsView
;
932 void DolphinView::restoreState(QDataStream
& stream
)
934 // Restore the current item that had the keyboard focus
935 stream
>> m_currentItemUrl
;
937 // Restore the view position
938 stream
>> m_restoredContentsPosition
;
940 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
943 fileItemModel()->restoreExpandedUrls(urls
);
946 void DolphinView::saveState(QDataStream
& stream
)
948 // Save the current item that has the keyboard focus
949 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
950 if (currentIndex
!= -1) {
951 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
952 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
953 KUrl currentItemUrl
= item
.url();
954 stream
<< currentItemUrl
;
959 // Save view position
960 const qreal x
= m_container
->horizontalScrollBar()->value();
961 const qreal y
= m_container
->verticalScrollBar()->value();
962 stream
<< QPoint(x
, y
);
964 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
965 stream
<< fileItemModel()->expandedUrls();
968 bool DolphinView::hasSelection() const
970 return m_container
->controller()->selectionManager()->hasSelection();
973 KFileItem
DolphinView::rootItem() const
975 return m_dirLister
->rootItem();
978 void DolphinView::observeCreatedItem(const KUrl
& url
)
980 m_createdItemUrl
= url
;
981 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
982 // this, SLOT(selectAndScrollToCreatedItem()));
985 void DolphinView::selectAndScrollToCreatedItem()
987 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
988 if (dirIndex.isValid()) {
989 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
990 QAbstractItemView* view = m_viewAccessor.itemView();
992 view->setCurrentIndex(proxyIndex);
996 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
997 this, SLOT(selectAndScrollToCreatedItem()));*/
998 m_createdItemUrl
= KUrl();
1001 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1003 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1004 emit
redirection(oldUrl
, newUrl
);
1005 m_url
= newUrl
; // #186947
1009 void DolphinView::updateViewState()
1011 if (m_currentItemUrl
!= KUrl()) {
1012 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1013 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1014 if (currentIndex
!= -1) {
1015 selectionManager
->setCurrentItem(currentIndex
);
1017 selectionManager
->setCurrentItem(0);
1019 m_currentItemUrl
= KUrl();
1022 if (!m_restoredContentsPosition
.isNull()) {
1023 const int x
= m_restoredContentsPosition
.x();
1024 const int y
= m_restoredContentsPosition
.y();
1025 m_restoredContentsPosition
= QPoint();
1027 m_container
->horizontalScrollBar()->setValue(x
);
1028 m_container
->verticalScrollBar()->setValue(y
);
1031 if (!m_selectedUrls
.isEmpty()) {
1032 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1033 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1034 const KFileItemModel
* model
= fileItemModel();
1036 foreach (const KUrl
& url
, m_selectedUrls
) {
1037 const int index
= model
->index(url
);
1039 selectedItems
.insert(index
);
1043 selectionManager
->setSelectedItems(selectedItems
);
1044 m_selectedUrls
.clear();
1048 void DolphinView::hideToolTip()
1050 if (GeneralSettings::showToolTips()) {
1051 m_toolTipManager
->hideToolTip();
1055 void DolphinView::showHoverInformation(const KFileItem
& item
)
1057 emit
requestItemInfo(item
);
1060 void DolphinView::clearHoverInformation()
1062 emit
requestItemInfo(KFileItem());
1065 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1067 if (job
->error() == 0) {
1068 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1069 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1070 emit
errorMessage(job
->errorString());
1074 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1076 // Disable the writestate temporary until it can be determined in a fast way
1077 // in DolphinView::slotLoadingCompleted()
1078 if (m_isFolderWritable
) {
1079 m_isFolderWritable
= false;
1080 emit
writeStateChanged(m_isFolderWritable
);
1083 emit
startedPathLoading(url
);
1086 void DolphinView::slotLoadingCompleted()
1088 // Update the view-state. This has to be done using a Qt::QueuedConnection
1089 // because the view might not be in its final state yet (the view also
1090 // listens to the completed()-signal from KDirLister and the order of
1091 // of slots is undefined).
1092 QTimer::singleShot(0, this, SLOT(updateViewState()));
1094 emit
finishedPathLoading(url());
1096 updateWritableState();
1099 void DolphinView::slotRefreshItems()
1101 if (m_assureVisibleCurrentIndex
) {
1102 m_assureVisibleCurrentIndex
= false;
1103 //QAbstractItemView* view = m_viewAccessor.itemView();
1105 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1110 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1113 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1115 ViewProperties
props(url());
1116 props
.setSortOrder(current
);
1118 emit
sortOrderChanged(current
);
1121 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1124 Q_ASSERT(fileItemModel()->sortRole() == current
);
1126 ViewProperties
props(url());
1127 const Sorting sorting
= sortingForSortRole(current
);
1128 props
.setSorting(sorting
);
1130 emit
sortingChanged(sorting
);
1133 KFileItemModel
* DolphinView::fileItemModel() const
1135 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1138 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1140 if (!url
.isValid()) {
1141 const QString
location(url
.pathOrUrl());
1142 if (location
.isEmpty()) {
1143 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1145 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1150 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1153 void DolphinView::applyViewProperties()
1155 m_container
->beginTransaction();
1157 const ViewProperties
props(url());
1158 KFileItemModel
* model
= fileItemModel();
1160 const Mode mode
= props
.viewMode();
1161 if (m_mode
!= mode
) {
1162 // Prevent an animated transition of the position and size of the items when switching
1163 // the view-mode by temporary clearing the model and updating it again after the view mode
1164 // has been modified.
1165 const bool restoreModel
= (model
->count() > 0);
1167 const int currentItemIndex
= m_container
->controller()->selectionManager()->currentItem();
1168 if (currentItemIndex
>= 0) {
1169 m_currentItemUrl
= model
->fileItem(currentItemIndex
).url();
1171 m_selectedUrls
= selectedItems().urlList();
1175 const Mode previousMode
= m_mode
;
1178 // Changing the mode might result in changing
1179 // the zoom level. Remember the old zoom level so
1180 // that zoomLevelChanged() can get emitted.
1181 const int oldZoomLevel
= m_container
->zoomLevel();
1184 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1185 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1186 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1187 default: Q_ASSERT(false); break;
1190 emit
modeChanged(m_mode
, previousMode
);
1192 if (m_container
->zoomLevel() != oldZoomLevel
) {
1193 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1197 loadDirectory(url());
1201 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1202 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1203 model
->setShowHiddenFiles(hiddenFilesShown
);
1204 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1207 const bool groupedSorting
= props
.groupedSorting();
1208 if (groupedSorting
!= model
->groupedSorting()) {
1209 model
->setGroupedSorting(groupedSorting
);
1210 emit
groupedSortingChanged(groupedSorting
);
1213 const DolphinView::Sorting sorting
= props
.sorting();
1214 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1215 if (newSortRole
!= model
->sortRole()) {
1216 model
->setSortRole(newSortRole
);
1217 emit
sortingChanged(sorting
);
1220 const Qt::SortOrder sortOrder
= props
.sortOrder();
1221 if (sortOrder
!= model
->sortOrder()) {
1222 model
->setSortOrder(sortOrder
);
1223 emit
sortOrderChanged(sortOrder
);
1226 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1227 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1228 model
->setSortFoldersFirst(sortFoldersFirst
);
1229 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1232 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1233 if (infoList
!= m_additionalInfoList
) {
1234 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1235 m_additionalInfoList
= infoList
;
1236 applyAdditionalInfoListToView();
1237 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1240 const bool previewsShown
= props
.previewsShown();
1241 if (previewsShown
!= m_container
->previewsShown()) {
1242 const int oldZoomLevel
= zoomLevel();
1244 m_container
->setPreviewsShown(previewsShown
);
1245 emit
previewsShownChanged(previewsShown
);
1247 // Changing the preview-state might result in a changed zoom-level
1248 if (oldZoomLevel
!= zoomLevel()) {
1249 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1253 m_container
->endTransaction();
1256 void DolphinView::applyAdditionalInfoListToView()
1258 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1260 QList
<QByteArray
> visibleRoles
;
1261 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1262 visibleRoles
.append("name");
1264 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1265 visibleRoles
.append(infoAccessor
.role(info
));
1268 m_container
->setVisibleRoles(visibleRoles
);
1271 void DolphinView::pasteToUrl(const KUrl
& url
)
1273 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1274 KonqOperations::doPaste(this, url
);
1277 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1281 const KFileItemList items
= selectedItems();
1282 foreach (const KFileItem
&item
, items
) {
1283 urls
.append(item
.url());
1286 if (itemsExpandable()) {
1287 // TODO: Check if we still need KDirModel for this in KDE 5.0
1288 urls
= KDirModel::simplifiedUrlList(urls
);
1294 QMimeData
* DolphinView::selectionMimeData() const
1296 const KFileItemModel
* model
= fileItemModel();
1297 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1298 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1300 return model
->createMimeData(selectedIndexes
);
1303 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1305 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1306 markUrlsAsSelected(urls
);
1309 void DolphinView::updateWritableState()
1311 const bool wasFolderWritable
= m_isFolderWritable
;
1312 m_isFolderWritable
= true;
1314 const KFileItem item
= m_dirLister
->rootItem();
1315 if (!item
.isNull()) {
1316 KFileItemListProperties
capabilities(KFileItemList() << item
);
1317 m_isFolderWritable
= capabilities
.supportsWriting();
1319 if (m_isFolderWritable
!= wasFolderWritable
) {
1320 emit
writeStateChanged(m_isFolderWritable
);
1324 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1327 case SortByName
: return "name";
1328 case SortBySize
: return "size";
1329 case SortByDate
: return "date";
1330 case SortByPermissions
: return "permissions";
1331 case SortByOwner
: return "owner";
1332 case SortByGroup
: return "group";
1333 case SortByType
: return "type";
1334 case SortByDestination
: return "destination";
1335 case SortByPath
: return "path";
1339 return QByteArray();
1342 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1344 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1345 if (sortHash
.isEmpty()) {
1346 sortHash
.insert("name", SortByName
);
1347 sortHash
.insert("size", SortBySize
);
1348 sortHash
.insert("date", SortByDate
);
1349 sortHash
.insert("permissions", SortByPermissions
);
1350 sortHash
.insert("owner", SortByOwner
);
1351 sortHash
.insert("group", SortByGroup
);
1352 sortHash
.insert("type", SortByType
);
1353 sortHash
.insert("destination", SortByDestination
);
1354 sortHash
.insert("path", SortByPath
);
1356 return sortHash
.value(sortRole
);
1359 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1361 const KLocale
* locale
= KGlobal::locale();
1362 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1366 if (fileSize
< multiplier
) {
1367 // Show the size in bytes
1368 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1369 } else if (fileSize
< multiplier
* multiplier
) {
1370 // Show the size in kilobytes and always round up. This is done
1371 // for consistency with the values shown e.g. in the "Size" column
1372 // of the details-view.
1373 fileSize
+= (multiplier
/ 2) - 1;
1374 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1376 // Show the size in the best fitting unit having one decimal
1377 text
= locale
->formatByteSize(fileSize
, 1);
1382 #include "dolphinview.moc"