Skip to content
Permalink
Browse files

[FEATURE] add loading of value maps from csv file (apply #1869)

git-svn-id: http://svn.osgeo.org/qgis/trunk@11481 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef
jef committed Aug 22, 2009
1 parent 728f88f commit 08f5f1485156bb560da0db296210f8f7397c2a5d
Showing with 93 additions and 8 deletions.
  1. +69 −2 src/app/qgsattributetypedialog.cpp
  2. +12 −1 src/app/qgsattributetypedialog.h
  3. +12 −5 src/ui/qgsattributetypeedit.ui
@@ -24,6 +24,9 @@
#include "qgslogger.h"

#include <QTableWidgetItem>
#include <QFile>
#include <QMessageBox>
#include <QFileDialog>

#include <climits>
#include <cfloat>
@@ -37,6 +40,7 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl )
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 @@ void QgsAttributeTypeDialog::loadFromLayerButtonPushed()
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 @@ void QgsAttributeTypeDialog::loadFromLayerButtonPushed()
tableWidget->setItem( row, 1, new QTableWidgetItem( mit.value().toString() ) );
}
}

}


@@ -96,10 +96,15 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
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
@@ -123,6 +128,12 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
*/
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;

@@ -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">

0 comments on commit 08f5f14

Please sign in to comment.
You can’t perform that action at this time.