23 #include <QFileDialog> 24 #include <QMessageBox> 25 #include <QTextStream> 34 tableWidget->insertRow( 0 );
36 tableWidget->horizontalHeader()->setClickable(
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 tableWidget->clearContents();
152 for (
int i = tableWidget->rowCount() - 1; i > 0; i-- )
154 tableWidget->removeRow( i );
164 for ( QMap<QString, QVariant>::const_iterator mit = map.begin(); mit != map.end(); ++mit, ++row )
166 if ( mit.value().isNull() )
167 setRow( row, mit.key(), QString() );
169 setRow( row, mit.key(), mit.value().toString() );
175 const QList<QVariant> valueList = config.value( QStringLiteral(
"map" ) ).toList();
177 if ( !valueList.empty() )
179 for (
const QVariant &value : valueList )
181 const QVariantMap valueMap = value.toMap();
186 comboBox->addItem( valueMap.constBegin().key(), valueMap.constBegin().value() );
191 const QVariantMap map = config.value( QStringLiteral(
"map" ) ).toMap();
192 for (
auto it = map.constBegin(); it != map.constEnd(); ++it )
197 comboBox->addItem( it.key(), it.value() );
205 if ( event->type() == QEvent::KeyRelease )
207 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
208 if ( keyEvent->matches( QKeySequence::Copy ) )
210 copySelectionToClipboard();
217 void QgsValueMapConfigDlg::setRow(
int row,
const QString &value,
const QString &description )
220 QTableWidgetItem *valueCell =
nullptr;
221 QTableWidgetItem *descriptionCell =
new QTableWidgetItem( description );
222 tableWidget->insertRow( row );
226 cellFont.setItalic(
true );
228 valueCell->setFont( cellFont );
229 valueCell->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
230 descriptionCell->setFont( cellFont );
234 valueCell =
new QTableWidgetItem( value );
236 tableWidget->setItem( row, 0, valueCell );
237 tableWidget->setItem( row, 1, descriptionCell );
240 void QgsValueMapConfigDlg::copySelectionToClipboard()
242 QAbstractItemModel *model = tableWidget->model();
243 QItemSelectionModel *selection = tableWidget->selectionModel();
244 const QModelIndexList indexes = selection->selectedIndexes();
246 QString clipboardText;
247 QModelIndex previous = indexes.first();
248 std::unique_ptr<QMimeData> mimeData = qgis::make_unique<QMimeData>();
249 for (
const QModelIndex ¤t : indexes )
251 const QString text = model->data( current ).toString();
252 if ( current.row() != previous.row() )
254 clipboardText.append(
'\n' );
256 else if ( current.column() != previous.column() )
258 clipboardText.append(
'\t' );
260 clipboardText.append( text );
263 mimeData->setData( QStringLiteral(
"text/plain" ), clipboardText.toUtf8() );
264 QApplication::clipboard()->setMimeData( mimeData.release() );
267 void QgsValueMapConfigDlg::addNullButtonPushed()
272 void QgsValueMapConfigDlg::loadFromLayerButtonPushed()
275 if ( !layerDialog.exec() )
281 void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
285 QString fileName = QFileDialog::getOpenFileName(
nullptr, tr(
"Select a File" ), QDir::homePath() );
286 if ( fileName.isNull() )
291 if ( !f.open( QIODevice::ReadOnly ) )
293 QMessageBox::information(
nullptr,
294 tr(
"Load Value Map from File" ),
295 tr(
"Could not open file %1\nError was: %2" ).arg( fileName, f.errorString() ),
296 QMessageBox::Cancel );
301 s.setAutoDetectUnicode(
true );
303 QRegExp re0(
"^([^;]*);(.*)$" );
304 re0.setMinimal(
true );
305 QRegExp re1(
"^([^,]*),(.*)$" );
306 re1.setMinimal(
true );
307 QMap<QString, QVariant> map;
313 QString l = s.readLine().trimmed();
316 if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
318 key = re0.cap( 1 ).trimmed();
319 val = re0.cap( 2 ).trimmed();
321 else if ( re1.indexIn( l ) >= 0 && re1.captureCount() == 2 )
323 key = re1.cap( 1 ).trimmed();
324 val = re1.cap( 2 ).trimmed();
329 if ( ( key.startsWith(
'\"' ) && key.endsWith(
'\"' ) ) ||
330 ( key.startsWith(
'\'' ) && key.endsWith(
'\'' ) ) )
332 key = key.mid( 1, key.length() - 2 );
335 if ( ( val.startsWith(
'\"' ) && val.endsWith(
'\"' ) ) ||
336 ( val.startsWith(
'\'' ) && val.endsWith(
'\'' ) ) )
338 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)
bool eventFilter(QObject *watched, QEvent *event) override
void setConfig(const QVariantMap &config) override
Update the configuration widget to represent the given configuration.
QMap< QString, QVariant > & valueMap()
Returns the 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.