From: Peter Penz Date: Thu, 4 Nov 2010 17:27:11 +0000 (+0000) Subject: Interface cleanup: The DolphinView should not expose QAbstractItemView-specific thing... X-Git-Url: https://cloud.milkyroute.net/gitweb/dolphin.git/commitdiff_plain/7e311509a4ac89ebe95f3d81928fe78a9f398aa9 Interface cleanup: The DolphinView should not expose QAbstractItemView-specific things like QItemSelectionModel. Just providing interfaces for KFileItem and KUrl will make it easier in future to change the view-implementations internally to QML-specific itemviews or whatever. svn path=/trunk/KDE/kdebase/apps/; revision=1193113 --- diff --git a/src/dolphinpart.cpp b/src/dolphinpart.cpp index 0691791a5..3d0748f84 100644 --- a/src/dolphinpart.cpp +++ b/src/dolphinpart.cpp @@ -480,49 +480,27 @@ void DolphinPart::slotSelectItemsMatchingPattern() { openSelectionDialog(i18nc("@title:window", "Select"), i18n("Select all items matching this pattern:"), - QItemSelectionModel::Select); + true); } void DolphinPart::slotUnselectItemsMatchingPattern() { openSelectionDialog(i18nc("@title:window", "Unselect"), i18n("Unselect all items matching this pattern:"), - QItemSelectionModel::Deselect); + false); } -void DolphinPart::openSelectionDialog(const QString& title, const QString& text, QItemSelectionModel::SelectionFlags command) +void DolphinPart::openSelectionDialog(const QString& title, const QString& text, bool selectItems) { bool okClicked; QString pattern = KInputDialog::getText(title, text, "*", &okClicked, m_view); if (okClicked && !pattern.isEmpty()) { QRegExp patternRegExp(pattern, Qt::CaseSensitive, QRegExp::Wildcard); - QItemSelection matchingIndexes = childrenMatchingPattern(QModelIndex(), patternRegExp); - m_view->selectionModel()->select(matchingIndexes, command); + m_view->setItemSelectionEnabled(patternRegExp, selectItems); } } -QItemSelection DolphinPart::childrenMatchingPattern(const QModelIndex& parent, const QRegExp& patternRegExp) -{ - QItemSelection matchingIndexes; - int numRows = m_proxyModel->rowCount(parent); - - for (int row = 0; row < numRows; row++) { - QModelIndex index = m_proxyModel->index(row, 0, parent); - QModelIndex sourceIndex = m_proxyModel->mapToSource(index); - - if (sourceIndex.isValid() && patternRegExp.exactMatch(m_dolphinModel->data(sourceIndex).toString())) { - matchingIndexes += QItemSelectionRange(index); - } - - if (m_proxyModel->hasChildren(index)) { - matchingIndexes += childrenMatchingPattern(index, patternRegExp); - } - } - - return matchingIndexes; -} - void DolphinPart::setCurrentViewMode(const QString& viewModeName) { QAction* action = actionCollection()->action(viewModeName); @@ -669,7 +647,7 @@ KFileItemList DolphinPartFileInfoExtension::queryFor(KParts::FileInfoExtension:: return part()->view()->selectedItems(); break; case KParts::FileInfoExtension::AllItems: - return part()->view()->allItems(); + return part()->view()->items(); default: break; } diff --git a/src/dolphinpart.h b/src/dolphinpart.h index b0b01d8d8..a23e08697 100644 --- a/src/dolphinpart.h +++ b/src/dolphinpart.h @@ -225,9 +225,7 @@ private: QActionGroup* actionGroup); void openSelectionDialog(const QString& title, const QString& text, - QItemSelectionModel::SelectionFlags command); - - QItemSelection childrenMatchingPattern(const QModelIndex& parent, const QRegExp& patternRegExp); + bool selectItems); private: DolphinView* m_view; diff --git a/src/views/dolphinview.cpp b/src/views/dolphinview.cpp index 8ed0d993e..2c2dbc231 100644 --- a/src/views/dolphinview.cpp +++ b/src/views/dolphinview.cpp @@ -279,18 +279,9 @@ bool DolphinView::supportsCategorizedSorting() const return m_viewAccessor.supportsCategorizedSorting(); } -bool DolphinView::hasSelection() const +KFileItemList DolphinView::items() const { - const QAbstractItemView* view = m_viewAccessor.itemView(); - 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); - } + return m_viewAccessor.dirLister()->items(); } KFileItemList DolphinView::selectedItems() const @@ -324,9 +315,21 @@ int DolphinView::selectedItemsCount() const return view->selectionModel()->selectedIndexes().count(); } -QItemSelectionModel* DolphinView::selectionModel() const +void DolphinView::markUrlsAsSelected(const QList& urls) +{ + foreach (const KUrl& url, urls) { + KFileItem item(KFileItem::Unknown, KFileItem::Unknown, url); + m_selectedItems.append(item); + } +} + +void DolphinView::setItemSelectionEnabled(const QRegExp& pattern, bool enabled) { - return m_viewAccessor.itemView()->selectionModel(); + const QItemSelection matchingIndexes = childrenMatchingPattern(QModelIndex(), pattern); + const QItemSelectionModel::SelectionFlags command = enabled + ? QItemSelectionModel::Select + : QItemSelectionModel::Deselect; + m_viewAccessor.itemView()->selectionModel()->select(matchingIndexes, command); } void DolphinView::setZoomLevel(int level) @@ -948,6 +951,12 @@ void DolphinView::saveState(QDataStream& stream) stream << m_viewAccessor.expandedUrls(); } +bool DolphinView::hasSelection() const +{ + const QAbstractItemView* view = m_viewAccessor.itemView(); + return (view != 0) && view->selectionModel()->hasSelection(); +} + void DolphinView::observeCreatedItem(const KUrl& url) { m_createdItemUrl = url; @@ -1248,6 +1257,30 @@ void DolphinView::addNewFileNames(const QMimeData* mimeData) } } +QItemSelection DolphinView::childrenMatchingPattern(const QModelIndex& parent, const QRegExp& pattern) const +{ + QItemSelection matchingIndexes; + const DolphinSortFilterProxyModel* proxyModel = m_viewAccessor.proxyModel(); + const DolphinModel* dolphinModel = m_viewAccessor.dirModel(); + + const int rowCount = proxyModel->rowCount(parent); + + for (int row = 0; row < rowCount; ++row) { + QModelIndex index = proxyModel->index(row, 0, parent); + QModelIndex sourceIndex = proxyModel->mapToSource(index); + + if (sourceIndex.isValid() && pattern.exactMatch(dolphinModel->data(sourceIndex).toString())) { + matchingIndexes += QItemSelectionRange(index); + } + + if (proxyModel->hasChildren(index)) { + matchingIndexes += childrenMatchingPattern(index, pattern); + } + } + + return matchingIndexes; +} + DolphinView::ViewAccessor::ViewAccessor(DolphinSortFilterProxyModel* proxyModel) : m_iconsView(0), m_detailsView(0), @@ -1458,9 +1491,4 @@ void DolphinView::restoreContentsPosition() } } -KFileItemList DolphinView::allItems() const -{ - return m_viewAccessor.dirLister()->items(); -} - #include "dolphinview.moc" diff --git a/src/views/dolphinview.h b/src/views/dolphinview.h index a06105cf3..5fcf1ab4d 100644 --- a/src/views/dolphinview.h +++ b/src/views/dolphinview.h @@ -43,6 +43,7 @@ typedef KIO::FileUndoManager::CommandType CommandType; class DolphinColumnViewContainer; class DolphinDetailsView; +class DolphinDetailsViewExpander; class DolphinIconsView; class DolphinModel; class DolphinSortFilterProxyModel; @@ -53,7 +54,7 @@ class KDirLister; class KUrl; class ViewModeController; class ViewProperties; -class DolphinDetailsViewExpander; +class QRegExp; /** * @short Represents a view for the directory content. @@ -181,12 +182,9 @@ public: bool supportsCategorizedSorting() const; /** - * Marks the items indicated by \p urls to get selected after the - * directory DolphinView::url() has been loaded. Note that nothing - * gets selected if no loading of a directory has been triggered - * by DolphinView::setUrl() or DolphinView::reload(). + * Returns the items of the view. */ - void markUrlsAsSelected(const QList& urls); + KFileItemList items() const; /** * Returns the selected items. The list is empty if no item has been @@ -201,7 +199,19 @@ public: */ int selectedItemsCount() const; - QItemSelectionModel* selectionModel() const; + /** + * Marks the items indicated by \p urls to get selected after the + * directory DolphinView::url() has been loaded. Note that nothing + * gets selected if no loading of a directory has been triggered + * by DolphinView::setUrl() or DolphinView::reload(). + */ + void markUrlsAsSelected(const QList& urls); + + /** + * All items that match to the pattern \a pattern will get selected + * if \a enabled is true and deselected if \a enabled is false. + */ + void setItemSelectionEnabled(const QRegExp& pattern, bool enabled); /** * Sets the zoom level to \a level. It is assured that the used @@ -324,10 +334,8 @@ public: */ void saveState(QDataStream& stream); - /** - * Returns all the items in the current view. - */ - KFileItemList allItems() const; + /** Returns true, if at least one item is selected. */ + bool hasSelection() const; public slots: /** @@ -349,9 +357,6 @@ public slots: */ void invertSelection(); - /** Returns true, if at least one item is selected. */ - bool hasSelection() const; - void clearSelection(); /** @@ -712,6 +717,12 @@ private: */ void addNewFileNames(const QMimeData* mimeData); + /** + * Helper method for DolphinView::setItemSelectionEnabled(): Returns the selection for + * all items of \p parent that match with the regular expression defined by \p pattern. + */ + QItemSelection childrenMatchingPattern(const QModelIndex& parent, const QRegExp& pattern) const; + private: /** * Abstracts the access to the different view implementations