QGIS API Documentation  2.8.2-Wien
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 // -------------------------- //
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( "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.png" ) ) );
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 
60 void QgsSmartGroupCondition::setConstraint( QString constraint )
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  foreach ( QgsSmartGroupCondition *condition, mConditionMap.values() )
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.count() == 0 )
119  {
120  cond->hideRemoveButton( true );
121  }
122  mConditionMap.insert( mCondCount, cond );
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  foreach ( QgsSmartGroupCondition* condition, mConditionMap.values() )
132  {
133  condition->hideRemoveButton( true );
134  }
135  }
136 
137  QgsSmartGroupCondition *cond = mConditionMap.take( id );
138  delete cond;
139 }
140 
142 {
143  QgsSmartConditionMap conditions;
144 
145  foreach ( QgsSmartGroupCondition* condition, mConditionMap.values() )
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  foreach ( int id, mConditionMap.keys() )
165  {
166  QgsSmartGroupCondition *cond = mConditionMap.take( id );
167  delete cond;
168  }
169 
170  //set the constraints
171  foreach ( const QString &constr, constraints )
172  {
173  QStringList params = map.values( constr );
174  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 
184  mConditionMap.insert( mCondCount, cond );
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 }