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),
93 m_currentItemIndex(-1),
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(completed()), this, SLOT(slotDirListerCompleted()));
155 connect(m_dirLister
, SIGNAL(refreshItems(QList
<QPair
<KFileItem
,KFileItem
> >)),
156 this, SLOT(slotRefreshItems()));
158 connect(m_dirLister
, SIGNAL(clear()), this, SIGNAL(itemCountChanged()));
159 connect(m_dirLister
, SIGNAL(newItems(KFileItemList
)), this, SIGNAL(itemCountChanged()));
160 connect(m_dirLister
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
161 connect(m_dirLister
, SIGNAL(errorMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
162 connect(m_dirLister
, SIGNAL(percent(int)), this, SIGNAL(pathLoadingProgress(int)));
163 connect(m_dirLister
, SIGNAL(urlIsFileError(KUrl
)), this, SIGNAL(urlIsFileError(KUrl
)));
164 connect(m_dirLister
, SIGNAL(itemsDeleted(KFileItemList
)), this, SIGNAL(itemCountChanged()));
166 m_container
= new DolphinItemListContainer(m_dirLister
, this);
167 QHash
<QByteArray
, int> visibleRoles
;
168 visibleRoles
.insert("name", 0);
169 m_container
->setVisibleRoles(visibleRoles
);
171 KItemListController
* controller
= m_container
->controller();
172 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
173 connect(controller
, SIGNAL(itemActivated(int)),
174 this, SLOT(slotItemActivated(int)));
175 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
176 connect(controller
, SIGNAL(contextMenuRequested(int,QPointF
)), this, SLOT(slotContextMenuRequested(int,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 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
184 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
185 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
187 m_toolTipManager
= new ToolTipManager(this);
189 m_versionControlObserver
= new VersionControlObserver(this);
190 m_versionControlObserver
->setModel(fileItemModel());
191 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
192 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
193 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
195 applyViewProperties();
196 m_topLayout
->addWidget(m_container
);
201 DolphinView::~DolphinView()
205 KUrl
DolphinView::url() const
210 void DolphinView::setActive(bool active
)
212 if (active
== m_active
) {
218 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
223 /*QAbstractItemView* view = m_viewAccessor.itemView();
224 QWidget* viewport = view ? view->viewport() : 0;
227 palette.setColor(viewport->backgroundRole(), color);
228 viewport->setPalette(palette);
238 emit
writeStateChanged(m_isFolderWritable
);
241 //m_viewModeController->indicateActivationChange(active);
244 bool DolphinView::isActive() const
249 void DolphinView::setMode(Mode mode
)
251 if (mode
!= m_mode
) {
252 ViewProperties
props(url());
253 props
.setViewMode(mode
);
256 applyViewProperties();
260 DolphinView::Mode
DolphinView::mode() const
265 bool DolphinView::previewsShown() const
267 return m_container
->previewsShown();
270 bool DolphinView::hiddenFilesShown() const
272 return m_dirLister
->showingDotFiles();
275 bool DolphinView::categorizedSorting() const
277 return false; //m_storedCategorizedSorting;
280 KFileItemList
DolphinView::items() const
282 return m_dirLister
->items();
285 KFileItemList
DolphinView::selectedItems() const
287 const KFileItemModel
* model
= fileItemModel();
288 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
289 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
291 KFileItemList selectedItems
;
292 QSetIterator
<int> it(selectedIndexes
);
293 while (it
.hasNext()) {
294 const int index
= it
.next();
295 selectedItems
.append(model
->fileItem(index
));
297 return selectedItems
;
300 int DolphinView::selectedItemsCount() const
302 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
303 return selectionManager
->selectedItems().count();
306 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
308 foreach (const KUrl
& url
, urls
) {
309 KFileItem
item(KFileItem::Unknown
, KFileItem::Unknown
, url
);
310 m_selectedItems
.append(item
);
314 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
316 const KItemListSelectionManager::SelectionMode mode
= enabled
317 ? KItemListSelectionManager::Select
318 : KItemListSelectionManager::Deselect
;
319 const KFileItemModel
* model
= fileItemModel();
320 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
322 for (int index
= 0; index
< model
->count(); index
++) {
323 const KFileItem item
= model
->fileItem(index
);
324 if (pattern
.exactMatch(item
.name())) {
325 // An alternative approach would be to store the matching items in a QSet<int> and
326 // select them in one go after the loop, but we'd need a new function
327 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
329 selectionManager
->setSelected(index
, 1, mode
);
334 void DolphinView::setZoomLevel(int level
)
336 const int oldZoomLevel
= zoomLevel();
337 m_container
->setZoomLevel(level
);
338 if (zoomLevel() != oldZoomLevel
) {
339 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
343 int DolphinView::zoomLevel() const
345 return m_container
->zoomLevel();
348 void DolphinView::setSorting(Sorting sorting
)
350 if (sorting
!= this->sorting()) {
351 updateSorting(sorting
);
355 DolphinView::Sorting
DolphinView::sorting() const
357 return DolphinView::SortByName
;
358 //return m_viewAccessor.proxyModel()->sorting();
361 void DolphinView::setSortOrder(Qt::SortOrder order
)
363 if (sortOrder() != order
) {
364 updateSortOrder(order
);
368 Qt::SortOrder
DolphinView::sortOrder() const
370 return Qt::AscendingOrder
; // m_viewAccessor.proxyModel()->sortOrder();
373 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
375 if (sortFoldersFirst() != foldersFirst
) {
376 updateSortFoldersFirst(foldersFirst
);
380 bool DolphinView::sortFoldersFirst() const
382 return true; // m_viewAccessor.proxyModel()->sortFoldersFirst();
385 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
387 const QList
<AdditionalInfo
> previousList
= info
;
389 ViewProperties
props(url());
390 props
.setAdditionalInfoList(info
);
392 m_additionalInfoList
= info
;
393 applyAdditionalInfoListToView();
395 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
398 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
400 return m_additionalInfoList
;
403 void DolphinView::reload()
405 QByteArray viewState
;
406 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
407 saveState(saveStream
);
408 m_selectedItems
= selectedItems();
411 loadDirectory(url(), true);
413 QDataStream
restoreStream(viewState
);
414 restoreState(restoreStream
);
417 void DolphinView::stopLoading()
422 void DolphinView::refresh()
424 const bool oldActivationState
= m_active
;
425 const int oldZoomLevel
= zoomLevel();
428 applyViewProperties();
431 setActive(oldActivationState
);
432 updateZoomLevel(oldZoomLevel
);
435 void DolphinView::setNameFilter(const QString
& nameFilter
)
437 Q_UNUSED(nameFilter
);
438 //m_viewModeController->setNameFilter(nameFilter);
441 QString
DolphinView::nameFilter() const
443 return QString(); //m_viewModeController->nameFilter();
446 void DolphinView::calculateItemCount(int& fileCount
,
448 KIO::filesize_t
& totalFileSize
) const
450 foreach (const KFileItem
& item
, m_dirLister
->items()) {
455 totalFileSize
+= item
.size();
460 QString
DolphinView::statusBarText() const
465 KIO::filesize_t totalFileSize
= 0;
467 if (hasSelection()) {
468 // give a summary of the status of the selected files
469 const KFileItemList list
= selectedItems();
470 if (list
.isEmpty()) {
471 // when an item is triggered, it is temporary selected but selectedItems()
472 // will return an empty list
476 KFileItemList::const_iterator it
= list
.begin();
477 const KFileItemList::const_iterator end
= list
.end();
479 const KFileItem
& item
= *it
;
484 totalFileSize
+= item
.size();
489 if (folderCount
+ fileCount
== 1) {
490 // if only one item is selected, show the filename
491 const QString name
= list
.first().text();
492 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
493 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
494 name
, KIO::convertSize(totalFileSize
));
496 // at least 2 items are selected
497 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
498 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
499 if ((folderCount
> 0) && (fileCount
> 0)) {
500 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
501 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
502 } else if (fileCount
> 0) {
503 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
505 Q_ASSERT(folderCount
> 0);
510 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
511 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
512 fileCount
, folderCount
,
513 totalFileSize
, true);
519 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
522 return QList
<QAction
*>(); //m_dolphinViewController->versionControlActions(items);
525 void DolphinView::setUrl(const KUrl
& url
)
531 emit
urlAboutToBeChanged(url
);
534 if (GeneralSettings::showToolTips()) {
535 m_toolTipManager
->hideToolTip();
538 // It is important to clear the items from the model before
539 // applying the view properties, otherwise expensive operations
540 // might be done on the existing items although they get cleared
541 // anyhow afterwards by loadDirectory().
542 fileItemModel()->clear();
543 applyViewProperties();
546 emit
urlChanged(url
);
549 void DolphinView::selectAll()
551 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
552 selectionManager
->setSelected(0, fileItemModel()->count());
555 void DolphinView::invertSelection()
557 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
558 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
561 void DolphinView::clearSelection()
563 m_container
->controller()->selectionManager()->clearSelection();
566 void DolphinView::renameSelectedItems()
568 KFileItemList items
= selectedItems();
569 const int itemCount
= items
.count();
574 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
575 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
576 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
577 m_viewAccessor.itemView()->edit(proxyIndex);
579 RenameDialog
* dialog
= new RenameDialog(this, items
);
580 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
583 dialog
->activateWindow();
586 // assure that the current index remains visible when KDirLister
587 // will notify the view about changed items
588 m_assureVisibleCurrentIndex
= true;
591 void DolphinView::trashSelectedItems()
593 const KUrl::List list
= simplifiedSelectedUrls();
594 KonqOperations::del(this, KonqOperations::TRASH
, list
);
597 void DolphinView::deleteSelectedItems()
599 const KUrl::List list
= simplifiedSelectedUrls();
600 const bool del
= KonqOperations::askDeleteConfirmation(list
,
602 KonqOperations::DEFAULT_CONFIRMATION
,
606 KIO::Job
* job
= KIO::del(list
);
607 connect(job
, SIGNAL(result(KJob
*)),
608 this, SLOT(slotDeleteFileFinished(KJob
*)));
612 void DolphinView::cutSelectedItems()
614 QMimeData
* mimeData
= selectionMimeData();
615 KonqMimeData::addIsCutSelection(mimeData
, true);
616 QApplication::clipboard()->setMimeData(mimeData
);
619 void DolphinView::copySelectedItems()
621 QMimeData
* mimeData
= selectionMimeData();
622 QApplication::clipboard()->setMimeData(mimeData
);
625 void DolphinView::paste()
630 void DolphinView::pasteIntoFolder()
632 const KFileItemList items
= selectedItems();
633 if ((items
.count() == 1) && items
.first().isDir()) {
634 pasteToUrl(items
.first().url());
638 void DolphinView::setPreviewsShown(bool show
)
640 if (previewsShown() == show
) {
644 ViewProperties
props(url());
645 props
.setPreviewsShown(show
);
647 m_container
->setPreviewsShown(show
);
648 emit
previewsShownChanged(show
);
651 void DolphinView::setHiddenFilesShown(bool show
)
653 if (m_dirLister
->showingDotFiles() == show
) {
657 m_selectedItems
= selectedItems();
659 ViewProperties
props(url());
660 props
.setHiddenFilesShown(show
);
662 m_dirLister
->setShowingDotFiles(show
);
663 m_dirLister
->emitChanges();
664 emit
hiddenFilesShownChanged(show
);
667 void DolphinView::setCategorizedSorting(bool categorized
)
669 if (categorized
== categorizedSorting()) {
673 ViewProperties
props(url());
674 props
.setCategorizedSorting(categorized
);
677 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
679 emit
categorizedSortingChanged(categorized
);
682 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
684 QWidget::mouseReleaseEvent(event
);
688 void DolphinView::contextMenuEvent(QContextMenuEvent
* event
)
692 const QPoint pos
= m_container
->mapFromGlobal(QCursor::pos());
693 const KItemListView
* view
= m_container
->controller()->view();
694 if (view
->itemAt(pos
) < 0) {
695 // Only open the context-menu if the cursor is above the viewport
696 // (the context-menu for items is handled in slotContextMenuRequested())
697 requestContextMenu(KFileItem(), url(), QList
<QAction
*>());
701 void DolphinView::wheelEvent(QWheelEvent
* event
)
703 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
704 const int numDegrees
= event
->delta() / 8;
705 const int numSteps
= numDegrees
/ 15;
707 setZoomLevel(zoomLevel() + numSteps
);
712 void DolphinView::activate()
717 void DolphinView::slotItemActivated(int index
)
721 const KFileItemList items
= selectedItems();
722 if (items
.isEmpty()) {
726 if (items
.count() == 1) {
727 emit
itemActivated(items
.at(0)); // caught by DolphinViewContainer or DolphinPart
729 foreach (const KFileItem
& item
, items
) {
731 emit
tabRequested(item
.url());
733 emit
itemActivated(item
);
739 void DolphinView::slotItemMiddleClicked(int index
)
741 const KFileItem item
= fileItemModel()->fileItem(index
);
742 if (item
.isDir() || isTabsForFilesEnabled()) {
743 emit
tabRequested(item
.url());
747 void DolphinView::slotContextMenuRequested(int index
, const QPointF
& pos
)
750 if (GeneralSettings::showToolTips()) {
751 m_toolTipManager
->hideToolTip();
753 const KFileItem item
= fileItemModel()->fileItem(index
);
754 emit
requestContextMenu(item
, url(), QList
<QAction
*>());
757 void DolphinView::slotItemExpansionToggleClicked(int index
)
759 // TODO: When doing a model->setExpanded(false) it should
760 // be checked here whether the current index is part of the
761 // closed sub-tree. If this is the case, the current index
762 // should be adjusted to the parent index.
763 KFileItemModel
* model
= fileItemModel();
764 const bool expanded
= model
->isExpanded(index
);
765 model
->setExpanded(index
, !expanded
);
768 void DolphinView::slotItemHovered(int index
)
770 const KFileItem item
= fileItemModel()->fileItem(index
);
772 if (GeneralSettings::showToolTips()) {
773 QRectF itemRect
= m_container
->controller()->view()->itemBoundingRect(index
);
774 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
775 itemRect
.moveTo(pos
);
777 m_toolTipManager
->showToolTip(item
, itemRect
);
780 emit
requestItemInfo(item
);
783 void DolphinView::slotItemUnhovered(int index
)
786 if (GeneralSettings::showToolTips()) {
787 m_toolTipManager
->hideToolTip();
789 emit
requestItemInfo(KFileItem());
792 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
794 const KFileItem destItem
= fileItemModel()->fileItem(index
);
796 QDropEvent
dropEvent(event
->pos().toPoint(),
797 event
->possibleActions(),
802 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
805 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
808 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
810 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
811 m_versionControlObserver
->setModel(fileItemModel
);
814 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
816 const int currentCount
= current
.count();
817 const int previousCount
= previous
.count();
818 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
819 (currentCount
> 0 && previousCount
== 0);
821 // If nothing has been selected before and something got selected (or if something
822 // was selected before and now nothing is selected) the selectionChangedSignal must
823 // be emitted asynchronously as fast as possible to update the edit-actions.
824 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
825 m_selectionChangedTimer
->start();
828 void DolphinView::emitSelectionChangedSignal()
830 m_selectionChangedTimer
->stop();
831 emit
selectionChanged(selectedItems());
834 void DolphinView::openContextMenu(const QPoint
& pos
,
835 const QList
<QAction
*>& customActions
)
838 const int index
= m_container
->controller()->view()->itemAt(pos
);
840 item
= fileItemModel()->fileItem(index
);
843 emit
requestContextMenu(item
, url(), customActions
);
846 void DolphinView::dropUrls(const KFileItem
& destItem
,
847 const KUrl
& destPath
,
852 markPastedUrlsAsSelected(event
->mimeData());
853 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
856 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
858 ViewProperties
props(url());
859 props
.setSorting(sorting
);
861 KItemModelBase
* model
= m_container
->controller()->model();
862 model
->setSortRole(sortRoleForSorting(sorting
));
864 emit
sortingChanged(sorting
);
867 void DolphinView::updateSortOrder(Qt::SortOrder order
)
869 ViewProperties
props(url());
870 props
.setSortOrder(order
);
872 //m_viewAccessor.proxyModel()->setSortOrder(order);
874 emit
sortOrderChanged(order
);
877 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
879 ViewProperties
props(url());
880 props
.setSortFoldersFirst(foldersFirst
);
882 //m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
884 emit
sortFoldersFirstChanged(foldersFirst
);
887 QPair
<bool, QString
> DolphinView::pasteInfo() const
889 return KonqOperations::pasteInfo(url());
892 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
894 m_tabsForFiles
= tabsForFiles
;
897 bool DolphinView::isTabsForFilesEnabled() const
899 return m_tabsForFiles
;
902 bool DolphinView::itemsExpandable() const
904 return m_mode
== DetailsView
;
907 void DolphinView::restoreState(QDataStream
& stream
)
909 // Restore the current item that had the keyboard focus
910 stream
>> m_currentItemIndex
;
912 // Restore the view position
913 stream
>> m_restoredContentsPosition
;
915 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
916 QSet
<KUrl
> urlsToExpand
;
917 stream
>> urlsToExpand
;
918 /*const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
920 m_expanderActive = true;
921 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
924 m_expanderActive = false;
928 void DolphinView::saveState(QDataStream
& stream
)
930 // Save the current item that has the keyboard focus
931 stream
<< m_container
->controller()->selectionManager()->currentItem();
933 // Save view position
934 const qreal x
= m_container
->horizontalScrollBar()->value();
935 const qreal y
= m_container
->verticalScrollBar()->value();
936 stream
<< QPoint(x
, y
);
938 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
939 //stream << m_viewAccessor.expandedUrls();
942 bool DolphinView::hasSelection() const
944 return m_container
->controller()->selectionManager()->hasSelection();
947 KFileItem
DolphinView::rootItem() const
949 return m_dirLister
->rootItem();
952 void DolphinView::observeCreatedItem(const KUrl
& url
)
954 m_createdItemUrl
= url
;
955 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
956 // this, SLOT(selectAndScrollToCreatedItem()));
959 void DolphinView::selectAndScrollToCreatedItem()
961 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
962 if (dirIndex.isValid()) {
963 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
964 QAbstractItemView* view = m_viewAccessor.itemView();
966 view->setCurrentIndex(proxyIndex);
970 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
971 this, SLOT(selectAndScrollToCreatedItem()));*/
972 m_createdItemUrl
= KUrl();
975 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
977 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
978 emit
redirection(oldUrl
, newUrl
);
979 m_url
= newUrl
; // #186947
983 void DolphinView::updateViewState()
985 if (m_currentItemIndex
>= 0) {
986 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
987 selectionManager
->setCurrentItem(m_currentItemIndex
);
988 m_currentItemIndex
=-1;
991 if (!m_restoredContentsPosition
.isNull()) {
992 const int x
= m_restoredContentsPosition
.x();
993 const int y
= m_restoredContentsPosition
.y();
994 m_restoredContentsPosition
= QPoint();
996 m_container
->horizontalScrollBar()->setValue(x
);
997 m_container
->verticalScrollBar()->setValue(y
);
1000 if (!m_selectedItems
.isEmpty()) {
1001 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1002 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1003 const KFileItemModel
* model
= fileItemModel();
1005 foreach (const KFileItem
& selectedItem
, m_selectedItems
) {
1006 const int index
= model
->index(selectedItem
);
1008 selectedItems
.insert(index
);
1012 selectionManager
->setSelectedItems(selectedItems
);
1013 m_selectedItems
.clear();
1017 void DolphinView::showHoverInformation(const KFileItem
& item
)
1019 emit
requestItemInfo(item
);
1022 void DolphinView::clearHoverInformation()
1024 emit
requestItemInfo(KFileItem());
1027 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1029 if (job
->error() == 0) {
1030 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1031 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1032 emit
errorMessage(job
->errorString());
1036 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1038 // Disable the writestate temporary until it can be determined in a fast way
1039 // in DolphinView::slotDirListerCompleted()
1040 if (m_isFolderWritable
) {
1041 m_isFolderWritable
= false;
1042 emit
writeStateChanged(m_isFolderWritable
);
1045 emit
startedPathLoading(url
);
1048 void DolphinView::slotDirListerCompleted()
1050 // Update the view-state. This has to be done using a Qt::QueuedConnection
1051 // because the view might not be in its final state yet (the view also
1052 // listens to the completed()-signal from KDirLister and the order of
1053 // of slots is undefined).
1054 QTimer::singleShot(0, this, SLOT(updateViewState()));
1056 emit
finishedPathLoading(url());
1058 updateWritableState();
1061 void DolphinView::slotRefreshItems()
1063 if (m_assureVisibleCurrentIndex
) {
1064 m_assureVisibleCurrentIndex
= false;
1065 //QAbstractItemView* view = m_viewAccessor.itemView();
1067 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1072 KFileItemModel
* DolphinView::fileItemModel() const
1074 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1077 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1079 if (!url
.isValid()) {
1080 const QString
location(url
.pathOrUrl());
1081 if (location
.isEmpty()) {
1082 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1084 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1089 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1092 void DolphinView::applyViewProperties()
1094 m_container
->beginTransaction();
1096 const ViewProperties
props(url());
1098 const Mode mode
= props
.viewMode();
1099 if (m_mode
!= mode
) {
1100 const Mode previousMode
= m_mode
;
1103 // Changing the mode might result in changing
1104 // the zoom level. Remember the old zoom level so
1105 // that zoomLevelChanged() can get emitted.
1106 const int oldZoomLevel
= m_container
->zoomLevel();
1109 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1110 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1111 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1112 default: Q_ASSERT(false); break;
1115 emit
modeChanged(m_mode
, previousMode
);
1117 if (m_container
->zoomLevel() != oldZoomLevel
) {
1118 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1122 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1123 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1124 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1125 m_dirLister
->emitChanges();
1126 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1129 /* m_storedCategorizedSorting = props.categorizedSorting();
1130 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1131 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1132 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1133 emit categorizedSortingChanged();
1136 const DolphinView::Sorting sorting
= props
.sorting();
1137 KItemModelBase
* model
= m_container
->controller()->model();
1138 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1139 if (newSortRole
!= model
->sortRole()) {
1140 model
->setSortRole(newSortRole
);
1141 emit
sortingChanged(sorting
);
1144 const Qt::SortOrder sortOrder = props.sortOrder();
1145 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1146 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1147 emit sortOrderChanged(sortOrder);
1150 const bool sortFoldersFirst = props.sortFoldersFirst();
1151 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1152 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1153 emit sortFoldersFirstChanged(sortFoldersFirst);
1156 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1157 if (infoList
!= m_additionalInfoList
) {
1158 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1159 m_additionalInfoList
= infoList
;
1160 applyAdditionalInfoListToView();
1161 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1164 const bool previewsShown
= props
.previewsShown();
1165 if (previewsShown
!= m_container
->previewsShown()) {
1166 const int oldZoomLevel
= zoomLevel();
1168 m_container
->setPreviewsShown(previewsShown
);
1169 emit
previewsShownChanged(previewsShown
);
1171 // Changing the preview-state might result in a changed zoom-level
1172 if (oldZoomLevel
!= zoomLevel()) {
1173 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1177 m_container
->endTransaction();
1180 void DolphinView::applyAdditionalInfoListToView()
1182 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1184 QHash
<QByteArray
, int> visibleRoles
;
1185 visibleRoles
.insert("name", 0);
1188 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1189 visibleRoles
.insert(infoAccessor
.role(info
), index
);
1193 m_container
->setVisibleRoles(visibleRoles
);
1196 void DolphinView::pasteToUrl(const KUrl
& url
)
1198 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1199 KonqOperations::doPaste(this, url
);
1202 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1204 Q_UNUSED(oldZoomLevel
);
1205 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1206 if (oldZoomLevel != newZoomLevel) {
1207 m_viewModeController->setZoomLevel(newZoomLevel);
1208 emit zoomLevelChanged(newZoomLevel);
1212 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1216 const KFileItemList items
= selectedItems();
1217 foreach (const KFileItem
&item
, items
) {
1218 urls
.append(item
.url());
1221 if (itemsExpandable()) {
1222 // TODO: Check if we still need KDirModel for this in KDE 5.0
1223 urls
= KDirModel::simplifiedUrlList(urls
);
1229 QMimeData
* DolphinView::selectionMimeData() const
1231 const KFileItemModel
* model
= fileItemModel();
1232 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1233 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1235 return model
->createMimeData(selectedIndexes
);
1238 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1240 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1241 markUrlsAsSelected(urls
);
1244 void DolphinView::updateWritableState()
1246 const bool wasFolderWritable
= m_isFolderWritable
;
1247 m_isFolderWritable
= true;
1249 const KFileItem item
= m_dirLister
->rootItem();
1250 if (!item
.isNull()) {
1251 KFileItemListProperties
capabilities(KFileItemList() << item
);
1252 m_isFolderWritable
= capabilities
.supportsWriting();
1254 if (m_isFolderWritable
!= wasFolderWritable
) {
1255 emit
writeStateChanged(m_isFolderWritable
);
1259 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1262 case SortByName
: return "name";
1263 case SortBySize
: return "size";
1264 case SortByDate
: return "date";
1265 case SortByPermissions
: return "permissions";
1266 case SortByOwner
: return "owner";
1267 case SortByGroup
: return "group";
1268 case SortByType
: return "type";
1269 case SortByDestination
: return "destination";
1270 case SortByPath
: return "path";
1274 return QByteArray();
1277 #include "dolphinview.moc"