Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor QgsOptionsDialogHighlighWidget to use it as an interface
by removing the inheritance of QObject, an implementation of QWidget can now also inherit from QgsOptionsDialogHighlighWidget and provide the interface to search/highlight texts
  • Loading branch information
3nids committed Apr 27, 2023
1 parent 7498d09 commit 83d735f
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 53 deletions.
11 changes: 5 additions & 6 deletions python/gui/auto_generated/qgsoptionsdialoghighlightwidget.sip.in
Expand Up @@ -9,7 +9,9 @@



class QgsOptionsDialogHighlightWidget : QObject


class QgsOptionsDialogHighlightWidget
{
%Docstring(signature="appended")
Container for a widget to be used to search text in the option dialog
Expand All @@ -34,6 +36,8 @@ For instance a :py:class:`QgsOptionsDialogHighlightButton` for button.
for the given widget.
%End

virtual ~QgsOptionsDialogHighlightWidget();

bool isValid();
%Docstring
Returns if it valid: if the widget type is handled and if the widget is not still available
Expand All @@ -51,11 +55,6 @@ search for a text pattern and highlight the widget if the text is found
Returns the widget
%End


virtual bool eventFilter( QObject *obj, QEvent *event );



protected:

virtual bool searchText( const QString &text ) = 0;
Expand Down
34 changes: 18 additions & 16 deletions src/gui/qgsoptionsdialogbase.cpp
Expand Up @@ -33,7 +33,6 @@
#include <functional>

#include "qgsfilterlineedit.h"
#include "qgsmessagebaritem.h"
#include "qgslogger.h"
#include "qgsoptionsdialoghighlightwidget.h"
#include "qgsoptionswidgetfactory.h"
Expand Down Expand Up @@ -652,31 +651,34 @@ void QgsOptionsDialogBase::registerTextSearchWidgets()

for ( int i = 0; i < mOptStackedWidget->count(); i++ )
{

const QList< QWidget * > widgets = mOptStackedWidget->widget( i )->findChildren<QWidget *>();
for ( QWidget *w : widgets )
for ( QWidget *widget : widgets )
{
// get custom highlight widget in user added pages
QHash<QWidget *, QgsOptionsDialogHighlightWidget *> customHighlightWidgets;
QgsOptionsPageWidget *opw = qobject_cast<QgsOptionsPageWidget *>( mOptStackedWidget->widget( i ) );
if ( opw )
{
customHighlightWidgets = opw->registeredHighlightWidgets();
}
QgsOptionsDialogHighlightWidget *shw = nullptr;
// take custom if exists
if ( customHighlightWidgets.contains( w ) )
// see if the widget also inherits QgsOptionsDialogHighlightWidget
QgsOptionsDialogHighlightWidget *shw = dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget );
if ( !shw )
{
shw = customHighlightWidgets.value( w );
// get custom highlight widget in user added pages
QHash<QWidget *, QgsOptionsDialogHighlightWidget *> customHighlightWidgets;
QgsOptionsPageWidget *opw = qobject_cast<QgsOptionsPageWidget *>( mOptStackedWidget->widget( i ) );
if ( opw )
{
customHighlightWidgets = opw->registeredHighlightWidgets();
}
// take custom if exists
if ( customHighlightWidgets.contains( widget ) )
{
shw = customHighlightWidgets.value( widget );
}
}
// try to construct one otherwise
if ( !shw || !shw->isValid() )
{
shw = QgsOptionsDialogHighlightWidget::createWidget( w );
shw = QgsOptionsDialogHighlightWidget::createWidget( widget );
}
if ( shw && shw->isValid() )
{
QgsDebugMsgLevel( QStringLiteral( "Registering: %1" ).arg( w->objectName() ), 4 );
QgsDebugMsgLevel( QStringLiteral( "Registering: %1" ).arg( widget->objectName() ), 4 );
mRegisteredSearchWidgets.append( qMakePair( shw, i ) );
}
else
Expand Down
44 changes: 29 additions & 15 deletions src/gui/qgsoptionsdialoghighlightwidget.cpp
Expand Up @@ -33,8 +33,7 @@


QgsOptionsDialogHighlightWidget::QgsOptionsDialogHighlightWidget( QWidget *widget )
: QObject( widget )
, mWidget( widget )
: mWidget( widget )
{}

QgsOptionsDialogHighlightWidget *QgsOptionsDialogHighlightWidget::createWidget( QWidget *widget )
Expand All @@ -52,6 +51,11 @@ QgsOptionsDialogHighlightWidget *QgsOptionsDialogHighlightWidget::createWidget(
}
}

if ( dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget ) )
{
return dynamic_cast<QgsOptionsDialogHighlightWidget *>( widget );
}

