Skip to content
Permalink
Browse files
Load the default relation editor if the relation editor type setting …
…is not set (#43591)

* Load the default relation editor if the relation editor type setting is missing

* Add setDefaultWidgetType and defaultWidgetType to the relation editor registry

* Use the new defaultWidgetType() when relation editor type setting is missing
  • Loading branch information
suricactus committed Jun 7, 2021
1 parent 106fe03 commit cd6e0cebe0d980cc5570b6820c44216a46acb3f7
@@ -70,6 +70,23 @@ Creates a configuration widget
:param parent: The parent widget for the created widget
%End


void setDefaultWidgetType( const QString &widgetType );
%Docstring
Sets the default editor widget type. Does nothing if the provided widget type is not present.

:param widgetType: The widget type to be used by default.

.. versionadded:: 3.20
%End

QString defaultWidgetType() const;
%Docstring
Returns the default editor widget type.

.. versionadded:: 3.20
%End

};

/************************************************************************
@@ -126,7 +126,9 @@ void QgsAttributeWidgetRelationEditWidget::setRelationEditorConfiguration( const
}

int widgetTypeIdx = mWidgetTypeComboBox->findData( config.mRelationWidgetType );
mWidgetTypeComboBox->setCurrentIndex( widgetTypeIdx >= 0 ? widgetTypeIdx : 0 );
mWidgetTypeComboBox->setCurrentIndex( widgetTypeIdx >= 0
? widgetTypeIdx
: mWidgetTypeComboBox->findData( QgsGui::relationWidgetRegistry()->defaultWidgetType() ) );

const QString widgetType = mWidgetTypeComboBox->currentData().toString();
mConfigWidget = QgsGui::relationWidgetRegistry()->createConfigWidget( widgetType, relation, this );
@@ -20,7 +20,9 @@

QgsRelationWidgetRegistry::QgsRelationWidgetRegistry()
{
addRelationWidget( new QgsRelationEditorWidgetFactory() );
QgsRelationEditorWidgetFactory *factory = new QgsRelationEditorWidgetFactory();
addRelationWidget( factory );
setDefaultWidgetType( factory->type() );
}

QgsRelationWidgetRegistry::~QgsRelationWidgetRegistry()
@@ -42,8 +44,8 @@ void QgsRelationWidgetRegistry::addRelationWidget( QgsAbstractRelationEditorWidg

void QgsRelationWidgetRegistry::removeRelationWidget( const QString &widgetType )
{
// protect the relation editor widget from removing, so the user has at least one relation widget type
if ( dynamic_cast<QgsRelationEditorWidgetFactory *>( mRelationWidgetFactories.value( widgetType ) ) )
// protect the default relation editor widget from removing, so the user has at least one relation widget type
if ( widgetType == mDefaultWidgetType )
return;

mRelationWidgetFactories.remove( widgetType );
@@ -54,6 +56,19 @@ QStringList QgsRelationWidgetRegistry::relationWidgetNames()
return mRelationWidgetFactories.keys();
}

void QgsRelationWidgetRegistry::setDefaultWidgetType( const QString &defaultWidgetType )
{
if ( !mRelationWidgetFactories.contains( defaultWidgetType ) )
return;

mDefaultWidgetType = defaultWidgetType;
}

QString QgsRelationWidgetRegistry::defaultWidgetType() const
{
return mDefaultWidgetType;
}

QMap<QString, QgsAbstractRelationEditorWidgetFactory *> QgsRelationWidgetRegistry::factories() const
{
return mRelationWidgetFactories;
@@ -79,9 +79,25 @@ class GUI_EXPORT QgsRelationWidgetRegistry
*/
QgsAbstractRelationEditorConfigWidget *createConfigWidget( const QString &widgetType, const QgsRelation &relation, QWidget *parent = nullptr ) const SIP_TRANSFERBACK;


/**
* Sets the default editor widget type. Does nothing if the provided widget type is not present.
* \param widgetType The widget type to be used by default.
* \since QGIS 3.20
*/
void setDefaultWidgetType( const QString &widgetType );

/**
* Returns the default editor widget type.
* \since QGIS 3.20
*/
QString defaultWidgetType() const;

private:

QMap<QString, QgsAbstractRelationEditorWidgetFactory *> mRelationWidgetFactories;

QString mDefaultWidgetType;
};

#endif // QGSRELATIONWIDGETREGISTRY_H

0 comments on commit cd6e0ce

Please sign in to comment.