QGIS API Documentation 4.3.0-Master (0cef2469b72)
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
6 Email : patrick.valsecchi@camptocamp.com
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 <QString>
22#include <qjsonarray.h>
23
24#include "moc_qgslistwidgetwrapper.cpp"
25
26using namespace Qt::StringLiterals;
27
29 : QgsEditorWidgetWrapper( layer, fieldIdx, editor, parent )
30{}
31
33{
34 mWidget->setList( QVariantList() );
35}
36
38{
39 if ( mWidget )
40 {
41 mWidget->setReadOnly( !enabled );
42 }
43}
44
45QWidget *QgsListWidgetWrapper::createWidget( QWidget *parent )
46{
47 QFrame *ret = new QFrame( parent );
48 ret->setFrameShape( QFrame::StyledPanel );
49 QHBoxLayout *layout = new QHBoxLayout( ret );
50 layout->setContentsMargins( 0, 0, 0, 0 );
51 QMetaType::Type usedSubType = field().subType();
52 // on a string field, we set also the subtype to string
53 if ( field().type() == QMetaType::Type::QString )
54 {
55 usedSubType = QMetaType::Type::QString;
56 }
57 QgsListWidget *widget = new QgsListWidget( usedSubType, ret );
58 layout->addWidget( widget );
59
60 if ( isInTable( parent ) )
61 {
62 // if to be put in a table, set a decent size
63 ret->setMinimumSize( QSize( 320, 110 ) );
64 }
65 return ret;
66}
67
68void QgsListWidgetWrapper::initWidget( QWidget *editor )
69{
70 mWidget = qobject_cast<QgsListWidget *>( editor );
71 if ( !mWidget )
72 {
73 mWidget = editor->findChild<QgsListWidget *>();
74 }
75
76 connect( mWidget, &QgsListWidget::valueChanged, this, &QgsListWidgetWrapper::onValueChanged );
77}
78
80{
81 return mWidget ? mWidget->valid() : true;
82}
83
84void QgsListWidgetWrapper::updateValues( const QVariant &value, const QVariantList & )
85{
86 QVariantList listValue = value.toList();
87 // on a string field, we convert the json content to variant list
88 if ( field().type() == QMetaType::Type::QString )
89 {
90 const QJsonDocument doc = QJsonDocument::fromJson( value.toString().toUtf8() );
91 if ( !doc.isNull() && doc.isArray() )
92 {
93 listValue = doc.array().toVariantList();
94 }
95 }
96 mWidget->setList( listValue );
97}
98
100{
101 const QMetaType::Type type = field().type();
102 if ( !mWidget )
104 const QVariantList list = mWidget->list();
105 if ( list.size() == 0 && config( u"EmptyIsNull"_s ).toBool() )
106 {
107 return QVariant();
108 }
109 if ( type == QMetaType::Type::QStringList )
110 {
111 QStringList result;
112 for ( QVariantList::const_iterator it = list.constBegin(); it != list.constEnd(); ++it )
113 result.append( it->toString() );
114 return result;
115 }
116 else
117 {
118 return list;
119 }
120}
121
122void QgsListWidgetWrapper::onValueChanged()
123{
125}
126
128{
129 // Nothing
130}
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:63
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:153
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 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.