29 qDeleteAll( mChildren );
33 : d( new QgsEditFormConfigPrivate() )
39 int fieldIndex = d->mFields.indexOf( widgetName );
40 if ( fieldIndex != -1 )
41 return d->mFields.at( fieldIndex ).editorWidgetSetup().config();
43 return d->mWidgetConfigs.value( widgetName );
46 void QgsEditFormConfig::setFields(
const QgsFields &fields )
51 if ( !d->mConfiguredRootContainer )
53 d->mInvisibleRootContainer->clear();
54 for (
int i = 0; i < d->mFields.size(); ++i )
57 d->mInvisibleRootContainer->addChildElement( field );
62 void QgsEditFormConfig::onRelationsLoaded()
78 if ( d->mFields.indexOf( widgetName ) != -1 )
80 QgsDebugMsg(
"Trying to set a widget config for a field on QgsEditFormConfig. Use layer->setEditorWidgetSetup() instead." );
85 d->mWidgetConfigs[widgetName] = config;
92 return d->mWidgetConfigs.remove( widgetName ) != 0;
117 d->mInvisibleRootContainer->addChildElement( data );
122 return d->mInvisibleRootContainer->children();
128 d->mInvisibleRootContainer->clear();
133 return d->mInvisibleRootContainer;
138 return d->mEditorLayout;
144 d->mEditorLayout = editorLayout;
147 d->mConfiguredRootContainer =
true;
152 return d->mUiFormPath;
157 if ( !ui.isEmpty() && !QUrl::fromUserInput( ui ).isLocalFile() )
176 if ( idx >= 0 && idx < d->mFields.count() )
181 return !d->mFieldEditables.value( d->mFields.at( idx ).name(), true );
189 if ( idx >= 0 && idx < d->mFields.count() )
190 return d->mLabelOnTop.value( d->mFields.at( idx ).name(), false );
197 if ( idx >= 0 && idx < d->mFields.count() )
200 d->mFieldEditables[ d->mFields.at( idx ).name()] = !
readOnly;
206 if ( idx >= 0 && idx < d->mFields.count() )
209 d->mLabelOnTop[ d->mFields.at( idx ).name()] = onTop;
215 return d->mInitFunction;
221 d->mInitFunction =
function;
237 return d->mInitFilePath;
243 d->mInitFilePath = filePath;
248 return d->mInitCodeSource;
259 return d->mSuppressForm;
265 d->mSuppressForm = s;
274 QDomNode editFormNode = node.namedItem( QStringLiteral(
"editform" ) );
275 if ( !editFormNode.isNull() )
277 QDomElement e = editFormNode.toElement();
281 QDomNode editFormInitNode = node.namedItem( QStringLiteral(
"editforminit" ) );
282 if ( !editFormInitNode.isNull() )
284 d->mInitFunction = editFormInitNode.toElement().text();
287 QDomNode editFormInitCodeSourceNode = node.namedItem( QStringLiteral(
"editforminitcodesource" ) );
288 if ( !editFormInitCodeSourceNode.isNull() && !editFormInitCodeSourceNode.toElement().text().isEmpty() )
290 setInitCodeSource( static_cast< QgsEditFormConfig::PythonInitCodeSource >( editFormInitCodeSourceNode.toElement().text().toInt() ) );
293 QDomNode editFormInitCodeNode = node.namedItem( QStringLiteral(
"editforminitcode" ) );
294 if ( !editFormInitCodeNode.isNull() )
296 setInitCode( editFormInitCodeNode.toElement().text() );
304 int dotPos = d->mInitFunction.lastIndexOf(
'.' );
308 setInitCode( QStringLiteral(
"from %1 import %2\n" ).arg( d->mInitFunction.left( dotPos ), d->mInitFunction.mid( dotPos + 1 ) ) );
312 QDomNode editFormInitFilePathNode = node.namedItem( QStringLiteral(
"editforminitfilepath" ) );
313 if ( !editFormInitFilePathNode.isNull() && !editFormInitFilePathNode.toElement().text().isEmpty() )
318 QDomNode fFSuppNode = node.namedItem( QStringLiteral(
"featformsuppress" ) );
319 if ( fFSuppNode.isNull() )
325 QDomElement e = fFSuppNode.toElement();
330 QDomNode editorLayoutNode = node.namedItem( QStringLiteral(
"editorlayout" ) );
331 if ( editorLayoutNode.isNull() )
337 if ( editorLayoutNode.toElement().text() == QLatin1String(
"uifilelayout" ) )
341 else if ( editorLayoutNode.toElement().text() == QLatin1String(
"tablayout" ) )
351 d->mFieldEditables.clear();
352 QDomNodeList editableNodeList = node.namedItem( QStringLiteral(
"editable" ) ).toElement().childNodes();
353 for (
int i = 0; i < editableNodeList.size(); ++i )
355 QDomElement editableElement = editableNodeList.at( i ).toElement();
356 d->mFieldEditables.insert( editableElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( editableElement.attribute( QStringLiteral(
"editable" ) ).toInt() ) );
359 d->mLabelOnTop.clear();
360 QDomNodeList labelOnTopNodeList = node.namedItem( QStringLiteral(
"labelOnTop" ) ).toElement().childNodes();
361 for (
int i = 0; i < labelOnTopNodeList.size(); ++i )
363 QDomElement labelOnTopElement = labelOnTopNodeList.at( i ).toElement();
364 d->mLabelOnTop.insert( labelOnTopElement.attribute( QStringLiteral(
"name" ) ),
static_cast< bool >( labelOnTopElement.attribute( QStringLiteral(
"labelOnTop" ) ).toInt() ) );
367 QDomNodeList widgetsNodeList = node.namedItem( QStringLiteral(
"widgets" ) ).toElement().childNodes();
369 for (
int i = 0; i < widgetsNodeList.size(); ++i )
371 QDomElement widgetElement = widgetsNodeList.at( i ).toElement();
374 d->mWidgetConfigs[widgetElement.attribute( QStringLiteral(
"name" ) )] = config.toMap();
378 QDomNode attributeEditorFormNode = node.namedItem( QStringLiteral(
"attributeEditorForm" ) );
379 if ( !attributeEditorFormNode.isNull() )
381 QDomNodeList attributeEditorFormNodeList = attributeEditorFormNode.toElement().childNodes();
383 if ( attributeEditorFormNodeList.size() )
385 d->mConfiguredRootContainer =
true;
388 for (
int i = 0; i < attributeEditorFormNodeList.size(); i++ )
390 QDomElement elem = attributeEditorFormNodeList.at( i ).toElement();
393 addTab( attributeEditorWidget );
403 QDomDocument doc( node.ownerDocument() );
405 QDomElement efField = doc.createElement( QStringLiteral(
"editform" ) );
407 efField.appendChild( efText );
408 node.appendChild( efField );
410 QDomElement efiField = doc.createElement( QStringLiteral(
"editforminit" ) );
412 efiField.appendChild( doc.createTextNode(
initFunction() ) );
413 node.appendChild( efiField );
415 QDomElement eficsField = doc.createElement( QStringLiteral(
"editforminitcodesource" ) );
416 eficsField.appendChild( doc.createTextNode( QString::number(
initCodeSource() ) ) );
417 node.appendChild( eficsField );
419 QDomElement efifpField = doc.createElement( QStringLiteral(
"editforminitfilepath" ) );
421 node.appendChild( efifpField );
423 QDomElement eficField = doc.createElement( QStringLiteral(
"editforminitcode" ) );
424 eficField.appendChild( doc.createCDATASection(
initCode() ) );
425 node.appendChild( eficField );
427 QDomElement fFSuppElem = doc.createElement( QStringLiteral(
"featformsuppress" ) );
428 QDomText fFSuppText = doc.createTextNode( QString::number(
suppress() ) );
429 fFSuppElem.appendChild( fFSuppText );
430 node.appendChild( fFSuppElem );
433 QDomElement editorLayoutElem = doc.createElement( QStringLiteral(
"editorlayout" ) );
437 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"uifilelayout" ) ) );
441 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"tablayout" ) ) );
446 editorLayoutElem.appendChild( doc.createTextNode( QStringLiteral(
"generatedlayout" ) ) );
450 node.appendChild( editorLayoutElem );
453 if ( !
tabs().empty() && d->mConfiguredRootContainer )
455 QDomElement tabsElem = doc.createElement( QStringLiteral(
"attributeEditorForm" ) );
457 QDomElement rootElem = d->mInvisibleRootContainer->toDomElement( doc );
458 QDomNodeList elemList = rootElem.childNodes();
460 while ( !elemList.isEmpty() )
462 tabsElem.appendChild( elemList.at( 0 ) );
465 node.appendChild( tabsElem );
468 QDomElement editableElem = doc.createElement( QStringLiteral(
"editable" ) );
469 for (
auto editIt = d->mFieldEditables.constBegin(); editIt != d->mFieldEditables.constEnd(); ++editIt )
471 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
472 fieldElem.setAttribute( QStringLiteral(
"name" ), editIt.key() );
473 fieldElem.setAttribute( QStringLiteral(
"editable" ), editIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
474 editableElem.appendChild( fieldElem );
476 node.appendChild( editableElem );
478 QDomElement labelOnTopElem = doc.createElement( QStringLiteral(
"labelOnTop" ) );
479 for (
auto labelOnTopIt = d->mLabelOnTop.constBegin(); labelOnTopIt != d->mLabelOnTop.constEnd(); ++labelOnTopIt )
481 QDomElement fieldElem = doc.createElement( QStringLiteral(
"field" ) );
482 fieldElem.setAttribute( QStringLiteral(
"name" ), labelOnTopIt.key() );
483 fieldElem.setAttribute( QStringLiteral(
"labelOnTop" ), labelOnTopIt.value() ? QStringLiteral(
"1" ) : QStringLiteral(
"0" ) );
484 labelOnTopElem.appendChild( fieldElem );
486 node.appendChild( labelOnTopElem );
488 QDomElement widgetsElem = doc.createElement( QStringLiteral(
"widgets" ) );
490 QMap<QString, QVariantMap >::ConstIterator configIt( d->mWidgetConfigs.constBegin() );
492 while ( configIt != d->mWidgetConfigs.constEnd() )
494 QDomElement widgetElem = doc.createElement( QStringLiteral(
"widget" ) );
495 widgetElem.setAttribute( QStringLiteral(
"name" ), configIt.key() );
499 configElem.setTagName( QStringLiteral(
"config" ) );
500 widgetElem.appendChild( configElem );
501 widgetsElem.appendChild( widgetElem );
505 node.appendChild( widgetsElem );
514 if ( elem.tagName() == QLatin1String(
"attributeEditorContainer" ) )
518 int cc = elem.attribute( QStringLiteral(
"columnCount" ) ).toInt( &ok );
523 bool isGroupBox = elem.attribute( QStringLiteral(
"groupBox" ) ).toInt( &ok );
529 bool visibilityExpressionEnabled = elem.attribute( QStringLiteral(
"visibilityExpressionEnabled" ) ).toInt( &ok );
533 visibilityExpression.
setEnabled( visibilityExpressionEnabled );
534 visibilityExpression.
setData(
QgsExpression( elem.attribute( QStringLiteral(
"visibilityExpression" ) ) ) );
538 QDomNodeList childNodeList = elem.childNodes();
540 for (
int i = 0; i < childNodeList.size(); i++ )
542 QDomElement childElem = childNodeList.at( i ).toElement();
548 newElement = container;
550 else if ( elem.tagName() == QLatin1String(
"attributeEditorField" ) )
552 QString name = elem.attribute( QStringLiteral(
"name" ) );
553 int idx = d->mFields.lookupField( name );
556 else if ( elem.tagName() == QLatin1String(
"attributeEditorRelation" ) )
561 relElement->
setShowLinkButton( elem.attribute( QStringLiteral(
"showLinkButton" ), QStringLiteral(
"1" ) ).toInt() );
562 relElement->
setShowUnlinkButton( elem.attribute( QStringLiteral(
"showUnlinkButton" ), QStringLiteral(
"1" ) ).toInt() );
563 newElement = relElement;
568 if ( elem.hasAttribute( QStringLiteral(
"showLabel" ) ) )
569 newElement->
setShowLabel( elem.attribute( QStringLiteral(
"showLabel" ) ).toInt() );
584 mColumnCount = columnCount;
595 element->mIsGroupBox = mIsGroupBox;
596 element->mColumnCount = mColumnCount;
597 element->mVisibilityExpression = mVisibilityExpression;
602 void QgsAttributeEditorContainer::saveConfiguration( QDomElement &elem )
const 604 elem.setAttribute( QStringLiteral(
"columnCount" ), mColumnCount );
605 elem.setAttribute( QStringLiteral(
"groupBox" ), mIsGroupBox ? 1 : 0 );
606 elem.setAttribute( QStringLiteral(
"visibilityExpressionEnabled" ), mVisibilityExpression.enabled() ? 1 : 0 );
607 elem.setAttribute( QStringLiteral(
"visibilityExpression" ), mVisibilityExpression->expression() );
611 QDomDocument doc = elem.ownerDocument();
616 QString QgsAttributeEditorContainer::typeIdentifier()
const 618 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...
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.
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
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...
QDomElement toDomElement(QDomDocument &doc) const
Gets 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.
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.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.
virtual void setIsGroupBox(bool isGroupBox)
Determines if this container is rendered as collapsible group box or tab in a tabwidget.
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.
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.