1 /***************************************************************************
2 * Copyright (C) 2006-2009 by Peter Penz <peter.penz19@gmail.com> *
3 * Copyright (C) 2006 by Gregor Kališnik <gregor@podnapisi.net> *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
21 #include "dolphinview.h"
23 #include <QAbstractItemView>
24 #include <QApplication>
28 #include <QGraphicsSceneDragDropEvent>
30 #include <QItemSelection>
34 #include <KActionCollection>
35 #include <KColorScheme>
38 #include <KIconEffect>
40 #include <KFileItemListProperties>
42 #include <kitemviews/kfileitemmodel.h>
43 #include <kitemviews/kfileitemlistview.h>
44 #include <kitemviews/kitemlistselectionmanager.h>
45 #include <kitemviews/kitemlistview.h>
46 #include <kitemviews/kitemlistcontroller.h>
47 #include <KIO/DeleteJob>
48 #include <KIO/NetAccess>
49 #include <KIO/PreviewJob>
52 #include <KMessageBox>
53 #include <konq_fileitemcapabilities.h>
54 #include <konq_operations.h>
55 #include <konqmimedata.h>
56 #include <KToggleAction>
59 #include "additionalinfoaccessor.h"
60 #include "dolphindirlister.h"
61 #include "dolphinnewfilemenuobserver.h"
62 #include "dolphin_detailsmodesettings.h"
63 #include "dolphin_generalsettings.h"
64 #include "dolphinitemlistcontainer.h"
65 #include "draganddrophelper.h"
66 #include "renamedialog.h"
67 #include "versioncontrol/versioncontrolobserver.h"
68 #include "viewmodecontroller.h"
69 #include "viewproperties.h"
70 #include "views/tooltips/tooltipmanager.h"
71 #include "zoomlevelinfo.h"
74 const int MaxModeEnum
= DolphinView::CompactView
;
75 const int MaxSortingEnum
= DolphinView::SortByPath
;
78 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
81 m_tabsForFiles(false),
82 m_assureVisibleCurrentIndex(false),
83 m_isFolderWritable(true),
85 m_mode(DolphinView::IconsView
),
86 m_additionalInfoList(),
91 m_selectionChangedTimer(0),
93 m_restoredContentsPosition(),
96 m_versionControlObserver(0)
98 m_topLayout
= new QVBoxLayout(this);
99 m_topLayout
->setSpacing(0);
100 m_topLayout
->setMargin(0);
102 // When a new item has been created by the "Create New..." menu, the item should
103 // get selected and it must be assured that the item will get visible. As the
104 // creation is done asynchronously, several signals must be checked:
105 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
106 this, SLOT(observeCreatedItem(KUrl
)));
108 m_selectionChangedTimer
= new QTimer(this);
109 m_selectionChangedTimer
->setSingleShot(true);
110 m_selectionChangedTimer
->setInterval(300);
111 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
112 this, SLOT(emitSelectionChangedSignal()));
114 m_dirLister
= new DolphinDirLister(this);
115 m_dirLister
->setAutoUpdate(true);
116 m_dirLister
->setDelayedMimeTypes(true);
118 connect(m_dirLister
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
119 connect(m_dirLister
, SIGNAL(started(KUrl
)), this, SLOT(slotDirListerStarted(KUrl
)));
120 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
121 this, SLOT(slotRefreshItems()));
123 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
124 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
125 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
126 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
127 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
128 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
129 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
131 m_container
= new DolphinItemListContainer(m_dirLister
, this);
132 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
133 m_container
->installEventFilter(this);
134 setFocusProxy(m_container
);
135 connect(m_container
->horizontalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
136 connect(m_container
->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(hideToolTip()));
138 KItemListController
* controller
= m_container
->controller();
139 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
140 connect(controller
, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
141 connect(controller
, SIGNAL(itemsActivated(QSet
<int>)), this, SLOT(slotItemsActivated(QSet
<int>)));
142 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
143 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
144 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
145 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
146 connect(controller
, SIGNAL(itemPressed(int,Qt::MouseButton
)), this, SLOT(hideToolTip()));
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 KItemListView
* view
= controller
->view();
158 connect(view
, SIGNAL(sortOrderChanged(Qt::SortOrder
,Qt::SortOrder
)),
159 this, SLOT(slotSortOrderChangedByHeader(Qt::SortOrder
,Qt::SortOrder
)));
160 connect(view
, SIGNAL(sortRoleChanged(QByteArray
,QByteArray
)),
161 this, SLOT(slotSortRoleChangedByHeader(QByteArray
,QByteArray
)));
163 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
164 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
165 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
167 m_toolTipManager
= new ToolTipManager(this);
169 m_versionControlObserver
= new VersionControlObserver(this);
170 m_versionControlObserver
->setModel(model
);
171 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
172 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
173 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
175 applyViewProperties();
176 m_topLayout
->addWidget(m_container
);
181 DolphinView::~DolphinView()
185 KUrl
DolphinView::url() const
190 void DolphinView::setActive(bool active
)
192 if (active
== m_active
) {
198 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
203 QWidget
* viewport
= m_container
->viewport();
206 palette
.setColor(viewport
->backgroundRole(), color
);
207 viewport
->setPalette(palette
);
213 m_container
->setFocus();
215 emit
writeStateChanged(m_isFolderWritable
);
219 bool DolphinView::isActive() const
224 void DolphinView::setMode(Mode mode
)
226 if (mode
!= m_mode
) {
227 ViewProperties
props(url());
228 props
.setViewMode(mode
);
231 applyViewProperties();
235 DolphinView::Mode
DolphinView::mode() const
240 void DolphinView::setPreviewsShown(bool show
)
242 if (previewsShown() == show
) {
246 ViewProperties
props(url());
247 props
.setPreviewsShown(show
);
249 m_container
->setPreviewsShown(show
);
250 emit
previewsShownChanged(show
);
253 bool DolphinView::previewsShown() const
255 return m_container
->previewsShown();
258 void DolphinView::setHiddenFilesShown(bool show
)
260 if (m_dirLister
->showingDotFiles() == show
) {
264 const KFileItemList itemList
= selectedItems();
265 m_selectedUrls
.clear();
266 m_selectedUrls
= itemList
.urlList();
268 ViewProperties
props(url());
269 props
.setHiddenFilesShown(show
);
271 fileItemModel()->setShowHiddenFiles(show
);
272 emit
hiddenFilesShownChanged(show
);
275 bool DolphinView::hiddenFilesShown() const
277 return m_dirLister
->showingDotFiles();
280 void DolphinView::setGroupedSorting(bool grouped
)
282 if (grouped
== groupedSorting()) {
286 ViewProperties
props(url());
287 props
.setGroupedSorting(grouped
);
290 m_container
->controller()->model()->setGroupedSorting(grouped
);
292 emit
groupedSortingChanged(grouped
);
295 bool DolphinView::groupedSorting() const
297 return fileItemModel()->groupedSorting();
300 KFileItemList
DolphinView::items() const
302 return m_dirLister
->items();
305 KFileItemList
DolphinView::selectedItems() const
307 const KFileItemModel
* model
= fileItemModel();
308 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
309 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
311 KFileItemList selectedItems
;
312 QSetIterator
<int> it(selectedIndexes
);
313 while (it
.hasNext()) {
314 const int index
= it
.next();
315 selectedItems
.append(model
->fileItem(index
));
317 return selectedItems
;
320 int DolphinView::selectedItemsCount() const
322 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
323 return selectionManager
->selectedItems().count();
326 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
328 m_selectedUrls
= urls
;
331 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
333 m_currentItemUrl
= url
;
336 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
338 const KItemListSelectionManager::SelectionMode mode
= enabled
339 ? KItemListSelectionManager::Select
340 : KItemListSelectionManager::Deselect
;
341 const KFileItemModel
* model
= fileItemModel();
342 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
344 for (int index
= 0; index
< model
->count(); index
++) {
345 const KFileItem item
= model
->fileItem(index
);
346 if (pattern
.exactMatch(item
.text())) {
347 // An alternative approach would be to store the matching items in a QSet<int> and
348 // select them in one go after the loop, but we'd need a new function
349 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
351 selectionManager
->setSelected(index
, 1, mode
);
356 void DolphinView::setZoomLevel(int level
)
358 const int oldZoomLevel
= zoomLevel();
359 m_container
->setZoomLevel(level
);
360 if (zoomLevel() != oldZoomLevel
) {
361 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
365 int DolphinView::zoomLevel() const
367 return m_container
->zoomLevel();
370 void DolphinView::setSorting(Sorting sorting
)
372 if (sorting
!= this->sorting()) {
373 updateSorting(sorting
);
377 DolphinView::Sorting
DolphinView::sorting() const
379 KItemModelBase
* model
= m_container
->controller()->model();
380 return sortingForSortRole(model
->sortRole());
383 void DolphinView::setSortOrder(Qt::SortOrder order
)
385 if (sortOrder() != order
) {
386 updateSortOrder(order
);
390 Qt::SortOrder
DolphinView::sortOrder() const
392 KItemModelBase
* model
= fileItemModel();
393 return model
->sortOrder();
396 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
398 if (sortFoldersFirst() != foldersFirst
) {
399 updateSortFoldersFirst(foldersFirst
);
403 bool DolphinView::sortFoldersFirst() const
405 KFileItemModel
* model
= fileItemModel();
406 return model
->sortFoldersFirst();
409 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
411 const QList
<AdditionalInfo
> previousList
= info
;
413 ViewProperties
props(url());
414 props
.setAdditionalInfoList(info
);
416 m_additionalInfoList
= info
;
417 applyAdditionalInfoListToView();
419 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
422 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
424 return m_additionalInfoList
;
427 void DolphinView::reload()
429 QByteArray viewState
;
430 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
431 saveState(saveStream
);
433 const KFileItemList itemList
= selectedItems();
434 m_selectedUrls
.clear();
435 m_selectedUrls
= itemList
.urlList();
438 loadDirectory(url(), true);
440 QDataStream
restoreStream(viewState
);
441 restoreState(restoreStream
);
444 void DolphinView::stopLoading()
449 void DolphinView::readSettings()
451 const int oldZoomLevel
= m_container
->zoomLevel();
453 GeneralSettings::self()->readConfig();
454 m_container
->readSettings();
455 applyViewProperties();
457 const int newZoomLevel
= m_container
->zoomLevel();
458 if (newZoomLevel
!= oldZoomLevel
) {
459 emit
zoomLevelChanged(newZoomLevel
, oldZoomLevel
);
463 void DolphinView::writeSettings()
465 GeneralSettings::self()->writeConfig();
466 m_container
->writeSettings();
469 void DolphinView::setNameFilter(const QString
& nameFilter
)
471 fileItemModel()->setNameFilter(nameFilter
);
474 QString
DolphinView::nameFilter() const
476 return fileItemModel()->nameFilter();
479 void DolphinView::calculateItemCount(int& fileCount
,
481 KIO::filesize_t
& totalFileSize
) const
483 foreach (const KFileItem
& item
, m_dirLister
->items()) {
488 totalFileSize
+= item
.size();
493 QString
DolphinView::statusBarText() const
501 KIO::filesize_t totalFileSize
= 0;
503 if (hasSelection()) {
504 // Give a summary of the status of the selected files
505 const KFileItemList list
= selectedItems();
506 foreach (const KFileItem
& item
, list
) {
511 totalFileSize
+= item
.size();
515 if (folderCount
+ fileCount
== 1) {
516 // If only one item is selected, show the filename
517 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
519 // At least 2 items are selected
520 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
521 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
524 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
525 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
526 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
529 if (fileCount
> 0 && folderCount
> 0) {
530 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
531 foldersText
, filesText
, fileSizeText(totalFileSize
));
532 } else if (fileCount
> 0) {
533 summary
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, fileSizeText(totalFileSize
));
534 } else if (folderCount
> 0) {
535 summary
= foldersText
;
541 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
543 QList
<QAction
*> actions
;
545 if (items
.isEmpty()) {
546 const KFileItem item
= fileItemModel()->rootItem();
547 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
549 actions
= m_versionControlObserver
->actions(items
);
555 void DolphinView::setUrl(const KUrl
& url
)
561 emit
urlAboutToBeChanged(url
);
566 // It is important to clear the items from the model before
567 // applying the view properties, otherwise expensive operations
568 // might be done on the existing items although they get cleared
569 // anyhow afterwards by loadDirectory().
570 fileItemModel()->clear();
571 applyViewProperties();
574 emit
urlChanged(url
);
577 void DolphinView::selectAll()
579 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
580 selectionManager
->setSelected(0, fileItemModel()->count());
583 void DolphinView::invertSelection()
585 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
586 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
589 void DolphinView::clearSelection()
591 m_container
->controller()->selectionManager()->clearSelection();
594 void DolphinView::renameSelectedItems()
596 KFileItemList items
= selectedItems();
597 const int itemCount
= items
.count();
602 // TODO: The new view-engine introduced with Dolphin 2.0 does not support inline
604 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
605 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
606 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
607 m_viewAccessor.itemView()->edit(proxyIndex);
609 RenameDialog
* dialog
= new RenameDialog(this, items
);
610 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
613 dialog
->activateWindow();
616 // assure that the current index remains visible when KDirLister
617 // will notify the view about changed items
618 m_assureVisibleCurrentIndex
= true;
621 void DolphinView::trashSelectedItems()
623 const KUrl::List list
= simplifiedSelectedUrls();
624 KonqOperations::del(this, KonqOperations::TRASH
, list
);
627 void DolphinView::deleteSelectedItems()
629 const KUrl::List list
= simplifiedSelectedUrls();
630 const bool del
= KonqOperations::askDeleteConfirmation(list
,
632 KonqOperations::DEFAULT_CONFIRMATION
,
636 KIO::Job
* job
= KIO::del(list
);
637 connect(job
, SIGNAL(result(KJob
*)),
638 this, SLOT(slotDeleteFileFinished(KJob
*)));
642 void DolphinView::cutSelectedItems()
644 QMimeData
* mimeData
= selectionMimeData();
645 KonqMimeData::addIsCutSelection(mimeData
, true);
646 QApplication::clipboard()->setMimeData(mimeData
);
649 void DolphinView::copySelectedItems()
651 QMimeData
* mimeData
= selectionMimeData();
652 QApplication::clipboard()->setMimeData(mimeData
);
655 void DolphinView::paste()
660 void DolphinView::pasteIntoFolder()
662 const KFileItemList items
= selectedItems();
663 if ((items
.count() == 1) && items
.first().isDir()) {
664 pasteToUrl(items
.first().url());
668 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
670 switch (event
->type()) {
671 case QEvent::FocusIn
:
672 if (watched
== m_container
) {
681 return QWidget::eventFilter(watched
, event
);
684 void DolphinView::wheelEvent(QWheelEvent
* event
)
686 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
687 const int numDegrees
= event
->delta() / 8;
688 const int numSteps
= numDegrees
/ 15;
690 setZoomLevel(zoomLevel() + numSteps
);
697 void DolphinView::hideEvent(QHideEvent
* event
)
700 QWidget::hideEvent(event
);
703 void DolphinView::activate()
708 void DolphinView::slotItemActivated(int index
)
710 const KFileItem item
= fileItemModel()->fileItem(index
);
711 if (!item
.isNull()) {
712 emit
itemActivated(item
);
716 void DolphinView::slotItemsActivated(const QSet
<int>& indexes
)
718 Q_ASSERT(indexes
.count() >= 2);
722 KFileItemModel
* model
= fileItemModel();
723 QSetIterator
<int> it(indexes
);
724 while (it
.hasNext()) {
725 const int index
= it
.next();
726 items
.append(model
->fileItem(index
));
729 foreach (const KFileItem
& item
, items
) {
731 emit
tabRequested(item
.url());
733 emit
itemActivated(item
);
738 void DolphinView::slotItemMiddleClicked(int index
)
740 const KFileItem item
= fileItemModel()->fileItem(index
);
741 if (item
.isDir() || isTabsForFilesEnabled()) {
742 emit
tabRequested(item
.url());
746 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
748 const KFileItem item
= fileItemModel()->fileItem(index
);
749 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
752 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
754 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
757 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
759 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
761 KItemListView
* view
= m_container
->controller()->view();
762 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
764 // Add all roles to the menu that can be shown or hidden by the user
765 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
766 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
767 foreach (const DolphinView::AdditionalInfo info
, keys
) {
768 const QByteArray
& role
= infoAccessor
.role(info
);
769 if (role
!= "name") {
770 const QString text
= fileItemModel()->roleDescription(role
);
772 QAction
* action
= menu
.data()->addAction(text
);
773 action
->setCheckable(true);
774 action
->setChecked(visibleRolesSet
.contains(role
));
775 action
->setData(info
);
779 QAction
* action
= menu
.data()->exec(pos
.toPoint());
781 // Show or hide the selected role
782 const DolphinView::AdditionalInfo info
=
783 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
785 ViewProperties
props(url());
786 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
788 const QByteArray selectedRole
= infoAccessor
.role(info
);
789 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
791 if (action
->isChecked()) {
792 const int index
= keys
.indexOf(info
) + 1;
793 visibleRoles
.insert(index
, selectedRole
);
794 infoList
.insert(index
, info
);
796 visibleRoles
.removeOne(selectedRole
);
797 infoList
.removeOne(info
);
800 view
->setVisibleRoles(visibleRoles
);
801 props
.setAdditionalInfoList(infoList
);
807 void DolphinView::slotItemHovered(int index
)
809 const KFileItem item
= fileItemModel()->fileItem(index
);
811 if (GeneralSettings::showToolTips() && QApplication::mouseButtons() == Qt::NoButton
) {
812 QRectF itemRect
= m_container
->controller()->view()->itemContextRect(index
);
813 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
814 itemRect
.moveTo(pos
);
816 m_toolTipManager
->showToolTip(item
, itemRect
);
819 emit
requestItemInfo(item
);
822 void DolphinView::slotItemUnhovered(int index
)
826 emit
requestItemInfo(KFileItem());
829 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
832 KFileItem destItem
= fileItemModel()->fileItem(index
);
833 if (destItem
.isNull() || (!destItem
.isDir() && !destItem
.isDesktopFile())) {
834 // Use the URL of the view as drop target if the item is no directory
836 destItem
= fileItemModel()->rootItem();
839 // The item represents a directory or desktop-file
840 destUrl
= destItem
.url();
843 QDropEvent
dropEvent(event
->pos().toPoint(),
844 event
->possibleActions(),
849 const QString error
= DragAndDropHelper::dropUrls(destItem
, destUrl
, &dropEvent
);
850 if (!error
.isEmpty()) {
851 emit
errorMessage(error
);
855 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
858 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
861 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
862 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
864 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
865 m_versionControlObserver
->setModel(fileItemModel
);
868 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
870 const int currentCount
= current
.count();
871 const int previousCount
= previous
.count();
872 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
873 (currentCount
> 0 && previousCount
== 0);
875 // If nothing has been selected before and something got selected (or if something
876 // was selected before and now nothing is selected) the selectionChangedSignal must
877 // be emitted asynchronously as fast as possible to update the edit-actions.
878 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
879 m_selectionChangedTimer
->start();
882 void DolphinView::emitSelectionChangedSignal()
884 m_selectionChangedTimer
->stop();
885 emit
selectionChanged(selectedItems());
888 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
890 ViewProperties
props(url());
891 props
.setSorting(sorting
);
893 KItemModelBase
* model
= m_container
->controller()->model();
894 model
->setSortRole(sortRoleForSorting(sorting
));
896 emit
sortingChanged(sorting
);
899 void DolphinView::updateSortOrder(Qt::SortOrder order
)
901 ViewProperties
props(url());
902 props
.setSortOrder(order
);
904 KItemModelBase
* model
= fileItemModel();
905 model
->setSortOrder(order
);
907 emit
sortOrderChanged(order
);
910 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
912 ViewProperties
props(url());
913 props
.setSortFoldersFirst(foldersFirst
);
915 KFileItemModel
* model
= fileItemModel();
916 model
->setSortFoldersFirst(foldersFirst
);
918 emit
sortFoldersFirstChanged(foldersFirst
);
921 QPair
<bool, QString
> DolphinView::pasteInfo() const
923 return KonqOperations::pasteInfo(url());
926 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
928 m_tabsForFiles
= tabsForFiles
;
931 bool DolphinView::isTabsForFilesEnabled() const
933 return m_tabsForFiles
;
936 bool DolphinView::itemsExpandable() const
938 return m_mode
== DetailsView
;
941 void DolphinView::restoreState(QDataStream
& stream
)
943 // Restore the current item that had the keyboard focus
944 stream
>> m_currentItemUrl
;
946 // Restore the view position
947 stream
>> m_restoredContentsPosition
;
949 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
952 fileItemModel()->restoreExpandedUrls(urls
);
955 void DolphinView::saveState(QDataStream
& stream
)
957 // Save the current item that has the keyboard focus
958 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
959 if (currentIndex
!= -1) {
960 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
961 Q_ASSERT(!item
.isNull()); // If the current index is valid a item must exist
962 KUrl currentItemUrl
= item
.url();
963 stream
<< currentItemUrl
;
968 // Save view position
969 const qreal x
= m_container
->horizontalScrollBar()->value();
970 const qreal y
= m_container
->verticalScrollBar()->value();
971 stream
<< QPoint(x
, y
);
973 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
974 stream
<< fileItemModel()->expandedUrls();
977 bool DolphinView::hasSelection() const
979 return m_container
->controller()->selectionManager()->hasSelection();
982 KFileItem
DolphinView::rootItem() const
984 return m_dirLister
->rootItem();
987 void DolphinView::observeCreatedItem(const KUrl
& url
)
989 m_createdItemUrl
= url
;
990 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
991 // this, SLOT(selectAndScrollToCreatedItem()));
994 void DolphinView::selectAndScrollToCreatedItem()
996 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
997 if (dirIndex.isValid()) {
998 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
999 QAbstractItemView* view = m_viewAccessor.itemView();
1001 view->setCurrentIndex(proxyIndex);
1005 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1006 this, SLOT(selectAndScrollToCreatedItem()));*/
1007 m_createdItemUrl
= KUrl();
1010 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1012 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1013 emit
redirection(oldUrl
, newUrl
);
1014 m_url
= newUrl
; // #186947
1018 void DolphinView::updateViewState()
1020 if (m_currentItemUrl
!= KUrl()) {
1021 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1022 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1023 if (currentIndex
!= -1) {
1024 selectionManager
->setCurrentItem(currentIndex
);
1026 selectionManager
->setCurrentItem(0);
1028 m_currentItemUrl
= KUrl();
1031 if (!m_restoredContentsPosition
.isNull()) {
1032 const int x
= m_restoredContentsPosition
.x();
1033 const int y
= m_restoredContentsPosition
.y();
1034 m_restoredContentsPosition
= QPoint();
1036 m_container
->horizontalScrollBar()->setValue(x
);
1037 m_container
->verticalScrollBar()->setValue(y
);
1040 if (!m_selectedUrls
.isEmpty()) {
1041 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1042 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1043 const KFileItemModel
* model
= fileItemModel();
1045 foreach (const KUrl
& url
, m_selectedUrls
) {
1046 const int index
= model
->index(url
);
1048 selectedItems
.insert(index
);
1052 selectionManager
->setSelectedItems(selectedItems
);
1053 m_selectedUrls
.clear();
1057 void DolphinView::hideToolTip()
1059 if (GeneralSettings::showToolTips()) {
1060 m_toolTipManager
->hideToolTip();
1064 void DolphinView::showHoverInformation(const KFileItem
& item
)
1066 emit
requestItemInfo(item
);
1069 void DolphinView::clearHoverInformation()
1071 emit
requestItemInfo(KFileItem());
1074 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1076 if (job
->error() == 0) {
1077 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1078 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1079 emit
errorMessage(job
->errorString());
1083 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1085 // Disable the writestate temporary until it can be determined in a fast way
1086 // in DolphinView::slotLoadingCompleted()
1087 if (m_isFolderWritable
) {
1088 m_isFolderWritable
= false;
1089 emit
writeStateChanged(m_isFolderWritable
);
1092 emit
startedPathLoading(url
);
1095 void DolphinView::slotLoadingCompleted()
1097 // Update the view-state. This has to be done using a Qt::QueuedConnection
1098 // because the view might not be in its final state yet (the view also
1099 // listens to the completed()-signal from KDirLister and the order of
1100 // of slots is undefined).
1101 QTimer::singleShot(0, this, SLOT(updateViewState()));
1103 emit
finishedPathLoading(url());
1105 updateWritableState();
1108 void DolphinView::slotRefreshItems()
1110 if (m_assureVisibleCurrentIndex
) {
1111 m_assureVisibleCurrentIndex
= false;
1112 //QAbstractItemView* view = m_viewAccessor.itemView();
1114 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1119 void DolphinView::slotSortOrderChangedByHeader(Qt::SortOrder current
, Qt::SortOrder previous
)
1122 Q_ASSERT(fileItemModel()->sortOrder() == current
);
1124 ViewProperties
props(url());
1125 props
.setSortOrder(current
);
1127 emit
sortOrderChanged(current
);
1130 void DolphinView::slotSortRoleChangedByHeader(const QByteArray
& current
, const QByteArray
& previous
)
1133 Q_ASSERT(fileItemModel()->sortRole() == current
);
1135 ViewProperties
props(url());
1136 const Sorting sorting
= sortingForSortRole(current
);
1137 props
.setSorting(sorting
);
1139 emit
sortingChanged(sorting
);
1142 KFileItemModel
* DolphinView::fileItemModel() const
1144 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1147 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1149 if (!url
.isValid()) {
1150 const QString
location(url
.pathOrUrl());
1151 if (location
.isEmpty()) {
1152 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1154 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1159 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1162 void DolphinView::applyViewProperties()
1164 m_container
->beginTransaction();
1166 const ViewProperties
props(url());
1167 KFileItemModel
* model
= fileItemModel();
1169 const Mode mode
= props
.viewMode();
1170 if (m_mode
!= mode
) {
1171 const Mode previousMode
= m_mode
;
1174 // Changing the mode might result in changing
1175 // the zoom level. Remember the old zoom level so
1176 // that zoomLevelChanged() can get emitted.
1177 const int oldZoomLevel
= m_container
->zoomLevel();
1180 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1181 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1182 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1183 default: Q_ASSERT(false); break;
1186 emit
modeChanged(m_mode
, previousMode
);
1188 if (m_container
->zoomLevel() != oldZoomLevel
) {
1189 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1193 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1194 if (hiddenFilesShown
!= model
->showHiddenFiles()) {
1195 model
->setShowHiddenFiles(hiddenFilesShown
);
1196 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1199 const bool groupedSorting
= props
.groupedSorting();
1200 if (groupedSorting
!= model
->groupedSorting()) {
1201 model
->setGroupedSorting(groupedSorting
);
1202 emit
groupedSortingChanged(groupedSorting
);
1205 const DolphinView::Sorting sorting
= props
.sorting();
1206 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1207 if (newSortRole
!= model
->sortRole()) {
1208 model
->setSortRole(newSortRole
);
1209 emit
sortingChanged(sorting
);
1212 const Qt::SortOrder sortOrder
= props
.sortOrder();
1213 if (sortOrder
!= model
->sortOrder()) {
1214 model
->setSortOrder(sortOrder
);
1215 emit
sortOrderChanged(sortOrder
);
1218 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1219 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1220 model
->setSortFoldersFirst(sortFoldersFirst
);
1221 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1224 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1225 if (infoList
!= m_additionalInfoList
) {
1226 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1227 m_additionalInfoList
= infoList
;
1228 applyAdditionalInfoListToView();
1229 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1232 const bool previewsShown
= props
.previewsShown();
1233 if (previewsShown
!= m_container
->previewsShown()) {
1234 const int oldZoomLevel
= zoomLevel();
1236 m_container
->setPreviewsShown(previewsShown
);
1237 emit
previewsShownChanged(previewsShown
);
1239 // Changing the preview-state might result in a changed zoom-level
1240 if (oldZoomLevel
!= zoomLevel()) {
1241 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1245 m_container
->endTransaction();
1248 void DolphinView::applyAdditionalInfoListToView()
1250 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1252 QList
<QByteArray
> visibleRoles
;
1253 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1254 visibleRoles
.append("name");
1256 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1257 visibleRoles
.append(infoAccessor
.role(info
));
1260 m_container
->setVisibleRoles(visibleRoles
);
1263 void DolphinView::pasteToUrl(const KUrl
& url
)
1265 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1266 KonqOperations::doPaste(this, url
);
1269 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1273 const KFileItemList items
= selectedItems();
1274 foreach (const KFileItem
&item
, items
) {
1275 urls
.append(item
.url());
1278 if (itemsExpandable()) {
1279 // TODO: Check if we still need KDirModel for this in KDE 5.0
1280 urls
= KDirModel::simplifiedUrlList(urls
);
1286 QMimeData
* DolphinView::selectionMimeData() const
1288 const KFileItemModel
* model
= fileItemModel();
1289 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1290 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1292 return model
->createMimeData(selectedIndexes
);
1295 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1297 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1298 markUrlsAsSelected(urls
);
1301 void DolphinView::updateWritableState()
1303 const bool wasFolderWritable
= m_isFolderWritable
;
1304 m_isFolderWritable
= true;
1306 const KFileItem item
= m_dirLister
->rootItem();
1307 if (!item
.isNull()) {
1308 KFileItemListProperties
capabilities(KFileItemList() << item
);
1309 m_isFolderWritable
= capabilities
.supportsWriting();
1311 if (m_isFolderWritable
!= wasFolderWritable
) {
1312 emit
writeStateChanged(m_isFolderWritable
);
1316 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1319 case SortByName
: return "name";
1320 case SortBySize
: return "size";
1321 case SortByDate
: return "date";
1322 case SortByPermissions
: return "permissions";
1323 case SortByOwner
: return "owner";
1324 case SortByGroup
: return "group";
1325 case SortByType
: return "type";
1326 case SortByDestination
: return "destination";
1327 case SortByPath
: return "path";
1331 return QByteArray();
1334 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1336 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1337 if (sortHash
.isEmpty()) {
1338 sortHash
.insert("name", SortByName
);
1339 sortHash
.insert("size", SortBySize
);
1340 sortHash
.insert("date", SortByDate
);
1341 sortHash
.insert("permissions", SortByPermissions
);
1342 sortHash
.insert("owner", SortByOwner
);
1343 sortHash
.insert("group", SortByGroup
);
1344 sortHash
.insert("type", SortByType
);
1345 sortHash
.insert("destination", SortByDestination
);
1346 sortHash
.insert("path", SortByPath
);
1348 return sortHash
.value(sortRole
);
1351 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1353 const KLocale
* locale
= KGlobal::locale();
1354 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1358 if (fileSize
< multiplier
) {
1359 // Show the size in bytes
1360 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1361 } else if (fileSize
< multiplier
* multiplier
) {
1362 // Show the size in kilobytes and always round up. This is done
1363 // for consistency with the values shown e.g. in the "Size" column
1364 // of the details-view.
1365 fileSize
+= (multiplier
/ 2) - 1;
1366 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1368 // Show the size in the best fitting unit having one decimal
1369 text
= locale
->formatByteSize(fileSize
, 1);
1374 #include "dolphinview.moc"