Skip to content

Commit 8d55cad

Browse files
committed
Allow adding custom highlight widget to custom pages in option
1 parent fe31c28 commit 8d55cad

File tree

4 files changed

+91
-10
lines changed

4 files changed

+91
-10
lines changed

python/gui/qgsoptionswidgetfactory.sip.in

+12
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,24 @@ If an empty string is returned by this method the default QGIS options
3838
help will be retrieved.
3939
%End
4040

41+
42+
43+
4144
public slots:
4245

4346
virtual void apply() = 0;
4447
%Docstring
4548
Called to permanently apply the settings shown in the options page (e.g. save them to
4649
QgsSettings objects). This is usually called when the options dialog is accepted.
50+
%End
51+
52+
protected:
53+
54+
void registerHighlightWidget( QgsOptionsDialogHighlightWidget *highlightWidget /Transfer/ );
55+
%Docstring
56+
Register a highlight widget to be used to search and highlight text in
57+
options dialogs. This can be used to provide a custom implementation of
58+
:py:class:`QgsOptionsDialogHighlightWidget`.
4759
%End
4860

4961
};

python/plugins/processing/gui/ConfigDialog.py

+34-9
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747

4848
from qgis.gui import (QgsDoubleSpinBox,
4949
QgsSpinBox,
50-
QgsOptionsPageWidget)
50+
QgsOptionsPageWidget,
51+
QgsOptionsDialogHighlightWidget)
5152
from qgis.core import NULL, QgsApplication, QgsSettings
5253
from qgis.utils import OverrideCursor
5354

@@ -68,13 +69,15 @@ class ConfigOptionsPage(QgsOptionsPageWidget):
6869

6970
def __init__(self, parent):
7071
super(ConfigOptionsPage, self).__init__(parent)
71-
self.config_widget = ConfigDialog()
72+
self.config_widget = ConfigDialog(False)
7273
layout = QHBoxLayout()
7374
layout.setContentsMargins(0, 0, 0, 0)
7475
layout.setMargin(0)
7576
self.setLayout(layout)
7677
layout.addWidget(self.config_widget)
7778
self.setObjectName('processingOptions')
79+
self.highlightWidget = ProcessingTreeHighlight(self.config_widget)
80+
self.registerHighlightWidget(self.highlightWidget)
7881

7982
def apply(self):
8083
self.config_widget.accept()
@@ -83,9 +86,24 @@ def helpKey(self):
8386
return 'processing/index.html'
8487

8588

89+
class ProcessingTreeHighlight(QgsOptionsDialogHighlightWidget):
90+
def __init__(self, config_dialog):
91+
super(ProcessingTreeHighlight, self).__init__(config_dialog.tree)
92+
self.config_dialog = config_dialog
93+
94+
def highlightText(self, text):
95+
return self.config_dialog.textChanged(text)
96+
97+
def searchText(self, text):
98+
return self.config_dialog.textChanged(text)
99+
100+
def reset(self):
101+
self.config_dialog.textChanged('')
102+
103+
86104
class ConfigDialog(BASE, WIDGET):
87105

88-
def __init__(self):
106+
def __init__(self, showSearch=True):
89107
super(ConfigDialog, self).__init__(None)
90108
self.setupUi(self)
91109

@@ -95,29 +113,36 @@ def __init__(self):
95113
self.groupIcon.addPixmap(self.style().standardPixmap(
96114
QStyle.SP_DirOpenIcon), QIcon.Normal, QIcon.On)
97115

98-
if hasattr(self.searchBox, 'setPlaceholderText'):
99-
self.searchBox.setPlaceholderText(self.tr('Search...'))
100-
101116
self.model = QStandardItemModel()
102117
self.tree.setModel(self.model)
103118

104119
self.delegate = SettingDelegate()
105120
self.tree.setItemDelegateForColumn(1, self.delegate)
106121

107-
self.searchBox.textChanged.connect(self.textChanged)
122+
if showSearch:
123+
if hasattr(self.searchBox, 'setPlaceholderText'):
124+
self.searchBox.setPlaceholderText(self.tr('Search...'))
125+
self.searchBox.textChanged.connect(self.textChanged)
126+
else:
127+
self.searchBox.hide()
108128

