QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
16 #include "qgslegendfilterbutton.h"
17 
18 #include <QMenu>
19 #include <QAction>
20 
21 #include <qgsapplication.h>
23 
25  : QToolButton( parent )
26  , mMenu( nullptr )
27  , mLayer( nullptr )
28 {
29  mMenu = new QMenu( this );
30  mSetExpressionAction = new QAction( tr( "Edit filter expression" ), mMenu );
31  connect( mSetExpressionAction, SIGNAL( triggered( bool ) ), this, SLOT( onSetLegendFilterExpression() ) );
32 
33  mClearExpressionAction = new QAction( tr( "Clear filter expression" ), mMenu );
34  connect( mClearExpressionAction, SIGNAL( triggered( bool ) ), this, SLOT( onClearFilterExpression() ) );
35  mClearExpressionAction->setEnabled( false );
36 
37  mMenu->addAction( mSetExpressionAction );
38  mMenu->addAction( mClearExpressionAction );
39 
40  setCheckable( true );
41  setIcon( QgsApplication::getThemeIcon( "/mIconExpressionFilter.svg" ) );
42  setPopupMode( QToolButton::MenuButtonPopup );
43 
44  setMenu( mMenu );
45 
46  connect( this, SIGNAL( toggled( bool ) ), this, SLOT( onToggle( bool ) ) );
47 }
48 
50 {
51 }
52 
53 void QgsLegendFilterButton::onToggle( bool checked )
54 {
55  if ( checked && expressionText().isEmpty() )
56  {
57  // show the dialog if the current expression is empty
58  blockSignals( true );
59  onSetLegendFilterExpression();
60  blockSignals( false );
61  }
62 }
63 
64 void QgsLegendFilterButton::onSetLegendFilterExpression()
65 {
66  QgsExpressionBuilderDialog dlg( mLayer, mExpression );
67  if ( dlg.exec() )
68  {
70 
71  bool emitSignal = false;
72  if ( !expressionText().isEmpty() )
73  {
74  emitSignal = isChecked();
75  setChecked( true );
76  }
77  else
78  {
79  emitSignal = !isChecked();
80  setChecked( false );
81  }
82  if ( emitSignal )
83  emit toggled( isChecked() );
84  }
85 }
86 
87 void QgsLegendFilterButton::onClearFilterExpression()
88 {
89  mClearExpressionAction->setEnabled( false );
90  setExpressionText( "" );
91 
92  setChecked( false );
93 }
94 
95 void QgsLegendFilterButton::updateMenu()
96 {
97  if ( !mExpression.isEmpty() )
98  {
99  mClearExpressionAction->setEnabled( true );
100  mSetExpressionAction->setText( QString( tr( "Edit filter expression (current: %1)" ) ).arg( mExpression ) );
101  }
102  else
103  {
104  mClearExpressionAction->setEnabled( false );
105  mSetExpressionAction->setText( tr( "Edit filter expression" ) );
106  }
107 }
108 
110 {
111  return mExpression;
112 }
113 
115 {
116  mExpression = expression;
117  updateMenu();
118 }
119 
121 {
122  return mLayer;
123 }
124 
126 {
127  mLayer = layer;
128 }
void setText(const QString &text)
void setMenu(QMenu *menu)
QgsVectorLayer * vectorLayer() const
Returns the current associated vectorLayer May be null.
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
void addAction(QAction *action)
int exec()
void triggered(QAction *action)
void setIcon(const QIcon &icon)
QString tr(const char *sourceText, const char *disambiguation, int n)
QString expressionText() const
Returns the current text used as filter expression.
bool isEmpty() const
void setCheckable(bool)
bool blockSignals(bool block)
void toggled(bool checked)
void setPopupMode(ToolButtonPopupMode mode)
void setExpressionText(const QString &expression)
Sets the current text used as filter expression.
QgsLegendFilterButton(QWidget *parent=nullptr)
Construct a new filter legend button.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Represents a vector layer which manages a vector based data sets.
void setVectorLayer(QgsVectorLayer *layer)
Sets the associated vectorLayer May be null.
A generic dialog for building expression strings.
void setEnabled(bool)