Skip to content
Permalink
Browse files

Merge pull request #6258 from 3nids/options_better_3

Allow adding custom highlight widget to custom pages in option
  • Loading branch information
3nids committed Feb 6, 2018
2 parents cbd3042 + 90f1d7b commit 8f2c857e552dd79e49c27bfc3cfa751952ac53ce
@@ -38,12 +38,24 @@ If an empty string is returned by this method the default QGIS options
help will be retrieved.
%End




public slots:

virtual void apply() = 0;
%Docstring
Called to permanently apply the settings shown in the options page (e.g. save them to
QgsSettings objects). This is usually called when the options dialog is accepted.
%End

protected:

void registerHighlightWidget( QgsOptionsDialogHighlightWidget *highlightWidget );
%Docstring
Register a highlight widget to be used to search and highlight text in
options dialogs. This can be used to provide a custom implementation of
:py:class:`QgsOptionsDialogHighlightWidget`.
%End

};
@@ -47,7 +47,8 @@

from qgis.gui import (QgsDoubleSpinBox,
QgsSpinBox,
QgsOptionsPageWidget)
QgsOptionsPageWidget,
QgsOptionsDialogHighlightWidget)
from qgis.core import NULL, QgsApplication, QgsSettings
from qgis.utils import OverrideCursor

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

def __init__(self, parent):
super(ConfigOptionsPage, self).__init__(parent)
self.config_widget = ConfigDialog()
self.config_widget = ConfigDialog(False)
layout = QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setMargin(0)
self.setLayout(layout)
layout.addWidget(self.config_widget)
self.setObjectName('processingOptions')
self.highlightWidget = ProcessingTreeHighlight(self.config_widget)
self.registerHighlightWidget(self.highlightWidget)

def apply(self):
self.config_widget.accept()
@@ -83,9 +86,25 @@ def helpKey(self):
return 'processing/index.html'


class ProcessingTreeHighlight(QgsOptionsDialogHighlightWidget):

def __init__(self, config_dialog):
super(ProcessingTreeHighlight, self).__init__(config_dialog.tree)
self.config_dialog = config_dialog

def highlightText(self, text):
return self.config_dialog.textChanged(text)

def searchText(self, text):
return self.config_dialog.textChanged(text)

def reset(self):
self.config_dialog.textChanged('')


class ConfigDialog(BASE, WIDGET):

def __init__(self):
def __init__(self, showSearch=True):
super(ConfigDialog, self).__init__(None)
self.setupUi(self)

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

if hasattr(self.searchBox, 'setPlaceholderText'):
self.searchBox.setPlaceholderText(self.tr('Search...'))

self.model = QStandardItemModel()
self.tree.setModel(self.model)

self.delegate = SettingDelegate()
self.tree.setItemDelegateForColumn(1, self.delegate)

self.searchBox.textChanged.connect(self.textChanged)
if showSearch:
if hasattr(self.searchBox, 'setPlaceholderText'):
self.searchBox.setPlaceholderText(self.tr('Search...'))
self.searchBox.textChanged.connect(self.textChanged)
else:
self.searchBox.hide()

self.fillTree()

self.saveMenus = False
self.tree.expanded.connect(self.itemExpanded)
self.auto_adjust_columns = True

def textChanged(self, text=None):
if text is not None:
text = str(text.lower())
else:
text = str(self.searchBox.text().lower())
found = self._filterItem(self.model.invisibleRootItem(), text)

def textChanged(self):
text = str(self.searchBox.text().lower())
self._filterItem(self.model.invisibleRootItem(), text)
self.auto_adjust_columns = False
if text:
self.tree.expandAll()
else:
self.tree.collapseAll()

def _filterItem(self, item, text):
self.adjustColumns()
self.auto_adjust_columns = True

if text:
return found
else:
self.tree.collapseAll()
return False

def _filterItem(self, item, text, forceShow=False):
if item.hasChildren():
show = False
show = forceShow or isinstance(item, QStandardItem) and bool(text) and (text in item.text().lower())
for i in range(item.rowCount()):
child = item.child(i)
showChild = self._filterItem(child, text)
show = (showChild or show)
show = self._filterItem(child, text, forceShow) or show
self.tree.setRowHidden(item.row(), item.index().parent(), not show)
return show

