Skip to content

Commit 2f86c2c

Browse files
authoredMay 25, 2018
Merge pull request #7075 from m-kuhn/multiselectValueRelation
Fix checkboxes do not show up in value relation multi
2 parents f33a2d0 + 37e0b9f commit 2f86c2c

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed
 

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.cpp‎

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,14 @@ QVariant QgsValueRelationWidgetWrapper::value() const
5252
}
5353
}
5454

55+
const int nofColumns = columnCount();
56+
5557
if ( mTableWidget )
5658
{
5759
QStringList selection;
5860
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
5961
{
60-
for ( int i = 0; i < config( QStringLiteral( "NofColumns" ) ).toInt(); ++i )
62+
for ( int i = 0; i < nofColumns; ++i )
6163
{
6264
QTableWidgetItem *item = mTableWidget->item( j, i );
6365
if ( item )
@@ -151,12 +153,14 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
151153

152154
QTableWidgetItem *lastChangedItem = nullptr;
153155

156+
const int nofColumns = columnCount();
157+
154158
// This block is needed because item->setCheckState triggers dataChanged gets back to value()
155159
// and iterate over all items again! This can be extremely slow on large items sets.
156160
mTableWidget->blockSignals( true );
157161
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
158162
{
159-
for ( int i = 0; i < config( QStringLiteral( "NofColumns" ) ).toInt() ; ++i )
163+
for ( int i = 0; i < nofColumns; ++i )
160164
{
161165
QTableWidgetItem *item = mTableWidget->item( j, i );
162166
if ( item )
@@ -230,6 +234,10 @@ void QgsValueRelationWidgetWrapper::setFeature( const QgsFeature &feature )
230234
}
231235
}
232236

237+
int QgsValueRelationWidgetWrapper::columnCount() const
238+
{
239+
return qMax( 1, config( QStringLiteral( "NofColumns" ) ).toInt() );
240+
}
233241

234242
void QgsValueRelationWidgetWrapper::populate( )
235243
{
@@ -258,24 +266,21 @@ void QgsValueRelationWidgetWrapper::populate( )
258266
}
259267
else if ( mTableWidget )
260268
{
269+
const int nofColumns = columnCount();
270+
261271
if ( mCache.size() > 0 )
262272
{
263-
const int nofCols = config( QStringLiteral( "NofColumns" ) ).toInt();
264-
const int denom = nofCols != 0 ? nofCols : 1;
265-
mTableWidget->setRowCount( ( mCache.size() + nofCols - 1 ) / denom );
273+
mTableWidget->setRowCount( ( mCache.size() + nofColumns - 1 ) / nofColumns );
266274
}
267275
else
268276
mTableWidget->setRowCount( 1 );
269-
if ( config( QStringLiteral( "NofColumns" ) ).toInt() > 0 )
270-
mTableWidget->setColumnCount( config( QStringLiteral( "NofColumns" ) ).toInt() );
271-
else
272-
mTableWidget->setColumnCount( 1 );
277+
mTableWidget->setColumnCount( nofColumns );
273278

274279
whileBlocking( mTableWidget )->clear();
275280
int row = 0, column = 0;
276281
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const( mCache ) )
277282
{
278-
if ( column == config( QStringLiteral( "NofColumns" ) ).toInt() )
283+
if ( column == nofColumns )
279284
{
280285
row++;
281286
column = 0;
@@ -304,11 +309,13 @@ void QgsValueRelationWidgetWrapper::populate( )
304309

305310
void QgsValueRelationWidgetWrapper::showIndeterminateState()
306311
{
312+
const int nofColumns = columnCount();
313+
307314
if ( mTableWidget )
308315
{
309316
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
310317
{
311-
for ( int i = 0; i < config( QStringLiteral( "NofColumns" ) ).toInt(); ++i )
318+
for ( int i = 0; i < nofColumns; ++i )
312319
{
313320
whileBlocking( mTableWidget )->item( j, i )->setCheckState( Qt::PartiallyChecked );
314321
}

‎src/gui/editorwidgets/qgsvaluerelationwidgetwrapper.h‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ class GUI_EXPORT QgsValueRelationWidgetWrapper : public QgsEditorWidgetWrapper
9898

9999
private:
100100

101+
/**
102+
* Returns the value configured in `NofColumns` or 1 if not
103+
* a positive integer.
104+
*/
105+
int columnCount() const;
106+
101107
//! Sets the values for the widgets, re-creates the cache when required
102108
void populate( );
103109

0 commit comments

Comments
 (0)
Please sign in to comment.