34#include "moc_qgseditformconfig.cpp"
37 : d( new QgsEditFormConfigPrivate() )
44 d->mDataDefinedFieldProperties[ fieldName ] = properties;
49 return d->mDataDefinedFieldProperties.
value( fieldName );
54 return QgsEditFormConfigPrivate::propertyDefinitions();
59 const int fieldIndex = d->mFields.indexOf( widgetName );
60 if ( fieldIndex != -1 )
61 return d->mFields.at( fieldIndex ).editorWidgetSetup().config();
63 return d->mWidgetConfigs.value( widgetName );
66void QgsEditFormConfig::setFields(
const QgsFields &fields )
71 if ( !d->mConfiguredRootContainer )
73 d->mInvisibleRootContainer->clear();
74 for (
int i = 0; i < d->mFields.size(); ++i )
77 d->mInvisibleRootContainer->addChildElement( field );
82void QgsEditFormConfig::onRelationsLoaded()
86 for ( QgsAttributeEditorElement *relElem : relations )
88 QgsAttributeEditorRelation *rel =
dynamic_cast< QgsAttributeEditorRelation *
>( relElem );
96bool QgsEditFormConfig::legacyUpdateRelationWidgetInTabs(
QgsAttributeEditorContainer *container,
const QString &widgetName,
const QVariantMap &config )
98 const QList<QgsAttributeEditorElement *> children = container->
children();
99 for ( QgsAttributeEditorElement *child : children )
103 QgsAttributeEditorContainer *container =
dynamic_cast<QgsAttributeEditorContainer *
>( child );
104 if ( legacyUpdateRelationWidgetInTabs( container, widgetName, config ) )
112 QgsAttributeEditorRelation *relation =
dynamic_cast< QgsAttributeEditorRelation *
>( child );
115 if ( relation->
relation().
id() == widgetName )
117 if ( config.contains( QStringLiteral(
"nm-rel" ) ) )
121 if ( config.contains( QStringLiteral(
"force-suppress-popup" ) ) )
135 if ( d->mFields.indexOf( widgetName ) != -1 )
137 QgsDebugError( QStringLiteral(
"Trying to set a widget config for a field on QgsEditFormConfig. Use layer->setEditorWidgetSetup() instead." ) );
142 if ( config.contains( QStringLiteral(
"force-suppress-popup" ) ) || config.contains( QStringLiteral(
"nm-rel" ) ) )
144 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 ) );
145 legacyUpdateRelationWidgetInTabs( d->mInvisibleRootContainer, widgetName, config );
149 d->mWidgetConfigs[widgetName] = config;
156 return d->mWidgetConfigs.remove( widgetName ) != 0;
184 d->mInvisibleRootContainer->addChildElement( data );
189 return d->mInvisibleRootContainer->children();
195 d->mInvisibleRootContainer->clear();
200 return d->mInvisibleRootContainer;
205 return d->mEditorLayout;
211 d->mEditorLayout = editorLayout;
214 d->mConfiguredRootContainer =
true;
219 return d->mUiFormPath;
224 if ( !ui.isEmpty() && !QUrl::fromUserInput( ui ).isLocalFile() )
243 if ( idx >= 0 && idx < d->mFields.count() )
248 if ( d->mFields.at( idx ).isReadOnly() )
250 return !d->mFieldEditables.value( d->mFields.at( idx ).name(),
true );
258 if ( idx >= 0 && idx < d->mFields.count() )
259 return d->mLabelOnTop.value( d->mFields.at( idx ).name(),
false );
266 if ( idx >= 0 && idx < d->mFields.count() )
269 d->mFieldEditables[ d->mFields.at( idx ).name()] = !
readOnly;
275 if ( idx >= 0 && idx < d->mFields.count() )
278 d->mLabelOnTop[ d->mFields.at( idx ).name()] = onTop;
284 if ( index >= 0 && index < d->mFields.count() )
292 if ( index >= 0 && index < d->mFields.count() )
301 if ( index >= 0 && index < d->mFields.count() )
309 if ( index >= 0 && index < d->mFields.count() )
312 d->mReuseLastValuePolicy[ d->mFields.at( index ).name()] = policy;
318 return d->mInitFunction;
324 d->mInitFunction = function;
340 return d->mInitFilePath;
346 d->mInitFilePath = filePath;
349 if ( !filePath.isEmpty() && !QUrl::fromUserInput( filePath ).isLocalFile() )
358 return d->mInitCodeSource;
369 return d->mSuppressForm;
375 d->mSuppressForm = s;
384 const QDomNode editFormNode = node.namedItem( QStringLiteral(
"editform" ) );
385 if ( !editFormNode.isNull() )
387 const QDomElement e = editFormNode.toElement();
388 const bool tolerantRemoteUrls = e.hasAttribute( QStringLiteral(
"tolerant" ) );
389 if ( !e.text().isEmpty() )
395 const bool localFile = QFileInfo::exists( uiFormPath );
396 if ( localFile || tolerantRemoteUrls || uiFormPath.startsWith( QLatin1String(
"http" ) ) )
401 const QDomNode editFormInitNode = node.namedItem( QStringLiteral(
"editforminit" ) );
402 if ( !editFormInitNode.isNull() )
404 d->mInitFunction = editFormInitNode.toElement().text();
407 const QDomNode editFormInitCodeSourceNode = node.namedItem( QStringLiteral(
"editforminitcodesource" ) );
408 if ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() )
413 const QDomNode editFormInitCodeNode = node.namedItem( QStringLiteral(
"editforminitcode" ) );
414 if ( !editFormInitCodeNode.isNull() )
416 setInitCode( editFormInitCodeNode.toElement().text() );
424 const int dotPos = d->mInitFunction.lastIndexOf(
'.' );
428 setInitCode( QStringLiteral(
"from %1 import %2\n" ).arg( d->mInitFunction.left( dotPos ), d->mInitFunction.mid( dotPos + 1 ) ) );
432 const QDomNode editFormInitFilePathNode = node.namedItem( QStringLiteral(
"editforminitfilepath" ) );
433 if ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() )
438 const QDomNode fFSuppNode = node.namedItem( QStringLiteral(
"featformsuppress" ) );
439 if ( fFSuppNode.isNull() )
445 const QDomElement e = fFSuppNode.toElement();
450 const QDomNode editorLayoutNode = node.namedItem( QStringLiteral(
"editorlayout" ) );
451 if ( editorLayoutNode.isNull() )
457 if ( editorLayoutNode.toElement().text() == QLatin1String(
"uifilelayout" ) )
461 else if ( editorLayoutNode.toElement().text() == QLatin1String(
"tablayout" ) )
471 d->mFieldEditables.clear();
472 const QDomNodeList editableNodeList = node.namedItem( QStringLiteral(
"editable" ) ).toElement().childNodes();
473 for (
int i = 0; i < editableNodeList.size(); ++i )
475 const QDomElement editableElement = editableNodeList.at( i ).toElement();
476 d->mFieldEditables.insert( editableElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( editableElement.attribute( QStringLiteral(
"editable" ) ).toInt() ) );
479 d->mLabelOnTop.clear();
480 const QDomNodeList labelOnTopNodeList = node.namedItem( QStringLiteral(
"labelOnTop" ) ).toElement().childNodes();
481 for (
int i = 0; i < labelOnTopNodeList.size(); ++i )
483 const QDomElement labelOnTopElement = labelOnTopNodeList.at( i ).toElement();
484 d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( labelOnTopElement.attribute( QStringLiteral(
"labelOnTop" ) ).toInt() ) );
487 d->mReuseLastValuePolicy.clear();
489 const QDomNodeList reuseLastValueNodeList = node.namedItem( QStringLiteral(
"reuseLastValue" ) ).toElement().childNodes();
490 for (
int i = 0; i < reuseLastValueNodeList.size(); ++i )
492 const QDomElement reuseLastValueElement = reuseLastValueNodeList.at( i ).toElement();
495 const QDomNodeList reuseLastValuePolicyNodeList = node.namedItem( QStringLiteral(
"reuseLastValuePolicy" ) ).toElement().childNodes();
496 for (
int i = 0; i < reuseLastValuePolicyNodeList.size(); ++i )
498 const QDomElement reuseLastValuePolicyElement = reuseLastValuePolicyNodeList.at( i ).toElement();
503 const QDomNodeList fieldDDPropertiesNodeList = node.namedItem( QStringLiteral(
"dataDefinedFieldProperties" ) ).toElement().childNodes();
504 for (
int i = 0; i < fieldDDPropertiesNodeList.size(); ++i )
506 const QDomElement DDElement = fieldDDPropertiesNodeList.at( i ).toElement();
509 d->mDataDefinedFieldProperties.insert( DDElement.attribute( QStringLiteral(
"name" ) ), collection );
512 const QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral(
"widgets" ) ).toElement().childNodes();
514 for (
int i = 0; i < widgetsNodeList.size(); ++i )
516 const QDomElement widgetElement = widgetsNodeList.at( i ).toElement();
519 d->mWidgetConfigs[widgetElement.attribute( QStringLiteral(
"name" ) )] = config.toMap();
523 const QDomNode attributeEditorFormNode = node.namedItem( QStringLiteral(
"attributeEditorForm" ) );
524 if ( !attributeEditorFormNode.isNull() )
526 const QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
528 if ( attributeEditorFormNodeList.size() )
530 d->mConfiguredRootContainer =
true;
533 for (
int i = 0; i < attributeEditorFormNodeList.size(); i++ )
535 QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
537 fixLegacyConfig( elem );
539 const QString layerId = node.namedItem( QStringLiteral(
"id" ) ).toElement().text();
541 if ( attributeEditorWidget )
542 addTab( attributeEditorWidget );
550void QgsEditFormConfig::fixLegacyConfig( QDomElement &el )
const
554 if ( el.tagName() == QLatin1String(
"attributeEditorRelation" ) )
556 if ( !el.hasAttribute( QStringLiteral(
"forceSuppressFormPopup" ) ) )
559 const bool forceSuppress =
widgetConfig( el.attribute( QStringLiteral(
"relation" ) ) ).value( QStringLiteral(
"force-suppress-popup" ),
false ).toBool();
560 el.setAttribute( QStringLiteral(
"forceSuppressFormPopup" ), forceSuppress ? 1 : 0 );
562 if ( !el.hasAttribute( QStringLiteral(
"nmRelationId" ) ) )
565 el.setAttribute( QStringLiteral(
"nmRelationId" ),
widgetConfig( el.attribute( QStringLiteral(
"relation" ) ) ).value( QStringLiteral(
"nm-rel" ) ).toString() );
569 const QDomNodeList children = el.childNodes();
570 for (
int i = 0; i < children.size(); i++ )
572 QDomElement child = children.at( i ).toElement();
573 fixLegacyConfig( child );
574 el.replaceChild( child, children.at( i ) );
580 QDomDocument doc( node.ownerDocument() );
582 QDomElement efField = doc.createElement( QStringLiteral(
"editform" ) );
583 efField.setAttribute( QStringLiteral(
"tolerant" ), QStringLiteral(
"1" ) );
585 efField.appendChild( efText );
586 node.appendChild( efField );
588 QDomElement efiField = doc.createElement( QStringLiteral(
"editforminit" ) );
590 efiField.appendChild( doc.createTextNode(
initFunction() ) );
591 node.appendChild( efiField );
593 QDomElement eficsField = doc.createElement( QStringLiteral(
"editforminitcodesource" ) );
594 eficsField.appendChild( doc.createTextNode( QString::number(
static_cast< int >(
initCodeSource() ) ) ) );
595 node.appendChild( eficsField );
597 QDomElement efifpField = doc.createElement( QStringLiteral(
"editforminitfilepath" ) );
599 node.appendChild( efifpField );
601 QDomElement eficField = doc.createElement( QStringLiteral(
"editforminitcode" ) );
602 eficField.appendChild( doc.createCDATASection(
initCode() ) );
603 node.appendChild( eficField );
605 QDomElement fFSuppElem = doc.createElement( QStringLiteral(
"featformsuppress" ) );
606 const QDomText fFSuppText = doc.createTextNode( QString::number(
static_cast< int >(
suppress() ) ) );
607 fFSuppElem.appendChild( fFSuppText );
608 node.appendChild( fFSuppElem );
611 QDomElement editorLayoutElem = doc.createElement( QStringLiteral(
"editorlayout" ) );
615 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"uifilelayout" ) ) );
619 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"tablayout" ) ) );
624 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"generatedlayout" ) ) );
628 node.appendChild( editorLayoutElem );
631 if ( !
tabs().empty() && d->mConfiguredRootContainer )
633 QDomElement tabsElem = doc.createElement( QStringLiteral(
"attributeEditorForm" ) );
634 const QDomElement rootElem = d->mInvisibleRootContainer->toDomElement( doc );
635 const QDomNodeList elemList = rootElem.childNodes();
636 while ( !elemList.isEmpty() )
638 tabsElem.appendChild( elemList.at( 0 ) );
640 node.appendChild( tabsElem );
643 QDomElement editableElem = doc.createElement( QStringLiteral(
"editable" ) );
644 for (
auto editIt = d->mFieldEditables.constBegin(); editIt != d->mFieldEditables.constEnd(); ++editIt )
646 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
647 fieldElem.setAttribute( QStringLiteral(
"name" ), editIt.key() );
648 fieldElem.setAttribute( QStringLiteral(
"editable" ), editIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
649 editableElem.appendChild( fieldElem );
651 node.appendChild( editableElem );
653 QDomElement labelOnTopElem = doc.createElement( QStringLiteral(
"labelOnTop" ) );
654 for (
auto labelOnTopIt = d->mLabelOnTop.constBegin(); labelOnTopIt != d->mLabelOnTop.constEnd(); ++labelOnTopIt )
656 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
657 fieldElem.setAttribute( QStringLiteral(
"name" ), labelOnTopIt.key() );
658 fieldElem.setAttribute( QStringLiteral(
"labelOnTop" ), labelOnTopIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
659 labelOnTopElem.appendChild( fieldElem );
661 node.appendChild( labelOnTopElem );
663 QDomElement reuseLastValuePolicyElem = doc.createElement( QStringLiteral(
"reuseLastValuePolicy" ) );
664 for (
auto reuseLastValuePolicyIt = d->mReuseLastValuePolicy.constBegin(); reuseLastValuePolicyIt != d->mReuseLastValuePolicy.constEnd(); ++reuseLastValuePolicyIt )
666 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
667 fieldElem.setAttribute( QStringLiteral(
"name" ), reuseLastValuePolicyIt.key() );
668 fieldElem.setAttribute( QStringLiteral(
"reuseLastValuePolicy" ),
qgsEnumValueToKey( reuseLastValuePolicyIt.value() ) );
669 reuseLastValuePolicyElem.appendChild( fieldElem );
671 node.appendChild( reuseLastValuePolicyElem );
674 QDomElement ddFieldPropsElement = doc.createElement( QStringLiteral(
"dataDefinedFieldProperties" ) );
675 for (
auto it = d->mDataDefinedFieldProperties.constBegin(); it != d->mDataDefinedFieldProperties.constEnd(); ++it )
677 QDomElement ddPropsElement = doc.createElement( QStringLiteral(
"field" ) );
678 ddPropsElement.setAttribute( QStringLiteral(
"name" ), it.key() );
680 ddFieldPropsElement.appendChild( ddPropsElement );
682 node.appendChild( ddFieldPropsElement );
684 QDomElement widgetsElem = doc.createElement( QStringLiteral(
"widgets" ) );
686 QMap<QString, QVariantMap >::ConstIterator configIt( d->mWidgetConfigs.constBegin() );
688 while ( configIt != d->mWidgetConfigs.constEnd() )
690 QDomElement widgetElem = doc.createElement( QStringLiteral(
"widget" ) );
691 widgetElem.setAttribute( QStringLiteral(
"name" ), configIt.key() );
695 configElem.setTagName( QStringLiteral(
"config" ) );
696 widgetElem.appendChild( configElem );
697 widgetsElem.appendChild( widgetElem );
701 node.appendChild( widgetsElem );
AttributeFormReuseLastValuePolicy
Attribute form policy for reusing last entered values.
@ AllowedDefaultOn
Reuse of last values allowed and enabled by default.
@ NotAllowed
Reuse of last values not allowed.
AttributeFormSuppression
Available form types for layout of the attribute form editor.
@ Default
Use the application-wide setting.
AttributeFormPythonInitCodeSource
The Python init code source for attribute forms.
@ Dialog
Use the Python code provided in the dialog.
AttributeFormLayout
Available form types for layout of the attribute form editor.
@ DragAndDrop
"Drag and drop" layout. Needs to be configured.
@ AutoGenerated
Autogenerate a simple tabular layout for the form.
@ UiFile
Load a .ui file for the layout. Needs to be configured.
@ Immediate
Action will start immediately.
@ Expression
Field is calculated from an expression.
@ Join
Field originates from a joined layer.
virtual bool readXml(const QDomElement &collectionElem, const QgsPropertiesDefinition &definitions)
Reads property collection state from an XML element.
static QgsNetworkContentFetcherRegistry * networkContentFetcherRegistry()
Returns the application's network content registry used for fetching temporary files during QGIS sess...
A container for attribute editors, used to group them visually in the attribute form if it is set to ...
QList< QgsAttributeEditorElement * > children() const
Gets a list of the children elements of this container.
An abstract base class for any elements of a drag and drop form.
static QgsAttributeEditorElement * create(const QDomElement &element, const QString &layerId, const QgsFields &fields, const QgsReadWriteContext &context, QgsAttributeEditorElement *parent=nullptr)
Constructs the editor element from the given element.
This element will load a field's widget onto the form.
void setNmRelationId(const QVariant &nmRelationId=QVariant())
Sets nmRelationId for the relation id of the second relation involved in an N:M relation.
bool init(QgsRelationManager *relManager)
Initializes the relation from the id.
const QgsRelation & relation() const
Gets the id of the relation which shall be embedded.
void setForceSuppressFormPopup(bool forceSuppressFormPopup)
Sets force suppress form popup status to forceSuppressFormPopup.
Container of fields for a vector layer.
static void logMessage(const QString &message, const QString &tag=QString(), Qgis::MessageLevel level=Qgis::MessageLevel::Warning, bool notifyUser=true, const char *file=__builtin_FILE(), const char *function=__builtin_FUNCTION(), int line=__builtin_LINE())
Adds a message to the log instance (and creates it if necessary).
QgsFetchedContent * fetch(const QString &url, Qgis::ActionStart fetchingMode=Qgis::ActionStart::Deferred, const QString &authConfig=QString())
Initialize a download for the given URL.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
static QgsProject * instance()
Returns the QgsProject singleton instance.
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
QVariant value(int key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const final
Returns the calculated value of the property with the specified key from within the collection.
Allows entering a context category and takes care of leaving this category on deletion of the class.
A container for the context for various read/write operations on objects.
QgsReadWriteContextCategoryPopper enterCategory(const QString &category, const QString &details=QString()) const
Push a category to the stack.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
T qgsEnumKeyToValue(const QString &key, const T &defaultValue, bool tryValueAsKey=true, bool *returnOk=nullptr)
Returns the value corresponding to the given key of an enum.
QString qgsEnumValueToKey(const T &value, bool *returnOk=nullptr)
Returns the value for the given key of an enum.
#define QgsDebugError(str)
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.