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
521 QList
<QAction
*> actions
;
523 if (items
.isEmpty()) {
524 const KUrl url
= fileItemModel()->rootDirectory();
525 actions
= m_versionControlObserver
->contextMenuActions(url
.path(KUrl::AddTrailingSlash
));
527 actions
= m_versionControlObserver
->contextMenuActions(items
);
533 void DolphinView::setUrl(const KUrl
& url
)
539 emit
urlAboutToBeChanged(url
);
542 if (GeneralSettings::showToolTips()) {
543 m_toolTipManager
->hideToolTip();
546 // It is important to clear the items from the model before
547 // applying the view properties, otherwise expensive operations
548 // might be done on the existing items although they get cleared
549 // anyhow afterwards by loadDirectory().
550 fileItemModel()->clear();
551 applyViewProperties();
554 emit
urlChanged(url
);
557 void DolphinView::selectAll()
559 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
560 selectionManager
->setSelected(0, fileItemModel()->count());
563 void DolphinView::invertSelection()
565 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
566 selectionManager
->setSelected(0, fileItemModel()->count(), KItemListSelectionManager::Toggle
);
569 void DolphinView::clearSelection()
571 m_container
->controller()->selectionManager()->clearSelection();
574 void DolphinView::renameSelectedItems()
576 KFileItemList items
= selectedItems();
577 const int itemCount
= items
.count();
582 /*if ((itemCount == 1) && DolphinSettings::instance().generalSettings()->renameInline()) {
583 const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForItem(items.first());
584 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
585 m_viewAccessor.itemView()->edit(proxyIndex);
587 RenameDialog
* dialog
= new RenameDialog(this, items
);
588 dialog
->setAttribute(Qt::WA_DeleteOnClose
);
591 dialog
->activateWindow();
594 // assure that the current index remains visible when KDirLister
595 // will notify the view about changed items
596 m_assureVisibleCurrentIndex
= true;
599 void DolphinView::trashSelectedItems()
601 const KUrl::List list
= simplifiedSelectedUrls();
602 KonqOperations::del(this, KonqOperations::TRASH
, list
);
605 void DolphinView::deleteSelectedItems()
607 const KUrl::List list
= simplifiedSelectedUrls();
608 const bool del
= KonqOperations::askDeleteConfirmation(list
,
610 KonqOperations::DEFAULT_CONFIRMATION
,
614 KIO::Job
* job
= KIO::del(list
);
615 connect(job
, SIGNAL(result(KJob
*)),
616 this, SLOT(slotDeleteFileFinished(KJob
*)));
620 void DolphinView::cutSelectedItems()
622 QMimeData
* mimeData
= selectionMimeData();
623 KonqMimeData::addIsCutSelection(mimeData
, true);
624 QApplication::clipboard()->setMimeData(mimeData
);
627 void DolphinView::copySelectedItems()
629 QMimeData
* mimeData
= selectionMimeData();
630 QApplication::clipboard()->setMimeData(mimeData
);
633 void DolphinView::paste()
638 void DolphinView::pasteIntoFolder()
640 const KFileItemList items
= selectedItems();
641 if ((items
.count() == 1) && items
.first().isDir()) {
642 pasteToUrl(items
.first().url());
646 void DolphinView::setPreviewsShown(bool show
)
648 if (previewsShown() == show
) {
652 ViewProperties
props(url());
653 props
.setPreviewsShown(show
);
655 m_container
->setPreviewsShown(show
);
656 emit
previewsShownChanged(show
);
659 void DolphinView::setHiddenFilesShown(bool show
)
661 if (m_dirLister
->showingDotFiles() == show
) {
665 m_selectedItems
= selectedItems();
667 ViewProperties
props(url());
668 props
.setHiddenFilesShown(show
);
670 m_dirLister
->setShowingDotFiles(show
);
671 m_dirLister
->emitChanges();
672 emit
hiddenFilesShownChanged(show
);
675 void DolphinView::setCategorizedSorting(bool categorized
)
677 if (categorized
== categorizedSorting()) {
681 ViewProperties
props(url());
682 props
.setCategorizedSorting(categorized
);
685 //m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
687 emit
categorizedSortingChanged(categorized
);
690 void DolphinView::mouseReleaseEvent(QMouseEvent
* event
)
692 QWidget::mouseReleaseEvent(event
);
696 void DolphinView::contextMenuEvent(QContextMenuEvent
* event
)
700 const QPoint pos
= m_container
->mapFromGlobal(QCursor::pos());
701 const KItemListView
* view
= m_container
->controller()->view();
702 if (view
->itemAt(pos
) < 0) {
703 // Only open the context-menu if the cursor is above the viewport
704 // (the context-menu for items is handled in slotContextMenuRequested())
705 requestContextMenu(KFileItem(), url(), QList
<QAction
*>());
709 void DolphinView::wheelEvent(QWheelEvent
* event
)
711 if (event
->modifiers().testFlag(Qt::ControlModifier
)) {
712 const int numDegrees
= event
->delta() / 8;
713 const int numSteps
= numDegrees
/ 15;
715 setZoomLevel(zoomLevel() + numSteps
);
720 void DolphinView::activate()
725 void DolphinView::slotItemActivated(int index
)
729 const KFileItemList items
= selectedItems();
730 if (items
.isEmpty()) {
734 if (items
.count() == 1) {
735 emit
itemActivated(items
.at(0)); // caught by DolphinViewContainer or DolphinPart
737 foreach (const KFileItem
& item
, items
) {
739 emit
tabRequested(item
.url());
741 emit
itemActivated(item
);
747 void DolphinView::slotItemMiddleClicked(int index
)
749 const KFileItem item
= fileItemModel()->fileItem(index
);
750 if (item
.isDir() || isTabsForFilesEnabled()) {
751 emit
tabRequested(item
.url());
755 void DolphinView::slotContextMenuRequested(int index
, const QPointF
& pos
)
758 if (GeneralSettings::showToolTips()) {
759 m_toolTipManager
->hideToolTip();
761 const KFileItem item
= fileItemModel()->fileItem(index
);
762 emit
requestContextMenu(item
, url(), QList
<QAction
*>());
765 void DolphinView::slotItemExpansionToggleClicked(int index
)
767 // TODO: When doing a model->setExpanded(false) it should
768 // be checked here whether the current index is part of the
769 // closed sub-tree. If this is the case, the current index
770 // should be adjusted to the parent index.
771 KFileItemModel
* model
= fileItemModel();
772 const bool expanded
= model
->isExpanded(index
);
773 model
->setExpanded(index
, !expanded
);
776 void DolphinView::slotItemHovered(int index
)
778 const KFileItem item
= fileItemModel()->fileItem(index
);
780 if (GeneralSettings::showToolTips()) {
781 QRectF itemRect
= m_container
->controller()->view()->itemBoundingRect(index
);
782 const QPoint pos
= m_container
->mapToGlobal(itemRect
.topLeft().toPoint());
783 itemRect
.moveTo(pos
);
785 m_toolTipManager
->showToolTip(item
, itemRect
);
788 emit
requestItemInfo(item
);
791 void DolphinView::slotItemUnhovered(int index
)
794 if (GeneralSettings::showToolTips()) {
795 m_toolTipManager
->hideToolTip();
797 emit
requestItemInfo(KFileItem());
800 void DolphinView::slotItemDropEvent(int index
, QGraphicsSceneDragDropEvent
* event
)
802 const KFileItem destItem
= fileItemModel()->fileItem(index
);
804 QDropEvent
dropEvent(event
->pos().toPoint(),
805 event
->possibleActions(),
810 DragAndDropHelper::dropUrls(destItem
, url(), &dropEvent
, this);
813 void DolphinView::slotModelChanged(KItemModelBase
* current
, KItemModelBase
* previous
)
816 Q_ASSERT(qobject_cast
<KFileItemModel
*>(current
));
818 KFileItemModel
* fileItemModel
= static_cast<KFileItemModel
*>(current
);
819 m_versionControlObserver
->setModel(fileItemModel
);
822 void DolphinView::slotSelectionChanged(const QSet
<int>& current
, const QSet
<int>& previous
)
824 const int currentCount
= current
.count();
825 const int previousCount
= previous
.count();
826 const bool selectionStateChanged
= (currentCount
== 0 && previousCount
> 0) ||
827 (currentCount
> 0 && previousCount
== 0);
829 // If nothing has been selected before and something got selected (or if something
830 // was selected before and now nothing is selected) the selectionChangedSignal must
831 // be emitted asynchronously as fast as possible to update the edit-actions.
832 m_selectionChangedTimer
->setInterval(selectionStateChanged
? 0 : 300);
833 m_selectionChangedTimer
->start();
836 void DolphinView::emitSelectionChangedSignal()
838 m_selectionChangedTimer
->stop();
839 emit
selectionChanged(selectedItems());
842 void DolphinView::openContextMenu(const QPoint
& pos
,
843 const QList
<QAction
*>& customActions
)
846 const int index
= m_container
->controller()->view()->itemAt(pos
);
848 item
= fileItemModel()->fileItem(index
);
851 emit
requestContextMenu(item
, url(), customActions
);
854 void DolphinView::dropUrls(const KFileItem
& destItem
,
855 const KUrl
& destPath
,
860 markPastedUrlsAsSelected(event
->mimeData());
861 //DragAndDropHelper::instance().dropUrls(destItem, destPath, event, this);
864 void DolphinView::updateSorting(DolphinView::Sorting sorting
)
866 ViewProperties
props(url());
867 props
.setSorting(sorting
);
869 KItemModelBase
* model
= m_container
->controller()->model();
870 model
->setSortRole(sortRoleForSorting(sorting
));
872 emit
sortingChanged(sorting
);
875 void DolphinView::updateSortOrder(Qt::SortOrder order
)
877 ViewProperties
props(url());
878 props
.setSortOrder(order
);
880 //m_viewAccessor.proxyModel()->setSortOrder(order);
882 emit
sortOrderChanged(order
);
885 void DolphinView::updateSortFoldersFirst(bool foldersFirst
)
887 ViewProperties
props(url());
888 props
.setSortFoldersFirst(foldersFirst
);
890 //m_viewAccessor.proxyModel()->setSortFoldersFirst(foldersFirst);
892 emit
sortFoldersFirstChanged(foldersFirst
);
895 QPair
<bool, QString
> DolphinView::pasteInfo() const
897 return KonqOperations::pasteInfo(url());
900 void DolphinView::setTabsForFilesEnabled(bool tabsForFiles
)
902 m_tabsForFiles
= tabsForFiles
;
905 bool DolphinView::isTabsForFilesEnabled() const
907 return m_tabsForFiles
;
910 bool DolphinView::itemsExpandable() const
912 return m_mode
== DetailsView
;
915 void DolphinView::restoreState(QDataStream
& stream
)
917 // Restore the current item that had the keyboard focus
918 stream
>> m_currentItemIndex
;
920 // Restore the view position
921 stream
>> m_restoredContentsPosition
;
923 // Restore expanded folders (only relevant for the details view - will be ignored by the view in other view modes)
924 QSet
<KUrl
> urlsToExpand
;
925 stream
>> urlsToExpand
;
926 /*const DolphinDetailsViewExpander* expander = m_viewAccessor.setExpandedUrls(urlsToExpand);
928 m_expanderActive = true;
929 connect (expander, SIGNAL(completed()), this, SLOT(slotLoadingCompleted()));
932 m_expanderActive = false;
936 void DolphinView::saveState(QDataStream
& stream
)
938 // Save the current item that has the keyboard focus
939 stream
<< m_container
->controller()->selectionManager()->currentItem();
941 // Save view position
942 const qreal x
= m_container
->horizontalScrollBar()->value();
943 const qreal y
= m_container
->verticalScrollBar()->value();
944 stream
<< QPoint(x
, y
);
946 // Save expanded folders (only relevant for the details view - the set will be empty in other view modes)
947 //stream << m_viewAccessor.expandedUrls();
950 bool DolphinView::hasSelection() const
952 return m_container
->controller()->selectionManager()->hasSelection();
955 KFileItem
DolphinView::rootItem() const
957 return m_dirLister
->rootItem();
960 void DolphinView::observeCreatedItem(const KUrl
& url
)
962 m_createdItemUrl
= url
;
963 //connect(m_dirModel, SIGNAL(rowsInserted(QModelIndex,int,int)),
964 // this, SLOT(selectAndScrollToCreatedItem()));
967 void DolphinView::selectAndScrollToCreatedItem()
969 /*const QModelIndex dirIndex = m_viewAccessor.dirModel()->indexForUrl(m_createdItemUrl);
970 if (dirIndex.isValid()) {
971 const QModelIndex proxyIndex = m_viewAccessor.proxyModel()->mapFromSource(dirIndex);
972 QAbstractItemView* view = m_viewAccessor.itemView();
974 view->setCurrentIndex(proxyIndex);
978 disconnect(m_viewAccessor.dirModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
979 this, SLOT(selectAndScrollToCreatedItem()));*/
980 m_createdItemUrl
= KUrl();
983 void DolphinView::slotRedirection(const KUrl
& oldUrl
, const KUrl
& newUrl
)
985 if (oldUrl
.equals(url(), KUrl::CompareWithoutTrailingSlash
)) {
986 emit
redirection(oldUrl
, newUrl
);
987 m_url
= newUrl
; // #186947
991 void DolphinView::updateViewState()
993 if (m_currentItemIndex
>= 0) {
994 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
995 selectionManager
->setCurrentItem(m_currentItemIndex
);
996 m_currentItemIndex
=-1;
999 if (!m_restoredContentsPosition
.isNull()) {
1000 const int x
= m_restoredContentsPosition
.x();
1001 const int y
= m_restoredContentsPosition
.y();
1002 m_restoredContentsPosition
= QPoint();
1004 m_container
->horizontalScrollBar()->setValue(x
);
1005 m_container
->verticalScrollBar()->setValue(y
);
1008 if (!m_selectedItems
.isEmpty()) {
1009 KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1010 QSet
<int> selectedItems
= selectionManager
->selectedItems();
1011 const KFileItemModel
* model
= fileItemModel();
1013 foreach (const KFileItem
& selectedItem
, m_selectedItems
) {
1014 const int index
= model
->index(selectedItem
);
1016 selectedItems
.insert(index
);
1020 selectionManager
->setSelectedItems(selectedItems
);
1021 m_selectedItems
.clear();
1025 void DolphinView::showHoverInformation(const KFileItem
& item
)
1027 emit
requestItemInfo(item
);
1030 void DolphinView::clearHoverInformation()
1032 emit
requestItemInfo(KFileItem());
1035 void DolphinView::slotDeleteFileFinished(KJob
* job
)
1037 if (job
->error() == 0) {
1038 emit
operationCompletedMessage(i18nc("@info:status", "Delete operation completed."));
1039 } else if (job
->error() != KIO::ERR_USER_CANCELED
) {
1040 emit
errorMessage(job
->errorString());
1044 void DolphinView::slotDirListerStarted(const KUrl
& url
)
1046 // Disable the writestate temporary until it can be determined in a fast way
1047 // in DolphinView::slotDirListerCompleted()
1048 if (m_isFolderWritable
) {
1049 m_isFolderWritable
= false;
1050 emit
writeStateChanged(m_isFolderWritable
);
1053 emit
startedPathLoading(url
);
1056 void DolphinView::slotDirListerCompleted()
1058 // Update the view-state. This has to be done using a Qt::QueuedConnection
1059 // because the view might not be in its final state yet (the view also
1060 // listens to the completed()-signal from KDirLister and the order of
1061 // of slots is undefined).
1062 QTimer::singleShot(0, this, SLOT(updateViewState()));
1064 emit
finishedPathLoading(url());
1066 updateWritableState();
1069 void DolphinView::slotRefreshItems()
1071 if (m_assureVisibleCurrentIndex
) {
1072 m_assureVisibleCurrentIndex
= false;
1073 //QAbstractItemView* view = m_viewAccessor.itemView();
1075 // m_viewAccessor.itemView()->scrollTo(m_viewAccessor.itemView()->currentIndex());
1080 KFileItemModel
* DolphinView::fileItemModel() const
1082 return static_cast<KFileItemModel
*>(m_container
->controller()->model());
1085 void DolphinView::loadDirectory(const KUrl
& url
, bool reload
)
1087 if (!url
.isValid()) {
1088 const QString
location(url
.pathOrUrl());
1089 if (location
.isEmpty()) {
1090 emit
errorMessage(i18nc("@info:status", "The location is empty."));
1092 emit
errorMessage(i18nc("@info:status", "The location '%1' is invalid.", location
));
1097 m_dirLister
->openUrl(url
, reload
? KDirLister::Reload
: KDirLister::NoFlags
);
1100 void DolphinView::applyViewProperties()
1102 m_container
->beginTransaction();
1104 const ViewProperties
props(url());
1106 const Mode mode
= props
.viewMode();
1107 if (m_mode
!= mode
) {
1108 const Mode previousMode
= m_mode
;
1111 // Changing the mode might result in changing
1112 // the zoom level. Remember the old zoom level so
1113 // that zoomLevelChanged() can get emitted.
1114 const int oldZoomLevel
= m_container
->zoomLevel();
1117 case IconsView
: m_container
->setItemLayout(KFileItemListView::IconsLayout
); break;
1118 case CompactView
: m_container
->setItemLayout(KFileItemListView::CompactLayout
); break;
1119 case DetailsView
: m_container
->setItemLayout(KFileItemListView::DetailsLayout
); break;
1120 default: Q_ASSERT(false); break;
1123 emit
modeChanged(m_mode
, previousMode
);
1125 if (m_container
->zoomLevel() != oldZoomLevel
) {
1126 emit
zoomLevelChanged(m_container
->zoomLevel(), oldZoomLevel
);
1130 const bool hiddenFilesShown
= props
.hiddenFilesShown();
1131 if (hiddenFilesShown
!= m_dirLister
->showingDotFiles()) {
1132 m_dirLister
->setShowingDotFiles(hiddenFilesShown
);
1133 m_dirLister
->emitChanges();
1134 emit
hiddenFilesShownChanged(hiddenFilesShown
);
1137 /* m_storedCategorizedSorting = props.categorizedSorting();
1138 const bool categorized = m_storedCategorizedSorting && supportsCategorizedSorting();
1139 if (categorized != m_viewAccessor.proxyModel()->isCategorizedModel()) {
1140 m_viewAccessor.proxyModel()->setCategorizedModel(categorized);
1141 emit categorizedSortingChanged();
1144 const DolphinView::Sorting sorting
= props
.sorting();
1145 KItemModelBase
* model
= m_container
->controller()->model();
1146 const QByteArray newSortRole
= sortRoleForSorting(sorting
);
1147 if (newSortRole
!= model
->sortRole()) {
1148 model
->setSortRole(newSortRole
);
1149 emit
sortingChanged(sorting
);
1152 const Qt::SortOrder sortOrder = props.sortOrder();
1153 if (sortOrder != m_viewAccessor.proxyModel()->sortOrder()) {
1154 m_viewAccessor.proxyModel()->setSortOrder(sortOrder);
1155 emit sortOrderChanged(sortOrder);
1158 const bool sortFoldersFirst = props.sortFoldersFirst();
1159 if (sortFoldersFirst != m_viewAccessor.proxyModel()->sortFoldersFirst()) {
1160 m_viewAccessor.proxyModel()->setSortFoldersFirst(sortFoldersFirst);
1161 emit sortFoldersFirstChanged(sortFoldersFirst);
1164 const QList
<DolphinView::AdditionalInfo
> infoList
= props
.additionalInfoList();
1165 if (infoList
!= m_additionalInfoList
) {
1166 const QList
<DolphinView::AdditionalInfo
> previousList
= m_additionalInfoList
;
1167 m_additionalInfoList
= infoList
;
1168 applyAdditionalInfoListToView();
1169 emit
additionalInfoListChanged(m_additionalInfoList
, previousList
);
1172 const bool previewsShown
= props
.previewsShown();
1173 if (previewsShown
!= m_container
->previewsShown()) {
1174 const int oldZoomLevel
= zoomLevel();
1176 m_container
->setPreviewsShown(previewsShown
);
1177 emit
previewsShownChanged(previewsShown
);
1179 // Changing the preview-state might result in a changed zoom-level
1180 if (oldZoomLevel
!= zoomLevel()) {
1181 emit
zoomLevelChanged(zoomLevel(), oldZoomLevel
);
1185 m_container
->endTransaction();
1188 void DolphinView::applyAdditionalInfoListToView()
1190 const AdditionalInfoAccessor
& infoAccessor
= AdditionalInfoAccessor::instance();
1192 QHash
<QByteArray
, int> visibleRoles
;
1193 visibleRoles
.insert("name", 0);
1196 foreach (AdditionalInfo info
, m_additionalInfoList
) {
1197 visibleRoles
.insert(infoAccessor
.role(info
), index
);
1201 m_container
->setVisibleRoles(visibleRoles
);
1204 void DolphinView::pasteToUrl(const KUrl
& url
)
1206 markPastedUrlsAsSelected(QApplication::clipboard()->mimeData());
1207 KonqOperations::doPaste(this, url
);
1210 void DolphinView::updateZoomLevel(int oldZoomLevel
)
1212 Q_UNUSED(oldZoomLevel
);
1213 /* const int newZoomLevel = ZoomLevelInfo::zoomLevelForIconSize(m_viewAccessor.itemView()->iconSize());
1214 if (oldZoomLevel != newZoomLevel) {
1215 m_viewModeController->setZoomLevel(newZoomLevel);
1216 emit zoomLevelChanged(newZoomLevel);
1220 KUrl::List
DolphinView::simplifiedSelectedUrls() const
1224 const KFileItemList items
= selectedItems();
1225 foreach (const KFileItem
&item
, items
) {
1226 urls
.append(item
.url());
1229 if (itemsExpandable()) {
1230 // TODO: Check if we still need KDirModel for this in KDE 5.0
1231 urls
= KDirModel::simplifiedUrlList(urls
);
1237 QMimeData
* DolphinView::selectionMimeData() const
1239 const KFileItemModel
* model
= fileItemModel();
1240 const KItemListSelectionManager
* selectionManager
= m_container
->controller()->selectionManager();
1241 const QSet
<int> selectedIndexes
= selectionManager
->selectedItems();
1243 return model
->createMimeData(selectedIndexes
);
1246 void DolphinView::markPastedUrlsAsSelected(const QMimeData
* mimeData
)
1248 const KUrl::List urls
= KUrl::List::fromMimeData(mimeData
);
1249 markUrlsAsSelected(urls
);
1252 void DolphinView::updateWritableState()
1254 const bool wasFolderWritable
= m_isFolderWritable
;
1255 m_isFolderWritable
= true;
1257 const KFileItem item
= m_dirLister
->rootItem();
1258 if (!item
.isNull()) {
1259 KFileItemListProperties
capabilities(KFileItemList() << item
);
1260 m_isFolderWritable
= capabilities
.supportsWriting();
1262 if (m_isFolderWritable
!= wasFolderWritable
) {
1263 emit
writeStateChanged(m_isFolderWritable
);
1267 QByteArray
DolphinView::sortRoleForSorting(Sorting sorting
) const
1270 case SortByName
: return "name";
1271 case SortBySize
: return "size";
1272 case SortByDate
: return "date";
1273 case SortByPermissions
: return "permissions";
1274 case SortByOwner
: return "owner";
1275 case SortByGroup
: return "group";
1276 case SortByType
: return "type";
1277 case SortByDestination
: return "destination";
1278 case SortByPath
: return "path";
1282 return QByteArray();
1285 #include "dolphinview.moc"