Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple relation editors via relation widget registry #40721

Merged
merged 23 commits into from
Jan 8, 2021

Conversation

suricactus
Copy link
Contributor

@suricactus suricactus commented Dec 22, 2020

Description

With this change the current relation editor is no longer the only way to edit relations. Similarly to the way different field editors exist, one can develop and register their own widget, using the newly added classes:
QgsRelationWidget - the widget used in the attribute form
QgsRelationConfigWidget - the configuration widget shown in layer's attribute table configuration tab
QgsRelationWidgetFactory - a factory that creates the QgsRelationWidget and QgsRelationConfigWidget
QgsRelationWidgetRegistry - a registry where the factory is being registered.

The currently existing QgsRelationEditorWidget is deprecated and replaced with QgsBasicRelationEditorWidget, which is protected from accidentally being deleted and is used as fallback widget.

The most notable UI change allows the user to select the desired relation widget from the drag n drop form configuration:
image
Note the widget type and widget configuration UI.

Sample widget:

class QgsExampleRelationWidget(QgsRelationWidget):

    def __init__(self, config, parent):
        super().__init__(config, parent)

        self.setLayout(QGridLayout())
        self.label = QLabel()
        self.label.setText(f'According to the configuration, the checkboxin state was {str(config.get("checkboxin", "Unknown"))}')
        self.layout().addWidget(self.label)

    def config(self):
        return {

        }

    def setConfig(self, config):
        self.label.setText(f'According to the configuration, the checkboxin state was {str(config.get("checkboxin", "Unknown"))}')


class QgsExampleRelationConfigWidget(QgsRelationConfigWidget):

    def __init__(self, relation, parent):
        super().__init__(relation, parent)

        self.setLayout(QGridLayout())
        self.checkbox = QCheckBox('Is this checkbox checkboxin?')
        self.layout().addWidget(self.checkbox)

    def config(self):
        return {
            "checkboxin": self.checkbox.isChecked()
        }

    def setConfig(self, config):
        self.checkbox.setChecked(config.get('checkboxin', False))


class QgsExampleRelationWidgetFactory(QgsRelationWidgetFactory):
    def type(self):
        return "example"
    
    def name(self):
        return "Example Relation Widget"

    def create(self, config, parent):
        return QgsExampleRelationWidget( config, parent )

    def configWidget(self, relation, parent):
        return QgsExampleRelationConfigWidget( relation, parent )


registry = QgsGui.relationWidgetRegistry()
registry.addRelationWidget(QgsExampleRelationWidgetFactory())
widget = QgsGui.relationWidgetRegistry().create('example', {})
widgetConfig = QgsGui.relationWidgetRegistry().createConfigWidget('example', QgsRelation())

These changes are part of qgis/QGIS-Enhancement-Proposals#79 .

TODOs:

  • improve documentation
  • some basic unit tests
  • improve backwards project compatibitlity with QGIS 3.10 and 3.16

@github-actions github-actions bot added this to the 3.18.0 milestone Dec 22, 2020
@suricactus suricactus force-pushed the relregistry branch 2 times, most recently from 2c34643 to 51d920d Compare December 22, 2020 13:41
@3nids 3nids self-assigned this Dec 22, 2020
src/core/qgsattributeeditorelement.h Outdated Show resolved Hide resolved
src/core/qgsattributeeditorelement.h Outdated Show resolved Hide resolved
src/core/qgsattributeeditorelement.h Show resolved Hide resolved
src/core/qgsattributeeditorelement.h Show resolved Hide resolved
src/core/qgseditformconfig.cpp Outdated Show resolved Hide resolved
src/gui/editorwidgets/qgsrelationwidgetwrapper.h Outdated Show resolved Hide resolved
src/gui/editorwidgets/qgsrelationwidgetwrapper.h Outdated Show resolved Hide resolved
src/gui/qgsrelationwidget.h Outdated Show resolved Hide resolved
@DelazJ DelazJ added the Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo. label Dec 24, 2020
@qgis-bot
Copy link
Collaborator

@suricactus
This pull request has been tagged as requiring documentation.

A documentation ticket will be opened at https://github.com/qgis/QGIS-Documentation when this PR is merged.

Please update the description (not the comments) with helpful description and screenshot to help the work from documentors.
Also, any commit having [needs-doc] or [Needs Documentation] in will see its message pushed to the issue, so please be as verbose as you can.

Thank you!

Copy link
Member

@3nids 3nids left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!
Some remarks in the code.

The main remaining thing is to move away from using the deprecated methods, as @nyalldawson pointed out.

It might be nice to move all the files under src/gui/editorwidgets/relationeditor.

Regarding the naming of the classes:

  • QgsRelationEditorWidget has been renamed to QgsRelationWidget
    is this required? (but see after)
  • For the default widget, I am not a big fan of "basic". Shall we rather use default instead of basic?
  • For the 2 abstract classes, the convention (or habit) in QGIS is to either use Abstract or Base in the name. So QgsRelationEditorWidgetBase and QgsRelationEditorConfigWidgetBase ?

src/core/qgsattributeeditorelement.h Outdated Show resolved Hide resolved
src/core/qgseditformconfig.cpp Outdated Show resolved Hide resolved
src/gui/editorwidgets/qgsrelationwidgetwrapper.h Outdated Show resolved Hide resolved
src/gui/qgsattributeform.h Outdated Show resolved Hide resolved
src/gui/qgsbasicrelationwidget.cpp Outdated Show resolved Hide resolved
@3nids
Copy link
Member

3nids commented Jan 8, 2021

nice work!

@qgis-bot
Copy link
Collaborator

qgis-bot commented Jan 8, 2021

@suricactus
A documentation ticket has been opened at qgis/QGIS-Documentation#6435
It is your responsibility to visit this ticket and add as much detail as possible for the documentation team to correctly document this change.
Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Documentation When merging a labeled PR, an issue will be created in the Doc repo.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants