30 qDeleteAll( mChildren );
34 : d( new QgsEditFormConfigPrivate() )
40 int fieldIndex = d->mFields.indexOf( widgetName );
41 if ( fieldIndex != -1 )
42 return d->mFields.at( fieldIndex ).editorWidgetSetup().config();
44 return d->mWidgetConfigs.value( widgetName );
47 void QgsEditFormConfig::setFields(
const QgsFields &fields )
52 if ( !d->mConfiguredRootContainer )
54 d->mInvisibleRootContainer->clear();
55 for (
int i = 0; i < d->mFields.size(); ++i )
58 d->mInvisibleRootContainer->addChildElement( field );
63 void QgsEditFormConfig::onRelationsLoaded()
79 if ( d->mFields.indexOf( widgetName ) != -1 )
81 QgsDebugMsg( QStringLiteral(
"Trying to set a widget config for a field on QgsEditFormConfig. Use layer->setEditorWidgetSetup() instead." ) );
86 d->mWidgetConfigs[widgetName] = config;
93 return d->mWidgetConfigs.remove( widgetName ) != 0;
118 d->mInvisibleRootContainer->addChildElement( data );
123 return d->mInvisibleRootContainer->children();
129 d->mInvisibleRootContainer->clear();
134 return d->mInvisibleRootContainer;
139 return d->mEditorLayout;
145 d->mEditorLayout = editorLayout;
148 d->mConfiguredRootContainer =
true;
153 return d->mUiFormPath;
158 if ( !ui.isEmpty() && !QUrl::fromUserInput( ui ).isLocalFile() )
177 if ( idx >= 0 && idx < d->mFields.count() )
182 return !d->mFieldEditables.value( d->mFields.at( idx ).name(), true );
190 if ( idx >= 0 && idx < d->mFields.count() )
191 return d->mLabelOnTop.value( d->mFields.at( idx ).name(), false );
198 if ( idx >= 0 && idx < d->mFields.count() )
201 d->mFieldEditables[ d->mFields.at( idx ).name()] = !
readOnly;
207 if ( idx >= 0 && idx < d->mFields.count() )
210 d->mLabelOnTop[ d->mFields.at( idx ).name()] = onTop;
216 return d->mInitFunction;
222 d->mInitFunction =
function;
238 return d->mInitFilePath;
244 d->mInitFilePath = filePath;
247 if ( !filePath.isEmpty() && !QUrl::fromUserInput( filePath ).isLocalFile() )
256 return d->mInitCodeSource;
267 return d->mSuppressForm;
273 d->mSuppressForm = s;
282 QDomNode editFormNode = node.namedItem( QStringLiteral(
"editform" ) );
283 if ( !editFormNode.isNull() )
285 QDomElement e = editFormNode.toElement();
286 const bool tolerantRemoteUrls = e.hasAttribute( QStringLiteral(
"tolerant" ) );
287 if ( !e.text().isEmpty() )
293 const bool localFile = QFileInfo::exists( uiFormPath );
294 if ( localFile || tolerantRemoteUrls || uiFormPath.startsWith( QLatin1String(
"http" ) ) )
299 QDomNode editFormInitNode = node.namedItem( QStringLiteral(
"editforminit" ) );
300 if ( !editFormInitNode.isNull() )
302 d->mInitFunction = editFormInitNode.toElement().text();
305 QDomNode editFormInitCodeSourceNode = node.namedItem( QStringLiteral(
"editforminitcodesource" ) );
306 if ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() )
308 setInitCodeSource( static_cast< QgsEditFormConfig::PythonInitCodeSource >( editFormInitCodeSourceNode.toElement().text().toInt() ) );
311 QDomNode editFormInitCodeNode = node.namedItem( QStringLiteral(
"editforminitcode" ) );
312 if ( !editFormInitCodeNode.isNull() )
314 setInitCode( editFormInitCodeNode.toElement().text() );
322 int dotPos = d->mInitFunction.lastIndexOf(
'.' );
326 setInitCode( QStringLiteral(
"from %1 import %2\n" ).arg( d->mInitFunction.left( dotPos ), d->mInitFunction.mid( dotPos + 1 ) ) );
330 QDomNode editFormInitFilePathNode = node.namedItem( QStringLiteral(
"editforminitfilepath" ) );
331 if ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() )
336 QDomNode fFSuppNode = node.namedItem( QStringLiteral(
"featformsuppress" ) );
337 if ( fFSuppNode.isNull() )
343 QDomElement e = fFSuppNode.toElement();
348 QDomNode editorLayoutNode = node.namedItem( QStringLiteral(
"editorlayout" ) );
349 if ( editorLayoutNode.isNull() )
355 if ( editorLayoutNode.toElement().text() == QLatin1String(
"uifilelayout" ) )
359 else if ( editorLayoutNode.toElement().text() == QLatin1String(
"tablayout" ) )
369 d->mFieldEditables.clear();
370 QDomNodeList editableNodeList = node.namedItem( QStringLiteral(
"editable" ) ).toElement().childNodes();
371 for (
int i = 0; i < editableNodeList.size(); ++i )
373 QDomElement editableElement = editableNodeList.at( i ).toElement();
374 d->mFieldEditables.insert( editableElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( editableElement.attribute( QStringLiteral(
"editable" ) ).toInt() ) );
377 d->mLabelOnTop.clear();
378 QDomNodeList labelOnTopNodeList = node.namedItem( QStringLiteral(
"labelOnTop" ) ).toElement().childNodes();
379 for (
int i = 0; i < labelOnTopNodeList.size(); ++i )
381 QDomElement labelOnTopElement = labelOnTopNodeList.at( i ).toElement();
382 d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( labelOnTopElement.attribute( QStringLiteral(
"labelOnTop" ) ).toInt() ) );
385 QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral(
"widgets" ) ).toElement().childNodes();
387 for (
int i = 0; i < widgetsNodeList.size(); ++i )
389 QDomElement widgetElement = widgetsNodeList.at( i ).toElement();
392 d->mWidgetConfigs[widgetElement.attribute( QStringLiteral(
"name" ) )] = config.toMap();
396 QDomNode attributeEditorFormNode = node.namedItem( QStringLiteral(
"attributeEditorForm" ) );
397 if ( !attributeEditorFormNode.isNull() )
399 QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
401 if ( attributeEditorFormNodeList.size() )
403 d->mConfiguredRootContainer =
true;
406 for (
int i = 0; i < attributeEditorFormNodeList.size(); i++ )
408 QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
411 addTab( attributeEditorWidget );
421 QDomDocument doc( node.ownerDocument() );
423 QDomElement efField = doc.createElement( QStringLiteral(
"editform" ) );
424 efField.setAttribute( QStringLiteral(
"tolerant" ), QStringLiteral(
"1" ) );
426 efField.appendChild( efText );
427 node.appendChild( efField );
429 QDomElement efiField = doc.createElement( QStringLiteral(
"editforminit" ) );
431 efiField.appendChild( doc.createTextNode(
initFunction() ) );
432 node.appendChild( efiField );
434 QDomElement eficsField = doc.createElement( QStringLiteral(
"editforminitcodesource" ) );
435 eficsField.appendChild( doc.createTextNode( QString::number(
initCodeSource() ) ) );
436 node.appendChild( eficsField );
438 QDomElement efifpField = doc.createElement( QStringLiteral(
"editforminitfilepath" ) );
440 node.appendChild( efifpField );
442 QDomElement eficField = doc.createElement( QStringLiteral(
"editforminitcode" ) );
443 eficField.appendChild( doc.createCDATASection(
initCode() ) );
444 node.appendChild( eficField );
446 QDomElement fFSuppElem = doc.createElement( QStringLiteral(
"featformsuppress" ) );
447 QDomText fFSuppText = doc.createTextNode( QString::number(
suppress() ) );
448 fFSuppElem.appendChild( fFSuppText );
449 node.appendChild( fFSuppElem );
452 QDomElement editorLayoutElem = doc.createElement( QStringLiteral(
"editorlayout" ) );
456 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"uifilelayout" ) ) );
460 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"tablayout" ) ) );
465 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"generatedlayout" ) ) );
469 node.appendChild( editorLayoutElem );
472 if ( !
tabs().empty() && d->mConfiguredRootContainer )
474 QDomElement tabsElem = doc.createElement( QStringLiteral(
"attributeEditorForm" ) );
476 QDomElement rootElem = d->mInvisibleRootContainer->toDomElement( doc );
477 QDomNodeList elemList = rootElem.childNodes();
479 while ( !elemList.isEmpty() )
481 tabsElem.appendChild( elemList.at( 0 ) );
484 node.appendChild( tabsElem );
487 QDomElement editableElem = doc.createElement( QStringLiteral(
"editable" ) );
488 for (
auto editIt = d->mFieldEditables.constBegin(); editIt != d->mFieldEditables.constEnd(); ++editIt )
490 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
491 fieldElem.setAttribute( QStringLiteral(
"name" ), editIt.key() );
492 fieldElem.setAttribute( QStringLiteral(
"editable" ), editIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
493 editableElem.appendChild( fieldElem );
495 node.appendChild( editableElem );
497 QDomElement labelOnTopElem = doc.createElement( QStringLiteral(
"labelOnTop" ) );
498 for (
auto labelOnTopIt = d->mLabelOnTop.constBegin(); labelOnTopIt != d->mLabelOnTop.constEnd(); ++labelOnTopIt )
500 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
501 fieldElem.setAttribute( QStringLiteral(
"name" ), labelOnTopIt.key() );
502 fieldElem.setAttribute( QStringLiteral(
"labelOnTop" ), labelOnTopIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
503 labelOnTopElem.appendChild( fieldElem );
505 node.appendChild( labelOnTopElem );
507 QDomElement widgetsElem = doc.createElement( QStringLiteral(
"widgets" ) );
509 QMap<QString, QVariantMap >::ConstIterator configIt( d->mWidgetConfigs.constBegin() );
511 while ( configIt != d->mWidgetConfigs.constEnd() )
513 QDomElement widgetElem = doc.createElement( QStringLiteral(
"widget" ) );
514 widgetElem.setAttribute( QStringLiteral(
"name" ), configIt.key() );
518 configElem.setTagName( QStringLiteral(
"config" ) );
519 widgetElem.appendChild( configElem );
520 widgetsElem.appendChild( widgetElem );
524 node.appendChild( widgetsElem );
533 if ( elem.tagName() == QLatin1String(
"attributeEditorContainer" ) )
535 QColor backgroundColor( elem.attribute( QStringLiteral(
"backgroundColor" ), QString() ) );
537 elem.attribute( QStringLiteral(
"name" ) ) ), parent, backgroundColor );
539 int cc = elem.attribute( QStringLiteral(
"columnCount" ) ).toInt( &ok );
542 container->setColumnCount( cc );
544 bool isGroupBox = elem.attribute( QStringLiteral(
"groupBox" ) ).toInt( &ok );
546 container->setIsGroupBox( isGroupBox );
548 container->setIsGroupBox( parent );
550 bool visibilityExpressionEnabled = elem.attribute( QStringLiteral(
"visibilityExpressionEnabled" ) ).toInt( &ok );
554 visibilityExpression.
setEnabled( visibilityExpressionEnabled );
555 visibilityExpression.
setData(
QgsExpression( elem.attribute( QStringLiteral(
"visibilityExpression" ) ) ) );
557 container->setVisibilityExpression( visibilityExpression );
559 QDomNodeList childNodeList = elem.childNodes();
561 for (
int i = 0; i < childNodeList.size(); i++ )
563 QDomElement childElem = childNodeList.at( i ).toElement();
566 container->addChildElement( myElem );
569 newElement = container;
571 else if ( elem.tagName() == QLatin1String(
"attributeEditorField" ) )
573 QString name = elem.attribute( QStringLiteral(
"name" ) );
574 int idx = d->mFields.lookupField( name );
577 else if ( elem.tagName() == QLatin1String(
"attributeEditorRelation" ) )
582 relElement->
setShowLinkButton( elem.attribute( QStringLiteral(
"showLinkButton" ), QStringLiteral(
"1" ) ).toInt() );
583 relElement->
setShowUnlinkButton( elem.attribute( QStringLiteral(
"showUnlinkButton" ), QStringLiteral(
"1" ) ).toInt() );
584 newElement = relElement;
586 else if ( elem.tagName() == QLatin1String(
"attributeEditorQmlElement" ) )
590 newElement = qmlElement;
592 else if ( elem.tagName() == QLatin1String(
"attributeEditorHtmlElement" ) )
596 newElement = htmlElement;
601 if ( elem.hasAttribute( QStringLiteral(
"showLabel" ) ) )
602 newElement->
setShowLabel( elem.attribute( QStringLiteral(
"showLabel" ) ).toInt() );
617 mColumnCount = columnCount;
624 const auto childElements = children();
630 element->mIsGroupBox = mIsGroupBox;
631 element->mColumnCount = mColumnCount;
632 element->mVisibilityExpression = mVisibilityExpression;
637 void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem )
const 639 elem.setAttribute( QStringLiteral(
"columnCount" ), mColumnCount );
640 elem.setAttribute( QStringLiteral(
"groupBox" ), mIsGroupBox ? 1 : 0 );
641 elem.setAttribute( QStringLiteral(
"visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
642 elem.setAttribute( QStringLiteral(
"visibilityExpression" ), mVisibilityExpression->expression() );
643 if ( mBackgroundColor.isValid() )
644 elem.setAttribute( QStringLiteral(
"backgroundColor" ), mBackgroundColor.name( ) );
645 const auto constMChildren = mChildren;
648 QDomDocument doc = elem.ownerDocument();
649 elem.appendChild( child->toDomElement( doc ) );
653 QString QgsAttributeEditorContainer::typeIdentifier()
const 655 return QStringLiteral(
"attributeEditorContainer" );
Class for parsing and evaluation of expressions (formerly called "search strings").
The class is used as a container of context for various read/write operations on other objects...
An attribute editor widget that will represent arbitrary QML code.
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.
static QgsApplication * instance()
Returns the singleton instance of the QgsApplication.
MAYBE_UNUSED NODISCARD QgsReadWriteContextCategoryPopper enterCategory(const QString &category, const QString &details=QString())
Push a category to the stack.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
~QgsAttributeEditorContainer() override
The download will start immediately, not need to run QgsFecthedContent::download() ...
Container of fields for a vector layer.
This element will load a field's widget onto the form.
Allows entering a context category and takes care of leaving this category on deletion of the class...
This element will load a relation editor onto the form.
void setColumnCount(int columnCount)
Set the number of columns in this group.
An attribute editor widget that will represent arbitrary HTML code.
const QgsProjectTranslator * projectTranslator() const
Returns the project translator.
void setQmlCode(const QString &qmlCode)
Sets the QML code that will be represented within this widget to qmlCode.
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.
void setShowLabel(bool showLabel)
Controls if this element should be labeled with a title (field, relation or groupname).
virtual QString translate(const QString &context, const QString &sourceText, const char *disambiguation=nullptr, int n=-1) const =0
The derived translate() translates with QTranslator and qm file the sourceText.
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.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
void setHtmlCode(const QString &htmlCode)
Sets the HTML code that will be represented within this widget to htmlCode.
int columnCount() const
Gets the number of columns in this group.
static QgsProject * instance()
Returns the QgsProject singleton instance.
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...
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
const QgsFetchedContent * fetch(const QString &url, FetchingMode fetchingMode=DownloadLater)
Initialize a download for the given URL.
virtual void addChildElement(QgsAttributeEditorElement *element)
Add a child element to this container.
void setEnabled(bool enabled)
Set if this optional is enabled.
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.