QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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 "qgsapplication.h"
21
22#include <QAction>
23#include <QMenu>
24#include <QString>
25
26#include "moc_qgslegendfilterbutton.cpp"
27
28using namespace Qt::StringLiterals;
29
31 : QToolButton( parent )
32
33{
34 mMenu = new QMenu( this );
35 mSetExpressionAction = new QAction( tr( "Edit Filter Expression…" ), mMenu );
36 connect( mSetExpressionAction, &QAction::triggered, this, &QgsLegendFilterButton::onSetLegendFilterExpression );
37
38 mClearExpressionAction = new QAction( tr( "Clear Filter Expression" ), mMenu );
39 connect( mClearExpressionAction, &QAction::triggered, this, &QgsLegendFilterButton::onClearFilterExpression );
40 mClearExpressionAction->setEnabled( false );
41
42 mMenu->addAction( mSetExpressionAction );
43 mMenu->addAction( mClearExpressionAction );
44
45 setCheckable( true );
46 setIcon( QgsApplication::getThemeIcon( u"/mIconExpressionFilter.svg"_s ) );
47 setPopupMode( QToolButton::MenuButtonPopup );
48
49 setMenu( mMenu );
50
51 connect( this, &QAbstractButton::toggled, this, &QgsLegendFilterButton::onToggle );
52}
53
54void QgsLegendFilterButton::onToggle( bool checked )
55{
56 if ( checked && expressionText().isEmpty() )
57 {
58 // show the dialog if the current expression is empty
59 blockSignals( true );
60 onSetLegendFilterExpression();
61 blockSignals( false );
62 }
63}
64
65void QgsLegendFilterButton::onSetLegendFilterExpression()
66{
67 QgsExpressionContext context;
68 if ( mExpressionContextGenerator )
69 context = mExpressionContextGenerator->createExpressionContext();
70 else
71 {
73 }
74 QgsExpressionBuilderDialog dlg( mLayer, mExpression, nullptr, u"generic"_s, context );
75 if ( dlg.exec() )
76 {
77 setExpressionText( dlg.expressionText() );
78
79 bool emitSignal = false;
80 if ( !expressionText().isEmpty() )
81 {
82 emitSignal = isChecked();
83 setChecked( true );
84 }
85 else
86 {
87 emitSignal = !isChecked();
88 setChecked( false );
89 }
90 if ( emitSignal )
91 emit toggled( isChecked() );
92 }
93}
94
96{
97 mExpressionContextGenerator = generator;
98}
99
100void QgsLegendFilterButton::onClearFilterExpression()
101{
102 mClearExpressionAction->setEnabled( false );
103 setExpressionText( QString() );
104
105 setChecked( false );
106}
107
108void QgsLegendFilterButton::updateMenu()
109{
110 if ( !mExpression.isEmpty() )
111 {
112 mClearExpressionAction->setEnabled( true );
113 mSetExpressionAction->setText( tr( "Edit Filter Expression (current: %1)" ).arg( mExpression ) );
114 }
115 else
116 {
117 mClearExpressionAction->setEnabled( false );
118 mSetExpressionAction->setText( tr( "Edit Filter Expression" ) );
119 }
120}
121
123{
124 return mExpression;
125}
126
127void QgsLegendFilterButton::setExpressionText( const QString &expression )
128{
129 mExpression = expression;
130 updateMenu();
131}
132
134{
135 return mLayer;
136}
137
139{
140 mLayer = layer;
141}
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Abstract interface for generating an expression context.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
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 dataset.