1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtGui>
|
---|
42 |
|
---|
43 | class Widget : public QGraphicsWidget
|
---|
44 | {
|
---|
45 | public:
|
---|
46 | Widget(const QColor &color, const QColor &textColor, const QString &caption,
|
---|
47 | QGraphicsItem *parent = 0)
|
---|
48 | : QGraphicsWidget(parent), caption(caption), color(color), textColor(textColor)
|
---|
49 | {
|
---|
50 | }
|
---|
51 |
|
---|
52 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget * = 0)
|
---|
53 | {
|
---|
54 | QFont font;
|
---|
55 | font.setPixelSize(0.75 * qMin(boundingRect().width(), boundingRect().height()));
|
---|
56 |
|
---|
57 | painter->fillRect(boundingRect(), color);
|
---|
58 | painter->save();
|
---|
59 | painter->setFont(font);
|
---|
60 | painter->setPen(textColor);
|
---|
61 | painter->drawText(boundingRect(), Qt::AlignCenter, caption);
|
---|
62 | painter->restore();
|
---|
63 | }
|
---|
64 |
|
---|
65 | private:
|
---|
66 | QString caption;
|
---|
67 | QColor color;
|
---|
68 | QColor textColor;
|
---|
69 | };
|
---|
70 |
|
---|
71 | int main(int argc, char *argv[])
|
---|
72 | {
|
---|
73 | QApplication app(argc, argv);
|
---|
74 |
|
---|
75 | QGraphicsScene *scene = new QGraphicsScene();
|
---|
76 |
|
---|
77 | Widget *a = new Widget(Qt::blue, Qt::white, "a");
|
---|
78 | a->setPreferredSize(100, 100);
|
---|
79 | Widget *b = new Widget(Qt::green, Qt::black, "b");
|
---|
80 | b->setPreferredSize(100, 100);
|
---|
81 | Widget *c = new Widget(Qt::red, Qt::black, "c");
|
---|
82 | c->setPreferredSize(100, 100);
|
---|
83 |
|
---|
84 | QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout();
|
---|
85 | /*
|
---|
86 | //! [adding a corner anchor in two steps]
|
---|
87 | layout->addAnchor(a, Qt::AnchorTop, layout, Qt::AnchorTop);
|
---|
88 | layout->addAnchor(a, Qt::AnchorLeft, layout, Qt::AnchorLeft);
|
---|
89 | //! [adding a corner anchor in two steps]
|
---|
90 | */
|
---|
91 | //! [adding a corner anchor]
|
---|
92 | layout->addCornerAnchors(a, Qt::TopLeftCorner, layout, Qt::TopLeftCorner);
|
---|
93 | //! [adding a corner anchor]
|
---|
94 |
|
---|
95 | //! [adding anchors]
|
---|
96 | layout->addAnchor(b, Qt::AnchorLeft, a, Qt::AnchorRight);
|
---|
97 | layout->addAnchor(b, Qt::AnchorTop, a, Qt::AnchorBottom);
|
---|
98 | //! [adding anchors]
|
---|
99 |
|
---|
100 | // Place a third widget below the second.
|
---|
101 | layout->addAnchor(b, Qt::AnchorBottom, c, Qt::AnchorTop);
|
---|
102 |
|
---|
103 | /*
|
---|
104 | //! [adding anchors to match sizes in two steps]
|
---|
105 | layout->addAnchor(b, Qt::AnchorLeft, c, Qt::AnchorLeft);
|
---|
106 | layout->addAnchor(b, Qt::AnchorRight, c, Qt::AnchorRight);
|
---|
107 | //! [adding anchors to match sizes in two steps]
|
---|
108 | */
|
---|
109 |
|
---|
110 | //! [adding anchors to match sizes]
|
---|
111 | layout->addAnchors(b, c, Qt::Horizontal);
|
---|
112 | //! [adding anchors to match sizes]
|
---|
113 |
|
---|
114 | // Anchor the bottom-right corner of the third widget to the bottom-right
|
---|
115 | // corner of the layout.
|
---|
116 | layout->addCornerAnchors(c, Qt::BottomRightCorner, layout, Qt::BottomRightCorner);
|
---|
117 |
|
---|
118 | QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window | Qt::CustomizeWindowHint | Qt::WindowTitleHint);
|
---|
119 | w->setPos(20, 20);
|
---|
120 | w->setMinimumSize(100, 100);
|
---|
121 | w->setPreferredSize(320, 240);
|
---|
122 | w->setLayout(layout);
|
---|
123 | w->setWindowTitle(QApplication::translate("simpleanchorlayout", "QGraphicsAnchorLayout in use"));
|
---|
124 | scene->addItem(w);
|
---|
125 |
|
---|
126 | QGraphicsView *view = new QGraphicsView();
|
---|
127 | view->setScene(scene);
|
---|
128 | view->setWindowTitle(QApplication::translate("simpleanchorlayout", "Simple Anchor Layout"));
|
---|
129 | view->resize(360, 320);
|
---|
130 | view->show();
|
---|
131 |
|
---|
132 | return app.exec();
|
---|
133 | }
|
---|