QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
qgslistwidgetwrapper.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslistwidgetwrapper.cpp
3 --------------------------------------
4 Date : 09.2016
5 Copyright : (C) 2016 Patrick Valsecchi
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsattributeform.h"
19#include "qgslistwidget.h"
20
21#include "moc_qgslistwidgetwrapper.cpp"
22
24 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
25{
26}
27
29{
30 mWidget->setList( QVariantList() );
31}
32
34{
35 if ( mWidget )
36 {
37 mWidget->setReadOnly( !enabled );
38 }
39}
40
41QWidget *QgsListWidgetWrapper::createWidget( QWidget *parent )
42{
43 QFrame *ret = new QFrame( parent );
44 ret->setFrameShape( QFrame::StyledPanel );
45 QHBoxLayout *layout = new QHBoxLayout( ret );
46 layout->setContentsMargins( 0, 0, 0, 0 );
47 QgsListWidget *widget = new QgsListWidget( field().subType(), ret );
48 layout->addWidget( widget );
49
50 if ( isInTable( parent ) )
51 {
52 // if to be put in a table, set a decent size
53 ret->setMinimumSize( QSize( 320, 110 ) );
54 }
55 return ret;
56}
57
58void QgsListWidgetWrapper::initWidget( QWidget *editor )
59{
60 mWidget = qobject_cast<QgsListWidget *>( editor );
61 if ( !mWidget )
62 {
63 mWidget = editor->findChild<QgsListWidget *>();
64 }
65
66 connect( mWidget, &QgsListWidget::valueChanged, this, &QgsListWidgetWrapper::onValueChanged );
67}
68
70{
71 return mWidget ? mWidget->valid() : true;
72}
73
74void QgsListWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
75{
76 mWidget->setList( value.toList() );
77}
78
80{
81 const QMetaType::Type type = field().type();
82 if ( !mWidget )
84 const QVariantList list = mWidget->list();
85 if ( list.size() == 0 && config( QStringLiteral( "EmptyIsNull" ) ).toBool() )
86 {
87 return QVariant();
88 }
89 if ( type == QMetaType::Type::QStringList )
90 {
91 QStringList result;
92 for ( QVariantList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it )
93 result.append( it->toString() );
94 return result;
95 }
96 else
97 {
98 return list;
99 }
100}
101
102void QgsListWidgetWrapper::onValueChanged()
103{
105}
106
108{
109 // Nothing
110}
virtual void updateConstraintWidgetStatus()
This should update the widget with a visual cue if a constraint status changed.
int fieldIdx() const
Access the field index.
QgsEditorWidgetWrapper(QgsVectorLayer *vl, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Create a new widget wrapper.
static bool isInTable(const QWidget *parent)
Check if the given widget or one of its parent is a QTableView.
void emitValueChanged()
Will call the value() method to determine the emitted value.
QgsField field() const
Access the field.
QMetaType::Type type
Definition qgsfield.h:61
QgsListWidgetWrapper(QgsVectorLayer *layer, int fieldIdx, QWidget *editor=nullptr, QWidget *parent=nullptr)
Constructor for QgsListWidgetWrapper.
QVariant value() const override
Will be used to access the widget's value.
QWidget * createWidget(QWidget *parent) override
This method should create a new widget with the provided parent.
void showIndeterminateState() override
Sets the widget to display in an indeterminate "mixed value" state.
bool valid() const override
Returns true if the widget has been properly initialized.
void setEnabled(bool enabled) override
void initWidget(QWidget *editor) override
This method should initialize the editor widget with runtime data.
Widget allowing editing a QVariantList, using a table.
void setList(const QVariantList &list)
Set the initial value of the widget.
void valueChanged()
Emitted each time a key or a value is changed.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
Represents a vector layer which manages a vector based dataset.
QWidget * widget()
Access the widget managed by this wrapper.
QVariant config(const QString &key, const QVariant &defaultVal=QVariant()) const
Use this inside your overridden classes to access the configuration.
QgsVectorLayer * layer() const
Returns the vector layer associated with the widget.