Skip to content

Commit

Permalink
use stylesheet rather than palette
Browse files Browse the repository at this point in the history
add doxygen comments and fix sip bindings
  • Loading branch information
3nids committed Feb 11, 2017
1 parent a136995 commit 4cba478
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 136 deletions.
30 changes: 30 additions & 0 deletions python/gui/qgsoptionsdialogbase.sip
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
class QgsSearchHighlightOptionWidget : QObject
{
%TypeHeaderCode
#include <qgsoptionsdialogbase.h>
%End
public:
explicit QgsSearchHighlightOptionWidget( QWidget* widget = 0 );
~QgsSearchHighlightOptionWidget();

bool isValid();

bool searchHighlight( QString searchText );

void reset();

QWidget* widget();
};



class QgsOptionsDialogBase : QDialog
{
%TypeHeaderCode
Expand Down Expand Up @@ -32,6 +52,14 @@ class QgsOptionsDialogBase : QDialog
*/
bool iconOnly();

public slots:

/**
* searchText searches for a text in all the pages of the stacked widget and highlight the results
* @param text the text to search
*/
void searchText( QString text );

protected slots:
void updateOptionsListVerticalTabs();
void optionsStackedWidget_CurrentChanged( int indx );
Expand All @@ -43,4 +71,6 @@ class QgsOptionsDialogBase : QDialog
void paintEvent( QPaintEvent* e );

virtual void updateWindowTitle();

void registerTextSearchWidgets();
};
2 changes: 1 addition & 1 deletion src/gui/qgscollapsiblegroupbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ void QgsCollapsibleGroupBoxBasic::updateStyle()
ss += QLatin1String( " background-color: rgba(0,0,0,0)" );
}
ss += '}';
setStyleSheet( ss );
setStyleSheet( styleSheet() + ss );

// clear toolbutton default background and border and apply offset
QString ssd;
Expand Down
111 changes: 60 additions & 51 deletions src/gui/qgsoptionsdialogbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <QCheckBox>
#include <QDialog>
#include <QDialogButtonBox>
#include <QEvent>
#include <QGroupBox>
#include <QLabel>
#include <QLayout>
Expand Down Expand Up @@ -218,24 +219,24 @@ void QgsOptionsDialogBase::searchText( QString text )
if ( !mOptStackedWidget )
return;

mOptStackedWidget->show();
if ( mOptButtonBox )
if ( mOptStackedWidget->isHidden() )
mOptStackedWidget->show();
if ( mOptButtonBox && mOptButtonBox->isHidden() )
mOptButtonBox->show();
// hide all page if text has to be search, show them all otherwise
for ( int r = 0; r < mOptListWidget->count(); ++r )
{
mOptListWidget->setRowHidden( r, !text.isEmpty() );
}

QPair< QgsSearchHighlightOptionWidget, int > rsw;
Q_FOREACH ( rsw, mRegisteredSearchWidgets )
for ( QPair< QgsSearchHighlightOptionWidget*, int > rsw : mRegisteredSearchWidgets )
{
rsw.first.reset();
if ( !text.isEmpty() && rsw.first.searchHighlight( text ) )
rsw.first->reset();
if ( !text.isEmpty() && rsw.first->searchHighlight( text ) )
{
QgsDebugMsg( QString( "Found %1 in %2 (tab: %3)" )
.arg( text )
.arg( rsw.first.widgetName() )
.arg( rsw.first->isValid() ? rsw.first->widget()->objectName() : "no widget" )
.arg( mOptListWidget->item( rsw.second )->text() ) );
mOptListWidget->setRowHidden( rsw.second, false );
}
Expand All @@ -259,18 +260,18 @@ void QgsOptionsDialogBase::searchText( QString text )
}
}

