Quantum GIS API Documentation  master-ce49b66
src/gui/symbology-ng/qgssmartgroupeditordialog.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002     qgssmartgroupeditordialog.cpp
00003     -----------------------------
00004     begin                : July 2012
00005     copyright            : (C) 2012 by Arunmozhi
00006     email                : aruntheguy at gmail.com
00007  ***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 
00016 #include "qgssmartgroupeditordialog.h"
00017 
00018 #include "qgsstylev2.h"
00019 #include "qgsapplication.h"
00020 
00021 #include <QVariant>
00022 #include <QMessageBox>
00023 
00024 // -------------------------- //
00025 // Condition Widget functions //
00026 // -------------------------- //
00027 QgsSmartGroupCondition::QgsSmartGroupCondition( int id, QWidget* parent ) : QWidget( parent )
00028 {
00029   setupUi( this );
00030 
00031   mConditionId = id;
00032 
00033   mCondCombo->addItem( "has the tag", QVariant( "tag" ) );
00034   mCondCombo->addItem( "is a member of group", QVariant( "group" ) );
00035   mCondCombo->addItem( "has a part of name matching", QVariant( "name" ) );
00036   mCondCombo->addItem( "does NOT have the tag", QVariant( "!tag" ) );
00037   mCondCombo->addItem( "is NOT a member of group", QVariant( "!group" ) );
00038   mCondCombo->addItem( "has NO part of name matching", QVariant( "!name" ) );
00039 
00040   mRemoveBtn->setIcon( QIcon( QgsApplication::iconPath( "symbologyRemove.png" ) ) );
00041 
00042   connect( mRemoveBtn, SIGNAL( clicked() ), this, SLOT( destruct() ) );
00043 }
00044 
00045 void QgsSmartGroupCondition::destruct()
00046 {
00047   emit removed( mConditionId );
00048 }
00049 
00050 QString QgsSmartGroupCondition::constraint()
00051 {
00052   return mCondCombo->itemData( mCondCombo->currentIndex() ).toString();
00053 }
00054 
00055 QString QgsSmartGroupCondition::parameter()
00056 {
00057   return mCondLineEdit->text();
00058 }
00059 
00060 void QgsSmartGroupCondition::setConstraint( QString constraint )
00061 {
00062   mCondCombo->setCurrentIndex( mCondCombo->findData( QVariant( constraint ) ) );
00063 }
00064 
00065 void QgsSmartGroupCondition::setParameter( QString param )
00066 {
00067   mCondLineEdit->setText( param );
00068 }
00069 
00070 void QgsSmartGroupCondition::hideRemoveButton( bool hide )
00071 {
00072   mRemoveBtn->setVisible( !hide );
00073 }
00074 
00075 
00076 // ------------------------ //
00077 // Editor Dialog Functions  //
00078 // ------------------------ //
00079 QgsSmartGroupEditorDialog::QgsSmartGroupEditorDialog( QgsStyleV2* style, QWidget* parent )
00080     : QDialog( parent ), mStyle( style )
00081 {
00082   setupUi( this );
00083 
00084   mCondCount = 0;
00085 
00086   mAndOrCombo->addItem( "ALL the constraints", QVariant( "AND" ) );
00087   mAndOrCombo->addItem( "any ONE of the constraints", QVariant( "OR" ) );
00088 
00089   mLayout = new QGridLayout( mConditionsBox );
00090   addCondition();
00091 
00092   connect( mAddConditionBtn, SIGNAL( clicked() ), this, SLOT( addCondition() ) );
00093 }
00094 
00095 QgsSmartGroupEditorDialog::~QgsSmartGroupEditorDialog()
00096 {
00097 }
00098 
00099 QString QgsSmartGroupEditorDialog::smartgroupName()
00100 {
00101   return mNameLineEdit->text();
00102 }
00103 
00104 void QgsSmartGroupEditorDialog::addCondition()
00105 {
00106   // enable the remove buttons when 2nd condition is added
00107   if ( mConditionMap.count() == 1 )
00108   {
00109     foreach ( QgsSmartGroupCondition *condition, mConditionMap.values() )
00110     {
00111       condition->hideRemoveButton( false );
00112     }
00113   }
00114   QgsSmartGroupCondition *cond = new QgsSmartGroupCondition( mCondCount, this );
00115   mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
00116 
00117   connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
00118   if ( mConditionMap.count() == 0 )
00119   {
00120     cond->hideRemoveButton( true );
00121   }
00122   mConditionMap.insert( mCondCount, cond );
00123   ++mCondCount;
00124 }
00125 
00126 void QgsSmartGroupEditorDialog::removeCondition( int id )
00127 {
00128   // hide the remove button of the last condition when 2nd last is removed
00129   if ( mConditionMap.count() == 2 )
00130   {
00131     foreach ( QgsSmartGroupCondition* condition, mConditionMap.values() )
00132     {
00133       condition->hideRemoveButton( true );
00134     }
00135   }
00136 
00137   QgsSmartGroupCondition *cond = mConditionMap.take( id );
00138   delete cond;
00139 }
00140 
00141 QgsSmartConditionMap QgsSmartGroupEditorDialog::conditionMap()
00142 {
00143   QgsSmartConditionMap conditions;
00144 
00145   foreach ( QgsSmartGroupCondition* condition, mConditionMap.values() )
00146   {
00147     conditions.insert( condition->constraint(), condition->parameter() );
00148   }
00149 
00150   return conditions;
00151 }
00152 
00153 QString QgsSmartGroupEditorDialog::conditionOperator()
00154 {
00155   return mAndOrCombo->itemData( mAndOrCombo->currentIndex() ).toString();
00156 }
00157 
00158 void QgsSmartGroupEditorDialog::setConditionMap( QgsSmartConditionMap map )
00159 {
00160   QStringList constraints;
00161   constraints << "tag" << "group" << "name" << "!tag" << "!group" << "!name" ;
00162 
00163   // clear any defaults
00164   foreach ( int id, mConditionMap.keys() )
00165   {
00166     QgsSmartGroupCondition *cond = mConditionMap.take( id );
00167     delete cond;
00168   }
00169 
00170   //set the constraints
00171   foreach ( const QString &constr, constraints )
00172   {
00173     QStringList params = map.values( constr );
00174     foreach ( const QString &param, params )
00175     {
00176       QgsSmartGroupCondition *cond = new QgsSmartGroupCondition( mCondCount, this );
00177       mLayout->addWidget( cond, mCondCount, 0, 1, 1 );
00178 
00179       cond->setConstraint( constr );
00180       cond->setParameter( param );
00181 
00182       connect( cond, SIGNAL( removed( int ) ), this, SLOT( removeCondition( int ) ) );
00183 
00184       mConditionMap.insert( mCondCount, cond );
00185       ++mCondCount;
00186     }
00187   }
00188 }
00189 
00190 void QgsSmartGroupEditorDialog::setOperator( QString op )
00191 {
00192   mAndOrCombo->setCurrentIndex( mAndOrCombo->findData( QVariant( op ) ) );
00193 }
00194 
00195 void QgsSmartGroupEditorDialog::setSmartgroupName( QString name )
00196 {
00197   mNameLineEdit->setText( name );
00198 }
00199 
00200 void QgsSmartGroupEditorDialog::on_buttonBox_accepted()
00201 {
00202   if ( mNameLineEdit->text().isEmpty() )
00203   {
00204     QMessageBox::critical( this, tr( "Invalid name" ), tr( "The smart group name field is empty. Kindly provide a name" ) );
00205     return;
00206   }
00207   accept();
00208 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines