PDA

View Full Version : How do I design this code?



lni
24th April 2012, 22:14
I have been using MVC approach for many years, and now users have a new request that I have to provide.

Let's say the users want to visualize a house that has a kitchen, several bed rooms, a guest room, and a garage. users want to create several views that can look at different parts of the house (see attachment draft). Each view is an MDI subwindow, which can be activated one at a time. The left panel is a tree widget that lists the house and its components.

When View 2 is activated, the items selected or unselected in the left panel will apply only to view 2. When view 1 is activated (by clicking on the view 1's title bar), the left panel will immediately show the selection state of the view 1, and then the items selected or unselected in the left panel will apply only to view 1, etc.

What is the best approach to design this project? Obviously, it has a Data Model, several View Models, and several Views. But how about the controller? Or should I use other design pattern?

Model-view-viewModel (MVVM) doesn't seem to work either...

Any suggestion is very welcome!

7621

qlands
25th April 2012, 07:52
Hi,

More information is needed, however I put some ideas in your image. Hope it helps.

Carlos.

7624

d_stranz
26th April 2012, 19:55
When View 2 is activated, the items selected or unselected in the left panel will apply only to view 2. When view 1 is activated (by clicking on the view 1's title bar), the left panel will immediately show the selection state of the view 1, and then the items selected or unselected in the left panel will apply only to view 1, etc.

It isn't clear from your description what the "views" contain. Does each "view" correspond to a single room of the house, or does each "view" show the entire house, but with only the selections that are active for that view? Do these views need to be interactive? (That is, can the user click on things in the view and move them or see details about them?)

Contrary to the other reply, I would implement the views as QGraphicsView, and map between your data model and QGraphicsScene instances. Selections from the tree would control the visibility status of the graphics items in a particular view.

lni
27th April 2012, 01:04
Thank you all for suggestions.

Let me describe it in more detail. Let's say "View 2" is currently the active view, and looking at Bed Room 1 and Bed Room 2. Now click on "View 1" to make View 1 active (at the same time, View 2 is deactivated), and now select "Kitchen" and keep all others unselected. View 1 should now show only "Kitchen".

When user click on "View 2" again, the selection state should change to what View 2 is showing: that is, Bed Room 1 and Bed Room 2 is selected, while all other items are unselected, etc...

For simplicity, let's not do any interactions at this stage...

At the end, user should be able to save the Data Model, and the View Models, then restore them at later time...

Implementation is not an issue. The problem is what design pattern should be used? I am looking at this article: http://www.codeproject.com/Articles/100175/Model-View-ViewModel-MVVM-Explained. But this article is not enough....

d_stranz
27th April 2012, 05:45
In the Qt world, I don't think there is anything equivalent to a "ViewModel". The ViewModel implementation is spread over a whole collection of classes:

The QAbstractItemModel takes on some of the duties of the ViewModel, by determining the presentation of items through the various Qt:ItemRole types. QAbstractProxyModel and its derivatives also have some aspects of the ViewModel, by enabling items to be filtered or rearranged in between the model and the view. QItemDelegate performs some editing functions in conjunction with the view, and QItemSelectionModel handles selection.

So, even though I know you don't want to hear about "implementation":

For your purposes (as long as you don't need interaction in the "view"), a QItemSelectionModel instance for each of your "views" can serve as the glue between that view and the tree. When each view is activated (see QMdiSubWindow::windowStateChanged()), it should emit a signal (like "selectionModelChanged( QItemSelectionModel * )"). These signals are all connected to a corresponding slot in the tree view ("onSelectionModelChanged( QItemSelectionModel * )") which changes the current selection model and updates the selections to match.

Sharing a selection model between multiple views based on the same underlying model is a standard way to synchronize selections between them.