Skip to content

Commit

Permalink
store valuemap as QList
Browse files Browse the repository at this point in the history
instead of QMap because with QMap it loses the order
actually it's currently stored in config['map'] as QDataStream
  • Loading branch information
signedav authored and 3nids committed Jan 22, 2018
1 parent 9c5797e commit bbac229
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
46 changes: 34 additions & 12 deletions src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp
Expand Up @@ -31,6 +31,9 @@ QgsValueMapConfigDlg::QgsValueMapConfigDlg( QgsVectorLayer *vl, int fieldIdx, QW

tableWidget->insertRow( 0 );

tableWidget->horizontalHeader()->setClickable( true );
tableWidget->setSortingEnabled( true );

connect( addNullButton, &QAbstractButton::clicked, this, &QgsValueMapConfigDlg::addNullButtonPushed );
connect( removeSelectedButton, &QAbstractButton::clicked, this, &QgsValueMapConfigDlg::removeSelectedButtonPushed );
connect( loadFromLayerButton, &QAbstractButton::clicked, this, &QgsValueMapConfigDlg::loadFromLayerButtonPushed );
Expand All @@ -40,8 +43,7 @@ QgsValueMapConfigDlg::QgsValueMapConfigDlg( QgsVectorLayer *vl, int fieldIdx, QW

QVariantMap QgsValueMapConfigDlg::config()
{
QVariantMap values;
QgsSettings settings;
QList<QPair<QString, QVariant>> valueList;

//store data to map
for ( int i = 0; i < tableWidget->rowCount() - 1; i++ )
Expand All @@ -58,16 +60,20 @@ QVariantMap QgsValueMapConfigDlg::config()

if ( !vi || vi->text().isNull() )
{
values.insert( ks, ks );
valueList.append( qMakePair( ks, ks ) );
}
else
{
values.insert( vi->text(), ks );
valueList.append( qMakePair( vi->text(), ks ) );
}
}

QByteArray ba;
QDataStream data( &ba, QIODevice::WriteOnly );
data << valueList;

QVariantMap cfg;
cfg.insert( QStringLiteral( "map" ), values );
cfg.insert( QStringLiteral( "map" ), ba );
return cfg;
}

Expand All @@ -79,14 +85,30 @@ void QgsValueMapConfigDlg::setConfig( const QVariantMap &config )
tableWidget->removeRow( i );
}

int row = 0;
QVariantMap values = config.value( QStringLiteral( "map" ) ).toMap();
for ( QVariantMap::ConstIterator mit = values.constBegin(); mit != values.constEnd(); mit++, row++ )
QList<QPair<QString, QVariant>> valueList;

QByteArray ba = config.value( QStringLiteral( "map" ) ).toByteArray();
QDataStream data( &ba, QIODevice::ReadOnly );
data >> valueList;

if ( valueList.count() > 0 )
{
if ( mit.value().isNull() )
setRow( row, mit.key(), QString() );
else
setRow( row, mit.value().toString(), mit.key() );
for ( int i = 0, row = 0; i < valueList.count(); i++, row++ )
{
setRow( row, valueList[i].second.toString(), valueList[i].first );
}
}
else
{
int row = 0;
QVariantMap values = config.value( QStringLiteral( "map" ) ).toMap();
for ( QVariantMap::ConstIterator mit = values.constBegin(); mit != values.constEnd(); mit++, row++ )
{
if ( mit.value().isNull() )
setRow( row, mit.key(), QString() );
else
setRow( row, mit.value().toString(), mit.key() );
}
}
}

Expand Down
27 changes: 22 additions & 5 deletions src/gui/editorwidgets/qgsvaluemapwidgetwrapper.cpp
Expand Up @@ -58,14 +58,31 @@ void QgsValueMapWidgetWrapper::initWidget( QWidget *editor )

if ( mComboBox )
{
const QVariantMap map = config().value( QStringLiteral( "map" ) ).toMap();
QVariantMap::ConstIterator it = map.constBegin();
QList<QPair<QString, QVariant>> valueList;

while ( it != map.constEnd() )
QByteArray ba = config().value( QStringLiteral( "map" ) ).toByteArray();
QDataStream data( &ba, QIODevice::ReadOnly );
data >> valueList;

if ( valueList.count() > 0 )
{
for ( int i = 0; i < valueList.count(); i++ )
{
mComboBox->addItem( valueList[i].first, valueList[i].second );
}
}
else
{
mComboBox->addItem( it.key(), it.value() );
++it;
const QVariantMap map = config().value( QStringLiteral( "map" ) ).toMap();
QVariantMap::ConstIterator it = map.constBegin();

while ( it != map.constEnd() )
{
mComboBox->addItem( it.key(), it.value() );
++it;
}
}

connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ) );
}
Expand Down

0 comments on commit bbac229

Please sign in to comment.