|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the examples of the Qt Toolkit. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
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 } |