[QGIS Commit] r15179 - in trunk/qgis/src: app app/attributetable core

svn_qgis at osgeo.org svn_qgis at osgeo.org
Wed Feb 16 18:11:04 EST 2011


Author: jef
Date: 2011-02-16 15:11:04 -0800 (Wed, 16 Feb 2011)
New Revision: 15179

Modified:
   trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
   trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
   trunk/qgis/src/app/qgsfieldcalculator.cpp
   trunk/qgis/src/app/qgsfieldcalculator.h
   trunk/qgis/src/core/qgssearchstringparser.yy
Log:
add support to set fields to NULL in field calculator and fix following update of attribute table

Modified: trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp	2011-02-16 11:04:49 UTC (rev 15178)
+++ trunk/qgis/src/app/attributetable/qgsattributetabledialog.cpp	2011-02-16 23:11:04 UTC (rev 15179)
@@ -674,7 +674,7 @@
   QgsSearchQueryBuilder dlg( mLayer, this );
   dlg.setSearchString( mQuery->displayText() );
 
-  if ( dlg.exec() )
+  if ( dlg.exec() == QDialog::Accepted )
     doSearch( dlg.searchString() );
 }
 
@@ -796,9 +796,19 @@
 void QgsAttributeTableDialog::on_mOpenFieldCalculator_clicked()
 {
   QgsFieldCalculator calc( mLayer );
-  calc.exec();
+  if ( calc.exec() == QDialog::Accepted )
+  {
+    int col = mModel->fieldCol( calc.changedAttributeId() );
+
+    if ( col >= 0 )
+    {
+      mModel->reload( mModel->index( 0, col ),
+                      mModel->index( mModel->rowCount(), col ) );
+    }
+  }
 }
 
+
 void QgsAttributeTableDialog::addFeature()
 {
   if ( !mLayer->isEditable() )

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2011-02-16 11:04:49 UTC (rev 15178)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.cpp	2011-02-16 23:11:04 UTC (rev 15179)
@@ -149,7 +149,7 @@
 
 void QgsAttributeTableModel::attributeValueChanged( int fid, int idx, const QVariant &value )
 {
-  setData( index( idToRow( fid ), mAttributes.indexOf( idx ) ), value, Qt::EditRole );
+  setData( index( idToRow( fid ), fieldCol( idx ) ), value, Qt::EditRole );
 }
 
 void QgsAttributeTableModel::loadAttributes()
@@ -297,6 +297,11 @@
   return mAttributes[ col ];
 }
 
+int QgsAttributeTableModel::fieldCol( int idx ) const
+{
+  return mAttributes.indexOf( idx );
+}
+
 int QgsAttributeTableModel::rowCount( const QModelIndex &parent ) const
 {
   return mRowIdMap.size();

Modified: trunk/qgis/src/app/attributetable/qgsattributetablemodel.h
===================================================================
--- trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2011-02-16 11:04:49 UTC (rev 15178)
+++ trunk/qgis/src/app/attributetable/qgsattributetablemodel.h	2011-02-16 23:11:04 UTC (rev 15179)
@@ -107,6 +107,10 @@
      */
     int fieldIdx( int col ) const;
     /**
+     * get column from field index
+     */
+    int fieldCol( int idx ) const;
+    /**
      * Maps row to feature id
      * @param row row number
      */

Modified: trunk/qgis/src/app/qgsfieldcalculator.cpp
===================================================================
--- trunk/qgis/src/app/qgsfieldcalculator.cpp	2011-02-16 11:04:49 UTC (rev 15178)
+++ trunk/qgis/src/app/qgsfieldcalculator.cpp	2011-02-16 23:11:04 UTC (rev 15179)
@@ -20,7 +20,10 @@
 #include "qgsvectorlayer.h"
 #include <QMessageBox>
 
-QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl ): QDialog(), mVectorLayer( vl )
+QgsFieldCalculator::QgsFieldCalculator( QgsVectorLayer* vl )
+    : QDialog()
+    , mVectorLayer( vl )
+    , mAttributeId( -1 )
 {
   setupUi( this );
 
@@ -92,15 +95,13 @@
 
     mVectorLayer->beginEditCommand( "Field calculator" );
 
-    int attributeId = -1; //id of the field (can be existing field or newly created one
-
     //update existing field
     if ( mUpdateExistingFieldCheckBox->checkState() == Qt::Checked )
     {
       QMap<QString, int>::const_iterator fieldIt = mFieldMap.find( mExistingFieldComboBox->currentText() );
       if ( fieldIt != mFieldMap.end() )
       {
-        attributeId = fieldIt.value();
+        mAttributeId = fieldIt.value();
       }
     }
     //create new field
@@ -128,14 +129,13 @@
       {
         if ( it.value().name() == mOutputFieldNameLineEdit->text() )
         {
-          attributeId = it.key();
+          mAttributeId = it.key();
           break;
         }
       }
     }
 
