/
qgsvaluerelationwidgetwrapper.cpp
387 lines (346 loc) · 11.9 KB
/
qgsvaluerelationwidgetwrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/***************************************************************************
qgsvaluerelationwidgetwrapper.cpp
--------------------------------------
Date : 5.1.2014
Copyright : (C) 2014 Matthias Kuhn
Email : matthias at opengis dot ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsvaluerelationwidgetwrapper.h"
#include "qgis.h"
#include "qgsfields.h"
#include "qgsproject.h"
#include "qgsvaluerelationwidgetfactory.h"
#include "qgsvectorlayer.h"
#include "qgsfilterlineedit.h"
#include "qgsfeatureiterator.h"
#include "qgsvaluerelationfieldformatter.h"
#include "qgsattributeform.h"
#include "qgsattributes.h"
#include <QHeaderView>
#include <QComboBox>
#include <QLineEdit>
#include <QTableWidget>
#include <QStringListModel>
#include <QCompleter>
QgsValueRelationWidgetWrapper::QgsValueRelationWidgetWrapper( QgsVectorLayer *layer, int fieldIdx, QWidget *editor, QWidget *parent )
: QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
{
}
QVariant QgsValueRelationWidgetWrapper::value() const
{
QVariant v;
if ( mComboBox )
{
int cbxIdx = mComboBox->currentIndex();
if ( cbxIdx > -1 )
{
v = mComboBox->currentData();
}
}
const int nofColumns = columnCount();
if ( mTableWidget )
{
QStringList selection;
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
for ( int i = 0; i < nofColumns; ++i )
{
QTableWidgetItem *item = mTableWidget->item( j, i );
if ( item )
{
if ( item->checkState() == Qt::Checked )
selection << item->data( Qt::UserRole ).toString();
}
}
}
v = selection.join( QStringLiteral( "," ) ).prepend( '{' ).append( '}' );
}
if ( mLineEdit )
{
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &item : qgis::as_const( mCache ) )
{
if ( item.value == mLineEdit->text() )
{
v = item.key;
break;
}
}
}
return v;
}
QWidget *QgsValueRelationWidgetWrapper::createWidget( QWidget *parent )
{
QgsAttributeForm *form = qobject_cast<QgsAttributeForm *>( parent );
if ( form )
connect( form, &QgsAttributeForm::widgetValueChanged, this, &QgsValueRelationWidgetWrapper::widgetValueChanged );
mExpression = config().value( QStringLiteral( "FilterExpression" ) ).toString();
if ( config( QStringLiteral( "AllowMulti" ) ).toBool() )
{
return new QTableWidget( parent );
}
else if ( config( QStringLiteral( "UseCompleter" ) ).toBool() )
{
return new QgsFilterLineEdit( parent );
}
{
return new QComboBox( parent );
}
}
void QgsValueRelationWidgetWrapper::initWidget( QWidget *editor )
{
mComboBox = qobject_cast<QComboBox *>( editor );
mTableWidget = qobject_cast<QTableWidget *>( editor );
mLineEdit = qobject_cast<QLineEdit *>( editor );
// Read current initial form values from the editor context
setFeature( context().formFeature() );
if ( mComboBox )
{
connect( mComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ),
this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ), Qt::UniqueConnection );
}
else if ( mTableWidget )
{
mTableWidget->horizontalHeader()->setResizeMode( QHeaderView::Stretch );
mTableWidget->horizontalHeader()->setVisible( false );
mTableWidget->verticalHeader()->setResizeMode( QHeaderView::Stretch );
mTableWidget->verticalHeader()->setVisible( false );
mTableWidget->setShowGrid( false );
mTableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
mTableWidget->setSelectionMode( QAbstractItemView::NoSelection );
connect( mTableWidget, &QTableWidget::itemChanged, this, static_cast<void ( QgsEditorWidgetWrapper::* )()>( &QgsEditorWidgetWrapper::emitValueChanged ), Qt::UniqueConnection );
}
else if ( mLineEdit )
{
connect( mLineEdit, &QLineEdit::textChanged, this, [ = ]( const QString & value ) { emit valueChanged( value ); }, Qt::UniqueConnection );
}
}
bool QgsValueRelationWidgetWrapper::valid() const
{
return mTableWidget || mLineEdit || mComboBox;
}
void QgsValueRelationWidgetWrapper::setValue( const QVariant &value )
{
if ( mTableWidget )
{
QStringList checkList( QgsValueRelationFieldFormatter::valueToStringList( value ) );
QTableWidgetItem *lastChangedItem = nullptr;
const int nofColumns = columnCount();
// This block is needed because item->setCheckState triggers dataChanged gets back to value()
// and iterate over all items again! This can be extremely slow on large items sets.
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
auto signalBlockedTableWidget = whileBlocking( mTableWidget );
Q_UNUSED( signalBlockedTableWidget )
for ( int i = 0; i < nofColumns; ++i )
{
QTableWidgetItem *item = mTableWidget->item( j, i );
if ( item )
{
item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
lastChangedItem = item;
}
}
}
// let's trigger the signal now, once and for all
if ( lastChangedItem )
lastChangedItem->setCheckState( checkList.contains( lastChangedItem->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
}
else if ( mComboBox )
{
// findData fails to tell a 0 from a NULL
// See: "Value relation, value 0 = NULL" - https://issues.qgis.org/issues/19981
int idx = -1; // default to not found
for ( int i = 0; i < mComboBox->count(); i++ )
{
QVariant v( mComboBox->itemData( i ) );
if ( qgsVariantEqual( v, value ) )
{
idx = i;
break;
}
}
mComboBox->setCurrentIndex( idx );
}
else if ( mLineEdit )
{
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : qgis::as_const( mCache ) )
{
if ( i.key == value )
{
mLineEdit->setText( i.value );
break;
}
}
}
}
void QgsValueRelationWidgetWrapper::widgetValueChanged( const QString &attribute, const QVariant &newValue, bool attributeChanged )
{
// Do nothing if the value has not changed
if ( attributeChanged )
{
QVariant oldValue( value( ) );
setFormFeatureAttribute( attribute, newValue );
// Update combos if the value used in the filter expression has changed
if ( QgsValueRelationFieldFormatter::expressionRequiresFormScope( mExpression )
&& QgsValueRelationFieldFormatter::expressionFormAttributes( mExpression ).contains( attribute ) )
{
populate();
// Restore value
setValue( value( ) );
// If the value has changed as a result of another widget's value change,
// we need to emit the signal to make sure other dependent widgets are
// updated.
if ( oldValue != value() && fieldIdx() < formFeature().fields().count() )
{
QString attributeName( formFeature().fields().names().at( fieldIdx() ) );
setFormFeatureAttribute( attributeName, value( ) );
emitValueChanged( );
}
}
}
}
void QgsValueRelationWidgetWrapper::setFeature( const QgsFeature &feature )
{
setFormFeature( feature );
whileBlocking( this )->populate();
whileBlocking( this )->setValue( feature.attribute( fieldIdx() ) );
// A bit of logic to set the default value if AllowNull is false and this is a new feature
// Note that this needs to be here after the cache has been created/updated by populate()
// and signals unblocked (we want this to propagate to the feature itself)
if ( formFeature().isValid()
&& ! formFeature().attribute( fieldIdx() ).isValid()
&& ! mCache.empty()
&& ! config( QStringLiteral( "AllowNull" ) ).toBool( ) )
{
// This is deferred because at the time the feature is set in one widget it is not
// set in the next, which is typically the "down" in a drill-down
QTimer::singleShot( 0, this, [ this ]
{
setValue( mCache.at( 0 ).key );
} );
}
}
int QgsValueRelationWidgetWrapper::columnCount() const
{
return std::max( 1, config( QStringLiteral( "NofColumns" ) ).toInt() );
}
void QgsValueRelationWidgetWrapper::populate( )
{
// Initialize, note that signals are blocked, to avoid double signals on new features
if ( QgsValueRelationFieldFormatter::expressionRequiresFormScope( mExpression ) )
{
mCache = QgsValueRelationFieldFormatter::createCache( config( ), formFeature() );
}
else if ( mCache.empty() )
{
mCache = QgsValueRelationFieldFormatter::createCache( config( ) );
}
if ( mComboBox )
{
mComboBox->clear();
if ( config( QStringLiteral( "AllowNull" ) ).toBool( ) )
{
whileBlocking( mComboBox )->addItem( tr( "(no selection)" ), QVariant( field().type( ) ) );
}
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const( mCache ) )
{
whileBlocking( mComboBox )->addItem( element.value, element.key );
}
}
else if ( mTableWidget )
{
const int nofColumns = columnCount();
if ( ! mCache.empty() )
{
mTableWidget->setRowCount( ( mCache.size() + nofColumns - 1 ) / nofColumns );
}
else
mTableWidget->setRowCount( 1 );
mTableWidget->setColumnCount( nofColumns );
whileBlocking( mTableWidget )->clear();
int row = 0;
int column = 0;
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &element : qgis::as_const( mCache ) )
{
if ( column == nofColumns )
{
row++;
column = 0;
}
QTableWidgetItem *item = nullptr;
item = new QTableWidgetItem( element.value );
item->setData( Qt::UserRole, element.key );
whileBlocking( mTableWidget )->setItem( row, column, item );
column++;
}
}
else if ( mLineEdit )
{
QStringList values;
values.reserve( mCache.size() );
for ( const QgsValueRelationFieldFormatter::ValueRelationItem &i : qgis::as_const( mCache ) )
{
values << i.value;
}
QStringListModel *m = new QStringListModel( values, mLineEdit );
QCompleter *completer = new QCompleter( m, mLineEdit );
completer->setCaseSensitivity( Qt::CaseInsensitive );
mLineEdit->setCompleter( completer );
}
}
void QgsValueRelationWidgetWrapper::showIndeterminateState()
{
const int nofColumns = columnCount();
if ( mTableWidget )
{
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
for ( int i = 0; i < nofColumns; ++i )
{
whileBlocking( mTableWidget )->item( j, i )->setCheckState( Qt::PartiallyChecked );
}
}
}
else if ( mComboBox )
{
whileBlocking( mComboBox )->setCurrentIndex( -1 );
}
else if ( mLineEdit )
{
whileBlocking( mLineEdit )->clear();
}
}
void QgsValueRelationWidgetWrapper::setEnabled( bool enabled )
{
if ( mEnabled == enabled )
return;
mEnabled = enabled;
if ( mTableWidget )
{
auto signalBlockedTableWidget = whileBlocking( mTableWidget );
Q_UNUSED( signalBlockedTableWidget )
for ( int j = 0; j < mTableWidget->rowCount(); j++ )
{
for ( int i = 0; i < mTableWidget->columnCount(); ++i )
{
QTableWidgetItem *item = mTableWidget->item( j, i );
if ( item )
{
if ( enabled )
item->setFlags( item->flags() | Qt::ItemIsEnabled );
else
item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
}
}
}
}
else
QgsEditorWidgetWrapper::setEnabled( enabled );
}