Skip to content

Commit 08f5f14

Browse files
author
jef
committed
[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
1 parent 728f88f commit 08f5f14

File tree

3 files changed

+93
-8
lines changed

3 files changed

+93
-8
lines changed

src/app/qgsattributetypedialog.cpp

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "qgslogger.h"
2525

2626
#include <QTableWidgetItem>
27+
#include <QFile>
28+
#include <QMessageBox>
29+
#include <QFileDialog>
2730

2831
#include <climits>
2932
#include <cfloat>
@@ -37,6 +40,7 @@ QgsAttributeTypeDialog::QgsAttributeTypeDialog( QgsVectorLayer *vl )
3740
connect( selectionComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setStackPage( int ) ) );
3841
connect( removeSelectedButton, SIGNAL( pressed( ) ), this, SLOT( removeSelectedButtonPushed( ) ) );
3942
connect( loadFromLayerButton, SIGNAL( pressed( ) ), this, SLOT( loadFromLayerButtonPushed( ) ) );
43+
connect( loadFromCSVButton, SIGNAL( pressed( ) ), this, SLOT( loadFromCSVButtonPushed( ) ) );
4044
connect( tableWidget, SIGNAL( cellChanged( int, int ) ), this, SLOT( vCellChanged( int, int ) ) );
4145
}
4246

@@ -101,13 +105,77 @@ void QgsAttributeTypeDialog::loadFromLayerButtonPushed()
101105
if ( !layerDialog.exec() )
102106
return;
103107

108+
updateMap( layerDialog.valueMap() );
109+
}
110+
111+
void QgsAttributeTypeDialog::loadFromCSVButtonPushed()
112+
{
113+
QString fileName = QFileDialog::getOpenFileName( 0 , tr( "Select a file" ) );
114+
if ( fileName.isNull() )
115+
return;
116+
117+
QFile f( fileName );
118+
119+
if ( !f.open( QIODevice::ReadOnly ) )
120+
{
121+
QMessageBox::information( NULL,
122+
tr( "Error" ),
123+
tr( "Could not open file %1\nError was:%2" ).arg( fileName ).arg( f.errorString() ), QMessageBox::Cancel );
124+
return;
125+
}
126+
127+
QRegExp re0( "^([^;]*);(.*)$" );
128+
re0.setMinimal( true );
129+
QRegExp re1( "^([^,]*),(.*)$" );
130+
re1.setMinimal( true );
131+
QMap<QString, QVariant> map;
132+
133+
f.readLine();
134+
135+
while ( !f.atEnd() )
136+
{
137+
QString l = f.readLine().trimmed();
138+
139+
QString key, val;
140+
if ( re0.indexIn( l ) >= 0 && re0.numCaptures() == 2 )
141+
{
142+
key = re0.cap( 1 ).trimmed();
143+
val = re0.cap( 2 ).trimmed();
144+
}
145+
else if ( re1.indexIn( l ) >= 0 && re1.numCaptures() == 2 )
146+
{
147+
key = re1.cap( 1 ).trimmed();
148+
val = re1.cap( 2 ).trimmed();
149+
}
150+
else
151+
continue;
152+
153+
if (( key.startsWith( "\"" ) && key.endsWith( "\"" ) ) ||
154+
( key.startsWith( "'" ) && key.endsWith( "'" ) ) )
155+
{
156+
key = key.mid( 1, key.length() - 2 );
157+
}
158+
159+
if (( val.startsWith( "\"" ) && val.endsWith( "\"" ) ) ||
160+
( val.startsWith( "'" ) && val.endsWith( "'" ) ) )
161+
{
162+
val = val.mid( 1, val.length() - 2 );
163+
}
164+
165+
map[ key ] = val;
166+
}
167+
168+
updateMap( map );
169+
}
170+
171+
void QgsAttributeTypeDialog::updateMap( const QMap<QString, QVariant> &map )
172+
{
104173
tableWidget->clearContents();
105174
for ( int i = tableWidget->rowCount() - 1; i > 0; i-- )
106175
{
107176
tableWidget->removeRow( i );
108177
}
109178
int row = 0;
110-
QMap<QString, QVariant> &map = layerDialog.valueMap();
111179
for ( QMap<QString, QVariant>::iterator mit = map.begin(); mit != map.end(); mit++, row++ )
112180
{
113181
tableWidget->insertRow( row );
@@ -121,7 +189,6 @@ void QgsAttributeTypeDialog::loadFromLayerButtonPushed()
121189
tableWidget->setItem( row, 1, new QTableWidgetItem( mit.value().toString() ) );
122190
}
123191
}
124-
125192
}
126193

