Skip to content

Commit 8f2c857

Browse files
authored
Merge pull request #6258 from 3nids/options_better_3
Allow adding custom highlight widget to custom pages in option
2 parents cbd3042 + 90f1d7b commit 8f2c857

File tree

5 files changed

+117
-22
lines changed

5 files changed

+117
-22
lines changed

python/gui/qgsoptionswidgetfactory.sip.in

Lines changed: 12 additions & 0 deletions
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 );
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

Lines changed: 54 additions & 18 deletions
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,25 @@ def helpKey(self):
8386
return 'processing/index.html'
8487

8588

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

88-
def __init__(self):
107+
def __init__(self, showSearch=True):
89108
super(ConfigDialog, self).__init__(None)
90109
self.setupUi(self)
91110

@@ -95,44 +114,60 @@ def __init__(self):
95114
self.groupIcon.addPixmap(self.style().standardPixmap(
96115
QStyle.SP_DirOpenIcon), QIcon.Normal, QIcon.On)
97116

98-
if hasattr(self.searchBox, 'setPlaceholderText'):
99-
self.searchBox.setPlaceholderText(self.tr('Search...'))
100-
101117
self.model = QStandardItemModel()
102118
self.tree.setModel(self.model)
103119

104120
self.delegate = SettingDelegate()
105121
self.tree.setItemDelegateForColumn(1, self.delegate)
106122

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

109130
self.fillTree()
110131

111132
self.saveMenus = False
112133
self.tree.expanded.connect(self.itemExpanded)
134+
self.auto_adjust_columns = True
135+
136+
def textChanged(self, text=None):
137+
if text is not None:
138+
text = str(text.lower())
139+
else:
140+
text = str(self.searchBox.text().lower())
141+
found = self._filterItem(self.model.invisibleRootItem(), text)
113142

114-
def textChanged(self):
115-
text = str(self.searchBox.text().lower())
116-
self._filterItem(self.model.invisibleRootItem(), text)
143+
self.auto_adjust_columns = False
117144
if text:
118145
self.tree.expandAll()
119146
else:
120147
self.tree.collapseAll()
121148

122-
def _filterItem(self, item, text):
149+
self.adjustColumns()
150+
self.auto_adjust_columns = True
151+
152+
if text:
153+
return found
154+
else:
155+
self.tree.collapseAll()
156+
return False
157+
158+
def _filterItem(self, item, text, forceShow=False):
123159
if item.hasChildren():
124-
show = False
160+
show = forceShow or isinstance(item, QStandardItem) and bool(text) and (text in item.text().lower())
125161
for i in range(item.rowCount()):
126162
child = item.child(i)
127-
showChild = self._filterItem(child, text)
128-
show = (showChild or show)
163+
show = self._filterItem(child, text, forceShow) or show
129164
self.tree.setRowHidden(item.row(), item.index().parent(), not show)
130165
return show
131166

132167
elif isinstance(item, QStandardItem):
133-
hide = bool(text) and (text not in item.text().lower())
134-
self.tree.setRowHidden(item.row(), item.index().parent(), hide)
135-
return not hide
168+
show = forceShow or bool(text) and (text in item.text().lower())
169+
self.tree.setRowHidden(item.row(), item.index().parent(), not show)
170+
return show
136171

137172
def fillTree(self):
138173
self.fillTreeUsingProviders()
@@ -304,7 +339,8 @@ def accept(self):
304339
def itemExpanded(self, idx):
305340
if idx == self.menusItem.index():
306341
self.saveMenus = True
307-
self.adjustColumns()
342+
if self.auto_adjust_columns:
343+
self.adjustColumns()
308344

309345
def adjustColumns(self):
310346
self.tree.resizeColumnToContents(0)

src/gui/qgsoptionsdialogbase.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@ void QgsOptionsDialogBase::restoreOptionsBaseUi( const QString &title )
207207

208208
void QgsOptionsDialogBase::searchText( const QString &text )
209209
{
210+
const int minimumTextLength = 3;
211+
210212
mSearchLineEdit->setMinimumWidth( text.isEmpty() ? 0 : 70 );
211213

212214
if ( !mOptStackedWidget )
@@ -219,12 +221,12 @@ void QgsOptionsDialogBase::searchText( const QString &text )
219221
// hide all page if text has to be search, show them all otherwise
220222
for ( int r = 0; r < mOptListWidget->count(); ++r )
221223
{
222-
mOptListWidget->setRowHidden( r, !text.isEmpty() );
224+
mOptListWidget->setRowHidden( r, text.length() >= minimumTextLength );
223225
}
224226

225227
for ( const QPair< QgsOptionsDialogHighlightWidget *, int > &rsw : qgis::as_const( mRegisteredSearchWidgets ) )
226228
{
227-
if ( rsw.first->searchHighlight( text ) )
229+
if ( rsw.first->searchHighlight( text.length() >= minimumTextLength ? text : QString() ) )
228230
{
229231
mOptListWidget->setRowHidden( rsw.second, false );
230232
}
@@ -256,7 +258,25 @@ void QgsOptionsDialogBase::registerTextSearchWidgets()
256258
{
257259
Q_FOREACH ( QWidget *w, mOptStackedWidget->widget( i )->findChildren<QWidget *>() )
258260
{
259-
QgsOptionsDialogHighlightWidget *shw = QgsOptionsDialogHighlightWidget::createWidget( w );
261+
262+
// get custom highlight widget in user added pages
263+
QMap<QWidget *, QgsOptionsDialogHighlightWidget *> customHighlightWidgets = QMap<QWidget *, QgsOptionsDialogHighlightWidget *>();
264+
QgsOptionsPageWidget *opw = qobject_cast<QgsOptionsPageWidget *>( mOptStackedWidget->widget( i ) );
265+
if ( opw )
266+
{
267+
customHighlightWidgets = opw->registeredHighlightWidgets();
268+
}
269+
QgsOptionsDialogHighlightWidget *shw = nullptr;
270+
// take custom if exists
271+
if ( customHighlightWidgets.contains( w ) )
272+
{
273+
shw = customHighlightWidgets.value( w );
274+
}
275+
// try to construct one otherwise
276+
if ( !shw || !shw->isValid() )
277+
{
278+
shw = QgsOptionsDialogHighlightWidget::createWidget( w );
279+
}
260280
if ( shw && shw->isValid() )
261281
{
262282
QgsDebugMsgLevel( QString( "Registering: %1" ).arg( w->objectName() ), 4 );

src/gui/qgsoptionsdialoghighlightwidgetsimpl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,14 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
214214
setChildrenVisible( item, true );
215215

216216
QTreeWidgetItem *parent = item;
217-
while ( ( parent = parent->parent() ) )
217+
while ( parent )
218218
{
219219
if ( mTreeInitialExpand.contains( parent ) )
220220
break;
221221
mTreeInitialExpand.insert( parent, parent->isExpanded() );
222222
parent->setExpanded( true );
223223
parent->setHidden( false );
224+
parent = parent->parent();
224225
}
225226
}
226227
}

src/gui/qgsoptionswidgetfactory.h

Lines changed: 26 additions & 0 deletions
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 )
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)