if ( qobject_cast<QLabel *>( widget ) )
{
return new QgsOptionsDialogHighlightLabel( qobject_cast<QLabel *>( widget ) );
Expand Down Expand Up @@ -97,46 +101,55 @@ bool QgsOptionsDialogHighlightWidget::searchHighlight( const QString &text )
mChangedStyle = false;
}

if ( mInstalledFilter )
if ( mEventFilter )
{
mWidget->removeEventFilter( this );
mInstalledFilter = false;
mWidget->removeEventFilter( mEventFilter );
delete mEventFilter;
mEventFilter = nullptr;
}

if ( !text.isEmpty() )
{
found = searchText( text );
found = searchText( mSearchText );
}

if ( found )
{

if ( !mWidget->isVisible() )
{
mWidget->installEventFilter( this );
mInstalledFilter = true;
mEventFilter = new QgsOptionsDialogHighlightWidgetEventFilter( this );
mWidget->installEventFilter( mEventFilter );
}
else
{
mChangedStyle = highlightText( text );
mChangedStyle = highlightText( mSearchText );
}
}

return found;
}

bool QgsOptionsDialogHighlightWidget::eventFilter( QObject *obj, QEvent *event )


///@cond PRIVATE

QgsOptionsDialogHighlightWidgetEventFilter::QgsOptionsDialogHighlightWidgetEventFilter( QgsOptionsDialogHighlightWidget *highlightWidget )
: QObject( highlightWidget->widget() )
, mHighlightWidget( highlightWidget )
{}

