[QGIS Commit] r11481 - in trunk/qgis/src: app ui

svn_qgis at osgeo.org svn_qgis at osgeo.org
Sat Aug 22 06:20:48 EDT 2009


Author: jef
Date: 2009-08-22 06:20:48 -0400 (Sat, 22 Aug 2009)
New Revision: 11481

Modified:
   trunk/qgis/src/app/qgsattributetypedialog.cpp
   trunk/qgis/src/app/qgsattributetypedialog.h
   trunk/qgis/src/ui/qgsattributetypeedit.ui
Log:
[FEATURE] add loading of value maps from csv file (apply #1869)

Modified: trunk/qgis/src/app/qgsattributetypedialog.cpp
===================================================================
--- trunk/qgis/src/app/qgsattributetypedialog.cpp	2009-08-22 10:20:31 UTC (rev 11480)
+++ trunk/qgis/src/app/qgsattributetypedialog.cpp	2009-08-22 10:20:48 UTC (rev 11481)
@@ -24,6 +24,9 @@
 #include "qgslogger.h"
 
 #include <QTableWidgetItem>
+#include <QFile>
+#include <QMessageBox>
+#include <QFileDialog>
 
 #include <climits>
 #include <cfloat>
@@ -37,6 +40,7 @@
   connect( selectionComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setStackPage( int ) ) );
   connect( removeSelectedButton, SIGNAL( pressed( ) ), this, SLOT( removeSelectedButtonPushed( ) ) );
   connect( loadFromLayerButton, SIGNAL( pressed( ) ), this, SLOT( loadFromLayerButtonPushed( ) ) );
+  connect( loadFromCSVButton, SIGNAL( pressed( ) ), this, SLOT( loadFromCSVButtonPushed( ) ) );
   connect( tableWidget,  SIGNAL( cellChanged( int, int ) ), this, SLOT( vCellChanged( int, int ) ) );
 }
 
@@ -101,13 +105,77 @@
   if ( !layerDialog.exec() )
     return;
 
+  updateMap( layerDialog.valueMap() );
+}
+
+void QgsAttributeTypeDialog::loadFromCSVButtonPushed()
+{
+  QString fileName = QFileDialog::getOpenFileName( 0 , tr( "Select a file" ) );
+  if ( fileName.isNull() )
+    return;
+
+  QFile f( fileName );
+
+  if ( !f.open( QIODevice::ReadOnly ) )
+  {
+    QMessageBox::information( NULL,
+                              tr( "Error" ),
+                              tr( "Could not open file %1\nError was:%2" ).arg( fileName ).arg( f.errorString() ), QMessageBox::Cancel );
+    return;
+  }
+
+  QRegExp re0( "^([^;]*);(.*)$" );
+  re0.setMinimal( true );
+  QRegExp re1( "^([^,]*),(.*)$" );
+  re1.setMinimal( true );
+  QMap<QString, QVariant> map;
+
+  f.readLine();
+
+  while ( !f.atEnd() )
+  {
+    QString l = f.readLine().trimmed();
+
+    QString key, val;
+    if ( re0.indexIn( l ) >= 0 && re0.numCaptures() == 2 )
+    {
+      key = re0.cap( 1 ).trimmed();
+      val = re0.cap( 2 ).trimmed();
+    }
+    else if ( re1.indexIn( l ) >= 0 && re1.numCaptures() == 2 )
+    {
+      key = re1.cap( 1 ).trimmed();
+      val = re1.cap( 2 ).trimmed();
+    }
+    else
+      continue;
+
+    if (( key.startsWith( "\"" ) && key.endsWith( "\"" ) ) ||
+        ( key.startsWith( "'" ) && key.endsWith( "'" ) ) )
+    {
+      key = key.mid( 1, key.length() - 2 );
+    }
+
+    if (( val.startsWith( "\"" ) && val.endsWith( "\"" ) ) ||
+        ( val.startsWith( "'" ) && val.endsWith( "'" ) ) )
+    {
+      val = val.mid( 1, val.length() - 2 );
+    }
+
+    map[ key ] = val;
+  }
+
+  updateMap( map );
+}
+
+void QgsAttributeTypeDialog::updateMap( const QMap<QString, QVariant> &map )
+{
   tableWidget->clearContents();
   for ( int i = tableWidget->rowCount() - 1; i > 0; i-- )
   {
     tableWidget->removeRow( i );
   }
   int row = 0;
-  QMap<QString, QVariant> &map = layerDialog.valueMap();
   for ( QMap<QString, QVariant>::iterator mit = map.begin(); mit != map.end(); mit++, row++ )
   {
     tableWidget->insertRow( row );
@@ -121,7 +189,6 @@
       tableWidget->setItem( row, 1, new QTableWidgetItem( mit.value().toString() ) );
     }
   }
