From ab39a5952001cdb3d1b9ca693da7f8e246558fb8 Mon Sep 17 00:00:00 2001 From: Frank Reininghaus Date: Wed, 25 Nov 2009 08:46:04 +0000 Subject: [PATCH] Restore the "Edit->Selection" menu from Konqueror 3 for file management. It includes the actions "Select All" and "Select Items Matching a Pattern". This feature will be in KDE 4.4. FEATURE: 197536 svn path=/trunk/KDE/kdebase/apps/; revision=1053976 --- src/dolphinpart.cpp | 68 ++++++++++++++++++++++++++++++ src/dolphinpart.h | 20 +++++++++ src/dolphinpart.rc | 15 +++++-- src/dolphinview.cpp | 71 +++++++++++++++++--------------- src/dolphinview.h | 38 +++++++++-------- src/dolphinviewactionhandler.cpp | 2 +- 6 files changed, 158 insertions(+), 56 deletions(-) diff --git a/src/dolphinpart.cpp b/src/dolphinpart.cpp index a7e03deeb..35cc81503 100644 --- a/src/dolphinpart.cpp +++ b/src/dolphinpart.cpp @@ -44,6 +44,7 @@ #include #include #include +#include #include "settings/dolphinsettings.h" @@ -164,6 +165,26 @@ void DolphinPart::createActions() editMimeTypeAction->setText( i18nc("@action:inmenu Edit", "&Edit File Type..." ) ); connect(editMimeTypeAction, SIGNAL(triggered()), SLOT(slotEditMimeType())); + KAction* selectItemsMatching = actionCollection()->addAction("select_items_matching"); + selectItemsMatching->setText(i18nc("@action:inmenu Edit", "Select Items Matching...")); + selectItemsMatching->setShortcut(Qt::CTRL | Qt::Key_S); + connect(selectItemsMatching, SIGNAL(triggered()), this, SLOT(slotSelectItemsMatchingPattern())); + + KAction* unselectItemsMatching = actionCollection()->addAction("unselect_items_matching"); + unselectItemsMatching->setText(i18nc("@action:inmenu Edit", "Unselect Items Matching...")); + connect(unselectItemsMatching, SIGNAL(triggered()), this, SLOT(slotUnselectItemsMatchingPattern())); + + actionCollection()->addAction(KStandardAction::SelectAll, "select_all", m_view, SLOT(selectAll())); + + KAction* unselectAll = actionCollection()->addAction("unselect_all"); + unselectAll->setText(i18nc("@action:inmenu Edit", "Unselect All")); + connect(unselectAll, SIGNAL(triggered()), m_view, SLOT(clearSelection())); + + KAction* invertSelection = actionCollection()->addAction("invert_selection"); + invertSelection->setText(i18nc("@action:inmenu Edit", "Invert Selection")); + invertSelection->setShortcut(Qt::CTRL | Qt::SHIFT | Qt::Key_A); + connect(invertSelection, SIGNAL(triggered()), m_view, SLOT(invertSelection())); + // View menu: all done by DolphinViewActionHandler // Go menu @@ -493,6 +514,53 @@ void DolphinPart::slotEditMimeType() } } +void DolphinPart::slotSelectItemsMatchingPattern() +{ + openSelectionDialog(i18nc("@title:window", "Select"), + i18n("Select all items matching this pattern:"), + QItemSelectionModel::Select); +} + +void DolphinPart::slotUnselectItemsMatchingPattern() +{ + openSelectionDialog(i18nc("@title:window", "Unselect"), + i18n("Unselect all items matching this pattern:"), + QItemSelectionModel::Deselect); +} + +void DolphinPart::openSelectionDialog(const QString& title, const QString& text, QItemSelectionModel::SelectionFlags command) +{ + 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); + } +} + +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); diff --git a/src/dolphinpart.h b/src/dolphinpart.h index ee6e38417..afbf387cc 100644 --- a/src/dolphinpart.h +++ b/src/dolphinpart.h @@ -22,6 +22,9 @@ #include #include + +#include + class KNewMenu; class DolphinViewActionHandler; class QActionGroup; @@ -175,6 +178,18 @@ private Q_SLOTS: */ void slotEditMimeType(); + /** + * Connected to the "select_items_matching" action. + * Opens a dialog which permits to select all items matching a pattern like "*.jpg". + */ + void slotSelectItemsMatchingPattern(); + + /** + * Connected to the "unselect_items_matching" action. + * Opens a dialog which permits to unselect all items matching a pattern like "*.jpg". + */ + void slotUnselectItemsMatchingPattern(); + /** * Open a terminal window, starting with the current directory. */ @@ -205,6 +220,11 @@ private: const QString& text, const QString& url, QActionGroup* actionGroup); + void openSelectionDialog(const QString& title, const QString& text, + QItemSelectionModel::SelectionFlags command); + + QItemSelection childrenMatchingPattern(const QModelIndex& parent, const QRegExp& patternRegExp); + private: DolphinView* m_view; DolphinViewActionHandler* m_actionHandler; diff --git a/src/dolphinpart.rc b/src/dolphinpart.rc index d0b2cc634..7340c4dc1 100644 --- a/src/dolphinpart.rc +++ b/src/dolphinpart.rc @@ -1,5 +1,5 @@ - + &Edit @@ -9,9 +9,16 @@ - - - + + + Selection + + + + + + + &View diff --git a/src/dolphinview.cpp b/src/dolphinview.cpp index b805543fd..e057c950a 100644 --- a/src/dolphinview.cpp +++ b/src/dolphinview.cpp @@ -285,45 +285,12 @@ bool DolphinView::supportsCategorizedSorting() const return m_viewAccessor.supportsCategorizedSorting(); } -void DolphinView::selectAll() -{ - QAbstractItemView* view = m_viewAccessor.itemView(); - // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if - // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the - // selection instead of selecting all items. This is bypassed for KDE 4.0 - // by invoking clearSelection() first. - view->clearSelection(); - view->selectAll(); -} - -void DolphinView::invertSelection() -{ - QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel(); - const QAbstractItemModel* itemModel = selectionModel->model(); - - const QModelIndex topLeft = itemModel->index(0, 0); - const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1, - itemModel->columnCount() - 1); - - const QItemSelection selection(topLeft, bottomRight); - selectionModel->select(selection, QItemSelectionModel::Toggle); -} - bool DolphinView::hasSelection() const { const QAbstractItemView* view = m_viewAccessor.itemView(); return view && view->selectionModel()->hasSelection(); } -void DolphinView::clearSelection() -{ - QItemSelectionModel* selModel = m_viewAccessor.itemView()->selectionModel(); - const QModelIndex currentIndex = selModel->currentIndex(); - selModel->setCurrentIndex(currentIndex, QItemSelectionModel::Current | - QItemSelectionModel::Clear); - m_selectedItems.clear(); -} - KFileItemList DolphinView::selectedItems() const { const QAbstractItemView* view = m_viewAccessor.itemView(); @@ -361,6 +328,11 @@ int DolphinView::selectedItemsCount() const return m_viewAccessor.itemView()->selectionModel()->selectedIndexes().count(); } +QItemSelectionModel* DolphinView::selectionModel() const +{ + return m_viewAccessor.itemView()->selectionModel(); +} + void DolphinView::setContentsPosition(int x, int y) { QAbstractItemView* view = m_viewAccessor.itemView(); @@ -595,6 +567,39 @@ void DolphinView::setUrl(const KUrl& url) updateView(url, KUrl()); } +void DolphinView::selectAll() +{ + QAbstractItemView* view = m_viewAccessor.itemView(); + // TODO: there seems to be a bug in QAbstractItemView::selectAll(); if + // the Ctrl-key is pressed (e. g. for Ctrl+A), selectAll() inverts the + // selection instead of selecting all items. This is bypassed for KDE 4.0 + // by invoking clearSelection() first. + view->clearSelection(); + view->selectAll(); +} + +void DolphinView::invertSelection() +{ + QItemSelectionModel* selectionModel = m_viewAccessor.itemView()->selectionModel(); + const QAbstractItemModel* itemModel = selectionModel->model(); + + const QModelIndex topLeft = itemModel->index(0, 0); + const QModelIndex bottomRight = itemModel->index(itemModel->rowCount() - 1, + itemModel->columnCount() - 1); + + const QItemSelection selection(topLeft, bottomRight); + selectionModel->select(selection, QItemSelectionModel::Toggle); +} + +void DolphinView::clearSelection() +{ + QItemSelectionModel* selModel = m_viewAccessor.itemView()->selectionModel(); + const QModelIndex currentIndex = selModel->currentIndex(); + selModel->setCurrentIndex(currentIndex, QItemSelectionModel::Current | + QItemSelectionModel::Clear); + m_selectedItems.clear(); +} + void DolphinView::changeSelection(const KFileItemList& selection) { clearSelection(); diff --git a/src/dolphinview.h b/src/dolphinview.h index 0d3b15098..97054b754 100644 --- a/src/dolphinview.h +++ b/src/dolphinview.h @@ -181,24 +181,6 @@ public: */ bool supportsCategorizedSorting() const; - /** - * Selects all items. - * @see DolphinView::selectedItems() - */ - void selectAll(); - - /** - * Inverts the current selection: selected items get unselected, - * unselected items get selected. - * @see DolphinView::selectedItems() - */ - void invertSelection(); - - /** Returns true, if at least one item is selected. */ - bool hasSelection() const; - - void clearSelection(); - /** * Returns the selected items. The list is empty if no item has been * selected. @@ -219,6 +201,8 @@ public: */ int selectedItemsCount() const; + QItemSelectionModel* selectionModel() const; + /** * Sets the upper left position of the view content * to (x,y). The content of the view might be larger than the visible area @@ -381,6 +365,24 @@ public slots: */ void setUrl(const KUrl& url); + /** + * Selects all items. + * @see DolphinView::selectedItems() + */ + void selectAll(); + + /** + * Inverts the current selection: selected items get unselected, + * unselected items get selected. + * @see DolphinView::selectedItems() + */ + void invertSelection(); + + /** Returns true, if at least one item is selected. */ + bool hasSelection() const; + + void clearSelection(); + /** * Request of a selection change. The view will do its best to accommodate * the request, but it is not guaranteed that all items in \a selection diff --git a/src/dolphinviewactionhandler.cpp b/src/dolphinviewactionhandler.cpp index 5844fff81..cc89725c2 100644 --- a/src/dolphinviewactionhandler.cpp +++ b/src/dolphinviewactionhandler.cpp @@ -87,7 +87,7 @@ void DolphinViewActionHandler::createActions() newDirAction->setIcon(KIcon("folder-new")); connect(newDirAction, SIGNAL(triggered()), this, SIGNAL(createDirectory())); - // Edit menu + // File menu KAction* rename = m_actionCollection->addAction("rename"); rename->setText(i18nc("@action:inmenu File", "Rename...")); -- 2.47.3