Skip to content
Permalink
Browse files

[options search] fix search not available in option tree

  • Loading branch information
3nids committed Jan 27, 2018
1 parent 71bdda5 commit 6be8249bec03d00f41e82899c32bd95db4f91aca
Showing with 100 additions and 18 deletions.
  1. +92 −17 src/gui/qgsoptionsdialogbase.cpp
  2. +8 −1 src/gui/qgsoptionsdialogbase.h
@@ -32,6 +32,7 @@
#include <QStackedWidget>
#include <QTimer>
#include <QTreeView>
#include <QTreeWidget>
#include <QAbstractItemModel>

#include "qgsfilterlineedit.h"
@@ -406,52 +407,122 @@ QgsSearchHighlightOptionWidget::QgsSearchHighlightOptionWidget( QWidget *widget
}
}

QString styleSheet;
if ( qobject_cast<QLabel *>( widget ) )
{
mStyleSheet = QStringLiteral( "QLabel { background-color: yellow; color: blue;}" );
styleSheet = QStringLiteral( "QLabel { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QLabel *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
}
else if ( qobject_cast<QCheckBox *>( widget ) )
{
mStyleSheet = QStringLiteral( "QCheckBox { background-color: yellow; color: blue;}" );
styleSheet = QStringLiteral( "QCheckBox { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QCheckBox *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
}
else if ( qobject_cast<QAbstractButton *>( widget ) )
{
mStyleSheet = QStringLiteral( "QAbstractButton { background-color: yellow; color: blue;}" );
styleSheet = QStringLiteral( "QAbstractButton { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QAbstractButton *>( mWidget )->text().contains( searchText, Qt::CaseInsensitive );};
}
else if ( qobject_cast<QGroupBox *>( widget ) )
{
mStyleSheet = QStringLiteral( "QGroupBox::title { background-color: yellow; color: blue;}" );
styleSheet = QStringLiteral( "QGroupBox::title { background-color: yellow; color: blue;}" );
mTextFound = [ = ]( QString searchText ) {return qobject_cast<QGroupBox *>( mWidget )->title().contains( searchText, Qt::CaseInsensitive );};
}
if ( !styleSheet.isEmpty() )
{
styleSheet.prepend( "/*!search!*/" ).append( "/*!search!*/" );

mHighlight = [ = ]( QString searchText )
{
Q_UNUSED( searchText );
mWidget->setStyleSheet( mWidget->styleSheet() + styleSheet );
};

mReset = [ = ]()
{
if ( mWidget )
{
QString ss = mWidget->styleSheet();
ss.remove( styleSheet );
mWidget->setStyleSheet( ss );
}
};
}
else if ( qobject_cast<QTreeView *>( widget ) )
{
// TODO - style individual matching items
mTextFound = [ = ]( QString searchText )
{
QTreeView *tree = qobject_cast<QTreeView *>( mWidget );
if ( !tree )
QTreeView *treeView = qobject_cast<QTreeView *>( mWidget );
if ( !treeView )
return false;
QModelIndexList hits = tree->model()->match( tree->model()->index( 0, 0 ), Qt::DisplayRole, searchText, 1, Qt::MatchContains | Qt::MatchRecursive );
QModelIndexList hits = treeView->model()->match( treeView->model()->index( 0, 0 ), Qt::DisplayRole, searchText, 1, Qt::MatchContains | Qt::MatchRecursive );
return !hits.isEmpty();
};

if ( qobject_cast<QTreeWidget *>( widget ) )
{
mHighlight = [ = ]( QString searchText )
{
QTreeWidget *treeWidget = qobject_cast<QTreeWidget *>( widget );
if ( treeWidget )
{
QList<QTreeWidgetItem *> items = treeWidget->findItems( searchText, Qt::MatchContains | Qt::MatchRecursive, 0 );
mChangedStyle = items.count() ? true : false;
mTreeInitialBackground.clear();
mTreeInitialExpand.clear();
for ( QTreeWidgetItem *item : items )
{
mTreeInitialBackground.insert( item, item->background( 0 ) );
item->setBackground( 0, QBrush( QColor( Qt::yellow ) ) );

QTreeWidgetItem *parent = item;
while ( ( parent = parent->parent() ) )
{
if ( mTreeInitialExpand.contains( parent ) )
break;
mTreeInitialExpand.insert( parent, parent->isExpanded() );
parent->setExpanded( true );
}
}

}
};

mReset = [ = ]()
{
for ( QTreeWidgetItem *item : mTreeInitialExpand.keys() )
{
if ( item )
{
item->setExpanded( mTreeInitialExpand.value( item ) );
}
}
for ( QTreeWidgetItem *item : mTreeInitialBackground.keys() )
{
if ( item )
{
item->setBackground( 0, mTreeInitialBackground.value( item ) );
}
}
mTreeInitialBackground.clear();
mTreeInitialExpand.clear();
};
}
}
else
{
mValid = false;
}

if ( mValid )
{
mStyleSheet.prepend( "/*!search!*/" ).append( "/*!search!*/" );
QgsDebugMsgLevel( mStyleSheet, 4 );
connect( mWidget, &QWidget::destroyed, this, &QgsSearchHighlightOptionWidget::widgetDestroyed );
}
}

bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText )
{
mSearchText = searchText;
bool found = false;
if ( !mWidget )
return found;
@@ -461,8 +532,13 @@ bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText
found = mTextFound( searchText );
}

if ( found && !mChangedStyle )
if ( found )
{
if ( mChangedStyle )
{
mReset();
mChangedStyle = false;
}
if ( !mWidget->isVisible() )
{
// show the widget to get initial stylesheet in case it's modified
@@ -474,7 +550,7 @@ bool QgsSearchHighlightOptionWidget::searchHighlight( const QString &searchText
}
else
{
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
mHighlight( searchText );
mChangedStyle = true;
}
}
@@ -493,7 +569,8 @@ bool QgsSearchHighlightOptionWidget::eventFilter( QObject *obj, QEvent *event )
// after the widget is shown
#if 1
mWidget->show();
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
mHighlight( mSearchText );
mChangedStyle = true;
return true;
#else
QTimer::singleShot( 500, this, [ = ]
@@ -512,12 +589,10 @@ void QgsSearchHighlightOptionWidget::reset()
{
if ( mChangedStyle )
{
QString ss = mWidget->styleSheet();
ss.remove( mStyleSheet );
mWidget->setStyleSheet( ss );
mReset();
mChangedStyle = false;
}
else if ( mInstalledFilter )
if ( mInstalledFilter )
{
mWidget->removeEventFilter( this );
mInstalledFilter = false;
@@ -26,6 +26,7 @@
#include <QDialog>
#include <QPointer>
#include <QStyledItemDelegate>
#include <QMap>


class QDialogButtonBox;
@@ -36,6 +37,7 @@ class QPainter;
class QStackedWidget;
class QStyleOptionViewItem;
class QSplitter;
class QTreeWidgetItem;

class QgsFilterLineEdit;

@@ -87,10 +89,15 @@ class GUI_EXPORT QgsSearchHighlightOptionWidget : public QObject

private:
QPointer< QWidget > mWidget;
QString mStyleSheet;
QString mSearchText = QString();
// a map to save the tree state (backouground, expanded) before highlighting items
QMap<QTreeWidgetItem *, QBrush> mTreeInitialBackground = QMap<QTreeWidgetItem *, QBrush>();
QMap<QTreeWidgetItem *, bool> mTreeInitialExpand = QMap<QTreeWidgetItem *, bool>();
bool mValid = true;
bool mChangedStyle = false;
std::function < bool( QString )> mTextFound = []( QString searchText ) {Q_UNUSED( searchText ); return false;};
std::function < void( QString )> mHighlight = []( QString searchText ) {Q_UNUSED( searchText );};
std::function < void()> mReset = []() {};
bool mInstalledFilter = false;
};

0 comments on commit 6be8249

Please sign in to comment.
You can’t perform that action at this time.