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 //m_dolphinViewController = new DolphinViewController(this);
104 //m_viewModeController = new ViewModeController(this);
105 //m_viewModeController->setUrl(url);
107 /*connect(m_viewModeController, SIGNAL(urlChanged(KUrl)),
108 this, SIGNAL(urlChanged(KUrl)));
110 connect(m_dolphinViewController, SIGNAL(requestContextMenu(QPoint,QList<QAction*>)),
111 this, SLOT(openContextMenu(QPoint,QList<QAction*>)));
112 connect(m_dolphinViewController, SIGNAL(urlsDropped(KFileItem,KUrl,QDropEvent*)),
113 this, SLOT(dropUrls(KFileItem,KUrl,QDropEvent*)));
114 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
115 this, SLOT(updateSorting(DolphinView::Sorting)));
116 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
117 this, SLOT(updateSortOrder(Qt::SortOrder)));
118 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
119 this, SLOT(updateSortFoldersFirst(bool)));
120 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(QList<DolphinView::AdditionalInfo>)),
121 this, SLOT(updateAdditionalInfo(QList<DolphinView::AdditionalInfo>)));*/
122 //connect(m_dolphinViewController, SIGNAL(itemActivated(KFileItem)),
123 // this, SLOT(triggerItem(KFileItem)));
124 //connect(m_dolphinViewController, SIGNAL(tabRequested(KUrl)),
125 // this, SIGNAL(tabRequested(KUrl)));
126 /*connect(m_dolphinViewController, SIGNAL(activated()),
127 this, SLOT(activate()));
128 connect(m_dolphinViewController, SIGNAL(itemEntered(KFileItem)),
129 this, SLOT(showHoverInformation(KFileItem)));
130 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
131 this, SLOT(clearHoverInformation()));
132 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
133 this, SLOT(slotUrlChangeRequested(KUrl)));*/
135 // When a new item has been created by the "Create New..." menu, the item should
136 // get selected and it must be assured that the item will get visible. As the
137 // creation is done asynchronously, several signals must be checked:
138 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
139 this, SLOT(observeCreatedItem(KUrl
)));
141 m_selectionChangedTimer
= new QTimer(this);
142 m_selectionChangedTimer
->setSingleShot(true);
143 m_selectionChangedTimer
->setInterval(300);
144 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
145 this, SLOT(emitSelectionChangedSignal()));
147 m_dirLister
= new DolphinDirLister(this);
148 m_dirLister
->setAutoUpdate(true);
149 m_dirLister
->setDelayedMimeTypes(true);
151 connect(m_dirLister
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
152 connect(m_dirLister
, SIGNAL(started(KUrl
)), this, SLOT(slotDirListerStarted(KUrl
)));
153 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
154 this, SLOT(slotRefreshItems()));
156 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
157 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
158 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
159 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
160 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
161 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
162 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
164 m_container
= new DolphinItemListContainer(m_dirLister
, this);
165 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
166 m_container
->installEventFilter(this);
168 KItemListController
* controller
= m_container
->controller();
169 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
170 connect(controller
, SIGNAL(itemActivated(int)),
171 this, SLOT(slotItemActivated(int)));
172 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
173 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
174 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
175 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
176 connect(controller
, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
177 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
178 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
179 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
180 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
182 KFileItemModel
* model
= fileItemModel();
184 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
187 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
188 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
189 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
191 m_toolTipManager
= new ToolTipManager(this);
193 m_versionControlObserver
= new VersionControlObserver(this);
194 m_versionControlObserver
->setModel(model
);
195 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
196 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
197 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
199 applyViewProperties();
200 m_topLayout
->addWidget(m_container
);
205 DolphinView::~DolphinView()
209 KUrl
DolphinView::url() const
214 void DolphinView::setActive(bool active
)
216 if (active
== m_active
) {
222 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
227 /*QAbstractItemView* view = m_viewAccessor.itemView();
228 QWidget* viewport = view ? view->viewport() : 0;
231 palette.setColor(viewport->backgroundRole(), color);
232 viewport->setPalette(palette);
242 emit
writeStateChanged(m_isFolderWritable
);
245 //m_viewModeController->indicateActivationChange(active);
248 bool DolphinView::isActive() const
253 void DolphinView::setMode(Mode mode
)
255 if (mode
!= m_mode
) {
256 ViewProperties
props(url());
257 props
.setViewMode(mode
);
260 applyViewProperties();
264 DolphinView::Mode
DolphinView::mode() const
269 void DolphinView::setPreviewsShown(bool show
)
271 if (previewsShown() == show
) {
275 ViewProperties
props(url());
276 props
.setPreviewsShown(show
);
278 m_container
->setPreviewsShown(show
);
279 emit
previewsShownChanged(show
);
282 bool DolphinView::previewsShown() const
284 return m_container
->previewsShown();
287 void DolphinView::setHiddenFilesShown(bool show
)
289 if (m_dirLister
->showingDotFiles() == show
) {
293 const KFileItemList itemList
= selectedItems();
294 m_selectedUrls
.clear();
295 m_selectedUrls
= itemList
.urlList();
297 ViewProperties
props(url());
298 props
.setHiddenFilesShown(show
);
300 m_dirLister
->setShowingDotFiles(show
);
301 m_dirLister
->emitChanges();
302 emit
hiddenFilesShownChanged(show
);
305 bool DolphinView::hiddenFilesShown() const
307 return m_dirLister
->showingDotFiles();
310 void DolphinView::setGroupedSorting(bool grouped
)
312 if (grouped
== groupedSorting()) {
316 ViewProperties
props(url());
317 props
.setGroupedSorting(grouped
);
320 m_container
->controller()->model()->setGroupedSorting(grouped
);
322 emit
groupedSortingChanged(grouped
);
325 bool DolphinView::groupedSorting() const
327 return fileItemModel()->groupedSorting();
330 KFileItemList
DolphinView::items() const
332 return m_dirLister
->items();
335 KFileItemList
DolphinView::selectedItems() const
337 const KFileItemModel
* model
= fileItemModel();
338 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
339 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
341 QList
<int> sortedIndexes
= selectedIndexes
.toList();
342 qSort(sortedIndexes
);
344 KFileItemList selectedItems
;
345 QListIterator
<int> it(sortedIndexes
);
346 while (it
.hasNext()) {
347 const int index
= it
.next();
348 selectedItems
.append(model
->fileItem(index
));
350 return selectedItems
;
353 int DolphinView::selectedItemsCount() const
355 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
356 return selectionManager
->selectedItems().count();
359 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
361 m_selectedUrls
= urls
;
364 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
366 m_currentItemUrl
= url
;
369 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
371 const KItemListSelectionManager::SelectionMode mode
= enabled
372 ? KItemListSelectionManager::Select
373 : KItemListSelectionManager::Deselect
;
374 const KFileItemModel
* model
= fileItemModel();
375 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
377 for (int index
= 0; index
< model
->count(); index
++) {
378 const KFileItem item
= model
->fileItem(index
);
379 if (pattern
.exactMatch(item
.name())) {
380 // An alternative approach would be to store the matching items in a QSet<int> and
381 // select them in one go after the loop, but we'd need a new function
382 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
384 selectionManager
->setSelected(index
, 1, mode
);
389 void DolphinView::setZoomLevel(int level
)
391 const int oldZoomLevel
= zoomLevel();
392 m_container
->setZoomLevel(level
);
393 if (zoomLevel() != oldZoomLevel
) {
394 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
398 int DolphinView::zoomLevel() const
400 return m_container
->zoomLevel();
403 void DolphinView::setSorting(Sorting sorting
)
405 if (sorting
!= this->sorting()) {
406 updateSorting(sorting
);
410 DolphinView::Sorting
DolphinView::sorting() const
412 KItemModelBase
* model
= m_container
->controller()->model();
413 return sortingForSortRole(model
->sortRole());
416 void DolphinView::setSortOrder(Qt::SortOrder order
)
418 if (sortOrder() != order
) {
419 updateSortOrder(order
);
423 Qt::SortOrder
DolphinView::sortOrder() const
425 KItemModelBase
* model
= fileItemModel();
426 return model
->sortOrder();
429 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
431 if (sortFoldersFirst() != foldersFirst
) {
432 updateSortFoldersFirst(foldersFirst
);
436 bool DolphinView::sortFoldersFirst() const
438 KFileItemModel
* model
= fileItemModel();
439 return model
->sortFoldersFirst();
442 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
444 const QList
<AdditionalInfo
> previousList
= info
;
446 ViewProperties
props(url());
447 props
.setAdditionalInfoList(info
);
449 m_additionalInfoList
= info
;
450 applyAdditionalInfoListToView();
452 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
455 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
457 return m_additionalInfoList
;
460 void DolphinView::reload()
462 QByteArray viewState
;
463 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
464 saveState(saveStream
);
466 const KFileItemList itemList
= selectedItems();
467 m_selectedUrls
.clear();
468 m_selectedUrls
= itemList
.urlList();
471 loadDirectory(url(), true);
473 QDataStream
restoreStream(viewState
);
474 restoreState(restoreStream
);
477 void DolphinView::stopLoading()
482 void DolphinView::refresh()
484 const bool oldActivationState
= m_active
;
485 const int oldZoomLevel
= zoomLevel();
488 applyViewProperties();
491 setActive(oldActivationState
);
492 updateZoomLevel(oldZoomLevel
);
495 void DolphinView::setNameFilter(const QString
& nameFilter
)
497 Q_UNUSED(nameFilter
);
498 //m_viewModeController->setNameFilter(nameFilter);
501 QString
DolphinView::nameFilter() const
503 return QString(); //m_viewModeController->nameFilter();
506 void DolphinView::calculateItemCount(int& fileCount
,
508 KIO::filesize_t
& totalFileSize
) const
510 foreach (const KFileItem
& item
, m_dirLister
->items()) {
515 totalFileSize
+= item
.size();
520 QString
DolphinView::statusBarText() const
528 KIO::filesize_t totalFileSize
= 0;
530 if (hasSelection()) {
531 // Give a summary of the status of the selected files
532 const KFileItemList list
= selectedItems();
533 foreach (const KFileItem
& item
, list
) {
538 totalFileSize
+= item
.size();
542 if (folderCount
+ fileCount
== 1) {
543 // If only one item is selected, show the filename
544 filesText
= i18nc("@info:status", "<filename>%1</filename> selected", list
.first().text());
546 // At least 2 items are selected
547 foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
548 filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
551 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
552 foldersText
= i18ncp("@info:status", "1 Folder", "%1 Folders", folderCount
);
553 filesText
= i18ncp("@info:status", "1 File", "%1 Files", fileCount
);
556 if (fileCount
> 0 && folderCount
> 0) {
557 summary
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
558 foldersText
, filesText
, fileSizeText(totalFileSize
));
559 } else if (fileCount
> 0) {
560 summary
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, fileSizeText(totalFileSize
));
561 } else if (folderCount
> 0) {
562 summary
= foldersText
;
568 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
570 QList
<QAction
*> actions
;
572 if (items
.isEmpty()) {
573 const KFileItem item
= fileItemModel()->rootItem();
574 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
576 actions
= m_versionControlObserver
->actions(items
);
582 void DolphinView::setUrl(const KUrl
& url
)
588 emit
urlAboutToBeChanged(url
);
591 if (GeneralSettings::showToolTips()) {
592 m_toolTipManager
->hideToolTip();
595 // It is important to clear the items from the model before
596 // applying the view properties, otherwise expensive operations
597 // might be done on the existing items although they get cleared
598 // anyhow afterwards by loadDirectory().
599 fileItemModel()->clear();
600 applyViewProperties();
603 emit
urlChanged(url
);
606 void DolphinView::selectAll()
608 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
609 selectionManager
->setSelected(0, fileItemModel()->count());
612 void DolphinView::invertSelection()
614 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
615 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
618 void DolphinView::clearSelection()
620 m_container
->controller()->selectionManager()->clearSelection();
623 void DolphinView::renameSelectedItems()
625 KFileItemList items
= selectedItems();
626 const int itemCount
= items
.count();
631 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
632 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
633 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
634 m_viewAccessor.itemView()->edit(proxyIndex);
636 RenameDialog
* dialog
= new RenameDialog(this, items
);
637 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
640 dialog
->activateWindow();
643 // assure that the current index remains visible when KDirLister
644 // will notify the view about changed items
645 m_assureVisibleCurrentIndex
= true;
648 void DolphinView::trashSelectedItems()
650 const KUrl::List list
= simplifiedSelectedUrls();
651 KonqOperations::del(this, KonqOperations::TRASH
, list
);
654 void DolphinView::deleteSelectedItems()
656 const KUrl::List list
= simplifiedSelectedUrls();
657 const bool del
= KonqOperations::askDeleteConfirmation(list
,
659 KonqOperations::DEFAULT_CONFIRMATION
,
663 KIO::Job
* job
= KIO::del(list
);
664 connect(job
, SIGNAL(result(KJob
*)),
665 this, SLOT(slotDeleteFileFinished(KJob
*)));
669 void DolphinView::cutSelectedItems()
671 QMimeData
* mimeData
= selectionMimeData();
672 KonqMimeData::addIsCutSelection(mimeData
, true);
673 QApplication::clipboard()->setMimeData(mimeData
);
676 void DolphinView::copySelectedItems()
678 QMimeData
* mimeData
= selectionMimeData();
679 QApplication::clipboard()->setMimeData(mimeData
);
682 void DolphinView::paste()
687 void DolphinView::pasteIntoFolder()
689 const KFileItemList items
= selectedItems();
690 if ((items
.count() == 1) && items
.first().isDir()) {
691 pasteToUrl(items
.first().url());
695 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
697 switch (event
->type()) {
698 case QEvent::FocusIn
:
699 if (watched
== m_container
) {
708 return QWidget::eventFilter(watched
, event
);
711 void DolphinView::wheelEvent(QWheelEvent
* event
)
713 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
714 const int numDegrees
= event
->delta() / 8;
715 const int numSteps
= numDegrees
/ 15;
717 setZoomLevel(zoomLevel() + numSteps
);
724 void DolphinView::activate()
729 void DolphinView::slotItemActivated(int index
)
733 const KFileItemList items
= selectedItems();
734 if (items
.isEmpty()) {
738 if (items
.count() == 1) {
739 emit
itemActivated(items
.at(0)); // caught by DolphinViewContainer or DolphinPart
741 foreach (const KFileItem
& item
, items
) {
743 emit
tabRequested(item
.url());
745 emit
itemActivated(item
);
751 void DolphinView::slotItemMiddleClicked(int index
)
753 const KFileItem item
= fileItemModel()->fileItem(index
);
754 if (item
.isDir() || isTabsForFilesEnabled()) {
755 emit
tabRequested(item
.url());
759 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
761 if (GeneralSettings::showToolTips()) {
762 m_toolTipManager
->hideToolTip();
764 const KFileItem item
= fileItemModel()->fileItem(index
);
765 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
768 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
770 if (GeneralSettings::showToolTips()) {
771 m_toolTipManager
->hideToolTip();
773 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
776 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
778 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
780 KItemListView
* view
= m_container
->controller()->view();
781 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
783 // Add all roles to the menu that can be shown or hidden by the user
784 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
785 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
786 foreach (const DolphinView::AdditionalInfo info
, keys
) {
787 const QByteArray
& role
= infoAccessor
.role(info
);
788 if (role
!= "name") {
789 const QString text
= fileItemModel()->roleDescription(role
);
791 QAction
* action
= menu
.data()->addAction(text
);
792 action
->setCheckable(true);
793 action
->setChecked(visibleRolesSet
.contains(role
));
794 action
->setData(info
);
798 QAction
* action
= menu
.data()->exec(pos
.toPoint());
800 // Show or hide the selected role
801 const DolphinView::AdditionalInfo info
=
802 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
804 ViewProperties
props(url());
805 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
807 const QByteArray selectedRole
= infoAccessor
.role(info
);
808 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
810 if (action
->isChecked()) {
811 const int index
= keys
.indexOf(info
) + 1;
812 visibleRoles
.insert(index
, selectedRole
);
813 infoList
.insert(index
, info
);
815 visibleRoles
.removeOne(selectedRole
);
816 infoList
.removeOne(info
);
819 view
->setVisibleRoles(visibleRoles
);
820 props
.setAdditionalInfoList(infoList
);
826 void DolphinView::slotItemExpansionToggleClicked(int index
)
828 // TODO: When doing a model->setExpanded(false) it should
829 // be checked here whether the current index is part of the
830 // closed sub-tree. If this is the case, the current index
831 // should be adjusted to the parent index.
832 KFileItemModel
* model
= fileItemModel();
833 const bool expanded
= model
->isExpanded(index
);
834 model
->setExpanded(index
, !expanded
);
837 void DolphinView::slotItemHovered(int index
)
839 const KFileItem item
= fileItemModel()->fileItem(index
);
841 if (GeneralSettings::showToolTips()) {
842 QRectF itemRect
= m_container
->controller()->view()->itemRect(index
);
843 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
844 itemRect
.moveTo(pos
);
846 m_toolTipManager
->showToolTip(item
, itemRect
);
849 emit
requestItemInfo(item
);
852 void DolphinView::slotItemUnhovered(int index
)
855 if (GeneralSettings::showToolTips()) {
856 m_toolTipManager
->hideToolTip();
858 emit
requestItemInfo(KFileItem());
861 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
863 const KFileItem destItem
= fileItemModel()->fileItem(index
);
865 QDropEvent
dropEvent(event
->pos().toPoint(),
866 event
->possibleActions(),
871 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
874 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
877 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
880 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
881 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
883 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
884 m_versionControlObserver
->setModel(fileItemModel
);
887 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
889 const int currentCount
= current
.count();
890 const int previousCount
= previous
.count();
891 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
892 (currentCount
> 0 && previousCount
== 0);
894 // If nothing has been selected before and something got selected (or if something
895 // was selected before and now nothing is selected) the selectionChangedSignal must
896 // be emitted asynchronously as fast as possible to update the edit-actions.
897 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
898 m_selectionChangedTimer
->start();
901 void DolphinView::emitSelectionChangedSignal()
903 m_selectionChangedTimer
->stop();
904 emit
selectionChanged(selectedItems());
907 void DolphinView::dropUrls(const KFileItem
& destItem
,
908 const KUrl
& destPath
,
913 markPastedUrlsAsSelected(event
->mimeData());
914 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
917 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
919 ViewProperties
props(url());
920 props
.setSorting(sorting
);
922 KItemModelBase
* model
= m_container
->controller()->model();
923 model
->setSortRole(sortRoleForSorting(sorting
));
925 emit
sortingChanged(sorting
);
928 void DolphinView::updateSortOrder(Qt::SortOrder order
)
930 ViewProperties
props(url());
931 props
.setSortOrder(order
);
933 KItemModelBase
* model
= fileItemModel();
934 model
->setSortOrder(order
);
936 emit
sortOrderChanged(order
);
939 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
941 ViewProperties
props(url());
942 props
.setSortFoldersFirst(foldersFirst
);
944 KFileItemModel
* model
= fileItemModel();
945 model
->setSortFoldersFirst(foldersFirst
);
947 emit
sortFoldersFirstChanged(foldersFirst
);
950 QPair
<bool, QString
> DolphinView::pasteInfo() const
952 return KonqOperations::pasteInfo(url());
955 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
957 m_tabsForFiles
= tabsForFiles
;
960 bool DolphinView::isTabsForFilesEnabled() const
962 return m_tabsForFiles
;
965 bool DolphinView::itemsExpandable() const
967 return m_mode
== DetailsView
;
970 void DolphinView::restoreState(QDataStream
& stream
)
972 // Restore the current item that had the keyboard focus
973 stream
>> m_currentItemUrl
;
975 // Restore the view position
976 stream
>> m_restoredContentsPosition
;
978 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
981 fileItemModel()->restoreExpandedUrls(urls
);
984 void DolphinView::saveState(QDataStream
& stream
)
986 // Save the current item that has the keyboard focus
987 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
988 if (currentIndex
!= -1) {
989 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
990 KUrl currentItemUrl
= item
.url();
991 stream
<< currentItemUrl
;
996 // Save view position
997 const qreal x
= m_container
->horizontalScrollBar()->value();
998 const qreal y
= m_container
->verticalScrollBar()->value();
999 stream
<< QPoint(x
, y
);
1001 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1002 stream
<< fileItemModel()->expandedUrls();
1005 bool DolphinView::hasSelection() const
1007 return m_container
->controller()->selectionManager()->hasSelection();
1010 KFileItem
DolphinView::rootItem() const
1012 return m_dirLister
->rootItem();
1015 void DolphinView::observeCreatedItem(const KUrl
& url
)
1017 m_createdItemUrl
= url
;
1018 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1019 // this, SLOT(selectAndScrollToCreatedItem()));
1022 void DolphinView::selectAndScrollToCreatedItem()
1024 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1025 if (dirIndex.isValid()) {
1026 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1027 QAbstractItemView* view = m_viewAccessor.itemView();
1029 view->setCurrentIndex(proxyIndex);
1033 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1034 this, SLOT(selectAndScrollToCreatedItem()));*/
1035 m_createdItemUrl
= KUrl();
1038 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1040 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1041 emit
redirection(oldUrl
, newUrl
);
1042 m_url
= newUrl
; // #186947
1046 void DolphinView::updateViewState()
1048 if (m_currentItemUrl
!= KUrl()) {
1049 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1050 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1051 if (currentIndex
!= -1) {
1052 selectionManager
->setCurrentItem(currentIndex
);
1054 selectionManager
->setCurrentItem(0);
1056 m_currentItemUrl
= KUrl();
1059 if (!m_restoredContentsPosition
.isNull()) {
1060 const int x
= m_restoredContentsPosition
.x();
1061 const int y
= m_restoredContentsPosition
.y();
1062 m_restoredContentsPosition
= QPoint();
1064 m_container
->horizontalScrollBar()->setValue(x
);
1065 m_container
->verticalScrollBar()->setValue(y
);
1068 if (!m_selectedUrls
.isEmpty()) {
1069 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1070 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1071 const KFileItemModel
* model
= fileItemModel();
1073 foreach (const KUrl
& url
, m_selectedUrls
) {
1074 const int index
= model
->index(url
);
1076 selectedItems
.insert(index
);
1080 selectionManager
->setSelectedItems(selectedItems
);
1081 m_selectedUrls
.clear();
1085 void DolphinView::showHoverInformation(const KFileItem
& item
)
1087 emit
requestItemInfo(item
);
1090 void DolphinView::clearHoverInformation()
1092 emit
requestItemInfo(KFileItem());
1095 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1097 if (job
->error() == 0) {
1098 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1099 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1100 emit
errorMessage(job
->errorString());
1104 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1106 // Disable the writestate temporary until it can be determined in a fast way
1107 // in DolphinView::slotLoadingCompleted()
1108 if (m_isFolderWritable
) {
1109 m_isFolderWritable
= false;
1110 emit
writeStateChanged(m_isFolderWritable
);
1113 emit
startedPathLoading(url
);
1116 void DolphinView::slotLoadingCompleted()
1118 // Update the view-state. This has to be done using a Qt::QueuedConnection
1119 // because the view might not be in its final state yet (the view also
1120 // listens to the completed()-signal from KDirLister and the order of
1121 // of slots is undefined).
1122 QTimer::singleShot(0, this, SLOT(updateViewState()));
1124 emit
finishedPathLoading(url());
1126 updateWritableState();
1129 void DolphinView::slotRefreshItems()
1131 if (m_assureVisibleCurrentIndex
) {
1132 m_assureVisibleCurrentIndex
= false;
1133 //QAbstractItemView* view = m_viewAccessor.itemView();
1135 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1140 KFileItemModel
* DolphinView::fileItemModel() const
1142 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1145 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1147 if (!url
.isValid()) {
1148 const QString
location(url
.pathOrUrl());
1149 if (location
.isEmpty()) {
1150 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1152 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1157 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1160 void DolphinView::applyViewProperties()
1162 m_container
->beginTransaction();
1164 const ViewProperties
props(url());
1166 const Mode mode
= props
.viewMode();
1167 if (m_mode
!= mode
) {
1168 const Mode previousMode
= m_mode
;
1171 // Changing the mode might result in changing
1172 // the zoom level. Remember the old zoom level so
1173 // that zoomLevelChanged() can get emitted.
1174 const int oldZoomLevel
= m_container
->zoomLevel();
1177 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1178 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1179 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1180 default: Q_ASSERT(false); break;
1183 emit
modeChanged(m_mode
, previousMode
);
1185 if (m_container
->zoomLevel() != oldZoomLevel
) {
1186 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1190 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1191 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1192 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1193 m_dirLister
->emitChanges();
1194 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1197 KFileItemModel
* model
= fileItemModel();
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 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1271 Q_UNUSED(oldZoomLevel
);
1272 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1273 if (oldZoomLevel != newZoomLevel) {
1274 m_viewModeController->setZoomLevel(newZoomLevel);
1275 emit zoomLevelChanged(newZoomLevel);
1279 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1283 const KFileItemList items
= selectedItems();
1284 foreach (const KFileItem
&item
, items
) {
1285 urls
.append(item
.url());
1288 if (itemsExpandable()) {
1289 // TODO: Check if we still need KDirModel for this in KDE 5.0
1290 urls
= KDirModel::simplifiedUrlList(urls
);
1296 QMimeData
* DolphinView::selectionMimeData() const
1298 const KFileItemModel
* model
= fileItemModel();
1299 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1300 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1302 return model
->createMimeData(selectedIndexes
);
1305 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1307 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1308 markUrlsAsSelected(urls
);
1311 void DolphinView::updateWritableState()
1313 const bool wasFolderWritable
= m_isFolderWritable
;
1314 m_isFolderWritable
= true;
1316 const KFileItem item
= m_dirLister
->rootItem();
1317 if (!item
.isNull()) {
1318 KFileItemListProperties
capabilities(KFileItemList() << item
);
1319 m_isFolderWritable
= capabilities
.supportsWriting();
1321 if (m_isFolderWritable
!= wasFolderWritable
) {
1322 emit
writeStateChanged(m_isFolderWritable
);
1326 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1329 case SortByName
: return "name";
1330 case SortBySize
: return "size";
1331 case SortByDate
: return "date";
1332 case SortByPermissions
: return "permissions";
1333 case SortByOwner
: return "owner";
1334 case SortByGroup
: return "group";
1335 case SortByType
: return "type";
1336 case SortByDestination
: return "destination";
1337 case SortByPath
: return "path";
1341 return QByteArray();
1344 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1346 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1347 if (sortHash
.isEmpty()) {
1348 sortHash
.insert("name", SortByName
);
1349 sortHash
.insert("size", SortBySize
);
1350 sortHash
.insert("date", SortByDate
);
1351 sortHash
.insert("permissions", SortByPermissions
);
1352 sortHash
.insert("owner", SortByOwner
);
1353 sortHash
.insert("group", SortByGroup
);
1354 sortHash
.insert("type", SortByType
);
1355 sortHash
.insert("destination", SortByDestination
);
1356 sortHash
.insert("path", SortByPath
);
1358 return sortHash
.value(sortRole
);
1361 QString
DolphinView::fileSizeText(KIO::filesize_t fileSize
)
1363 const KLocale
* locale
= KGlobal::locale();
1364 const unsigned int multiplier
= (locale
->binaryUnitDialect() == KLocale::MetricBinaryDialect
)
1368 if (fileSize
< multiplier
) {
1369 // Show the size in bytes
1370 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitByte
);
1371 } else if (fileSize
< multiplier
* multiplier
) {
1372 // Show the size in kilobytes and always round up. This is done
1373 // for consistency with the values shown e.g. in the "Size" column
1374 // of the details-view.
1375 fileSize
+= (multiplier
/ 2) - 1;
1376 text
= locale
->formatByteSize(fileSize
, 0, KLocale::DefaultBinaryDialect
, KLocale::UnitKiloByte
);
1378 // Show the size in the best fitting unit having one decimal
1379 text
= locale
->formatByteSize(fileSize
, 1);
1384 #include "dolphinview.moc"