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

« back to all changes in this revision

Viewing changes to examples/painting/basicdrawing/window.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
#include "window.h"
 
33
 
 
34
const int IdRole = Qt::UserRole;
 
35
 
 
36
Window::Window()
 
37
{
 
38
    renderArea = new RenderArea;
 
39
 
 
40
    shapeComboBox = new QComboBox;
 
41
    shapeComboBox->addItem(tr("Rectangle"), RenderArea::Rect);
 
42
    shapeComboBox->addItem(tr("Round Rectangle"), RenderArea::RoundRect);
 
43
    shapeComboBox->addItem(tr("Ellipse"), RenderArea::Ellipse);
 
44
    shapeComboBox->addItem(tr("Pie"), RenderArea::Pie);
 
45
    shapeComboBox->addItem(tr("Chord"), RenderArea::Chord);
 
46
    shapeComboBox->addItem(tr("Polygon"), RenderArea::Polygon);
 
47
    shapeComboBox->addItem(tr("Path"), RenderArea::Path);
 
48
    shapeComboBox->addItem(tr("Line"), RenderArea::Line);
 
49
    shapeComboBox->addItem(tr("Polyline"), RenderArea::Polyline);
 
50
    shapeComboBox->addItem(tr("Arc"), RenderArea::Arc);
 
51
    shapeComboBox->addItem(tr("Points"), RenderArea::Points);
 
52
    shapeComboBox->addItem(tr("Text"), RenderArea::Text);
 
53
    shapeComboBox->addItem(tr("Pixmap"), RenderArea::Pixmap);
 
54
 
 
55
    shapeLabel = new QLabel(tr("&Shape:"));
 
56
    shapeLabel->setBuddy(shapeComboBox);
 
57
 
 
58
    penWidthSpinBox = new QSpinBox;
 
59
    penWidthSpinBox->setRange(0, 20);
 
60
 
 
61
    penWidthLabel = new QLabel(tr("Pen &Width:"));
 
62
    penWidthLabel->setBuddy(penWidthSpinBox);
 
63
 
 
64
    penStyleComboBox = new QComboBox;
 
65
    penStyleComboBox->addItem(tr("Solid"), Qt::SolidLine);
 
66
    penStyleComboBox->addItem(tr("Dash"), Qt::DashLine);
 
67
    penStyleComboBox->addItem(tr("Dot"), Qt::DotLine);
 
68
    penStyleComboBox->addItem(tr("Dash Dot"), Qt::DashDotLine);
 
69
    penStyleComboBox->addItem(tr("Dash Dot Dot"), Qt::DashDotDotLine);
 
70
    penStyleComboBox->addItem(tr("None"), Qt::NoPen);
 
71
 
 
72
    penStyleLabel = new QLabel(tr("&Pen Style:"));
 
73
    penStyleLabel->setBuddy(penStyleComboBox);
 
74
 
 
75
    penCapComboBox = new QComboBox;
 
76
    penCapComboBox->addItem(tr("Flat"), Qt::FlatCap);
 
77
    penCapComboBox->addItem(tr("Square"), Qt::SquareCap);
 
78
    penCapComboBox->addItem(tr("Round"), Qt::RoundCap);
 
79
 
 
80
    penCapLabel = new QLabel(tr("Pen &Cap:"));
 
81
    penCapLabel->setBuddy(penCapComboBox);
 
82
 
 
83
    penJoinComboBox = new QComboBox;
 
84
    penJoinComboBox->addItem(tr("Miter"), Qt::MiterJoin);
 
85
    penJoinComboBox->addItem(tr("Bevel"), Qt::BevelJoin);
 
86
    penJoinComboBox->addItem(tr("Round"), Qt::RoundJoin);
 
87
 
 
88
    penJoinLabel = new QLabel(tr("Pen &Join:"));
 
89
    penJoinLabel->setBuddy(penJoinComboBox);
 
90
 
 
91
    brushStyleComboBox = new QComboBox;
 
92
    brushStyleComboBox->addItem(tr("Linear Gradient"),
 
93
            Qt::LinearGradientPattern);
 
94
    brushStyleComboBox->addItem(tr("Radial Gradient"),
 
95
            Qt::RadialGradientPattern);
 
96
    brushStyleComboBox->addItem(tr("Conical Gradient"),
 
97
            Qt::ConicalGradientPattern);
 
98
    brushStyleComboBox->addItem(tr("Texture"), Qt::TexturePattern);
 
99
    brushStyleComboBox->addItem(tr("Solid"), Qt::SolidPattern);
 
100
    brushStyleComboBox->addItem(tr("Horizontal"), Qt::HorPattern);
 
101
    brushStyleComboBox->addItem(tr("Vertical"), Qt::VerPattern);
 
102
    brushStyleComboBox->addItem(tr("Cross"), Qt::CrossPattern);
 
103
    brushStyleComboBox->addItem(tr("Backward Diagonal"), Qt::BDiagPattern);
 
104
    brushStyleComboBox->addItem(tr("Forward Diagonal"), Qt::FDiagPattern);
 
105
    brushStyleComboBox->addItem(tr("Diagonal Cross"), Qt::DiagCrossPattern);
 
106
    brushStyleComboBox->addItem(tr("Dense 1"), Qt::Dense1Pattern);
 
107
    brushStyleComboBox->addItem(tr("Dense 2"), Qt::Dense2Pattern);
 
108
    brushStyleComboBox->addItem(tr("Dense 3"), Qt::Dense3Pattern);
 
109
    brushStyleComboBox->addItem(tr("Dense 4"), Qt::Dense4Pattern);
 
110
    brushStyleComboBox->addItem(tr("Dense 5"), Qt::Dense5Pattern);
 
111
    brushStyleComboBox->addItem(tr("Dense 6"), Qt::Dense6Pattern);
 
112
    brushStyleComboBox->addItem(tr("Dense 7"), Qt::Dense7Pattern);
 
113
    brushStyleComboBox->addItem(tr("None"), Qt::NoBrush);
 
114
 
 
115
    brushStyleLabel = new QLabel(tr("&Brush Style:"));
 
116
    brushStyleLabel->setBuddy(brushStyleComboBox);
 
117
 
 
118
    antialiasingCheckBox = new QCheckBox(tr("&Antialiasing"));
 
119
    transformationsCheckBox = new QCheckBox(tr("&Transformations"));
 
120
 
 
121
    connect(shapeComboBox, SIGNAL(activated(int)),
 
122
            this, SLOT(shapeChanged()));
 
123
    connect(penWidthSpinBox, SIGNAL(valueChanged(int)),
 
124
            this, SLOT(penChanged()));
 
125
    connect(penStyleComboBox, SIGNAL(activated(int)),
 
126
            this, SLOT(penChanged()));
 
127
    connect(penCapComboBox, SIGNAL(activated(int)),
 
128
            this, SLOT(penChanged()));
 
129
    connect(penJoinComboBox, SIGNAL(activated(int)),
 
130
            this, SLOT(penChanged()));
 
131
    connect(brushStyleComboBox, SIGNAL(activated(int)),
 
132
            this, SLOT(brushChanged()));
 
133
    connect(antialiasingCheckBox, SIGNAL(toggled(bool)),
 
134
            renderArea, SLOT(setAntialiased(bool)));
 
135
    connect(transformationsCheckBox, SIGNAL(toggled(bool)),
 
136
            renderArea, SLOT(setTransformed(bool)));
 
137
 
 
138
    QHBoxLayout *checkBoxLayout = new QHBoxLayout;
 
139
    checkBoxLayout->addWidget(antialiasingCheckBox);
 
140
    checkBoxLayout->addWidget(transformationsCheckBox);
 
141
 
 
142
    QGridLayout *mainLayout = new QGridLayout;
 
143
    mainLayout->addWidget(renderArea, 0, 0, 1, 2);
 
144
    mainLayout->addWidget(shapeLabel, 1, 0);
 
145
    mainLayout->addWidget(shapeComboBox, 1, 1);
 
146
    mainLayout->addWidget(penWidthLabel, 2, 0);
 
147
    mainLayout->addWidget(penWidthSpinBox, 2, 1);
 
148
    mainLayout->addWidget(penStyleLabel, 3, 0);
 
149
    mainLayout->addWidget(penStyleComboBox, 3, 1);
 
150
    mainLayout->addWidget(penCapLabel, 4, 0);
 
151
    mainLayout->addWidget(penCapComboBox, 4, 1);
 
152
    mainLayout->addWidget(penJoinLabel, 5, 0);
 
153
    mainLayout->addWidget(penJoinComboBox, 5, 1);
 
154
    mainLayout->addWidget(brushStyleLabel, 6, 0);
 
155
    mainLayout->addWidget(brushStyleComboBox, 6, 1);
 
156
    mainLayout->addLayout(checkBoxLayout, 7, 0, 1, 2);
 
157
    setLayout(mainLayout);
 
158
 
 
159
    shapeChanged();
 
160
    penChanged();
 
161
    brushChanged();
 
162
    penChanged();
 
163
    renderArea->setAntialiased(false);
 
164
    renderArea->setTransformed(false);
 
165
 
 
166
    setWindowTitle(tr("Basic Drawing"));
 
167
}
 
