views:

68

answers:

1

I am writing an application which is used for drawing widgets/menus/controls etc to create application mockups. Each screen is represented as collection of widgets, and each widget is simple class e.g.

class Model(object):
    def __init__(self):
        self.widgets = []

class Widget(object):
    def __init__(self):
        self.x, self.y = 0, 0
        self.w, self.h = 100,20
        self.text = "Widget"

Now user can edit x,y,w,h in editor and it is rendered in many views(places), rendering itself may change w and h because we want to at least show best fit. e.g. text "Widget" may need width 200 or 205 in different views

Question:

So problem is rendering/view itself modifes the model, how to avoid that? For now I have main view and main model, any other view if wants to render copies model and renders it hence avoiding the change in main model.

This approach is simple and code remains simple but needs a unnecessary copy of model, I have thought of many ways to avoid that but all will complicate code and may not be that efficient because anyway if model is not copied render-time-attributes needs to be placed somewhere e.g. in each renderer for each widget.

I am implementing it in python but that is not relevant for the answer.

+1  A: 

If the only thing that rendering changes in the model is the x,y,w,h, then you have a few options:

  1. Duplicate those fields: x,y,w,h and x_drawn, y_drawn, etc. Rendering can change the _drawn values, and make your models not mind that they have changed. For example, when saving a model, don't save the _drawn values. Then the models can change during rendering, but it won't matter.

  2. Move x,y,w,h into a RenderedGeometry class, and give each model an instance. Now you can still copy the model, but it can be a shallow copy, plus a new copy of the RenderedGeometry class, reducing the amount you have to copy.

  3. Duplicate the fields into a x_original, y_original set of fields. Before rendering, copy x,y into the _original fields. After rendering, copy them back. The model instances themselves don't have to be copied, just a small amount of data.

Ned Batchelder
+1 for 3 options, but I think all of three will complicate code, number 1 would have been promising but I do rendering in threads also that would mean I may need x_drawn per renderer, which will further complicate things
Anurag Uniyal