-
-    if ( attributeId == -1 )
+    if ( mAttributeId == -1 )
     {
       mVectorLayer->destroyEditCommand();
       return;
@@ -172,9 +172,9 @@
       if ( value.isError() )
       {
         //insert NULL value for this feature and continue the calculation
-        if( searchTree->errorMsg() == QObject::tr( "Division by zero." ) )
+        if ( searchTree->errorMsg() == QObject::tr( "Division by zero." ) )
         {
-          mVectorLayer->changeAttributeValue( feature.id(), attributeId, QVariant(), false );
+          mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, QVariant(), false );
         }
         else
         {
@@ -184,11 +184,15 @@
       }
       else if ( value.isNumeric() )
       {
-        mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.number(), false );
+        mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value.number(), false );
       }
+      else if ( value.isNull() )
+      {
+        mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, QVariant(), false );
+      }
       else
       {
-        mVectorLayer->changeAttributeValue( feature.id(), attributeId, value.string(), false );
+        mVectorLayer->changeAttributeValue( feature.id(), mAttributeId, value.string(), false );
       }
 
       rownum++;
@@ -499,7 +503,7 @@
 }
 
 
-void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged(QListWidgetItem * current, QListWidgetItem * previous )
+void QgsFieldCalculator::on_mFieldsListWidget_currentItemChanged( QListWidgetItem * current, QListWidgetItem * previous )
 {
   getFieldValues( 25 );
 }

Modified: trunk/qgis/src/app/qgsfieldcalculator.h
===================================================================
--- trunk/qgis/src/app/qgsfieldcalculator.h	2011-02-16 11:04:49 UTC (rev 15178)
+++ trunk/qgis/src/app/qgsfieldcalculator.h	2011-02-16 23:11:04 UTC (rev 15179)
@@ -29,6 +29,8 @@
     QgsFieldCalculator( QgsVectorLayer* vl );
     ~QgsFieldCalculator();
 
+    int changedAttributeId() const { return mAttributeId; }
+
   public slots:
     void accept();
 
@@ -60,7 +62,7 @@
     void on_mOutputFieldNameLineEdit_textChanged( const QString& text );
     void on_mExpressionTextEdit_textChanged();
     void on_mOutputFieldTypeComboBox_activated( int index );
-    void on_mFieldsListWidget_currentItemChanged(QListWidgetItem * current, QListWidgetItem * previous );
+    void on_mFieldsListWidget_currentItemChanged( QListWidgetItem * current, QListWidgetItem * previous );
 
     void on_mButtonBox_helpRequested() { QgsContextHelp::run( metaObject()->className() ); }
 
@@ -80,6 +82,9 @@
     QgsVectorLayer* mVectorLayer;
     /**Key: field name, Value: field index*/
     QMap<QString, int> mFieldMap;
+
+    /**idx of changed attribute*/
+    int mAttributeId;
 };
 
 #endif // QGSFIELDCALCULATOR_H

Modified: trunk/qgis/src/core/qgssearchstringparser.yy
===================================================================
--- trunk/qgis/src/core/qgssearchstringparser.yy	2011-02-16 11:04:49 UTC (rev 15178)
+++ trunk/qgis/src/core/qgssearchstringparser.yy	2011-02-16 23:11:04 UTC (rev 15179)
@@ -174,6 +174,7 @@
     | ID                          { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opID, 0, 0); addToTmpNodes($$); }
     | NUMBER                      { $$ = new QgsSearchTreeNode($1); addToTmpNodes($$); }
     | STRING                      { $$ = new QgsSearchTreeNode(QString::fromUtf8(yytext), 0); addToTmpNodes($$); }
+    | NULLVALUE                   { $$ = new QgsSearchTreeNode(QString::null, 0); addToTmpNodes($$); }
     | COLUMN_REF                  { $$ = new QgsSearchTreeNode(QString::fromUtf8(yytext), 1); addToTmpNodes($$); }
     | scalar_exp IN '(' scalar_exp_list ')' { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opIN, $1, $4); joinTmpNodes($$,$1,$4); }
     | scalar_exp NOT IN '(' scalar_exp_list ')' { $$ = new QgsSearchTreeNode(QgsSearchTreeNode::opNOTIN, $1, $5); joinTmpNodes($$,$1,$5); }



More information about the QGIS-commit mailing list