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 "settings/dolphinsettings.h"
68 #include "versioncontrol/versioncontrolobserver.h"
69 #include "viewmodecontroller.h"
70 #include "viewproperties.h"
71 #include "views/tooltips/tooltipmanager.h"
72 #include "zoomlevelinfo.h"
75 const int MaxModeEnum
= DolphinView::CompactView
;
76 const int MaxSortingEnum
= DolphinView::SortByPath
;
79 DolphinView::DolphinView(const KUrl
& url
, QWidget
* parent
) :
82 m_tabsForFiles(false),
83 m_assureVisibleCurrentIndex(false),
84 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 //m_dolphinViewController = new DolphinViewController(this);
105 //m_viewModeController = new ViewModeController(this);
106 //m_viewModeController->setUrl(url);
108 /*connect(m_viewModeController, SIGNAL(urlChanged(KUrl)),
109 this, SIGNAL(urlChanged(KUrl)));
111 connect(m_dolphinViewController, SIGNAL(requestContextMenu(QPoint,QList<QAction*>)),
112 this, SLOT(openContextMenu(QPoint,QList<QAction*>)));
113 connect(m_dolphinViewController, SIGNAL(urlsDropped(KFileItem,KUrl,QDropEvent*)),
114 this, SLOT(dropUrls(KFileItem,KUrl,QDropEvent*)));
115 connect(m_dolphinViewController, SIGNAL(sortingChanged(DolphinView::Sorting)),
116 this, SLOT(updateSorting(DolphinView::Sorting)));
117 connect(m_dolphinViewController, SIGNAL(sortOrderChanged(Qt::SortOrder)),
118 this, SLOT(updateSortOrder(Qt::SortOrder)));
119 connect(m_dolphinViewController, SIGNAL(sortFoldersFirstChanged(bool)),
120 this, SLOT(updateSortFoldersFirst(bool)));
121 connect(m_dolphinViewController, SIGNAL(additionalInfoChanged(QList<DolphinView::AdditionalInfo>)),
122 this, SLOT(updateAdditionalInfo(QList<DolphinView::AdditionalInfo>)));*/
123 //connect(m_dolphinViewController, SIGNAL(itemActivated(KFileItem)),
124 // this, SLOT(triggerItem(KFileItem)));
125 //connect(m_dolphinViewController, SIGNAL(tabRequested(KUrl)),
126 // this, SIGNAL(tabRequested(KUrl)));
127 /*connect(m_dolphinViewController, SIGNAL(activated()),
128 this, SLOT(activate()));
129 connect(m_dolphinViewController, SIGNAL(itemEntered(KFileItem)),
130 this, SLOT(showHoverInformation(KFileItem)));
131 connect(m_dolphinViewController, SIGNAL(viewportEntered()),
132 this, SLOT(clearHoverInformation()));
133 connect(m_dolphinViewController, SIGNAL(urlChangeRequested(KUrl)),
134 this, SLOT(slotUrlChangeRequested(KUrl)));*/
136 // When a new item has been created by the "Create New..." menu, the item should
137 // get selected and it must be assured that the item will get visible. As the
138 // creation is done asynchronously, several signals must be checked:
139 connect(&DolphinNewFileMenuObserver::instance(), SIGNAL(itemCreated(KUrl
)),
140 this, SLOT(observeCreatedItem(KUrl
)));
142 m_selectionChangedTimer
= new QTimer(this);
143 m_selectionChangedTimer
->setSingleShot(true);
144 m_selectionChangedTimer
->setInterval(300);
145 connect(m_selectionChangedTimer
, SIGNAL(timeout()),
146 this, SLOT(emitSelectionChangedSignal()));
148 m_dirLister
= new DolphinDirLister(this);
149 m_dirLister
->setAutoUpdate(true);
150 m_dirLister
->setDelayedMimeTypes(true);
152 connect(m_dirLister
, SIGNAL(redirection(KUrl
,KUrl
)), this, SLOT(slotRedirection(KUrl
,KUrl
)));
153 connect(m_dirLister
, SIGNAL(started(KUrl
)), this, SLOT(slotDirListerStarted(KUrl
)));
154 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
155 this, SLOT(slotRefreshItems()));
157 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
158 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
159 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
160 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
161 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
162 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
163 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
165 m_container
= new DolphinItemListContainer(m_dirLister
, this);
166 m_container
->setVisibleRoles(QList
<QByteArray
>() << "name");
167 m_container
->installEventFilter(this);
169 KItemListController
* controller
= m_container
->controller();
170 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
171 connect(controller
, SIGNAL(itemActivated(int)),
172 this, SLOT(slotItemActivated(int)));
173 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
174 connect(controller
, SIGNAL(itemContextMenuRequested(int,QPointF
)), this, SLOT(slotItemContextMenuRequested(int,QPointF
)));
175 connect(controller
, SIGNAL(viewContextMenuRequested(QPointF
)), this, SLOT(slotViewContextMenuRequested(QPointF
)));
176 connect(controller
, SIGNAL(headerContextMenuRequested(QPointF
)), this, SLOT(slotHeaderContextMenuRequested(QPointF
)));
177 connect(controller
, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
178 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
179 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
180 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
181 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
183 KFileItemModel
* model
= fileItemModel();
185 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
188 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
189 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
190 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
192 m_toolTipManager
= new ToolTipManager(this);
194 m_versionControlObserver
= new VersionControlObserver(this);
195 m_versionControlObserver
->setModel(model
);
196 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
197 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
198 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
200 applyViewProperties();
201 m_topLayout
->addWidget(m_container
);
206 DolphinView::~DolphinView()
210 KUrl
DolphinView::url() const
215 void DolphinView::setActive(bool active
)
217 if (active
== m_active
) {
223 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
228 /*QAbstractItemView* view = m_viewAccessor.itemView();
229 QWidget* viewport = view ? view->viewport() : 0;
232 palette.setColor(viewport->backgroundRole(), color);
233 viewport->setPalette(palette);
243 emit
writeStateChanged(m_isFolderWritable
);
246 //m_viewModeController->indicateActivationChange(active);
249 bool DolphinView::isActive() const
254 void DolphinView::setMode(Mode mode
)
256 if (mode
!= m_mode
) {
257 ViewProperties
props(url());
258 props
.setViewMode(mode
);
261 applyViewProperties();
265 DolphinView::Mode
DolphinView::mode() const
270 bool DolphinView::previewsShown() const
272 return m_container
->previewsShown();
275 bool DolphinView::hiddenFilesShown() const
277 return m_dirLister
->showingDotFiles();
280 bool DolphinView::categorizedSorting() const
282 return false; //m_storedCategorizedSorting;
285 KFileItemList
DolphinView::items() const
287 return m_dirLister
->items();
290 KFileItemList
DolphinView::selectedItems() const
292 const KFileItemModel
* model
= fileItemModel();
293 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
294 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
296 KFileItemList selectedItems
;
297 QSetIterator
<int> it(selectedIndexes
);
298 while (it
.hasNext()) {
299 const int index
= it
.next();
300 selectedItems
.append(model
->fileItem(index
));
302 return selectedItems
;
305 int DolphinView::selectedItemsCount() const
307 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
308 return selectionManager
->selectedItems().count();
311 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
313 m_selectedUrls
= urls
;
316 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
318 const KItemListSelectionManager::SelectionMode mode
= enabled
319 ? KItemListSelectionManager::Select
320 : KItemListSelectionManager::Deselect
;
321 const KFileItemModel
* model
= fileItemModel();
322 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
324 for (int index
= 0; index
< model
->count(); index
++) {
325 const KFileItem item
= model
->fileItem(index
);
326 if (pattern
.exactMatch(item
.name())) {
327 // An alternative approach would be to store the matching items in a QSet<int> and
328 // select them in one go after the loop, but we'd need a new function
329 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
331 selectionManager
->setSelected(index
, 1, mode
);
336 void DolphinView::setZoomLevel(int level
)
338 const int oldZoomLevel
= zoomLevel();
339 m_container
->setZoomLevel(level
);
340 if (zoomLevel() != oldZoomLevel
) {
341 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
345 int DolphinView::zoomLevel() const
347 return m_container
->zoomLevel();
350 void DolphinView::setSorting(Sorting sorting
)
352 if (sorting
!= this->sorting()) {
353 updateSorting(sorting
);
357 DolphinView::Sorting
DolphinView::sorting() const
359 KItemModelBase
* model
= m_container
->controller()->model();
360 return sortingForSortRole(model
->sortRole());
363 void DolphinView::setSortOrder(Qt::SortOrder order
)
365 if (sortOrder() != order
) {
366 updateSortOrder(order
);
370 Qt::SortOrder
DolphinView::sortOrder() const
372 KItemModelBase
* model
= fileItemModel();
373 return model
->sortOrder();
376 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
378 if (sortFoldersFirst() != foldersFirst
) {
379 updateSortFoldersFirst(foldersFirst
);
383 bool DolphinView::sortFoldersFirst() const
385 KFileItemModel
* model
= fileItemModel();
386 return model
->sortFoldersFirst();
389 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
391 const QList
<AdditionalInfo
> previousList
= info
;
393 ViewProperties
props(url());
394 props
.setAdditionalInfoList(info
);
396 m_additionalInfoList
= info
;
397 applyAdditionalInfoListToView();
399 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
402 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
404 return m_additionalInfoList
;
407 void DolphinView::reload()
409 QByteArray viewState
;
410 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
411 saveState(saveStream
);
413 const KFileItemList itemList
= selectedItems();
414 m_selectedUrls
.clear();
415 m_selectedUrls
= itemList
.urlList();
418 loadDirectory(url(), true);
420 QDataStream
restoreStream(viewState
);
421 restoreState(restoreStream
);
424 void DolphinView::stopLoading()
429 void DolphinView::refresh()
431 const bool oldActivationState
= m_active
;
432 const int oldZoomLevel
= zoomLevel();
435 applyViewProperties();
438 setActive(oldActivationState
);
439 updateZoomLevel(oldZoomLevel
);
442 void DolphinView::setNameFilter(const QString
& nameFilter
)
444 Q_UNUSED(nameFilter
);
445 //m_viewModeController->setNameFilter(nameFilter);
448 QString
DolphinView::nameFilter() const
450 return QString(); //m_viewModeController->nameFilter();
453 void DolphinView::calculateItemCount(int& fileCount
,
455 KIO::filesize_t
& totalFileSize
) const
457 foreach (const KFileItem
& item
, m_dirLister
->items()) {
462 totalFileSize
+= item
.size();
467 QString
DolphinView::statusBarText() const
472 KIO::filesize_t totalFileSize
= 0;
474 if (hasSelection()) {
475 // give a summary of the status of the selected files
476 const KFileItemList list
= selectedItems();
477 if (list
.isEmpty()) {
478 // when an item is triggered, it is temporary selected but selectedItems()
479 // will return an empty list
483 KFileItemList::const_iterator it
= list
.begin();
484 const KFileItemList::const_iterator end
= list
.end();
486 const KFileItem
& item
= *it
;
491 totalFileSize
+= item
.size();
496 if (folderCount
+ fileCount
== 1) {
497 // if only one item is selected, show the filename
498 const QString name
= list
.first().text();
499 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
500 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
501 name
, KIO::convertSize(totalFileSize
));
503 // at least 2 items are selected
504 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
505 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
506 if ((folderCount
> 0) && (fileCount
> 0)) {
507 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
508 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
509 } else if (fileCount
> 0) {
510 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
512 Q_ASSERT(folderCount
> 0);
517 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
518 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
519 fileCount
, folderCount
,
520 totalFileSize
, true);
526 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
528 QList
<QAction
*> actions
;
530 if (items
.isEmpty()) {
531 const KFileItem item
= fileItemModel()->rootItem();
532 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
534 actions
= m_versionControlObserver
->actions(items
);
540 void DolphinView::setUrl(const KUrl
& url
)
546 emit
urlAboutToBeChanged(url
);
549 if (GeneralSettings::showToolTips()) {
550 m_toolTipManager
->hideToolTip();
553 // It is important to clear the items from the model before
554 // applying the view properties, otherwise expensive operations
555 // might be done on the existing items although they get cleared
556 // anyhow afterwards by loadDirectory().
557 fileItemModel()->clear();
558 applyViewProperties();
561 emit
urlChanged(url
);
564 void DolphinView::selectAll()
566 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
567 selectionManager
->setSelected(0, fileItemModel()->count());
570 void DolphinView::invertSelection()
572 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
573 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
576 void DolphinView::clearSelection()
578 m_container
->controller()->selectionManager()->clearSelection();
581 void DolphinView::renameSelectedItems()
583 KFileItemList items
= selectedItems();
584 const int itemCount
= items
.count();
589 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
590 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
591 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
592 m_viewAccessor.itemView()->edit(proxyIndex);
594 RenameDialog
* dialog
= new RenameDialog(this, items
);
595 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
598 dialog
->activateWindow();
601 // assure that the current index remains visible when KDirLister
602 // will notify the view about changed items
603 m_assureVisibleCurrentIndex
= true;
606 void DolphinView::trashSelectedItems()
608 const KUrl::List list
= simplifiedSelectedUrls();
609 KonqOperations::del(this, KonqOperations::TRASH
, list
);
612 void DolphinView::deleteSelectedItems()
614 const KUrl::List list
= simplifiedSelectedUrls();
615 const bool del
= KonqOperations::askDeleteConfirmation(list
,
617 KonqOperations::DEFAULT_CONFIRMATION
,
621 KIO::Job
* job
= KIO::del(list
);
622 connect(job
, SIGNAL(result(KJob
*)),
623 this, SLOT(slotDeleteFileFinished(KJob
*)));
627 void DolphinView::cutSelectedItems()
629 QMimeData
* mimeData
= selectionMimeData();
630 KonqMimeData::addIsCutSelection(mimeData
, true);
631 QApplication::clipboard()->setMimeData(mimeData
);
634 void DolphinView::copySelectedItems()
636 QMimeData
* mimeData
= selectionMimeData();
637 QApplication::clipboard()->setMimeData(mimeData
);
640 void DolphinView::paste()
645 void DolphinView::pasteIntoFolder()
647 const KFileItemList items
= selectedItems();
648 if ((items
.count() == 1) && items
.first().isDir()) {
649 pasteToUrl(items
.first().url());
653 void DolphinView::setPreviewsShown(bool show
)
655 if (previewsShown() == show
) {
659 ViewProperties
props(url());
660 props
.setPreviewsShown(show
);
662 m_container
->setPreviewsShown(show
);
663 emit
previewsShownChanged(show
);
666 void DolphinView::setHiddenFilesShown(bool show
)
668 if (m_dirLister
->showingDotFiles() == show
) {
672 const KFileItemList itemList
= selectedItems();
673 m_selectedUrls
.clear();
674 m_selectedUrls
= itemList
.urlList();
676 ViewProperties
props(url());
677 props
.setHiddenFilesShown(show
);
679 m_dirLister
->setShowingDotFiles(show
);
680 m_dirLister
->emitChanges();
681 emit
hiddenFilesShownChanged(show
);
684 void DolphinView::setCategorizedSorting(bool categorized
)
686 if (categorized
== categorizedSorting()) {
690 ViewProperties
props(url());
691 props
.setCategorizedSorting(categorized
);
694 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
696 emit
categorizedSortingChanged(categorized
);
699 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
701 switch (event
->type()) {
702 case QEvent::FocusIn
:
703 if (watched
== m_container
) {
712 return QWidget::eventFilter(watched
, event
);
715 void DolphinView::activate()
720 void DolphinView::slotItemActivated(int index
)
724 const KFileItemList items
= selectedItems();
725 if (items
.isEmpty()) {
729 if (items
.count() == 1) {
730 emit
itemActivated(items
.at(0)); // caught by DolphinViewContainer or DolphinPart
732 foreach (const KFileItem
& item
, items
) {
734 emit
tabRequested(item
.url());
736 emit
itemActivated(item
);
742 void DolphinView::slotItemMiddleClicked(int index
)
744 const KFileItem item
= fileItemModel()->fileItem(index
);
745 if (item
.isDir() || isTabsForFilesEnabled()) {
746 emit
tabRequested(item
.url());
750 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
752 if (GeneralSettings::showToolTips()) {
753 m_toolTipManager
->hideToolTip();
755 const KFileItem item
= fileItemModel()->fileItem(index
);
756 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
759 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
761 if (GeneralSettings::showToolTips()) {
762 m_toolTipManager
->hideToolTip();
764 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
767 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
769 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
771 KItemListView
* view
= m_container
->controller()->view();
772 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
774 // Add all roles to the menu that can be shown or hidden by the user
775 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
776 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
777 foreach (const DolphinView::AdditionalInfo info
, keys
) {
778 const QByteArray
& role
= infoAccessor
.role(info
);
779 if (role
!= "name") {
780 const QString text
= fileItemModel()->roleDescription(role
);
782 QAction
* action
= menu
.data()->addAction(text
);
783 action
->setCheckable(true);
784 action
->setChecked(visibleRolesSet
.contains(role
));
785 action
->setData(info
);
789 QAction
* action
= menu
.data()->exec(pos
.toPoint());
791 // Show or hide the selected role
792 const DolphinView::AdditionalInfo info
=
793 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
795 ViewProperties
props(url());
796 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
798 const QByteArray selectedRole
= infoAccessor
.role(info
);
799 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
801 if (action
->isChecked()) {
802 const int index
= keys
.indexOf(info
) + 1;
803 visibleRoles
.insert(index
, selectedRole
);
804 infoList
.insert(index
, info
);
806 visibleRoles
.removeOne(selectedRole
);
807 infoList
.removeOne(info
);
810 view
->setVisibleRoles(visibleRoles
);
811 props
.setAdditionalInfoList(infoList
);
817 void DolphinView::slotItemExpansionToggleClicked(int index
)
819 // TODO: When doing a model->setExpanded(false) it should
820 // be checked here whether the current index is part of the
821 // closed sub-tree. If this is the case, the current index
822 // should be adjusted to the parent index.
823 KFileItemModel
* model
= fileItemModel();
824 const bool expanded
= model
->isExpanded(index
);
825 model
->setExpanded(index
, !expanded
);
828 void DolphinView::slotItemHovered(int index
)
830 const KFileItem item
= fileItemModel()->fileItem(index
);
832 if (GeneralSettings::showToolTips()) {
833 QRectF itemRect
= m_container
->controller()->view()->itemBoundingRect(index
);
834 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
835 itemRect
.moveTo(pos
);
837 m_toolTipManager
->showToolTip(item
, itemRect
);
840 emit
requestItemInfo(item
);
843 void DolphinView::slotItemUnhovered(int index
)
846 if (GeneralSettings::showToolTips()) {
847 m_toolTipManager
->hideToolTip();
849 emit
requestItemInfo(KFileItem());
852 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
854 const KFileItem destItem
= fileItemModel()->fileItem(index
);
856 QDropEvent
dropEvent(event
->pos().toPoint(),
857 event
->possibleActions(),
862 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
865 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
868 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
871 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
872 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
874 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
875 m_versionControlObserver
->setModel(fileItemModel
);
878 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
880 const int currentCount
= current
.count();
881 const int previousCount
= previous
.count();
882 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
883 (currentCount
> 0 && previousCount
== 0);
885 // If nothing has been selected before and something got selected (or if something
886 // was selected before and now nothing is selected) the selectionChangedSignal must
887 // be emitted asynchronously as fast as possible to update the edit-actions.
888 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
889 m_selectionChangedTimer
->start();
892 void DolphinView::emitSelectionChangedSignal()
894 m_selectionChangedTimer
->stop();
895 emit
selectionChanged(selectedItems());
898 void DolphinView::dropUrls(const KFileItem
& destItem
,
899 const KUrl
& destPath
,
904 markPastedUrlsAsSelected(event
->mimeData());
905 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
908 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
910 ViewProperties
props(url());
911 props
.setSorting(sorting
);
913 KItemModelBase
* model
= m_container
->controller()->model();
914 model
->setSortRole(sortRoleForSorting(sorting
));
916 emit
sortingChanged(sorting
);
919 void DolphinView::updateSortOrder(Qt::SortOrder order
)
921 ViewProperties
props(url());
922 props
.setSortOrder(order
);
924 KItemModelBase
* model
= fileItemModel();
925 model
->setSortOrder(order
);
927 emit
sortOrderChanged(order
);
930 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
932 ViewProperties
props(url());
933 props
.setSortFoldersFirst(foldersFirst
);
935 KFileItemModel
* model
= fileItemModel();
936 model
->setSortFoldersFirst(foldersFirst
);
938 emit
sortFoldersFirstChanged(foldersFirst
);
941 QPair
<bool, QString
> DolphinView::pasteInfo() const
943 return KonqOperations::pasteInfo(url());
946 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
948 m_tabsForFiles
= tabsForFiles
;
951 bool DolphinView::isTabsForFilesEnabled() const
953 return m_tabsForFiles
;
956 bool DolphinView::itemsExpandable() const
958 return m_mode
== DetailsView
;
961 void DolphinView::restoreState(QDataStream
& stream
)
963 // Restore the current item that had the keyboard focus
964 stream
>> m_currentItemUrl
;
966 // Restore the view position
967 stream
>> m_restoredContentsPosition
;
969 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
972 fileItemModel()->restoreExpandedUrls(urls
);
975 void DolphinView::saveState(QDataStream
& stream
)
977 // Save the current item that has the keyboard focus
978 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
979 if (currentIndex
!= -1) {
980 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
981 KUrl currentItemUrl
= item
.url();
982 stream
<< currentItemUrl
;
987 // Save view position
988 const qreal x
= m_container
->horizontalScrollBar()->value();
989 const qreal y
= m_container
->verticalScrollBar()->value();
990 stream
<< QPoint(x
, y
);
992 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
993 stream
<< fileItemModel()->expandedUrls();
996 bool DolphinView::hasSelection() const
998 return m_container
->controller()->selectionManager()->hasSelection();
1001 KFileItem
DolphinView::rootItem() const
1003 return m_dirLister
->rootItem();
1006 void DolphinView::observeCreatedItem(const KUrl
& url
)
1008 m_createdItemUrl
= url
;
1009 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1010 // this, SLOT(selectAndScrollToCreatedItem()));
1013 void DolphinView::selectAndScrollToCreatedItem()
1015 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1016 if (dirIndex.isValid()) {
1017 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1018 QAbstractItemView* view = m_viewAccessor.itemView();
1020 view->setCurrentIndex(proxyIndex);
1024 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1025 this, SLOT(selectAndScrollToCreatedItem()));*/
1026 m_createdItemUrl
= KUrl();
1029 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1031 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1032 emit
redirection(oldUrl
, newUrl
);
1033 m_url
= newUrl
; // #186947
1037 void DolphinView::updateViewState()
1039 if (m_currentItemUrl
!= KUrl()) {
1040 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1041 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1042 if (currentIndex
!= -1) {
1043 selectionManager
->setCurrentItem(currentIndex
);
1045 selectionManager
->setCurrentItem(0);
1047 m_currentItemUrl
= KUrl();
1050 if (!m_restoredContentsPosition
.isNull()) {
1051 const int x
= m_restoredContentsPosition
.x();
1052 const int y
= m_restoredContentsPosition
.y();
1053 m_restoredContentsPosition
= QPoint();
1055 m_container
->horizontalScrollBar()->setValue(x
);
1056 m_container
->verticalScrollBar()->setValue(y
);
1059 if (!m_selectedUrls
.isEmpty()) {
1060 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1061 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1062 const KFileItemModel
* model
= fileItemModel();
1064 foreach (const KUrl
& url
, m_selectedUrls
) {
1065 const int index
= model
->index(url
);
1067 selectedItems
.insert(index
);
1071 selectionManager
->setSelectedItems(selectedItems
);
1072 m_selectedUrls
.clear();
1076 void DolphinView::showHoverInformation(const KFileItem
& item
)
1078 emit
requestItemInfo(item
);
1081 void DolphinView::clearHoverInformation()
1083 emit
requestItemInfo(KFileItem());
1086 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1088 if (job
->error() == 0) {
1089 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1090 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1091 emit
errorMessage(job
->errorString());
1095 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1097 // Disable the writestate temporary until it can be determined in a fast way
1098 // in DolphinView::slotLoadingCompleted()
1099 if (m_isFolderWritable
) {
1100 m_isFolderWritable
= false;
1101 emit
writeStateChanged(m_isFolderWritable
);
1104 emit
startedPathLoading(url
);
1107 void DolphinView::slotLoadingCompleted()
1109 // Update the view-state. This has to be done using a Qt::QueuedConnection
1110 // because the view might not be in its final state yet (the view also
1111 // listens to the completed()-signal from KDirLister and the order of
1112 // of slots is undefined).
1113 QTimer::singleShot(0, this, SLOT(updateViewState()));
1115 emit
finishedPathLoading(url());
1117 updateWritableState();
1120 void DolphinView::slotRefreshItems()
1122 if (m_assureVisibleCurrentIndex
) {
1123 m_assureVisibleCurrentIndex
= false;
1124 //QAbstractItemView* view = m_viewAccessor.itemView();
1126 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1131 KFileItemModel
* DolphinView::fileItemModel() const
1133 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1136 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1138 if (!url
.isValid()) {
1139 const QString
location(url
.pathOrUrl());
1140 if (location
.isEmpty()) {
1141 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1143 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1148 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1151 void DolphinView::applyViewProperties()
1153 m_container
->beginTransaction();
1155 const ViewProperties
props(url());
1157 const Mode mode
= props
.viewMode();
1158 if (m_mode
!= mode
) {
1159 const Mode previousMode
= m_mode
;
1162 // Changing the mode might result in changing
1163 // the zoom level. Remember the old zoom level so
1164 // that zoomLevelChanged() can get emitted.
1165 const int oldZoomLevel
= m_container
->zoomLevel();
1168 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1169 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1170 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1171 default: Q_ASSERT(false); break;
1174 emit
modeChanged(m_mode
, previousMode
);
1176 if (m_container
->zoomLevel() != oldZoomLevel
) {
1177 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1181 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1182 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1183 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1184 m_dirLister
->emitChanges();
1185 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1188 /* m_storedCategorizedSorting = props.categorizedSorting();
1189 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1190 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1191 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1192 emit categorizedSortingChanged();
1195 const DolphinView::Sorting sorting
= props
.sorting();
1196 KFileItemModel
* model
= fileItemModel();
1197 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1198 if (newSortRole
!= model
->sortRole()) {
1199 model
->setSortRole(newSortRole
);
1200 emit
sortingChanged(sorting
);
1203 const Qt::SortOrder sortOrder
= props
.sortOrder();
1204 if (sortOrder
!= model
->sortOrder()) {
1205 model
->setSortOrder(sortOrder
);
1206 emit
sortOrderChanged(sortOrder
);
1209 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1210 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1211 model
->setSortFoldersFirst(sortFoldersFirst
);
1212 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1215 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1216 if (infoList
!= m_additionalInfoList
) {
1217 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1218 m_additionalInfoList
= infoList
;
1219 applyAdditionalInfoListToView();
1220 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1223 const bool previewsShown
= props
.previewsShown();
1224 if (previewsShown
!= m_container
->previewsShown()) {
1225 const int oldZoomLevel
= zoomLevel();
1227 m_container
->setPreviewsShown(previewsShown
);
1228 emit
previewsShownChanged(previewsShown
);
1230 // Changing the preview-state might result in a changed zoom-level
1231 if (oldZoomLevel
!= zoomLevel()) {
1232 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1236 m_container
->endTransaction();
1239 void DolphinView::applyAdditionalInfoListToView()
1241 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1243 QList
<QByteArray
> visibleRoles
;
1244 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1245 visibleRoles
.append("name");
1247 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1248 visibleRoles
.append(infoAccessor
.role(info
));
1251 m_container
->setVisibleRoles(visibleRoles
);
1254 void DolphinView::pasteToUrl(const KUrl
& url
)
1256 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1257 KonqOperations::doPaste(this, url
);
1260 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1262 Q_UNUSED(oldZoomLevel
);
1263 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1264 if (oldZoomLevel != newZoomLevel) {
1265 m_viewModeController->setZoomLevel(newZoomLevel);
1266 emit zoomLevelChanged(newZoomLevel);
1270 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1274 const KFileItemList items
= selectedItems();
1275 foreach (const KFileItem
&item
, items
) {
1276 urls
.append(item
.url());
1279 if (itemsExpandable()) {
1280 // TODO: Check if we still need KDirModel for this in KDE 5.0
1281 urls
= KDirModel::simplifiedUrlList(urls
);
1287 QMimeData
* DolphinView::selectionMimeData() const
1289 const KFileItemModel
* model
= fileItemModel();
1290 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1291 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1293 return model
->createMimeData(selectedIndexes
);
1296 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1298 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1299 markUrlsAsSelected(urls
);
1302 void DolphinView::updateWritableState()
1304 const bool wasFolderWritable
= m_isFolderWritable
;
1305 m_isFolderWritable
= true;
1307 const KFileItem item
= m_dirLister
->rootItem();
1308 if (!item
.isNull()) {
1309 KFileItemListProperties
capabilities(KFileItemList() << item
);
1310 m_isFolderWritable
= capabilities
.supportsWriting();
1312 if (m_isFolderWritable
!= wasFolderWritable
) {
1313 emit
writeStateChanged(m_isFolderWritable
);
1317 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1320 case SortByName
: return "name";
1321 case SortBySize
: return "size";
1322 case SortByDate
: return "date";
1323 case SortByPermissions
: return "permissions";
1324 case SortByOwner
: return "owner";
1325 case SortByGroup
: return "group";
1326 case SortByType
: return "type";
1327 case SortByDestination
: return "destination";
1328 case SortByPath
: return "path";
1332 return QByteArray();
1335 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1337 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1338 if (sortHash
.isEmpty()) {
1339 sortHash
.insert("name", SortByName
);
1340 sortHash
.insert("size", SortBySize
);
1341 sortHash
.insert("date", SortByDate
);
1342 sortHash
.insert("permissions", SortByPermissions
);
1343 sortHash
.insert("owner", SortByOwner
);
1344 sortHash
.insert("group", SortByGroup
);
1345 sortHash
.insert("type", SortByType
);
1346 sortHash
.insert("destination", SortByDestination
);
1347 sortHash
.insert("path", SortByPath
);
1349 return sortHash
.value(sortRole
);
1352 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
1354 m_currentItemUrl
= url
;
1357 #include "dolphinview.moc"