QGIS API Documentation  3.23.0-Master (eb871beae0)
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 
18 #include "qgsstyle.h"
19 #include "qgsapplication.h"
20 #include "qgsgui.h"
21 
22 #include <QVariant>
23 #include <QMessageBox>
24 
25 // -------------------------- //
26 // Condition Widget functions //
27 // -------------------------- //
28 QgsSmartGroupCondition::QgsSmartGroupCondition( int id, QWidget *parent ) : QWidget( parent )
29 {
30  setupUi( this );
31 
32  mConditionId = id;
33 
34  mCondCombo->addItem( tr( "has the tag" ), QVariant( "tag" ) );
35  mCondCombo->addItem( tr( "has a part of name matching" ), QVariant( "name" ) );
36  mCondCombo->addItem( tr( "does NOT have the tag" ), QVariant( "!tag" ) );
37  mCondCombo->addItem( tr( "has NO part of name matching" ), QVariant( "!name" ) );
38 
39  mRemoveBtn->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) );
40 
41  connect( mRemoveBtn, &QAbstractButton::clicked, this, &QgsSmartGroupCondition::destruct );
42 }
43 
45 {
46  emit removed( mConditionId );
47 }
48 
50 {
51  return mCondCombo->currentData().toString();
52 }
53 
55 {
56  return mCondLineEdit->text();
57 }
58 
59 void QgsSmartGroupCondition::setConstraint( const QString &constraint )
60 {
61  mCondCombo->setCurrentIndex( mCondCombo->findData( QVariant( constraint ) ) );
62 }
63 
64 void QgsSmartGroupCondition::setParameter( const QString &param )
65 {
66  mCondLineEdit->setText( param );
67 }
68 
70 {
71  mRemoveBtn->setVisible( !hide );
72 }
73 
74 
75 // ------------------------ //
76 // Editor Dialog Functions //
77 // ------------------------ //
79  : QDialog( parent )
80  , mStyle( style )
81 {
82  setupUi( this );
84 
85  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsSmartGroupEditorDialog::buttonBox_accepted );
86 
87  mCondCount = 0;
88 
89  mAndOrCombo->addItem( tr( "ALL the constraints" ), QVariant( "AND" ) );
90  mAndOrCombo->addItem( tr( "any ONE of the constraints" ), QVariant( "OR" ) );
91 
92  mLayout = new QGridLayout( mConditionsBox );
93  addCondition();
94 
95  connect( mAddConditionBtn, &QAbstractButton::clicked, this, &QgsSmartGroupEditorDialog::addCondition );
96 }
97 
99 {
100  return mNameLineEdit->text();
101 }
102 
104 {
105  // enable the remove buttons when 2nd condition is added
106  if ( mConditionMap.count() == 1 )
107  {
108  const auto constMConditionMap = mConditionMap;
109  for ( QgsSmartGroupCondition *condition : constMConditionMap )
110  {
111  condition->hideRemoveButton( false );
112  }
113  }
115  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
116 
118  if ( mConditionMap.isEmpty() )
119  {
120  cond->hideRemoveButton( true );
121  }
122  mConditionMap.insert( mCondCount, cond );
123  ++mCondCount;
124 }
125 
127 {
128  // hide the remove button of the last condition when 2nd last is removed
129  if ( mConditionMap.count() == 2 )
130  {
131  const auto constMConditionMap = mConditionMap;
132  for ( QgsSmartGroupCondition *condition : constMConditionMap )
133  {
134  condition->hideRemoveButton( true );
135  }
136  }
137 
138  QgsSmartGroupCondition *cond = mConditionMap.take( id );
139  delete cond;
140 }
141 
143 {
144  QgsSmartConditionMap conditions;
145 
146  const auto constMConditionMap = mConditionMap;
147  for ( QgsSmartGroupCondition *condition : constMConditionMap )
148  {
149  conditions.insert( condition->constraint(), condition->parameter() );
150  }
151 
152  return conditions;
153 }
154 
156 {
157  return mAndOrCombo->currentData().toString();
158 }
159 
161 {
162  QStringList constraints;
163  constraints << QStringLiteral( "tag" ) << QStringLiteral( "name" ) << QStringLiteral( "!tag" ) << QStringLiteral( "!name" );
164 
165  // clear any defaults
166  qDeleteAll( mConditionMap );
167  mConditionMap.clear();
168 
169  //set the constraints
170  const auto constConstraints = constraints;
171  for ( const QString &constr : constConstraints )
172  {
173  const QStringList params = map.values( constr );
174  const auto constParams = params;
175  for ( const QString &param : constParams )
176  {
178  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
179 
180  cond->setConstraint( constr );
181  cond->setParameter( param );
182 
184 
185  mConditionMap.insert( mCondCount, cond );
186  ++mCondCount;
187  }
188  }
189 }
190 
191 void QgsSmartGroupEditorDialog::setOperator( const QString &op )
192 {
193  mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
194 }
195 
197 {
198  mNameLineEdit->setText( name );
199 }
200 
201 void QgsSmartGroupEditorDialog::buttonBox_accepted()
202 {
203  if ( mNameLineEdit->text().isEmpty() )
204  {
205  QMessageBox::critical( this, tr( "Edit Smart Group" ), tr( "The smart group name field is empty. Kindly provide a name." ) );
206  return;
207  }
208  accept();
209 }
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:168
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 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