28 qDeleteAll( mChildren );
32 : d( new QgsEditFormConfigPrivate() )
38 int fieldIndex = d->mFields.indexOf( widgetName );
39 if ( fieldIndex != -1 )
40 return d->mFields.at( fieldIndex ).editorWidgetSetup().config();
42 return d->mWidgetConfigs.value( widgetName );
45 void QgsEditFormConfig::setFields(
const QgsFields &fields )
50 if ( !d->mConfiguredRootContainer )
52 d->mInvisibleRootContainer->clear();
53 for (
int i = 0; i < d->mFields.size(); ++i )
56 d->mInvisibleRootContainer->addChildElement( field );
61 void QgsEditFormConfig::onRelationsLoaded()
77 if ( d->mFields.indexOf( widgetName ) != -1 )
79 QgsDebugMsg(
"Trying to set a widget config for a field on QgsEditFormConfig. Use layer->setEditorWidgetSetup() instead." );
84 d->mWidgetConfigs[widgetName] = config;
91 return d->mWidgetConfigs.remove( widgetName ) != 0;
116 d->mInvisibleRootContainer->addChildElement( data );
121 return d->mInvisibleRootContainer->children();
127 d->mInvisibleRootContainer->clear();
132 return d->mInvisibleRootContainer;
137 return d->mEditorLayout;
143 d->mEditorLayout = editorLayout;
146 d->mConfiguredRootContainer =
true;
151 return d->mUiFormPath;
156 if ( ui.isEmpty() || ui.isNull() )
169 if ( idx >= 0 && idx < d->mFields.count() )
174 return !d->mFieldEditables.value( d->mFields.at( idx ).name(), true );
182 if ( idx >= 0 && idx < d->mFields.count() )
183 return d->mLabelOnTop.value( d->mFields.at( idx ).name(), false );
190 if ( idx >= 0 && idx < d->mFields.count() )
193 d->mFieldEditables[ d->mFields.at( idx ).name()] = !
readOnly;
199 if ( idx >= 0 && idx < d->mFields.count() )
202 d->mLabelOnTop[ d->mFields.at( idx ).name()] = onTop;
208 return d->mInitFunction;
214 d->mInitFunction =
function;
230 return d->mInitFilePath;
236 d->mInitFilePath = filePath;
241 return d->mInitCodeSource;
252 return d->mSuppressForm;
258 d->mSuppressForm = s;
265 QDomNode editFormNode = node.namedItem( QStringLiteral(
"editform" ) );
266 if ( !editFormNode.isNull() )
268 QDomElement e = editFormNode.toElement();
272 QDomNode editFormInitNode = node.namedItem( QStringLiteral(
"editforminit" ) );
273 if ( !editFormInitNode.isNull() )
275 d->mInitFunction = editFormInitNode.toElement().text();
278 QDomNode editFormInitCodeSourceNode = node.namedItem( QStringLiteral(
"editforminitcodesource" ) );
279 if ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() )
281 setInitCodeSource( static_cast< QgsEditFormConfig::PythonInitCodeSource >( editFormInitCodeSourceNode.toElement().text().toInt() ) );
284 QDomNode editFormInitCodeNode = node.namedItem( QStringLiteral(
"editforminitcode" ) );
285 if ( !editFormInitCodeNode.isNull() )
287 setInitCode( editFormInitCodeNode.toElement().text() );
295 int dotPos = d->mInitFunction.lastIndexOf(
'.' );
299 setInitCode( QStringLiteral(
"from %1 import %2\n" ).arg( d->mInitFunction.left( dotPos ), d->mInitFunction.mid( dotPos + 1 ) ) );
303 QDomNode editFormInitFilePathNode = node.namedItem( QStringLiteral(
"editforminitfilepath" ) );
304 if ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() )
309 QDomNode fFSuppNode = node.namedItem( QStringLiteral(
"featformsuppress" ) );
310 if ( fFSuppNode.isNull() )
316 QDomElement e = fFSuppNode.toElement();
321 QDomNode editorLayoutNode = node.namedItem( QStringLiteral(
"editorlayout" ) );
322 if ( editorLayoutNode.isNull() )
328 if ( editorLayoutNode.toElement().text() == QLatin1String(
"uifilelayout" ) )
332 else if ( editorLayoutNode.toElement().text() == QLatin1String(
"tablayout" ) )
342 d->mFieldEditables.clear();
343 QDomNodeList editableNodeList = node.namedItem( QStringLiteral(
"editable" ) ).toElement().childNodes();
344 for (
int i = 0; i < editableNodeList.size(); ++i )
346 QDomElement editableElement = editableNodeList.at( i ).toElement();
347 d->mFieldEditables.insert( editableElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( editableElement.attribute( QStringLiteral(
"editable" ) ).toInt() ) );
350 d->mLabelOnTop.clear();
351 QDomNodeList labelOnTopNodeList = node.namedItem( QStringLiteral(
"labelOnTop" ) ).toElement().childNodes();
352 for (
int i = 0; i < labelOnTopNodeList.size(); ++i )
354 QDomElement labelOnTopElement = labelOnTopNodeList.at( i ).toElement();
355 d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( labelOnTopElement.attribute( QStringLiteral(
"labelOnTop" ) ).toInt() ) );
358 QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral(
"widgets" ) ).toElement().childNodes();
360 for (
int i = 0; i < widgetsNodeList.size(); ++i )
362 QDomElement widgetElement = widgetsNodeList.at( i ).toElement();
365 d->mWidgetConfigs[widgetElement.attribute( QStringLiteral(
"name" ) )] = config.toMap();
369 QDomNode attributeEditorFormNode = node.namedItem( QStringLiteral(
"attributeEditorForm" ) );
370 if ( !attributeEditorFormNode.isNull() )
372 QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
374 if ( attributeEditorFormNodeList.size() )
376 d->mConfiguredRootContainer =
true;
379 for (
int i = 0; i < attributeEditorFormNodeList.size(); i++ )
381 QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
384 addTab( attributeEditorWidget );
394 QDomDocument doc( node.ownerDocument() );
396 QDomElement efField = doc.createElement( QStringLiteral(
"editform" ) );
398 efField.appendChild( efText );
399 node.appendChild( efField );
401 QDomElement efiField = doc.createElement( QStringLiteral(
"editforminit" ) );
403 efiField.appendChild( doc.createTextNode(
initFunction() ) );
404 node.appendChild( efiField );
406 QDomElement eficsField = doc.createElement( QStringLiteral(
"editforminitcodesource" ) );
407 eficsField.appendChild( doc.createTextNode( QString::number(
initCodeSource() ) ) );
408 node.appendChild( eficsField );
410 QDomElement efifpField = doc.createElement( QStringLiteral(
"editforminitfilepath" ) );
412 node.appendChild( efifpField );
414 QDomElement eficField = doc.createElement( QStringLiteral(
"editforminitcode" ) );
415 eficField.appendChild( doc.createCDATASection(
initCode() ) );
416 node.appendChild( eficField );
418 QDomElement fFSuppElem = doc.createElement( QStringLiteral(
"featformsuppress" ) );
419 QDomText fFSuppText = doc.createTextNode( QString::number(
suppress() ) );
420 fFSuppElem.appendChild( fFSuppText );
421 node.appendChild( fFSuppElem );
424 QDomElement editorLayoutElem = doc.createElement( QStringLiteral(
"editorlayout" ) );
428 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"uifilelayout" ) ) );
432 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"tablayout" ) ) );
437 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"generatedlayout" ) ) );
441 node.appendChild( editorLayoutElem );
444 if ( !
tabs().empty() && d->mConfiguredRootContainer )
446 QDomElement tabsElem = doc.createElement( QStringLiteral(
"attributeEditorForm" ) );
448 QDomElement rootElem = d->mInvisibleRootContainer->toDomElement( doc );
449 QDomNodeList elemList = rootElem.childNodes();
451 while ( !elemList.isEmpty() )
453 tabsElem.appendChild( elemList.at( 0 ) );
456 node.appendChild( tabsElem );
459 QDomElement editableElem = doc.createElement( QStringLiteral(
"editable" ) );
460 for (
auto editIt = d->mFieldEditables.constBegin(); editIt != d->mFieldEditables.constEnd(); ++editIt )
462 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
463 fieldElem.setAttribute( QStringLiteral(
"name" ), editIt.key() );
464 fieldElem.setAttribute( QStringLiteral(
"editable" ), editIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
465 editableElem.appendChild( fieldElem );
467 node.appendChild( editableElem );
469 QDomElement labelOnTopElem = doc.createElement( QStringLiteral(
"labelOnTop" ) );
470 for (
auto labelOnTopIt = d->mLabelOnTop.constBegin(); labelOnTopIt != d->mLabelOnTop.constEnd(); ++labelOnTopIt )
472 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
473 fieldElem.setAttribute( QStringLiteral(
"name" ), labelOnTopIt.key() );
474 fieldElem.setAttribute( QStringLiteral(
"labelOnTop" ), labelOnTopIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
475 labelOnTopElem.appendChild( fieldElem );
477 node.appendChild( labelOnTopElem );
479 QDomElement widgetsElem = doc.createElement( QStringLiteral(
"widgets" ) );
481 QMap<QString, QVariantMap >::ConstIterator configIt( d->mWidgetConfigs.constBegin() );
483 while ( configIt != d->mWidgetConfigs.constEnd() )
485 QDomElement widgetElem = doc.createElement( QStringLiteral(
"widget" ) );
486 widgetElem.setAttribute( QStringLiteral(
"name" ), configIt.key() );
490 configElem.setTagName( QStringLiteral(
"config" ) );
491 widgetElem.appendChild( configElem );
492 widgetsElem.appendChild( widgetElem );
496 node.appendChild( widgetsElem );
505 if ( elem.tagName() == QLatin1String(
"attributeEditorContainer" ) )
509 int cc = elem.attribute( QStringLiteral(
"columnCount" ) ).toInt( &ok );
514 bool isGroupBox = elem.attribute( QStringLiteral(
"groupBox" ) ).toInt( &ok );
520 bool visibilityExpressionEnabled = elem.attribute( QStringLiteral(
"visibilityExpressionEnabled" ) ).toInt( &ok );
524 visibilityExpression.
setEnabled( visibilityExpressionEnabled );
525 visibilityExpression.
setData( QgsExpression( elem.attribute( QStringLiteral(
"visibilityExpression" ) ) ) );
529 QDomNodeList childNodeList = elem.childNodes();
531 for (
int i = 0; i < childNodeList.size(); i++ )
533 QDomElement childElem = childNodeList.at( i ).toElement();
539 newElement = container;
541 else if ( elem.tagName() == QLatin1String(
"attributeEditorField" ) )
543 QString name = elem.attribute( QStringLiteral(
"name" ) );
544 int idx = d->mFields.lookupField( name );
547 else if ( elem.tagName() == QLatin1String(
"attributeEditorRelation" ) )
551 QString name = elem.attribute( QStringLiteral(
"name" ) );
553 relElement->
setShowLinkButton( elem.attribute( QStringLiteral(
"showLinkButton" ), QStringLiteral(
"1" ) ).toInt() );
554 relElement->
setShowUnlinkButton( elem.attribute( QStringLiteral(
"showUnlinkButton" ), QStringLiteral(
"1" ) ).toInt() );
555 newElement = relElement;
560 if ( elem.hasAttribute( QStringLiteral(
"showLabel" ) ) )
561 newElement->
setShowLabel( elem.attribute( QStringLiteral(
"showLabel" ) ).toInt() );
576 mColumnCount = columnCount;
587 element->mIsGroupBox = mIsGroupBox;
588 element->mColumnCount = mColumnCount;
589 element->mVisibilityExpression = mVisibilityExpression;
594 void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem )
const 596 elem.setAttribute( QStringLiteral(
"columnCount" ), mColumnCount );
597 elem.setAttribute( QStringLiteral(
"groupBox" ), mIsGroupBox ? 1 : 0 );
598 elem.setAttribute( QStringLiteral(
"visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
599 elem.setAttribute( QStringLiteral(
"visibilityExpression" ), mVisibilityExpression->expression() );
603 QDomDocument doc = elem.ownerDocument();
608 QString QgsAttributeEditorContainer::typeIdentifier()
const 610 return QStringLiteral(
"attributeEditorContainer" );
The class is used as a container of context for various read/write operations on other objects...
bool init(QgsRelationManager *relManager)
Initializes the relation from the id.
Field comes from a joined layer (originIndex / 1000 = index of the join, originIndex % 1000 = index w...
This is an abstract base class for any elements of a drag and drop form.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
void setVisibilityExpression(const QgsOptionalExpression &visibilityExpression)
The visibility expression is used in the attribute form to show or hide this container based on an ex...
~QgsAttributeEditorContainer() override
Container of fields for a vector layer.
This element will load a field's widget onto the form.
QDomElement toDomElement(QDomDocument &doc) const
Get the XML Dom element to save this element.
This element will load a relation editor onto the form.
void setColumnCount(int columnCount)
Set the number of columns in this group.
void setData(const T &data)
Set the payload data.
An expression with an additional enabled flag.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
void setShowLabel(bool showLabel)
Controls if this element should be labeled with a title (field, relation or groupname).
void setShowLinkButton(bool showLinkButton)
Determines if the "link feature" button should be shown.
void setShowUnlinkButton(bool showUnlinkButton)
Determines if the "unlink feature" button should be shown.
virtual void setIsGroupBox(bool isGroupBox)
Determines if this container is rendered as collapsible group box or tab in a tabwidget.
int columnCount() const
Get the number of columns in this group.
static QgsProject * instance()
Returns the QgsProject singleton instance.
This is a container for attribute editors, used to group them visually in the attribute form if it is...
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
virtual void addChildElement(QgsAttributeEditorElement *element)
Add a child element to this container.
void setEnabled(bool enabled)
Set if this optional is enabled.
virtual QgsAttributeEditorElement * clone(QgsAttributeEditorElement *parent) const =0
Returns a clone of this element.
Field is calculated from an expression.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
QgsAttributeEditorElement * clone(QgsAttributeEditorElement *parent) const override
Creates a deep copy of this element.