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