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");
168 KItemListController
* controller
= m_container
->controller();
169 controller
->setSelectionBehavior(KItemListController::MultiSelection
);
170 connect(controller
, SIGNAL(itemActivated(int)),
171 this, SLOT(slotItemActivated(int)));
172 connect(controller
, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
173 connect(controller
, SIGNAL(contextMenuRequested(int,QPointF
)), this, SLOT(slotContextMenuRequested(int,QPointF
)));
174 connect(controller
, SIGNAL(itemExpansionToggleClicked(int)), this, SLOT(slotItemExpansionToggleClicked(int)));
175 connect(controller
, SIGNAL(itemHovered(int)), this, SLOT(slotItemHovered(int)));
176 connect(controller
, SIGNAL(itemUnhovered(int)), this, SLOT(slotItemUnhovered(int)));
177 connect(controller
, SIGNAL(itemDropEvent(int,QGraphicsSceneDragDropEvent
*)), this, SLOT(slotItemDropEvent(int,QGraphicsSceneDragDropEvent
*)));
178 connect(controller
, SIGNAL(modelChanged(KItemModelBase
*,KItemModelBase
*)), this, SLOT(slotModelChanged(KItemModelBase
*,KItemModelBase
*)));
180 KFileItemModel
* model
= fileItemModel();
182 connect(model
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
185 KItemListSelectionManager
* selectionManager
= controller
->selectionManager();
186 connect(selectionManager
, SIGNAL(selectionChanged(QSet
<int>,QSet
<int>)),
187 this, SLOT(slotSelectionChanged(QSet
<int>,QSet
<int>)));
189 m_toolTipManager
= new ToolTipManager(this);
191 m_versionControlObserver
= new VersionControlObserver(this);
192 m_versionControlObserver
->setModel(model
);
193 connect(m_versionControlObserver
, SIGNAL(infoMessage(QString
)), this, SIGNAL(infoMessage(QString
)));
194 connect(m_versionControlObserver
, SIGNAL(errorMessage(QString
)), this, SIGNAL(errorMessage(QString
)));
195 connect(m_versionControlObserver
, SIGNAL(operationCompletedMessage(QString
)), this, SIGNAL(operationCompletedMessage(QString
)));
197 applyViewProperties();
198 m_topLayout
->addWidget(m_container
);
203 DolphinView::~DolphinView()
207 KUrl
DolphinView::url() const
212 void DolphinView::setActive(bool active
)
214 if (active
== m_active
) {
220 QColor color
= KColorScheme(QPalette::Active
, KColorScheme::View
).background().color();
225 /*QAbstractItemView* view = m_viewAccessor.itemView();
226 QWidget* viewport = view ? view->viewport() : 0;
229 palette.setColor(viewport->backgroundRole(), color);
230 viewport->setPalette(palette);
240 emit
writeStateChanged(m_isFolderWritable
);
243 //m_viewModeController->indicateActivationChange(active);
246 bool DolphinView::isActive() const
251 void DolphinView::setMode(Mode mode
)
253 if (mode
!= m_mode
) {
254 ViewProperties
props(url());
255 props
.setViewMode(mode
);
258 applyViewProperties();
262 DolphinView::Mode
DolphinView::mode() const
267 bool DolphinView::previewsShown() const
269 return m_container
->previewsShown();
272 bool DolphinView::hiddenFilesShown() const
274 return m_dirLister
->showingDotFiles();
277 bool DolphinView::categorizedSorting() const
279 return false; //m_storedCategorizedSorting;
282 KFileItemList
DolphinView::items() const
284 return m_dirLister
->items();
287 KFileItemList
DolphinView::selectedItems() const
289 const KFileItemModel
* model
= fileItemModel();
290 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
291 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
293 KFileItemList selectedItems
;
294 QSetIterator
<int> it(selectedIndexes
);
295 while (it
.hasNext()) {
296 const int index
= it
.next();
297 selectedItems
.append(model
->fileItem(index
));
299 return selectedItems
;
302 int DolphinView::selectedItemsCount() const
304 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
305 return selectionManager
->selectedItems().count();
308 void DolphinView::markUrlsAsSelected(const QList
<KUrl
>& urls
)
310 m_selectedUrls
= urls
;
313 void DolphinView::setItemSelectionEnabled(const QRegExp
& pattern
, bool enabled
)
315 const KItemListSelectionManager::SelectionMode mode
= enabled
316 ? KItemListSelectionManager::Select
317 : KItemListSelectionManager::Deselect
;
318 const KFileItemModel
* model
= fileItemModel();
319 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
321 for (int index
= 0; index
< model
->count(); index
++) {
322 const KFileItem item
= model
->fileItem(index
);
323 if (pattern
.exactMatch(item
.name())) {
324 // An alternative approach would be to store the matching items in a QSet<int> and
325 // select them in one go after the loop, but we'd need a new function
326 // KItemListSelectionManager::setSelected(QSet<int>, SelectionMode mode)
328 selectionManager
->setSelected(index
, 1, mode
);
333 void DolphinView::setZoomLevel(int level
)
335 const int oldZoomLevel
= zoomLevel();
336 m_container
->setZoomLevel(level
);
337 if (zoomLevel() != oldZoomLevel
) {
338 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
342 int DolphinView::zoomLevel() const
344 return m_container
->zoomLevel();
347 void DolphinView::setSorting(Sorting sorting
)
349 if (sorting
!= this->sorting()) {
350 updateSorting(sorting
);
354 DolphinView::Sorting
DolphinView::sorting() const
356 KItemModelBase
* model
= m_container
->controller()->model();
357 return sortingForSortRole(model
->sortRole());
360 void DolphinView::setSortOrder(Qt::SortOrder order
)
362 if (sortOrder() != order
) {
363 updateSortOrder(order
);
367 Qt::SortOrder
DolphinView::sortOrder() const
369 KItemModelBase
* model
= fileItemModel();
370 return model
->sortOrder();
373 void DolphinView::setSortFoldersFirst(bool foldersFirst
)
375 if (sortFoldersFirst() != foldersFirst
) {
376 updateSortFoldersFirst(foldersFirst
);
380 bool DolphinView::sortFoldersFirst() const
382 KFileItemModel
* model
= fileItemModel();
383 return model
->sortFoldersFirst();
386 void DolphinView::setAdditionalInfoList(const QList
<AdditionalInfo
>& info
)
388 const QList
<AdditionalInfo
> previousList
= info
;
390 ViewProperties
props(url());
391 props
.setAdditionalInfoList(info
);
393 m_additionalInfoList
= info
;
394 applyAdditionalInfoListToView();
396 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
399 QList
<DolphinView::AdditionalInfo
> DolphinView::additionalInfoList() const
401 return m_additionalInfoList
;
404 void DolphinView::reload()
406 QByteArray viewState
;
407 QDataStream
saveStream(&viewState
, QIODevice::WriteOnly
);
408 saveState(saveStream
);
410 const KFileItemList itemList
= selectedItems();
411 m_selectedUrls
.clear();
412 m_selectedUrls
= itemList
.urlList();
415 loadDirectory(url(), true);
417 QDataStream
restoreStream(viewState
);
418 restoreState(restoreStream
);
421 void DolphinView::stopLoading()
426 void DolphinView::refresh()
428 const bool oldActivationState
= m_active
;
429 const int oldZoomLevel
= zoomLevel();
432 applyViewProperties();
435 setActive(oldActivationState
);
436 updateZoomLevel(oldZoomLevel
);
439 void DolphinView::setNameFilter(const QString
& nameFilter
)
441 Q_UNUSED(nameFilter
);
442 //m_viewModeController->setNameFilter(nameFilter);
445 QString
DolphinView::nameFilter() const
447 return QString(); //m_viewModeController->nameFilter();
450 void DolphinView::calculateItemCount(int& fileCount
,
452 KIO::filesize_t
& totalFileSize
) const
454 foreach (const KFileItem
& item
, m_dirLister
->items()) {
459 totalFileSize
+= item
.size();
464 QString
DolphinView::statusBarText() const
469 KIO::filesize_t totalFileSize
= 0;
471 if (hasSelection()) {
472 // give a summary of the status of the selected files
473 const KFileItemList list
= selectedItems();
474 if (list
.isEmpty()) {
475 // when an item is triggered, it is temporary selected but selectedItems()
476 // will return an empty list
480 KFileItemList::const_iterator it
= list
.begin();
481 const KFileItemList::const_iterator end
= list
.end();
483 const KFileItem
& item
= *it
;
488 totalFileSize
+= item
.size();
493 if (folderCount
+ fileCount
== 1) {
494 // if only one item is selected, show the filename
495 const QString name
= list
.first().text();
496 text
= (folderCount
== 1) ? i18nc("@info:status", "<filename>%1</filename> selected", name
) :
497 i18nc("@info:status", "<filename>%1</filename> selected (%2)",
498 name
, KIO::convertSize(totalFileSize
));
500 // at least 2 items are selected
501 const QString foldersText
= i18ncp("@info:status", "1 Folder selected", "%1 Folders selected", folderCount
);
502 const QString filesText
= i18ncp("@info:status", "1 File selected", "%1 Files selected", fileCount
);
503 if ((folderCount
> 0) && (fileCount
> 0)) {
504 text
= i18nc("@info:status folders, files (size)", "%1, %2 (%3)",
505 foldersText
, filesText
, KIO::convertSize(totalFileSize
));
506 } else if (fileCount
> 0) {
507 text
= i18nc("@info:status files (size)", "%1 (%2)", filesText
, KIO::convertSize(totalFileSize
));
509 Q_ASSERT(folderCount
> 0);
514 calculateItemCount(fileCount
, folderCount
, totalFileSize
);
515 text
= KIO::itemsSummaryString(fileCount
+ folderCount
,
516 fileCount
, folderCount
,
517 totalFileSize
, true);
523 QList
<QAction
*> DolphinView::versionControlActions(const KFileItemList
& items
) const
525 QList
<QAction
*> actions
;
527 if (items
.isEmpty()) {
528 const KFileItem item
= fileItemModel()->rootItem();
529 actions
= m_versionControlObserver
->actions(KFileItemList() << item
);
531 actions
= m_versionControlObserver
->actions(items
);
537 void DolphinView::setUrl(const KUrl
& url
)
543 emit
urlAboutToBeChanged(url
);
546 if (GeneralSettings::showToolTips()) {
547 m_toolTipManager
->hideToolTip();
550 // It is important to clear the items from the model before
551 // applying the view properties, otherwise expensive operations
552 // might be done on the existing items although they get cleared
553 // anyhow afterwards by loadDirectory().
554 fileItemModel()->clear();
555 applyViewProperties();
558 emit
urlChanged(url
);
561 void DolphinView::selectAll()
563 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
564 selectionManager
->setSelected(0, fileItemModel()->count());
567 void DolphinView::invertSelection()
569 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
570 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
573 void DolphinView::clearSelection()
575 m_container
->controller()->selectionManager()->clearSelection();
578 void DolphinView::renameSelectedItems()
580 KFileItemList items
= selectedItems();
581 const int itemCount
= items
.count();
586 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
587 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
588 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
589 m_viewAccessor.itemView()->edit(proxyIndex);
591 RenameDialog
* dialog
= new RenameDialog(this, items
);
592 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
595 dialog
->activateWindow();
598 // assure that the current index remains visible when KDirLister
599 // will notify the view about changed items
600 m_assureVisibleCurrentIndex
= true;
603 void DolphinView::trashSelectedItems()
605 const KUrl::List list
= simplifiedSelectedUrls();
606 KonqOperations::del(this, KonqOperations::TRASH
, list
);
609 void DolphinView::deleteSelectedItems()
611 const KUrl::List list
= simplifiedSelectedUrls();
612 const bool del
= KonqOperations::askDeleteConfirmation(list
,
614 KonqOperations::DEFAULT_CONFIRMATION
,
618 KIO::Job
* job
= KIO::del(list
);
619 connect(job
, SIGNAL(result(KJob
*)),
620 this, SLOT(slotDeleteFileFinished(KJob
*)));
624 void DolphinView::cutSelectedItems()
626 QMimeData
* mimeData
= selectionMimeData();
627 KonqMimeData::addIsCutSelection(mimeData
, true);
628 QApplication::clipboard()->setMimeData(mimeData
);
631 void DolphinView::copySelectedItems()
633 QMimeData
* mimeData
= selectionMimeData();
634 QApplication::clipboard()->setMimeData(mimeData
);
637 void DolphinView::paste()
642 void DolphinView::pasteIntoFolder()
644 const KFileItemList items
= selectedItems();
645 if ((items
.count() == 1) && items
.first().isDir()) {
646 pasteToUrl(items
.first().url());
650 void DolphinView::setPreviewsShown(bool show
)
652 if (previewsShown() == show
) {
656 ViewProperties
props(url());
657 props
.setPreviewsShown(show
);
659 m_container
->setPreviewsShown(show
);
660 emit
previewsShownChanged(show
);
663 void DolphinView::setHiddenFilesShown(bool show
)
665 if (m_dirLister
->showingDotFiles() == show
) {
669 const KFileItemList itemList
= selectedItems();
670 m_selectedUrls
.clear();
671 m_selectedUrls
= itemList
.urlList();
673 ViewProperties
props(url());
674 props
.setHiddenFilesShown(show
);
676 m_dirLister
->setShowingDotFiles(show
);
677 m_dirLister
->emitChanges();
678 emit
hiddenFilesShownChanged(show
);
681 void DolphinView::setCategorizedSorting(bool categorized
)
683 if (categorized
== categorizedSorting()) {
687 ViewProperties
props(url());
688 props
.setCategorizedSorting(categorized
);
691 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
693 emit
categorizedSortingChanged(categorized
);
696 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
698 QWidget::mouseReleaseEvent(event
);
702 void DolphinView::contextMenuEvent(QContextMenuEvent
* event
)
706 const QPoint pos
= m_container
->mapFromGlobal(QCursor::pos());
707 const KItemListView
* view
= m_container
->controller()->view();
708 if (view
->itemAt(pos
) < 0) {
709 // Only open the context-menu if the cursor is above the viewport
710 // (the context-menu for items is handled in slotContextMenuRequested())
711 requestContextMenu(KFileItem(), url(), QList
<QAction
*>());
715 void DolphinView::wheelEvent(QWheelEvent
* event
)
717 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
718 const int numDegrees
= event
->delta() / 8;
719 const int numSteps
= numDegrees
/ 15;
721 setZoomLevel(zoomLevel() + numSteps
);
726 void DolphinView::activate()
731 void DolphinView::slotItemActivated(int index
)
735 const KFileItemList items
= selectedItems();
736 if (items
.isEmpty()) {
740 if (items
.count() == 1) {
741 emit
itemActivated(items
.at(0)); // caught by DolphinViewContainer or DolphinPart
743 foreach (const KFileItem
& item
, items
) {
745 emit
tabRequested(item
.url());
747 emit
itemActivated(item
);
753 void DolphinView::slotItemMiddleClicked(int index
)
755 const KFileItem item
= fileItemModel()->fileItem(index
);
756 if (item
.isDir() || isTabsForFilesEnabled()) {
757 emit
tabRequested(item
.url());
761 void DolphinView::slotContextMenuRequested(int index
, const QPointF
& pos
)
764 if (GeneralSettings::showToolTips()) {
765 m_toolTipManager
->hideToolTip();
767 const KFileItem item
= fileItemModel()->fileItem(index
);
768 emit
requestContextMenu(item
, url(), QList
<QAction
*>());
771 void DolphinView::slotItemExpansionToggleClicked(int index
)
773 // TODO: When doing a model->setExpanded(false) it should
774 // be checked here whether the current index is part of the
775 // closed sub-tree. If this is the case, the current index
776 // should be adjusted to the parent index.
777 KFileItemModel
* model
= fileItemModel();
778 const bool expanded
= model
->isExpanded(index
);
779 model
->setExpanded(index
, !expanded
);
782 void DolphinView::slotItemHovered(int index
)
784 const KFileItem item
= fileItemModel()->fileItem(index
);
786 if (GeneralSettings::showToolTips()) {
787 QRectF itemRect
= m_container
->controller()->view()->itemBoundingRect(index
);
788 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
789 itemRect
.moveTo(pos
);
791 m_toolTipManager
->showToolTip(item
, itemRect
);
794 emit
requestItemInfo(item
);
797 void DolphinView::slotItemUnhovered(int index
)
800 if (GeneralSettings::showToolTips()) {
801 m_toolTipManager
->hideToolTip();
803 emit
requestItemInfo(KFileItem());
806 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
808 const KFileItem destItem
= fileItemModel()->fileItem(index
);
810 QDropEvent
dropEvent(event
->pos().toPoint(),
811 event
->possibleActions(),
816 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
819 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
822 disconnect(previous
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
825 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
826 connect(current
, SIGNAL(loadingCompleted()), this, SLOT(slotLoadingCompleted()));
828 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
829 m_versionControlObserver
->setModel(fileItemModel
);
832 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
834 const int currentCount
= current
.count();
835 const int previousCount
= previous
.count();
836 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
837 (currentCount
> 0 && previousCount
== 0);
839 // If nothing has been selected before and something got selected (or if something
840 // was selected before and now nothing is selected) the selectionChangedSignal must
841 // be emitted asynchronously as fast as possible to update the edit-actions.
842 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
843 m_selectionChangedTimer
->start();
846 void DolphinView::emitSelectionChangedSignal()
848 m_selectionChangedTimer
->stop();
849 emit
selectionChanged(selectedItems());
852 void DolphinView::openContextMenu(const QPoint
& pos
,
853 const QList
<QAction
*>& customActions
)
856 const int index
= m_container
->controller()->view()->itemAt(pos
);
858 item
= fileItemModel()->fileItem(index
);
861 emit
requestContextMenu(item
, url(), customActions
);
864 void DolphinView::dropUrls(const KFileItem
& destItem
,
865 const KUrl
& destPath
,
870 markPastedUrlsAsSelected(event
->mimeData());
871 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
874 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
876 ViewProperties
props(url());
877 props
.setSorting(sorting
);
879 KItemModelBase
* model
= m_container
->controller()->model();
880 model
->setSortRole(sortRoleForSorting(sorting
));
882 emit
sortingChanged(sorting
);
885 void DolphinView::updateSortOrder(Qt::SortOrder order
)
887 ViewProperties
props(url());
888 props
.setSortOrder(order
);
890 KItemModelBase
* model
= fileItemModel();
891 model
->setSortOrder(order
);
893 emit
sortOrderChanged(order
);
896 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
898 ViewProperties
props(url());
899 props
.setSortFoldersFirst(foldersFirst
);
901 KFileItemModel
* model
= fileItemModel();
902 model
->setSortFoldersFirst(foldersFirst
);
904 emit
sortFoldersFirstChanged(foldersFirst
);
907 QPair
<bool, QString
> DolphinView::pasteInfo() const
909 return KonqOperations::pasteInfo(url());
912 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
914 m_tabsForFiles
= tabsForFiles
;
917 bool DolphinView::isTabsForFilesEnabled() const
919 return m_tabsForFiles
;
922 bool DolphinView::itemsExpandable() const
924 return m_mode
== DetailsView
;
927 void DolphinView::restoreState(QDataStream
& stream
)
929 // Restore the current item that had the keyboard focus
930 stream
>> m_currentItemUrl
;
932 // Restore the view position
933 stream
>> m_restoredContentsPosition
;
935 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
938 fileItemModel()->restoreExpandedUrls(urls
);
941 void DolphinView::saveState(QDataStream
& stream
)
943 // Save the current item that has the keyboard focus
944 const int currentIndex
= m_container
->controller()->selectionManager()->currentItem();
945 if (currentIndex
!= -1) {
946 KFileItem item
= fileItemModel()->fileItem(currentIndex
);
947 KUrl currentItemUrl
= item
.url();
948 stream
<< currentItemUrl
;
953 // Save view position
954 const qreal x
= m_container
->horizontalScrollBar()->value();
955 const qreal y
= m_container
->verticalScrollBar()->value();
956 stream
<< QPoint(x
, y
);
958 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
959 stream
<< fileItemModel()->expandedUrls();
962 bool DolphinView::hasSelection() const
964 return m_container
->controller()->selectionManager()->hasSelection();
967 KFileItem
DolphinView::rootItem() const
969 return m_dirLister
->rootItem();
972 void DolphinView::observeCreatedItem(const KUrl
& url
)
974 m_createdItemUrl
= url
;
975 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
976 // this, SLOT(selectAndScrollToCreatedItem()));
979 void DolphinView::selectAndScrollToCreatedItem()
981 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
982 if (dirIndex.isValid()) {
983 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
984 QAbstractItemView* view = m_viewAccessor.itemView();
986 view->setCurrentIndex(proxyIndex);
990 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
991 this, SLOT(selectAndScrollToCreatedItem()));*/
992 m_createdItemUrl
= KUrl();
995 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
997 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
998 emit
redirection(oldUrl
, newUrl
);
999 m_url
= newUrl
; // #186947
1003 void DolphinView::updateViewState()
1005 if (m_currentItemUrl
!= KUrl()) {
1006 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1007 const int currentIndex
= fileItemModel()->index(m_currentItemUrl
);
1008 if (currentIndex
!= -1) {
1009 selectionManager
->setCurrentItem(currentIndex
);
1011 selectionManager
->setCurrentItem(0);
1013 m_currentItemUrl
= KUrl();
1016 if (!m_restoredContentsPosition
.isNull()) {
1017 const int x
= m_restoredContentsPosition
.x();
1018 const int y
= m_restoredContentsPosition
.y();
1019 m_restoredContentsPosition
= QPoint();
1021 m_container
->horizontalScrollBar()->setValue(x
);
1022 m_container
->verticalScrollBar()->setValue(y
);
1025 if (!m_selectedUrls
.isEmpty()) {
1026 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1027 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1028 const KFileItemModel
* model
= fileItemModel();
1030 foreach (const KUrl
& url
, m_selectedUrls
) {
1031 const int index
= model
->index(url
);
1033 selectedItems
.insert(index
);
1037 selectionManager
->setSelectedItems(selectedItems
);
1038 m_selectedUrls
.clear();
1042 void DolphinView::showHoverInformation(const KFileItem
& item
)
1044 emit
requestItemInfo(item
);
1047 void DolphinView::clearHoverInformation()
1049 emit
requestItemInfo(KFileItem());
1052 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1054 if (job
->error() == 0) {
1055 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1056 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1057 emit
errorMessage(job
->errorString());
1061 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1063 // Disable the writestate temporary until it can be determined in a fast way
1064 // in DolphinView::slotLoadingCompleted()
1065 if (m_isFolderWritable
) {
1066 m_isFolderWritable
= false;
1067 emit
writeStateChanged(m_isFolderWritable
);
1070 emit
startedPathLoading(url
);
1073 void DolphinView::slotLoadingCompleted()
1075 // Update the view-state. This has to be done using a Qt::QueuedConnection
1076 // because the view might not be in its final state yet (the view also
1077 // listens to the completed()-signal from KDirLister and the order of
1078 // of slots is undefined).
1079 QTimer::singleShot(0, this, SLOT(updateViewState()));
1081 emit
finishedPathLoading(url());
1083 updateWritableState();
1086 void DolphinView::slotRefreshItems()
1088 if (m_assureVisibleCurrentIndex
) {
1089 m_assureVisibleCurrentIndex
= false;
1090 //QAbstractItemView* view = m_viewAccessor.itemView();
1092 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1097 KFileItemModel
* DolphinView::fileItemModel() const
1099 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1102 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1104 if (!url
.isValid()) {
1105 const QString
location(url
.pathOrUrl());
1106 if (location
.isEmpty()) {
1107 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1109 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1114 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1117 void DolphinView::applyViewProperties()
1119 m_container
->beginTransaction();
1121 const ViewProperties
props(url());
1123 const Mode mode
= props
.viewMode();
1124 if (m_mode
!= mode
) {
1125 const Mode previousMode
= m_mode
;
1128 // Changing the mode might result in changing
1129 // the zoom level. Remember the old zoom level so
1130 // that zoomLevelChanged() can get emitted.
1131 const int oldZoomLevel
= m_container
->zoomLevel();
1134 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1135 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1136 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1137 default: Q_ASSERT(false); break;
1140 emit
modeChanged(m_mode
, previousMode
);
1142 if (m_container
->zoomLevel() != oldZoomLevel
) {
1143 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1147 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1148 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1149 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1150 m_dirLister
->emitChanges();
1151 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1154 /* m_storedCategorizedSorting = props.categorizedSorting();
1155 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1156 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1157 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1158 emit categorizedSortingChanged();
1161 const DolphinView::Sorting sorting
= props
.sorting();
1162 KFileItemModel
* model
= fileItemModel();
1163 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1164 if (newSortRole
!= model
->sortRole()) {
1165 model
->setSortRole(newSortRole
);
1166 emit
sortingChanged(sorting
);
1169 const Qt::SortOrder sortOrder
= props
.sortOrder();
1170 if (sortOrder
!= model
->sortOrder()) {
1171 model
->setSortOrder(sortOrder
);
1172 emit
sortOrderChanged(sortOrder
);
1175 const bool sortFoldersFirst
= props
.sortFoldersFirst();
1176 if (sortFoldersFirst
!= model
->sortFoldersFirst()) {
1177 model
->setSortFoldersFirst(sortFoldersFirst
);
1178 emit
sortFoldersFirstChanged(sortFoldersFirst
);
1181 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1182 if (infoList
!= m_additionalInfoList
) {
1183 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1184 m_additionalInfoList
= infoList
;
1185 applyAdditionalInfoListToView();
1186 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1189 const bool previewsShown
= props
.previewsShown();
1190 if (previewsShown
!= m_container
->previewsShown()) {
1191 const int oldZoomLevel
= zoomLevel();
1193 m_container
->setPreviewsShown(previewsShown
);
1194 emit
previewsShownChanged(previewsShown
);
1196 // Changing the preview-state might result in a changed zoom-level
1197 if (oldZoomLevel
!= zoomLevel()) {
1198 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1202 m_container
->endTransaction();
1205 void DolphinView::applyAdditionalInfoListToView()
1207 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1209 QList
<QByteArray
> visibleRoles
;
1210 visibleRoles
.reserve(m_additionalInfoList
.count() + 1);
1211 visibleRoles
.append("name");
1213 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1214 visibleRoles
.append(infoAccessor
.role(info
));
1217 m_container
->setVisibleRoles(visibleRoles
);
1220 void DolphinView::pasteToUrl(const KUrl
& url
)
1222 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1223 KonqOperations::doPaste(this, url
);
1226 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1228 Q_UNUSED(oldZoomLevel
);
1229 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1230 if (oldZoomLevel != newZoomLevel) {
1231 m_viewModeController->setZoomLevel(newZoomLevel);
1232 emit zoomLevelChanged(newZoomLevel);
1236 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1240 const KFileItemList items
= selectedItems();
1241 foreach (const KFileItem
&item
, items
) {
1242 urls
.append(item
.url());
1245 if (itemsExpandable()) {
1246 // TODO: Check if we still need KDirModel for this in KDE 5.0
1247 urls
= KDirModel::simplifiedUrlList(urls
);
1253 QMimeData
* DolphinView::selectionMimeData() const
1255 const KFileItemModel
* model
= fileItemModel();
1256 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1257 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1259 return model
->createMimeData(selectedIndexes
);
1262 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1264 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1265 markUrlsAsSelected(urls
);
1268 void DolphinView::updateWritableState()
1270 const bool wasFolderWritable
= m_isFolderWritable
;
1271 m_isFolderWritable
= true;
1273 const KFileItem item
= m_dirLister
->rootItem();
1274 if (!item
.isNull()) {
1275 KFileItemListProperties
capabilities(KFileItemList() << item
);
1276 m_isFolderWritable
= capabilities
.supportsWriting();
1278 if (m_isFolderWritable
!= wasFolderWritable
) {
1279 emit
writeStateChanged(m_isFolderWritable
);
1283 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1286 case SortByName
: return "name";
1287 case SortBySize
: return "size";
1288 case SortByDate
: return "date";
1289 case SortByPermissions
: return "permissions";
1290 case SortByOwner
: return "owner";
1291 case SortByGroup
: return "group";
1292 case SortByType
: return "type";
1293 case SortByDestination
: return "destination";
1294 case SortByPath
: return "path";
1298 return QByteArray();
1301 DolphinView::Sorting
DolphinView::sortingForSortRole(const QByteArray
& sortRole
) const
1303 static QHash
<QByteArray
, DolphinView::Sorting
> sortHash
;
1304 if (sortHash
.isEmpty()) {
1305 sortHash
.insert("name", SortByName
);
1306 sortHash
.insert("size", SortBySize
);
1307 sortHash
.insert("date", SortByDate
);
1308 sortHash
.insert("permissions", SortByPermissions
);
1309 sortHash
.insert("owner", SortByOwner
);
1310 sortHash
.insert("group", SortByGroup
);
1311 sortHash
.insert("type", SortByType
);
1312 sortHash
.insert("destination", SortByDestination
);
1313 sortHash
.insert("path", SortByPath
);
1315 return sortHash
.value(sortRole
);
1318 void DolphinView::markUrlAsCurrent(const KUrl
& url
)
1320 m_currentItemUrl
= url
;
1323 #include "dolphinview.moc"