| 1 |
// Copyright (C) 2017 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include <QtWidgets> |
| 5 |
#include "displaywidget.h" |
| 6 |
|
| 7 |
DisplayWidget::DisplayWidget(QWidget *parent) |
| 8 |
: QWidget(parent) |
| 9 |
{ |
| 10 |
QPainterPath car; |
| 11 |
QPainterPath house; |
| 12 |
|
| 13 |
QFile file(":resources/shapes.dat"); |
| 14 |
file.open(QFile::ReadOnly); |
| 15 |
QDataStream stream(&file); |
| 16 |
stream >> car >> house >> tree >> moon; |
| 17 |
file.close(); |
| 18 |
|
| 19 |
shapeMap[Car] = car; |
| 20 |
shapeMap[House] = house; |
| 21 |
|
| 22 |
background = Sky; |
| 23 |
shapeColor = Qt::darkYellow; |
| 24 |
shape = House; |
| 25 |
} |
| 26 |
|
| 27 |
//! [paint event] |
| 28 |
void DisplayWidget::paintEvent(QPaintEvent * /* event */) |
| 29 |
{ |
| 30 |
QPainter painter; |
| 31 |
painter.begin(this); |
| 32 |
painter.setRenderHint(QPainter::Antialiasing); |
| 33 |
paint(painter); |
| 34 |
painter.end(); |
| 35 |
} |
| 36 |
//! [paint event] |
| 37 |
|
| 38 |
//! [paint function] |
| 39 |
void DisplayWidget::paint(QPainter &painter) |
| 40 |
{ |
| 41 |
//![paint picture] |
| 42 |
painter.setClipRect(QRect(0, 0, 200, 200)); |
| 43 |
painter.setPen(Qt::NoPen); |
| 44 |
|
| 45 |
switch (background) { |
| 46 |
case Sky: |
| 47 |
default: |
| 48 |
painter.fillRect(QRect(0, 0, 200, 200), Qt::darkBlue); |
| 49 |
painter.translate(145, 10); |
| 50 |
painter.setBrush(Qt::white); |
| 51 |
painter.drawPath(moon); |
| 52 |
painter.translate(-145, -10); |
| 53 |
break; |
| 54 |
case Trees: |
| 55 |
{ |
| 56 |
painter.fillRect(QRect(0, 0, 200, 200), Qt::darkGreen); |
| 57 |
painter.setBrush(Qt::green); |
| 58 |
painter.setPen(Qt::black); |
| 59 |
for (int y = -55, row = 0; y < 200; y += 50, ++row) { |
| 60 |
int xs; |
| 61 |
if (row == 2 || row == 3) |
| 62 |
xs = 150; |
| 63 |
else |
| 64 |
xs = 50; |
| 65 |
for (int x = 0; x < 200; x += xs) { |
| 66 |
painter.save(); |
| 67 |
painter.translate(x, y); |
| 68 |
painter.drawPath(tree); |
| 69 |
painter.restore(); |
| 70 |
} |
| 71 |
} |
| 72 |
break; |
| 73 |
} |
| 74 |
case Road: |
| 75 |
painter.fillRect(QRect(0, 0, 200, 200), Qt::gray); |
| 76 |
painter.setPen(QPen(Qt::white, 4, Qt::DashLine)); |
| 77 |
painter.drawLine(QLine(0, 35, 200, 35)); |
| 78 |
painter.drawLine(QLine(0, 165, 200, 165)); |
| 79 |
break; |
| 80 |
} |
| 81 |
|
| 82 |
painter.setBrush(shapeColor); |
| 83 |
painter.setPen(Qt::black); |
| 84 |
painter.translate(100, 100); |
| 85 |
painter.drawPath(shapeMap[shape]); |
| 86 |
//![paint picture] |
| 87 |
} |
| 88 |
//! [paint function] |
| 89 |
|
| 90 |
QColor DisplayWidget::color() const |
| 91 |
{ |
| 92 |
return shapeColor; |
| 93 |
} |
| 94 |
|
| 95 |
void DisplayWidget::setBackground(Background background) |
| 96 |
{ |
| 97 |
this->background = background; |
| 98 |
update(); |
| 99 |
} |
| 100 |
|
| 101 |
void DisplayWidget::setColor(const QColor &color) |
| 102 |
{ |
| 103 |
this->shapeColor = color; |
| 104 |
update(); |
| 105 |
} |
| 106 |
|
| 107 |
void DisplayWidget::setShape(Shape shape) |
| 108 |
{ |
| 109 |
this->shape = shape; |
| 110 |
update(); |
| 111 |
} |