109129
self.fillTree()
110130

111131
self.saveMenus = False
112132
self.tree.expanded.connect(self.itemExpanded)
113133

114-
def textChanged(self):
115-
text = str(self.searchBox.text().lower())
134+
def textChanged(self, text=None):
135+
if text is not None:
136+
text = str(text.lower())
137+
else:
138+
text = str(self.searchBox.text().lower())
116139
self._filterItem(self.model.invisibleRootItem(), text)
117140
if text:
118141
self.tree.expandAll()
142+
return True
119143
else:
120144
self.tree.collapseAll()
145+
return False
121146

122147
def _filterItem(self, item, text):
123148
if item.hasChildren():

src/gui/qgsoptionsdialogbase.cpp

+19-1
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,25 @@ void QgsOptionsDialogBase::registerTextSearchWidgets()
256256
{
257257
Q_FOREACH ( QWidget *w, mOptStackedWidget->widget( i )->findChildren<QWidget *>() )
258258
{
259-
QgsOptionsDialogHighlightWidget *shw = QgsOptionsDialogHighlightWidget::createWidget( w );
259+
260+
// get custom highlight widget in user added pages
261+
QMap<QWidget *, QgsOptionsDialogHighlightWidget *> customHighlightWidgets = QMap<QWidget *, QgsOptionsDialogHighlightWidget *>();
262+
QgsOptionsPageWidget *opw = qobject_cast<QgsOptionsPageWidget *>( mOptStackedWidget->widget( i ) );
263+
if ( opw )
264+
{
265+
customHighlightWidgets = opw->registeredHighlightWidgets();
266+
}
267+
QgsOptionsDialogHighlightWidget *shw = nullptr;
268+
// take custom if exists
269+
if ( customHighlightWidgets.contains( w ) )
270+
{
271+
shw = customHighlightWidgets.value( w );
272+
}
273+
// try to construct one otherwise
274+
if ( !shw || !shw->isValid() )
275+
{
276+
shw = QgsOptionsDialogHighlightWidget::createWidget( w );
277+
}
260278
if ( shw && shw->isValid() )
261279
{
262280
QgsDebugMsgLevel( QString( "Registering: %1" ).arg( w->objectName() ), 4 );

src/gui/qgsoptionswidgetfactory.h

+26
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <QListWidgetItem>
2020
#include "qgis_gui.h"
2121
#include "qgis.h"
22+
#include "qgsoptionsdialoghighlightwidget.h"
2223

2324
/**
2425
* \ingroup gui
@@ -51,6 +52,14 @@ class GUI_EXPORT QgsOptionsPageWidget : public QWidget
5152
*/
5253
virtual QString helpKey() const { return QString(); }
5354

55+
56+
/**
57+
* Returns the registered highlight widgets used to search and highlight text in
58+
* options dialogs.
59+
*/
60+
QMap<QWidget *, QgsOptionsDialogHighlightWidget *> registeredHighlightWidgets() {return mHighlighWidgets;} SIP_SKIP
61+
62+
5463
public slots:
5564

5665
/**
@@ -59,6 +68,23 @@ class GUI_EXPORT QgsOptionsPageWidget : public QWidget
5968
*/
6069
virtual void apply() = 0;
6170

71+
protected:
72+
73+
/**
74+
* Register a highlight widget to be used to search and highlight text in
75+
* options dialogs. This can be used to provide a custom implementation of
76+
* QgsOptionsDialogHighlightWidget.
77+
*/
78+
void registerHighlightWidget( QgsOptionsDialogHighlightWidget *highlightWidget SIP_TRANSFER )
79+
{
80+
mHighlighWidgets.insert( highlightWidget->widget(), highlightWidget );
81+
}
82+
83+
private:
84+
QMap<QWidget *, QgsOptionsDialogHighlightWidget *> mHighlighWidgets = QMap<QWidget *, QgsOptionsDialogHighlightWidget *>();
85+
86+
87+
6288
};
6389

6490
/**

0 commit comments

Comments
 (0)