source: trunk/examples/painting/svggenerator/displaywidget.cpp@ 561

Last change on this file since 561 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 4.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** 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 have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include "displaywidget.h"
44
45DisplayWidget::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]
66void 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]
77void 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
128QColor DisplayWidget::color() const
129{
130 return shapeColor;
131}
132
133void DisplayWidget::setBackground(Background background)
134{
135 this->background = background;
136 update();
137}
138
139void DisplayWidget::setColor(const QColor &color)
140{
141 this->shapeColor = color;
142 update();
143}
144
145void DisplayWidget::setShape(Shape shape)
146{
147 this->shape = shape;
148 update();
149}
Note: See TracBrowser for help on using the repository browser.