QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
qgslegendfilterbutton.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslegendfilterbutton.h - QToolButton for legend filter by map content
3 --------------------------------------
4 Date : June 2015
5 Copyright : (C) 2015 by Hugo Mercier at Oslandia
6 Email : hugo dot mercier at oslandia dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include <QMenu>
19#include <QAction>
20
21#include "qgsapplication.h"
24
26 : QToolButton( parent )
27
28{
29 mMenu = new QMenu( this );
30 mSetExpressionAction = new QAction( tr( "Edit Filter Expression…" ), mMenu );
31 connect( mSetExpressionAction, &QAction::triggered, this, &QgsLegendFilterButton::onSetLegendFilterExpression );
32
33 mClearExpressionAction = new QAction( tr( "Clear Filter Expression" ), mMenu );
34 connect( mClearExpressionAction, &QAction::triggered, this, &QgsLegendFilterButton::onClearFilterExpression );
35 mClearExpressionAction->setEnabled( false );
36
37 mMenu->addAction( mSetExpressionAction );
38 mMenu->addAction( mClearExpressionAction );
39
40 setCheckable( true );
41 setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionFilter.svg" ) ) );
42 setPopupMode( QToolButton::MenuButtonPopup );
43
44 setMenu( mMenu );
45
46 connect( this, &QAbstractButton::toggled, this, &QgsLegendFilterButton::onToggle );
47}
48
49void QgsLegendFilterButton::onToggle( bool checked )
50{
51 if ( checked && expressionText().isEmpty() )
52 {
53 // show the dialog if the current expression is empty
54 blockSignals( true );
55 onSetLegendFilterExpression();
56 blockSignals( false );
57 }
58}
59
60void QgsLegendFilterButton::onSetLegendFilterExpression()
61{
63 if ( mExpressionContextGenerator )
64 context = mExpressionContextGenerator->createExpressionContext();
65 else
66 {
68 }
69 QgsExpressionBuilderDialog dlg( mLayer, mExpression, nullptr, QStringLiteral( "generic" ), context );
70 if ( dlg.exec() )
71 {
72 setExpressionText( dlg.expressionText() );
73
74 bool emitSignal = false;
75 if ( !expressionText().isEmpty() )
76 {
77 emitSignal = isChecked();
78 setChecked( true );
79 }
80 else
81 {
82 emitSignal = !isChecked();
83 setChecked( false );
84 }
85 if ( emitSignal )
86 emit toggled( isChecked() );
87 }
88}
89
91{
92 mExpressionContextGenerator = generator;
93}
94
95void QgsLegendFilterButton::onClearFilterExpression()
96{
97 mClearExpressionAction->setEnabled( false );
98 setExpressionText( QString() );
99
100 setChecked( false );
101}
102
103void QgsLegendFilterButton::updateMenu()
104{
105 if ( !mExpression.isEmpty() )
106 {
107 mClearExpressionAction->setEnabled( true );
108 mSetExpressionAction->setText( tr( "Edit Filter Expression (current: %1)" ).arg( mExpression ) );
109 }
110 else
111 {
112 mClearExpressionAction->setEnabled( false );
113 mSetExpressionAction->setText( tr( "Edit Filter Expression" ) );
114 }
115}
116
118{
119 return mExpression;
120}
121
122void QgsLegendFilterButton::setExpressionText( const QString &expression )
123{
124 mExpression = expression;
125 updateMenu();
126}
127
129{
130 return mLayer;
131}
132
134{
135 mLayer = layer;
136}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
A generic dialog for building expression strings.
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScopes(const QList< QgsExpressionContextScope * > &scopes)
Appends a list of scopes to the end of the context.
QgsVectorLayer * vectorLayer() const
Returns the current associated vectorLayer May be nullptr.
QgsLegendFilterButton(QWidget *parent=nullptr)
Construct a new filter legend button.
QString expressionText() const
Returns the current text used as filter expression.
void registerExpressionContextGenerator(QgsExpressionContextGenerator *generator)
Register an expression context generator class that will be used to retrieve an expression context fo...
void setVectorLayer(QgsVectorLayer *layer)
Sets the associated vectorLayer May be nullptr.
void setExpressionText(const QString &expression)
Sets the current text used as filter expression.
Represents a vector layer which manages a vector based data sets.