168
 
 
169
void Window::shapeChanged()
 
170
{
 
171
    RenderArea::Shape shape =
 
172
        (RenderArea::Shape)shapeComboBox->itemData(shapeComboBox->currentIndex(), IdRole).toInt();
 
173
    renderArea->setShape(shape);
 
174
}
 
175
 
 
176
void Window::penChanged()
 
177
{
 
178
    int width = penWidthSpinBox->value();
 
179
    Qt::PenStyle style =
 
180
        (Qt::PenStyle)penStyleComboBox->itemData(penStyleComboBox->currentIndex(), IdRole).toInt();
 
181
    Qt::PenCapStyle cap =
 
182
        (Qt::PenCapStyle)penCapComboBox->itemData(penCapComboBox->currentIndex(), IdRole).toInt();
 
183
    Qt::PenJoinStyle join =
 
184
        (Qt::PenJoinStyle)penJoinComboBox->itemData(penJoinComboBox->currentIndex(), IdRole).toInt();
 
185
 
 
186
    renderArea->setPen(QPen(Qt::blue, width, style, cap, join));
 
187
}
 
188
 
 
189
void Window::brushChanged()
 
190
{
 
191
    Qt::BrushStyle style =
 
192
        (Qt::BrushStyle)brushStyleComboBox->itemData(brushStyleComboBox->currentIndex(), IdRole).toInt();
 
193
 
 
194
    if (style == Qt::LinearGradientPattern) {
 
195
        QLinearGradient linearGradient(0, 0, 100, 100);
 
196
        linearGradient.setColorAt(0.0, Qt::white);
 
197
        linearGradient.setColorAt(0.2, Qt::green);
 
198
        linearGradient.setColorAt(1.0, Qt::black);
 
199
        renderArea->setBrush(linearGradient);
 
200
    } else if (style == Qt::RadialGradientPattern) {
 
201
        QRadialGradient radialGradient(50, 50, 50, 50, 50);
 
202
        radialGradient.setColorAt(0.0, Qt::white);
 
203
        radialGradient.setColorAt(0.2, Qt::green);
 
204
        radialGradient.setColorAt(1.0, Qt::black);
 
205
        renderArea->setBrush(radialGradient);
 
206
    } else if (style == Qt::ConicalGradientPattern) {
 
207
        QConicalGradient conicalGradient(50, 50, 150);
 
208
        conicalGradient.setColorAt(0.0, Qt::white);
 
209
        conicalGradient.setColorAt(0.2, Qt::green);
 
210
        conicalGradient.setColorAt(1.0, Qt::black);
 
211
        renderArea->setBrush(conicalGradient);
 
212
    } else if (style == Qt::TexturePattern) {
 
213
        renderArea->setBrush(QBrush(QPixmap(":/images/brick.png")));
 
214
    } else {
 
215
        renderArea->setBrush(QBrush(Qt::red, style));
 
216
    }
 
217
}