29 qDeleteAll( mChildren );
33 : d( new QgsEditFormConfigPrivate() )
40 d->mDataDefinedFieldProperties[ fieldName ] = properties;
45 return d->mDataDefinedFieldProperties.value( fieldName );
50 return QgsEditFormConfigPrivate::propertyDefinitions();
55 int fieldIndex = d->mFields.indexOf( widgetName );
56 if ( fieldIndex != -1 )
57 return d->mFields.at( fieldIndex ).editorWidgetSetup().config();
59 return d->mWidgetConfigs.value( widgetName );
62 void QgsEditFormConfig::setFields(
const QgsFields &fields )
67 if ( !d->mConfiguredRootContainer )
69 d->mInvisibleRootContainer->clear();
70 for (
int i = 0; i < d->mFields.size(); ++i )
73 d->mInvisibleRootContainer->addChildElement(
field );
78 void QgsEditFormConfig::onRelationsLoaded()
92 bool QgsEditFormConfig::legacyUpdateRelationWidgetInTabs(
QgsAttributeEditorContainer *container,
const QString &widgetName,
const QVariantMap &config )
94 const QList<QgsAttributeEditorElement *> children = container->
children();
100 if ( legacyUpdateRelationWidgetInTabs( container, widgetName, config ) )
111 if ( relation->
relation().
id() == widgetName )
113 if ( config.contains( QStringLiteral(
"nm-rel" ) ) )
117 if ( config.contains( QStringLiteral(
"force-suppress-popup" ) ) )
131 if ( d->mFields.indexOf( widgetName ) != -1 )
133 QgsDebugMsg( QStringLiteral(
"Trying to set a widget config for a field on QgsEditFormConfig. Use layer->setEditorWidgetSetup() instead." ) );
138 if ( config.contains( QStringLiteral(
"force-suppress-popup" ) ) || config.contains( QStringLiteral(
"nm-rel" ) ) )
140 QgsMessageLog::logMessage( QStringLiteral(
"Deprecation Warning: Trying to set a relation config directly on the relation %1. Relation settings should be done for the specific widget instance instead. Use attributeEditorRelation->setNmRelationId() or attributeEditorRelation->setForceSuppressFormPopup() instead." ).arg( widgetName ) );
141 legacyUpdateRelationWidgetInTabs( d->mInvisibleRootContainer, widgetName, config );
145 d->mWidgetConfigs[widgetName] = config;
152 return d->mWidgetConfigs.remove( widgetName ) != 0;
177 d->mInvisibleRootContainer->addChildElement( data );
182 return d->mInvisibleRootContainer->children();
188 d->mInvisibleRootContainer->clear();
193 return d->mInvisibleRootContainer;
198 return d->mEditorLayout;
204 d->mEditorLayout = editorLayout;
207 d->mConfiguredRootContainer =
true;
212 return d->mUiFormPath;
217 if ( !ui.isEmpty() && !QUrl::fromUserInput( ui ).isLocalFile() )
236 if ( idx >= 0 && idx < d->mFields.count() )
241 return !d->mFieldEditables.value( d->mFields.at( idx ).name(),
true );
249 if ( idx >= 0 && idx < d->mFields.count() )
250 return d->mLabelOnTop.value( d->mFields.at( idx ).name(),
false );
257 if ( idx >= 0 && idx < d->mFields.count() )
260 d->mFieldEditables[ d->mFields.at( idx ).name()] = !
readOnly;
266 if ( idx >= 0 && idx < d->mFields.count() )
269 d->mLabelOnTop[ d->mFields.at( idx ).name()] = onTop;
275 return d->mInitFunction;
281 d->mInitFunction =
function;
297 return d->mInitFilePath;
303 d->mInitFilePath = filePath;
306 if ( !filePath.isEmpty() && !QUrl::fromUserInput( filePath ).isLocalFile() )
315 return d->mInitCodeSource;
326 return d->mSuppressForm;
332 d->mSuppressForm = s;
341 QDomNode editFormNode = node.namedItem( QStringLiteral(
"editform" ) );
342 if ( !editFormNode.isNull() )
344 QDomElement e = editFormNode.toElement();
345 const bool tolerantRemoteUrls = e.hasAttribute( QStringLiteral(
"tolerant" ) );
346 if ( !e.text().isEmpty() )
352 const bool localFile = QFileInfo::exists( uiFormPath );
353 if ( localFile || tolerantRemoteUrls || uiFormPath.startsWith( QLatin1String(
"http" ) ) )
358 QDomNode editFormInitNode = node.namedItem( QStringLiteral(
"editforminit" ) );
359 if ( !editFormInitNode.isNull() )
361 d->mInitFunction = editFormInitNode.toElement().text();
364 QDomNode editFormInitCodeSourceNode = node.namedItem( QStringLiteral(
"editforminitcodesource" ) );
365 if ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() )
370 QDomNode editFormInitCodeNode = node.namedItem( QStringLiteral(
"editforminitcode" ) );
371 if ( !editFormInitCodeNode.isNull() )
373 setInitCode( editFormInitCodeNode.toElement().text() );
381 int dotPos = d->mInitFunction.lastIndexOf(
'.' );
385 setInitCode( QStringLiteral(
"from %1 import %2\n" ).arg( d->mInitFunction.left( dotPos ), d->mInitFunction.mid( dotPos + 1 ) ) );
389 QDomNode editFormInitFilePathNode = node.namedItem( QStringLiteral(
"editforminitfilepath" ) );
390 if ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() )
395 QDomNode fFSuppNode = node.namedItem( QStringLiteral(
"featformsuppress" ) );
396 if ( fFSuppNode.isNull() )
402 QDomElement e = fFSuppNode.toElement();
407 QDomNode editorLayoutNode = node.namedItem( QStringLiteral(
"editorlayout" ) );
408 if ( editorLayoutNode.isNull() )
414 if ( editorLayoutNode.toElement().text() == QLatin1String(
"uifilelayout" ) )
418 else if ( editorLayoutNode.toElement().text() == QLatin1String(
"tablayout" ) )
428 d->mFieldEditables.clear();
429 QDomNodeList editableNodeList = node.namedItem( QStringLiteral(
"editable" ) ).toElement().childNodes();
430 for (
int i = 0; i < editableNodeList.size(); ++i )
432 QDomElement editableElement = editableNodeList.at( i ).toElement();
433 d->mFieldEditables.insert( editableElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( editableElement.attribute( QStringLiteral(
"editable" ) ).toInt() ) );
436 d->mLabelOnTop.clear();
437 QDomNodeList labelOnTopNodeList = node.namedItem( QStringLiteral(
"labelOnTop" ) ).toElement().childNodes();
438 for (
int i = 0; i < labelOnTopNodeList.size(); ++i )
440 QDomElement labelOnTopElement = labelOnTopNodeList.at( i ).toElement();
441 d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( labelOnTopElement.attribute( QStringLiteral(
"labelOnTop" ) ).toInt() ) );
445 QDomNodeList fieldDDPropertiesNodeList = node.namedItem( QStringLiteral(
"dataDefinedFieldProperties" ) ).toElement().childNodes();
446 for (
int i = 0; i < fieldDDPropertiesNodeList.size(); ++i )
448 QDomElement DDElement = fieldDDPropertiesNodeList.at( i ).toElement();
451 d->mDataDefinedFieldProperties.insert( DDElement.attribute( QStringLiteral(
"name" ) ), collection );
454 QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral(
"widgets" ) ).toElement().childNodes();
456 for (
int i = 0; i < widgetsNodeList.size(); ++i )
458 QDomElement widgetElement = widgetsNodeList.at( i ).toElement();
461 d->mWidgetConfigs[widgetElement.attribute( QStringLiteral(
"name" ) )] = config.toMap();
465 QDomNode attributeEditorFormNode = node.namedItem( QStringLiteral(
"attributeEditorForm" ) );
466 if ( !attributeEditorFormNode.isNull() )
468 QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
470 if ( attributeEditorFormNodeList.size() )
472 d->mConfiguredRootContainer =
true;
475 for (
int i = 0; i < attributeEditorFormNodeList.size(); i++ )
477 QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
480 if ( attributeEditorWidget )
481 addTab( attributeEditorWidget );
491 QDomDocument doc( node.ownerDocument() );
493 QDomElement efField = doc.createElement( QStringLiteral(
"editform" ) );
494 efField.setAttribute( QStringLiteral(
"tolerant" ), QStringLiteral(
"1" ) );
496 efField.appendChild( efText );
497 node.appendChild( efField );
499 QDomElement efiField = doc.createElement( QStringLiteral(
"editforminit" ) );
501 efiField.appendChild( doc.createTextNode(
initFunction() ) );
502 node.appendChild( efiField );
504 QDomElement eficsField = doc.createElement( QStringLiteral(
"editforminitcodesource" ) );
505 eficsField.appendChild( doc.createTextNode( QString::number(
initCodeSource() ) ) );
506 node.appendChild( eficsField );
508 QDomElement efifpField = doc.createElement( QStringLiteral(
"editforminitfilepath" ) );
510 node.appendChild( efifpField );
512 QDomElement eficField = doc.createElement( QStringLiteral(
"editforminitcode" ) );
513 eficField.appendChild( doc.createCDATASection(
initCode() ) );
514 node.appendChild( eficField );
516 QDomElement fFSuppElem = doc.createElement( QStringLiteral(
"featformsuppress" ) );
517 QDomText fFSuppText = doc.createTextNode( QString::number(
suppress() ) );
518 fFSuppElem.appendChild( fFSuppText );
519 node.appendChild( fFSuppElem );
522 QDomElement editorLayoutElem = doc.createElement( QStringLiteral(
"editorlayout" ) );
526 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"uifilelayout" ) ) );
530 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"tablayout" ) ) );
535 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"generatedlayout" ) ) );
539 node.appendChild( editorLayoutElem );
542 if ( !
tabs().empty() && d->mConfiguredRootContainer )
544 QDomElement tabsElem = doc.createElement( QStringLiteral(
"attributeEditorForm" ) );
546 QDomElement rootElem = d->mInvisibleRootContainer->toDomElement( doc );
547 QDomNodeList elemList = rootElem.childNodes();
549 while ( !elemList.isEmpty() )
551 tabsElem.appendChild( elemList.at( 0 ) );
554 node.appendChild( tabsElem );
557 QDomElement editableElem = doc.createElement( QStringLiteral(
"editable" ) );
558 for (
auto editIt = d->mFieldEditables.constBegin(); editIt != d->mFieldEditables.constEnd(); ++editIt )
560 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
561 fieldElem.setAttribute( QStringLiteral(
"name" ), editIt.key() );
562 fieldElem.setAttribute( QStringLiteral(
"editable" ), editIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
563 editableElem.appendChild( fieldElem );
565 node.appendChild( editableElem );
567 QDomElement labelOnTopElem = doc.createElement( QStringLiteral(
"labelOnTop" ) );
568 for (
auto labelOnTopIt = d->mLabelOnTop.constBegin(); labelOnTopIt != d->mLabelOnTop.constEnd(); ++labelOnTopIt )
570 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
571 fieldElem.setAttribute( QStringLiteral(
"name" ), labelOnTopIt.key() );
572 fieldElem.setAttribute( QStringLiteral(
"labelOnTop" ), labelOnTopIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
573 labelOnTopElem.appendChild( fieldElem );
575 node.appendChild( labelOnTopElem );
578 QDomElement ddFieldPropsElement = doc.createElement( QStringLiteral(
"dataDefinedFieldProperties" ) );
579 for (
auto it = d->mDataDefinedFieldProperties.constBegin(); it != d->mDataDefinedFieldProperties.constEnd(); ++it )
581 QDomElement ddPropsElement = doc.createElement( QStringLiteral(
"field" ) );
582 ddPropsElement.setAttribute( QStringLiteral(
"name" ), it.key() );
584 ddFieldPropsElement.appendChild( ddPropsElement );
586 node.appendChild( ddFieldPropsElement );
588 QDomElement widgetsElem = doc.createElement( QStringLiteral(
"widgets" ) );
590 QMap<QString, QVariantMap >::ConstIterator configIt( d->mWidgetConfigs.constBegin() );
592 while ( configIt != d->mWidgetConfigs.constEnd() )
594 QDomElement widgetElem = doc.createElement( QStringLiteral(
"widget" ) );
595 widgetElem.setAttribute( QStringLiteral(
"name" ), configIt.key() );
599 configElem.setTagName( QStringLiteral(
"config" ) );
600 widgetElem.appendChild( configElem );
601 widgetsElem.appendChild( widgetElem );
605 node.appendChild( widgetsElem );
614 if ( elem.tagName() == QLatin1String(
"attributeEditorContainer" ) )
616 QColor backgroundColor( elem.attribute( QStringLiteral(
"backgroundColor" ), QString() ) );
618 elem.attribute( QStringLiteral(
"name" ) ) ), parent, backgroundColor );
620 int cc = elem.attribute( QStringLiteral(
"columnCount" ) ).toInt( &ok );
625 bool isGroupBox = elem.attribute( QStringLiteral(
"groupBox" ) ).toInt( &ok );
631 bool visibilityExpressionEnabled = elem.attribute( QStringLiteral(
"visibilityExpressionEnabled" ) ).toInt( &ok );
635 visibilityExpression.
setEnabled( visibilityExpressionEnabled );
636 visibilityExpression.
setData(
QgsExpression( elem.attribute( QStringLiteral(
"visibilityExpression" ) ) ) );
640 QDomNodeList childNodeList = elem.childNodes();
642 for (
int i = 0; i < childNodeList.size(); i++ )
644 QDomElement childElem = childNodeList.at( i ).toElement();
650 newElement = container;
652 else if ( elem.tagName() == QLatin1String(
"attributeEditorField" ) )
654 QString name = elem.attribute( QStringLiteral(
"name" ) );
655 int idx = d->mFields.lookupField( name );
658 else if ( elem.tagName() == QLatin1String(
"attributeEditorRelation" ) )
663 if ( elem.hasAttribute(
"buttons" ) )
665 QString buttonString = elem.attribute( QStringLiteral(
"buttons" ),
qgsFlagValueToKeys( QgsAttributeEditorRelation::Button::AllButtons ) );
671 QgsAttributeEditorRelation::Buttons buttons = QgsAttributeEditorRelation::Button::AllButtons;
672 buttons.setFlag( QgsAttributeEditorRelation::Button::Link, elem.attribute( QStringLiteral(
"showLinkButton" ), QStringLiteral(
"1" ) ).toInt() );
673 buttons.setFlag( QgsAttributeEditorRelation::Button::Unlink, elem.attribute( QStringLiteral(
"showUnlinkButton" ), QStringLiteral(
"1" ) ).toInt() );
674 buttons.setFlag( QgsAttributeEditorRelation::Button::SaveChildEdits, elem.attribute( QStringLiteral(
"showSaveChildEditsButton" ), QStringLiteral(
"1" ) ).toInt() );
677 if ( elem.hasAttribute( QStringLiteral(
"forceSuppressFormPopup" ) ) )
687 if ( elem.hasAttribute( QStringLiteral(
"nmRelationId" ) ) )
689 relElement->
setNmRelationId( elem.attribute( QStringLiteral(
"nmRelationId" ) ) );
696 if ( elem.hasAttribute(
"label" ) )
698 QString label = elem.attribute( QStringLiteral(
"label" ) );
701 newElement = relElement;
703 else if ( elem.tagName() == QLatin1String(
"attributeEditorQmlElement" ) )
707 newElement = qmlElement;
709 else if ( elem.tagName() == QLatin1String(
"attributeEditorHtmlElement" ) )
713 newElement = htmlElement;
718 if ( elem.hasAttribute( QStringLiteral(
"showLabel" ) ) )
719 newElement->
setShowLabel( elem.attribute( QStringLiteral(
"showLabel" ) ).toInt() );
741 const auto childElements =
children();
747 element->mIsGroupBox = mIsGroupBox;
748 element->mColumnCount = mColumnCount;
749 element->mVisibilityExpression = mVisibilityExpression;
754 void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem )
const
756 elem.setAttribute( QStringLiteral(
"columnCount" ), mColumnCount );
757 elem.setAttribute( QStringLiteral(
"groupBox" ), mIsGroupBox ? 1 : 0 );
758 elem.setAttribute( QStringLiteral(
"visibilityExpressionEnabled" ), mVisibilityExpression.
enabled() ? 1 : 0 );
759 elem.setAttribute( QStringLiteral(
"visibilityExpression" ), mVisibilityExpression->expression() );
760 if ( mBackgroundColor.isValid() )
761 elem.setAttribute( QStringLiteral(
"backgroundColor" ), mBackgroundColor.name( ) );
762 const auto constMChildren = mChildren;
765 QDomDocument doc = elem.ownerDocument();
766 elem.appendChild( child->toDomElement( doc ) );
770 QString QgsAttributeEditorContainer::typeIdentifier()
const
772 return QStringLiteral(
"attributeEditorContainer" );