Index: src/app/qgsattributetypedialog.h =================================================================== --- src/app/qgsattributetypedialog.h (revision 11344) +++ src/app/qgsattributetypedialog.h (working copy) @@ -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 &map ); + QMap mValueMap; QgsVectorLayer *mLayer; Index: src/app/qgsattributetypedialog.cpp =================================================================== --- src/app/qgsattributetypedialog.cpp (revision 11344) +++ src/app/qgsattributetypedialog.cpp (working copy) @@ -24,6 +24,9 @@ #include "qgslogger.h" #include +#include +#include +#include #include #include @@ -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 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 &map ) +{ tableWidget->clearContents(); for ( int i = tableWidget->rowCount() - 1; i > 0; i-- ) { tableWidget->removeRow( i ); } int row = 0; - QMap &map = layerDialog.valueMap(); for ( QMap::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() ) ); } } - } Index: src/ui/qgsattributetypeedit.ui =================================================================== --- src/ui/qgsattributetypeedit.ui (revision 11344) +++ src/ui/qgsattributetypeedit.ui (working copy) @@ -320,7 +320,7 @@ - + Combo box with predefined items. Value is stored in the attribute, description is shown in the combo box. @@ -337,7 +337,7 @@ - + Qt::Horizontal @@ -350,7 +350,7 @@ - + @@ -371,7 +371,7 @@ - + Qt::Horizontal @@ -384,6 +384,13 @@ + + + + Load Data from CSV file + + +