QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgsstylev2.h"
19 #include "qgsapplication.h"
20 
21 #include <QVariant>
22 #include <QMessageBox>
23 
24 // -------------------------- //
25 // Condition Widget functions //
26 // -------------------------- //
28 {
29  setupUi( this );
30 
31  mConditionId = id;
32 
33  mCondCombo->addItem( tr( "has the tag" ), QVariant( "tag" ) );
34  mCondCombo->addItem( tr( "is a member of group" ), QVariant( "group" ) );
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( "is NOT a member of group" ), QVariant( "!group" ) );
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, SIGNAL( clicked() ), this, SLOT( destruct() ) );
43 }
44 
46 {
47  emit removed( mConditionId );
48 }
49 
51 {
52  return mCondCombo->itemData( mCondCombo->currentIndex() ).toString();
53 }
54 
56 {
57  return mCondLineEdit->text();
58 }
59 
61 {
62  mCondCombo->setCurrentIndex( mCondCombo->findData( QVariant( constraint ) ) );
63 }
64 
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 );
84 
85  mCondCount = 0;
86 
87  mAndOrCombo->addItem( tr( "ALL the constraints" ), QVariant( "AND" ) );
88  mAndOrCombo->addItem( tr( "any ONE of the constraints" ), QVariant( "OR" ) );
89 
90  mLayout = new QGridLayout( mConditionsBox );
91  addCondition();
92 
93  connect( mAddConditionBtn, SIGNAL( clicked() ), this, SLOT( addCondition() ) );
94 }
95 
97 {
98 }
99 
101 {
102  return mNameLineEdit->text();
103 }
104 
106 {
107  // enable the remove buttons when 2nd condition is added
108  if ( mConditionMap.count() == 1 )
109  {
110  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
111  {
112  condition->hideRemoveButton( false );
113  }
114  }
116  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
117 
118  connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
119  if ( mConditionMap.isEmpty() )
120  {
121  cond->hideRemoveButton( true );
122  }
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  Q_FOREACH ( QgsSmartGroupCondition* condition, mConditionMap )
133  {
134  condition->hideRemoveButton( true );
135  }
136  }
137 
139  delete cond;
140 }
141 
143 {
144  QgsSmartConditionMap conditions;
145 
146  Q_FOREACH ( QgsSmartGroupCondition* condition, mConditionMap )
147  {
148  conditions.insert( condition->constraint(), condition->parameter() );
149  }
150 
151  return conditions;
152 }
153 
155 {
156  return mAndOrCombo->itemData( mAndOrCombo->currentIndex() ).toString();
157 }
158 
160 {
161  QStringList constraints;
162  constraints << "tag" << "group" << "name" << "!tag" << "!group" << "!name";
163 
164  // clear any defaults
165  Q_FOREACH ( int id, mConditionMap.keys() )
166  {
168  delete cond;
169  }
170 
171  //set the constraints
172  Q_FOREACH ( const QString &constr, constraints )
173  {
174  QStringList params = map.values( constr );
175  Q_FOREACH ( const QString &param, params )
176  {
178  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
179 
180  cond->setConstraint( constr );
181  cond->setParameter( param );
182 
183  connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
184 
186  ++mCondCount;
187  }
188  }
189 }
190 
192 {
193  mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
194 }
195 
197 {
198  mNameLineEdit->setText( name );
199 }
200 
202 {
203  if ( mNameLineEdit->text().isEmpty() )
204  {
205  QMessageBox::critical( this, tr( "Invalid name" ), tr( "The smart group name field is empty. Kindly provide a name" ) );
206  return;
207  }
208  accept();
209 }
void setParameter(const QString &param)
sets the given param
void setupUi(QWidget *widget)
QList< T > values() const
void addWidget(QWidget *widget, int row, int column, QFlags< Qt::AlignmentFlag > alignment)
QgsSmartConditionMap conditionMap()
returns the condition map
QStyle * style() const
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 tr(const char *sourceText, const char *disambiguation, int n)
QList< Key > keys() const
QgsSmartGroupEditorDialog(QgsStyleV2 *style, QWidget *parent=nullptr)
const char * name() const
QMap< Key, T >::iterator insert(const Key &key, const T &value)
void hide()
QString parameter()
returns the parameter
virtual void accept()
QMap< int, QgsSmartGroupCondition * > mConditionMap
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
StandardButton critical(QWidget *parent, const QString &title, const QString &text, QFlags< QMessageBox::StandardButton > buttons, StandardButton defaultButton)
void setSmartgroupName(const QString &)
sets the smart group Name
iterator insert(const Key &key, const T &value)
bool isEmpty() const
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const
QgsSmartGroupCondition(int id, QWidget *parent=nullptr)
void setConditionMap(const QgsSmartConditionMap &)
sets up the GUI for the given conditionmap
int count(const Key &key) const
T take(const Key &key)
QString conditionOperator()
returns the AND/OR condition