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
This commit is contained in:
Elvis Stansvik 2009-10-15 20:25:47 +00:00
parent fd6072ae69
commit 4154fa8665
2 changed files with 78 additions and 2 deletions

View file

@ -23,6 +23,7 @@
#include <QtCore/QList>
#include <QtCore/QTimer>
#include <QtCore/QRegExp>
#include <QtGui/QApplication>
#include <QtGui/QContextMenuEvent>
#include <QtGui/QHBoxLayout>
@ -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<QTreeView *> 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<int> 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<int>::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") );

View file

@ -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<QTreeView *> &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() )
};
/**