@@ -52,12 +52,14 @@ QVariant QgsValueRelationWidgetWrapper::value() const
52
52
}
53
53
}
54
54
55
+ const int nofColumns = columnCount ();
56
+
55
57
if ( mTableWidget )
56
58
{
57
59
QStringList selection;
58
60
for ( int j = 0 ; j < mTableWidget ->rowCount (); j++ )
59
61
{
60
- for ( int i = 0 ; i < config ( QStringLiteral ( " NofColumns " ) ). toInt () ; ++i )
62
+ for ( int i = 0 ; i < nofColumns ; ++i )
61
63
{
62
64
QTableWidgetItem *item = mTableWidget ->item ( j, i );
63
65
if ( item )
@@ -151,12 +153,14 @@ void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
151
153
152
154
QTableWidgetItem *lastChangedItem = nullptr ;
153
155
156
+ const int nofColumns = columnCount ();
157
+
154
158
// This block is needed because item->setCheckState triggers dataChanged gets back to value()
155
159
// and iterate over all items again! This can be extremely slow on large items sets.
156
160
mTableWidget ->blockSignals ( true );
157
161
for ( int j = 0 ; j < mTableWidget ->rowCount (); j++ )
158
162
{
159
- for ( int i = 0 ; i < config ( QStringLiteral ( " NofColumns " ) ). toInt () ; ++i )
163
+ for ( int i = 0 ; i < nofColumns ; ++i )
160
164
{
161
165
QTableWidgetItem *item = mTableWidget ->item ( j, i );
162
166
if ( item )
@@ -230,6 +234,10 @@ void QgsValueRelationWidgetWrapper::setFeature( const QgsFeature &feature )
230
234
}
231
235
}
232
236
237
+ int QgsValueRelationWidgetWrapper::columnCount () const
238
+ {
239
+ return qMax ( 1 , config ( QStringLiteral ( " NofColumns" ) ).toInt () );
240
+ }
233
241
234
242
void QgsValueRelationWidgetWrapper::populate ( )
235
243
{
@@ -258,24 +266,21 @@ void QgsValueRelationWidgetWrapper::populate( )
258
266
}
259
267
else if ( mTableWidget )
260
268
{
269
+ const int nofColumns = columnCount ();
270
+
261
271
if ( mCache .size () > 0 )
262
272
{
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 );
266
274
}
267
275
else
268
276
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 );
273
278
274
279
whileBlocking ( mTableWidget )->clear ();
275
280
int row = 0 , column = 0 ;
276
281
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const ( mCache ) )
277
282
{
278
- if ( column == config ( QStringLiteral ( " NofColumns " ) ). toInt () )
283
+ if ( column == nofColumns )
279
284
{
280
285
row++;
281
286
column = 0 ;
@@ -304,11 +309,13 @@ void QgsValueRelationWidgetWrapper::populate( )
304
309
305
310
void QgsValueRelationWidgetWrapper::showIndeterminateState ()
306
311
{
312
+ const int nofColumns = columnCount ();
313
+
307
314
if ( mTableWidget )
308
315
{
309
316
for ( int j = 0 ; j < mTableWidget ->rowCount (); j++ )
310
317
{
311
- for ( int i = 0 ; i < config ( QStringLiteral ( " NofColumns " ) ). toInt () ; ++i )
318
+ for ( int i = 0 ; i < nofColumns ; ++i )
312
319
{
313
320
whileBlocking ( mTableWidget )->item ( j, i )->setCheckState ( Qt::PartiallyChecked );
314
321
}
0 commit comments