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::KeyPress )
207 QKeyEvent *keyEvent =
static_cast<QKeyEvent *
>( event );
208 if ( keyEvent->matches( QKeySequence::Copy ) )
210 copySelectionToClipboard();
218 void QgsValueMapConfigDlg::setRow(
int row,
const QString &value,
const QString &description )
221 QTableWidgetItem *valueCell =
nullptr;
222 QTableWidgetItem *descriptionCell =
new QTableWidgetItem( description );
223 tableWidget->insertRow( row );
227 cellFont.setItalic(
true );
229 valueCell->setFont( cellFont );
230 valueCell->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
231 descriptionCell->setFont( cellFont );
235 valueCell =
new QTableWidgetItem( value );
237 tableWidget->setItem( row, 0, valueCell );
238 tableWidget->setItem( row, 1, descriptionCell );
241 void QgsValueMapConfigDlg::copySelectionToClipboard()
243 QAbstractItemModel *model = tableWidget->model();
244 QItemSelectionModel *selection = tableWidget->selectionModel();
245 const QModelIndexList indexes = selection->selectedIndexes();
247 QString clipboardText;
248 QModelIndex previous = indexes.first();
249 std::unique_ptr<QMimeData> mimeData = qgis::make_unique<QMimeData>();
250 for (
const QModelIndex ¤t : indexes )
252 const QString text = model->data( current ).toString();
253 if ( current.row() != previous.row() )
255 clipboardText.append(
'\n' );
257 else if ( current.column() != previous.column() )
259 clipboardText.append(
'\t' );
261 clipboardText.append( text );
264 mimeData->setData( QStringLiteral(
"text/plain" ), clipboardText.toUtf8() );
265 QApplication::clipboard()->setMimeData( mimeData.release() );
268 void QgsValueMapConfigDlg::addNullButtonPushed()
273 void QgsValueMapConfigDlg::loadFromLayerButtonPushed()
276 if ( !layerDialog.exec() )
282 void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
286 QString fileName = QFileDialog::getOpenFileName(
nullptr, tr(
"Select a File" ), QDir::homePath() );
287 if ( fileName.isNull() )
292 if ( !f.open( QIODevice::ReadOnly ) )
294 QMessageBox::information(
nullptr,
295 tr(
"Load Value Map from File" ),
296 tr(
"Could not open file %1\nError was: %2" ).arg( fileName, f.errorString() ),
297 QMessageBox::Cancel );
302 s.setAutoDetectUnicode(
true );
304 QRegExp re0(
"^([^;]*);(.*)$" );
305 re0.setMinimal(
true );
306 QRegExp re1(
"^([^,]*),(.*)$" );
307 re1.setMinimal(
true );
308 QMap<QString, QVariant> map;
314 QString l = s.readLine().trimmed();
317 if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
319 key = re0.cap( 1 ).trimmed();
320 val = re0.cap( 2 ).trimmed();
322 else if ( re1.indexIn( l ) >= 0 && re1.captureCount() == 2 )
324 key = re1.cap( 1 ).trimmed();
325 val = re1.cap( 2 ).trimmed();
330 if ( ( key.startsWith(
'\"' ) && key.endsWith(
'\"' ) ) ||
331 ( key.startsWith(
'\'' ) && key.endsWith(
'\'' ) ) )
333 key = key.mid( 1, key.length() - 2 );
336 if ( ( val.startsWith(
'\"' ) && val.endsWith(
'\"' ) ) ||
337 ( val.startsWith(
'\'' ) && val.endsWith(
'\'' ) ) )
339 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.