Skip to content

Commit

Permalink
[FEATURE] add loading of value maps from csv file (apply #1869)
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@11481 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
jef committed Aug 22, 2009
1 parent 3f6c58c commit 6b946af
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 8 deletions.
71 changes: 69 additions & 2 deletions src/app/qgsattributetypedialog.cpp
Expand Up @@ -24,6 +24,9 @@
#include "qgslogger.h"

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

#include <climits>
#include <cfloat>
Expand All @@ -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 ) ) );
}

Expand Down Expand Up @@ -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 );
Expand All @@ -121,7 +189,6 @@ void QgsAttributeTypeDialog::loadFromLayerButtonPushed()
tableWidget->setItem( row, 1, new QTableWidgetItem( mit.value().toString() ) );
}
}

}


Expand Down
13 changes: 12 additions & 1 deletion src/app/qgsattributetypedialog.h
Expand Up @@ -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
Expand All @@ -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;

Expand Down
17 changes: 12 additions & 5 deletions src/ui/qgsattributetypeedit.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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">
Expand All @@ -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>
Expand All @@ -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">
Expand Down

0 comments on commit 6b946af

Please sign in to comment.