21 #include <QFileDialog>
22 #include <QMessageBox>
23 #include <QTextStream>
30 tableWidget->insertRow( 0 );
32 connect( removeSelectedButton, SIGNAL( clicked() ),
this, SLOT( removeSelectedButtonPushed() ) );
33 connect( loadFromLayerButton, SIGNAL( clicked() ),
this, SLOT( loadFromLayerButtonPushed() ) );
34 connect( loadFromCSVButton, SIGNAL( clicked() ),
this, SLOT( loadFromCSVButtonPushed() ) );
35 connect( tableWidget, SIGNAL( cellChanged(
int,
int ) ),
this, SLOT( vCellChanged(
int,
int ) ) );
43 for (
int i = 0; i < tableWidget->rowCount() - 1; i++ )
45 QTableWidgetItem *ki = tableWidget->item( i, 0 );
46 QTableWidgetItem *vi = tableWidget->item( i, 1 );
51 if ( !vi || vi->text().isNull() )
53 cfg.insert( ki->text(), ki->text() );
57 cfg.insert( vi->text(), ki->text() );
66 tableWidget->clearContents();
67 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
69 tableWidget->removeRow( i );
73 for ( QgsEditorWidgetConfig::ConstIterator mit = config.begin(); mit != config.end(); mit++, row++ )
75 tableWidget->insertRow( row );
76 if ( mit.value().isNull() )
78 tableWidget->setItem( row, 0,
new QTableWidgetItem( mit.key() ) );
82 tableWidget->setItem( row, 0,
new QTableWidgetItem( mit.value().toString() ) );
83 tableWidget->setItem( row, 1,
new QTableWidgetItem( mit.key() ) );
88 void QgsValueMapConfigDlg::vCellChanged(
int row,
int column )
91 if ( row == tableWidget->rowCount() - 1 )
93 tableWidget->insertRow( row + 1 );
97 void QgsValueMapConfigDlg::removeSelectedButtonPushed()
99 QList<QTableWidgetItem *> list = tableWidget->selectedItems();
100 QSet<int> rowsToRemove;
103 for ( i = 0; i < list.size(); i++ )
105 if ( list[i]->column() == 0 )
107 int row = list[i]->row();
108 if ( !rowsToRemove.contains( row ) )
110 rowsToRemove.insert( row );
114 for ( i = 0; i < rowsToRemove.values().size(); i++ )
116 tableWidget->removeRow( rowsToRemove.values()[i] - removed );
123 tableWidget->clearContents();
124 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
126 tableWidget->removeRow( i );
133 tableWidget->setItem( row, 0,
new QTableWidgetItem( settings.value(
"qgis/nullValue",
"NULL" ).toString() ) );
134 tableWidget->setItem( row, 1,
new QTableWidgetItem(
"<NULL>" ) );
138 for ( QMap<QString, QVariant>::const_iterator mit = map.begin(); mit != map.end(); ++mit, ++row )
140 tableWidget->insertRow( row );
141 if ( mit.value().isNull() )
143 tableWidget->setItem( row, 0,
new QTableWidgetItem( mit.key() ) );
147 tableWidget->setItem( row, 0,
new QTableWidgetItem( mit.key() ) );
148 tableWidget->setItem( row, 1,
new QTableWidgetItem( mit.value().toString() ) );
153 void QgsValueMapConfigDlg::loadFromLayerButtonPushed()
156 if ( !layerDialog.exec() )
159 updateMap( layerDialog.valueMap(), layerDialog.insertNull() );
162 void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
164 QString fileName = QFileDialog::getOpenFileName( 0,
tr(
"Select a file" ) );
165 if ( fileName.isNull() )
170 if ( !f.open( QIODevice::ReadOnly ) )
172 QMessageBox::information( NULL,
174 tr(
"Could not open file %1\nError was:%2" ).arg( fileName ).arg( f.errorString() ),
175 QMessageBox::Cancel );
180 s.setAutoDetectUnicode(
true );
182 QRegExp re0(
"^([^;]*);(.*)$" );
183 re0.setMinimal(
true );
184 QRegExp re1(
"^([^,]*),(.*)$" );
185 re1.setMinimal(
true );
186 QMap<QString, QVariant> map;
192 QString l = s.readLine().trimmed();
195 if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
197 key = re0.cap( 1 ).trimmed();
198 val = re0.cap( 2 ).trimmed();
200 else if ( re1.indexIn( l ) >= 0 && re1.captureCount() == 2 )
202 key = re1.cap( 1 ).trimmed();
203 val = re1.cap( 2 ).trimmed();
208 if (( key.startsWith(
"\"" ) && key.endsWith(
"\"" ) ) ||
209 ( key.startsWith(
"'" ) && key.endsWith(
"'" ) ) )
211 key = key.mid( 1, key.length() - 2 );
214 if (( val.startsWith(
"\"" ) && val.endsWith(
"\"" ) ) ||
215 ( val.startsWith(
"'" ) && val.endsWith(
"'" ) ) )
217 val = val.mid( 1, val.length() - 2 );