2007-09-09 15:50:00 +00:00
/*
Copyright ( c ) 2003 Scott Wheeler < wheeler @ kde . org >
Copyright ( c ) 2005 Rafal Rzepecki < divide @ users . sourceforge . net >
Copyright ( c ) 2006 Hamish Rodda < rodda @ kde . org >
Copyright 2007 Pino Toscano < pino @ kde . org >
This library is free software ; you can redistribute it and / or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation .
This library is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the GNU
Library General Public License for more details .
You should have received a copy of the GNU Library General Public License
along with this library ; see the file COPYING . LIB . If not , write to
the Free Software Foundation , Inc . , 51 Franklin Street , Fifth Floor ,
Boston , MA 02110 - 1301 , USA .
*/
# include "ktreeviewsearchline.h"
2018-08-31 09:23:45 +00:00
# include <QApplication>
# include <QContextMenuEvent>
# include <QHBoxLayout>
# include <QHeaderView>
# include <QLabel>
2020-07-10 22:15:05 +00:00
# include <QList>
2018-08-31 09:23:45 +00:00
# include <QMenu>
2020-07-10 22:15:05 +00:00
# include <QRegularExpression>
# include <QTimer>
2018-08-31 09:23:45 +00:00
# include <QToolButton>
# include <QTreeView>
2014-10-14 05:37:01 +00:00
# include <KLocalizedString>
2020-07-08 11:54:37 +00:00
# include <KToolBar>
2020-07-10 22:15:05 +00:00
# include <QDebug>
2007-09-09 15:50:00 +00:00
class KTreeViewSearchLine : : Private
{
2020-07-10 22:15:05 +00:00
public :
Private ( KTreeViewSearchLine * _parent )
: parent ( _parent )
, treeView ( nullptr )
, caseSensitive ( Qt : : CaseInsensitive )
, regularExpression ( false )
, activeSearch ( false )
, queuedSearches ( 0 )
2007-09-09 15:50:00 +00:00
{
}
KTreeViewSearchLine * parent ;
2020-07-10 22:15:05 +00:00
QTreeView * treeView ;
2007-09-09 15:50:00 +00:00
Qt : : CaseSensitivity caseSensitive ;
2009-10-15 20:25:47 +00:00
bool regularExpression ;
2007-09-09 15:50:00 +00:00
bool activeSearch ;
QString search ;
int queuedSearches ;
2020-07-10 22:15:05 +00:00
void rowsInserted ( const QModelIndex & parent , int start , int end ) const ;
void treeViewDeleted ( QObject * object ) ;
2009-10-15 20:25:47 +00:00
void slotCaseSensitive ( ) ;
void slotRegularExpression ( ) ;
2007-09-09 15:50:00 +00:00
void checkItemParentsNotVisible ( QTreeView * treeView ) ;
2020-05-23 10:11:48 +00:00
bool filterItems ( QTreeView * treeView , const QModelIndex & index ) ;
2007-09-09 15:50:00 +00:00
} ;
////////////////////////////////////////////////////////////////////////////////
// private slots
////////////////////////////////////////////////////////////////////////////////
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : Private : : rowsInserted ( const QModelIndex & parentIndex , int start , int end ) const
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
QAbstractItemModel * model = qobject_cast < QAbstractItemModel * > ( parent - > sender ( ) ) ;
if ( ! model )
return ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
QTreeView * widget = nullptr ;
if ( treeView - > model ( ) = = model ) {
widget = treeView ;
}
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
if ( ! widget )
return ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
for ( int i = start ; i < = end ; + + i ) {
widget - > setRowHidden ( i , parentIndex , ! parent - > itemMatches ( parentIndex , i , parent - > text ( ) ) ) ;
}
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : Private : : treeViewDeleted ( QObject * object )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
if ( object = = treeView ) {
treeView = nullptr ;
parent - > setEnabled ( false ) ;
}
2007-09-09 15:50:00 +00:00
}
2009-10-15 20:25:47 +00:00
void KTreeViewSearchLine : : Private : : slotCaseSensitive ( )
{
2020-07-10 22:15:05 +00:00
if ( caseSensitive = = Qt : : CaseSensitive )
parent - > setCaseSensitivity ( Qt : : CaseInsensitive ) ;
else
parent - > setCaseSensitivity ( Qt : : CaseSensitive ) ;
2009-10-15 20:25:47 +00:00
2020-07-10 22:15:05 +00:00
parent - > updateSearch ( ) ;
2009-10-15 20:25:47 +00:00
}
void KTreeViewSearchLine : : Private : : slotRegularExpression ( )
{
2020-07-10 22:15:05 +00:00
if ( regularExpression )
parent - > setRegularExpression ( false ) ;
else
parent - > setRegularExpression ( true ) ;
2009-10-15 20:25:47 +00:00
2020-07-10 22:15:05 +00:00
parent - > updateSearch ( ) ;
2009-10-15 20:25:47 +00:00
}
2007-09-09 15:50:00 +00:00
////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////
2018-11-14 19:12:15 +00:00
/** Check whether \p item, its siblings and their descendants should be shown. Show or hide the items as necessary.
2007-09-09 15:50:00 +00:00
*
* \ p item The list view item to start showing / hiding items at . Typically , this is the first child of another item , or the
* the first child of the list view .
* \ return \ c true if an item which should be visible is found , \ c false if all items found should be hidden . If this function
* returns true and \ p highestHiddenParent was not 0 , highestHiddenParent will have been shown .
*/
2020-07-10 22:15:05 +00:00
bool KTreeViewSearchLine : : Private : : filterItems ( QTreeView * treeView , const QModelIndex & index )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
bool childMatch = false ;
const int rowcount = treeView - > model ( ) - > rowCount ( index ) ;
for ( int i = 0 ; i < rowcount ; + + i )
childMatch | = filterItems ( treeView , treeView - > model ( ) - > index ( i , 0 , index ) ) ;
// Should this item be shown? It should if any children should be, or if it matches.
const QModelIndex parentindex = index . parent ( ) ;
if ( childMatch | | parent - > itemMatches ( parentindex , index . row ( ) , search ) ) {
treeView - > setRowHidden ( index . row ( ) , parentindex , false ) ;
return true ;
}
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
treeView - > setRowHidden ( index . row ( ) , parentindex , true ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
return false ;
2007-09-09 15:50:00 +00:00
}
////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////
2020-07-10 22:15:05 +00:00
KTreeViewSearchLine : : KTreeViewSearchLine ( QWidget * parent , QTreeView * treeView )
: KLineEdit ( parent )
, d ( new Private ( this ) )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
connect ( this , & KTreeViewSearchLine : : textChanged , this , & KTreeViewSearchLine : : queueSearch ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
setClearButtonEnabled ( true ) ;
setTreeView ( treeView ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
if ( ! treeView ) {
setEnabled ( false ) ;
}
2007-09-09 15:50:00 +00:00
}
KTreeViewSearchLine : : ~ KTreeViewSearchLine ( )
{
2020-07-10 22:15:05 +00:00
delete d ;
2007-09-09 15:50:00 +00:00
}
Qt : : CaseSensitivity KTreeViewSearchLine : : caseSensitivity ( ) const
{
2020-07-10 22:15:05 +00:00
return d - > caseSensitive ;
2007-09-09 15:50:00 +00:00
}
2009-10-15 20:25:47 +00:00
bool KTreeViewSearchLine : : regularExpression ( ) const
{
2020-07-10 22:15:05 +00:00
return d - > regularExpression ;
2009-10-15 20:25:47 +00:00
}
2007-09-09 15:50:00 +00:00
QTreeView * KTreeViewSearchLine : : treeView ( ) const
{
2020-07-10 22:15:05 +00:00
return d - > treeView ;
2007-09-09 15:50:00 +00:00
}
////////////////////////////////////////////////////////////////////////////////
// public slots
////////////////////////////////////////////////////////////////////////////////
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : updateSearch ( const QString & pattern )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
d - > search = pattern . isNull ( ) ? text ( ) : pattern ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
updateSearch ( d - > treeView ) ;
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : updateSearch ( QTreeView * treeView )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
if ( ! treeView | | ! treeView - > model ( ) - > rowCount ( ) )
return ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
// If there's a selected item that is visible, make sure that it's visible
// when the search changes too (assuming that it still matches).
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
QModelIndex currentIndex = treeView - > currentIndex ( ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
bool wasUpdateEnabled = treeView - > updatesEnabled ( ) ;
treeView - > setUpdatesEnabled ( false ) ;
d - > filterItems ( treeView , treeView - > rootIndex ( ) ) ;
treeView - > setUpdatesEnabled ( wasUpdateEnabled ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
if ( currentIndex . isValid ( ) )
treeView - > scrollTo ( currentIndex ) ;
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : setCaseSensitivity ( Qt : : CaseSensitivity caseSensitivity )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
if ( d - > caseSensitive ! = caseSensitivity ) {
d - > caseSensitive = caseSensitivity ;
updateSearch ( ) ;
emit searchOptionsChanged ( ) ;
}
2009-10-15 20:25:47 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : setRegularExpression ( bool value )
2009-10-15 20:25:47 +00:00
{
2020-07-10 22:15:05 +00:00
if ( d - > regularExpression ! = value ) {
d - > regularExpression = value ;
updateSearch ( ) ;
emit searchOptionsChanged ( ) ;
}
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : setTreeView ( QTreeView * treeView )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
disconnectTreeView ( d - > treeView ) ;
d - > treeView = treeView ;
connectTreeView ( treeView ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
setEnabled ( treeView ! = nullptr ) ;
2007-09-09 15:50:00 +00:00
}
////////////////////////////////////////////////////////////////////////////////
// protected members
////////////////////////////////////////////////////////////////////////////////
2020-07-10 22:15:05 +00:00
bool KTreeViewSearchLine : : itemMatches ( const QModelIndex & parentIndex , int row , const QString & pattern ) const
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
if ( pattern . isEmpty ( ) )
return true ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
if ( ! parentIndex . isValid ( ) & & parentIndex ! = d - > treeView - > rootIndex ( ) )
return false ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
// Construct a regular expression object with the right options.
QRegularExpression re ;
2020-05-23 10:11:48 +00:00
if ( d - > regularExpression ) {
2020-07-10 22:15:05 +00:00
re . setPattern ( pattern ) ;
re . setPatternOptions ( d - > caseSensitive ? QRegularExpression : : NoPatternOption : QRegularExpression : : CaseInsensitiveOption ) ;
}
// If the search column list is populated, search just the columns
// specified. If it is empty default to searching all of the columns.
QAbstractItemModel * model = d - > treeView - > model ( ) ;
const int columncount = model - > columnCount ( parentIndex ) ;
for ( int i = 0 ; i < columncount ; + + i ) {
const QString str = model - > data ( model - > index ( row , i , parentIndex ) , Qt : : DisplayRole ) . toString ( ) ;
if ( d - > regularExpression ) {
return str . contains ( re ) ;
} else {
return str . contains ( pattern , d - > caseSensitive ? Qt : : CaseSensitive : Qt : : CaseInsensitive ) ;
}
2020-01-26 11:40:33 +00:00
}
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
return false ;
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : contextMenuEvent ( QContextMenuEvent * event )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
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 , [ this ] { d - > 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 , [ this ] { d - > slotRegularExpression ( ) ; } ) ;
regularExpressionAction - > setCheckable ( true ) ;
regularExpressionAction - > setChecked ( d - > regularExpression ) ;
popup - > exec ( event - > globalPos ( ) ) ;
delete popup ;
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : connectTreeView ( QTreeView * treeView )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
if ( treeView ) {
connect ( treeView , & QTreeView : : destroyed , this , & KTreeViewSearchLine : : treeViewDeleted ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
connect ( treeView - > model ( ) , & QAbstractItemModel : : rowsInserted , this , & KTreeViewSearchLine : : rowsInserted ) ;
}
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : disconnectTreeView ( QTreeView * treeView )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
if ( treeView ) {
disconnect ( treeView , & QTreeView : : destroyed , this , & KTreeViewSearchLine : : treeViewDeleted ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
disconnect ( treeView - > model ( ) , & QAbstractItemModel : : rowsInserted , this , & KTreeViewSearchLine : : rowsInserted ) ;
}
2007-09-09 15:50:00 +00:00
}
////////////////////////////////////////////////////////////////////////////////
// protected slots
////////////////////////////////////////////////////////////////////////////////
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : queueSearch ( const QString & search )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
d - > queuedSearches + + ;
d - > search = search ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
QTimer : : singleShot ( 200 , this , & KTreeViewSearchLine : : activateSearch ) ;
2007-09-09 15:50:00 +00:00
}
void KTreeViewSearchLine : : activateSearch ( )
{
2020-07-10 22:15:05 +00:00
- - ( d - > queuedSearches ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
if ( d - > queuedSearches = = 0 )
updateSearch ( d - > search ) ;
2007-09-09 15:50:00 +00:00
}
2019-12-19 22:18:31 +00:00
////////////////////////////////////////////////////////////////////////////////
// private functions
////////////////////////////////////////////////////////////////////////////////
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : rowsInserted ( const QModelIndex & parent , int start , int end ) const
2019-12-19 22:18:31 +00:00
{
d - > rowsInserted ( parent , start , end ) ;
}
2020-07-10 22:15:05 +00:00
void KTreeViewSearchLine : : treeViewDeleted ( QObject * treeView )
2019-12-19 22:18:31 +00:00
{
2020-07-10 22:15:05 +00:00
d - > treeViewDeleted ( treeView ) ;
2019-12-19 22:18:31 +00:00
}
2007-09-09 15:50:00 +00:00
////////////////////////////////////////////////////////////////////////////////
// KTreeViewSearchLineWidget
////////////////////////////////////////////////////////////////////////////////
class KTreeViewSearchLineWidget : : Private
{
2020-07-10 22:15:05 +00:00
public :
2007-09-09 15:50:00 +00:00
Private ( )
2020-07-10 22:15:05 +00:00
: treeView ( nullptr )
, searchLine ( nullptr )
2007-09-09 15:50:00 +00:00
{
}
QTreeView * treeView ;
KTreeViewSearchLine * searchLine ;
} ;
2020-07-10 22:15:05 +00:00
KTreeViewSearchLineWidget : : KTreeViewSearchLineWidget ( QWidget * parent , QTreeView * treeView )
: QWidget ( parent )
, d ( new Private )
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
d - > treeView = treeView ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
QTimer : : singleShot ( 0 , this , & KTreeViewSearchLineWidget : : createWidgets ) ;
2007-09-09 15:50:00 +00:00
}
KTreeViewSearchLineWidget : : ~ KTreeViewSearchLineWidget ( )
{
2020-07-10 22:15:05 +00:00
delete d ;
2007-09-09 15:50:00 +00:00
}
2020-07-10 22:15:05 +00:00
KTreeViewSearchLine * KTreeViewSearchLineWidget : : createSearchLine ( QTreeView * treeView ) const
2007-09-09 15:50:00 +00:00
{
2020-07-10 22:15:05 +00:00
return new KTreeViewSearchLine ( const_cast < KTreeViewSearchLineWidget * > ( this ) , treeView ) ;
2007-09-09 15:50:00 +00:00
}
void KTreeViewSearchLineWidget : : createWidgets ( )
{
2020-07-10 22:15:05 +00:00
QLabel * label = new QLabel ( i18n ( " S&earch: " ) , this ) ;
label - > setObjectName ( QStringLiteral ( " kde toolbar widget " ) ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
searchLine ( ) - > show ( ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
label - > setBuddy ( d - > searchLine ) ;
label - > show ( ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
QHBoxLayout * layout = new QHBoxLayout ( this ) ;
layout - > setSpacing ( 5 ) ;
layout - > setContentsMargins ( 0 , 0 , 0 , 0 ) ;
layout - > addWidget ( label ) ;
layout - > addWidget ( d - > searchLine ) ;
2007-09-09 15:50:00 +00:00
}
KTreeViewSearchLine * KTreeViewSearchLineWidget : : searchLine ( ) const
{
2020-07-10 22:15:05 +00:00
if ( ! d - > searchLine )
d - > searchLine = createSearchLine ( d - > treeView ) ;
2007-09-09 15:50:00 +00:00
2020-07-10 22:15:05 +00:00
return d - > searchLine ;
2007-09-09 15:50:00 +00:00
}
2014-08-08 22:00:07 +00:00
# include "moc_ktreeviewsearchline.cpp"