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 connect(controller
, SIGNAL(itemActivated(int)),
139 this, SLOT(slotItemActivated(int)));
140 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
141 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
142 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
143 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
144 connect(controller
, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
145 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
146 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
147 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
148 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
150 KFileItemModel
* model
= fileItemModel();
152 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
155 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
156 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
157 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
159 m_toolTipManager
= new ToolTipManager(this);
161 m_versionControlObserver
= new VersionControlObserver(this);
162 m_versionControlObserver
->setModel(model
);
163 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
164 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
165 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
167 applyViewProperties();
168 m_topLayout
->addWidget(m_container
);
173 DolphinView::~DolphinView()
177 KUrl
DolphinView::url() const
182 void DolphinView::setActive(bool active
)
184 if (active
== m_active
) {
190 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
195 QWidget
* viewport
= m_container
->viewport();
198 palette
.setColor(viewport
->backgroundRole(), color
);
199 viewport
->setPalette(palette
);
205 m_container
->setFocus();
207 emit
writeStateChanged(m_isFolderWritable
);
211 bool DolphinView::isActive() const
216 void DolphinView::setMode(Mode mode
)
218 if (mode
!= m_mode
) {
219 ViewProperties
props(url());
220 props
.setViewMode(mode
);
223 applyViewProperties();
227 DolphinView::Mode
DolphinView::mode() const
232 void DolphinView::setPreviewsShown(bool show
)
234 if (previewsShown() == show
) {
238 ViewProperties
props(url());
239 props
.setPreviewsShown(show
);
241 m_container
->setPreviewsShown(show
);
242 emit
previewsShownChanged(show
);
245 bool DolphinView::previewsShown() const
247 return m_container
->previewsShown();
250 void DolphinView::setHiddenFilesShown(bool show
)
252 if (m_dirLister
->showingDotFiles() == show
) {
256 const KFileItemList itemList
= selectedItems();
257 m_selectedUrls
.clear();
258 m_selectedUrls
= itemList
.urlList();
260 ViewProperties
props(url());
261 props
.setHiddenFilesShown(show
);
263 m_dirLister
->setShowingDotFiles(show
);
264 m_dirLister
->emitChanges();
265 emit
hiddenFilesShownChanged(show
);
268 bool DolphinView::hiddenFilesShown() const
270 return m_dirLister
->showingDotFiles();
273 void DolphinView::setGroupedSorting(bool grouped
)
275 if (grouped
== groupedSorting()) {
279 ViewProperties
props(url());
280 props
.setGroupedSorting(grouped
);
283 m_container
->controller()->model()->setGroupedSorting(grouped
);
285 emit
groupedSortingChanged(grouped
);
288 bool DolphinView::groupedSorting() const
290 return fileItemModel()->groupedSorting();
293 KFileItemList
DolphinView::items() const
295 return m_dirLister
->items();
298 KFileItemList
DolphinView::selectedItems() const
300 const KFileItemModel
* model
= fileItemModel();
301 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
302 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
304 QList
<int> sortedIndexes
= selectedIndexes
.toList();
305 qSort(sortedIndexes
);
307 KFileItemList selectedItems
;
308 QListIterator
<int> it(sortedIndexes
);
309 while (it
.hasNext()) {
310 const int index
= it
.next();
311 selectedItems
.append(model
->fileItem(index
));
313 return selectedItems
;
316 int DolphinView::selectedItemsCount() const
318 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
319 return selectionManager
->selectedItems().count();
322 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
324 m_selectedUrls
= urls
;
327 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
329 m_currentItemUrl
= url
;
332 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
334 const KItemListSelectionManager::SelectionMode mode
= enabled
335 ? KItemListSelectionManager::Select
336 : KItemListSelectionManager::Deselect
;
337 const KFileItemModel
* model
= fileItemModel();
338 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
340 for (int index
= 0; index
< model
->count(); index
++) {
341 const KFileItem item
= model
->fileItem(index
);
342 if (pattern
.exactMatch(item
.name())) {
343 // An alternative approach would be to store the matching items in a QSet<int> and
344 // select them in one go after the loop, but we'd need a new function
345 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
347 selectionManager
->setSelected(index
, 1, mode
);
352 void DolphinView::setZoomLevel(int level
)
354 const int oldZoomLevel
= zoomLevel();
355 m_container
->setZoomLevel(level
);
356 if (zoomLevel() != oldZoomLevel
) {
357 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
361 int DolphinView::zoomLevel() const
363 return m_container
->zoomLevel();
366 void DolphinView::setSorting(Sorting sorting
)
368 if (sorting
!= this->sorting()) {
369 updateSorting(sorting
);
373 DolphinView::Sorting
DolphinView::sorting() const
375 KItemModelBase
* model
= m_container
->controller()->model();
376 return sortingForSortRole(model
->sortRole());
379 void DolphinView::setSortOrder(Qt::SortOrder order
)
381 if (sortOrder() != order
) {
382 updateSortOrder(order
);
386 Qt::SortOrder
DolphinView::sortOrder() const
388 KItemModelBase
* model
= fileItemModel();
389 return model
->sortOrder();
392 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
394 if (sortFoldersFirst() != foldersFirst
) {
395 updateSortFoldersFirst(foldersFirst
);
399 bool DolphinView::sortFoldersFirst() const
401 KFileItemModel
* model
= fileItemModel();
402 return model
->sortFoldersFirst();
405 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
407 const QList
<AdditionalInfo
> previousList
= info
;
409 ViewProperties
props(url());
410 props
.setAdditionalInfoList(info
);
412 m_additionalInfoList
= info
;
413 applyAdditionalInfoListToView();
415 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
418 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
420 return m_additionalInfoList
;
423 void DolphinView::reload()
425 QByteArray viewState
;
426 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
427 saveState(saveStream
);
429 const KFileItemList itemList
= selectedItems();
430 m_selectedUrls
.clear();
431 m_selectedUrls
= itemList
.urlList();
434 loadDirectory(url(), true);
436 QDataStream
restoreStream(viewState
);
437 restoreState(restoreStream
);
440 void DolphinView::stopLoading()
445 void DolphinView::refresh()
447 const bool oldActivationState
= m_active
;
448 const int oldZoomLevel
= zoomLevel();
451 applyViewProperties();
454 setActive(oldActivationState
);
455 updateZoomLevel(oldZoomLevel
);
458 void DolphinView::setNameFilter(const QString
& nameFilter
)
460 Q_UNUSED(nameFilter
);
461 //m_viewModeController->setNameFilter(nameFilter);
464 QString
DolphinView::nameFilter() const
466 return QString(); //m_viewModeController->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
);
554 if (GeneralSettings::showToolTips()) {
555 m_toolTipManager
->hideToolTip();
558 // It is important to clear the items from the model before
559 // applying the view properties, otherwise expensive operations
560 // might be done on the existing items although they get cleared
561 // anyhow afterwards by loadDirectory().
562 fileItemModel()->clear();
563 applyViewProperties();
566 emit
urlChanged(url
);
569 void DolphinView::selectAll()
571 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
572 selectionManager
->setSelected(0, fileItemModel()->count());
575 void DolphinView::invertSelection()
577 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
578 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
581 void DolphinView::clearSelection()
583 m_container
->controller()->selectionManager()->clearSelection();
586 void DolphinView::renameSelectedItems()
588 KFileItemList items
= selectedItems();
589 const int itemCount
= items
.count();
594 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
596 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
597 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
598 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
599 m_viewAccessor.itemView()->edit(proxyIndex);
601 RenameDialog
* dialog
= new RenameDialog(this, items
);
602 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
605 dialog
->activateWindow();
608 // assure that the current index remains visible when KDirLister
609 // will notify the view about changed items
610 m_assureVisibleCurrentIndex
= true;
613 void DolphinView::trashSelectedItems()
615 const KUrl::List list
= simplifiedSelectedUrls();
616 KonqOperations::del(this, KonqOperations::TRASH
, list
);
619 void DolphinView::deleteSelectedItems()
621 const KUrl::List list
= simplifiedSelectedUrls();
622 const bool del
= KonqOperations::askDeleteConfirmation(list
,
624 KonqOperations::DEFAULT_CONFIRMATION
,
628 KIO::Job
* job
= KIO::del(list
);
629 connect(job
, SIGNAL(result(KJob
*)),
630 this, SLOT(slotDeleteFileFinished(KJob
*)));
634 void DolphinView::cutSelectedItems()
636 QMimeData
* mimeData
= selectionMimeData();
637 KonqMimeData::addIsCutSelection(mimeData
, true);
638 QApplication::clipboard()->setMimeData(mimeData
);
641 void DolphinView::copySelectedItems()
643 QMimeData
* mimeData
= selectionMimeData();
644 QApplication::clipboard()->setMimeData(mimeData
);
647 void DolphinView::paste()
652 void DolphinView::pasteIntoFolder()
654 const KFileItemList items
= selectedItems();
655 if ((items
.count() == 1) && items
.first().isDir()) {
656 pasteToUrl(items
.first().url());
660 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
662 switch (event
->type()) {
663 case QEvent::FocusIn
:
664 if (watched
== m_container
) {
673 return QWidget::eventFilter(watched
, event
);
676 void DolphinView::wheelEvent(QWheelEvent
* event
)
678 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
679 const int numDegrees
= event
->delta() / 8;
680 const int numSteps
= numDegrees
/ 15;
682 setZoomLevel(zoomLevel() + numSteps
);
689 void DolphinView::activate()
694 void DolphinView::slotItemActivated(int index
)
698 const KFileItemList items
= selectedItems();
699 if (items
.isEmpty()) {
703 if (items
.count() == 1) {
704 emit
itemActivated(items
.at(0)); // caught by DolphinViewContainer or DolphinPart
706 foreach (const KFileItem
& item
, items
) {
708 emit
tabRequested(item
.url());
710 emit
itemActivated(item
);
716 void DolphinView::slotItemMiddleClicked(int index
)
718 const KFileItem item
= fileItemModel()->fileItem(index
);
719 if (item
.isDir() || isTabsForFilesEnabled()) {
720 emit
tabRequested(item
.url());
724 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
726 if (GeneralSettings::showToolTips()) {
727 m_toolTipManager
->hideToolTip();
729 const KFileItem item
= fileItemModel()->fileItem(index
);
730 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
733 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
735 if (GeneralSettings::showToolTips()) {
736 m_toolTipManager
->hideToolTip();
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::slotItemExpansionToggleClicked(int index
)
793 // TODO: When doing a model->setExpanded(false) it should
794 // be checked here whether the current index is part of the
795 // closed sub-tree. If this is the case, the current index
796 // should be adjusted to the parent index.
797 KFileItemModel
* model
= fileItemModel();
798 const bool expanded
= model
->isExpanded(index
);
799 model
->setExpanded(index
, !expanded
);
802 void DolphinView::slotItemHovered(int index
)
804 const KFileItem item
= fileItemModel()->fileItem(index
);
806 if (GeneralSettings::showToolTips()) {
807 QRectF itemRect
= m_container
->controller()->view()->itemRect(index
);
808 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
809 itemRect
.moveTo(pos
);
811 m_toolTipManager
->showToolTip(item
, itemRect
);
814 emit
requestItemInfo(item
);
817 void DolphinView::slotItemUnhovered(int index
)
820 if (GeneralSettings::showToolTips()) {
821 m_toolTipManager
->hideToolTip();
823 emit
requestItemInfo(KFileItem());
826 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
828 const KFileItem destItem
= fileItemModel()->fileItem(index
);
830 QDropEvent
dropEvent(event
->pos().toPoint(),
831 event
->possibleActions(),
836 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
839 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
842 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
845 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
846 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
848 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
849 m_versionControlObserver
->setModel(fileItemModel
);
852 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
854 const int currentCount
= current
.count();
855 const int previousCount
= previous
.count();
856 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
857 (currentCount
> 0 && previousCount
== 0);
859 // If nothing has been selected before and something got selected (or if something
860 // was selected before and now nothing is selected) the selectionChangedSignal must
861 // be emitted asynchronously as fast as possible to update the edit-actions.
862 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
863 m_selectionChangedTimer
->start();
866 void DolphinView::emitSelectionChangedSignal()
868 m_selectionChangedTimer
->stop();
869 emit
selectionChanged(selectedItems());
872 void DolphinView::dropUrls(const KFileItem
& destItem
,
873 const KUrl
& destPath
,
878 markPastedUrlsAsSelected(event
->mimeData());
879 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
882 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
884 ViewProperties
props(url());
885 props
.setSorting(sorting
);
887 KItemModelBase
* model
= m_container
->controller()->model();
888 model
->setSortRole(sortRoleForSorting(sorting
));
890 emit
sortingChanged(sorting
);
893 void DolphinView::updateSortOrder(Qt::SortOrder order
)
895 ViewProperties
props(url());
896 props
.setSortOrder(order
);
898 KItemModelBase
* model
= fileItemModel();
899 model
->setSortOrder(order
);
901 emit
sortOrderChanged(order
);
904 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
906 ViewProperties
props(url());
907 props
.setSortFoldersFirst(foldersFirst
);
909 KFileItemModel
* model
= fileItemModel();
910 model
->setSortFoldersFirst(foldersFirst
);
912 emit
sortFoldersFirstChanged(foldersFirst
);
915 QPair
<bool, QString
> DolphinView::pasteInfo() const
917 return KonqOperations::pasteInfo(url());
920 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
922 m_tabsForFiles
= tabsForFiles
;
925 bool DolphinView::isTabsForFilesEnabled() const
927 return m_tabsForFiles
;
930 bool DolphinView::itemsExpandable() const
932 return m_mode
== DetailsView
;
935 void DolphinView::restoreState(QDataStream
& stream
)
937 // Restore the current item that had the keyboard focus
938 stream
>> m_currentItemUrl
;
940 // Restore the view position
941 stream
>> m_restoredContentsPosition
;
943 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
946 fileItemModel()->restoreExpandedUrls(urls
);
949 void DolphinView::saveState(QDataStream
& stream
)
951 // Save the current item that has the keyboard focus
952 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
953 if (currentIndex
!= -1) {
954 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
955 KUrl currentItemUrl
= item
.url();
956 stream
<< currentItemUrl
;
961 // Save view position
962 const qreal x
= m_container
->horizontalScrollBar()->value();
963 const qreal y
= m_container
->verticalScrollBar()->value();
964 stream
<< QPoint(x
, y
);
966 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
967 stream
<< fileItemModel()->expandedUrls();
970 bool DolphinView::hasSelection() const
972 return m_container
->controller()->selectionManager()->hasSelection();
975 KFileItem
DolphinView::rootItem() const
977 return m_dirLister
->rootItem();
980 void DolphinView::observeCreatedItem(const KUrl
& url
)
982 m_createdItemUrl
= url
;
983 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
984 // this, SLOT(selectAndScrollToCreatedItem()));
987 void DolphinView::selectAndScrollToCreatedItem()
989 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
990 if (dirIndex.isValid()) {
991 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
992 QAbstractItemView* view = m_viewAccessor.itemView();
994 view->setCurrentIndex(proxyIndex);
998 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
999 this, SLOT(selectAndScrollToCreatedItem()));*/
1000 m_createdItemUrl
= KUrl();
1003 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1005 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1006 emit
redirection(oldUrl
, newUrl
);
1007 m_url
= newUrl
; // #186947
1011 void DolphinView::updateViewState()
1013 if (m_currentItemUrl
!= KUrl()) {
1014 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1015 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1016 if (currentIndex
!= -1) {
1017 selectionManager
->setCurrentItem(currentIndex
);
1019 selectionManager
->setCurrentItem(0);
1021 m_currentItemUrl
= KUrl();
1024 if (!m_restoredContentsPosition
.isNull()) {
1025 const int x
= m_restoredContentsPosition
.x();
1026 const int y
= m_restoredContentsPosition
.y();
1027 m_restoredContentsPosition
= QPoint();
1029 m_container
->horizontalScrollBar()->setValue(x
);
1030 m_container
->verticalScrollBar()->setValue(y
);
1033 if (!m_selectedUrls
.isEmpty()) {
1034 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1035 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1036 const KFileItemModel
* model
= fileItemModel();
1038 foreach (const KUrl
& url
, m_selectedUrls
) {
1039 const int index
= model
->index(url
);
1041 selectedItems
.insert(index
);
1045 selectionManager
->setSelectedItems(selectedItems
);
1046 m_selectedUrls
.clear();
1050 void DolphinView::showHoverInformation(const KFileItem
& item
)
1052 emit
requestItemInfo(item
);
1055 void DolphinView::clearHoverInformation()
1057 emit
requestItemInfo(KFileItem());
1060 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1062 if (job
->error() == 0) {
1063 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1064 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1065 emit
errorMessage(job
->errorString());
1069 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1071 // Disable the writestate temporary until it can be determined in a fast way
1072 // in DolphinView::slotLoadingCompleted()
1073 if (m_isFolderWritable
) {
1074 m_isFolderWritable
= false;
1075 emit
writeStateChanged(m_isFolderWritable
);
1078 emit
startedPathLoading(url
);
1081 void DolphinView::slotLoadingCompleted()
1083 // Update the view-state. This has to be done using a Qt::QueuedConnection
1084 // because the view might not be in its final state yet (the view also
1085 // listens to the completed()-signal from KDirLister and the order of
1086 // of slots is undefined).
1087 QTimer::singleShot(0, this, SLOT(updateViewState()));
1089 emit
finishedPathLoading(url());
1091 updateWritableState();
1094 void DolphinView::slotRefreshItems()
1096 if (m_assureVisibleCurrentIndex
) {
1097 m_assureVisibleCurrentIndex
= false;
1098 //QAbstractItemView* view = m_viewAccessor.itemView();
1100 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1105 KFileItemModel
* DolphinView::fileItemModel() const
1107 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1110 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1112 if (!url
.isValid()) {
1113 const QString
location(url
.pathOrUrl());
1114 if (location
.isEmpty()) {
1115 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1117 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1122 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1125 void DolphinView::applyViewProperties()
1127 m_container
->beginTransaction();
1129 const ViewProperties
props(url());
1131 const Mode mode
= props
.viewMode();
1132 if (m_mode
!= mode
) {
1133 const Mode previousMode
= m_mode
;
1136 // Changing the mode might result in changing
1137 // the zoom level. Remember the old zoom level so
1138 // that zoomLevelChanged() can get emitted.
1139 const int oldZoomLevel
= m_container
->zoomLevel();
1142 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1143 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1144 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1145 default: Q_ASSERT(false); break;
1148 emit
modeChanged(m_mode
, previousMode
);
1150 if (m_container
->zoomLevel() != oldZoomLevel
) {
1151 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1155 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1156 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1157 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1158 m_dirLister
->emitChanges();
1159 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1162 KFileItemModel
* model
= fileItemModel();
1164 const bool groupedSorting
= props
.groupedSorting();
1165 if (groupedSorting
!= model
->groupedSorting()) {
1166 model
->setGroupedSorting(groupedSorting
);
1167 emit
groupedSortingChanged(groupedSorting
);
1170 const DolphinView::Sorting sorting
= props
.sorting();
1171 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1172 if (newSortRole
!= model
->sortRole()) {
1173 model
->setSortRole(newSortRole
);
1174 emit
sortingChanged(sorting
);
1177 const Qt::SortOrder sortOrder
= props
.sortOrder();
1178 if (sortOrder
!= model
->sortOrder()) {
1179 model
->setSortOrder(sortOrder
);
1180 emit
sortOrderChanged(sortOrder
);
1183 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1184 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1185 model
->setSortFoldersFirst(sortFoldersFirst
);
1186 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1189 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1190 if (infoList
!= m_additionalInfoList
) {
1191 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1192 m_additionalInfoList
= infoList
;
1193 applyAdditionalInfoListToView();
1194 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1197 const bool previewsShown
= props
.previewsShown();
1198 if (previewsShown
!= m_container
->previewsShown()) {
1199 const int oldZoomLevel
= zoomLevel();
1201 m_container
->setPreviewsShown(previewsShown
);
1202 emit
previewsShownChanged(previewsShown
);
1204 // Changing the preview-state might result in a changed zoom-level
1205 if (oldZoomLevel
!= zoomLevel()) {
1206 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1210 m_container
->endTransaction();
1213 void DolphinView::applyAdditionalInfoListToView()
1215 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1217 QList
<QByteArray
> visibleRoles
;
1218 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1219 visibleRoles
.append("name");
1221 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1222 visibleRoles
.append(infoAccessor
.role(info
));
1225 m_container
->setVisibleRoles(visibleRoles
);
1228 void DolphinView::pasteToUrl(const KUrl
& url
)
1230 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1231 KonqOperations::doPaste(this, url
);
1234 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1236 Q_UNUSED(oldZoomLevel
);
1237 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1238 if (oldZoomLevel != newZoomLevel) {
1239 m_viewModeController->setZoomLevel(newZoomLevel);
1240 emit zoomLevelChanged(newZoomLevel);
1244 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1248 const KFileItemList items
= selectedItems();
1249 foreach (const KFileItem
&item
, items
) {
1250 urls
.append(item
.url());
1253 if (itemsExpandable()) {
1254 // TODO: Check if we still need KDirModel for this in KDE 5.0
1255 urls
= KDirModel::simplifiedUrlList(urls
);
1261 QMimeData
* DolphinView::selectionMimeData() const
1263 const KFileItemModel
* model
= fileItemModel();
1264 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1265 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1267 return model
->createMimeData(selectedIndexes
);
1270 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1272 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1273 markUrlsAsSelected(urls
);
1276 void DolphinView::updateWritableState()
1278 const bool wasFolderWritable
= m_isFolderWritable
;
1279 m_isFolderWritable
= true;
1281 const KFileItem item
= m_dirLister
->rootItem();
1282 if (!item
.isNull()) {
1283 KFileItemListProperties
capabilities(KFileItemList() << item
);
1284 m_isFolderWritable
= capabilities
.supportsWriting();
1286 if (m_isFolderWritable
!= wasFolderWritable
) {
1287 emit
writeStateChanged(m_isFolderWritable
);
1291 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1294 case SortByName
: return "name";
1295 case SortBySize
: return "size";
1296 case SortByDate
: return "date";
1297 case SortByPermissions
: return "permissions";
1298 case SortByOwner
: return "owner";
1299 case SortByGroup
: return "group";
1300 case SortByType
: return "type";
1301 case SortByDestination
: return "destination";
1302 case SortByPath
: return "path";
1306 return QByteArray();
1309 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1311 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1312 if (sortHash
.isEmpty()) {
1313 sortHash
.insert("name", SortByName
);
1314 sortHash
.insert("size", SortBySize
);
1315 sortHash
.insert("date", SortByDate
);
1316 sortHash
.insert("permissions", SortByPermissions
);
1317 sortHash
.insert("owner", SortByOwner
);
1318 sortHash
.insert("group", SortByGroup
);
1319 sortHash
.insert("type", SortByType
);
1320 sortHash
.insert("destination", SortByDestination
);
1321 sortHash
.insert("path", SortByPath
);
1323 return sortHash
.value(sortRole
);
1326 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1328 const KLocale
* locale
= KGlobal::locale();
1329 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1333 if (fileSize
< multiplier
) {
1334 // Show the size in bytes
1335 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1336 } else if (fileSize
< multiplier
* multiplier
) {
1337 // Show the size in kilobytes and always round up. This is done
1338 // for consistency with the values shown e.g. in the "Size" column
1339 // of the details-view.
1340 fileSize
+= (multiplier
/ 2) - 1;
1341 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1343 // Show the size in the best fitting unit having one decimal
1344 text
= locale
->formatByteSize(fileSize
, 1);
1349 #include "dolphinview.moc"