QGIS API Documentation  2.99.0-Master (53aba61)
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 
21 #include <QVariant>
22 #include <QMessageBox>
23 
24 // -------------------------- //
25 // Condition Widget functions //
26 // -------------------------- //
27 QgsSmartGroupCondition::QgsSmartGroupCondition( int id, QWidget *parent ) : QWidget( parent )
28 {
29  setupUi( this );
30 
31  mConditionId = id;
32 
33  mCondCombo->addItem( tr( "has the tag" ), QVariant( "tag" ) );
34  mCondCombo->addItem( tr( "has a part of name matching" ), QVariant( "name" ) );
35  mCondCombo->addItem( tr( "does NOT have the tag" ), QVariant( "!tag" ) );
36  mCondCombo->addItem( tr( "has NO part of name matching" ), QVariant( "!name" ) );
37 
38  mRemoveBtn->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) );
39 
40  connect( mRemoveBtn, &QAbstractButton::clicked, this, &QgsSmartGroupCondition::destruct );
41 }
42 
44 {
45  emit removed( mConditionId );
46 }
47 
49 {
50  return mCondCombo->currentData().toString();
51 }
52 
54 {
55  return mCondLineEdit->text();
56 }
57 
59 {
60  mCondCombo->setCurrentIndex( mCondCombo->findData( QVariant( constraint ) ) );
61 }
62 
63 void QgsSmartGroupCondition::setParameter( const QString &param )
64 {
65  mCondLineEdit->setText( param );
66 }
67 
69 {
70  mRemoveBtn->setVisible( !hide );
71 }
72 
73 
74 // ------------------------ //
75 // Editor Dialog Functions //
76 // ------------------------ //
78  : QDialog( parent )
79  , mStyle( style )
80 {
81  setupUi( this );
82 
83  mCondCount = 0;
84 
85  mAndOrCombo->addItem( tr( "ALL the constraints" ), QVariant( "AND" ) );
86  mAndOrCombo->addItem( tr( "any ONE of the constraints" ), QVariant( "OR" ) );
87 
88  mLayout = new QGridLayout( mConditionsBox );
89  addCondition();
90 
91  connect( mAddConditionBtn, &QAbstractButton::clicked, this, &QgsSmartGroupEditorDialog::addCondition );
92 }
93 
95 {
96  return mNameLineEdit->text();
97 }
98 
100 {
101  // enable the remove buttons when 2nd condition is added
102  if ( mConditionMap.count() == 1 )
103  {
104  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
105  {
106  condition->hideRemoveButton( false );
107  }
108  }
110  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
111 
113  if ( mConditionMap.isEmpty() )
114  {
115  cond->hideRemoveButton( true );
116  }
117  mConditionMap.insert( mCondCount, cond );
118  ++mCondCount;
119 }
120 
122 {
123  // hide the remove button of the last condition when 2nd last is removed
124  if ( mConditionMap.count() == 2 )
125  {
126  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
127  {
128  condition->hideRemoveButton( true );
129  }
130  }
131 
132  QgsSmartGroupCondition *cond = mConditionMap.take( id );
133  delete cond;
134 }
135 
137 {
138  QgsSmartConditionMap conditions;
139 
140  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
141  {
142  conditions.insert( condition->constraint(), condition->parameter() );
143  }
144 
145  return conditions;
146 }
147 
149 {
150  return mAndOrCombo->currentData().toString();
151 }
152 
154 {
155  QStringList constraints;
156  constraints << QStringLiteral( "tag" ) << QStringLiteral( "name" ) << QStringLiteral( "!tag" ) << QStringLiteral( "!name" );
157 
158  // clear any defaults
159  Q_FOREACH ( int id, mConditionMap.keys() )
160  {
161  QgsSmartGroupCondition *cond = mConditionMap.take( id );
162  delete cond;
163  }
164 
165  //set the constraints
166  Q_FOREACH ( const QString &constr, constraints )
167  {
168  QStringList params = map.values( constr );
169  Q_FOREACH ( const QString &param, params )
170  {
172  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
173 
174  cond->setConstraint( constr );
175  cond->setParameter( param );
176 
178 
179  mConditionMap.insert( mCondCount, cond );
180  ++mCondCount;
181  }
182  }
183 }
184 
185 void QgsSmartGroupEditorDialog::setOperator( const QString &op )
186 {
187  mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
188 }
189 
191 {
192  mNameLineEdit->setText( name );
193 }
194 
196 {
197  if ( mNameLineEdit->text().isEmpty() )
198  {
199  QMessageBox::critical( this, tr( "Invalid name" ), tr( "The smart group name field is empty. Kindly provide a name" ) );
200  return;
201  }
202  accept();
203 }
void setParameter(const QString &param)
sets the given param
QMap< int, QgsSmartGroupCondition * > mConditionMap
QgsSmartGroupEditorDialog(QgsStyle *style, QWidget *parent=nullptr)
QgsSmartConditionMap conditionMap()
returns the condition map
static QString iconPath(const QString &iconFile)
Returns path to the desired icon file.
void hideRemoveButton(bool hide)
sets the remove button hidden state to &#39;hide&#39;
void addCondition()
function to create a new ConditionBox and update UI
QString smartgroupName()
returns the value from mNameLineEdit
QString parameter()
returns the parameter
QString constraint()
returns the constraint key
void removeCondition(int)
slot to remove the condition with id int
void setOperator(const QString &)
sets the operator AND/OR
void setConstraint(const QString &constraint)
sets the given constraint
void setSmartgroupName(const QString &)
sets the smart group Name
QgsSmartGroupCondition(int id, QWidget *parent=nullptr)
void setConditionMap(const QgsSmartConditionMap &)
sets up the GUI for the given conditionmap
QMultiMap< QString, QString > QgsSmartConditionMap
A multimap to hold the smart group conditions as constraint and parameter pairs.
Definition: qgsstyle.h:61
QString conditionOperator()
returns the AND/OR condition