1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include <QtGui>
|
---|
43 | #include "displaywidget.h"
|
---|
44 |
|
---|
45 | DisplayWidget::DisplayWidget(QWidget *parent)
|
---|
46 | : QWidget(parent)
|
---|
47 | {
|
---|
48 | QPainterPath car;
|
---|
49 | QPainterPath house;
|
---|
50 |
|
---|
51 | QFile file(":resources/shapes.dat");
|
---|
52 | file.open(QFile::ReadOnly);
|
---|
53 | QDataStream stream(&file);
|
---|
54 | stream >> car >> house >> tree >> moon;
|
---|
55 | file.close();
|
---|
56 |
|
---|
57 | shapeMap[Car] = car;
|
---|
58 | shapeMap[House] = house;
|
---|
59 |
|
---|
60 | background = Sky;
|
---|
61 | shapeColor = Qt::darkYellow;
|
---|
62 | shape = House;
|
---|
63 | }
|
---|
64 |
|
---|
65 | //! [paint event]
|
---|
66 | void DisplayWidget::paintEvent(QPaintEvent *event)
|
---|
67 | {
|
---|
68 | QPainter painter;
|
---|
69 | painter.begin(this);
|
---|
70 | painter.setRenderHint(QPainter::Antialiasing);
|
---|
71 | paint(painter);
|
---|
72 | painter.end();
|
---|
73 | }
|
---|
74 | //! [paint event]
|
---|
75 |
|
---|
76 | //! [paint function]
|
---|
77 | void DisplayWidget::paint(QPainter &painter)
|
---|
78 | {
|
---|
79 | //![paint picture]
|
---|
80 | painter.setClipRect(QRect(0, 0, 200, 200));
|
---|
81 | painter.setPen(Qt::NoPen);
|
---|
82 |
|
---|
83 | switch (background) {
|
---|
84 | case Sky:
|
---|
85 | default:
|
---|
86 | painter.fillRect(QRect(0, 0, 200, 200), Qt::darkBlue);
|
---|
87 | painter.translate(145, 10);
|
---|
88 | painter.setBrush(Qt::white);
|
---|
89 | painter.drawPath(moon);
|
---|
90 | painter.translate(-145, -10);
|
---|
91 | break;
|
---|
92 | case Trees:
|
---|
93 | {
|
---|
94 | painter.fillRect(QRect(0, 0, 200, 200), Qt::darkGreen);
|
---|
95 | painter.setBrush(Qt::green);
|
---|
96 | painter.setPen(Qt::black);
|
---|
97 | for (int y = -55, row = 0; y < 200; y += 50, ++row) {
|
---|
98 | int xs;
|
---|
99 | if (row == 2 || row == 3)
|
---|
100 | xs = 150;
|
---|
101 | else
|
---|
102 | xs = 50;
|
---|
103 | for (int x = 0; x < 200; x += xs) {
|
---|
104 | painter.save();
|
---|
105 | painter.translate(x, y);
|
---|
106 | painter.drawPath(tree);
|
---|
107 | painter.restore();
|
---|
108 | }
|
---|
109 | }
|
---|
110 | break;
|
---|
111 | }
|
---|
112 | case Road:
|
---|
113 | painter.fillRect(QRect(0, 0, 200, 200), Qt::gray);
|
---|
114 | painter.setPen(QPen(Qt::white, 4, Qt::DashLine));
|
---|
115 | painter.drawLine(QLine(0, 35, 200, 35));
|
---|
116 | painter.drawLine(QLine(0, 165, 200, 165));
|
---|
117 | break;
|
---|
118 | }
|
---|
119 |
|
---|
120 | painter.setBrush(shapeColor);
|
---|
121 | painter.setPen(Qt::black);
|
---|
122 | painter.translate(100, 100);
|
---|
123 | painter.drawPath(shapeMap[shape]);
|
---|
124 | //![paint picture]
|
---|
125 | }
|
---|
126 | //! [paint function]
|
---|
127 |
|
---|
128 | QColor DisplayWidget::color() const
|
---|
129 | {
|
---|
130 | return shapeColor;
|
---|
131 | }
|
---|
132 |
|
---|
133 | void DisplayWidget::setBackground(Background background)
|
---|
134 | {
|
---|
135 | this->background = background;
|
---|
136 | update();
|
---|
137 | }
|
---|
138 |
|
---|
139 | void DisplayWidget::setColor(const QColor &color)
|
---|
140 | {
|
---|
141 | this->shapeColor = color;
|
---|
142 | update();
|
---|
143 | }
|
---|
144 |
|
---|
145 | void DisplayWidget::setShape(Shape shape)
|
---|
146 | {
|
---|
147 | this->shape = shape;
|
---|
148 | update();
|
---|
149 | }
|
---|