Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to design specialised UI for subclasses

Started by XTAL256 Jan 14, 2011 at 10:55 PM 4 replies 1.9k views
Original Post
XTAL256
XTAL256
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
[Window Detective] - Windows UI spy utility for programmers
XTAL256
XTAL256
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.
[Window Detective] - Windows UI spy utility for programmers
Oberon_Command
Oberon_Command
[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.

XTAL256
XTAL256

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 type 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.
[Window Detective] - Windows UI spy utility for programmers

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.