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