bool QgsOptionsDialogHighlightWidgetEventFilter::eventFilter( QObject *obj, QEvent *event )
{
if ( mInstalledFilter && event->type() == QEvent::Show && obj == mWidget )
if ( event->type() == QEvent::Show && obj == mHighlightWidget->widget() )
{
mWidget->removeEventFilter( this );
mInstalledFilter = false;
mHighlightWidget->widget()->removeEventFilter( this );
// instead of catching the event and calling show again
// it might be better to use a timer to change the style
// after the widget is shown
#if 1
mWidget->show();
mChangedStyle = highlightText( mSearchText );
mHighlightWidget->widget()->show();
mHighlightWidget->mChangedStyle = mHighlightWidget->highlightText( mHighlightWidget->mSearchText );
return true;
#else
QTimer::singleShot( 500, this, [ = ]
Expand All @@ -148,5 +161,6 @@ bool QgsOptionsDialogHighlightWidget::eventFilter( QObject *obj, QEvent *event )
return QObject::eventFilter( obj, event );
}

///@endcond


42 changes: 34 additions & 8 deletions src/gui/qgsoptionsdialoghighlightwidget.h
Expand Up @@ -23,6 +23,34 @@
#include "qgis_gui.h"
#include "qgis_sip.h"

class QgsOptionsDialogHighlightWidget;

#ifndef SIP_RUN

///@cond PRIVATE

/**
* \ingroup gui
* \class QgsOptionsDialogHighlightWidgetEventFilter
* \brief QgsOptionsDialogHighlightWidgetEventFilter is an event filter implementation for QgsOptionsDialogHighlightWidget
* \since QGIS 3.32
*/
class QgsOptionsDialogHighlightWidgetEventFilter : public QObject
{
Q_OBJECT
public:
//! Constructor
QgsOptionsDialogHighlightWidgetEventFilter( QgsOptionsDialogHighlightWidget *highlightWidget );
bool eventFilter( QObject *obj, QEvent *event ) override;
private:
QgsOptionsDialogHighlightWidget *mHighlightWidget;

};

///@endcond

#endif

/**
* \ingroup gui
* \class QgsOptionsDialogHighlightWidget
Expand All @@ -32,10 +60,8 @@
* This uses stylesheets.
* \since QGIS 3.0
*/
class GUI_EXPORT QgsOptionsDialogHighlightWidget : public QObject
class GUI_EXPORT QgsOptionsDialogHighlightWidget
{

Q_OBJECT
public:

/**
Expand All @@ -46,6 +72,8 @@ class GUI_EXPORT QgsOptionsDialogHighlightWidget : public QObject
*/
static QgsOptionsDialogHighlightWidget *createWidget( QWidget *widget ) SIP_FACTORY;

virtual ~QgsOptionsDialogHighlightWidget() = default;

/**
* Returns if it valid: if the widget type is handled and if the widget is not still available
*/
Expand All @@ -62,10 +90,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightWidget : public QObject
*/
QWidget *widget() {return mWidget;}


bool eventFilter( QObject *obj, QEvent *event ) override;


protected:

/**
Expand Down Expand Up @@ -94,9 +118,11 @@ class GUI_EXPORT QgsOptionsDialogHighlightWidget : public QObject
QPointer< QWidget > mWidget;

private:
friend class QgsOptionsDialogHighlightWidgetEventFilter;

QString mSearchText = QString();
bool mChangedStyle = false;
bool mInstalledFilter = false;
QgsOptionsDialogHighlightWidgetEventFilter *mEventFilter = nullptr;
};

#endif // QGSOPTIONSDIALOGHIGHLIGHTWIDGET_H
2 changes: 0 additions & 2 deletions src/gui/qgsoptionsdialoghighlightwidgetsimpl.cpp
Expand Up @@ -26,8 +26,6 @@
#include <QTextDocumentFragment>

#include "qgsoptionsdialoghighlightwidget.h"
#include "qgsmessagebaritem.h"
#include "qgslogger.h"

#include "qgsoptionsdialoghighlightwidgetsimpl.h"

Expand Down
6 changes: 0 additions & 6 deletions src/gui/qgsoptionsdialoghighlightwidgetsimpl.h
Expand Up @@ -43,7 +43,6 @@ class QTableView;
*/
class GUI_EXPORT QgsOptionsDialogHighlightLabel : public QgsOptionsDialogHighlightWidget
{
Q_OBJECT
public:
//! constructs a highlight widget for a label
QgsOptionsDialogHighlightLabel( QLabel *label );
Expand All @@ -64,7 +63,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightLabel : public QgsOptionsDialogHighlig
*/
class GUI_EXPORT QgsOptionsDialogHighlightCheckBox : public QgsOptionsDialogHighlightWidget
{
Q_OBJECT
public:
//! constructs a highlight widget for a checkbox
QgsOptionsDialogHighlightCheckBox( QCheckBox *checkBox );
Expand All @@ -85,7 +83,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightCheckBox : public QgsOptionsDialogHigh
*/
class GUI_EXPORT QgsOptionsDialogHighlightButton : public QgsOptionsDialogHighlightWidget
{
Q_OBJECT
public:
//! constructs a highlight widget for a button.
QgsOptionsDialogHighlightButton( QAbstractButton *button );
Expand All @@ -106,7 +103,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightButton : public QgsOptionsDialogHighli
*/
class GUI_EXPORT QgsOptionsDialogHighlightGroupBox : public QgsOptionsDialogHighlightWidget
{
Q_OBJECT
public:
//! constructs a highlight widget for a group box.
QgsOptionsDialogHighlightGroupBox( QGroupBox *groupBox );
Expand All @@ -129,7 +125,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightGroupBox : public QgsOptionsDialogHigh
*/
class GUI_EXPORT QgsOptionsDialogHighlightTree : public QgsOptionsDialogHighlightWidget
{
Q_OBJECT
public:
//! constructs a highlight widget for a tree view or widget.
QgsOptionsDialogHighlightTree( QTreeView *treeView );
Expand All @@ -152,7 +147,6 @@ class GUI_EXPORT QgsOptionsDialogHighlightTree : public QgsOptionsDialogHighligh
*/
class GUI_EXPORT QgsOptionsDialogHighlightTable : public QgsOptionsDialogHighlightWidget
{
Q_OBJECT
public:
//! constructs a highlight widget for a table view or widget.
QgsOptionsDialogHighlightTable( QTableView *tableView );
Expand Down

0 comments on commit 83d735f

Please sign in to comment.