QGIS API Documentation  2.14.0-Essen
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 ), mStyle( style )
81 {
82  setupUi( this );
83 
84  mCondCount = 0;
85 
86  mAndOrCombo->addItem( tr( "ALL the constraints" ), QVariant( "AND" ) );
87  mAndOrCombo->addItem( tr( "any ONE of the constraints" ), QVariant( "OR" ) );
88 
89  mLayout = new QGridLayout( mConditionsBox );
90  addCondition();
91 
92  connect( mAddConditionBtn, SIGNAL( clicked() ), this, SLOT( addCondition() ) );
93 }
94 
96 {
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  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
110  {
111  condition->hideRemoveButton( false );
112  }
113  }
115  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
116 
117  connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
118  if ( mConditionMap.isEmpty() )
119  {
120  cond->hideRemoveButton( true );
121  }
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  Q_FOREACH ( QgsSmartGroupCondition* condition, mConditionMap )
132  {
133  condition->hideRemoveButton( true );
134  }
135  }
136 
138  delete cond;
139 }
140 
142 {
143  QgsSmartConditionMap conditions;
144 
145  Q_FOREACH ( QgsSmartGroupCondition* condition, mConditionMap )
146  {
147  conditions.insert( condition->constraint(), condition->parameter() );
148  }
149 
150  return conditions;
151 }
152 
154 {
155  return mAndOrCombo->itemData( mAndOrCombo->currentIndex() ).toString();
156 }
157 
159 {
160  QStringList constraints;
161  constraints << "tag" << "group" << "name" << "!tag" << "!group" << "!name";
162 
163  // clear any defaults
164  Q_FOREACH ( int id, mConditionMap.keys() )
165  {
167  delete cond;
168  }
169 
170  //set the constraints
171  Q_FOREACH ( const QString &constr, constraints )
172  {
173  QStringList params = map.values( constr );
174  Q_FOREACH ( const QString &param, params )
175  {
177  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
178 
179  cond->setConstraint( constr );
180  cond->setParameter( param );
181 
182  connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
183 
185  ++mCondCount;
186  }
187  }
188 }
189 
191 {
192  mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
193 }
194 
196 {
197  mNameLineEdit->setText( name );
198 }
199 
201 {
202  if ( mNameLineEdit->text().isEmpty() )
203  {
204  QMessageBox::critical( this, tr( "Invalid name" ), tr( "The smart group name field is empty. Kindly provide a name" ) );
205  return;
206  }
207  accept();
208 }
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