QGIS API Documentation  3.2.0-Bonn (bc43194)
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  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsSmartGroupEditorDialog::buttonBox_accepted );
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, &QAbstractButton::clicked, this, &QgsSmartGroupEditorDialog::addCondition );
93 }
94 
96 {
97  return mNameLineEdit->text();
98 }
99 
101 {
102  // enable the remove buttons when 2nd condition is added
103  if ( mConditionMap.count() == 1 )
104  {
105  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
106  {
107  condition->hideRemoveButton( false );
108  }
109  }
111  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
112 
114  if ( mConditionMap.isEmpty() )
115  {
116  cond->hideRemoveButton( true );
117  }
118  mConditionMap.insert( mCondCount, cond );
119  ++mCondCount;
120 }
121 
123 {
124  // hide the remove button of the last condition when 2nd last is removed
125  if ( mConditionMap.count() == 2 )
126  {
127  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
128  {
129  condition->hideRemoveButton( true );
130  }
131  }
132 
133  QgsSmartGroupCondition *cond = mConditionMap.take( id );
134  delete cond;
135 }
136 
138 {
139  QgsSmartConditionMap conditions;
140 
141  Q_FOREACH ( QgsSmartGroupCondition *condition, mConditionMap )
142  {
143  conditions.insert( condition->constraint(), condition->parameter() );
144  }
145 
146  return conditions;
147 }
148 
150 {
151  return mAndOrCombo->currentData().toString();
152 }
153 
155 {
156  QStringList constraints;
157  constraints << QStringLiteral( "tag" ) << QStringLiteral( "name" ) << QStringLiteral( "!tag" ) << QStringLiteral( "!name" );
158 
159  // clear any defaults
160  qDeleteAll( mConditionMap );
161  mConditionMap.clear();
162 
163  //set the constraints
164  Q_FOREACH ( const QString &constr, constraints )
165  {
166  QStringList params = map.values( constr );
167  Q_FOREACH ( const QString &param, params )
168  {
170  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
171 
172  cond->setConstraint( constr );
173  cond->setParameter( param );
174 
176 
177  mConditionMap.insert( mCondCount, cond );
178  ++mCondCount;
179  }
180  }
181 }
182 
183 void QgsSmartGroupEditorDialog::setOperator( const QString &op )
184 {
185  mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
186 }
187 
189 {
190  mNameLineEdit->setText( name );
191 }
192 
193 void QgsSmartGroupEditorDialog::buttonBox_accepted()
194 {
195  if ( mNameLineEdit->text().isEmpty() )
196  {
197  QMessageBox::critical( this, tr( "Edit Smart Group" ), tr( "The smart group name field is empty. Kindly provide a name." ) );
198  return;
199  }
200  accept();
201 }
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:63
QString conditionOperator()
returns the AND/OR condition