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 void DolphinView::setPreviewsShown(bool show
)
272 if (previewsShown() == show
) {
276 ViewProperties
props(url());
277 props
.setPreviewsShown(show
);
279 m_container
->setPreviewsShown(show
);
280 emit
previewsShownChanged(show
);
283 bool DolphinView::previewsShown() const
285 return m_container
->previewsShown();
288 void DolphinView::setHiddenFilesShown(bool show
)
290 if (m_dirLister
->showingDotFiles() == show
) {
294 const KFileItemList itemList
= selectedItems();
295 m_selectedUrls
.clear();
296 m_selectedUrls
= itemList
.urlList();
298 ViewProperties
props(url());
299 props
.setHiddenFilesShown(show
);
301 m_dirLister
->setShowingDotFiles(show
);
302 m_dirLister
->emitChanges();
303 emit
hiddenFilesShownChanged(show
);
306 bool DolphinView::hiddenFilesShown() const
308 return m_dirLister
->showingDotFiles();
311 void DolphinView::setGroupedSorting(bool grouped
)
313 if (grouped
== groupedSorting()) {
317 ViewProperties
props(url());
318 props
.setGroupedSorting(grouped
);
321 m_container
->controller()->model()->setGroupedSorting(grouped
);
323 emit
groupedSortingChanged(grouped
);
326 bool DolphinView::groupedSorting() const
328 return fileItemModel()->groupedSorting();
331 KFileItemList
DolphinView::items() const
333 return m_dirLister
->items();
336 KFileItemList
DolphinView::selectedItems() const
338 const KFileItemModel
* model
= fileItemModel();
339 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
340 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
342 QList
<int> sortedIndexes
= selectedIndexes
.toList();
343 qSort(sortedIndexes
);
345 KFileItemList selectedItems
;
346 QSetIterator
<int> it(selectedIndexes
);
347 while (it
.hasNext()) {
348 const int index
= it
.next();
349 selectedItems
.append(model
->fileItem(index
));
351 return selectedItems
;
354 int DolphinView::selectedItemsCount() const
356 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
357 return selectionManager
->selectedItems().count();
360 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
362 m_selectedUrls
= urls
;
365 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
367 const KItemListSelectionManager::SelectionMode mode
= enabled
368 ? KItemListSelectionManager::Select
369 : KItemListSelectionManager::Deselect
;
370 const KFileItemModel
* model
= fileItemModel();
371 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
373 for (int index
= 0; index
< model
->count(); index
++) {
374 const KFileItem item
= model
->fileItem(index
);
375 if (pattern
.exactMatch(item
.name())) {
376 // An alternative approach would be to store the matching items in a QSet<int> and
377 // select them in one go after the loop, but we'd need a new function
378 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
380 selectionManager
->setSelected(index
, 1, mode
);
385 void DolphinView::setZoomLevel(int level
)
387 const int oldZoomLevel
= zoomLevel();
388 m_container
->setZoomLevel(level
);
389 if (zoomLevel() != oldZoomLevel
) {
390 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
394 int DolphinView::zoomLevel() const
396 return m_container
->zoomLevel();
399 void DolphinView::setSorting(Sorting sorting
)
401 if (sorting
!= this->sorting()) {
402 updateSorting(sorting
);
406 DolphinView::Sorting
DolphinView::sorting() const
408 KItemModelBase
* model
= m_container
->controller()->model();
409 return sortingForSortRole(model
->sortRole());
412 void DolphinView::setSortOrder(Qt::SortOrder order
)
414 if (sortOrder() != order
) {
415 updateSortOrder(order
);
419 Qt::SortOrder
DolphinView::sortOrder() const
421 KItemModelBase
* model
= fileItemModel();
422 return model
->sortOrder();
425 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
427 if (sortFoldersFirst() != foldersFirst
) {
428 updateSortFoldersFirst(foldersFirst
);
432 bool DolphinView::sortFoldersFirst() const
434 KFileItemModel
* model
= fileItemModel();
435 return model
->sortFoldersFirst();
438 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
440 const QList
<AdditionalInfo
> previousList
= info
;
442 ViewProperties
props(url());
443 props
.setAdditionalInfoList(info
);
445 m_additionalInfoList
= info
;
446 applyAdditionalInfoListToView();
448 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
451 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
453 return m_additionalInfoList
;
456 void DolphinView::reload()
458 QByteArray viewState
;
459 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
460 saveState(saveStream
);
462 const KFileItemList itemList
= selectedItems();
463 m_selectedUrls
.clear();
464 m_selectedUrls
= itemList
.urlList();
467 loadDirectory(url(), true);
469 QDataStream
restoreStream(viewState
);
470 restoreState(restoreStream
);
473 void DolphinView::stopLoading()
478 void DolphinView::refresh()
480 const bool oldActivationState
= m_active
;
481 const int oldZoomLevel
= zoomLevel();
484 applyViewProperties();
487 setActive(oldActivationState
);
488 updateZoomLevel(oldZoomLevel
);
491 void DolphinView::setNameFilter(const QString
& nameFilter
)
493 Q_UNUSED(nameFilter
);
494 //m_viewModeController->setNameFilter(nameFilter);
497 QString
DolphinView::nameFilter() const
499 return QString(); //m_viewModeController->nameFilter();
502 void DolphinView::calculateItemCount(int& fileCount
,
504 KIO::filesize_t
& totalFileSize
) const
506 foreach (const KFileItem
& item
, m_dirLister
->items()) {
511 totalFileSize
+= item
.size();
516 QString
DolphinView::statusBarText() const
521 KIO::filesize_t totalFileSize
= 0;
523 if (hasSelection()) {
524 // give a summary of the status of the selected files
525 const KFileItemList list
= selectedItems();
526 if (list
.isEmpty()) {
527 // when an item is triggered, it is temporary selected but selectedItems()
528 // will return an empty list
532 KFileItemList::const_iterator it
= list
.begin();
533 const KFileItemList::const_iterator end
= list
.end();
535 const KFileItem
& item
= *it
;
540 totalFileSize
+= item
.size();
545 if (folderCount
+ fileCount
== 1) {
546 // if only one item is selected, show the filename
547 const QString name
= list
.first().text();
548 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
549 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
550 name
, KIO::convertSize(totalFileSize
));
552 // at least 2 items are selected
553 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
554 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
555 if ((folderCount
> 0) && (fileCount
> 0)) {
556 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
557 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
558 } else if (fileCount
> 0) {
559 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
561 Q_ASSERT(folderCount
> 0);
566 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
567 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
568 fileCount
, folderCount
,
569 totalFileSize
, true);
575 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
577 QList
<QAction
*> actions
;
579 if (items
.isEmpty()) {
580 const KFileItem item
= fileItemModel()->rootItem();
581 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
583 actions
= m_versionControlObserver
->actions(items
);
589 void DolphinView::setUrl(const KUrl
& url
)
595 emit
urlAboutToBeChanged(url
);
598 if (GeneralSettings::showToolTips()) {
599 m_toolTipManager
->hideToolTip();
602 // It is important to clear the items from the model before
603 // applying the view properties, otherwise expensive operations
604 // might be done on the existing items although they get cleared
605 // anyhow afterwards by loadDirectory().
606 fileItemModel()->clear();
607 applyViewProperties();
610 emit
urlChanged(url
);
613 void DolphinView::selectAll()
615 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
616 selectionManager
->setSelected(0, fileItemModel()->count());
619 void DolphinView::invertSelection()
621 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
622 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
625 void DolphinView::clearSelection()
627 m_container
->controller()->selectionManager()->clearSelection();
630 void DolphinView::renameSelectedItems()
632 KFileItemList items
= selectedItems();
633 const int itemCount
= items
.count();
638 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
639 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
640 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
641 m_viewAccessor.itemView()->edit(proxyIndex);
643 RenameDialog
* dialog
= new RenameDialog(this, items
);
644 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
647 dialog
->activateWindow();
650 // assure that the current index remains visible when KDirLister
651 // will notify the view about changed items
652 m_assureVisibleCurrentIndex
= true;
655 void DolphinView::trashSelectedItems()
657 const KUrl::List list
= simplifiedSelectedUrls();
658 KonqOperations::del(this, KonqOperations::TRASH
, list
);
661 void DolphinView::deleteSelectedItems()
663 const KUrl::List list
= simplifiedSelectedUrls();
664 const bool del
= KonqOperations::askDeleteConfirmation(list
,
666 KonqOperations::DEFAULT_CONFIRMATION
,
670 KIO::Job
* job
= KIO::del(list
);
671 connect(job
, SIGNAL(result(KJob
*)),
672 this, SLOT(slotDeleteFileFinished(KJob
*)));
676 void DolphinView::cutSelectedItems()
678 QMimeData
* mimeData
= selectionMimeData();
679 KonqMimeData::addIsCutSelection(mimeData
, true);
680 QApplication::clipboard()->setMimeData(mimeData
);
683 void DolphinView::copySelectedItems()
685 QMimeData
* mimeData
= selectionMimeData();
686 QApplication::clipboard()->setMimeData(mimeData
);
689 void DolphinView::paste()
694 void DolphinView::pasteIntoFolder()
696 const KFileItemList items
= selectedItems();
697 if ((items
.count() == 1) && items
.first().isDir()) {
698 pasteToUrl(items
.first().url());
702 bool DolphinView::eventFilter(QObject
* watched
, QEvent
* event
)
704 switch (event
->type()) {
705 case QEvent::FocusIn
:
706 if (watched
== m_container
) {
715 return QWidget::eventFilter(watched
, event
);
718 void DolphinView::wheelEvent(QWheelEvent
* event
)
720 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
721 const int numDegrees
= event
->delta() / 8;
722 const int numSteps
= numDegrees
/ 15;
724 setZoomLevel(zoomLevel() + numSteps
);
731 void DolphinView::activate()
736 void DolphinView::slotItemActivated(int index
)
740 const KFileItemList items
= selectedItems();
741 if (items
.isEmpty()) {
745 if (items
.count() == 1) {
746 emit
itemActivated(items
.at(0)); // caught by DolphinViewContainer or DolphinPart
748 foreach (const KFileItem
& item
, items
) {
750 emit
tabRequested(item
.url());
752 emit
itemActivated(item
);
758 void DolphinView::slotItemMiddleClicked(int index
)
760 const KFileItem item
= fileItemModel()->fileItem(index
);
761 if (item
.isDir() || isTabsForFilesEnabled()) {
762 emit
tabRequested(item
.url());
766 void DolphinView::slotItemContextMenuRequested(int index
, const QPointF
& pos
)
768 if (GeneralSettings::showToolTips()) {
769 m_toolTipManager
->hideToolTip();
771 const KFileItem item
= fileItemModel()->fileItem(index
);
772 emit
requestContextMenu(pos
.toPoint(), item
, url(), QList
<QAction
*>());
775 void DolphinView::slotViewContextMenuRequested(const QPointF
& pos
)
777 if (GeneralSettings::showToolTips()) {
778 m_toolTipManager
->hideToolTip();
780 emit
requestContextMenu(pos
.toPoint(), KFileItem(), url(), QList
<QAction
*>());
783 void DolphinView::slotHeaderContextMenuRequested(const QPointF
& pos
)
785 QWeakPointer
<KMenu
> menu
= new KMenu(QApplication::activeWindow());
787 KItemListView
* view
= m_container
->controller()->view();
788 const QSet
<QByteArray
> visibleRolesSet
= view
->visibleRoles().toSet();
790 // Add all roles to the menu that can be shown or hidden by the user
791 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
792 const QList
<DolphinView::AdditionalInfo
> keys
= infoAccessor
.keys();
793 foreach (const DolphinView::AdditionalInfo info
, keys
) {
794 const QByteArray
& role
= infoAccessor
.role(info
);
795 if (role
!= "name") {
796 const QString text
= fileItemModel()->roleDescription(role
);
798 QAction
* action
= menu
.data()->addAction(text
);
799 action
->setCheckable(true);
800 action
->setChecked(visibleRolesSet
.contains(role
));
801 action
->setData(info
);
805 QAction
* action
= menu
.data()->exec(pos
.toPoint());
807 // Show or hide the selected role
808 const DolphinView::AdditionalInfo info
=
809 static_cast<DolphinView::AdditionalInfo
>(action
->data().toInt());
811 ViewProperties
props(url());
812 QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
814 const QByteArray selectedRole
= infoAccessor
.role(info
);
815 QList
<QByteArray
> visibleRoles
= view
->visibleRoles();
817 if (action
->isChecked()) {
818 const int index
= keys
.indexOf(info
) + 1;
819 visibleRoles
.insert(index
, selectedRole
);
820 infoList
.insert(index
, info
);
822 visibleRoles
.removeOne(selectedRole
);
823 infoList
.removeOne(info
);
826 view
->setVisibleRoles(visibleRoles
);
827 props
.setAdditionalInfoList(infoList
);
833 void DolphinView::slotItemExpansionToggleClicked(int index
)
835 // TODO: When doing a model->setExpanded(false) it should
836 // be checked here whether the current index is part of the
837 // closed sub-tree. If this is the case, the current index
838 // should be adjusted to the parent index.
839 KFileItemModel
* model
= fileItemModel();
840 const bool expanded
= model
->isExpanded(index
);
841 model
->setExpanded(index
, !expanded
);
844 void DolphinView::slotItemHovered(int index
)
846 const KFileItem item
= fileItemModel()->fileItem(index
);
848 if (GeneralSettings::showToolTips()) {
849 QRectF itemRect
= m_container
->controller()->view()->itemBoundingRect(index
);
850 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
851 itemRect
.moveTo(pos
);
853 m_toolTipManager
->showToolTip(item
, itemRect
);
856 emit
requestItemInfo(item
);
859 void DolphinView::slotItemUnhovered(int index
)
862 if (GeneralSettings::showToolTips()) {
863 m_toolTipManager
->hideToolTip();
865 emit
requestItemInfo(KFileItem());
868 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
870 const KFileItem destItem
= fileItemModel()->fileItem(index
);
872 QDropEvent
dropEvent(event
->pos().toPoint(),
873 event
->possibleActions(),
878 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
881 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
884 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
887 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
888 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
890 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
891 m_versionControlObserver
->setModel(fileItemModel
);
894 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
896 const int currentCount
= current
.count();
897 const int previousCount
= previous
.count();
898 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
899 (currentCount
> 0 && previousCount
== 0);
901 // If nothing has been selected before and something got selected (or if something
902 // was selected before and now nothing is selected) the selectionChangedSignal must
903 // be emitted asynchronously as fast as possible to update the edit-actions.
904 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
905 m_selectionChangedTimer
->start();
908 void DolphinView::emitSelectionChangedSignal()
910 m_selectionChangedTimer
->stop();
911 emit
selectionChanged(selectedItems());
914 void DolphinView::dropUrls(const KFileItem
& destItem
,
915 const KUrl
& destPath
,
920 markPastedUrlsAsSelected(event
->mimeData());
921 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
924 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
926 ViewProperties
props(url());
927 props
.setSorting(sorting
);
929 KItemModelBase
* model
= m_container
->controller()->model();
930 model
->setSortRole(sortRoleForSorting(sorting
));
932 emit
sortingChanged(sorting
);
935 void DolphinView::updateSortOrder(Qt::SortOrder order
)
937 ViewProperties
props(url());
938 props
.setSortOrder(order
);
940 KItemModelBase
* model
= fileItemModel();
941 model
->setSortOrder(order
);
943 emit
sortOrderChanged(order
);
946 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
948 ViewProperties
props(url());
949 props
.setSortFoldersFirst(foldersFirst
);
951 KFileItemModel
* model
= fileItemModel();
952 model
->setSortFoldersFirst(foldersFirst
);
954 emit
sortFoldersFirstChanged(foldersFirst
);
957 QPair
<bool, QString
> DolphinView::pasteInfo() const
959 return KonqOperations::pasteInfo(url());
962 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
964 m_tabsForFiles
= tabsForFiles
;
967 bool DolphinView::isTabsForFilesEnabled() const
969 return m_tabsForFiles
;
972 bool DolphinView::itemsExpandable() const
974 return m_mode
== DetailsView
;
977 void DolphinView::restoreState(QDataStream
& stream
)
979 // Restore the current item that had the keyboard focus
980 stream
>> m_currentItemUrl
;
982 // Restore the view position
983 stream
>> m_restoredContentsPosition
;
985 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
988 fileItemModel()->restoreExpandedUrls(urls
);
991 void DolphinView::saveState(QDataStream
& stream
)
993 // Save the current item that has the keyboard focus
994 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
995 if (currentIndex
!= -1) {
996 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
997 KUrl currentItemUrl
= item
.url();
998 stream
<< currentItemUrl
;
1003 // Save view position
1004 const qreal x
= m_container
->horizontalScrollBar()->value();
1005 const qreal y
= m_container
->verticalScrollBar()->value();
1006 stream
<< QPoint(x
, y
);
1008 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
1009 stream
<< fileItemModel()->expandedUrls();
1012 bool DolphinView::hasSelection() const
1014 return m_container
->controller()->selectionManager()->hasSelection();
1017 KFileItem
DolphinView::rootItem() const
1019 return m_dirLister
->rootItem();
1022 void DolphinView::observeCreatedItem(const KUrl
& url
)
1024 m_createdItemUrl
= url
;
1025 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
1026 // this, SLOT(selectAndScrollToCreatedItem()));
1029 void DolphinView::selectAndScrollToCreatedItem()
1031 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
1032 if (dirIndex.isValid()) {
1033 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
1034 QAbstractItemView* view = m_viewAccessor.itemView();
1036 view->setCurrentIndex(proxyIndex);
1040 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
1041 this, SLOT(selectAndScrollToCreatedItem()));*/
1042 m_createdItemUrl
= KUrl();
1045 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
1047 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
1048 emit
redirection(oldUrl
, newUrl
);
1049 m_url
= newUrl
; // #186947
1053 void DolphinView::updateViewState()
1055 if (m_currentItemUrl
!= KUrl()) {
1056 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1057 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1058 if (currentIndex
!= -1) {
1059 selectionManager
->setCurrentItem(currentIndex
);
1061 selectionManager
->setCurrentItem(0);
1063 m_currentItemUrl
= KUrl();
1066 if (!m_restoredContentsPosition
.isNull()) {
1067 const int x
= m_restoredContentsPosition
.x();
1068 const int y
= m_restoredContentsPosition
.y();
1069 m_restoredContentsPosition
= QPoint();
1071 m_container
->horizontalScrollBar()->setValue(x
);
1072 m_container
->verticalScrollBar()->setValue(y
);
1075 if (!m_selectedUrls
.isEmpty()) {
1076 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1077 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1078 const KFileItemModel
* model
= fileItemModel();
1080 foreach (const KUrl
& url
, m_selectedUrls
) {
1081 const int index
= model
->index(url
);
1083 selectedItems
.insert(index
);
1087 selectionManager
->setSelectedItems(selectedItems
);
1088 m_selectedUrls
.clear();
1092 void DolphinView::showHoverInformation(const KFileItem
& item
)
1094 emit
requestItemInfo(item
);
1097 void DolphinView::clearHoverInformation()
1099 emit
requestItemInfo(KFileItem());
1102 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1104 if (job
->error() == 0) {
1105 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1106 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1107 emit
errorMessage(job
->errorString());
1111 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1113 // Disable the writestate temporary until it can be determined in a fast way
1114 // in DolphinView::slotLoadingCompleted()
1115 if (m_isFolderWritable
) {
1116 m_isFolderWritable
= false;
1117 emit
writeStateChanged(m_isFolderWritable
);
1120 emit
startedPathLoading(url
);
1123 void DolphinView::slotLoadingCompleted()
1125 // Update the view-state. This has to be done using a Qt::QueuedConnection
1126 // because the view might not be in its final state yet (the view also
1127 // listens to the completed()-signal from KDirLister and the order of
1128 // of slots is undefined).
1129 QTimer::singleShot(0, this, SLOT(updateViewState()));
1131 emit
finishedPathLoading(url());
1133 updateWritableState();
1136 void DolphinView::slotRefreshItems()
1138 if (m_assureVisibleCurrentIndex
) {
1139 m_assureVisibleCurrentIndex
= false;
1140 //QAbstractItemView* view = m_viewAccessor.itemView();
1142 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1147 KFileItemModel
* DolphinView::fileItemModel() const
1149 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1152 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1154 if (!url
.isValid()) {
1155 const QString
location(url
.pathOrUrl());
1156 if (location
.isEmpty()) {
1157 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1159 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1164 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1167 void DolphinView::applyViewProperties()
1169 m_container
->beginTransaction();
1171 const ViewProperties
props(url());
1173 const Mode mode
= props
.viewMode();
1174 if (m_mode
!= mode
) {
1175 const Mode previousMode
= m_mode
;
1178 // Changing the mode might result in changing
1179 // the zoom level. Remember the old zoom level so
1180 // that zoomLevelChanged() can get emitted.
1181 const int oldZoomLevel
= m_container
->zoomLevel();
1184 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1185 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1186 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1187 default: Q_ASSERT(false); break;
1190 emit
modeChanged(m_mode
, previousMode
);
1192 if (m_container
->zoomLevel() != oldZoomLevel
) {
1193 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1197 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1198 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1199 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1200 m_dirLister
->emitChanges();
1201 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1204 KFileItemModel
* model
= fileItemModel();
1206 const bool groupedSorting
= props
.groupedSorting();
1207 if (groupedSorting
!= model
->groupedSorting()) {
1208 model
->setGroupedSorting(groupedSorting
);
1209 emit
groupedSortingChanged(groupedSorting
);
1212 const DolphinView::Sorting sorting
= props
.sorting();
1213 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1214 if (newSortRole
!= model
->sortRole()) {
1215 model
->setSortRole(newSortRole
);
1216 emit
sortingChanged(sorting
);
1219 const Qt::SortOrder sortOrder
= props
.sortOrder();
1220 if (sortOrder
!= model
->sortOrder()) {
1221 model
->setSortOrder(sortOrder
);
1222 emit
sortOrderChanged(sortOrder
);
1225 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1226 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1227 model
->setSortFoldersFirst(sortFoldersFirst
);
1228 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1231 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1232 if (infoList
!= m_additionalInfoList
) {
1233 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1234 m_additionalInfoList
= infoList
;
1235 applyAdditionalInfoListToView();
1236 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1239 const bool previewsShown
= props
.previewsShown();
1240 if (previewsShown
!= m_container
->previewsShown()) {
1241 const int oldZoomLevel
= zoomLevel();
1243 m_container
->setPreviewsShown(previewsShown
);
1244 emit
previewsShownChanged(previewsShown
);
1246 // Changing the preview-state might result in a changed zoom-level
1247 if (oldZoomLevel
!= zoomLevel()) {
1248 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1252 m_container
->endTransaction();
1255 void DolphinView::applyAdditionalInfoListToView()
1257 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1259 QList
<QByteArray
> visibleRoles
;
1260 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1261 visibleRoles
.append("name");
1263 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1264 visibleRoles
.append(infoAccessor
.role(info
));
1267 m_container
->setVisibleRoles(visibleRoles
);
1270 void DolphinView::pasteToUrl(const KUrl
& url
)
1272 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1273 KonqOperations::doPaste(this, url
);
1276 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1278 Q_UNUSED(oldZoomLevel
);
1279 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1280 if (oldZoomLevel != newZoomLevel) {
1281 m_viewModeController->setZoomLevel(newZoomLevel);
1282 emit zoomLevelChanged(newZoomLevel);
1286 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1290 const KFileItemList items
= selectedItems();
1291 foreach (const KFileItem
&item
, items
) {
1292 urls
.append(item
.url());
1295 if (itemsExpandable()) {
1296 // TODO: Check if we still need KDirModel for this in KDE 5.0
1297 urls
= KDirModel::simplifiedUrlList(urls
);
1303 QMimeData
* DolphinView::selectionMimeData() const
1305 const KFileItemModel
* model
= fileItemModel();
1306 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1307 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1309 return model
->createMimeData(selectedIndexes
);
1312 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1314 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1315 markUrlsAsSelected(urls
);
1318 void DolphinView::updateWritableState()
1320 const bool wasFolderWritable
= m_isFolderWritable
;
1321 m_isFolderWritable
= true;
1323 const KFileItem item
= m_dirLister
->rootItem();
1324 if (!item
.isNull()) {
1325 KFileItemListProperties
capabilities(KFileItemList() << item
);
1326 m_isFolderWritable
= capabilities
.supportsWriting();
1328 if (m_isFolderWritable
!= wasFolderWritable
) {
1329 emit
writeStateChanged(m_isFolderWritable
);
1333 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1336 case SortByName
: return "name";
1337 case SortBySize
: return "size";
1338 case SortByDate
: return "date";
1339 case SortByPermissions
: return "permissions";
1340 case SortByOwner
: return "owner";
1341 case SortByGroup
: return "group";
1342 case SortByType
: return "type";
1343 case SortByDestination
: return "destination";
1344 case SortByPath
: return "path";
1348 return QByteArray();
1351 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1353 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1354 if (sortHash
.isEmpty()) {
1355 sortHash
.insert("name", SortByName
);
1356 sortHash
.insert("size", SortBySize
);
1357 sortHash
.insert("date", SortByDate
);
1358 sortHash
.insert("permissions", SortByPermissions
);
1359 sortHash
.insert("owner", SortByOwner
);
1360 sortHash
.insert("group", SortByGroup
);
1361 sortHash
.insert("type", SortByType
);
1362 sortHash
.insert("destination", SortByDestination
);
1363 sortHash
.insert("path", SortByPath
);
1365 return sortHash
.value(sortRole
);
1368 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
1370 m_currentItemUrl
= url
;
1373 #include "dolphinview.moc"