127194

src/app/qgsattributetypedialog.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,15 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
9696
void removeSelectedButtonPushed( );
9797

9898
/**
99-
* Slot to handle load from button pushed to display dialo to load data
99+
* Slot to handle load from layer button pushed to display dialog to load data
100100
*/
101101
void loadFromLayerButtonPushed( );
102102

103+
/**
104+
* Slot to handle load from CSV button pushed to display dialog to load data
105+
*/
106+
void loadFromCSVButtonPushed( );
107+
103108
/**
104109
* Slot to handle change of cell to have always empty row at end
105110
* @param row index of row which was changed
@@ -123,6 +128,12 @@ class QgsAttributeTypeDialog: public QDialog, private Ui::QgsAttributeTypeDialog
123128
*/
124129
void setPageForEditType( QgsVectorLayer::EditType editType );
125130

131+
/**
132+
* Function to update the value map
133+
* @param map new map
134+
*/
135+
void updateMap( const QMap<QString, QVariant> &map );
136+
126137

127138
QMap<QString, QVariant> mValueMap;
128139

src/ui/qgsattributetypeedit.ui

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<item>
6262
<widget class="QStackedWidget" name="stackedWidget">
6363
<property name="currentIndex">
64-
<number>0</number>
64+
<number>5</number>
6565
</property>
6666
<widget class="QWidget" name="lineEditPage">
6767
<layout class="QVBoxLayout" name="verticalLayout_9">
@@ -320,7 +320,7 @@
320320
</widget>
321321
<widget class="QWidget" name="valueMapPage">
322322
<layout class="QGridLayout" name="gridLayout">
323-
<item row="0" column="0" colspan="2">
323+
<item row="0" column="0" colspan="3">
324324
<widget class="QLabel" name="valueMapLabel">
325325
<property name="text">
326326
<string>Combo box with predefined items. Value is stored in the attribute, description is shown in the combo box.</string>
@@ -337,7 +337,7 @@
337337
</property>
338338
</widget>
339339
</item>
340-
<item row="1" column="1">
340+
<item row="1" column="2">
341341
<spacer name="horizontalSpacer">
342342
<property name="orientation">
343343
<enum>Qt::Horizontal</enum>
@@ -350,7 +350,7 @@
350350
</property>
351351
</spacer>
352352
</item>
353-
<item row="2" column="0" colspan="2">
353+
<item row="2" column="0" colspan="3">
354354
<widget class="QTableWidget" name="tableWidget">
355355
<column>
356356
<property name="text">
@@ -371,7 +371,7 @@
371371
</property>
372372
</widget>
373373
</item>
374-
<item row="3" column="1">
374+
<item row="3" column="1" colspan="2">
375375
<spacer name="horizontalSpacer_2">
376376
<property name="orientation">
377377
<enum>Qt::Horizontal</enum>
@@ -384,6 +384,13 @@
384384
</property>
385385
</spacer>
386386
</item>
387+
<item row="1" column="1">
388+
<widget class="QPushButton" name="loadFromCSVButton">
389+
<property name="text">
390+
<string>Load Data from CSV file</string>
391+
</property>
392+
</widget>
393+
</item>
387394
</layout>
388395
</widget>
389396
<widget class="QWidget" name="enumerationPage">

0 commit comments

Comments
 (0)