jeudi 16 juillet 2020

Design pattern to expose class members to GUI

I'm working on developer tools for a C++ project. My goal is to create a basic "editor interface" that allows objects to expose their members to the GUI for editing. Below I provided the basic "user story" and an example:

  1. An object is defined, and some of its members are selected to be exposed to the GUI
  2. The GUI has implementation for creating components based on the type of the data member. For example, numeric members (int, float, etc.) would create a line edit that displays the current value and allows the user to modify it.
  3. When an object is opened for editing, it provides a list of its members (optionally with additional info, e.g tooltips), which the GUI converts into controls as described in 2. Each time a control is modified, the object's member is updated with the new value.
  4. The GUI is agnostic to the back-end objects linked to it. If the object's definition changes, it simply sends a different list of members to the GUI, which generates different UI components as a result.

As an example, suppose I have the below objects, where all of their members are meant to be editable in the GUI:

struct A
{
    int m_int;
    float m_float;
    std::string m_string;
};

struct B
{
    A m_a1;
    A m_a2;
};

In the GUI code, int float and std::string would be defined to generate a simple line edit that shows the current value and allows the user to type in a new value. For A, the resulting dialog would have three such controls, one for each member of A. Meanwhile, I could add A itself to the GUI code, displaying it as a composite control (made up of multiple line edits), which would allow B to expose its members directly.

Is there a C++ design pattern that offers a simple and elegant solution to this? A method that cleanly separates the GUI and back-end implementation? I can think of a naive approach using OOP, with a subclass for each supported type, but that seems like it would just get very ugly very quickly.

Aucun commentaire:

Enregistrer un commentaire