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),
86 m_mode(DolphinView::IconsView
),
87 m_additionalInfoList(),
92 m_selectionChangedTimer(0),
94 m_restoredContentsPosition(),
97 m_versionControlObserver(0)
99 m_topLayout
= new QVBoxLayout(this);
100 m_topLayout
->setSpacing(0);
101 m_topLayout
->setMargin(0);
103 // When a new item has been created by the "Create New..." menu, the item should
104 // get selected and it must be assured that the item will get visible. As the
105 // creation is done asynchronously, several signals must be checked:
106 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
107 this, SLOT(observeCreatedItem(KUrl
)));
109 m_selectionChangedTimer
= new QTimer(this);
110 m_selectionChangedTimer
->setSingleShot(true);
111 m_selectionChangedTimer
->setInterval(300);
112 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
113 this, SLOT(emitSelectionChangedSignal()));
115 m_dirLister
= new DolphinDirLister(this);
116 m_dirLister
->setAutoUpdate(true);
117 m_dirLister
->setDelayedMimeTypes(true);
119 connect(m_dirLister
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
120 connect(m_dirLister
, SIGNAL(started(KUrl
)), this, SLOT(slotDirListerStarted(KUrl
)));
121 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
122 this, SLOT(slotRefreshItems()));
124 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
125 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
126 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
127 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
128 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
129 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
130 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
132 m_container
= new DolphinItemListContainer(m_dirLister
, this);
133 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
134 m_container
->installEventFilter(this);
135 setFocusProxy(m_container
);
136 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
137 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
139 KItemListController
* controller
= m_container
->controller();
140 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
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(itemPressed(int,Qt::MouseButton
)), this, SLOT(hideToolTip()));
148 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
149 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
150 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
151 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
153 KFileItemModel
* model
= fileItemModel();
155 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
158 KItemListView
* view
= controller
->view();
159 view
->installEventFilter(this);
160 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
161 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
162 connect(view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
163 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
165 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
166 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
167 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
169 m_toolTipManager
= new ToolTipManager(this);
171 m_versionControlObserver
= new VersionControlObserver(this);
172 m_versionControlObserver
->setModel(model
);
173 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
174 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
175 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
177 applyViewProperties();
178 m_topLayout
->addWidget(m_container
);
183 DolphinView::~DolphinView()
187 KUrl
DolphinView::url() const
192 void DolphinView::setActive(bool active
)
194 if (active
== m_active
) {
200 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
205 QWidget
* viewport
= m_container
->viewport();
208 palette
.setColor(viewport
->backgroundRole(), color
);
209 viewport
->setPalette(palette
);
215 m_container
->setFocus();
217 emit
writeStateChanged(m_isFolderWritable
);
221 bool DolphinView::isActive() const
226 void DolphinView::setMode(Mode mode
)
228 if (mode
!= m_mode
) {
229 ViewProperties
props(url());
230 props
.setViewMode(mode
);
233 applyViewProperties();
237 DolphinView::Mode
DolphinView::mode() const
242 void DolphinView::setPreviewsShown(bool show
)
244 if (previewsShown() == show
) {
248 ViewProperties
props(url());
249 props
.setPreviewsShown(show
);
251 m_container
->setPreviewsShown(show
);
252 emit
previewsShownChanged(show
);
255 bool DolphinView::previewsShown() const
257 return m_container
->previewsShown();
260 void DolphinView::setHiddenFilesShown(bool show
)
262 if (m_dirLister
->showingDotFiles() == show
) {
266 const KFileItemList itemList
= selectedItems();
267 m_selectedUrls
.clear();
268 m_selectedUrls
= itemList
.urlList();
270 ViewProperties
props(url());
271 props
.setHiddenFilesShown(show
);
273 fileItemModel()->setShowHiddenFiles(show
);
274 emit
hiddenFilesShownChanged(show
);
277 bool DolphinView::hiddenFilesShown() const
279 return m_dirLister
->showingDotFiles();
282 void DolphinView::setGroupedSorting(bool grouped
)
284 if (grouped
== groupedSorting()) {
288 ViewProperties
props(url());
289 props
.setGroupedSorting(grouped
);
292 m_container
->controller()->model()->setGroupedSorting(grouped
);
294 emit
groupedSortingChanged(grouped
);
297 bool DolphinView::groupedSorting() const
299 return fileItemModel()->groupedSorting();
302 KFileItemList
DolphinView::items() const
304 return m_dirLister
->items();
307 KFileItemList
DolphinView::selectedItems() const
309 const KFileItemModel
* model
= fileItemModel();
310 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
311 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
313 KFileItemList selectedItems
;
314 QSetIterator
<int> it(selectedIndexes
);
315 while (it
.hasNext()) {
316 const int index
= it
.next();
317 selectedItems
.append(model
->fileItem(index
));
319 return selectedItems
;
322 int DolphinView::selectedItemsCount() const
324 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
325 return selectionManager
->selectedItems().count();
328 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
330 m_selectedUrls
= urls
;
333 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
335 m_currentItemUrl
= url
;
338 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
340 const KItemListSelectionManager::SelectionMode mode
= enabled
341 ? KItemListSelectionManager::Select
342 : KItemListSelectionManager::Deselect
;
343 const KFileItemModel
* model
= fileItemModel();
344 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
346 for (int index
= 0; index
< model
->count(); index
++) {
347 const KFileItem item
= model
->fileItem(index
);
348 if (pattern
.exactMatch(item
.text())) {
349 // An alternative approach would be to store the matching items in a QSet<int> and
350 // select them in one go after the loop, but we'd need a new function
351 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
353 selectionManager
->setSelected(index
, 1, mode
);
358 void DolphinView::setZoomLevel(int level
)
360 const int oldZoomLevel
= zoomLevel();
361 m_container
->setZoomLevel(level
);
362 if (zoomLevel() != oldZoomLevel
) {
363 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
367 int DolphinView::zoomLevel() const
369 return m_container
->zoomLevel();
372 void DolphinView::setSorting(Sorting sorting
)
374 if (sorting
!= this->sorting()) {
375 updateSorting(sorting
);
379 DolphinView::Sorting
DolphinView::sorting() const
381 KItemModelBase
* model
= m_container
->controller()->model();
382 return sortingForSortRole(model
->sortRole());
385 void DolphinView::setSortOrder(Qt::SortOrder order
)
387 if (sortOrder() != order
) {
388 updateSortOrder(order
);
392 Qt::SortOrder
DolphinView::sortOrder() const
394 KItemModelBase
* model
= fileItemModel();
395 return model
->sortOrder();
398 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
400 if (sortFoldersFirst() != foldersFirst
) {
401 updateSortFoldersFirst(foldersFirst
);
405 bool DolphinView::sortFoldersFirst() const
407 KFileItemModel
* model
= fileItemModel();
408 return model
->sortFoldersFirst();
411 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
413 const QList
<AdditionalInfo
> previousList
= info
;
415 ViewProperties
props(url());
416 props
.setAdditionalInfoList(info
);
418 m_additionalInfoList
= info
;
419 applyAdditionalInfoListToView();
421 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
424 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
426 return m_additionalInfoList
;
429 void DolphinView::reload()
431 QByteArray viewState
;
432 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
433 saveState(saveStream
);
435 const KFileItemList itemList
= selectedItems();
436 m_selectedUrls
.clear();
437 m_selectedUrls
= itemList
.urlList();
440 loadDirectory(url(), true);
442 QDataStream
restoreStream(viewState
);
443 restoreState(restoreStream
);
446 void DolphinView::stopLoading()
451 void DolphinView::readSettings()
453 const int oldZoomLevel
= m_container
->zoomLevel();
455 GeneralSettings::self()->readConfig();
456 m_container
->readSettings();
457 applyViewProperties();
459 const int newZoomLevel
= m_container
->zoomLevel();
460 if (newZoomLevel
!= oldZoomLevel
) {
461 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
465 void DolphinView::writeSettings()
467 GeneralSettings::self()->writeConfig();
468 m_container
->writeSettings();
471 void DolphinView::setNameFilter(const QString
& nameFilter
)
473 fileItemModel()->setNameFilter(nameFilter
);
476 QString
DolphinView::nameFilter() const
478 return fileItemModel()->nameFilter();
481 void DolphinView::calculateItemCount(int& fileCount
,
483 KIO::filesize_t
& totalFileSize
) const
485 foreach (const KFileItem
& item
, m_dirLister
->items()) {
490 totalFileSize
+= item
.size();
495 QString
DolphinView::statusBarText() const
503 KIO::filesize_t totalFileSize
= 0;
505 if (hasSelection()) {
506 // Give a summary of the status of the selected files
507 const KFileItemList list
= selectedItems();
508 foreach (const KFileItem
& item
, list
) {
513 totalFileSize
+= item
.size();
517 if (folderCount
+ fileCount
== 1) {
518 // If only one item is selected, show the filename
519 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
521 // At least 2 items are selected
522 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
523 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
526 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
527 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
528 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
531 if (fileCount
> 0 && folderCount
> 0) {
532 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
533 foldersText
, filesText
, fileSizeText(totalFileSize
));
534 } else if (fileCount
> 0) {
535 summary
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, fileSizeText(totalFileSize
));
536 } else if (folderCount
> 0) {
537 summary
= foldersText
;
543 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
545 QList
<QAction
*> actions
;
547 if (items
.isEmpty()) {
548 const KFileItem item
= fileItemModel()->rootItem();
549 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
551 actions
= m_versionControlObserver
->actions(items
);
557 void DolphinView::setUrl(const KUrl
& url
)
563 emit
urlAboutToBeChanged(url
);
568 // It is important to clear the items from the model before
569 // applying the view properties, otherwise expensive operations
570 // might be done on the existing items although they get cleared
571 // anyhow afterwards by loadDirectory().
572 fileItemModel()->clear();
573 applyViewProperties();
576 emit
urlChanged(url
);
579 void DolphinView::selectAll()
581 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
582 selectionManager
->setSelected(0, fileItemModel()->count());
585 void DolphinView::invertSelection()
587 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
588 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
591 void DolphinView::clearSelection()
593 m_container
->controller()->selectionManager()->clearSelection();
596 void DolphinView::renameSelectedItems()
598 KFileItemList items
= selectedItems();
599 const int itemCount
= items
.count();
604 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
606 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
607 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
608 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
609 m_viewAccessor.itemView()->edit(proxyIndex);
611 RenameDialog
* dialog
= new RenameDialog(this, items
);
612 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
615 dialog
->activateWindow();
618 // assure that the current index remains visible when KDirLister
619 // will notify the view about changed items
620 m_assureVisibleCurrentIndex
= true;
623 void DolphinView::trashSelectedItems()
625 const KUrl::List list
= simplifiedSelectedUrls();
626 KonqOperations::del(this, KonqOperations::TRASH
, list
);
629 void DolphinView::deleteSelectedItems()
631 const KUrl::List list
= simplifiedSelectedUrls();
632 const bool del
= KonqOperations::askDeleteConfirmation(list
,
634 KonqOperations::DEFAULT_CONFIRMATION
,
638 KIO::Job
* job
= KIO::del(list
);
639 connect(job
, SIGNAL(result(KJob
*)),
640 this, SLOT(slotDeleteFileFinished(KJob
*)));
644 void DolphinView::cutSelectedItems()
646 QMimeData
* mimeData
= selectionMimeData();
647 KonqMimeData::addIsCutSelection(mimeData
, true);
648 QApplication::clipboard()->setMimeData(mimeData
);
651 void DolphinView::copySelectedItems()
653 QMimeData
* mimeData
= selectionMimeData();
654 QApplication::clipboard()->setMimeData(mimeData
);
657 void DolphinView::paste()
662 void DolphinView::pasteIntoFolder()
664 const KFileItemList items
= selectedItems();
665 if ((items
.count() == 1) && items
.first().isDir()) {
666 pasteToUrl(items
.first().url());
670 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
672 switch (event
->type()) {
673 case QEvent::FocusIn
:
674 if (watched
== m_container
) {
679 case QEvent::GraphicsSceneDragEnter
:
680 if (watched
== m_container
->controller()->view()) {
685 case QEvent::GraphicsSceneDragLeave
:
686 if (watched
== m_container
->controller()->view()) {
691 case QEvent::GraphicsSceneDrop
:
692 if (watched
== m_container
->controller()->view()) {
699 return QWidget::eventFilter(watched
, event
);
702 void DolphinView::wheelEvent(QWheelEvent
* event
)
704 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
705 const int numDegrees
= event
->delta() / 8;
706 const int numSteps
= numDegrees
/ 15;
708 setZoomLevel(zoomLevel() + numSteps
);
715 void DolphinView::hideEvent(QHideEvent
* event
)
718 QWidget::hideEvent(event
);
721 void DolphinView::activate()
726 void DolphinView::slotItemActivated(int index
)
728 const KFileItem item
= fileItemModel()->fileItem(index
);
729 if (!item
.isNull()) {
730 emit
itemActivated(item
);
734 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
736 Q_ASSERT(indexes
.count() >= 2);
740 KFileItemModel
* model
= fileItemModel();
741 QSetIterator
<int> it(indexes
);
742 while (it
.hasNext()) {
743 const int index
= it
.next();
744 items
.append(model
->fileItem(index
));
747 foreach (const KFileItem
& item
, items
) {
749 emit
tabRequested(item
.url());
751 emit
itemActivated(item
);
756 void DolphinView::slotItemMiddleClicked(int index
)
758 const KFileItem item
= fileItemModel()->fileItem(index
);
759 if (item
.isDir() || isTabsForFilesEnabled()) {
760 emit
tabRequested(item
.url());
764 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
766 const KFileItem item
= fileItemModel()->fileItem(index
);
767 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
770 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
772 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
775 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
777 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
779 KItemListView
* view
= m_container
->controller()->view();
780 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
782 // Add all roles to the menu that can be shown or hidden by the user
783 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
784 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
785 foreach (const DolphinView::AdditionalInfo info
, keys
) {
786 const QByteArray
& role
= infoAccessor
.role(info
);
787 if (role
!= "name") {
788 const QString text
= fileItemModel()->roleDescription(role
);
790 QAction
* action
= menu
.data()->addAction(text
);
791 action
->setCheckable(true);
792 action
->setChecked(visibleRolesSet
.contains(role
));
793 action
->setData(info
);
797 QAction
* action
= menu
.data()->exec(pos
.toPoint());
799 // Show or hide the selected role
800 const DolphinView::AdditionalInfo info
=
801 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
803 ViewProperties
props(url());
804 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
806 const QByteArray selectedRole
= infoAccessor
.role(info
);
807 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
809 if (action
->isChecked()) {
810 const int index
= keys
.indexOf(info
) + 1;
811 visibleRoles
.insert(index
, selectedRole
);
812 infoList
.insert(index
, info
);
814 visibleRoles
.removeOne(selectedRole
);
815 infoList
.removeOne(info
);
818 view
->setVisibleRoles(visibleRoles
);
819 props
.setAdditionalInfoList(infoList
);
825 void DolphinView::slotItemHovered(int index
)
827 const KFileItem item
= fileItemModel()->fileItem(index
);
829 if (GeneralSettings::showToolTips() && !m_dragging
) {
830 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
831 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
832 itemRect
.moveTo(pos
);
834 m_toolTipManager
->showToolTip(item
, itemRect
);
837 emit
requestItemInfo(item
);
840 void DolphinView::slotItemUnhovered(int index
)
844 emit
requestItemInfo(KFileItem());
847 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
850 KFileItem destItem
= fileItemModel()->fileItem(index
);
851 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
852 // Use the URL of the view as drop target if the item is no directory
854 destItem
= fileItemModel()->rootItem();
857 // The item represents a directory or desktop-file
858 destUrl
= destItem
.url();
861 QDropEvent
dropEvent(event
->pos().toPoint(),
862 event
->possibleActions(),
867 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
868 if (!error
.isEmpty()) {
869 emit
errorMessage(error
);
873 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
876 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
879 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
880 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
882 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
883 m_versionControlObserver
->setModel(fileItemModel
);
886 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
888 const int currentCount
= current
.count();
889 const int previousCount
= previous
.count();
890 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
891 (currentCount
> 0 && previousCount
== 0);
893 // If nothing has been selected before and something got selected (or if something
894 // was selected before and now nothing is selected) the selectionChangedSignal must
895 // be emitted asynchronously as fast as possible to update the edit-actions.
896 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
897 m_selectionChangedTimer
->start();
900 void DolphinView::emitSelectionChangedSignal()
902 m_selectionChangedTimer
->stop();
903 emit
selectionChanged(selectedItems());
906 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
908 ViewProperties
props(url());
909 props
.setSorting(sorting
);
911 KItemModelBase
* model
= m_container
->controller()->model();
912 model
->setSortRole(sortRoleForSorting(sorting
));
914 emit
sortingChanged(sorting
);
917 void DolphinView::updateSortOrder(Qt::SortOrder order
)
919 ViewProperties
props(url());
920 props
.setSortOrder(order
);
922 KItemModelBase
* model
= fileItemModel();
923 model
->setSortOrder(order
);
925 emit
sortOrderChanged(order
);
928 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
930 ViewProperties
props(url());
931 props
.setSortFoldersFirst(foldersFirst
);
933 KFileItemModel
* model
= fileItemModel();
934 model
->setSortFoldersFirst(foldersFirst
);
936 emit
sortFoldersFirstChanged(foldersFirst
);
939 QPair
<bool, QString
> DolphinView::pasteInfo() const
941 return KonqOperations::pasteInfo(url());
944 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
946 m_tabsForFiles
= tabsForFiles
;
949 bool DolphinView::isTabsForFilesEnabled() const
951 return m_tabsForFiles
;
954 bool DolphinView::itemsExpandable() const
956 return m_mode
== DetailsView
;
959 void DolphinView::restoreState(QDataStream
& stream
)
961 // Restore the current item that had the keyboard focus
962 stream
>> m_currentItemUrl
;
964 // Restore the view position
965 stream
>> m_restoredContentsPosition
;
967 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
970 fileItemModel()->restoreExpandedUrls(urls
);
973 void DolphinView::saveState(QDataStream
& stream
)
975 // Save the current item that has the keyboard focus
976 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
977 if (currentIndex
!= -1) {
978 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
979 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
980 KUrl currentItemUrl
= item
.url();
981 stream
<< currentItemUrl
;
986 // Save view position
987 const qreal x
= m_container
->horizontalScrollBar()->value();
988 const qreal y
= m_container
->verticalScrollBar()->value();
989 stream
<< QPoint(x
, y
);
991 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
992 stream
<< fileItemModel()->expandedUrls();
995 bool DolphinView::hasSelection() const
997 return m_container
->controller()->selectionManager()->hasSelection();
1000 KFileItem
DolphinView::rootItem() const
1002 return m_dirLister
->rootItem();
1005 void DolphinView::observeCreatedItem(const KUrl
& url
)
1007 m_createdItemUrl
= url
;
1008 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1009 // this, SLOT(selectAndScrollToCreatedItem()));
1012 void DolphinView::selectAndScrollToCreatedItem()
1014 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1015 if (dirIndex.isValid()) {
1016 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1017 QAbstractItemView* view = m_viewAccessor.itemView();
1019 view->setCurrentIndex(proxyIndex);
1023 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1024 this, SLOT(selectAndScrollToCreatedItem()));*/
1025 m_createdItemUrl
= KUrl();
1028 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1030 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1031 emit
redirection(oldUrl
, newUrl
);
1032 m_url
= newUrl
; // #186947
1036 void DolphinView::updateViewState()
1038 if (m_currentItemUrl
!= KUrl()) {
1039 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1040 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1041 if (currentIndex
!= -1) {
1042 selectionManager
->setCurrentItem(currentIndex
);
1044 selectionManager
->setCurrentItem(0);
1046 m_currentItemUrl
= KUrl();
1049 if (!m_restoredContentsPosition
.isNull()) {
1050 const int x
= m_restoredContentsPosition
.x();
1051 const int y
= m_restoredContentsPosition
.y();
1052 m_restoredContentsPosition
= QPoint();
1054 m_container
->horizontalScrollBar()->setValue(x
);
1055 m_container
->verticalScrollBar()->setValue(y
);
1058 if (!m_selectedUrls
.isEmpty()) {
1059 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1060 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1061 const KFileItemModel
* model
= fileItemModel();
1063 foreach (const KUrl
& url
, m_selectedUrls
) {
1064 const int index
= model
->index(url
);
1066 selectedItems
.insert(index
);
1070 selectionManager
->setSelectedItems(selectedItems
);
1071 m_selectedUrls
.clear();
1075 void DolphinView::hideToolTip()
1077 if (GeneralSettings::showToolTips()) {
1078 m_toolTipManager
->hideToolTip();
1082 void DolphinView::showHoverInformation(const KFileItem
& item
)
1084 emit
requestItemInfo(item
);
1087 void DolphinView::clearHoverInformation()
1089 emit
requestItemInfo(KFileItem());
1092 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1094 if (job
->error() == 0) {
1095 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1096 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1097 emit
errorMessage(job
->errorString());
1101 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1103 // Disable the writestate temporary until it can be determined in a fast way
1104 // in DolphinView::slotLoadingCompleted()
1105 if (m_isFolderWritable
) {
1106 m_isFolderWritable
= false;
1107 emit
writeStateChanged(m_isFolderWritable
);
1110 emit
startedPathLoading(url
);
1113 void DolphinView::slotLoadingCompleted()
1115 // Update the view-state. This has to be done using a Qt::QueuedConnection
1116 // because the view might not be in its final state yet (the view also
1117 // listens to the completed()-signal from KDirLister and the order of
1118 // of slots is undefined).
1119 QTimer::singleShot(0, this, SLOT(updateViewState()));
1121 emit
finishedPathLoading(url());
1123 updateWritableState();
1126 void DolphinView::slotRefreshItems()
1128 if (m_assureVisibleCurrentIndex
) {
1129 m_assureVisibleCurrentIndex
= false;
1130 //QAbstractItemView* view = m_viewAccessor.itemView();
1132 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1137 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1140 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1142 ViewProperties
props(url());
1143 props
.setSortOrder(current
);
1145 emit
sortOrderChanged(current
);
1148 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1151 Q_ASSERT(fileItemModel()->sortRole() == current
);
1153 ViewProperties
props(url());
1154 const Sorting sorting
= sortingForSortRole(current
);
1155 props
.setSorting(sorting
);
1157 emit
sortingChanged(sorting
);
1160 KFileItemModel
* DolphinView::fileItemModel() const
1162 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1165 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1167 if (!url
.isValid()) {
1168 const QString
location(url
.pathOrUrl());
1169 if (location
.isEmpty()) {
1170 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1172 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1177 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1180 void DolphinView::applyViewProperties()
1182 m_container
->beginTransaction();
1184 const ViewProperties
props(url());
1185 KFileItemModel
* model
= fileItemModel();
1187 const Mode mode
= props
.viewMode();
1188 if (m_mode
!= mode
) {
1189 const Mode previousMode
= m_mode
;
1192 // Changing the mode might result in changing
1193 // the zoom level. Remember the old zoom level so
1194 // that zoomLevelChanged() can get emitted.
1195 const int oldZoomLevel
= m_container
->zoomLevel();
1198 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1199 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1200 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1201 default: Q_ASSERT(false); break;
1204 emit
modeChanged(m_mode
, previousMode
);
1206 if (m_container
->zoomLevel() != oldZoomLevel
) {
1207 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1211 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1212 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1213 model
->setShowHiddenFiles(hiddenFilesShown
);
1214 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1217 const bool groupedSorting
= props
.groupedSorting();
1218 if (groupedSorting
!= model
->groupedSorting()) {
1219 model
->setGroupedSorting(groupedSorting
);
1220 emit
groupedSortingChanged(groupedSorting
);
1223 const DolphinView::Sorting sorting
= props
.sorting();
1224 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1225 if (newSortRole
!= model
->sortRole()) {
1226 model
->setSortRole(newSortRole
);
1227 emit
sortingChanged(sorting
);
1230 const Qt::SortOrder sortOrder
= props
.sortOrder();
1231 if (sortOrder
!= model
->sortOrder()) {
1232 model
->setSortOrder(sortOrder
);
1233 emit
sortOrderChanged(sortOrder
);
1236 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1237 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1238 model
->setSortFoldersFirst(sortFoldersFirst
);
1239 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1242 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1243 if (infoList
!= m_additionalInfoList
) {
1244 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1245 m_additionalInfoList
= infoList
;
1246 applyAdditionalInfoListToView();
1247 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1250 const bool previewsShown
= props
.previewsShown();
1251 if (previewsShown
!= m_container
->previewsShown()) {
1252 const int oldZoomLevel
= zoomLevel();
1254 m_container
->setPreviewsShown(previewsShown
);
1255 emit
previewsShownChanged(previewsShown
);
1257 // Changing the preview-state might result in a changed zoom-level
1258 if (oldZoomLevel
!= zoomLevel()) {
1259 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1263 m_container
->endTransaction();
1266 void DolphinView::applyAdditionalInfoListToView()
1268 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1270 QList
<QByteArray
> visibleRoles
;
1271 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1272 visibleRoles
.append("name");
1274 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1275 visibleRoles
.append(infoAccessor
.role(info
));
1278 m_container
->setVisibleRoles(visibleRoles
);
1281 void DolphinView::pasteToUrl(const KUrl
& url
)
1283 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1284 KonqOperations::doPaste(this, url
);
1287 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1291 const KFileItemList items
= selectedItems();
1292 foreach (const KFileItem
&item
, items
) {
1293 urls
.append(item
.url());
1296 if (itemsExpandable()) {
1297 // TODO: Check if we still need KDirModel for this in KDE 5.0
1298 urls
= KDirModel::simplifiedUrlList(urls
);
1304 QMimeData
* DolphinView::selectionMimeData() const
1306 const KFileItemModel
* model
= fileItemModel();
1307 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1308 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1310 return model
->createMimeData(selectedIndexes
);
1313 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1315 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1316 markUrlsAsSelected(urls
);
1319 void DolphinView::updateWritableState()
1321 const bool wasFolderWritable
= m_isFolderWritable
;
1322 m_isFolderWritable
= true;
1324 const KFileItem item
= m_dirLister
->rootItem();
1325 if (!item
.isNull()) {
1326 KFileItemListProperties
capabilities(KFileItemList() << item
);
1327 m_isFolderWritable
= capabilities
.supportsWriting();
1329 if (m_isFolderWritable
!= wasFolderWritable
) {
1330 emit
writeStateChanged(m_isFolderWritable
);
1334 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1337 case SortByName
: return "name";
1338 case SortBySize
: return "size";
1339 case SortByDate
: return "date";
1340 case SortByPermissions
: return "permissions";
1341 case SortByOwner
: return "owner";
1342 case SortByGroup
: return "group";
1343 case SortByType
: return "type";
1344 case SortByDestination
: return "destination";
1345 case SortByPath
: return "path";
1349 return QByteArray();
1352 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1354 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1355 if (sortHash
.isEmpty()) {
1356 sortHash
.insert("name", SortByName
);
1357 sortHash
.insert("size", SortBySize
);
1358 sortHash
.insert("date", SortByDate
);
1359 sortHash
.insert("permissions", SortByPermissions
);
1360 sortHash
.insert("owner", SortByOwner
);
1361 sortHash
.insert("group", SortByGroup
);
1362 sortHash
.insert("type", SortByType
);
1363 sortHash
.insert("destination", SortByDestination
);
1364 sortHash
.insert("path", SortByPath
);
1366 return sortHash
.value(sortRole
);
1369 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1371 const KLocale
* locale
= KGlobal::locale();
1372 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1376 if (fileSize
< multiplier
) {
1377 // Show the size in bytes
1378 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1379 } else if (fileSize
< multiplier
* multiplier
) {
1380 // Show the size in kilobytes and always round up. This is done
1381 // for consistency with the values shown e.g. in the "Size" column
1382 // of the details-view.
1383 fileSize
+= (multiplier
/ 2) - 1;
1384 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1386 // Show the size in the best fitting unit having one decimal
1387 text
= locale
->formatByteSize(fileSize
, 1);
1392 #include "dolphinview.moc"