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 )
182 mComboBox->setCurrentIndex( mComboBox->findData( value ) );
184 else if ( mLineEdit )
188 if ( i.key == value )
190 mLineEdit->setText( i.value );
201 if ( attributeChanged )
227 && !
config( QStringLiteral(
"AllowNull" ) ).toBool( ) )
231 QTimer::singleShot( 0, [
this ]
238 int QgsValueRelationWidgetWrapper::columnCount()
const 240 return std::max( 1,
config( QStringLiteral(
"NofColumns" ) ).toInt() );
243 void QgsValueRelationWidgetWrapper::populate( )
250 else if ( mCache.empty() )
258 if (
config( QStringLiteral(
"AllowNull" ) ).toBool( ) )
260 whileBlocking( mComboBox )->addItem( tr(
"(no selection)" ), QVariant(
field().type( ) ) );
265 whileBlocking( mComboBox )->addItem( element.value, element.key );
268 else if ( mTableWidget )
270 const int nofColumns = columnCount();
272 if ( ! mCache.empty() )
274 mTableWidget->setRowCount( ( mCache.size() + nofColumns - 1 ) / nofColumns );
277 mTableWidget->setRowCount( 1 );
278 mTableWidget->setColumnCount( nofColumns );
285 if ( column == nofColumns )
290 QTableWidgetItem *item =
nullptr;
291 item =
new QTableWidgetItem( element.value );
292 item->setData( Qt::UserRole, element.key );
297 else if ( mLineEdit )
300 values.reserve( mCache.size() );
305 QStringListModel *m =
new QStringListModel( values, mLineEdit );
306 QCompleter *completer =
new QCompleter( m, mLineEdit );
307 completer->setCaseSensitivity( Qt::CaseInsensitive );
308 mLineEdit->setCompleter( completer );
314 const int nofColumns = columnCount();
318 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
320 for (
int i = 0; i < nofColumns; ++i )
322 whileBlocking( mTableWidget )->item( j, i )->setCheckState( Qt::PartiallyChecked );
326 else if ( mComboBox )
330 else if ( mLineEdit )
338 if ( mEnabled == enabled )
345 auto signalBlockedTableWidget =
whileBlocking( mTableWidget );
346 Q_UNUSED( signalBlockedTableWidget )
348 for (
int j = 0; j < mTableWidget->rowCount(); j++ )
350 for (
int i = 0; i < mTableWidget->columnCount(); ++i )
352 QTableWidgetItem *item = mTableWidget->item( j, i );
356 item->setFlags( item->flags() | Qt::ItemIsEnabled );
358 item->setFlags( item->flags() & ~Qt::ItemIsEnabled );
bool isValid() const
Returns the validity of this feature.
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.
Represents a vector layer which manages a vector based data sets.
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.