31 #include <QHeaderView>
34 #include <QTableWidget>
35 #include <QStringListModel>
38 #include <nlohmann/json.hpp>
39 using namespace nlohmann;
54 int cbxIdx = mComboBox->currentIndex();
57 v = mComboBox->currentData();
61 const int nofColumns = columnCount();
65 QStringList selection;
66 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
68 for (
int i = 0; i < nofColumns; ++i )
70 QTableWidgetItem *item = mTableWidget->item( j, i );
73 if ( item->checkState() == Qt::Checked )
74 selection << item->data( Qt::UserRole ).toString();
81 for (
const QString &s : qgis::as_const( selection ) )
84 const QVariant::Type type { fkType() };
87 case QVariant::Type::Int:
88 vl.push_back( s.toInt() );
90 case QVariant::Type::LongLong:
91 vl.push_back( s.toLongLong() );
99 if (
layer()->fields().at(
fieldIdx() ).type() == QVariant::Map ||
100 layer()->fields().at(
fieldIdx() ).type() == QVariant::List )
115 if ( item.value == mLineEdit->text() )
132 mExpression =
config().value( QStringLiteral(
"FilterExpression" ) ).toString();
134 if (
config( QStringLiteral(
"AllowMulti" ) ).toBool() )
136 return new QTableWidget( parent );
138 else if (
config( QStringLiteral(
"UseCompleter" ) ).toBool() )
143 return new QComboBox( parent );
150 mComboBox = qobject_cast<QComboBox *>( editor );
151 mTableWidget = qobject_cast<QTableWidget *>( editor );
152 mLineEdit = qobject_cast<QLineEdit *>( editor );
159 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
162 else if ( mTableWidget )
164 mTableWidget->horizontalHeader()->setSectionResizeMode( QHeaderView::Stretch );
165 mTableWidget->horizontalHeader()->setVisible(
false );
166 mTableWidget->verticalHeader()->setSectionResizeMode( QHeaderView::Stretch );
167 mTableWidget->verticalHeader()->setVisible(
false );
168 mTableWidget->setShowGrid(
false );
169 mTableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
170 mTableWidget->setSelectionMode( QAbstractItemView::NoSelection );
173 else if ( mLineEdit )
175 connect( mLineEdit, &QLineEdit::textChanged,
this, &QgsValueRelationWidgetWrapper::emitValueChangedInternal, Qt::UniqueConnection );
181 return mTableWidget || mLineEdit || mComboBox;
184 void QgsValueRelationWidgetWrapper::updateValues(
const QVariant &value,
const QVariantList & )
188 QStringList checkList;
190 if (
layer()->fields().at(
fieldIdx() ).type() == QVariant::Map ||
191 layer()->fields().at(
fieldIdx() ).type() == QVariant::List )
193 checkList =
value.toStringList();
200 QTableWidgetItem *lastChangedItem =
nullptr;
202 const int nofColumns = columnCount();
206 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
208 auto signalBlockedTableWidget =
whileBlocking( mTableWidget );
209 Q_UNUSED( signalBlockedTableWidget )
211 for (
int i = 0; i < nofColumns; ++i )
213 QTableWidgetItem *item = mTableWidget->item( j, i );
216 item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
218 item->setFlags( mEnabled ? item->flags() | Qt::ItemIsEnabled : item->flags() & ~Qt::ItemIsEnabled );
219 lastChangedItem = item;
224 if ( lastChangedItem )
225 lastChangedItem->setCheckState( checkList.contains( lastChangedItem->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
228 else if ( mComboBox )
233 for (
int i = 0; i < mComboBox->count(); i++ )
235 QVariant v( mComboBox->itemData( i ) );
242 mComboBox->setCurrentIndex( idx );
244 else if ( mLineEdit )
248 if ( i.key ==
value )
250 mLineEdit->setText( i.value );
261 if ( attributeChanged )
263 QVariant oldValue(
value( ) );
271 updateValues(
value( ) );
301 && ! mCache.isEmpty()
302 && !
config( QStringLiteral(
"AllowNull" ) ).toBool( ) )
306 QTimer::singleShot( 0,
this, [
this ]
308 if ( ! mCache.isEmpty() )
310 updateValues( mCache.at( 0 ).key );
316 int QgsValueRelationWidgetWrapper::columnCount()
const
318 return std::max( 1,
config( QStringLiteral(
"NofColumns" ) ).toInt() );
322 QVariant::Type QgsValueRelationWidgetWrapper::fkType()
const
331 return fields.
at( idx ).
type();
334 return QVariant::Type::Invalid;
337 void QgsValueRelationWidgetWrapper::populate( )
343 if (
context().parentFormFeature().isValid() )
352 else if ( mCache.empty() )
360 if (
config( QStringLiteral(
"AllowNull" ) ).toBool( ) )
362 whileBlocking( mComboBox )->addItem( tr(
"(no selection)" ), QVariant(
field().type( ) ) );
367 whileBlocking( mComboBox )->addItem( element.value, element.key );
368 if ( !element.description.isEmpty() )
369 mComboBox->setItemData( mComboBox->count() - 1, element.description, Qt::ToolTipRole );
373 else if ( mTableWidget )
375 const int nofColumns = columnCount();
377 if ( ! mCache.empty() )
379 mTableWidget->setRowCount( ( mCache.size() + nofColumns - 1 ) / nofColumns );
382 mTableWidget->setRowCount( 1 );
383 mTableWidget->setColumnCount( nofColumns );
390 if ( column == nofColumns )
395 QTableWidgetItem *item =
nullptr;
396 item =
new QTableWidgetItem( element.value );
397 item->setData( Qt::UserRole, element.key );
403 else if ( mLineEdit )
406 values.reserve( mCache.size() );
411 QStringListModel *m =
new QStringListModel( values, mLineEdit );
412 QCompleter *completer =
new QCompleter( m, mLineEdit );
413 completer->setCaseSensitivity( Qt::CaseInsensitive );
414 mLineEdit->setCompleter( completer );
420 const int nofColumns = columnCount();
424 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
426 for (
int i = 0; i < nofColumns; ++i )
428 whileBlocking( mTableWidget )->item( j, i )->setCheckState( Qt::PartiallyChecked );
432 else if ( mComboBox )
436 else if ( mLineEdit )
444 if ( mEnabled == enabled )
451 auto signalBlockedTableWidget =
whileBlocking( mTableWidget );
452 Q_UNUSED( signalBlockedTableWidget )
454 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
456 for (
int i = 0; i < mTableWidget->columnCount(); ++i )
458 QTableWidgetItem *item = mTableWidget->item( j, i );
461 item->setFlags( enabled ? item->flags() | Qt::ItemIsEnabled : item->flags() & ~Qt::ItemIsEnabled );
477 ctx.setParentFormFeature( feature );
482 && (
config( QStringLiteral(
"Value" ) ).toString() == attribute ||
483 config( QStringLiteral(
"Key" ) ).toString() == attribute ||
492 void QgsValueRelationWidgetWrapper::emitValueChangedInternal(
const QString &value )