X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/blobdiff_plain/ebf33317555336aac15253591ebfc0c0f564bdca..a0d3cf4aa8ffa76ed22a67efa4ea156eb2ef9f68:/src/dolphinview.cpp diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index 54294161d..606c5202d 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -156,7 +156,7 @@ DolphinView::DolphinView(QWidget* parent, this, SLOT(emitSelectionChangedSignal())); applyViewProperties(); - m_topLayout->addWidget(m_viewAccessor.itemView()); + m_topLayout->addWidget(m_viewAccessor.layoutTarget()); } DolphinView::~DolphinView() @@ -222,6 +222,10 @@ void DolphinView::setMode(Mode mode) const int oldZoomLevel = m_controller->zoomLevel(); m_mode = mode; + // remember the currently selected items, so that they will + // be restored after reloading the directory + m_selectedItems = selectedItems(); + deleteView(); const KUrl viewPropsUrl = rootUrl(); @@ -248,9 +252,7 @@ void DolphinView::setMode(Mode mode) emit modeChanged(); updateZoomLevel(oldZoomLevel); - if (m_showPreview) { - loadDirectory(viewPropsUrl); - } + loadDirectory(viewPropsUrl); } DolphinView::Mode DolphinView::mode() const @@ -288,19 +290,26 @@ bool DolphinView::supportsCategorizedSorting() const bool DolphinView::hasSelection() const { const QAbstractItemView* view = m_viewAccessor.itemView(); - return view && view->selectionModel()->hasSelection(); + return (view != 0) && view->selectionModel()->hasSelection(); +} + +void DolphinView::markUrlsAsSelected(const QList& urls) +{ + foreach (const KUrl& url, urls) { + KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url); + m_selectedItems.append(item); + } } KFileItemList DolphinView::selectedItems() const { + KFileItemList itemList; const QAbstractItemView* view = m_viewAccessor.itemView(); - - // Our view has a selection, we will map them back to the DolphinModel - // and then fill the KFileItemList. - Q_ASSERT((view != 0) && (view->selectionModel() != 0)); + if (view == 0) { + return itemList; + } const QItemSelection selection = m_viewAccessor.proxyModel()->mapSelectionToSource(view->selectionModel()->selection()); - KFileItemList itemList; const QModelIndexList indexList = selection.indexes(); foreach (const QModelIndex &index, indexList) { @@ -325,7 +334,12 @@ KUrl::List DolphinView::selectedUrls() const int DolphinView::selectedItemsCount() const { - return m_viewAccessor.itemView()->selectionModel()->selectedIndexes().count(); + const QAbstractItemView* view = m_viewAccessor.itemView(); + if (view == 0) { + return 0; + } + + return view->selectionModel()->selectedIndexes().count(); } QItemSelectionModel* DolphinView::selectionModel() const @@ -441,7 +455,7 @@ void DolphinView::reload() QByteArray viewState; QDataStream saveStream(&viewState, QIODevice::WriteOnly); saveState(saveStream); - m_selectedItems = selectedItems(); + m_selectedItems= selectedItems(); setUrl(url()); loadDirectory(url(), true); @@ -598,27 +612,6 @@ void DolphinView::clearSelection() m_viewAccessor.itemView()->clearSelection(); } -void DolphinView::changeSelection(const KFileItemList& selection) -{ - clearSelection(); - if (selection.isEmpty()) { - return; - } - const KUrl& baseUrl = url(); - KUrl url; - QItemSelection newSelection; - foreach(const KFileItem& item, selection) { - url = item.url().upUrl(); - if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) { - QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item)); - newSelection.select(index, index); - } - } - m_viewAccessor.itemView()->selectionModel()->select(newSelection, - QItemSelectionModel::ClearAndSelect - | QItemSelectionModel::Current); -} - void DolphinView::renameSelectedItems() { KFileItemList items = selectedItems(); @@ -918,11 +911,16 @@ void DolphinView::triggerItem(const KFileItem& item) emit itemTriggered(item); // caught by DolphinViewContainer or DolphinPart } -void DolphinView::emitDelayedSelectionChangedSignal() +void DolphinView::slotSelectionChanged(const QItemSelection& selected, const QItemSelection& deselected) { - // Invoke emitSelectionChangedSignal() with a delay of 300 ms. This assures - // that fast selection changes don't result in expensive operations to - // collect all file items for the signal (see DolphinView::selectedItems()). + const int count = selectedItemsCount(); + const bool selectionStateChanged = ((count > 0) && (selected.count() == count)) || + ((count == 0) && !deselected.isEmpty()); + + // If nothing has been selected before and something got selected (or if something + // was selected before and now nothing is selected) the selectionChangedSignal must + // be emitted asynchronously as fast as possible to update the edit-actions. + m_selectionChangedTimer->setInterval(selectionStateChanged ? 0 : 300); m_selectionChangedTimer->start(); } @@ -1227,7 +1225,19 @@ void DolphinView::slotLoadingCompleted() } if (!m_selectedItems.isEmpty()) { - changeSelection(m_selectedItems); + const KUrl& baseUrl = url(); + KUrl url; + QItemSelection newSelection; + foreach(const KFileItem& item, m_selectedItems) { + url = item.url().upUrl(); + if (baseUrl.equals(url, KUrl::CompareWithoutTrailingSlash)) { + QModelIndex index = m_viewAccessor.proxyModel()->mapFromSource(m_viewAccessor.dirModel()->indexForItem(item)); + newSelection.select(index, index); + } + } + m_viewAccessor.itemView()->selectionModel()->select(newSelection, + QItemSelectionModel::ClearAndSelect + | QItemSelectionModel::Current); m_selectedItems.clear(); } @@ -1356,8 +1366,6 @@ void DolphinView::createView() view->viewport()->installEventFilter(this); m_controller->setItemView(view); - connect(m_controller, SIGNAL(selectionChanged()), - this, SLOT(emitDelayedSelectionChangedSignal())); // When changing the view mode, the selection is lost due to reinstantiating // a new item view with a custom selection model. Pass the ownership of the @@ -1368,6 +1376,8 @@ void DolphinView::createView() m_selectionModel = view->selectionModel(); } m_selectionModel->setParent(this); + connect(view->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&, const QItemSelection&)), + this, SLOT(slotSelectionChanged(const QItemSelection&, const QItemSelection&))); connect(view->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(emitContentsMoved()));