1 /****************************************************************************
3 ** Copyright (C) 2008 Trolltech ASA. All rights reserved.
5 ** This file is part of the Qt Script Generator project on Trolltech Labs.
7 ** This file may be used under the terms of the GNU General Public
8 ** License version 2.0 as published by the Free Software Foundation
9 ** and appearing in the file LICENSE.GPL included in the packaging of
10 ** this file. Please review the following information to ensure GNU
11 ** General Public Licensing requirements will be met:
12 ** http://www.trolltech.com/products/qt/opensource.html
14 ** If you are unsure which license is appropriate for your use, please
15 ** review the following information:
16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
17 ** sales department at sales@trolltech.com.
19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 ****************************************************************************/
24 function tr(s) { return s; }
28 function RenderArea(path, parent)
30 QWidget.call(this, parent);
34 this.rotationAngle = 0;
35 this.setBackgroundRole(QPalette.Base);
38 RenderArea.prototype = new QWidget();
40 RenderArea.prototype.getMinimumSizeHint = function()
42 return new QSize(50, 50);
45 RenderArea.prototype.getSizeHint = function()
47 return new QSize(100, 100);
50 RenderArea.prototype.setFillRule = function(rule)
52 this.path.setFillRule(rule);
56 RenderArea.prototype.setFillGradient = function(color1, color2)
58 this.fillColor1 = color1;
59 this.fillColor2 = color2;
63 RenderArea.prototype.setPenWidth = function(width)
65 this.penWidth = width;
69 RenderArea.prototype.setPenColor = function(color)
71 this.penColor = color;
75 RenderArea.prototype.setRotationAngle = function(degrees)
77 this.rotationAngle = degrees;
81 RenderArea.prototype.paintEvent = function()
83 var painter = new QPainter();
85 painter.setRenderHint(QPainter.Antialiasing);
86 painter.scale(this.width / 100.0, this.height / 100.0);
87 painter.translate(50.0, 50.0);
88 painter.rotate(-this.rotationAngle);
89 painter.translate(-50.0, -50.0);
91 painter.setPen(new QPen(this.penColor, this.penWidth, Qt.SolidLine, Qt.RoundCap,
93 var gradient = new QLinearGradient(0, 0, 0, 100);
94 gradient.setColorAt(0.0, this.fillColor1);
95 gradient.setColorAt(1.0, this.fillColor2);
96 painter.setBrush(new QBrush(gradient));
97 painter.drawPath(this.path);
103 function Window(parent)
105 QWidget.call(this, parent);
107 var rectPath = new QPainterPath();
108 rectPath.moveTo(20.0, 30.0);
109 rectPath.lineTo(80.0, 30.0);
110 rectPath.lineTo(80.0, 70.0);
111 rectPath.lineTo(20.0, 70.0);
112 rectPath.closeSubpath();
114 var roundRectPath = new QPainterPath();
115 roundRectPath.moveTo(80.0, 35.0);
116 roundRectPath.arcTo(70.0, 30.0, 10.0, 10.0, 0.0, 90.0);
117 roundRectPath.lineTo(25.0, 30.0);
118 roundRectPath.arcTo(20.0, 30.0, 10.0, 10.0, 90.0, 90.0);
119 roundRectPath.lineTo(20.0, 65.0);
120 roundRectPath.arcTo(20.0, 60.0, 10.0, 10.0, 180.0, 90.0);
121 roundRectPath.lineTo(75.0, 70.0);
122 roundRectPath.arcTo(70.0, 60.0, 10.0, 10.0, 270.0, 90.0);
123 roundRectPath.closeSubpath();
125 var ellipsePath = new QPainterPath();
126 ellipsePath.moveTo(80.0, 50.0);
127 ellipsePath.arcTo(20.0, 30.0, 60.0, 40.0, 0.0, 360.0);
129 var piePath = new QPainterPath();
130 piePath.moveTo(50.0, 50.0);
131 piePath.arcTo(20.0, 30.0, 60.0, 40.0, 60.0, 240.0);
132 piePath.closeSubpath();
134 var polygonPath = new QPainterPath();
135 polygonPath.moveTo(10.0, 80.0);
136 polygonPath.lineTo(20.0, 10.0);
137 polygonPath.lineTo(80.0, 30.0);
138 polygonPath.lineTo(90.0, 70.0);
139 polygonPath.closeSubpath();
141 var groupPath = new QPainterPath();
142 groupPath.moveTo(60.0, 40.0);
143 groupPath.arcTo(20.0, 20.0, 40.0, 40.0, 0.0, 360.0);
144 groupPath.moveTo(40.0, 40.0);
145 groupPath.lineTo(40.0, 80.0);
146 groupPath.lineTo(80.0, 80.0);
147 groupPath.lineTo(80.0, 40.0);
148 groupPath.closeSubpath();
150 var textPath = new QPainterPath();
151 var timesFont = new QFont("Times", 50);
152 timesFont.setStyleStrategy(QFont.ForceOutline);
153 textPath.addText(10, 70, timesFont, tr("Qt"));
155 var bezierPath = new QPainterPath();
156 bezierPath.moveTo(20, 30);
157 bezierPath.cubicTo(80, 0, 50, 50, 80, 80);
159 var starPath = new QPainterPath();
160 starPath.moveTo(90, 50);
161 for (var i = 1; i < 5; ++i) {
162 starPath.lineTo(50 + 40 * Math.cos(0.8 * i * Math.PI),
163 50 + 40 * Math.sin(0.8 * i * Math.PI));
165 starPath.closeSubpath();
167 this.renderAreas = new Array(Window.NumRenderAreas);
168 this.renderAreas[0] = new RenderArea(rectPath);
169 this.renderAreas[1] = new RenderArea(roundRectPath);
170 this.renderAreas[2] = new RenderArea(ellipsePath);
171 this.renderAreas[3] = new RenderArea(piePath);
172 this.renderAreas[4] = new RenderArea(polygonPath);
173 this.renderAreas[5] = new RenderArea(groupPath);
174 this.renderAreas[6] = new RenderArea(textPath);
175 this.renderAreas[7] = new RenderArea(bezierPath);
176 this.renderAreas[8] = new RenderArea(starPath);
178 this.fillRuleComboBox = new QComboBox();
179 this.fillRuleComboBox.addItem(tr("Odd Even"), Qt.OddEvenFill);
180 this.fillRuleComboBox.addItem(tr("Winding"), Qt.WindingFill);
182 this.fillRuleLabel = new QLabel(tr("Fill &Rule:"));
183 this.fillRuleLabel.setBuddy(this.fillRuleComboBox);
185 this.fillColor1ComboBox = new QComboBox();
186 this.populateWithColors(this.fillColor1ComboBox);
187 this.fillColor1ComboBox.setCurrentIndex(
188 this.fillColor1ComboBox.findText("mediumslateblue"));
190 this.fillColor2ComboBox = new QComboBox();
191 this.populateWithColors(this.fillColor2ComboBox);
192 this.fillColor2ComboBox.setCurrentIndex(
193 this.fillColor2ComboBox.findText("cornsilk"));
195 this.fillGradientLabel = new QLabel(tr("&Fill Gradient:"));
196 this.fillGradientLabel.setBuddy(this.fillColor1ComboBox);
198 this.fillToLabel = new QLabel(tr("to"));
199 this.fillToLabel.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed);
201 this.penWidthSpinBox = new QSpinBox();
202 this.penWidthSpinBox.setRange(0, 20);
204 this.penWidthLabel = new QLabel(tr("&Pen Width:"));
205 this.penWidthLabel.setBuddy(this.penWidthSpinBox);
207 this.penColorComboBox = new QComboBox();
208 this.populateWithColors(this.penColorComboBox);
209 this.penColorComboBox.setCurrentIndex(
210 this.penColorComboBox.findText("darkslateblue"));
212 this.penColorLabel = new QLabel(tr("Pen &Color:"));
213 this.penColorLabel.setBuddy(this.penColorComboBox);
215 this.rotationAngleSpinBox = new QSpinBox();
216 this.rotationAngleSpinBox.setRange(0, 359);
217 this.rotationAngleSpinBox.wrapping = true;
218 this.rotationAngleSpinBox.suffix = "\xB0";
220 this.rotationAngleLabel = new QLabel(tr("&Rotation Angle:"));
221 this.rotationAngleLabel.setBuddy(this.rotationAngleSpinBox);
223 this.fillRuleComboBox["activated(int)"].connect(
224 this, this.fillRuleChanged);
225 this.fillColor1ComboBox["activated(int)"].connect(
226 this, this.fillGradientChanged);
227 this.fillColor2ComboBox["activated(int)"].connect(
228 this, this.fillGradientChanged);
229 this.penColorComboBox["activated(int)"].connect(
230 this, this.penColorChanged);
232 for (var i = 0; i < Window.NumRenderAreas; ++i) {
233 this.penWidthSpinBox["valueChanged(int)"].connect(
234 this.renderAreas[i], this.renderAreas[i].setPenWidth);
235 this.rotationAngleSpinBox["valueChanged(int)"].connect(
236 this.renderAreas[i], this.renderAreas[i].setRotationAngle);
239 var topLayout = new QGridLayout();
240 for (var i = 0; i < Window.NumRenderAreas; ++i)
241 topLayout.addWidget(this.renderAreas[i], i / 3, i % 3);
243 var mainLayout = new QGridLayout();
244 mainLayout.addLayout(topLayout, 0, 0, 1, 4);
245 mainLayout.addWidget(this.fillRuleLabel, 1, 0);
246 mainLayout.addWidget(this.fillRuleComboBox, 1, 1, 1, 3);
247 mainLayout.addWidget(this.fillGradientLabel, 2, 0);
248 mainLayout.addWidget(this.fillColor1ComboBox, 2, 1);
249 mainLayout.addWidget(this.fillToLabel, 2, 2);
250 mainLayout.addWidget(this.fillColor2ComboBox, 2, 3);
251 mainLayout.addWidget(this.penWidthLabel, 3, 0);
252 mainLayout.addWidget(this.penWidthSpinBox, 3, 1, 1, 3);
253 mainLayout.addWidget(this.penColorLabel, 4, 0);
254 mainLayout.addWidget(this.penColorComboBox, 4, 1, 1, 3);
255 mainLayout.addWidget(this.rotationAngleLabel, 5, 0);
256 mainLayout.addWidget(this.rotationAngleSpinBox, 5, 1, 1, 3);
257 this.setLayout(mainLayout);
259 this.fillRuleChanged();
260 this.fillGradientChanged();
261 this.penColorChanged();
262 this.penWidthSpinBox.value = 2;
264 this.windowTitle = tr("Painter Paths");
267 Window.NumRenderAreas = 9;
269 Window.prototype = new QWidget();
271 Window.prototype.fillRuleChanged = function()
273 var rule = Qt.FillRule(this.currentItemData(this.fillRuleComboBox));
275 for (var i = 0; i < Window.NumRenderAreas; ++i)
276 this.renderAreas[i].setFillRule(rule);
279 Window.prototype.fillGradientChanged = function()
281 var color1 = this.currentItemData(this.fillColor1ComboBox);
282 var color2 = this.currentItemData(this.fillColor2ComboBox);
284 for (var i = 0; i < Window.NumRenderAreas; ++i)
285 this.renderAreas[i].setFillGradient(color1, color2);
288 Window.prototype.penColorChanged = function()
290 var color = this.currentItemData(this.penColorComboBox);
292 for (var i = 0; i < Window.NumRenderAreas; ++i)
293 this.renderAreas[i].setPenColor(color);
296 Window.prototype.populateWithColors = function(comboBox)
298 var colorNames = QColor.colorNames();
299 for (var i = 0; i < colorNames.length; ++i) {
300 var name = colorNames[i];
301 comboBox.addItem(name, new QColor(name));
305 Window.prototype.currentItemData = function(comboBox)
307 return comboBox.itemData(comboBox.currentIndex);
312 var win = new Window();
314 QCoreApplication.exec();