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

Data Model Interface for Qt #1902

Open
trisyoungs opened this issue May 22, 2024 · 2 comments · May be fixed by #1959
Open

Data Model Interface for Qt #1902

trisyoungs opened this issue May 22, 2024 · 2 comments · May be fixed by #1959
Assignees
Labels
Scope: Codebase Related to source code management, build etc. Type: Enhancement Enhancement for existing feature

Comments

@trisyoungs
Copy link
Member

Describe the feature / issue
There are many reasons to use models for managing the viewing and editing of our data in the Dissolve GUI, however there is something of a disconnect in the way we manage data between the CLI "core" code and the Qt GUI.

Can we implement a custom "model" which sits alongside, e.g., a std::vector<CustomData> and provides "row" addition and deletion, read/write access to data, much as a QtAbstractItemModel does, but without any of the Qt? A companion Qt class in the GUI could then be designed to directly use this non-Qt model to achieve the desired result in the GUI. Why bother doing this? I'm thinking this would allow:

  • Parts of the core "CLI" code to edit data and automatically inform Qt (via, e.g., our GUI registering lambda callbacks to "signals" in our custom model) and hence update the GUI without any manual calling of update functions
  • Implementing model/views for stuff in the GUI becomes coherent and simplified, and provides the reverse notification stream to the CLI core.
@trisyoungs trisyoungs added the Type: Enhancement Enhancement for existing feature label May 22, 2024
@trisyoungs
Copy link
Member Author

@rprospero Thoughts?

@rprospero
Copy link
Contributor

rprospero commented May 24, 2024

I've had a similar idea and I guess it's time to try and formalise a bit of the design. Or it's just time for me to vastly overengineer something.

We're likely thinking of the same design (i.e. a template class that takes a reference to a vector and can be passed callbacks for insertion, update, and delete). Here are the issues that I'm currently worried about.

  1. It's easy to wrap the insertion and removal commands, but modifying values is much trickier, especially since the callback needs to be called before and after modification. I see two ways of handling this
    a. For modifications, the model returns a tiny wrapper around the vector reference. It has all of the standard vector methods, so it can act as a drop in for our current methods. Additionally, the constructor calls the beginReset callback and the destructor calls the endReset callback to ensure that the GUI model gets updated after the modifications are finished
    b. We provide a method that calls a lambda on each element in the vector. The lambda returns an optional new value. If the value isn't empty, update the value and call the modification callback for that index. This is more awkward to use (e.g. must use lambda callbacks, can only look at one value at a time), but does have the advantage of only updating the parts of the model that actually changed.
  2. We can make a template class that implements QAbstractItemModel and connects into the model. However, our model wrapper can't provide any of the display. My thought there is to make two overloaded functions: QVariant asTableRow(&T value, int column, int role) and bool writeTableRow(QVariant value, &T old, int column, int role). There would be two additional template functions int tableColumns<T>() and QVariant tableColumnHeaders<T>(int column, int role). If we implement these overloads and specialise the template functions for the types, then it should be possible to drop in our template model class with no extra boiler plate.

@rprospero rprospero self-assigned this May 24, 2024
@trisyoungs trisyoungs added the Scope: Codebase Related to source code management, build etc. label Jul 10, 2024
@trisyoungs trisyoungs linked a pull request Jul 10, 2024 that will close this issue
30 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Scope: Codebase Related to source code management, build etc. Type: Enhancement Enhancement for existing feature
Projects
Status: Codebase
Development

Successfully merging a pull request may close this issue.

2 participants