From 4154fa86655209dd5c72d6a38ea6664ad5734f35 Mon Sep 17 00:00:00 2001 From: Elvis Stansvik Date: Thu, 15 Oct 2009 20:25:47 +0000 Subject: [PATCH] Support case sensitivity and regular expressions in the Contents and Reviews side panel. The search options are accessed using a context sub-menu in the search input field. Fixes bug 204787. I'm intentionally leaving out support for saving the current search options to the config, as pinotree said he was researching a more maintainable approach to this. BUG: 204787 svn path=/trunk/KDE/kdegraphics/okular/; revision=1035766 --- ui/ktreeviewsearchline.cpp | 58 ++++++++++++++++++++++++++++++++++++-- ui/ktreeviewsearchline.h | 22 +++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/ui/ktreeviewsearchline.cpp b/ui/ktreeviewsearchline.cpp index de3ec4f04..e26fd9f76 100644 --- a/ui/ktreeviewsearchline.cpp +++ b/ui/ktreeviewsearchline.cpp @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -43,6 +44,7 @@ class KTreeViewSearchLine::Private Private( KTreeViewSearchLine *_parent ) : parent( _parent ), caseSensitive( Qt::CaseInsensitive ), + regularExpression( false ), activeSearch( false ), keepParentsVisible( true ), canChooseColumns( true ), @@ -53,6 +55,7 @@ class KTreeViewSearchLine::Private KTreeViewSearchLine *parent; QList treeViews; Qt::CaseSensitivity caseSensitive; + bool regularExpression; bool activeSearch; bool keepParentsVisible; bool canChooseColumns; @@ -64,6 +67,8 @@ class KTreeViewSearchLine::Private void treeViewDeleted( QObject *treeView ); void slotColumnActivated(QAction* action); void slotAllVisibleColumns(); + void slotCaseSensitive(); + void slotRegularExpression(); void checkColumns(); void checkItemParentsNotVisible(QTreeView *treeView); @@ -150,6 +155,26 @@ void KTreeViewSearchLine::Private::slotAllVisibleColumns() parent->updateSearch(); } +void KTreeViewSearchLine::Private::slotCaseSensitive() +{ + if ( caseSensitive == Qt::CaseSensitive) + parent->setCaseSensitivity( Qt::CaseInsensitive ); + else + parent->setCaseSensitivity( Qt::CaseSensitive ); + + parent->updateSearch(); +} + +void KTreeViewSearchLine::Private::slotRegularExpression() +{ + if ( regularExpression ) + parent->setRegularExpression( false ); + else + parent->setRegularExpression( true ); + + parent->updateSearch(); +} + //////////////////////////////////////////////////////////////////////////////// // private methods //////////////////////////////////////////////////////////////////////////////// @@ -241,6 +266,11 @@ Qt::CaseSensitivity KTreeViewSearchLine::caseSensitivity() const return d->caseSensitive; } +bool KTreeViewSearchLine::regularExpression() const +{ + return d->regularExpression; +} + QList KTreeViewSearchLine::searchColumns() const { if ( d->canChooseColumns ) @@ -337,6 +367,16 @@ void KTreeViewSearchLine::setCaseSensitivity( Qt::CaseSensitivity caseSensitive if ( d->caseSensitive != caseSensitive ) { d->caseSensitive = caseSensitive; updateSearch(); + emit searchOptionsChanged(); + } +} + +void KTreeViewSearchLine::setRegularExpression( bool value ) +{ + if ( d->regularExpression != value ) { + d->regularExpression = value; + updateSearch(); + emit searchOptionsChanged(); } } @@ -387,6 +427,11 @@ bool KTreeViewSearchLine::itemMatches( const QModelIndex &index, int row, const if ( !index.isValid() ) return false; + // Contruct a regular expression object with the right options. + QRegExp expression = QRegExp( pattern, + d->caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive, + d->regularExpression ? QRegExp::RegExp : QRegExp::FixedString ); + // If the search column list is populated, search just the columns // specifified. If it is empty default to searching all of the columns. @@ -395,12 +440,12 @@ bool KTreeViewSearchLine::itemMatches( const QModelIndex &index, int row, const QList::ConstIterator it = d->searchColumns.constBegin(); for ( ; it != d->searchColumns.constEnd(); ++it ) { if ( *it < columncount && - index.child( row, *it ).data( Qt::DisplayRole ).toString().indexOf( pattern, 0, d->caseSensitive ) >= 0 ) + expression.indexIn( index.child( row, *it ).data( Qt::DisplayRole ).toString() ) >= 0 ) return true; } } else { for ( int i = 0; i < columncount; ++i) { - if ( index.child( row, i ).data( Qt::DisplayRole ).toString().indexOf( pattern, 0, d->caseSensitive ) >= 0 ) + if ( expression.indexIn( index.child( row, i ).data( Qt::DisplayRole ).toString() ) >= 0 ) return true; } } @@ -412,6 +457,15 @@ void KTreeViewSearchLine::contextMenuEvent( QContextMenuEvent *event ) { QMenu *popup = KLineEdit::createStandardContextMenu(); + popup->addSeparator(); + QMenu *optionsSubMenu = popup->addMenu( i18n("Search Options") ); + QAction* caseSensitiveAction = optionsSubMenu->addAction( i18nc("Enable case sensitive search in the side navigation panels", "Case Sensitive"), this, SLOT( slotCaseSensitive() ) ); + caseSensitiveAction->setCheckable( true ); + caseSensitiveAction->setChecked( d->caseSensitive ); + QAction* regularExpressionAction = optionsSubMenu->addAction( i18nc("Enable regular expression search in the side navigation panels", "Regular Expression"), this, SLOT( slotRegularExpression() ) ); + regularExpressionAction->setCheckable( true ); + regularExpressionAction->setChecked( d->regularExpression ); + if ( d->canChooseColumns ) { popup->addSeparator(); QMenu *subMenu = popup->addMenu( i18n("Search Columns") ); diff --git a/ui/ktreeviewsearchline.h b/ui/ktreeviewsearchline.h index b3f9b386c..fbf9f215e 100644 --- a/ui/ktreeviewsearchline.h +++ b/ui/ktreeviewsearchline.h @@ -75,6 +75,13 @@ class KTreeViewSearchLine : public KLineEdit */ Qt::CaseSensitivity caseSensitivity() const; + /** + * Returns true if the search is a regular expression search. This defaults to false. + * + * @see setRegularExpression() + */ + bool regularExpression() const; + /** * Returns the current list of columns that will be searched. If the * returned list is empty all visible columns will be searched. @@ -138,6 +145,13 @@ class KTreeViewSearchLine : public KLineEdit */ void setCaseSensitivity( Qt::CaseSensitivity caseSensitivity ); + /** + * Make the search a regular expression search or not. + * + * @see regularExpression() + */ + void setRegularExpression( bool value ); + /** * When a search is active on a list that's organized into a tree view if * a parent or ancesestor of an item is does not match the search then it @@ -179,6 +193,12 @@ class KTreeViewSearchLine : public KLineEdit */ void setTreeViews( const QList &treeViews ); + Q_SIGNALS: + /** + * This signal is emitted when search options have been changed. It is emitted so + * that users of this class can choose to save the search options to the settings. + */ + void searchOptionsChanged(); protected: /** @@ -260,6 +280,8 @@ class KTreeViewSearchLine : public KLineEdit Q_PRIVATE_SLOT( d, void treeViewDeleted( QObject* ) ) Q_PRIVATE_SLOT( d, void slotColumnActivated( QAction* ) ) Q_PRIVATE_SLOT( d, void slotAllVisibleColumns() ) + Q_PRIVATE_SLOT( d, void slotCaseSensitive() ) + Q_PRIVATE_SLOT( d, void slotRegularExpression() ) }; /**