23 #include <QFileDialog>
24 #include <QMessageBox>
25 #include <QTextStream>
34 tableWidget->insertRow( 0 );
36 tableWidget->horizontalHeader()->setSectionsClickable(
true );
37 tableWidget->setSortingEnabled(
true );
39 connect( addNullButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::addNullButtonPushed );
40 connect( removeSelectedButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::removeSelectedButtonPushed );
41 connect( loadFromLayerButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::loadFromLayerButtonPushed );
42 connect( loadFromCSVButton, &QAbstractButton::clicked,
this, &QgsValueMapConfigDlg::loadFromCSVButtonPushed );
43 connect( tableWidget, &QTableWidget::cellChanged,
this, &QgsValueMapConfigDlg::vCellChanged );
44 tableWidget->installEventFilter(
this );
49 QList<QVariant> valueList;
52 for (
int i = 0; i < tableWidget->rowCount() - 1; i++ )
54 QTableWidgetItem *ki = tableWidget->item( i, 0 );
55 QTableWidgetItem *vi = tableWidget->item( i, 1 );
60 QString ks = ki->text();
66 if ( !vi || vi->text().isNull() )
68 value.insert( ks, ks );
72 value.insert( vi->text(), ks );
74 valueList.append( value );
78 cfg.insert( QStringLiteral(
"map" ), valueList );
84 tableWidget->clearContents();
85 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
87 tableWidget->removeRow( i );
90 QList<QVariant> valueList =
config.value( QStringLiteral(
"map" ) ).toList();
92 if ( valueList.count() > 0 )
94 for (
int i = 0, row = 0; i < valueList.count(); i++, row++ )
96 setRow( row, valueList[i].toMap().constBegin().value().toString(), valueList[i].toMap().constBegin().key() );
102 QVariantMap values =
config.value( QStringLiteral(
"map" ) ).toMap();
103 for ( QVariantMap::ConstIterator mit = values.constBegin(); mit != values.constEnd(); mit++, row++ )
105 if ( mit.value().isNull() )
106 setRow( row, mit.key(), QString() );
108 setRow( row, mit.value().toString(), mit.key() );
113 void QgsValueMapConfigDlg::vCellChanged(
int row,
int column )
116 if ( row == tableWidget->rowCount() - 1 )
118 tableWidget->insertRow( row + 1 );
124 void QgsValueMapConfigDlg::removeSelectedButtonPushed()
126 QList<QTableWidgetItem *> list = tableWidget->selectedItems();
127 QSet<int> rowsToRemove;
130 for ( i = 0; i < list.size(); i++ )
132 if ( list[i]->column() == 0 )
134 int row = list[i]->row();
135 if ( !rowsToRemove.contains( row ) )
137 rowsToRemove.insert( row );
141 for ( i = 0; i < rowsToRemove.size(); i++ )
143 tableWidget->removeRow( rowsToRemove.values().at( i ) - removed );
151 QList<QPair<QString, QVariant>> orderedMap;
152 auto end = map.constEnd();
153 for (
auto it = map.constBegin(); it != end; ++it )
155 orderedMap.append( qMakePair( it.key(), it.value() ) );
163 tableWidget->clearContents();
164 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
166 tableWidget->removeRow( i );
176 for (
const auto &pair : list )
178 if ( pair.second.isNull() )
179 setRow( row, pair.first, QString() );
181 setRow( row, pair.first, pair.second.toString() );
188 const QList<QVariant> valueList =
config.value( QStringLiteral(
"map" ) ).toList();
190 if ( !valueList.empty() )
192 for (
const QVariant &value : valueList )
194 const QVariantMap valueMap = value.toMap();
199 comboBox->addItem( valueMap.constBegin().key(), valueMap.constBegin().value() );
204 const QVariantMap map =
config.value( QStringLiteral(
"map" ) ).toMap();
205 for (
auto it = map.constBegin(); it != map.constEnd(); ++it )
210 comboBox->addItem( it.key(), it.value() );
218 if ( event->type() == QEvent::KeyPress )
220 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
221 if ( keyEvent->matches( QKeySequence::Copy ) )
223 copySelectionToClipboard();
231 void QgsValueMapConfigDlg::setRow(
int row,
const QString &value,
const QString &description )
234 QTableWidgetItem *valueCell =
nullptr;
235 QTableWidgetItem *descriptionCell =
new QTableWidgetItem( description );
236 tableWidget->insertRow( row );
240 cellFont.setItalic(
true );
242 valueCell->setFont( cellFont );
243 valueCell->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
244 descriptionCell->setFont( cellFont );
248 valueCell =
new QTableWidgetItem( value );
250 tableWidget->setItem( row, 0, valueCell );
251 tableWidget->setItem( row, 1, descriptionCell );
254 void QgsValueMapConfigDlg::copySelectionToClipboard()
256 QAbstractItemModel *model = tableWidget->model();
257 QItemSelectionModel *selection = tableWidget->selectionModel();
258 const QModelIndexList indexes = selection->selectedIndexes();
260 QString clipboardText;
261 QModelIndex previous = indexes.first();
262 std::unique_ptr<QMimeData> mimeData = qgis::make_unique<QMimeData>();
263 for (
const QModelIndex ¤t : indexes )
265 const QString text = model->data( current ).toString();
266 if ( current.row() != previous.row() )
268 clipboardText.append(
'\n' );
270 else if ( current.column() != previous.column() )
272 clipboardText.append(
'\t' );
274 clipboardText.append( text );
277 mimeData->setData( QStringLiteral(
"text/plain" ), clipboardText.toUtf8() );
278 QApplication::clipboard()->setMimeData( mimeData.release() );
281 void QgsValueMapConfigDlg::addNullButtonPushed()
286 void QgsValueMapConfigDlg::loadFromLayerButtonPushed()
289 if ( !layerDialog.exec() )
292 updateMap( layerDialog.valueMap(), layerDialog.insertNull() );
295 void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
299 QString fileName = QFileDialog::getOpenFileName(
nullptr, tr(
"Load Value Map from File" ), QDir::homePath() );
300 if ( fileName.isNull() )
305 if ( !f.open( QIODevice::ReadOnly ) )
307 QMessageBox::information(
nullptr,
308 tr(
"Load Value Map from File" ),
309 tr(
"Could not open file %1\nError was: %2" ).arg( fileName, f.errorString() ),
310 QMessageBox::Cancel );
315 s.setAutoDetectUnicode(
true );
317 QRegExp re0(
"^([^;]*);(.*)$" );
318 re0.setMinimal(
true );
319 QRegExp re1(
"^([^,]*),(.*)$" );
320 re1.setMinimal(
true );
322 QList<QPair<QString, QVariant>> map;
326 QString l = s.readLine().trimmed();
331 if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
333 key = re0.cap( 1 ).trimmed();
334 val = re0.cap( 2 ).trimmed();
336 else if ( re1.indexIn( l ) >= 0 && re1.captureCount() == 2 )
338 key = re1.cap( 1 ).trimmed();
339 val = re1.cap( 2 ).trimmed();
344 if ( ( key.startsWith(
'\"' ) && key.endsWith(
'\"' ) ) ||
345 ( key.startsWith(
'\'' ) && key.endsWith(
'\'' ) ) )
347 key = key.mid( 1, key.length() - 2 );
350 if ( ( val.startsWith(
'\"' ) && val.endsWith(
'\"' ) ) ||
351 ( val.startsWith(
'\'' ) && val.endsWith(
'\'' ) ) )
353 val = val.mid( 1, val.length() - 2 );
359 map.append( qMakePair( key, val ) );