~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to examples/painting/basicdrawing/renderarea.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2005-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the example classes of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include <QtGui>
 
30
 
 
31
#include "renderarea.h"
 
32
 
 
33
RenderArea::RenderArea(QWidget *parent)
 
34
    : QWidget(parent)
 
35
{
 
36
    shape = Polygon;
 
37
    antialiased = false;
 
38
    pixmap.load(":/images/qt-logo.png");
 
39
 
 
40
    setBackgroundRole(QPalette::Base);
 
41
}
 
42
 
 
43
QSize RenderArea::minimumSizeHint() const
 
44
{
 
45
    return QSize(100, 100);
 
46
}
 
47
 
 
48
QSize RenderArea::sizeHint() const
 
49
{
 
50
    return QSize(400, 200);
 
51
}
 
52
 
 
53
void RenderArea::setShape(Shape shape)
 
54
{
 
55
    this->shape = shape;
 
56
    update();
 
57
}
 
58
 
 
59
void RenderArea::setPen(const QPen &pen)
 
60
{
 
61
    this->pen = pen;
 
62
    update();
 
63
}
 
64
 
 
65
void RenderArea::setBrush(const QBrush &brush)
 
66
{
 
67
    this->brush = brush;
 
68
    update();
 
69
}
 
70
 
 
71
void RenderArea::setAntialiased(bool antialiased)
 
72
{
 
73
    this->antialiased = antialiased;
 
74
    update();
 
75
}
 
76
 
 
77
void RenderArea::setTransformed(bool transformed)
 
78
{
 
79
    this->transformed = transformed;
 
80
    update();
 
81
}
 
82
 
 
83
void RenderArea::paintEvent(QPaintEvent *)
 
84
{
 
85
    static const int polygonPoints[8] = { 10, 80, 20, 10, 80, 30, 90, 70 };
 
86
    QPolygon polygon(4, polygonPoints);
 
87
 
 
88
    QRect rect(10, 20, 80, 60);
 
89
 
 
90
    QPainterPath path;
 
91
    path.moveTo(20, 80);
 
92
    path.lineTo(20, 30);
 
93
    path.cubicTo(80, 0, 50, 50, 80, 80);
 
94
 
 
95
    int startAngle = 30 * 16;
 
96
    int arcLength = 120 * 16;
 
97
 
 
98
    QPainter painter(this);
 
99
    painter.setPen(pen);
 
100
    painter.setBrush(brush);
 
101
    if (antialiased)
 
102
        painter.setRenderHint(QPainter::Antialiasing);
 
103
 
 
104
    for (int x = 0; x < width(); x += 100) {
 
105
        for (int y = 0; y < height(); y += 100) {
 
106
            painter.save();
 
107
            painter.translate(x, y);
 
108
            if (transformed) {
 
109
                painter.translate(50, 50);
 
110
                painter.rotate(60.0);
 
111
                painter.scale(0.6, 0.9);
 
112
                painter.translate(-50, -50);
 
113
            }
 
114
 
 
115
            switch (shape) {
 
116
            case Line:
 
117
                painter.drawLine(rect.bottomLeft(), rect.topRight());
 
118
                break;
 
119
            case Points:
 
120
                painter.drawPoints(polygon);
 
121
                break;
 
122
            case Polyline:
 
123
                painter.drawPolyline(polygon);
 
124
                break;
 
125
            case Polygon:
 
126
                painter.drawPolygon(polygon);
 
127
                break;
 
128
            case Rect:
 
129
                painter.drawRect(rect);
 
130
                break;
 
131
            case RoundRect:
 
132
                painter.drawRoundRect(rect);
 
133
                break;
 
134
            case Ellipse:
 
135
                painter.drawEllipse(rect);
 
136
                break;
 
137
            case Arc:
 
138
                painter.drawArc(rect, startAngle, arcLength);
 
139
                break;
 
140
            case Chord:
 
141
                painter.drawChord(rect, startAngle, arcLength);
 
142
                break;
 
143
            case Pie:
 
144
                painter.drawPie(rect, startAngle, arcLength);
 
145
                break;
 
146
            case Path:
 
147
                painter.drawPath(path);
 
148
                break;
 
149
            case Text:
 
150
                painter.drawText(rect, Qt::AlignCenter, tr("Qt by\nTrolltech"));
 
151
                break;
 
152
            case Pixmap:
 
153
                painter.drawPixmap(10, 10, pixmap);
 
154
            }
 
155
            painter.restore();
 
156
        }
 
157
    }
 
158
}