-
 }
 
 

Modified: trunk/qgis/src/app/qgsattributetypedialog.h
===================================================================
--- trunk/qgis/src/app/qgsattributetypedialog.h	2009-08-22 10:20:31 UTC (rev 11480)
+++ trunk/qgis/src/app/qgsattributetypedialog.h	2009-08-22 10:20:48 UTC (rev 11481)
@@ -96,11 +96,16 @@
     void removeSelectedButtonPushed( );
 
     /**
-     * Slot to handle load from button pushed to display dialo to load data
+     * Slot to handle load from layer button pushed to display dialog to load data
      */
     void loadFromLayerButtonPushed( );
 
     /**
+     * Slot to handle load from CSV button pushed to display dialog to load data
+     */
+    void loadFromCSVButtonPushed( );
+
+    /**
      * Slot to handle change of cell to have always empty row at end
      * @param row index of row which was changed
      * @param column index of column which was changed
@@ -123,7 +128,13 @@
      */
     void setPageForEditType( QgsVectorLayer::EditType editType );
 
+    /**
+     * Function to update the value map
+     * @param map new map
+     */
+    void updateMap( const QMap<QString, QVariant> &map );
 
+
     QMap<QString, QVariant> mValueMap;
 
     QgsVectorLayer *mLayer;

Modified: trunk/qgis/src/ui/qgsattributetypeedit.ui
===================================================================
--- trunk/qgis/src/ui/qgsattributetypeedit.ui	2009-08-22 10:20:31 UTC (rev 11480)
+++ trunk/qgis/src/ui/qgsattributetypeedit.ui	2009-08-22 10:20:48 UTC (rev 11481)
@@ -61,7 +61,7 @@
    <item>
     <widget class="QStackedWidget" name="stackedWidget">
      <property name="currentIndex">
-      <number>0</number>
+      <number>5</number>
      </property>
      <widget class="QWidget" name="lineEditPage">
       <layout class="QVBoxLayout" name="verticalLayout_9">
@@ -320,7 +320,7 @@
      </widget>
      <widget class="QWidget" name="valueMapPage">
       <layout class="QGridLayout" name="gridLayout">
-       <item row="0" column="0" colspan="2">
+       <item row="0" column="0" colspan="3">
         <widget class="QLabel" name="valueMapLabel">
          <property name="text">
           <string>Combo box with predefined items. Value is stored in the attribute, description is shown in the combo box.</string>
@@ -337,7 +337,7 @@
          </property>
         </widget>
        </item>
-       <item row="1" column="1">
+       <item row="1" column="2">
         <spacer name="horizontalSpacer">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
@@ -350,7 +350,7 @@
          </property>
         </spacer>
        </item>
-       <item row="2" column="0" colspan="2">
+       <item row="2" column="0" colspan="3">
         <widget class="QTableWidget" name="tableWidget">
          <column>
           <property name="text">
@@ -371,7 +371,7 @@
          </property>
         </widget>
        </item>
-       <item row="3" column="1">
+       <item row="3" column="1" colspan="2">
         <spacer name="horizontalSpacer_2">
          <property name="orientation">
           <enum>Qt::Horizontal</enum>
@@ -384,6 +384,13 @@
          </property>
         </spacer>
        </item>
+       <item row="1" column="1">
+        <widget class="QPushButton" name="loadFromCSVButton">
+         <property name="text">
+          <string>Load Data from CSV file</string>
+         </property>
+        </widget>
+       </item>
       </layout>
      </widget>
      <widget class="QWidget" name="enumerationPage">



More information about the QGIS-commit mailing list