QGIS API Documentation 3.99.0-Master (2fe06baccd8)
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
25#include "moc_qgslegendfilterbutton.cpp"
26
28 : QToolButton( parent )
29
30{
31 mMenu = new QMenu( this );
32 mSetExpressionAction = new QAction( tr( "Edit Filter Expression…" ), mMenu );
33 connect( mSetExpressionAction, &QAction::triggered, this, &QgsLegendFilterButton::onSetLegendFilterExpression );
34
35 mClearExpressionAction = new QAction( tr( "Clear Filter Expression" ), mMenu );
36 connect( mClearExpressionAction, &QAction::triggered, this, &QgsLegendFilterButton::onClearFilterExpression );
37 mClearExpressionAction->setEnabled( false );
38
39 mMenu->addAction( mSetExpressionAction );
40 mMenu->addAction( mClearExpressionAction );
41
42 setCheckable( true );
43 setIcon( QgsApplication::getThemeIcon( QStringLiteral( "/mIconExpressionFilter.svg" ) ) );
44 setPopupMode( QToolButton::MenuButtonPopup );
45
46 setMenu( mMenu );
47
48 connect( this, &QAbstractButton::toggled, this, &QgsLegendFilterButton::onToggle );
49}
50
51void QgsLegendFilterButton::onToggle( bool checked )
52{
53 if ( checked && expressionText().isEmpty() )
54 {
55 // show the dialog if the current expression is empty
56 blockSignals( true );
57 onSetLegendFilterExpression();
58 blockSignals( false );
59 }
60}
61
62void QgsLegendFilterButton::onSetLegendFilterExpression()
63{
64 QgsExpressionContext context;
65 if ( mExpressionContextGenerator )
66 context = mExpressionContextGenerator->createExpressionContext();
67 else
68 {
70 }
71 QgsExpressionBuilderDialog dlg( mLayer, mExpression, nullptr, QStringLiteral( "generic" ), context );
72 if ( dlg.exec() )
73 {
74 setExpressionText( dlg.expressionText() );
75
76 bool emitSignal = false;
77 if ( !expressionText().isEmpty() )
78 {
79 emitSignal = isChecked();
80 setChecked( true );
81 }
82 else
83 {
84 emitSignal = !isChecked();
85 setChecked( false );
86 }
87 if ( emitSignal )
88 emit toggled( isChecked() );
89 }
90}
91
93{
94 mExpressionContextGenerator = generator;
95}
96
97void QgsLegendFilterButton::onClearFilterExpression()
98{
99 mClearExpressionAction->setEnabled( false );
100 setExpressionText( QString() );
101
102 setChecked( false );
103}
104
105void QgsLegendFilterButton::updateMenu()
106{
107 if ( !mExpression.isEmpty() )
108 {
109 mClearExpressionAction->setEnabled( true );
110 mSetExpressionAction->setText( tr( "Edit Filter Expression (current: %1)" ).arg( mExpression ) );
111 }
112 else
113 {
114 mClearExpressionAction->setEnabled( false );
115 mSetExpressionAction->setText( tr( "Edit Filter Expression" ) );
116 }
117}
118
120{
121 return mExpression;
122}
123
124void QgsLegendFilterButton::setExpressionText( const QString &expression )
125{
126 mExpression = expression;
127 updateMenu();
128}
129
131{
132 return mLayer;
133}
134
136{
137 mLayer = layer;
138}
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.