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

Add a button to synchronize the QGIS plugin with the QGIS project #1687

Open
Huite opened this issue Aug 2, 2024 · 1 comment
Open

Add a button to synchronize the QGIS plugin with the QGIS project #1687

Huite opened this issue Aug 2, 2024 · 1 comment
Labels
enhancement New feature or request QGIS Ribasim QGIS plugin

Comments

@Huite
Copy link
Contributor

Huite commented Aug 2, 2024

Problem

To illustrate:

Open QGIS and the Ribasim plugin.
Click New and create a new model called test.toml
test is added as a group in the Layers panel
Save the project as test-ribasim.qgz
Close QGIS

Reopen QGIS and the Ribasim plugin
Go to Project > Open project, open test-ribasim.qgz
Note that test is still in the Layers panel
Note that the QGIS plugin is empty (it's not synchronized with the project)

To get the plugin up and running again:
click open and open test.toml
Note that are now two "test" layer groups in the Layers Panel

Solution

There's no trivially simple solution to this. Ideally, you would open a project, the plugin recognizes the Layers panel group, recognizes the layers; re-associates with the layers, and re-adds layers from the geopackage that have been (accidentally) deleted by the user prior to saving the QGIS project.
I've tried implementing this before, and I created a lot of complexity. What I decided on instead is to register the input group and the model path in the QGS project instead, and add a "Restore" button which uses those entries to re-create the input group with all Layers. This isn't fancy, but it's simple, robust, and does what you want >95% of the time.

  • Add a "Restore" button (better name might be available). Add it next to the open / new buttons:
    image
  • Whenever a geopackage is loaded, write two custom entries into the project file:
        self.parent.qgs_project.writeEntry("ribasim", "toml_path", self.path)
        self.parent.qgs_project.writeEntry("ribasim", "input_group", input_group)
  • Create a restore_plugin_from_project method:
    def restore_plugin_from_project(self) -> None:
        qgs_project = QgsProject.instance()
        toml_path, success = qgs_project.readEntry("ribasim", "toml_path")
        if not success:
            self.parent.message_bar.pushMessage(
                title="Error",
                text="Could not find a Ribasim TOML in this QGIS Project.",
                level=Qgis.Critical,
            )
            return

        if not Path(toml_path).exists():
            self.parent.message_bar.pushMessage(
                title="Error",
                text=f"Ribasim TOML {toml_path} does not exist.",
                level=Qgis.Critical,
            )

        input_group_name, _ = qgs_project.readEntry("ribasim", "input_group")

        reply = QMessageBox.question(
            self,
            "Restore Model from Project",
            f"Re-create Layers Panel group: {input_group_name}?",
            QMessageBox.Yes | QMessageBox.No,
            QMessageBox.No,
        )
        if reply == QMessageBox.No:
            return

        self.open_model_from_toml(toml_path)
        return
  • Modify existing open_model method:
    def open_model_from_toml(self, path) -> None:
        """Open a Ribasim model file."""
        self.dataset_line_edit.setText(path)
        self.load_geopackage()
        self.ribasim_widget.toggle_node_buttons(True)
        self.refresh_results()
        self.dataset_tree.sortByColumn(0, Qt.SortOrder.AscendingOrder)

def open_model(self) -> None:
        """Open a Ribasim model file."""
        self.dataset_tree.clear()
        path, _ = QFileDialog.getOpenFileName(self, "Select file", "", "*.toml")
        if path != "":  # Empty string in case of cancel button press
             self.open_model_from_toml(path)
  • Connect restore_plugin_from_project with the Restore button.

Improved functionality

After implementing this:

Reopen QGIS and the Ribasim plugin
Go to Project > Open project, open test-ribasim.qgz
Note that test is still in the Layers panel
Note that the QGIS plugin is empty (it's not synchronized with the project)

To get the plugin up and running again:
Click restore
Click Yes to recreate the input group in the layers panel

It doesn't save a lot of clicks, but somehow it's far less frustrating.

Weaknesses

Uh, the name "Restore" is probably to general? Synchronize could work too? But it's not specific enough. If we add the buttons to a new row (like the example above), we could probably spell it out? i.e. "Synchronize with project"?

A remaining weakness of this method is that this will not keep non-default styling of input layers, since it recreates the layers fully. This is another argument for storing styling information in the geopackage instead of relying on the default layer_styling provided by the QGIS plugin.

The problem with checking for the existence of layers is that all kinds of thing may have changed in them. A user can freely change the name in the Layers Panel (since it's just cosmetic). Layers may have been moved to other Layers groups entirely. The group may have been deleted entirely. It's all doable, but I don't think it's worth the hassle.

Some other thoughts:

  • The input group in the Layers panel is clearly associated with Ribasim. And if you work in a structured way, you probably want to keep everything grouped there -- i.e. if a layer was moved out of the group, it was probably a mistake while dragging layers.
  • The default layer styling should be good enough -- if people feel the need to change it, we should provide better defaults.
  • If you worked very hard on custom styling though, you are warned with the prompt. You can rename the group if you so like.
@github-project-automation github-project-automation bot moved this to To do in Ribasim Aug 2, 2024
@Huite Huite added QGIS Ribasim QGIS plugin enhancement New feature or request labels Aug 2, 2024
@visr
Copy link
Member

visr commented Aug 5, 2024

This is another argument for storing styling information in the geopackage

We plan to do this soon: #610

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request QGIS Ribasim QGIS plugin
Projects
Status: To do
Development

No branches or pull requests

2 participants