Jump to content
  • Advertisement
Sign in to follow this  
XTAL256

How to design specialised UI for subclasses

This topic is 2804 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am writing a C++ application where i have an object model and a UI (as most applications do). For the model, i have a main object and a number of specialised subclasses. The UI has context menus and property dialogs that i want to be specific to each object type. The main object has a number of actions that are available in the menu, but the subclasses need to add menu items (actions). And similarly for the property dialog - it needs to display data specific to each subclass.

My question is what is a good way to design this?

I can put all the menu creation code in each class, so that the specialised classes override the function to give their own menus. Like this (pseudo-code):

class MainObject {
virtual MenuList getMenus() {
return Menu("do something"),
Menu("do something else"),
Menu("do a third thing"),
}
};

class SpecialObjectA : public MainObject {
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu A");
}
};

class SpecialObjectB : public MainObject {
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu B");
}
};


But that seems like the model classes are doing too much. They shouldn't be responsible for creating and managing UI objects.

So maybe i could create a separate object hierarchy for creating the menus. Each class would be responsible for creating the menu for it's respective model object. Something like this:

class MainMenuController {
MainObject* theObject;
virtual MenuList getMenus() {
return Menu("do something"),
Menu("do something else"),
Menu("do a third thing"),
}
};

class SpecialMenuControllerA : public MainMenuController {
SpecialObjectA* theObject;
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu A");
}
};

class SpecialMenuControllerB : public MainMenuController {
SpecialObjectB* theObject;
virtual MenuList getMenus() {
return MainObject::getMenus() + Menu("special menu B");
}
};


class MainObject {
virtual MenuList getMenus() {
return new MainMenuController()->getMenus();
}
};

class SpecialObjectA : public MainObject {
virtual MenuList getMenus() {
return new SpecialMenuControllerA()->getMenus();
}
};

class SpecialObjectB : public MainObject {
virtual MenuList getMenus() {
return new SpecialMenuControllerB()->getMenus();
}
};


But then i'm not sure where the logic for performing each menu action should go. Since each action operates on the model object, those MenuController classes above would need to know what object the action is for.

I am using Qt, so i could use their powerful signals and slots mechanism to do this, but i'm not quite sure how.
Does anyone have an idea of what Object Oriented design patterns to use here? I can provide more detail if my problem is unclear.

thanks

Share this post


Link to post
Share on other sites
Advertisement
Perhaps each class can provide a list of paired strings and functions (as map<string, function> or vector<pair<string, function>>), from which the UI will build the context menu.

Share this post


Link to post
Share on other sites
What about the property pages? It uses HTML to display the data in a table, and the code to build that table is currently being done in the UI class. But now that i want specifics for each subclass, where should this code go? Like the menu problem, the model classes shouldn't be responsible for creating data for the UI. But i need to utilise polymorphism so the dialog can be used for all model objects yet display specific data.

Share this post


Link to post
Share on other sites
[color=#BBBBBB]

The UI has context menus and property dialogs that i want to be specific to each object type. [/quote]
[color=#BBBBBB]



[color=#BBBBBB]

If you want to only have specific dialogs for each TYPE, then I might create some mapping in the UI of the type of object to its menu tree object (or whatever you're using), probably using Qt's equivalent to std::map. This mapping would exist in the UI and doesn't bother with instances of model objects - only their types. The mapping would essentially describe to the UI how to treat an object of a particular type. I would perhaps use a static member in each model class (or somewhere else logical) specifying which UI actions can be performed on it (perhaps a vector of strings?), which would be read by the UI and used to generate the menu data (by the UI). In this way, a model class only needs to know what can be done to it, and the UI worries about actually doing those things.
[color=#BBBBBB]



[color=#BBBBBB]

[color=#BBBBBB]

Does anyone have an idea of what Object Oriented design patterns to use here?[/quote]
[color=#BBBBBB]



[color="#BBBBBB"]

I would argue that thinking about this in terms of "what design pattern should I use?" isn't going to help you very much. Just solve the problem, think about what pattern you used later. Design patterns are descriptive, not prescriptive.

Share this post


Link to post
Share on other sites
Same approach: invent a GUI-independent data structure that can be returned from a polymorphic function and parsed by the GUI.

Share this post


Link to post
Share on other sites

The UI has context menus and property dialogs that i want to be specific to each object type.

If you want to only have specific dialogs for each TYPE, then I might create some mapping in the UI of the type of object to its menu tree object (or whatever you're using), probably using Qt's equivalent to std::map. This mapping would exist in the UI and doesn't bother with instances of model objects - only their types. The mapping would essentially describe to the UI how to treat an object of a particular type. I would perhaps use a static member in each model class (or somewhere else logical) specifying which UI actions can be performed on it (perhaps a vector of strings?), which would be read by the UI and used to generate the menu data (by the UI). In this way, a model class only needs to know what can be done to it, and the UI worries about actually doing those things.[/quote]
That is good, but how do i use an object's <i>type</i> in C++? It's use of RTTI is quite limited. Qt has more powerful extensions, but i'm not sure if that will do what i want. I can create different dialogs which map to a model type, but each one would then have to dynamic_cast the model object from it's super type since i keep a single list of these objects.


Does anyone have an idea of what Object Oriented design patterns to use here?

I would argue that thinking about this in terms of "what design pattern should I use?" isn't going to help you very much. Just solve the problem, think about what pattern you used later. Design patterns are descriptive, not prescriptive.[/quote]
True, but my mind right now is a big blob. I need to structure my thoughts and wrap my head around how i am going to do it. I thought design patterns might help with that, it would give me something to build on.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!