1#!/usr/bin/env python
2
3
4#############################################################################
5##
6## Copyright (C) 2013 Riverbank Computing Limited.
7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8## All rights reserved.
9##
10## This file is part of the examples of PyQt.
11##
12## $QT_BEGIN_LICENSE:BSD$
13## You may use this file under the terms of the BSD license as follows:
14##
15## "Redistribution and use in source and binary forms, with or without
16## modification, are permitted provided that the following conditions are
17## met:
18##   * Redistributions of source code must retain the above copyright
19##     notice, this list of conditions and the following disclaimer.
20##   * Redistributions in binary form must reproduce the above copyright
21##     notice, this list of conditions and the following disclaimer in
22##     the documentation and/or other materials provided with the
23##     distribution.
24##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
25##     the names of its contributors may be used to endorse or promote
26##     products derived from this software without specific prior written
27##     permission.
28##
29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
40## $QT_END_LICENSE$
41##
42#############################################################################
43
44
45from PyQt5.QtCore import QSizeF, Qt
46from PyQt5.QtWidgets import (QApplication, QGraphicsAnchorLayout,
47        QGraphicsProxyWidget, QGraphicsScene, QGraphicsView, QGraphicsWidget,
48        QPushButton, QSizePolicy)
49
50
51def createItem(minimum, preferred, maximum, name):
52    w = QGraphicsProxyWidget()
53
54    w.setWidget(QPushButton(name))
55    w.setMinimumSize(minimum)
56    w.setPreferredSize(preferred)
57    w.setMaximumSize(maximum)
58    w.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
59
60    return w
61
62
63if __name__ == '__main__':
64
65    import sys
66
67    app = QApplication(sys.argv)
68
69    scene = QGraphicsScene()
70    scene.setSceneRect(0, 0, 800, 480)
71
72    minSize = QSizeF(30, 100)
73    prefSize = QSizeF(210, 100)
74    maxSize = QSizeF(300, 100)
75
76    a = createItem(minSize, prefSize, maxSize, "A")
77    b = createItem(minSize, prefSize, maxSize, "B")
78    c = createItem(minSize, prefSize, maxSize, "C")
79    d = createItem(minSize, prefSize, maxSize, "D")
80    e = createItem(minSize, prefSize, maxSize, "E")
81    f = createItem(QSizeF(30, 50), QSizeF(150, 50), maxSize, "F")
82    g = createItem(QSizeF(30, 50), QSizeF(30, 100), maxSize, "G")
83
84    l = QGraphicsAnchorLayout()
85    l.setSpacing(0)
86
87    w = QGraphicsWidget(None, Qt.Window)
88    w.setPos(20, 20)
89    w.setLayout(l)
90
91    # Vertical.
92    l.addAnchor(a, Qt.AnchorTop, l, Qt.AnchorTop)
93    l.addAnchor(b, Qt.AnchorTop, l, Qt.AnchorTop)
94
95    l.addAnchor(c, Qt.AnchorTop, a, Qt.AnchorBottom)
96    l.addAnchor(c, Qt.AnchorTop, b, Qt.AnchorBottom)
97    l.addAnchor(c, Qt.AnchorBottom, d, Qt.AnchorTop)
98    l.addAnchor(c, Qt.AnchorBottom, e, Qt.AnchorTop)
99
100    l.addAnchor(d, Qt.AnchorBottom, l, Qt.AnchorBottom)
101    l.addAnchor(e, Qt.AnchorBottom, l, Qt.AnchorBottom)
102
103    l.addAnchor(c, Qt.AnchorTop, f, Qt.AnchorTop)
104    l.addAnchor(c, Qt.AnchorVerticalCenter, f, Qt.AnchorBottom)
105    l.addAnchor(f, Qt.AnchorBottom, g, Qt.AnchorTop)
106    l.addAnchor(c, Qt.AnchorBottom, g, Qt.AnchorBottom)
107
108    # Horizontal.
109    l.addAnchor(l, Qt.AnchorLeft, a, Qt.AnchorLeft)
110    l.addAnchor(l, Qt.AnchorLeft, d, Qt.AnchorLeft)
111    l.addAnchor(a, Qt.AnchorRight, b, Qt.AnchorLeft)
112
113    l.addAnchor(a, Qt.AnchorRight, c, Qt.AnchorLeft)
114    l.addAnchor(c, Qt.AnchorRight, e, Qt.AnchorLeft)
115
116    l.addAnchor(b, Qt.AnchorRight, l, Qt.AnchorRight)
117    l.addAnchor(e, Qt.AnchorRight, l, Qt.AnchorRight)
118    l.addAnchor(d, Qt.AnchorRight, e, Qt.AnchorLeft)
119
120    l.addAnchor(l, Qt.AnchorLeft, f, Qt.AnchorLeft)
121    l.addAnchor(l, Qt.AnchorLeft, g, Qt.AnchorLeft)
122    l.addAnchor(f, Qt.AnchorRight, g, Qt.AnchorRight)
123
124    scene.addItem(w)
125    scene.setBackgroundBrush(Qt.darkGreen)
126
127    view = QGraphicsView(scene)
128    view.show()
129
130    sys.exit(app.exec_())
131