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

« back to all changes in this revision

Viewing changes to examples/painting/transformations/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
    QFont newFont = font();
 
37
    newFont.setPixelSize(12);
 
38
    setFont(newFont);
 
39
 
 
40
    QFontMetrics fontMetrics(newFont);
 
41
    xBoundingRect = fontMetrics.boundingRect(tr("x"));
 
42
    yBoundingRect = fontMetrics.boundingRect(tr("y"));
 
43
}
 
44
 
 
45
void RenderArea::setOperations(const QList<Operation> &operations)
 
46
{
 
47
    this->operations = operations;
 
48
    update();
 
49
}
 
50
 
 
51
void RenderArea::setShape(const QPainterPath &shape)
 
52
{
 
53
    this->shape = shape;
 
54
    update();
 
55
}
 
56
 
 
57
QSize RenderArea::minimumSizeHint() const
 
58
{
 
59
    return QSize(50, 50);
 
60
}
 
61
 
 
62
QSize RenderArea::sizeHint() const
 
63
{
 
64
    return QSize(232, 232);
 
65
}
 
66
 
 
67
void RenderArea::paintEvent(QPaintEvent *event)
 
68
{
 
69
    QPainter painter(this);
 
70
    painter.setRenderHint(QPainter::Antialiasing);
 
71
    painter.fillRect(event->rect(), QBrush(Qt::white));
 
72
 
 
73
    painter.translate(66, 66);
 
74
 
 
75
    painter.save();
 
76
    transformPainter(painter);
 
77
    drawShape(painter);
 
78
    painter.restore();
 
79
 
 
80
    drawOutline(painter);
 
81
 
 
82
    painter.save();
 
83
    transformPainter(painter);
 
84
    drawCoordinates(painter);
 
85
    painter.restore();
 
86
}
 
87
 
 
88
void RenderArea::drawCoordinates(QPainter &painter)
 
89
{
 
90
    painter.setPen(Qt::red);
 
91
 
 
92
    painter.drawLine(0, 0, 50, 0);
 
93
    painter.drawLine(48, -2, 50, 0);
 
94
    painter.drawLine(48, 2, 50, 0);
 
95
    painter.drawText(60 - xBoundingRect.width() / 2,
 
96
                     0 + xBoundingRect.height() / 2, tr("x"));
 
97
 
 
98
    painter.drawLine(0, 0, 0, 50);
 
99
    painter.drawLine(-2, 48, 0, 50);
 
100
    painter.drawLine(2, 48, 0, 50);
 
101
    painter.drawText(0 - yBoundingRect.width() / 2,
 
102
                     60 + yBoundingRect.height() / 2, tr("y"));
 
103
}
 
104
 
 
105
void RenderArea::drawOutline(QPainter &painter)
 
106
{
 
107
    painter.setPen(Qt::darkGreen);
 
108
    painter.setPen(Qt::DashLine);
 
109
    painter.setBrush(Qt::NoBrush);
 
110
    painter.drawRect(0, 0, 100, 100);
 
111
}
 
112
 
 
113
void RenderArea::drawShape(QPainter &painter)
 
114
{
 
115
    painter.fillPath(shape, Qt::blue);
 
116
}
 
117
 
 
118
void RenderArea::transformPainter(QPainter &painter)
 
119
{
 
120
    for (int i = 0; i < operations.size(); ++i) {
 
121
        switch (operations[i]) {
 
122
        case Translate:
 
123
            painter.translate(50, 50);
 
124
            break;
 
125
        case Scale:
 
126
            painter.scale(0.75, 0.75);
 
127
            break;
 
128
        case Rotate:
 
129
            painter.rotate(60);
 
130
            break;
 
131
        case NoTransformation:
 
132
        default:
 
133
            ;
 
134
        }
 
135
    }
 
136
}