QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgssmartgroupeditordialog.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgssmartgroupeditordialog.cpp
3 -----------------------------
4 begin : July 2012
5 copyright : (C) 2012 by Arunmozhi
6 email : aruntheguy at gmail.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_qgssmartgroupeditordialog.cpp"
18
19#include "qgsstyle.h"
20#include "qgsapplication.h"
21#include "qgsgui.h"
22
23#include <QVariant>
24#include <QMessageBox>
25
26// -------------------------- //
27// Condition Widget functions //
28// -------------------------- //
29QgsSmartGroupCondition::QgsSmartGroupCondition( int id, QWidget *parent ) : QWidget( parent )
30{
31 setupUi( this );
32
33 mConditionId = id;
34
35 mCondCombo->addItem( tr( "has the tag" ), QVariant( "tag" ) );
36 mCondCombo->addItem( tr( "has a part of name matching" ), QVariant( "name" ) );
37 mCondCombo->addItem( tr( "does NOT have the tag" ), QVariant( "!tag" ) );
38 mCondCombo->addItem( tr( "has NO part of name matching" ), QVariant( "!name" ) );
39
40 mRemoveBtn->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) );
41
42 connect( mRemoveBtn, &QAbstractButton::clicked, this, &QgsSmartGroupCondition::destruct );
43}
44
49
51{
52 return mCondCombo->currentData().toString();
53}
54
56{
57 return mCondLineEdit->text();
58}
59
60void QgsSmartGroupCondition::setConstraint( const QString &constraint )
61{
62 mCondCombo->setCurrentIndex( mCondCombo->findData( QVariant( constraint ) ) );
63}
64
65void QgsSmartGroupCondition::setParameter( const QString &param )
66{
67 mCondLineEdit->setText( param );
68}
69
71{
72 mRemoveBtn->setVisible( !hide );
73}
74
75
76// ------------------------ //
77// Editor Dialog Functions //
78// ------------------------ //
80 : QDialog( parent )
81 , mStyle( style )
82{
83 setupUi( this );
85
86 connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsSmartGroupEditorDialog::buttonBox_accepted );
87
88 mCondCount = 0;
89
90 mAndOrCombo->addItem( tr( "ALL the constraints" ), QVariant( "AND" ) );
91 mAndOrCombo->addItem( tr( "any ONE of the constraints" ), QVariant( "OR" ) );
92
93 mLayout = new QGridLayout( mConditionsBox );
95
96 connect( mAddConditionBtn, &QAbstractButton::clicked, this, &QgsSmartGroupEditorDialog::addCondition );
97}
98
100{
101 return mNameLineEdit->text();
102}
103
105{
106 // enable the remove buttons when 2nd condition is added
107 if ( mConditionMap.count() == 1 )
108 {
109 const auto constMConditionMap = mConditionMap;
110 for ( QgsSmartGroupCondition *condition : constMConditionMap )
111 {
112 condition->hideRemoveButton( false );
113 }
114 }
116 mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
117
119 if ( mConditionMap.isEmpty() )
120 {
121 cond->hideRemoveButton( true );
122 }
123 mConditionMap.insert( mCondCount, cond );
124 ++mCondCount;
125}
126
128{
129 // hide the remove button of the last condition when 2nd last is removed
130 if ( mConditionMap.count() == 2 )
131 {
132 const auto constMConditionMap = mConditionMap;
133 for ( QgsSmartGroupCondition *condition : constMConditionMap )
134 {
135 condition->hideRemoveButton( true );
136 }
137 }
138
139 QgsSmartGroupCondition *cond = mConditionMap.take( id );
140 delete cond;
141}
142
144{
145 QgsSmartConditionMap conditions;
146
147 const auto constMConditionMap = mConditionMap;
148 for ( QgsSmartGroupCondition *condition : constMConditionMap )
149 {
150 conditions.insert( condition->constraint(), condition->parameter() );
151 }
152
153 return conditions;
154}
155
157{
158 return mAndOrCombo->currentData().toString();
159}
160
162{
163 QStringList constraints;
164 constraints << QStringLiteral( "tag" ) << QStringLiteral( "name" ) << QStringLiteral( "!tag" ) << QStringLiteral( "!name" );
165
166 // clear any defaults
167 qDeleteAll( mConditionMap );
168 mConditionMap.clear();
169
170 //set the constraints
171 const auto constConstraints = constraints;
172 for ( const QString &constr : constConstraints )
173 {
174 const QStringList params = map.values( constr );
175 const auto constParams = params;
176 for ( const QString &param : constParams )
177 {
179 mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
180
181 cond->setConstraint( constr );
182 cond->setParameter( param );
183
185
186 mConditionMap.insert( mCondCount, cond );
187 ++mCondCount;
188 }
189 }
190}
191
193{
194 mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
195}
196
198{
199 mNameLineEdit->setText( name );
200}
201
202void QgsSmartGroupEditorDialog::buttonBox_accepted()
203{
204 if ( mNameLineEdit->text().isEmpty() )
205 {
206 QMessageBox::critical( this, tr( "Edit Smart Group" ), tr( "The smart group name field is empty. Kindly provide a name." ) );
207 return;
208 }
209 accept();
210}
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition qgsgui.cpp:209
void setConstraint(const QString &constraint)
sets the given constraint
QgsSmartGroupCondition(int id, QWidget *parent=nullptr)
QString parameter()
returns the parameter
void setParameter(const QString &param)
sets the given param
void removed(int id)
Emitted when the group with the specified id is removed.
void hideRemoveButton(bool hide)
sets the remove button hidden state to 'hide'
QString constraint()
returns the constraint key
void setConditionMap(const QgsSmartConditionMap &)
sets up the GUI for the given conditionmap
QMap< int, QgsSmartGroupCondition * > mConditionMap
QgsSmartConditionMap conditionMap()
returns the condition map
QString smartgroupName()
returns the value from mNameLineEdit
QgsSmartGroupEditorDialog(QgsStyle *style, QWidget *parent=nullptr)
void addCondition()
function to create a new ConditionBox and update UI
void removeCondition(int)
slot to remove the condition with id int
void setSmartgroupName(const QString &)
sets the smart group Name
void setOperator(const QString &)
sets the operator AND/OR
QString conditionOperator()
returns the AND/OR condition
QMultiMap< QString, QString > QgsSmartConditionMap
A multimap to hold the smart group conditions as constraint and parameter pairs.
Definition qgsstyle.h:79