29 #include <QHeaderView> 32 #include <QTableWidget> 33 #include <QStringListModel> 48 int cbxIdx = mComboBox->currentIndex();
51 v = mComboBox->currentData();
55 const int nofColumns = columnCount();
59 QStringList selection;
60 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
62 for (
int i = 0; i < nofColumns; ++i )
64 QTableWidgetItem *item = mTableWidget->item( j, i );
67 if ( item->checkState() == Qt::Checked )
68 selection << item->data( Qt::UserRole ).toString();
72 v = selection.join( QStringLiteral(
"," ) ).prepend(
'{' ).append(
'}' );
79 if ( item.value == mLineEdit->text() )
96 mExpression =
config().value( QStringLiteral(
"FilterExpression" ) ).toString();
98 if (
config( QStringLiteral(
"AllowMulti" ) ).toBool() )
100 return new QTableWidget( parent );
102 else if (
config( QStringLiteral(
"UseCompleter" ) ).toBool() )
107 return new QComboBox( parent );
114 mComboBox = qobject_cast<QComboBox *>( editor );
115 mTableWidget = qobject_cast<QTableWidget *>( editor );
116 mLineEdit = qobject_cast<QLineEdit *>( editor );
123 connect( mComboBox,
static_cast<void ( QComboBox::* )(
int )
>( &QComboBox::currentIndexChanged ),
126 else if ( mTableWidget )
128 mTableWidget->horizontalHeader()->setResizeMode( QHeaderView::Stretch );
129 mTableWidget->horizontalHeader()->setVisible(
false );
130 mTableWidget->verticalHeader()->setResizeMode( QHeaderView::Stretch );
131 mTableWidget->verticalHeader()->setVisible(
false );
132 mTableWidget->setShowGrid(
false );
133 mTableWidget->setEditTriggers( QAbstractItemView::NoEditTriggers );
134 mTableWidget->setSelectionMode( QAbstractItemView::NoSelection );
137 else if ( mLineEdit )
139 connect( mLineEdit, &QLineEdit::textChanged,
this, [ = ](
const QString &
value ) { emit
valueChanged( value ); }, Qt::UniqueConnection );
145 return mTableWidget || mLineEdit || mComboBox;
154 QTableWidgetItem *lastChangedItem =
nullptr;
156 const int nofColumns = columnCount();
160 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
162 auto signalBlockedTableWidget =
whileBlocking( mTableWidget );
163 Q_UNUSED( signalBlockedTableWidget )
165 for (
int i = 0; i < nofColumns; ++i )
167 QTableWidgetItem *item = mTableWidget->item( j, i );
170 item->setCheckState( checkList.contains( item->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
171 lastChangedItem = item;
176 if ( lastChangedItem )
177 lastChangedItem->setCheckState( checkList.contains( lastChangedItem->data( Qt::UserRole ).toString() ) ? Qt::Checked : Qt::Unchecked );
180 else if ( mComboBox )
185 for (
int i = 0; i < mComboBox->count(); i++ )
187 QVariant v( mComboBox->itemData( i ) );
194 mComboBox->setCurrentIndex( idx );
196 else if ( mLineEdit )
200 if ( i.key == value )
202 mLineEdit->setText( i.value );
213 if ( attributeChanged )
215 QVariant oldValue(
value( ) );
254 && !
config( QStringLiteral(
"AllowNull" ) ).toBool( ) )
258 QTimer::singleShot( 0,
this, [
this ]
265 int QgsValueRelationWidgetWrapper::columnCount()
const 267 return std::max( 1,
config( QStringLiteral(
"NofColumns" ) ).toInt() );
270 void QgsValueRelationWidgetWrapper::populate( )
277 else if ( mCache.empty() )
285 if (
config( QStringLiteral(
"AllowNull" ) ).toBool( ) )
287 whileBlocking( mComboBox )->addItem( tr(
"(no selection)" ), QVariant(
field().type( ) ) );
292 whileBlocking( mComboBox )->addItem( element.value, element.key );
295 else if ( mTableWidget )
297 const int nofColumns = columnCount();
299 if ( ! mCache.empty() )
301 mTableWidget->setRowCount( ( mCache.size() + nofColumns - 1 ) / nofColumns );
304 mTableWidget->setRowCount( 1 );
305 mTableWidget->setColumnCount( nofColumns );
312 if ( column == nofColumns )
317 QTableWidgetItem *item =
nullptr;
318 item =
new QTableWidgetItem( element.value );
319 item->setData( Qt::UserRole, element.key );
324 else if ( mLineEdit )
327 values.reserve( mCache.size() );
332 QStringListModel *m =
new QStringListModel( values, mLineEdit );
333 QCompleter *completer =
new QCompleter( m, mLineEdit );
334 completer->setCaseSensitivity( Qt::CaseInsensitive );
335 mLineEdit->setCompleter( completer );
341 const int nofColumns = columnCount();
345 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
347 for (
int i = 0; i < nofColumns; ++i )
349 whileBlocking( mTableWidget )->item( j, i )->setCheckState( Qt::PartiallyChecked );
353 else if ( mComboBox )
357 else if ( mLineEdit )
365 if ( mEnabled == enabled )
372 auto signalBlockedTableWidget =
whileBlocking( mTableWidget );
373 Q_UNUSED( signalBlockedTableWidget )
375 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
377 for (
int i = 0; i < mTableWidget->columnCount(); ++i )
379 QTableWidgetItem *item = mTableWidget->item( j, i );
383 item->setFlags( item->flags() | Qt::ItemIsEnabled );
385 item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
QLineEdit subclass with built in support for clearing the widget's value and handling custom null val...
QgsSignalBlocker< Object > whileBlocking(Object *object)
Temporarily blocks signals from a QObject while calling a single method from the object.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
bool qgsVariantEqual(const QVariant &lhs, const QVariant &rhs)
Compares two QVariant values and returns whether they are equal, NULL values are treated as equal...
Represents a vector layer which manages a vector based data sets.