23 #include <QFileDialog> 24 #include <QMessageBox> 25 #include <QTextStream> 32 tableWidget->insertRow( 0 );
34 tableWidget->horizontalHeader()->setClickable(
true );
35 tableWidget->setSortingEnabled(
true );
37 connect( addNullButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::addNullButtonPushed );
38 connect( removeSelectedButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::removeSelectedButtonPushed );
39 connect( loadFromLayerButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::loadFromLayerButtonPushed );
40 connect( loadFromCSVButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::loadFromCSVButtonPushed );
41 connect( tableWidget, &QTableWidget::cellChanged,
this, &QgsValueMapConfigDlg::vCellChanged );
46 QList<QVariant> valueList;
49 for (
int i = 0; i < tableWidget->rowCount() - 1; i++ )
51 QTableWidgetItem *ki = tableWidget->item( i, 0 );
52 QTableWidgetItem *vi = tableWidget->item( i, 1 );
57 QString ks = ki->text();
63 if ( !vi || vi->text().isNull() )
65 value.insert( ks, ks );
69 value.insert( vi->text(), ks );
71 valueList.append( value );
75 cfg.insert( QStringLiteral(
"map" ), valueList );
81 tableWidget->clearContents();
82 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
84 tableWidget->removeRow( i );
87 QList<QVariant> valueList = config.value( QStringLiteral(
"map" ) ).toList();
89 if ( valueList.count() > 0 )
91 for (
int i = 0, row = 0; i < valueList.count(); i++, row++ )
93 setRow( row, valueList[i].toMap().constBegin().value().toString(), valueList[i].toMap().constBegin().key() );
99 QVariantMap values = config.value( QStringLiteral(
"map" ) ).toMap();
100 for ( QVariantMap::ConstIterator mit = values.constBegin(); mit != values.constEnd(); mit++, row++ )
102 if ( mit.value().isNull() )
103 setRow( row, mit.key(), QString() );
105 setRow( row, mit.value().toString(), mit.key() );
110 void QgsValueMapConfigDlg::vCellChanged(
int row,
int column )
113 if ( row == tableWidget->rowCount() - 1 )
115 tableWidget->insertRow( row + 1 );
121 void QgsValueMapConfigDlg::removeSelectedButtonPushed()
123 QList<QTableWidgetItem *> list = tableWidget->selectedItems();
124 QSet<int> rowsToRemove;
127 for ( i = 0; i < list.size(); i++ )
129 if ( list[i]->column() == 0 )
131 int row = list[i]->row();
132 if ( !rowsToRemove.contains( row ) )
134 rowsToRemove.insert( row );
138 for ( i = 0; i < rowsToRemove.size(); i++ )
140 tableWidget->removeRow( rowsToRemove.values().at( i ) - removed );
148 tableWidget->clearContents();
149 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
151 tableWidget->removeRow( i );
161 for ( QMap<QString, QVariant>::const_iterator mit = map.begin(); mit != map.end(); ++mit, ++row )
163 if ( mit.value().isNull() )
164 setRow( row, mit.key(), QString() );
166 setRow( row, mit.key(), mit.value().toString() );
172 const QList<QVariant> valueList = config.value( QStringLiteral(
"map" ) ).toList();
174 if ( !valueList.empty() )
176 for (
const QVariant &value : valueList )
178 const QVariantMap valueMap = value.toMap();
183 comboBox->addItem( valueMap.constBegin().key(), valueMap.constBegin().value() );
188 const QVariantMap map = config.value( QStringLiteral(
"map" ) ).toMap();
189 for (
auto it = map.constBegin(); it != map.constEnd(); ++it )
194 comboBox->addItem( it.key(), it.value() );
199 void QgsValueMapConfigDlg::setRow(
int row,
const QString &value,
const QString &description )
202 QTableWidgetItem *valueCell =
nullptr;
203 QTableWidgetItem *descriptionCell =
new QTableWidgetItem( description );
204 tableWidget->insertRow( row );
208 cellFont.setItalic(
true );
210 valueCell->setFont( cellFont );
211 valueCell->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
212 descriptionCell->setFont( cellFont );
216 valueCell =
new QTableWidgetItem( value );
218 tableWidget->setItem( row, 0, valueCell );
219 tableWidget->setItem( row, 1, descriptionCell );
222 void QgsValueMapConfigDlg::addNullButtonPushed()
227 void QgsValueMapConfigDlg::loadFromLayerButtonPushed()
230 if ( !layerDialog.exec() )
236 void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
240 QString fileName = QFileDialog::getOpenFileName(
nullptr, tr(
"Select a File" ), QDir::homePath() );
241 if ( fileName.isNull() )
246 if ( !f.open( QIODevice::ReadOnly ) )
248 QMessageBox::information(
nullptr,
249 tr(
"Load Value Map from File" ),
250 tr(
"Could not open file %1\nError was: %2" ).arg( fileName, f.errorString() ),
251 QMessageBox::Cancel );
256 s.setAutoDetectUnicode(
true );
258 QRegExp re0(
"^([^;]*);(.*)$" );
259 re0.setMinimal(
true );
260 QRegExp re1(
"^([^,]*),(.*)$" );
261 re1.setMinimal(
true );
262 QMap<QString, QVariant> map;
268 QString l = s.readLine().trimmed();
271 if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
273 key = re0.cap( 1 ).trimmed();
274 val = re0.cap( 2 ).trimmed();
276 else if ( re1.indexIn( l ) >= 0 && re1.captureCount() == 2 )
278 key = re1.cap( 1 ).trimmed();
279 val = re1.cap( 2 ).trimmed();
284 if ( ( key.startsWith(
'\"' ) && key.endsWith(
'\"' ) ) ||
285 ( key.startsWith(
'\'' ) && key.endsWith(
'\'' ) ) )
287 key = key.mid( 1, key.length() - 2 );
290 if ( ( val.startsWith(
'\"' ) && val.endsWith(
'\"' ) ) ||
291 ( val.startsWith(
'\'' ) && val.endsWith(
'\'' ) ) )
293 val = val.mid( 1, val.length() - 2 );
void updateMap(const QMap< QString, QVariant > &map, bool insertNull)
This class is a composition of two QSettings instances:
QgsValueMapConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
QMap< QString, QVariant > & valueMap()
Getter to value map which is currently active.
QVariantMap config() override
Create a configuration from the current GUI state.
bool insertNull()
Returns true if the "Add NULL value" checkbox has been checked.
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Represents a vector layer which manages a vector based data sets.
static void populateComboBox(QComboBox *comboBox, const QVariantMap &configuration, bool skipNull)
Populates a comboBox with the appropriate entries based on a value map configuration.