| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#include <QApplication> |
| 5 |
#include <QLabel> |
| 6 |
#include <QGraphicsAnchorLayout> |
| 7 |
#include <QGraphicsProxyWidget> |
| 8 |
#include <QGraphicsScene> |
| 9 |
#include <QGraphicsSceneResizeEvent> |
| 10 |
#include <QGraphicsView> |
| 11 |
#include <QGraphicsWidget> |
| 12 |
#include <QPainter> |
| 13 |
#include <QPushButton> |
| 14 |
|
| 15 |
|
| 16 |
class GraphicsView : public QGraphicsView |
| 17 |
{ |
| 18 |
public: |
| 19 |
GraphicsView(QGraphicsScene *scene, QGraphicsWidget *widget) |
| 20 |
: QGraphicsView(scene), w(widget) |
| 21 |
{ |
| 22 |
} |
| 23 |
|
| 24 |
void resizeEvent(QResizeEvent *event) override |
| 25 |
{ |
| 26 |
w->setGeometry(0, 0, event->size().width(), event->size().height()); |
| 27 |
} |
| 28 |
|
| 29 |
QGraphicsWidget *w; |
| 30 |
}; |
| 31 |
|
| 32 |
class PixmapWidget : public QGraphicsLayoutItem |
| 33 |
{ |
| 34 |
public: |
| 35 |
PixmapWidget(const QPixmap &pix) |
| 36 |
: QGraphicsLayoutItem(), original(new QGraphicsPixmapItem(pix)) |
| 37 |
, r(QRectF(QPointF(0, 0), pix.size())) |
| 38 |
{ |
| 39 |
setGraphicsItem(original); |
| 40 |
original->show(); |
| 41 |
} |
| 42 |
|
| 43 |
~PixmapWidget() |
| 44 |
{ |
| 45 |
setGraphicsItem(nullptr); |
| 46 |
delete original; |
| 47 |
} |
| 48 |
|
| 49 |
void setZValue(qreal z) |
| 50 |
{ |
| 51 |
original->setZValue(z); |
| 52 |
} |
| 53 |
|
| 54 |
void setGeometry(const QRectF &rect) override |
| 55 |
{ |
| 56 |
original->setTransform(QTransform::fromScale(rect.width() / r.width(), |
| 57 |
rect.height() / r.height()), true); |
| 58 |
original->setPos(rect.x(), rect.y()); |
| 59 |
r = rect; |
| 60 |
} |
| 61 |
|
| 62 |
protected: |
| 63 |
QSizeF sizeHint(Qt::SizeHint which, const QSizeF &constraint = QSizeF()) const override |
| 64 |
{ |
| 65 |
Q_UNUSED(constraint); |
| 66 |
QSizeF sh; |
| 67 |
switch (which) { |
| 68 |
case Qt::MinimumSize: |
| 69 |
sh = QSizeF(0, 0); |
| 70 |
break; |
| 71 |
case Qt::PreferredSize: |
| 72 |
sh = QSizeF(50, 50); |
| 73 |
break; |
| 74 |
case Qt::MaximumSize: |
| 75 |
sh = QSizeF(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX); |
| 76 |
break; |
| 77 |
default: |
| 78 |
break; |
| 79 |
} |
| 80 |
return sh; |
| 81 |
} |
| 82 |
|
| 83 |
private: |
| 84 |
QGraphicsPixmapItem *original; |
| 85 |
QRectF r; |
| 86 |
}; |
| 87 |
|
| 88 |
|
| 89 |
class PlaceWidget : public QGraphicsWidget |
| 90 |
{ |
| 91 |
Q_OBJECT |
| 92 |
|
| 93 |
public: |
| 94 |
PlaceWidget(const QPixmap &pix) |
| 95 |
: QGraphicsWidget() |
| 96 |
, original(pix) |
| 97 |
, scaled(pix) |
| 98 |
{ |
| 99 |
} |
| 100 |
|
| 101 |
void paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *) override |
| 102 |
{ |
| 103 |
const QPointF reflection(0, scaled.height() + 2); |
| 104 |
|
| 105 |
painter->drawPixmap(QPointF(), scaled); |
| 106 |
|
| 107 |
QPixmap tmp(scaled.size()); |
| 108 |
tmp.fill(Qt::transparent); |
| 109 |
QPainter p(&tmp); |
| 110 |
|
| 111 |
// create gradient |
| 112 |
QPoint p1(scaled.width() / 2, 0); |
| 113 |
QPoint p2(scaled.width() / 2, scaled.height()); |
| 114 |
QLinearGradient linearGrad(p1, p2); |
| 115 |
linearGrad.setColorAt(0, QColor(0, 0, 0, 0)); |
| 116 |
linearGrad.setColorAt(0.65, QColor(0, 0, 0, 127)); |
| 117 |
linearGrad.setColorAt(1, QColor(0, 0, 0, 255)); |
| 118 |
|
| 119 |
// apply 'mask' |
| 120 |
p.setBrush(linearGrad); |
| 121 |
p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad)); |
| 122 |
p.fillRect(0, 0, tmp.width(), tmp.height(), QBrush(linearGrad)); |
| 123 |
|
| 124 |
// paint the image flipped |
| 125 |
p.setCompositionMode(QPainter::CompositionMode_DestinationOver); |
| 126 |
p.drawPixmap(0, 0, QPixmap::fromImage(scaled.toImage().flipped())); |
| 127 |
p.end(); |
| 128 |
|
| 129 |
painter->drawPixmap(reflection, tmp); |
| 130 |
} |
| 131 |
|
| 132 |
void resizeEvent(QGraphicsSceneResizeEvent *event) override |
| 133 |
{ |
| 134 |
QSize newSize = event->newSize().toSize(); |
| 135 |
newSize.setHeight(newSize.height() / 2); |
| 136 |
scaled = original.scaled(newSize); |
| 137 |
} |
| 138 |
|
| 139 |
QRectF boundingRect() const override |
| 140 |
{ |
| 141 |
QSize size(scaled.width(), scaled.height() * 2 + 2); |
| 142 |
return QRectF(QPointF(0, 0), size); |
| 143 |
} |
| 144 |
|
| 145 |
private: |
| 146 |
QPixmap original; |
| 147 |
QPixmap scaled; |
| 148 |
}; |
| 149 |
|
| 150 |
|
| 151 |
int main(int argc, char *argv[]) |
| 152 |
{ |
| 153 |
Q_INIT_RESOURCE(weatheranchorlayout); |
| 154 |
|
| 155 |
QApplication app(argc, argv); |
| 156 |
|
| 157 |
QGraphicsScene scene; |
| 158 |
scene.setSceneRect(0, 0, 800, 480); |
| 159 |
|
| 160 |
// pixmaps widgets |
| 161 |
PixmapWidget *title = new PixmapWidget(QPixmap(":/images/title.jpg")); |
| 162 |
PlaceWidget *place = new PlaceWidget(QPixmap(":/images/place.jpg")); |
| 163 |
PixmapWidget *details = new PixmapWidget(QPixmap(":/images/5days.jpg")); |
| 164 |
PixmapWidget *sunnyWeather = new PixmapWidget(QPixmap(":/images/weather-few-clouds.png")); |
| 165 |
PixmapWidget *tabbar = new PixmapWidget(QPixmap(":/images/tabbar.jpg")); |
| 166 |
|
| 167 |
|
| 168 |
// setup sizes |
| 169 |
title->setPreferredSize(QSizeF(348, 45)); |
| 170 |
title->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 171 |
|
| 172 |
place->setPreferredSize(QSizeF(96, 72)); |
| 173 |
place->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 174 |
|
| 175 |
details->setMinimumSize(QSizeF(200, 112)); |
| 176 |
details->setPreferredSize(QSizeF(200, 112)); |
| 177 |
details->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 178 |
|
| 179 |
tabbar->setPreferredSize(QSizeF(70, 24)); |
| 180 |
tabbar->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 181 |
|
| 182 |
sunnyWeather->setPreferredSize(QSizeF(128, 97)); |
| 183 |
sunnyWeather->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 184 |
sunnyWeather->setZValue(9999); |
| 185 |
|
| 186 |
// start anchor layout |
| 187 |
QGraphicsAnchorLayout *layout = new QGraphicsAnchorLayout; |
| 188 |
layout->setSpacing(0); |
| 189 |
|
| 190 |
// setup the main widget |
| 191 |
QGraphicsWidget *widget = new QGraphicsWidget(nullptr, Qt::Window); |
| 192 |
QPalette p; |
| 193 |
p.setColor(QPalette::Window, Qt::black); |
| 194 |
widget->setPalette(p); |
| 195 |
widget->setPos(20, 20); |
| 196 |
widget->setLayout(layout); |
| 197 |
|
| 198 |
// vertical anchors |
| 199 |
QGraphicsAnchor *anchor = layout->addAnchor(title, Qt::AnchorTop, layout, Qt::AnchorTop); |
| 200 |
anchor = layout->addAnchor(place, Qt::AnchorTop, title, Qt::AnchorBottom); |
| 201 |
anchor->setSpacing(12); |
| 202 |
anchor = layout->addAnchor(place, Qt::AnchorBottom, layout, Qt::AnchorBottom); |
| 203 |
anchor->setSpacing(12); |
| 204 |
|
| 205 |
anchor = layout->addAnchor(sunnyWeather, Qt::AnchorTop, title, Qt::AnchorTop); |
| 206 |
anchor = layout->addAnchor(sunnyWeather, Qt::AnchorBottom, layout, Qt::AnchorVerticalCenter); |
| 207 |
|
| 208 |
anchor = layout->addAnchor(tabbar, Qt::AnchorTop, title, Qt::AnchorBottom); |
| 209 |
anchor->setSpacing(5); |
| 210 |
anchor = layout->addAnchor(details, Qt::AnchorTop, tabbar, Qt::AnchorBottom); |
| 211 |
anchor->setSpacing(2); |
| 212 |
anchor = layout->addAnchor(details, Qt::AnchorBottom, layout, Qt::AnchorBottom); |
| 213 |
anchor->setSpacing(12); |
| 214 |
|
| 215 |
// horizontal anchors |
| 216 |
anchor = layout->addAnchor(layout, Qt::AnchorLeft, title, Qt::AnchorLeft); |
| 217 |
anchor = layout->addAnchor(title, Qt::AnchorRight, layout, Qt::AnchorRight); |
| 218 |
|
| 219 |
anchor = layout->addAnchor(place, Qt::AnchorLeft, layout, Qt::AnchorLeft); |
| 220 |
anchor->setSpacing(15); |
| 221 |
anchor = layout->addAnchor(place, Qt::AnchorRight, details, Qt::AnchorLeft); |
| 222 |
anchor->setSpacing(35); |
| 223 |
|
| 224 |
anchor = layout->addAnchor(sunnyWeather, Qt::AnchorLeft, place, Qt::AnchorHorizontalCenter); |
| 225 |
anchor = layout->addAnchor(sunnyWeather, Qt::AnchorRight, layout, Qt::AnchorHorizontalCenter); |
| 226 |
|
| 227 |
anchor = layout->addAnchor(tabbar, Qt::AnchorHorizontalCenter, details, Qt::AnchorHorizontalCenter); |
| 228 |
anchor = layout->addAnchor(details, Qt::AnchorRight, layout, Qt::AnchorRight); |
| 229 |
|
| 230 |
// QGV setup |
| 231 |
scene.addItem(widget); |
| 232 |
scene.setBackgroundBrush(Qt::white); |
| 233 |
QGraphicsView *view = new QGraphicsView(&scene); |
| 234 |
view->show(); |
| 235 |
|
| 236 |
return app.exec(); |
| 237 |
} |
| 238 |
|
| 239 |
#include "main.moc" |