2

我正在寻找一种方法来自动“缩放以适应” GtkLayout 的内容,具体取决于它允许占用的空间量。

这是我的解决方案:

class MyLayout(Gtk.Layout):
    def __init__(self, document, **args):
        Gtk.Layout.__init__(self, **args)
        doc_width = 0
        doc_height = 0
        self.document = document

        # Determine the size of the document, I want to display
        for page in self.document.pages:
            doc_width = max(doc_width, page.width)
            doc_height += page.height
        self.aspect_ratio = doc_height / doc_width

        self.connect("draw", self.draw)

    def draw(self, widget, context):
        w = self.get_allocated_width()
        h = self.get_allocated_width()*self.aspect_ratio
        self.set_size(w, h) # sets the _content_ size, not the actual widget size
        print("draw")

不幸的是,这会导致每次调整窗口大小时都会调用 draw() 两次,这很慢,一旦我实际上需要在布局中绘制一些东西。

这个问题的正确解决方案是什么?

最好的问候,
Fabian Henze

解决方案:我前段时间找到了解决方案。它在我项目中。我没有时间从中提取一个最小的例子。如果有人愿意这样做,我很乐意接受它作为答案。

4

1 回答 1

1

我的猜测是,这可能是父母多次重新绘制孩子并由层次结构对象触发的问题。

我扩展了您的示例以在绘制流程上显示更多详细信息。

from gi.repository import Gtk

class MyLayout(Gtk.Layout):
    def __init__(self, document, **args):
        Gtk.Layout.__init__(self, **args)
        doc_width = 0
        doc_height = 0
        self.document = document

        # Determine the size of the document, I want to display
        for page in self.document.pages:
            doc_width = max(doc_width, page.width)
            doc_height += page.height
        self.aspect_ratio = doc_height / doc_width

        self.connect("draw", self.draw)

    def do_realize(self):
        Gtk.Layout.do_realize(self)
        print ('do_realize')

    def do_show(self):
        Gtk.Layout.do_show(self)
        print ('do_show')

    def do_draw(self, widget):
        Gtk.Layout.do_draw(self, widget)
        print ('do_draw'), widget

    def draw(self, widget, context):
        print widget, context
        w = self.get_allocated_width()
        h = self.get_allocated_width() * self.aspect_ratio
        self.set_size(w, h) # sets the _content_ size, not the actual widget size
        print("draw")

class page(object):
    def __init__(self, width, height):
        self.width = width
        self.height = height

class document(object):
    def __init__(self):
        self.pages = [page(20, 10), page(40, 30)]

layout = MyLayout(document())
win = Gtk.Window()
layout.show()
win.add(layout)
win.show()
Gtk.main()

对我来说,输出是:

do_show
do_realize
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>
<MyLayout object at 0xb6b0ef54 (__main__+MyLayout at 0x898e950)> <cairo.Context object at 0xb73237f0>
draw
do_draw <cairo.Context object at 0xb73237f0>

我可以看到绘图信号在 do_draw 内部小部件函数之前发出,并且被多次触发。

于 2012-06-21T02:10:15.250 回答