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

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 4.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#include <QtGui>
42#include "displaywidget.h"
43
44DisplayWidget::DisplayWidget(QWidget *parent)
45 : QWidget(parent)
46{
47 QPainterPath car;
48 QPainterPath house;
49
50 QFile file(":resources/shapes.dat");
51 file.open(QFile::ReadOnly);
52 QDataStream stream(&file);
53 stream >> car >> house >> tree >> moon;
54 file.close();
55
56 shapeMap[Car] = car;
57 shapeMap[House] = house;
58
59 background = Sky;
60 shapeColor = Qt::darkYellow;
61 shape = House;
62}
63
64//! [paint event]
65void DisplayWidget::paintEvent(QPaintEvent * /* event */)
66{
67 QPainter painter;
68 painter.begin(this);
69 painter.setRenderHint(QPainter::Antialiasing);
70 paint(painter);
71 painter.end();
72}
73//! [paint event]
74
75//! [paint function]
76void DisplayWidget::paint(QPainter &painter)
77{
78//![paint picture]
79 painter.setClipRect(QRect(0, 0, 200, 200));
80 painter.setPen(Qt::NoPen);
81
82 switch (background) {
83 case Sky:
84 default:
85 painter.fillRect(QRect(0, 0, 200, 200), Qt::darkBlue);
86 painter.translate(145, 10);
87 painter.setBrush(Qt::white);
88 painter.drawPath(moon);
89 painter.translate(-145, -10);
90 break;
91 case Trees:
92 {
93 painter.fillRect(QRect(0, 0, 200, 200), Qt::darkGreen);
94 painter.setBrush(Qt::green);
95 painter.setPen(Qt::black);
96 for (int y = -55, row = 0; y < 200; y += 50, ++row) {
97 int xs;
98 if (row == 2 || row == 3)
99 xs = 150;
100 else
101 xs = 50;
102 for (int x = 0; x < 200; x += xs) {
103 painter.save();
104 painter.translate(x, y);
105 painter.drawPath(tree);
106 painter.restore();
107 }
108 }
109 break;
110 }
111 case Road:
112 painter.fillRect(QRect(0, 0, 200, 200), Qt::gray);
113 painter.setPen(QPen(Qt::white, 4, Qt::DashLine));
114 painter.drawLine(QLine(0, 35, 200, 35));
115 painter.drawLine(QLine(0, 165, 200, 165));
116 break;
117 }
118
119 painter.setBrush(shapeColor);
120 painter.setPen(Qt::black);
121 painter.translate(100, 100);
122 painter.drawPath(shapeMap[shape]);
123//![paint picture]
124}
125//! [paint function]
126
127QColor DisplayWidget::color() const
128{
129 return shapeColor;
130}
131
132void DisplayWidget::setBackground(Background background)
133{
134 this->background = background;
135 update();
136}
137
138void DisplayWidget::setColor(const QColor &color)
139{
140 this->shapeColor = color;
141 update();
142}
143
144void DisplayWidget::setShape(Shape shape)
145{
146 this->shape = shape;
147 update();
148}
Note: See TracBrowser for help on using the repository browser.