void QgsOptionsDialogBase::registerTextSearch()
void QgsOptionsDialogBase::registerTextSearchWidgets()
{
mRegisteredSearchWidgets.clear();

for ( int i = 0; i < mOptStackedWidget->count(); i++ )
{
Q_FOREACH ( QWidget* w, mOptStackedWidget->widget( i )->findChildren<QWidget*>() )
{
QgsSearchHighlightOptionWidget shw = QgsSearchHighlightOptionWidget( w );
QgsDebugMsg( QString( "Registering: %1 %2" ).arg( w->objectName() ).arg( shw.isValid() ? "valid" : "invalid" ) );
if ( shw.isValid() )
QgsSearchHighlightOptionWidget* shw = new QgsSearchHighlightOptionWidget( w );
if ( shw->isValid() )
{
QgsDebugMsg( QString( "Registering: %1" ).arg( w->objectName() ) );
mRegisteredSearchWidgets.append( qMakePair( shw, i ) );
}
}
Expand All @@ -291,7 +292,7 @@ void QgsOptionsDialogBase::showEvent( QShowEvent* e )

if ( mSearchLineEdit )
{
registerTextSearch();
registerTextSearchWidgets();
}

QDialog::showEvent( e );
Expand Down Expand Up @@ -368,7 +369,14 @@ void QgsOptionsDialogBase::optionsStackedWidget_WidgetRemoved( int indx )
// will need to take item first, if widgets are set for item in future
delete mOptListWidget->item( indx );

registerTextSearch();
QList<QPair< QgsSearchHighlightOptionWidget*, int > >::iterator it = mRegisteredSearchWidgets.begin();
while ( it != mRegisteredSearchWidgets.end() )
{
if (( *it ).second == indx )
it = mRegisteredSearchWidgets.erase( it );
else
++it;
}
}

void QgsOptionsDialogBase::warnAboutMissingObjects()
Expand All @@ -383,49 +391,48 @@ void QgsOptionsDialogBase::warnAboutMissingObjects()


QgsSearchHighlightOptionWidget::QgsSearchHighlightOptionWidget( QWidget* widget )
: mWidget( nullptr )
, mOriginalPalette( QPalette() )
, mColorRole( QPalette::Window )
, mColor( Qt::yellow )
, mText( [=]( QWidget* ) {return QString();} )
: mWidget( widget )
, mStyleSheet( QString() )
, mValid( true )
, mChangedStyle( false )
, mText( [=]() {return QString();} )
{
if ( !widget )
{
return;
}

if ( qobject_cast<QLabel*>( widget ) )
{
mColorRole = QPalette::Window;
mText = [=]( QWidget * widget ) {return widget ? qobject_cast<QLabel*>( widget )->text() : QString(); };
mStyleSheet = "QLabel { background-color: yellow; color: blue;}";
mText = [=]() {return qobject_cast<QLabel*>( mWidget )->text();};
}
else if ( qobject_cast<QCheckBox*>( widget ) )
{
mColorRole = QPalette::Button;
mText = [=]( QWidget * widget ) {return widget ? qobject_cast<QCheckBox*>( widget )->text() : QString(); };
mStyleSheet = "QCheckBox { background-color: yellow; color: blue;}";
mText = [=]() {return qobject_cast<QCheckBox*>( mWidget )->text();};
}
else if ( qobject_cast<QAbstractButton*>( widget ) )
{
mStyleSheet = "QAbstractButton { background-color: yellow; color: blue;}";
mText = [=]() {return qobject_cast<QAbstractButton*>( mWidget )->text();};
}
else if ( qobject_cast<QGroupBox*>( widget ) )
{
mColorRole = QPalette::WindowText;
mText = [=]( QWidget * widget ) {return widget ? qobject_cast<QGroupBox*>( widget )->title() : QString(); };
mStyleSheet = "QGroupBox::title { background-color: yellow; color: blue;}";
mText = [=]() {return qobject_cast<QGroupBox*>( mWidget )->title();};
}
else
{
return;
mValid = false;
}
if ( mValid )
{
mStyleSheet.prepend( "/*!search!*/" ).append( "/*!search!*/" );
QgsDebugMsg( mStyleSheet );
connect( mWidget, &QWidget::destroyed, this, &QgsSearchHighlightOptionWidget::widgetDestroyed );
}
mWidget = widget;
mOriginalPalette = mWidget->palette();
}

QgsSearchHighlightOptionWidget::~QgsSearchHighlightOptionWidget()
{
}

bool QgsSearchHighlightOptionWidget::isValid()
{
return mWidget;
}

bool QgsSearchHighlightOptionWidget::searchHighlight( QString searchText )
{
bool found = false;
Expand All @@ -434,37 +441,39 @@ bool QgsSearchHighlightOptionWidget::searchHighlight( QString searchText )

if ( !searchText.isEmpty() )
{
QString origText = mText( mWidget );
QString origText = mText();
if ( origText.contains( searchText, Qt::CaseInsensitive ) )
{
found = true;
}
}

if ( found )
if ( found && !mChangedStyle )
{
QPalette pal( mOriginalPalette );
pal.setColor( mColorRole, mColor );

mWidget->setAutoFillBackground( true );
mWidget->setPalette( pal );
if ( !mWidget->isVisible() )
{
// show the widget to get initial stylesheet in case it's modified
mWidget->show();
}
mWidget->setStyleSheet( mWidget->styleSheet() + mStyleSheet );
mChangedStyle = true;
}

return found;
}

void QgsSearchHighlightOptionWidget::reset()
{
if ( mWidget )
if ( mValid && mChangedStyle )
{
mWidget->setPalette( mOriginalPalette );
QString ss = mWidget->styleSheet();
ss.remove( mStyleSheet );
mWidget->setStyleSheet( ss );
mChangedStyle = false;
}
}

QString QgsSearchHighlightOptionWidget::widgetName()
void QgsSearchHighlightOptionWidget::widgetDestroyed()
{
QString name;
if ( mWidget )
name = mWidget->objectName();
return name;
mValid = false;
}
54 changes: 44 additions & 10 deletions src/gui/qgsoptionsdialogbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,27 +38,55 @@ class QSplitter;

class QgsFilterLineEdit;


class GUI_EXPORT QgsSearchHighlightOptionWidget
/** \ingroup gui
* \class QgsSearchHighlightOptionWidget
* Container for a widget to be used to search text in the option dialog
* If the widget type is handled, it is valid.
* It can perform a text search in the widget and highlight it in case of success.
* This uses stylesheets.
* @note added in 3.0
*/
class GUI_EXPORT QgsSearchHighlightOptionWidget : public QObject
{
Q_OBJECT
public:

/** Constructor
* @param widget the widget used to search text into
*/
explicit QgsSearchHighlightOptionWidget( QWidget* widget = 0 );
~QgsSearchHighlightOptionWidget();

bool isValid();
/**
* Returns if it valid: if the widget type is handled and if the widget is not still available
*/
bool isValid() {return mValid;}

/**
* search for a text pattern and highlight the widget if the text is found
* @return true if the text pattern is found
*/
bool searchHighlight( QString searchText );

/**
* reset the style to the original state
*/
void reset();

QString widgetName();
/**
* return the widget
*/
QWidget* widget() {return mWidget;}

private slots:
void widgetDestroyed();

private:
QWidget* mWidget;
QPalette mOriginalPalette;
QPalette::ColorRole mColorRole;
QColor mColor;
std::function < QString( QWidget* ) > mText;
QString mStyleSheet;
bool mValid;
bool mChangedStyle;
std::function < QString() > mText;
};


Expand Down Expand Up @@ -118,6 +146,7 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog
/**
* searchText searches for a text in all the pages of the stacked widget and highlight the results
* @param text the text to search
* @note added in 3.0
*/
void searchText( QString text );

Expand All @@ -133,9 +162,14 @@ class GUI_EXPORT QgsOptionsDialogBase : public QDialog

virtual void updateWindowTitle();

void registerTextSearch();
/**
* register widgets in the dialog to search for text in it
* it is automatically called if a line edit has "mSearchLineEdit" as object name.
* @note added in 3.0
*/
void registerTextSearchWidgets();

QList< QPair< QgsSearchHighlightOptionWidget, int > > mRegisteredSearchWidgets;
QList< QPair< QgsSearchHighlightOptionWidget*, int > > mRegisteredSearchWidgets;

QString mOptsKey;
bool mInit;
Expand Down
Loading

0 comments on commit 4cba478

Please sign in to comment.