如何解除窗口小部件的模型/视图 [英] How to decouple model/view for widgets

查看:109
本文介绍了如何解除窗口小部件的模型/视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,用于绘制小部件/菜单/控件等,以创建应用程序模型。每个屏幕都被表示为小部件的集合,每个小部件都是简单的类例如

 类模型(对象):
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

现在,用户可以在编辑器中编辑x,y,w,h,并在许多视图(场所)中呈现,渲染本身可能会改变w和h,因为我们至少显示最佳匹配。
例如

b

$ b

问题:



所以问题是渲染/查看本身修改模型,如何避免?现在我有主视图和主要模型,任何其他视图,如果想要渲染副本模型,并渲染它,因此避免主模型的变化。



这种方法很简单,代码仍然很简单,但需要一个不必要的模型副本,我已经想到了许多方法来避免这一点,但是所有这些都会使代码复杂化,并且可能不会那么有效,因为无论如何,如果模型不被复制,那么时间属性需要被放置在某个地方在每个小部件的每个渲染器中。



我在python中实现它,但与答案无关。

解决方案

如果在模型中渲染更改的唯一方法是x,y,w,h,那么您有几个选项:


  1. 复制这些字段:x,y,w,h和x_drawn,y_drawn等。渲染可以更改_drawn值,并使您的模型不会改变它们。例如,保存模型时,请勿保存_drawn值。然后模型可以在渲染过程中改变,但并不重要。


  2. 将x,y,w,h移动到RenderedGeometry类中,并给出每个模型一个例子现在,您仍然可以复制模型,但它可以是一个浅层的副本,加上RenderedGeometry类的新副本,减少了您必须复制的数量。


  3. 将字段复制到一个x_original,y_original字段集合中。在渲染之前,将x,y复制到_original字段。渲染后,将其复制回来。模型实例本身不需要复制,只需少量数据。



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.

解决方案

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.

这篇关于如何解除窗口小部件的模型/视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