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