QGIS API Documentation  2.99.0-Master (e077efd)
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, SIGNAL( clicked() ), this, SLOT( 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, SIGNAL( clicked() ), this, SLOT( addCondition() ) );
92 }
93 
95 {
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  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
109  {
110  condition->hideRemoveButton( false );
111  }
112  }
114  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
115 
116  connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
117  if ( mConditionMap.isEmpty() )
118  {
119  cond->hideRemoveButton( true );
120  }
121  mConditionMap.insert( mCondCount, cond );
122  ++mCondCount;
123 }
124 
126 {
127  // hide the remove button of the last condition when 2nd last is removed
128  if ( mConditionMap.count() == 2 )
129  {
130  Q_FOREACH ( QgsSmartGroupCondition* condition, mConditionMap )
131  {
132  condition->hideRemoveButton( true );
133  }
134  }
135 
136  QgsSmartGroupCondition *cond = mConditionMap.take( id );
137  delete cond;
138 }
139 
141 {
142  QgsSmartConditionMap conditions;
143 
144  Q_FOREACH ( QgsSmartGroupCondition* condition, mConditionMap )
145  {
146  conditions.insert( condition->constraint(), condition->parameter() );
147  }
148 
149  return conditions;
150 }
151 
153 {
154  return mAndOrCombo->currentData().toString();
155 }
156 
158 {
159  QStringList constraints;
160  constraints << QStringLiteral( "tag" ) << QStringLiteral( "name" ) << QStringLiteral( "!tag" ) << QStringLiteral( "!name" );
161 
162  // clear any defaults
163  Q_FOREACH ( int id, mConditionMap.keys() )
164  {
165  QgsSmartGroupCondition *cond = mConditionMap.take( id );
166  delete cond;
167  }
168 
169  //set the constraints
170  Q_FOREACH ( const QString &constr, constraints )
171  {
172  QStringList params = map.values( constr );
173  Q_FOREACH ( const QString &param, params )
174  {
176  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
177 
178  cond->setConstraint( constr );
179  cond->setParameter( param );
180 
181  connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
182 
183  mConditionMap.insert( mCondCount, cond );
184  ++mCondCount;
185  }
186  }
187 }
188 
189 void QgsSmartGroupEditorDialog::setOperator( const QString& op )
190 {
191  mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
192 }
193 
195 {
196  mNameLineEdit->setText( name );
197 }
198 
200 {
201  if ( mNameLineEdit->text().isEmpty() )
202  {
203  QMessageBox::critical( this, tr( "Invalid name" ), tr( "The smart group name field is empty. Kindly provide a name" ) );
204  return;
205  }
206  accept();
207 }
void setParameter(const QString &param)
sets the given param
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
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
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:54
QString conditionOperator()
returns the AND/OR condition