| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include <QApplication> |
| 52 | #include <QLabel> |
| 53 | #include <QGraphicsAnchorLayout> |
| 54 | #include <QGraphicsProxyWidget> |
| 55 | #include <QGraphicsScene> |
| 56 | #include <QGraphicsSceneResizeEvent> |
| 57 | #include <QGraphicsView> |
| 58 | #include <QGraphicsWidget> |
| 59 | #include <QPainter> |
| 60 | #include <QPushButton> |
| 61 | |
| 62 | |
| 63 | class GraphicsView : public QGraphicsView |
| 64 | { |
| 65 | public: |
| 66 | GraphicsView(QGraphicsScene *scene, QGraphicsWidget *widget) |
| 67 | : QGraphicsView(scene), w(widget) |
| 68 | { |
| 69 | } |
| 70 | |
| 71 | void resizeEvent(QResizeEvent *event) override |
| 72 | { |
| 73 | w->setGeometry(0, 0, event->size().width(), event->size().height()); |
| 74 | } |
| 75 | |
| 76 | QGraphicsWidget *w; |
| 77 | }; |
| 78 | |
| 79 | class PixmapWidget : public QGraphicsLayoutItem |
| 80 | { |
| 81 | public: |
| 82 | PixmapWidget(const QPixmap &pix) |
| 83 | : QGraphicsLayoutItem(), original(new QGraphicsPixmapItem(pix)) |
| 84 | , r(QRectF(QPointF(0, 0), pix.size())) |
| 85 | { |
| 86 | setGraphicsItem(original); |
| 87 | original->show(); |
| 88 | } |
| 89 | |
| 90 | ~PixmapWidget() |
| 91 | { |
| 92 | setGraphicsItem(nullptr); |
| 93 | delete original; |
| 94 | } |
| 95 | |
| 96 | void setZValue(qreal z) |
| 97 | { |
| 98 | original->setZValue(z); |
| 99 | } |
| 100 | |
| 101 | void setGeometry(const QRectF &rect) override |
| 102 | { |
| 103 | original->setTransform(QTransform::fromScale(rect.width() / r.width(), |
| 104 | rect.height() / r.height()), true); |
| 105 | original->setPos(rect.x(), rect.y()); |
| 106 | r = rect; |
| 107 | } |
| 108 | |
| 109 | protected: |
| 110 | QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override |
| 111 | { |
| 112 | Q_UNUSED(constraint); |
| 113 | QSizeF sh; |
| 114 | switch (which) { |
| 115 | case Qt::MinimumSize: |
| 116 | sh = QSizeF(0, 0); |
| 117 | break; |
| 118 | case Qt::PreferredSize: |
| 119 | sh = QSizeF(50, 50); |
| 120 | break; |
| 121 | case Qt::MaximumSize: |
| 122 | sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); |
| 123 | break; |
| 124 | default: |
| 125 | break; |
| 126 | } |
| 127 | return sh; |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | QGraphicsPixmapItem *original; |
| 132 | QRectF r; |
| 133 | }; |
| 134 | |
| 135 | |
| 136 | class PlaceWidget : public QGraphicsWidget |
| 137 | { |
| 138 | Q_OBJECT |
| 139 | |
| 140 | public: |
| 141 | PlaceWidget(const QPixmap &pix) |
| 142 | : QGraphicsWidget() |
| 143 | , original(pix) |
| 144 | , scaled(pix) |
| 145 | { |
| 146 | } |
| 147 | |
| 148 | void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override |
| 149 | { |
| 150 | const QPointF reflection(0, scaled.height() + 2); |
| 151 | |
| 152 | painter->drawPixmap(QPointF(), scaled); |
| 153 | |
| 154 | QPixmap tmp(scaled.size()); |
| 155 | tmp.fill(Qt::transparent); |
| 156 | QPainter p(&tmp); |
| 157 | |
| 158 | // create gradient |
| 159 | QPoint p1(scaled.width() / 2, 0); |
| 160 | QPoint p2(scaled.width() / 2, scaled.height()); |
| 161 | QLinearGradient linearGrad(p1, p2); |
| 162 | linearGrad.setColorAt(0, QColor(0, 0, 0, 0)); |
| 163 | linearGrad.setColorAt(0.65, QColor(0, 0, 0, 127)); |
| 164 | linearGrad.setColorAt(1, QColor(0, 0, 0, 255)); |
| 165 | |
| 166 | // apply 'mask' |
| 167 | p.setBrush(linearGrad); |
| 168 | p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad)); |
| 169 | p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad)); |
| 170 | |
| 171 | // paint the image flipped |
| 172 | p.setCompositionMode(QPainter::CompositionMode_DestinationOver); |
| 173 | p.drawPixmap(0, 0, QPixmap::fromImage(scaled.toImage().mirrored(false, true))); |
| 174 | p.end(); |
| 175 | |
| 176 | painter->drawPixmap(reflection, tmp); |
| 177 | } |
| 178 | |
| 179 | void resizeEvent(QGraphicsSceneResizeEvent *event) override |
| 180 | { |
| 181 | QSize newSize = event->newSize().toSize(); |
| 182 | newSize.setHeight(newSize.height() / 2); |
| 183 | scaled = original.scaled(newSize); |
| 184 | } |
| 185 | |
| 186 | QRectF boundingRect() const override |
| 187 | { |
| 188 | QSize size(scaled.width(), scaled.height() * 2 + 2); |
| 189 | return QRectF(QPointF(0, 0), size); |
| 190 | } |
| 191 | |
| 192 | private: |
| 193 | QPixmap original; |
| 194 | QPixmap scaled; |
| 195 | }; |
| 196 | |
| 197 | |
| 198 | int main(int argc, char *argv[]) |
| 199 | { |
| 200 | Q_INIT_RESOURCE(weatheranchorlayout); |
| 201 | |
| 202 | QApplication app(argc, argv); |
| 203 | |
| 204 | QGraphicsScene scene; |
| 205 | scene.setSceneRect(0, 0, 800, 480); |
| 206 | |
| 207 | // pixmaps widgets |
| 208 | PixmapWidget *title = new PixmapWidget(QPixmap(":/images/title.jpg" )); |
| 209 | PlaceWidget *place = new PlaceWidget(QPixmap(":/images/place.jpg" )); |
| 210 | PixmapWidget *details = new PixmapWidget(QPixmap(":/images/5days.jpg" )); |
| 211 | PixmapWidget *sunnyWeather = new PixmapWidget(QPixmap(":/images/weather-few-clouds.png" )); |
| 212 | PixmapWidget *tabbar = new PixmapWidget(QPixmap(":/images/tabbar.jpg" )); |
| 213 | |
| 214 | |
| 215 | // setup sizes |
| 216 | title->setPreferredSize(QSizeF(348, 45)); |
| 217 | title->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 218 | |
| 219 | place->setPreferredSize(QSizeF(96, 72)); |
| 220 | place->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 221 | |
| 222 | details->setMinimumSize(QSizeF(200, 112)); |
| 223 | details->setPreferredSize(QSizeF(200, 112)); |
| 224 | details->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 225 | |
| 226 | tabbar->setPreferredSize(QSizeF(70, 24)); |
| 227 | tabbar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 228 | |
| 229 | sunnyWeather->setPreferredSize(QSizeF(128, 97)); |
| 230 | sunnyWeather->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 231 | sunnyWeather->setZValue(9999); |
| 232 | |
| 233 | // start anchor layout |
| 234 | QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout; |
| 235 | layout->setSpacing(0); |
| 236 | |
| 237 | // setup the main widget |
| 238 | QGraphicsWidget *widget = new QGraphicsWidget(nullptr, Qt::Window); |
| 239 | QPalette p; |
| 240 | p.setColor(QPalette::Window, Qt::black); |
| 241 | widget->setPalette(p); |
| 242 | widget->setPos(20, 20); |
| 243 | widget->setLayout(layout); |
| 244 | |
| 245 | // vertical anchors |
| 246 | QGraphicsAnchor *anchor = layout->addAnchor(title, Qt::AnchorTop, layout, Qt::AnchorTop); |
| 247 | anchor = layout->addAnchor(place, Qt::AnchorTop, title, Qt::AnchorBottom); |
| 248 | anchor->setSpacing(12); |
| 249 | anchor = layout->addAnchor(place, Qt::AnchorBottom, layout, Qt::AnchorBottom); |
| 250 | anchor->setSpacing(12); |
| 251 | |
| 252 | anchor = layout->addAnchor(sunnyWeather, Qt::AnchorTop, title, Qt::AnchorTop); |
| 253 | anchor = layout->addAnchor(sunnyWeather, Qt::AnchorBottom, layout, Qt::AnchorVerticalCenter); |
| 254 | |
| 255 | anchor = layout->addAnchor(tabbar, Qt::AnchorTop, title, Qt::AnchorBottom); |
| 256 | anchor->setSpacing(5); |
| 257 | anchor = layout->addAnchor(details, Qt::AnchorTop, tabbar, Qt::AnchorBottom); |
| 258 | anchor->setSpacing(2); |
| 259 | anchor = layout->addAnchor(details, Qt::AnchorBottom, layout, Qt::AnchorBottom); |
| 260 | anchor->setSpacing(12); |
| 261 | |
| 262 | // horizontal anchors |
| 263 | anchor = layout->addAnchor(layout, Qt::AnchorLeft, title, Qt::AnchorLeft); |
| 264 | anchor = layout->addAnchor(title, Qt::AnchorRight, layout, Qt::AnchorRight); |
| 265 | |
| 266 | anchor = layout->addAnchor(place, Qt::AnchorLeft, layout, Qt::AnchorLeft); |
| 267 | anchor->setSpacing(15); |
| 268 | anchor = layout->addAnchor(place, Qt::AnchorRight, details, Qt::AnchorLeft); |
| 269 | anchor->setSpacing(35); |
| 270 | |
| 271 | anchor = layout->addAnchor(sunnyWeather, Qt::AnchorLeft, place, Qt::AnchorHorizontalCenter); |
| 272 | anchor = layout->addAnchor(sunnyWeather, Qt::AnchorRight, layout, Qt::AnchorHorizontalCenter); |
| 273 | |
| 274 | anchor = layout->addAnchor(tabbar, Qt::AnchorHorizontalCenter, details, Qt::AnchorHorizontalCenter); |
| 275 | anchor = layout->addAnchor(details, Qt::AnchorRight, layout, Qt::AnchorRight); |
| 276 | |
| 277 | // QGV setup |
| 278 | scene.addItem(widget); |
| 279 | scene.setBackgroundBrush(Qt::white); |
| 280 | QGraphicsView *view = new QGraphicsView(&scene); |
| 281 | view->show(); |
| 282 | |
| 283 | return app.exec(); |
| 284 | } |
| 285 | |
| 286 | #include "main.moc" |
| 287 | |