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
);
136 KItemListController
* controller
= m_container
->controller();
137 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
138 if (GeneralSettings::autoExpandFolders()) {
139 controller
->setAutoActivationDelay(750);
141 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
142 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
143 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
144 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
145 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
146 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
147 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
148 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
149 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
150 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
152 KFileItemModel
* model
= fileItemModel();
154 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
157 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
158 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
159 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
161 m_toolTipManager
= new ToolTipManager(this);
163 m_versionControlObserver
= new VersionControlObserver(this);
164 m_versionControlObserver
->setModel(model
);
165 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
166 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
167 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
169 applyViewProperties();
170 m_topLayout
->addWidget(m_container
);
175 DolphinView::~DolphinView()
179 KUrl
DolphinView::url() const
184 void DolphinView::setActive(bool active
)
186 if (active
== m_active
) {
192 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
197 QWidget
* viewport
= m_container
->viewport();
200 palette
.setColor(viewport
->backgroundRole(), color
);
201 viewport
->setPalette(palette
);
207 m_container
->setFocus();
209 emit
writeStateChanged(m_isFolderWritable
);
213 bool DolphinView::isActive() const
218 void DolphinView::setMode(Mode mode
)
220 if (mode
!= m_mode
) {
221 ViewProperties
props(url());
222 props
.setViewMode(mode
);
225 applyViewProperties();
229 DolphinView::Mode
DolphinView::mode() const
234 void DolphinView::setPreviewsShown(bool show
)
236 if (previewsShown() == show
) {
240 ViewProperties
props(url());
241 props
.setPreviewsShown(show
);
243 m_container
->setPreviewsShown(show
);
244 emit
previewsShownChanged(show
);
247 bool DolphinView::previewsShown() const
249 return m_container
->previewsShown();
252 void DolphinView::setHiddenFilesShown(bool show
)
254 if (m_dirLister
->showingDotFiles() == show
) {
258 const KFileItemList itemList
= selectedItems();
259 m_selectedUrls
.clear();
260 m_selectedUrls
= itemList
.urlList();
262 ViewProperties
props(url());
263 props
.setHiddenFilesShown(show
);
265 m_dirLister
->setShowingDotFiles(show
);
266 m_dirLister
->emitChanges();
267 emit
hiddenFilesShownChanged(show
);
270 bool DolphinView::hiddenFilesShown() const
272 return m_dirLister
->showingDotFiles();
275 void DolphinView::setGroupedSorting(bool grouped
)
277 if (grouped
== groupedSorting()) {
281 ViewProperties
props(url());
282 props
.setGroupedSorting(grouped
);
285 m_container
->controller()->model()->setGroupedSorting(grouped
);
287 emit
groupedSortingChanged(grouped
);
290 bool DolphinView::groupedSorting() const
292 return fileItemModel()->groupedSorting();
295 KFileItemList
DolphinView::items() const
297 return m_dirLister
->items();
300 KFileItemList
DolphinView::selectedItems() const
302 const KFileItemModel
* model
= fileItemModel();
303 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
304 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
306 QList
<int> sortedIndexes
= selectedIndexes
.toList();
307 qSort(sortedIndexes
);
309 KFileItemList selectedItems
;
310 QListIterator
<int> it(sortedIndexes
);
311 while (it
.hasNext()) {
312 const int index
= it
.next();
313 selectedItems
.append(model
->fileItem(index
));
315 return selectedItems
;
318 int DolphinView::selectedItemsCount() const
320 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
321 return selectionManager
->selectedItems().count();
324 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
326 m_selectedUrls
= urls
;
329 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
331 m_currentItemUrl
= url
;
334 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
336 const KItemListSelectionManager::SelectionMode mode
= enabled
337 ? KItemListSelectionManager::Select
338 : KItemListSelectionManager::Deselect
;
339 const KFileItemModel
* model
= fileItemModel();
340 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
342 for (int index
= 0; index
< model
->count(); index
++) {
343 const KFileItem item
= model
->fileItem(index
);
344 if (pattern
.exactMatch(item
.text())) {
345 // An alternative approach would be to store the matching items in a QSet<int> and
346 // select them in one go after the loop, but we'd need a new function
347 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
349 selectionManager
->setSelected(index
, 1, mode
);
354 void DolphinView::setZoomLevel(int level
)
356 const int oldZoomLevel
= zoomLevel();
357 m_container
->setZoomLevel(level
);
358 if (zoomLevel() != oldZoomLevel
) {
359 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
363 int DolphinView::zoomLevel() const
365 return m_container
->zoomLevel();
368 void DolphinView::setSorting(Sorting sorting
)
370 if (sorting
!= this->sorting()) {
371 updateSorting(sorting
);
375 DolphinView::Sorting
DolphinView::sorting() const
377 KItemModelBase
* model
= m_container
->controller()->model();
378 return sortingForSortRole(model
->sortRole());
381 void DolphinView::setSortOrder(Qt::SortOrder order
)
383 if (sortOrder() != order
) {
384 updateSortOrder(order
);
388 Qt::SortOrder
DolphinView::sortOrder() const
390 KItemModelBase
* model
= fileItemModel();
391 return model
->sortOrder();
394 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
396 if (sortFoldersFirst() != foldersFirst
) {
397 updateSortFoldersFirst(foldersFirst
);
401 bool DolphinView::sortFoldersFirst() const
403 KFileItemModel
* model
= fileItemModel();
404 return model
->sortFoldersFirst();
407 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
409 const QList
<AdditionalInfo
> previousList
= info
;
411 ViewProperties
props(url());
412 props
.setAdditionalInfoList(info
);
414 m_additionalInfoList
= info
;
415 applyAdditionalInfoListToView();
417 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
420 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
422 return m_additionalInfoList
;
425 void DolphinView::reload()
427 QByteArray viewState
;
428 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
429 saveState(saveStream
);
431 const KFileItemList itemList
= selectedItems();
432 m_selectedUrls
.clear();
433 m_selectedUrls
= itemList
.urlList();
436 loadDirectory(url(), true);
438 QDataStream
restoreStream(viewState
);
439 restoreState(restoreStream
);
442 void DolphinView::stopLoading()
447 void DolphinView::refresh()
449 const bool oldActivationState
= m_active
;
450 const int oldZoomLevel
= zoomLevel();
453 applyViewProperties();
456 setActive(oldActivationState
);
457 updateZoomLevel(oldZoomLevel
);
460 void DolphinView::setNameFilter(const QString
& nameFilter
)
462 Q_UNUSED(nameFilter
);
463 //m_viewModeController->setNameFilter(nameFilter);
466 QString
DolphinView::nameFilter() const
468 return QString(); //m_viewModeController->nameFilter();
471 void DolphinView::calculateItemCount(int& fileCount
,
473 KIO::filesize_t
& totalFileSize
) const
475 foreach (const KFileItem
& item
, m_dirLister
->items()) {
480 totalFileSize
+= item
.size();
485 QString
DolphinView::statusBarText() const
493 KIO::filesize_t totalFileSize
= 0;
495 if (hasSelection()) {
496 // Give a summary of the status of the selected files
497 const KFileItemList list
= selectedItems();
498 foreach (const KFileItem
& item
, list
) {
503 totalFileSize
+= item
.size();
507 if (folderCount
+ fileCount
== 1) {
508 // If only one item is selected, show the filename
509 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
511 // At least 2 items are selected
512 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
513 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
516 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
517 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
518 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
521 if (fileCount
> 0 && folderCount
> 0) {
522 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
523 foldersText
, filesText
, fileSizeText(totalFileSize
));
524 } else if (fileCount
> 0) {
525 summary
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, fileSizeText(totalFileSize
));
526 } else if (folderCount
> 0) {
527 summary
= foldersText
;
533 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
535 QList
<QAction
*> actions
;
537 if (items
.isEmpty()) {
538 const KFileItem item
= fileItemModel()->rootItem();
539 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
541 actions
= m_versionControlObserver
->actions(items
);
547 void DolphinView::setUrl(const KUrl
& url
)
553 emit
urlAboutToBeChanged(url
);
556 if (GeneralSettings::showToolTips()) {
557 m_toolTipManager
->hideToolTip();
560 // It is important to clear the items from the model before
561 // applying the view properties, otherwise expensive operations
562 // might be done on the existing items although they get cleared
563 // anyhow afterwards by loadDirectory().
564 fileItemModel()->clear();
565 applyViewProperties();
568 emit
urlChanged(url
);
571 void DolphinView::selectAll()
573 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
574 selectionManager
->setSelected(0, fileItemModel()->count());
577 void DolphinView::invertSelection()
579 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
580 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
583 void DolphinView::clearSelection()
585 m_container
->controller()->selectionManager()->clearSelection();
588 void DolphinView::renameSelectedItems()
590 KFileItemList items
= selectedItems();
591 const int itemCount
= items
.count();
596 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
598 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
599 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
600 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
601 m_viewAccessor.itemView()->edit(proxyIndex);
603 RenameDialog
* dialog
= new RenameDialog(this, items
);
604 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
607 dialog
->activateWindow();
610 // assure that the current index remains visible when KDirLister
611 // will notify the view about changed items
612 m_assureVisibleCurrentIndex
= true;
615 void DolphinView::trashSelectedItems()
617 const KUrl::List list
= simplifiedSelectedUrls();
618 KonqOperations::del(this, KonqOperations::TRASH
, list
);
621 void DolphinView::deleteSelectedItems()
623 const KUrl::List list
= simplifiedSelectedUrls();
624 const bool del
= KonqOperations::askDeleteConfirmation(list
,
626 KonqOperations::DEFAULT_CONFIRMATION
,
630 KIO::Job
* job
= KIO::del(list
);
631 connect(job
, SIGNAL(result(KJob
*)),
632 this, SLOT(slotDeleteFileFinished(KJob
*)));
636 void DolphinView::cutSelectedItems()
638 QMimeData
* mimeData
= selectionMimeData();
639 KonqMimeData::addIsCutSelection(mimeData
, true);
640 QApplication::clipboard()->setMimeData(mimeData
);
643 void DolphinView::copySelectedItems()
645 QMimeData
* mimeData
= selectionMimeData();
646 QApplication::clipboard()->setMimeData(mimeData
);
649 void DolphinView::paste()
654 void DolphinView::pasteIntoFolder()
656 const KFileItemList items
= selectedItems();
657 if ((items
.count() == 1) && items
.first().isDir()) {
658 pasteToUrl(items
.first().url());
662 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
664 switch (event
->type()) {
665 case QEvent::FocusIn
:
666 if (watched
== m_container
) {
675 return QWidget::eventFilter(watched
, event
);
678 void DolphinView::wheelEvent(QWheelEvent
* event
)
680 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
681 const int numDegrees
= event
->delta() / 8;
682 const int numSteps
= numDegrees
/ 15;
684 setZoomLevel(zoomLevel() + numSteps
);
691 void DolphinView::activate()
696 void DolphinView::slotItemActivated(int index
)
698 const KFileItem item
= fileItemModel()->fileItem(index
);
699 if (!item
.isNull()) {
700 emit
itemActivated(item
);
704 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
706 Q_ASSERT(indexes
.count() >= 2);
710 KFileItemModel
* model
= fileItemModel();
711 QSetIterator
<int> it(indexes
);
712 while (it
.hasNext()) {
713 const int index
= it
.next();
714 items
.append(model
->fileItem(index
));
717 foreach (const KFileItem
& item
, items
) {
719 emit
tabRequested(item
.url());
721 emit
itemActivated(item
);
726 void DolphinView::slotItemMiddleClicked(int index
)
728 const KFileItem item
= fileItemModel()->fileItem(index
);
729 if (item
.isDir() || isTabsForFilesEnabled()) {
730 emit
tabRequested(item
.url());
734 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
736 if (GeneralSettings::showToolTips()) {
737 m_toolTipManager
->hideToolTip();
739 const KFileItem item
= fileItemModel()->fileItem(index
);
740 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
743 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
745 if (GeneralSettings::showToolTips()) {
746 m_toolTipManager
->hideToolTip();
748 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
751 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
753 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
755 KItemListView
* view
= m_container
->controller()->view();
756 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
758 // Add all roles to the menu that can be shown or hidden by the user
759 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
760 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
761 foreach (const DolphinView::AdditionalInfo info
, keys
) {
762 const QByteArray
& role
= infoAccessor
.role(info
);
763 if (role
!= "name") {
764 const QString text
= fileItemModel()->roleDescription(role
);
766 QAction
* action
= menu
.data()->addAction(text
);
767 action
->setCheckable(true);
768 action
->setChecked(visibleRolesSet
.contains(role
));
769 action
->setData(info
);
773 QAction
* action
= menu
.data()->exec(pos
.toPoint());
775 // Show or hide the selected role
776 const DolphinView::AdditionalInfo info
=
777 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
779 ViewProperties
props(url());
780 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
782 const QByteArray selectedRole
= infoAccessor
.role(info
);
783 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
785 if (action
->isChecked()) {
786 const int index
= keys
.indexOf(info
) + 1;
787 visibleRoles
.insert(index
, selectedRole
);
788 infoList
.insert(index
, info
);
790 visibleRoles
.removeOne(selectedRole
);
791 infoList
.removeOne(info
);
794 view
->setVisibleRoles(visibleRoles
);
795 props
.setAdditionalInfoList(infoList
);
801 void DolphinView::slotItemHovered(int index
)
803 const KFileItem item
= fileItemModel()->fileItem(index
);
805 if (GeneralSettings::showToolTips()) {
806 QRectF itemRect
= m_container
->controller()->view()->itemRect(index
);
807 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
808 itemRect
.moveTo(pos
);
810 m_toolTipManager
->showToolTip(item
, itemRect
);
813 emit
requestItemInfo(item
);
816 void DolphinView::slotItemUnhovered(int index
)
819 if (GeneralSettings::showToolTips()) {
820 m_toolTipManager
->hideToolTip();
822 emit
requestItemInfo(KFileItem());
825 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
827 const KFileItem destItem
= fileItemModel()->fileItem(index
);
829 QDropEvent
dropEvent(event
->pos().toPoint(),
830 event
->possibleActions(),
835 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
838 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
841 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
844 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
845 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
847 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
848 m_versionControlObserver
->setModel(fileItemModel
);
851 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
853 const int currentCount
= current
.count();
854 const int previousCount
= previous
.count();
855 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
856 (currentCount
> 0 && previousCount
== 0);
858 // If nothing has been selected before and something got selected (or if something
859 // was selected before and now nothing is selected) the selectionChangedSignal must
860 // be emitted asynchronously as fast as possible to update the edit-actions.
861 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
862 m_selectionChangedTimer
->start();
865 void DolphinView::emitSelectionChangedSignal()
867 m_selectionChangedTimer
->stop();
868 emit
selectionChanged(selectedItems());
871 void DolphinView::dropUrls(const KFileItem
& destItem
,
872 const KUrl
& destPath
,
877 markPastedUrlsAsSelected(event
->mimeData());
878 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
881 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
883 ViewProperties
props(url());
884 props
.setSorting(sorting
);
886 KItemModelBase
* model
= m_container
->controller()->model();
887 model
->setSortRole(sortRoleForSorting(sorting
));
889 emit
sortingChanged(sorting
);
892 void DolphinView::updateSortOrder(Qt::SortOrder order
)
894 ViewProperties
props(url());
895 props
.setSortOrder(order
);
897 KItemModelBase
* model
= fileItemModel();
898 model
->setSortOrder(order
);
900 emit
sortOrderChanged(order
);
903 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
905 ViewProperties
props(url());
906 props
.setSortFoldersFirst(foldersFirst
);
908 KFileItemModel
* model
= fileItemModel();
909 model
->setSortFoldersFirst(foldersFirst
);
911 emit
sortFoldersFirstChanged(foldersFirst
);
914 QPair
<bool, QString
> DolphinView::pasteInfo() const
916 return KonqOperations::pasteInfo(url());
919 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
921 m_tabsForFiles
= tabsForFiles
;
924 bool DolphinView::isTabsForFilesEnabled() const
926 return m_tabsForFiles
;
929 bool DolphinView::itemsExpandable() const
931 return m_mode
== DetailsView
;
934 void DolphinView::restoreState(QDataStream
& stream
)
936 // Restore the current item that had the keyboard focus
937 stream
>> m_currentItemUrl
;
939 // Restore the view position
940 stream
>> m_restoredContentsPosition
;
942 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
945 fileItemModel()->restoreExpandedUrls(urls
);
948 void DolphinView::saveState(QDataStream
& stream
)
950 // Save the current item that has the keyboard focus
951 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
952 if (currentIndex
!= -1) {
953 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
954 KUrl currentItemUrl
= item
.url();
955 stream
<< currentItemUrl
;
960 // Save view position
961 const qreal x
= m_container
->horizontalScrollBar()->value();
962 const qreal y
= m_container
->verticalScrollBar()->value();
963 stream
<< QPoint(x
, y
);
965 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
966 stream
<< fileItemModel()->expandedUrls();
969 bool DolphinView::hasSelection() const
971 return m_container
->controller()->selectionManager()->hasSelection();
974 KFileItem
DolphinView::rootItem() const
976 return m_dirLister
->rootItem();
979 void DolphinView::observeCreatedItem(const KUrl
& url
)
981 m_createdItemUrl
= url
;
982 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
983 // this, SLOT(selectAndScrollToCreatedItem()));
986 void DolphinView::selectAndScrollToCreatedItem()
988 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
989 if (dirIndex.isValid()) {
990 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
991 QAbstractItemView* view = m_viewAccessor.itemView();
993 view->setCurrentIndex(proxyIndex);
997 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
998 this, SLOT(selectAndScrollToCreatedItem()));*/
999 m_createdItemUrl
= KUrl();
1002 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1004 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1005 emit
redirection(oldUrl
, newUrl
);
1006 m_url
= newUrl
; // #186947
1010 void DolphinView::updateViewState()
1012 if (m_currentItemUrl
!= KUrl()) {
1013 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1014 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1015 if (currentIndex
!= -1) {
1016 selectionManager
->setCurrentItem(currentIndex
);
1018 selectionManager
->setCurrentItem(0);
1020 m_currentItemUrl
= KUrl();
1023 if (!m_restoredContentsPosition
.isNull()) {
1024 const int x
= m_restoredContentsPosition
.x();
1025 const int y
= m_restoredContentsPosition
.y();
1026 m_restoredContentsPosition
= QPoint();
1028 m_container
->horizontalScrollBar()->setValue(x
);
1029 m_container
->verticalScrollBar()->setValue(y
);
1032 if (!m_selectedUrls
.isEmpty()) {
1033 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1034 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1035 const KFileItemModel
* model
= fileItemModel();
1037 foreach (const KUrl
& url
, m_selectedUrls
) {
1038 const int index
= model
->index(url
);
1040 selectedItems
.insert(index
);
1044 selectionManager
->setSelectedItems(selectedItems
);
1045 m_selectedUrls
.clear();
1049 void DolphinView::showHoverInformation(const KFileItem
& item
)
1051 emit
requestItemInfo(item
);
1054 void DolphinView::clearHoverInformation()
1056 emit
requestItemInfo(KFileItem());
1059 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1061 if (job
->error() == 0) {
1062 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1063 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1064 emit
errorMessage(job
->errorString());
1068 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1070 // Disable the writestate temporary until it can be determined in a fast way
1071 // in DolphinView::slotLoadingCompleted()
1072 if (m_isFolderWritable
) {
1073 m_isFolderWritable
= false;
1074 emit
writeStateChanged(m_isFolderWritable
);
1077 emit
startedPathLoading(url
);
1080 void DolphinView::slotLoadingCompleted()
1082 // Update the view-state. This has to be done using a Qt::QueuedConnection
1083 // because the view might not be in its final state yet (the view also
1084 // listens to the completed()-signal from KDirLister and the order of
1085 // of slots is undefined).
1086 QTimer::singleShot(0, this, SLOT(updateViewState()));
1088 emit
finishedPathLoading(url());
1090 updateWritableState();
1093 void DolphinView::slotRefreshItems()
1095 if (m_assureVisibleCurrentIndex
) {
1096 m_assureVisibleCurrentIndex
= false;
1097 //QAbstractItemView* view = m_viewAccessor.itemView();
1099 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1104 KFileItemModel
* DolphinView::fileItemModel() const
1106 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1109 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1111 if (!url
.isValid()) {
1112 const QString
location(url
.pathOrUrl());
1113 if (location
.isEmpty()) {
1114 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1116 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1121 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1124 void DolphinView::applyViewProperties()
1126 m_container
->beginTransaction();
1128 const ViewProperties
props(url());
1130 const Mode mode
= props
.viewMode();
1131 if (m_mode
!= mode
) {
1132 const Mode previousMode
= m_mode
;
1135 // Changing the mode might result in changing
1136 // the zoom level. Remember the old zoom level so
1137 // that zoomLevelChanged() can get emitted.
1138 const int oldZoomLevel
= m_container
->zoomLevel();
1141 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1142 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1143 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1144 default: Q_ASSERT(false); break;
1147 emit
modeChanged(m_mode
, previousMode
);
1149 if (m_container
->zoomLevel() != oldZoomLevel
) {
1150 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1154 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1155 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1156 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1157 m_dirLister
->emitChanges();
1158 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1161 KFileItemModel
* model
= fileItemModel();
1163 const bool groupedSorting
= props
.groupedSorting();
1164 if (groupedSorting
!= model
->groupedSorting()) {
1165 model
->setGroupedSorting(groupedSorting
);
1166 emit
groupedSortingChanged(groupedSorting
);
1169 const DolphinView::Sorting sorting
= props
.sorting();
1170 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1171 if (newSortRole
!= model
->sortRole()) {
1172 model
->setSortRole(newSortRole
);
1173 emit
sortingChanged(sorting
);
1176 const Qt::SortOrder sortOrder
= props
.sortOrder();
1177 if (sortOrder
!= model
->sortOrder()) {
1178 model
->setSortOrder(sortOrder
);
1179 emit
sortOrderChanged(sortOrder
);
1182 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1183 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1184 model
->setSortFoldersFirst(sortFoldersFirst
);
1185 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1188 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1189 if (infoList
!= m_additionalInfoList
) {
1190 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1191 m_additionalInfoList
= infoList
;
1192 applyAdditionalInfoListToView();
1193 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1196 const bool previewsShown
= props
.previewsShown();
1197 if (previewsShown
!= m_container
->previewsShown()) {
1198 const int oldZoomLevel
= zoomLevel();
1200 m_container
->setPreviewsShown(previewsShown
);
1201 emit
previewsShownChanged(previewsShown
);
1203 // Changing the preview-state might result in a changed zoom-level
1204 if (oldZoomLevel
!= zoomLevel()) {
1205 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1209 m_container
->endTransaction();
1212 void DolphinView::applyAdditionalInfoListToView()
1214 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1216 QList
<QByteArray
> visibleRoles
;
1217 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1218 visibleRoles
.append("name");
1220 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1221 visibleRoles
.append(infoAccessor
.role(info
));
1224 m_container
->setVisibleRoles(visibleRoles
);
1227 void DolphinView::pasteToUrl(const KUrl
& url
)
1229 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1230 KonqOperations::doPaste(this, url
);
1233 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1235 Q_UNUSED(oldZoomLevel
);
1236 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1237 if (oldZoomLevel != newZoomLevel) {
1238 m_viewModeController->setZoomLevel(newZoomLevel);
1239 emit zoomLevelChanged(newZoomLevel);
1243 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1247 const KFileItemList items
= selectedItems();
1248 foreach (const KFileItem
&item
, items
) {
1249 urls
.append(item
.url());
1252 if (itemsExpandable()) {
1253 // TODO: Check if we still need KDirModel for this in KDE 5.0
1254 urls
= KDirModel::simplifiedUrlList(urls
);
1260 QMimeData
* DolphinView::selectionMimeData() const
1262 const KFileItemModel
* model
= fileItemModel();
1263 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1264 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1266 return model
->createMimeData(selectedIndexes
);
1269 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1271 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1272 markUrlsAsSelected(urls
);
1275 void DolphinView::updateWritableState()
1277 const bool wasFolderWritable
= m_isFolderWritable
;
1278 m_isFolderWritable
= true;
1280 const KFileItem item
= m_dirLister
->rootItem();
1281 if (!item
.isNull()) {
1282 KFileItemListProperties
capabilities(KFileItemList() << item
);
1283 m_isFolderWritable
= capabilities
.supportsWriting();
1285 if (m_isFolderWritable
!= wasFolderWritable
) {
1286 emit
writeStateChanged(m_isFolderWritable
);
1290 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1293 case SortByName
: return "name";
1294 case SortBySize
: return "size";
1295 case SortByDate
: return "date";
1296 case SortByPermissions
: return "permissions";
1297 case SortByOwner
: return "owner";
1298 case SortByGroup
: return "group";
1299 case SortByType
: return "type";
1300 case SortByDestination
: return "destination";
1301 case SortByPath
: return "path";
1305 return QByteArray();
1308 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1310 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1311 if (sortHash
.isEmpty()) {
1312 sortHash
.insert("name", SortByName
);
1313 sortHash
.insert("size", SortBySize
);
1314 sortHash
.insert("date", SortByDate
);
1315 sortHash
.insert("permissions", SortByPermissions
);
1316 sortHash
.insert("owner", SortByOwner
);
1317 sortHash
.insert("group", SortByGroup
);
1318 sortHash
.insert("type", SortByType
);
1319 sortHash
.insert("destination", SortByDestination
);
1320 sortHash
.insert("path", SortByPath
);
1322 return sortHash
.value(sortRole
);
1325 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1327 const KLocale
* locale
= KGlobal::locale();
1328 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1332 if (fileSize
< multiplier
) {
1333 // Show the size in bytes
1334 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1335 } else if (fileSize
< multiplier
* multiplier
) {
1336 // Show the size in kilobytes and always round up. This is done
1337 // for consistency with the values shown e.g. in the "Size" column
1338 // of the details-view.
1339 fileSize
+= (multiplier
/ 2) - 1;
1340 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1342 // Show the size in the best fitting unit having one decimal
1343 text
= locale
->formatByteSize(fileSize
, 1);
1348 #include "dolphinview.moc"