1869_cvsvaluemaps.diff

Jürgen Fischer, 2009-08-11 02:11 PM

Download (6.01 KB)

View differences:

src/app/qgsattributetypedialog.h (working copy)
96 96
    void removeSelectedButtonPushed( );
97 97

  
98 98
    /**
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
100 100
     */
101 101
    void loadFromLayerButtonPushed( );
102 102

  
103 103
    /**
104
     * Slot to handle load from CSV button pushed to display dialog to load data
105
     */
106
    void loadFromCSVButtonPushed( );
107

  
108
    /**
104 109
     * Slot to handle change of cell to have always empty row at end
105 110
     * @param row index of row which was changed
106 111
     * @param column index of column which was changed
......
123 128
     */
124 129
    void setPageForEditType( QgsVectorLayer::EditType editType );
125 130

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

  
137

  
127 138
    QMap<QString, QVariant> mValueMap;
128 139

  
129 140
    QgsVectorLayer *mLayer;
src/app/qgsattributetypedialog.cpp (working copy)
24 24
#include "qgslogger.h"
25 25

  
26 26
#include <QTableWidgetItem>
27
#include <QFile>
28
#include <QMessageBox>
29
#include <QFileDialog>
27 30

  
28 31
#include <climits>
29 32
#include <cfloat>
......
37 40
  connect( selectionComboBox, SIGNAL( currentIndexChanged( int ) ), this, SLOT( setStackPage( int ) ) );
38 41
  connect( removeSelectedButton, SIGNAL( pressed( ) ), this, SLOT( removeSelectedButtonPushed( ) ) );
39 42
  connect( loadFromLayerButton, SIGNAL( pressed( ) ), this, SLOT( loadFromLayerButtonPushed( ) ) );
43
  connect( loadFromCSVButton, SIGNAL( pressed( ) ), this, SLOT( loadFromCSVButtonPushed( ) ) );
40 44
  connect( tableWidget,  SIGNAL( cellChanged( int, int ) ), this, SLOT( vCellChanged( int, int ) ) );
41 45
}
42 46

  
......
101 105
  if ( !layerDialog.exec() )
102 106
    return;
103 107

  
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
{
104 173
  tableWidget->clearContents();
105 174
  for ( int i = tableWidget->rowCount() - 1; i > 0; i-- )
106 175
  {
107 176
    tableWidget->removeRow( i );
108 177
  }
109 178
  int row = 0;
110
  QMap<QString, QVariant> &map = layerDialog.valueMap();
111 179
  for ( QMap<QString, QVariant>::iterator mit = map.begin(); mit != map.end(); mit++, row++ )
112 180
  {
113 181
    tableWidget->insertRow( row );
......
121 189
      tableWidget->setItem( row, 1, new QTableWidgetItem( mit.value().toString() ) );
122 190
    }
123 191
  }
124

  
125 192
}
126 193

  
127 194

  
src/ui/qgsattributetypeedit.ui (working copy)
320 320
     </widget>
321 321
     <widget class="QWidget" name="valueMapPage">
322 322
      <layout class="QGridLayout" name="gridLayout">
323
       <item row="0" column="0" colspan="2">
323
       <item row="0" column="0" colspan="3">
324 324
        <widget class="QLabel" name="valueMapLabel">
325 325
         <property name="text">
326 326
          <string>Combo box with predefined items. Value is stored in the attribute, description is shown in the combo box.</string>
......
337 337
         </property>
338 338
        </widget>
339 339
       </item>
340
       <item row="1" column="1">
340
       <item row="1" column="2">
341 341
        <spacer name="horizontalSpacer">
342 342
         <property name="orientation">
343 343
          <enum>Qt::Horizontal</enum>
......
350 350
         </property>
351 351
        </spacer>
352 352
       </item>
353
       <item row="2" column="0" colspan="2">
353
       <item row="2" column="0" colspan="3">
354 354
        <widget class="QTableWidget" name="tableWidget">
355 355
         <column>
356 356
          <property name="text">
......
371 371
         </property>
372 372
        </widget>
373 373
       </item>
374
       <item row="3" column="1">
374
       <item row="3" column="1" colspan="2">
375 375
        <spacer name="horizontalSpacer_2">
376 376
         <property name="orientation">
377 377
          <enum>Qt::Horizontal</enum>
......
384 384
         </property>
385 385
        </spacer>
386 386
       </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>
387 394
      </layout>
388 395
     </widget>
389 396
     <widget class="QWidget" name="enumerationPage">