1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 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:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QLabel>
|
---|
43 | #include <QPainter>
|
---|
44 | #include <QPushButton>
|
---|
45 | #include <QApplication>
|
---|
46 |
|
---|
47 | #include <QGraphicsView>
|
---|
48 | #include <QGraphicsScene>
|
---|
49 | #include <QGraphicsWidget>
|
---|
50 | #include <QGraphicsProxyWidget>
|
---|
51 | #include <QGraphicsAnchorLayout>
|
---|
52 | #include <QGraphicsSceneResizeEvent>
|
---|
53 |
|
---|
54 |
|
---|
55 | class PixmapWidget : public QGraphicsLayoutItem
|
---|
56 | {
|
---|
57 |
|
---|
58 | public:
|
---|
59 | PixmapWidget(const QPixmap &pix) : QGraphicsLayoutItem()
|
---|
60 | {
|
---|
61 | original = new QGraphicsPixmapItem(pix);
|
---|
62 | setGraphicsItem(original);
|
---|
63 | original->show();
|
---|
64 | r = QRectF(QPointF(0, 0), pix.size());
|
---|
65 | }
|
---|
66 |
|
---|
67 | ~PixmapWidget()
|
---|
68 | {
|
---|
69 | setGraphicsItem(0);
|
---|
70 | delete original;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void setZValue(qreal z)
|
---|
74 | {
|
---|
75 | original->setZValue(z);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void setGeometry (const QRectF &rect)
|
---|
79 | {
|
---|
80 | original->scale(rect.width() / r.width(), rect.height() / r.height());
|
---|
81 | original->setPos(rect.x(), rect.y());
|
---|
82 | r = rect;
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected:
|
---|
86 | QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const
|
---|
87 | {
|
---|
88 | Q_UNUSED(constraint);
|
---|
89 | QSizeF sh;
|
---|
90 | switch (which) {
|
---|
91 | case Qt::MinimumSize:
|
---|
92 | sh = QSizeF(0, 0);
|
---|
93 | break;
|
---|
94 | case Qt::PreferredSize:
|
---|
95 | sh = QSizeF(50, 50);
|
---|
96 | break;
|
---|
97 | case Qt::MaximumSize:
|
---|
98 | sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
|
---|
99 | break;
|
---|
100 | }
|
---|
101 | return sh;
|
---|
102 | }
|
---|
103 |
|
---|
104 | private:
|
---|
105 | QGraphicsPixmapItem *original;
|
---|
106 | QRectF r;
|
---|
107 | };
|
---|
108 |
|
---|
109 |
|
---|
110 | class PlaceWidget : public QGraphicsWidget
|
---|
111 | {
|
---|
112 | Q_OBJECT
|
---|
113 |
|
---|
114 | public:
|
---|
115 | PlaceWidget(const QPixmap &pix) : QGraphicsWidget(), original(pix), scaled(pix)
|
---|
116 | {
|
---|
117 | }
|
---|
118 |
|
---|
119 | void paint(QPainter *painter, const QStyleOptionGraphicsItem*, QWidget*)
|
---|
120 | {
|
---|
121 | QPointF reflection = QPointF();
|
---|
122 | reflection.setY(scaled.height() + 2);
|
---|
123 |
|
---|
124 | painter->drawPixmap(QPointF(), scaled);
|
---|
125 |
|
---|
126 | QPixmap tmp(scaled.size());
|
---|
127 | tmp.fill(Qt::transparent);
|
---|
128 | QPainter p(&tmp);
|
---|
129 |
|
---|
130 | // create gradient
|
---|
131 | QPoint p1(scaled.width() / 2, 0);
|
---|
132 | QPoint p2(scaled.width() / 2, scaled.height());
|
---|
133 | QLinearGradient linearGrad(p1, p2);
|
---|
134 | linearGrad.setColorAt(0, QColor(0, 0, 0, 0));
|
---|
135 | linearGrad.setColorAt(0.65, QColor(0, 0, 0, 127));
|
---|
136 | linearGrad.setColorAt(1, QColor(0, 0, 0, 255));
|
---|
137 |
|
---|
138 | // apply 'mask'
|
---|
139 | p.setBrush(linearGrad);
|
---|
140 | p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad));
|
---|
141 | p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad));
|
---|
142 |
|
---|
143 | // paint the image flipped
|
---|
144 | p.setCompositionMode(QPainter::CompositionMode_DestinationOver);
|
---|
145 | p.drawPixmap(0, 0, QPixmap::fromImage(scaled.toImage().mirrored(false, true)));
|
---|
146 | p.end();
|
---|
147 |
|
---|
148 | painter->drawPixmap(reflection, tmp);
|
---|
149 | }
|
---|
150 |
|
---|
151 | void resizeEvent(QGraphicsSceneResizeEvent *event)
|
---|
152 | {
|
---|
153 | QSize newSize = event->newSize().toSize();
|
---|
154 | newSize.setHeight(newSize.height() / 2);
|
---|
155 | scaled = original.scaled(newSize);
|
---|
156 | }
|
---|
157 |
|
---|
158 | QRectF boundingRect() const
|
---|
159 | {
|
---|
160 | QSize size(scaled.width(), scaled.height() * 2 + 2);
|
---|
161 | return QRectF(QPointF(0, 0), size);
|
---|
162 | }
|
---|
163 |
|
---|
164 | private:
|
---|
165 | QPixmap original;
|
---|
166 | QPixmap scaled;
|
---|
167 | };
|
---|
168 |
|
---|
169 |
|
---|
170 | static QGraphicsProxyWidget *createItem(const QString &name = "Unnamed")
|
---|
171 | {
|
---|
172 | QGraphicsProxyWidget *w = new QGraphicsProxyWidget;
|
---|
173 | w->setWidget(new QPushButton(name));
|
---|
174 | w->setData(0, name);
|
---|
175 | w->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
|
---|
176 | return w;
|
---|
177 | }
|
---|
178 |
|
---|
179 | int main(int argc, char **argv)
|
---|
180 | {
|
---|
181 | Q_INIT_RESOURCE(weatheranchorlayout);
|
---|
182 |
|
---|
183 | QApplication app(argc, argv);
|
---|
184 |
|
---|
185 | QGraphicsScene scene;
|
---|
186 | scene.setSceneRect(0, 0, 800, 480);
|
---|
187 |
|
---|
188 | // pixmaps widgets
|
---|
189 | PixmapWidget *title = new PixmapWidget(QPixmap(":/images/title.jpg"));
|
---|
190 | PlaceWidget *place = new PlaceWidget(QPixmap(":/images/place.jpg"));
|
---|
191 | PixmapWidget *details = new PixmapWidget(QPixmap(":/images/5days.jpg"));
|
---|
192 | PixmapWidget *sunnyWeather = new PixmapWidget(QPixmap(":/images/weather-few-clouds.png"));
|
---|
193 | PixmapWidget *tabbar = new PixmapWidget(QPixmap(":/images/tabbar.jpg"));
|
---|
194 |
|
---|
195 |
|
---|
196 | // setup sizes
|
---|
197 | title->setPreferredSize(QSizeF(348, 45));
|
---|
198 | title->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
---|
199 |
|
---|
200 | place->setPreferredSize(QSizeF(96, 72));
|
---|
201 | place->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
---|
202 |
|
---|
203 | details->setMinimumSize(QSizeF(200, 112));
|
---|
204 | details->setPreferredSize(QSizeF(200, 112));
|
---|
205 | details->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
---|
206 |
|
---|
207 | tabbar->setPreferredSize(QSizeF(70, 24));
|
---|
208 | tabbar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
---|
209 |
|
---|
210 | sunnyWeather->setPreferredSize(QSizeF(128, 97));
|
---|
211 | sunnyWeather->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
|
---|
212 | sunnyWeather->setZValue(9999);
|
---|
213 |
|
---|
214 | // start anchor layout
|
---|
215 | QGraphicsAnchorLayout *l = new QGraphicsAnchorLayout;
|
---|
216 | l->setSpacing(0);
|
---|
217 |
|
---|
218 | // setup the main widget
|
---|
219 | QGraphicsWidget *w = new QGraphicsWidget(0, Qt::Window);
|
---|
220 | QPalette p;
|
---|
221 | p.setColor(QPalette::Window, Qt::black);
|
---|
222 | w->setPalette(p);
|
---|
223 | w->setPos(20, 20);
|
---|
224 | w->setLayout(l);
|
---|
225 |
|
---|
226 | // vertical anchors
|
---|
227 | QGraphicsAnchor *anchor = l->addAnchor(title, Qt::AnchorTop, l, Qt::AnchorTop);
|
---|
228 | anchor = l->addAnchor(place, Qt::AnchorTop, title, Qt::AnchorBottom);
|
---|
229 | anchor->setSpacing(12);
|
---|
230 | anchor = l->addAnchor(place, Qt::AnchorBottom, l, Qt::AnchorBottom);
|
---|
231 | anchor->setSpacing(12);
|
---|
232 |
|
---|
233 | anchor = l->addAnchor(sunnyWeather, Qt::AnchorTop, title, Qt::AnchorTop);
|
---|
234 | anchor = l->addAnchor(sunnyWeather, Qt::AnchorBottom, l, Qt::AnchorVerticalCenter);
|
---|
235 |
|
---|
236 | anchor = l->addAnchor(tabbar, Qt::AnchorTop, title, Qt::AnchorBottom);
|
---|
237 | anchor->setSpacing(5);
|
---|
238 | anchor = l->addAnchor(details, Qt::AnchorTop, tabbar, Qt::AnchorBottom);
|
---|
239 | anchor->setSpacing(2);
|
---|
240 | anchor = l->addAnchor(details, Qt::AnchorBottom, l, Qt::AnchorBottom);
|
---|
241 | anchor->setSpacing(12);
|
---|
242 |
|
---|
243 | // horizontal anchors
|
---|
244 | anchor = l->addAnchor(l, Qt::AnchorLeft, title, Qt::AnchorLeft);
|
---|
245 | anchor = l->addAnchor(title, Qt::AnchorRight, l, Qt::AnchorRight);
|
---|
246 |
|
---|
247 | anchor = l->addAnchor(place, Qt::AnchorLeft, l, Qt::AnchorLeft);
|
---|
248 | anchor->setSpacing(15);
|
---|
249 | anchor = l->addAnchor(place, Qt::AnchorRight, details, Qt::AnchorLeft);
|
---|
250 | anchor->setSpacing(35);
|
---|
251 |
|
---|
252 | anchor = l->addAnchor(sunnyWeather, Qt::AnchorLeft, place, Qt::AnchorHorizontalCenter);
|
---|
253 | anchor = l->addAnchor(sunnyWeather, Qt::AnchorRight, l, Qt::AnchorHorizontalCenter);
|
---|
254 |
|
---|
255 | anchor = l->addAnchor(tabbar, Qt::AnchorHorizontalCenter, details, Qt::AnchorHorizontalCenter);
|
---|
256 | anchor = l->addAnchor(details, Qt::AnchorRight, l, Qt::AnchorRight);
|
---|
257 |
|
---|
258 | // QGV setup
|
---|
259 | scene.addItem(w);
|
---|
260 | scene.setBackgroundBrush(Qt::white);
|
---|
261 | QGraphicsView *view = new QGraphicsView(&scene);
|
---|
262 | view->show();
|
---|
263 |
|
---|
264 | return app.exec();
|
---|
265 | }
|
---|
266 |
|
---|
267 | #include "main.moc"
|
---|