QGIS API Documentation  3.6.0-Noosa (5873452)
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 #include "qgsgui.h"
21 
22 #include <QVariant>
23 #include <QMessageBox>
24 
25 // -------------------------- //
26 // Condition Widget functions //
27 // -------------------------- //
28 QgsSmartGroupCondition::QgsSmartGroupCondition( int id, QWidget *parent ) : QWidget( parent )
29 {
30  setupUi( this );
31 
32  mConditionId = id;
33 
34  mCondCombo->addItem( tr( "has the tag" ), QVariant( "tag" ) );
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( "has NO part of name matching" ), QVariant( "!name" ) );
38 
39  mRemoveBtn->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.svg" ) ) );
40 
41  connect( mRemoveBtn, &QAbstractButton::clicked, this, &QgsSmartGroupCondition::destruct );
42 }
43 
45 {
46  emit removed( mConditionId );
47 }
48 
50 {
51  return mCondCombo->currentData().toString();
52 }
53 
55 {
56  return mCondLineEdit->text();
57 }
58 
60 {
61  mCondCombo->setCurrentIndex( mCondCombo->findData( QVariant( constraint ) ) );
62 }
63 
64 void QgsSmartGroupCondition::setParameter( const QString &param )
65 {
66  mCondLineEdit->setText( param );
67 }
68 
70 {
71  mRemoveBtn->setVisible( !hide );
72 }
73 
74 
75 // ------------------------ //
76 // Editor Dialog Functions //
77 // ------------------------ //
79  : QDialog( parent )
80  , mStyle( style )
81 {
82  setupUi( this );
84 
85  connect( buttonBox, &QDialogButtonBox::accepted, this, &QgsSmartGroupEditorDialog::buttonBox_accepted );
86 
87  mCondCount = 0;
88 
89  mAndOrCombo->addItem( tr( "ALL the constraints" ), QVariant( "AND" ) );
90  mAndOrCombo->addItem( tr( "any ONE of the constraints" ), QVariant( "OR" ) );
91 
92  mLayout = new QGridLayout( mConditionsBox );
93  addCondition();
94 
95  connect( mAddConditionBtn, &QAbstractButton::clicked, this, &QgsSmartGroupEditorDialog::addCondition );
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 
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  qDeleteAll( mConditionMap );
164  mConditionMap.clear();
165 
166  //set the constraints
167  Q_FOREACH ( const QString &constr, constraints )
168  {
169  QStringList params = map.values( constr );
170  Q_FOREACH ( const QString &param, params )
171  {
173  mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
174 
175  cond->setConstraint( constr );
176  cond->setParameter( param );
177 
179 
180  mConditionMap.insert( mCondCount, cond );
181  ++mCondCount;
182  }
183  }
184 }
185 
186 void QgsSmartGroupEditorDialog::setOperator( const QString &op )
187 {
188  mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
189 }
190 
192 {
193  mNameLineEdit->setText( name );
194 }
195 
196 void QgsSmartGroupEditorDialog::buttonBox_accepted()
197 {
198  if ( mNameLineEdit->text().isEmpty() )
199  {
200  QMessageBox::critical( this, tr( "Edit Smart Group" ), tr( "The smart group name field is empty. Kindly provide a name." ) );
201  return;
202  }
203  accept();
204 }
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
static void enableAutoGeometryRestore(QWidget *widget, const QString &key=QString())
Register the widget to allow its position to be automatically saved and restored when open and closed...
Definition: qgsgui.cpp:104
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