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

Refactored validated input widgets to be more extensible #42

Merged
merged 3 commits into from
Oct 14, 2024

Conversation

alexhroom
Copy link
Collaborator

@alexhroom alexhroom commented Oct 4, 2024

This PR refactors validated input widgets to a more object-oriented system that makes them easier to extend and change. Fixes #41.

To add a new input widget, the user should create a subclass of BaseInputWidget which has one method and three class attributes defined:

class MyInputWidget(BaseInputWidget):
    """My special input widget."""
    
    data_getter: str = "myGetter"  # data getter method for the editor
    data_setter: str = "mySetter"  # data setter method for the editor
    edit_signal: str = "editedSignal"  # method for signal editor produces when changed

    def create_editor(self, field_info: FieldInfo) -> QtWidgets.QWidget:
        """Method that creates the editor widget from Pydantic field info."""
        # whatever goes here...

and then register the widget and its relevant type in the class_widgets dictionary in a new get_validated_input function. This will produce an input widget which has methods editor for its editor, get_data for its data getter, set_data for its data setter and edited_signal for its change signal as before.

Accessing the validated input widgets is almost the same: we just change widget = ValidatedInputWidget(field_info) to widget = get_validated_input(field_info).

This means that the creation of editors is more encapsulated for each type, rather than the previous system of "just get the type of widget from the data type and then use this big if/elif/elif... block to sort out the relevant validation"

@alexhroom alexhroom requested a review from StephenNneji October 4, 2024 08:36
@alexhroom alexhroom force-pushed the 41-better-validated-input branch from 06eac91 to 657a2b9 Compare October 4, 2024 12:26
@alexhroom alexhroom force-pushed the 41-better-validated-input branch from 657a2b9 to 4da2160 Compare October 10, 2024 08:47
Copy link
Contributor

@StephenNneji StephenNneji left a comment

Choose a reason for hiding this comment

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

Looks good to me, please see comment

Enum: EnumInputWidget,
}

for type, widget in class_widgets.items():
Copy link
Contributor

Choose a reason for hiding this comment

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

The names type and input shadow built-in globals while I don't think anyone will every use input in this project, I advise changing these names to avoid conflict or bugs in future. Might be good to add this to our linter if its easy to do

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good spot - i've added the flake8-naming ruleset to the linting and fixed a couple other places where it was broken.

@alexhroom alexhroom merged commit 86afb6e into RascalSoftware:main Oct 14, 2024
5 checks passed
@alexhroom alexhroom deleted the 41-better-validated-input branch October 14, 2024 12:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make ValidatedInputWidget more flexible
2 participants