Skip to content
Permalink
Browse files

store valuemap as QList

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 17, 2018
1 parent 9c5797e commit bbac229c0eb303482e6becfdcf6441811ee1a9a3
Showing with 56 additions and 17 deletions.
  1. +34 −12 src/gui/editorwidgets/qgsvaluemapconfigdlg.cpp
  2. +22 −5 src/gui/editorwidgets/qgsvaluemapwidgetwrapper.cpp
@@ -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 );
@@ -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++ )
@@ -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;
}

@@ -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() );
}
}
}

@@ -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 ) );
}

0 comments on commit bbac229

Please sign in to comment.
You can’t perform that action at this time.