elif isinstance(item, QStandardItem):
hide = bool(text) and (text not in item.text().lower())
self.tree.setRowHidden(item.row(), item.index().parent(), hide)
return not hide
show = forceShow or bool(text) and (text in item.text().lower())
self.tree.setRowHidden(item.row(), item.index().parent(), not show)
return show

def fillTree(self):
self.fillTreeUsingProviders()
@@ -304,7 +339,8 @@ def accept(self):
def itemExpanded(self, idx):
if idx == self.menusItem.index():
self.saveMenus = True
self.adjustColumns()
if self.auto_adjust_columns:
self.adjustColumns()

def adjustColumns(self):
self.tree.resizeColumnToContents(0)
@@ -207,6 +207,8 @@ void QgsOptionsDialogBase::restoreOptionsBaseUi( const QString &title )

void QgsOptionsDialogBase::searchText( const QString &text )
{
const int minimumTextLength = 3;

mSearchLineEdit->setMinimumWidth( text.isEmpty() ? 0 : 70 );

if ( !mOptStackedWidget )
@@ -219,12 +221,12 @@ void QgsOptionsDialogBase::searchText( const QString &text )
// 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() );
mOptListWidget->setRowHidden( r, text.length() >= minimumTextLength );
}

for ( const QPair< QgsOptionsDialogHighlightWidget *, int > &rsw : qgis::as_const( mRegisteredSearchWidgets ) )
{
if ( rsw.first->searchHighlight( text ) )
if ( rsw.first->searchHighlight( text.length() >= minimumTextLength ? text : QString() ) )
{
mOptListWidget->setRowHidden( rsw.second, false );
}
@@ -256,7 +258,25 @@ void QgsOptionsDialogBase::registerTextSearchWidgets()
{
Q_FOREACH ( QWidget *w, mOptStackedWidget->widget( i )->findChildren<QWidget *>() )
{
QgsOptionsDialogHighlightWidget *shw = QgsOptionsDialogHighlightWidget::createWidget( w );

// get custom highlight widget in user added pages
QMap<QWidget *, QgsOptionsDialogHighlightWidget *> customHighlightWidgets = QMap<QWidget *, QgsOptionsDialogHighlightWidget *>();
QgsOptionsPageWidget *opw = qobject_cast<QgsOptionsPageWidget *>( mOptStackedWidget->widget( i ) );
if ( opw )
{
customHighlightWidgets = opw->registeredHighlightWidgets();
}
QgsOptionsDialogHighlightWidget *shw = nullptr;
// take custom if exists
if ( customHighlightWidgets.contains( w ) )
{
shw = customHighlightWidgets.value( w );
}
// try to construct one otherwise
if ( !shw || !shw->isValid() )
{
shw = QgsOptionsDialogHighlightWidget::createWidget( w );
}
if ( shw && shw->isValid() )
{
QgsDebugMsgLevel( QString( "Registering: %1" ).arg( w->objectName() ), 4 );
@@ -214,13 +214,14 @@ bool QgsOptionsDialogHighlightTree::highlightText( const QString &text )
setChildrenVisible( item, true );

QTreeWidgetItem *parent = item;
while ( ( parent = parent->parent() ) )
while ( parent )
{
if ( mTreeInitialExpand.contains( parent ) )
break;
mTreeInitialExpand.insert( parent, parent->isExpanded() );
parent->setExpanded( true );
parent->setHidden( false );
parent = parent->parent();
}
}
}
@@ -19,6 +19,7 @@
#include <QListWidgetItem>
#include "qgis_gui.h"
#include "qgis.h"
#include "qgsoptionsdialoghighlightwidget.h"

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


/**
* Returns the registered highlight widgets used to search and highlight text in
* options dialogs.
*/
QMap<QWidget *, QgsOptionsDialogHighlightWidget *> registeredHighlightWidgets() {return mHighlighWidgets;} SIP_SKIP


public slots:

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

protected:

/**
* Register a highlight widget to be used to search and highlight text in
* options dialogs. This can be used to provide a custom implementation of
* QgsOptionsDialogHighlightWidget.
*/
void registerHighlightWidget( QgsOptionsDialogHighlightWidget *highlightWidget )
{
mHighlighWidgets.insert( highlightWidget->widget(), highlightWidget );
}

private:
QMap<QWidget *, QgsOptionsDialogHighlightWidget *> mHighlighWidgets = QMap<QWidget *, QgsOptionsDialogHighlightWidget *>();



};

/**

0 comments on commit 8f2c857

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