33 : d( new QgsEditFormConfigPrivate() )
40 d->mDataDefinedFieldProperties[ fieldName ] = properties;
45 return d->mDataDefinedFieldProperties.value( fieldName );
50 return QgsEditFormConfigPrivate::propertyDefinitions();
55 const int fieldIndex = d->mFields.indexOf( widgetName );
56 if ( fieldIndex != -1 )
57 return d->mFields.at( fieldIndex ).editorWidgetSetup().config();
59 return d->mWidgetConfigs.value( widgetName );
62void 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 );
78void QgsEditFormConfig::onRelationsLoaded()
92bool 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 QgsDebugError( 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 if ( d->mFields.at( idx ).isReadOnly() )
243 return !d->mFieldEditables.value( d->mFields.at( idx ).name(),
true );
251 if ( idx >= 0 && idx < d->mFields.count() )
252 return d->mLabelOnTop.value( d->mFields.at( idx ).name(),
false );
259 if ( idx >= 0 && idx < d->mFields.count() )
262 d->mFieldEditables[ d->mFields.at( idx ).name()] = !
readOnly;
268 if ( idx >= 0 && idx < d->mFields.count() )
271 d->mLabelOnTop[ d->mFields.at( idx ).name()] = onTop;
277 if ( index >= 0 && index < d->mFields.count() )
278 return d->mReuseLastValue.value( d->mFields.at( index ).name(),
false );
285 if ( index >= 0 && index < d->mFields.count() )
288 d->mReuseLastValue[ d->mFields.at( index ).name()] = reuse;
294 return d->mInitFunction;
300 d->mInitFunction = function;
316 return d->mInitFilePath;
322 d->mInitFilePath = filePath;
325 if ( !filePath.isEmpty() && !QUrl::fromUserInput( filePath ).isLocalFile() )
334 return d->mInitCodeSource;
345 return d->mSuppressForm;
351 d->mSuppressForm = s;
360 const QDomNode editFormNode = node.namedItem( QStringLiteral(
"editform" ) );
361 if ( !editFormNode.isNull() )
363 const QDomElement e = editFormNode.toElement();
364 const bool tolerantRemoteUrls = e.hasAttribute( QStringLiteral(
"tolerant" ) );
365 if ( !e.text().isEmpty() )
371 const bool localFile = QFileInfo::exists( uiFormPath );
372 if ( localFile || tolerantRemoteUrls || uiFormPath.startsWith( QLatin1String(
"http" ) ) )
377 const QDomNode editFormInitNode = node.namedItem( QStringLiteral(
"editforminit" ) );
378 if ( !editFormInitNode.isNull() )
380 d->mInitFunction = editFormInitNode.toElement().text();
383 const QDomNode editFormInitCodeSourceNode = node.namedItem( QStringLiteral(
"editforminitcodesource" ) );
384 if ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() )
389 const QDomNode editFormInitCodeNode = node.namedItem( QStringLiteral(
"editforminitcode" ) );
390 if ( !editFormInitCodeNode.isNull() )
392 setInitCode( editFormInitCodeNode.toElement().text() );
400 const int dotPos = d->mInitFunction.lastIndexOf(
'.' );
404 setInitCode( QStringLiteral(
"from %1 import %2\n" ).arg( d->mInitFunction.left( dotPos ), d->mInitFunction.mid( dotPos + 1 ) ) );
408 const QDomNode editFormInitFilePathNode = node.namedItem( QStringLiteral(
"editforminitfilepath" ) );
409 if ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() )
414 const QDomNode fFSuppNode = node.namedItem( QStringLiteral(
"featformsuppress" ) );
415 if ( fFSuppNode.isNull() )
421 const QDomElement e = fFSuppNode.toElement();
426 const QDomNode editorLayoutNode = node.namedItem( QStringLiteral(
"editorlayout" ) );
427 if ( editorLayoutNode.isNull() )
433 if ( editorLayoutNode.toElement().text() == QLatin1String(
"uifilelayout" ) )
437 else if ( editorLayoutNode.toElement().text() == QLatin1String(
"tablayout" ) )
447 d->mFieldEditables.clear();
448 const QDomNodeList editableNodeList = node.namedItem( QStringLiteral(
"editable" ) ).toElement().childNodes();
449 for (
int i = 0; i < editableNodeList.size(); ++i )
451 const QDomElement editableElement = editableNodeList.at( i ).toElement();
452 d->mFieldEditables.insert( editableElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( editableElement.attribute( QStringLiteral(
"editable" ) ).toInt() ) );
455 d->mLabelOnTop.clear();
456 const QDomNodeList labelOnTopNodeList = node.namedItem( QStringLiteral(
"labelOnTop" ) ).toElement().childNodes();
457 for (
int i = 0; i < labelOnTopNodeList.size(); ++i )
459 const QDomElement labelOnTopElement = labelOnTopNodeList.at( i ).toElement();
460 d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( labelOnTopElement.attribute( QStringLiteral(
"labelOnTop" ) ).toInt() ) );
463 d->mReuseLastValue.clear();
464 const QDomNodeList reuseLastValueNodeList = node.namedItem( QStringLiteral(
"reuseLastValue" ) ).toElement().childNodes();
465 for (
int i = 0; i < reuseLastValueNodeList.size(); ++i )
467 const QDomElement reuseLastValueElement = reuseLastValueNodeList.at( i ).toElement();
468 d->mReuseLastValue.insert( reuseLastValueElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( reuseLastValueElement.attribute( QStringLiteral(
"reuseLastValue" ) ).toInt() ) );
472 const QDomNodeList fieldDDPropertiesNodeList = node.namedItem( QStringLiteral(
"dataDefinedFieldProperties" ) ).toElement().childNodes();
473 for (
int i = 0; i < fieldDDPropertiesNodeList.size(); ++i )
475 const QDomElement DDElement = fieldDDPropertiesNodeList.at( i ).toElement();
478 d->mDataDefinedFieldProperties.insert( DDElement.attribute( QStringLiteral(
"name" ) ), collection );
481 const QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral(
"widgets" ) ).toElement().childNodes();
483 for (
int i = 0; i < widgetsNodeList.size(); ++i )
485 const QDomElement widgetElement = widgetsNodeList.at( i ).toElement();
488 d->mWidgetConfigs[widgetElement.attribute( QStringLiteral(
"name" ) )] = config.toMap();
492 const QDomNode attributeEditorFormNode = node.namedItem( QStringLiteral(
"attributeEditorForm" ) );
493 if ( !attributeEditorFormNode.isNull() )
495 const QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
497 if ( attributeEditorFormNodeList.size() )
499 d->mConfiguredRootContainer =
true;
502 for (
int i = 0; i < attributeEditorFormNodeList.size(); i++ )
504 QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
506 fixLegacyConfig( elem );
508 const QString layerId = node.namedItem( QStringLiteral(
"id" ) ).toElement().text();
510 if ( attributeEditorWidget )
511 addTab( attributeEditorWidget );
519void QgsEditFormConfig::fixLegacyConfig( QDomElement &el )
const
523 if ( el.tagName() == QLatin1String(
"attributeEditorRelation" ) )
525 if ( !el.hasAttribute( QStringLiteral(
"forceSuppressFormPopup" ) ) )
528 const bool forceSuppress =
widgetConfig( el.attribute( QStringLiteral(
"relation" ) ) ).value( QStringLiteral(
"force-suppress-popup" ),
false ).toBool();
529 el.setAttribute( QStringLiteral(
"forceSuppressFormPopup" ), forceSuppress ? 1 : 0 );
531 if ( !el.hasAttribute( QStringLiteral(
"nmRelationId" ) ) )
534 el.setAttribute( QStringLiteral(
"nmRelationId" ),
widgetConfig( el.attribute( QStringLiteral(
"relation" ) ) ).value( QStringLiteral(
"nm-rel" ) ).toString() );
538 const QDomNodeList children = el.childNodes();
539 for (
int i = 0; i < children.size(); i++ )
541 QDomElement child = children.at( i ).toElement();
542 fixLegacyConfig( child );
543 el.replaceChild( child, children.at( i ) );
549 QDomDocument doc( node.ownerDocument() );
551 QDomElement efField = doc.createElement( QStringLiteral(
"editform" ) );
552 efField.setAttribute( QStringLiteral(
"tolerant" ), QStringLiteral(
"1" ) );
554 efField.appendChild( efText );
555 node.appendChild( efField );
557 QDomElement efiField = doc.createElement( QStringLiteral(
"editforminit" ) );
559 efiField.appendChild( doc.createTextNode(
initFunction() ) );
560 node.appendChild( efiField );
562 QDomElement eficsField = doc.createElement( QStringLiteral(
"editforminitcodesource" ) );
563 eficsField.appendChild( doc.createTextNode( QString::number(
static_cast< int >(
initCodeSource() ) ) ) );
564 node.appendChild( eficsField );
566 QDomElement efifpField = doc.createElement( QStringLiteral(
"editforminitfilepath" ) );
568 node.appendChild( efifpField );
570 QDomElement eficField = doc.createElement( QStringLiteral(
"editforminitcode" ) );
571 eficField.appendChild( doc.createCDATASection(
initCode() ) );
572 node.appendChild( eficField );
574 QDomElement fFSuppElem = doc.createElement( QStringLiteral(
"featformsuppress" ) );
575 const QDomText fFSuppText = doc.createTextNode( QString::number(
static_cast< int >(
suppress() ) ) );
576 fFSuppElem.appendChild( fFSuppText );
577 node.appendChild( fFSuppElem );
580 QDomElement editorLayoutElem = doc.createElement( QStringLiteral(
"editorlayout" ) );
584 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"uifilelayout" ) ) );
588 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"tablayout" ) ) );
593 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"generatedlayout" ) ) );
597 node.appendChild( editorLayoutElem );
600 if ( !
tabs().empty() && d->mConfiguredRootContainer )
602 QDomElement tabsElem = doc.createElement( QStringLiteral(
"attributeEditorForm" ) );
603 const QDomElement rootElem = d->mInvisibleRootContainer->toDomElement( doc );
604 const QDomNodeList elemList = rootElem.childNodes();
605 while ( !elemList.isEmpty() )
607 tabsElem.appendChild( elemList.at( 0 ) );
609 node.appendChild( tabsElem );
612 QDomElement editableElem = doc.createElement( QStringLiteral(
"editable" ) );
613 for (
auto editIt = d->mFieldEditables.constBegin(); editIt != d->mFieldEditables.constEnd(); ++editIt )
615 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
616 fieldElem.setAttribute( QStringLiteral(
"name" ), editIt.key() );
617 fieldElem.setAttribute( QStringLiteral(
"editable" ), editIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
618 editableElem.appendChild( fieldElem );
620 node.appendChild( editableElem );
622 QDomElement labelOnTopElem = doc.createElement( QStringLiteral(
"labelOnTop" ) );
623 for (
auto labelOnTopIt = d->mLabelOnTop.constBegin(); labelOnTopIt != d->mLabelOnTop.constEnd(); ++labelOnTopIt )
625 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
626 fieldElem.setAttribute( QStringLiteral(
"name" ), labelOnTopIt.key() );
627 fieldElem.setAttribute( QStringLiteral(
"labelOnTop" ), labelOnTopIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
628 labelOnTopElem.appendChild( fieldElem );
630 node.appendChild( labelOnTopElem );
632 QDomElement reuseLastValueElem = doc.createElement( QStringLiteral(
"reuseLastValue" ) );
633 for (
auto reuseLastValueIt = d->mReuseLastValue.constBegin(); reuseLastValueIt != d->mReuseLastValue.constEnd(); ++reuseLastValueIt )
635 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
636 fieldElem.setAttribute( QStringLiteral(
"name" ), reuseLastValueIt.key() );
637 fieldElem.setAttribute( QStringLiteral(
"reuseLastValue" ), reuseLastValueIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
638 reuseLastValueElem.appendChild( fieldElem );
640 node.appendChild( reuseLastValueElem );
643 QDomElement ddFieldPropsElement = doc.createElement( QStringLiteral(
"dataDefinedFieldProperties" ) );
644 for (
auto it = d->mDataDefinedFieldProperties.constBegin(); it != d->mDataDefinedFieldProperties.constEnd(); ++it )
646 QDomElement ddPropsElement = doc.createElement( QStringLiteral(
"field" ) );
647 ddPropsElement.setAttribute( QStringLiteral(
"name" ), it.key() );
649 ddFieldPropsElement.appendChild( ddPropsElement );
651 node.appendChild( ddFieldPropsElement );
653 QDomElement widgetsElem = doc.createElement( QStringLiteral(
"widgets" ) );
655 QMap<QString, QVariantMap >::ConstIterator configIt( d->mWidgetConfigs.constBegin() );
657 while ( configIt != d->mWidgetConfigs.constEnd() )
659 QDomElement widgetElem = doc.createElement( QStringLiteral(
"widget" ) );
660 widgetElem.setAttribute( QStringLiteral(
"name" ), configIt.key() );
664 configElem.setTagName( QStringLiteral(
"config" ) );
665 widgetElem.appendChild( configElem );
666 widgetsElem.appendChild( widgetElem );
670 node.appendChild( widgetsElem );
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...
This is a container for attribute editors, used to group them visually in the attribute form if it is...
QList< QgsAttributeEditorElement * > children() const
Gets a list of the children elements of this container.
This is 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.
This element will load a relation editor 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)
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.
QgsRelationManager * relationManager
static QgsProject * instance()
Returns the QgsProject singleton instance.
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
Allows entering a context category and takes care of leaving this category on deletion of the class.
The class is used as a container of context for various read/write operations on other objects.
MAYBE_UNUSED NODISCARD 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.
#define QgsDebugError(str)
QMap< int, QgsPropertyDefinition > QgsPropertiesDefinition
Definition of available properties.