Skip to content

Commit

Permalink
Merge pull request #33060 from m-kuhn/value_map_ordered_import
Browse files Browse the repository at this point in the history
Keep order of value map items when importing CSV
  • Loading branch information
m-kuhn committed Nov 27, 2019
2 parents a997ab8 + 4512318 commit 98d1f98
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
29 changes: 22 additions & 7 deletions src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp
Expand Up @@ -147,6 +147,18 @@ void QgsValueMapConfigDlg::removeSelectedButtonPushed()
}

void QgsValueMapConfigDlg::updateMap( const QMap<QString, QVariant> &map, bool insertNull )
{
QList<QPair<QString, QVariant>> orderedMap;
auto end = map.constEnd();
for ( auto it = map.constBegin(); it != end; ++it )
{
orderedMap.append( qMakePair( it.key(), it.value() ) );
}

updateMap( orderedMap, insertNull );
}

void QgsValueMapConfigDlg::updateMap( const QList<QPair<QString, QVariant>> &list, bool insertNull )
{
tableWidget->clearContents();
for ( int i = tableWidget->rowCount() - 1; i > 0; i-- )
Expand All @@ -161,12 +173,12 @@ void QgsValueMapConfigDlg::updateMap( const QMap<QString, QVariant> &map, bool i
++row;
}

for ( QMap<QString, QVariant>::const_iterator mit = map.begin(); mit != map.end(); ++mit, ++row )
for ( const auto &pair : list )
{
if ( mit.value().isNull() )
setRow( row, mit.key(), QString() );
if ( pair.second.isNull() )
setRow( row, pair.first, QString() );
else
setRow( row, mit.key(), mit.value().toString() );
setRow( row, pair.first, pair.second.toString() );
}
}

Expand Down Expand Up @@ -305,15 +317,18 @@ void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
re0.setMinimal( true );
QRegExp re1( "^([^,]*),(.*)$" );
re1.setMinimal( true );
QMap<QString, QVariant> map;

QList<QPair<QString, QVariant>> map;

s.readLine();

while ( !s.atEnd() )
{
QString l = s.readLine().trimmed();

QString key, val;
QString key;
QString val;

if ( re0.indexIn( l ) >= 0 && re0.captureCount() == 2 )
{
key = re0.cap( 1 ).trimmed();
Expand Down Expand Up @@ -342,7 +357,7 @@ void QgsValueMapConfigDlg::loadFromCSVButtonPushed()
if ( key == QgsApplication::nullRepresentation() )
key = QgsValueMapFieldFormatter::NULL_VALUE;

map[ key ] = val;
map.append( qMakePair( key, val ) );
}

updateMap( map, false );
Expand Down
16 changes: 16 additions & 0 deletions src/gui/editorwidgets/qgsvaluemapconfigdlg.h
Expand Up @@ -40,8 +40,24 @@ class GUI_EXPORT QgsValueMapConfigDlg : public QgsEditorConfigWidget, private Ui
QVariantMap config() override;
void setConfig( const QVariantMap &config ) override;

/**
* Updates the displayed table with the values from \a map.
* If \a insertNull is set to TRUE, it will also insert a NULL value.
*
* \note In most cases the overload that accepts a list is preferred as it
* keeps the order of the values.
*/
void updateMap( const QMap<QString, QVariant> &map, bool insertNull );

/**
* Updates the displayed table with the values from \a list, the order of the values
* is preserved.
* If \a insertNull is set to TRUE, it will also insert a NULL value.
*
* \since QGIS 3.12
*/
void updateMap( const QList<QPair<QString, QVariant>> &list, bool insertNull );

/**
* Populates a \a comboBox with the appropriate entries based on a value map \a configuration.
*
Expand Down

0 comments on commit 98d1f98

Please sign in to comment.