21 #include "qgssettings.h"
23 #include <QFileDialog>
24 #include <QMessageBox>
25 #include <QTextStream>
35 tableWidget->insertRow( 0 );
37 tableWidget->horizontalHeader()->setSectionsClickable(
true );
38 tableWidget->setSortingEnabled(
true );
40 connect( addNullButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::addNullButtonPushed );
41 connect( removeSelectedButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::removeSelectedButtonPushed );
42 connect( loadFromLayerButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::loadFromLayerButtonPushed );
43 connect( loadFromCSVButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::loadFromCSVButtonPushed );
44 connect( tableWidget, &QTableWidget::cellChanged,
this, &QgsValueMapConfigDlg::vCellChanged );
45 tableWidget->installEventFilter(
this );
50 QList<QVariant> valueList;
53 for (
int i = 0; i < tableWidget->rowCount() - 1; i++ )
55 QTableWidgetItem *ki = tableWidget->item( i, 0 );
56 QTableWidgetItem *vi = tableWidget->item( i, 1 );
61 QString ks = ki->text();
67 if ( !vi || vi->text().isNull() )
69 value.insert( ks, ks );
73 value.insert( vi->text(), ks );
75 valueList.append( value );
79 cfg.insert( QStringLiteral(
"map" ), valueList );
85 tableWidget->clearContents();
86 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
88 tableWidget->removeRow( i );
91 QList<QVariant> valueList =
config.value( QStringLiteral(
"map" ) ).toList();
93 if ( valueList.count() > 0 )
95 for (
int i = 0, row = 0; i < valueList.count(); i++, row++ )
97 setRow( row, valueList[i].toMap().constBegin().value().toString(), valueList[i].toMap().constBegin().key() );
103 QVariantMap values =
config.value( QStringLiteral(
"map" ) ).toMap();
104 for ( QVariantMap::ConstIterator mit = values.constBegin(); mit != values.constEnd(); mit++, row++ )
106 if ( mit.value().isNull() )
107 setRow( row, mit.key(), QString() );
109 setRow( row, mit.value().toString(), mit.key() );
114 void QgsValueMapConfigDlg::vCellChanged(
int row,
int column )
117 if ( row == tableWidget->rowCount() - 1 )
119 tableWidget->insertRow( row + 1 );
125 void QgsValueMapConfigDlg::removeSelectedButtonPushed()
127 QList<QTableWidgetItem *> list = tableWidget->selectedItems();
128 QSet<int> rowsToRemove;
131 for ( i = 0; i < list.size(); i++ )
133 if ( list[i]->column() == 0 )
135 int row = list[i]->row();
136 if ( !rowsToRemove.contains( row ) )
138 rowsToRemove.insert( row );
142 for ( i = 0; i < rowsToRemove.size(); i++ )
144 tableWidget->removeRow( rowsToRemove.values().at( i ) - removed );
152 QList<QPair<QString, QVariant>> orderedMap;
153 auto end = map.constEnd();
154 for (
auto it = map.constBegin(); it != end; ++it )
156 orderedMap.append( qMakePair( it.key(), it.value() ) );
164 tableWidget->clearContents();
165 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
167 tableWidget->removeRow( i );
177 for (
const auto &pair : list )
179 if ( pair.second.isNull() )
180 setRow( row, pair.first, QString() );
182 setRow( row, pair.first, pair.second.toString() );
189 const QList<QVariant> valueList =
config.value( QStringLiteral(
"map" ) ).toList();
191 if ( !valueList.empty() )
193 for (
const QVariant &value : valueList )
195 const QVariantMap valueMap = value.toMap();
200 comboBox->addItem( valueMap.constBegin().key(), valueMap.constBegin().value() );
205 const QVariantMap map =
config.value( QStringLiteral(
"map" ) ).toMap();
206 for (
auto it = map.constBegin(); it != map.constEnd(); ++it )
211 comboBox->addItem( it.key(), it.value() );
219 if ( event->type() == QEvent::KeyPress )
221 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
222 if ( keyEvent->matches( QKeySequence::Copy ) )
224 copySelectionToClipboard();
232 void QgsValueMapConfigDlg::setRow(
int row,
const QString &value,
const QString &description )
234 QgsSettings settings;
235 QTableWidgetItem *valueCell =
nullptr;
236 QTableWidgetItem *descriptionCell =
new QTableWidgetItem( description );
237 tableWidget->insertRow( row );
241 cellFont.setItalic(
true );
243 valueCell->setFont( cellFont );
244 valueCell->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
245 descriptionCell->setFont( cellFont );
249 valueCell =
new QTableWidgetItem( value );
251 tableWidget->setItem( row, 0, valueCell );
252 tableWidget->setItem( row, 1, descriptionCell );
255 void QgsValueMapConfigDlg::copySelectionToClipboard()
257 QAbstractItemModel *model = tableWidget->model();
258 QItemSelectionModel *selection = tableWidget->selectionModel();
259 const QModelIndexList indexes = selection->selectedIndexes();
261 QString clipboardText;
262 QModelIndex previous = indexes.first();
263 std::unique_ptr<QMimeData> mimeData = std::make_unique<QMimeData>();
264 for (
const QModelIndex ¤t : indexes )
266 const QString text = model->data( current ).toString();
267 if ( current.row() != previous.row() )
269 clipboardText.append(
'\n' );
271 else if ( current.column() != previous.column() )
273 clipboardText.append(
'\t' );
275 clipboardText.append( text );
278 mimeData->setData( QStringLiteral(
"text/plain" ), clipboardText.toUtf8() );
279 QApplication::clipboard()->setMimeData( mimeData.release() );
282 void QgsValueMapConfigDlg::addNullButtonPushed()
287 void QgsValueMapConfigDlg::loadFromLayerButtonPushed()
290 if ( !layerDialog.exec() )
293 updateMap( layerDialog.valueMap(), layerDialog.insertNull() );
296 void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
298 QgsSettings settings;
300 QString fileName = QFileDialog::getOpenFileName(
nullptr, tr(
"Load Value Map from File" ), QDir::homePath() );
301 if ( fileName.isNull() )
306 if ( !f.open( QIODevice::ReadOnly ) )
308 QMessageBox::information(
nullptr,
309 tr(
"Load Value Map from File" ),
310 tr(
"Could not open file %1\nError was: %2" ).arg( fileName, f.errorString() ),
311 QMessageBox::Cancel );
316 s.setAutoDetectUnicode(
true );
318 QRegExp re0(
"^([^;]*);(.*)$" );
319 re0.setMinimal(
true );
320 QRegExp re1(
"^([^,]*),(.*)$" );
321 re1.setMinimal(
true );
323 QList<QPair<QString, QVariant>> map;
327 QString l = s.readLine().trimmed();
332 if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
334 key = re0.cap( 1 ).trimmed();
335 val = re0.cap( 2 ).trimmed();
337 else if ( re1.indexIn( l ) >= 0 && re1.captureCount() == 2 )
339 key = re1.cap( 1 ).trimmed();
340 val = re1.cap( 2 ).trimmed();
345 if ( ( key.startsWith(
'\"' ) && key.endsWith(
'\"' ) ) ||
346 ( key.startsWith(
'\'' ) && key.endsWith(
'\'' ) ) )
348 key = key.mid( 1, key.length() - 2 );
351 if ( ( val.startsWith(
'\"' ) && val.endsWith(
'\"' ) ) ||
352 ( val.startsWith(
'\'' ) && val.endsWith(
'\'' ) ) )
354 val = val.mid( 1, val.length() - 2 );
360 map.append( qMakePair( key, val ) );
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
bool eventFilter(QObject *watched, QEvent *event) override
QgsValueMapConfigDlg(QgsVectorLayer *vl, int fieldIdx, QWidget *parent)
void updateMap(const QMap< QString, QVariant > &map, bool insertNull)
Updates the displayed table with the values from map.
static void populateComboBox(QComboBox *comboBox, const QVariantMap &configuration, bool skipNull)
Populates a comboBox with the appropriate entries based on a value map configuration.
QVariantMap config() override
Create a configuration from the current GUI state.
Represents a vector layer which manages a vector based data sets.