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; }
27 Operation = { NoTransformation: 0, Rotate: 1, Scale: 2, Translate: 3 };
30 function RenderArea(parent)
32 QWidget.call(this, parent);
34 var newFont = new QFont(this.font);
35 newFont.setPixelSize(12);
38 var fontMetrics = new QFontMetrics(newFont);
39 this.xBoundingRect = fontMetrics.boundingRect(tr("x"));
40 this.yBoundingRect = fontMetrics.boundingRect(tr("y"));
41 this.shape = new QPainterPath();
42 this.operations = new Array();
45 RenderArea.prototype = new QWidget();
47 RenderArea.prototype.setOperations = function(operations)
49 this.operations = operations;
53 RenderArea.prototype.setShape = function(shape)
59 RenderArea.prototype.getMinimumSizeHint = function()
61 return new QSize(182, 182);
64 RenderArea.prototype.getSizeHint = function()
66 return new QSize(232, 232);
69 RenderArea.prototype.paintEvent = function(event)
71 var painter = new QPainter();
73 painter.setRenderHint(QPainter.Antialiasing);
74 painter.fillRect(event.rect(), new QBrush(new QColor(Qt.white)));
76 painter.translate(66, 66);
79 this.transformPainter(painter);
80 this.drawShape(painter);
83 this.drawOutline(painter);
85 this.transformPainter(painter);
86 this.drawCoordinates(painter);
91 RenderArea.prototype.drawCoordinates = function(painter)
93 painter.setPen(new QColor(Qt.red));
95 painter.drawLine(0, 0, 50, 0);
96 painter.drawLine(48, -2, 50, 0);
97 painter.drawLine(48, 2, 50, 0);
98 painter.drawText(60 - this.xBoundingRect.width() / 2,
99 0 + this.xBoundingRect.height() / 2, tr("x"));
101 painter.drawLine(0, 0, 0, 50);
102 painter.drawLine(-2, 48, 0, 50);
103 painter.drawLine(2, 48, 0, 50);
104 painter.drawText(0 - this.yBoundingRect.width() / 2,
105 60 + this.yBoundingRect.height() / 2, tr("y"));
108 RenderArea.prototype.drawOutline = function(painter)
110 painter.setPen(new QPen(new QColor(Qt.darkGreen)));
111 painter.setPen(new QPen(Qt.DashLine));
112 painter.setBrush(new QBrush(Qt.NoBrush));
113 painter.drawRect(0, 0, 100, 100);
116 RenderArea.prototype.drawShape = function(painter)
118 painter.fillPath(this.shape, new QColor(Qt.blue));
121 RenderArea.prototype.transformPainter = function(painter)
123 for (var i = 0; i < this.operations.length; ++i) {
124 switch (this.operations[i]) {
125 case Operation.Translate:
126 painter.translate(50, 50);
128 case Operation.Scale:
129 painter.scale(0.75, 0.75);
131 case Operation.Rotate:
134 case Operation.NoTransformation:
143 function Window(parent)
145 QWidget.call(this, parent);
147 this.originalRenderArea = new RenderArea();
149 this.shapeComboBox = new QComboBox();
150 this.shapeComboBox.addItem(tr("Clock"));
151 this.shapeComboBox.addItem(tr("House"));
152 this.shapeComboBox.addItem(tr("Text"));
153 this.shapeComboBox.addItem(tr("Truck"));
155 var layout = new QGridLayout();
156 layout.addWidget(this.originalRenderArea, 0, 0);
157 layout.addWidget(this.shapeComboBox, 1, 0);
159 this.transformedRenderAreas = new Array(Window.NumTransformedAreas);
160 this.operationComboBoxes = new Array(Window.NumTransformedAreas);
161 for (var i = 0; i < Window.NumTransformedAreas; ++i) {
162 this.transformedRenderAreas[i] = new RenderArea();
164 this.operationComboBoxes[i] = new QComboBox;
165 this.operationComboBoxes[i].addItem(tr("No transformation"));
166 this.operationComboBoxes[i].addItem(tr("Rotate by 60\xB0"));
167 this.operationComboBoxes[i].addItem(tr("Scale to 75%"));
168 this.operationComboBoxes[i].addItem(tr("Translate by (50, 50)"));
170 this.operationComboBoxes[i]["activated(int)"].connect(
171 this, this.operationChanged);
173 layout.addWidget(this.transformedRenderAreas[i], 0, i + 1);
174 layout.addWidget(this.operationComboBoxes[i], 1, i + 1);
177 this.setLayout(layout);
179 this.shapeSelected(0);
181 this.windowTitle = tr("Transformations");
184 Window.prototype = new QWidget();
186 Window.NumTransformedAreas = 3;
188 Window.prototype.setupShapes = function()
190 var truck = new QPainterPath();
191 truck.setFillRule(Qt.WindingFill);
192 truck.moveTo(0.0, 87.0);
193 truck.lineTo(0.0, 60.0);
194 truck.lineTo(10.0, 60.0);
195 truck.lineTo(35.0, 35.0);
196 truck.lineTo(100.0, 35.0);
197 truck.lineTo(100.0, 87.0);
198 truck.lineTo(0.0, 87.0);
199 truck.moveTo(17.0, 60.0);
200 truck.lineTo(55.0, 60.0);
201 truck.lineTo(55.0, 40.0);
202 truck.lineTo(37.0, 40.0);
203 truck.lineTo(17.0, 60.0);
204 truck.addEllipse(17.0, 75.0, 25.0, 25.0);
205 truck.addEllipse(63.0, 75.0, 25.0, 25.0);
207 var clock = new QPainterPath();
208 clock.addEllipse(-50.0, -50.0, 100.0, 100.0);
209 clock.addEllipse(-48.0, -48.0, 96.0, 96.0);
210 clock.moveTo(0.0, 0.0);
211 clock.lineTo(-2.0, -2.0);
212 clock.lineTo(0.0, -42.0);
213 clock.lineTo(2.0, -2.0);
214 clock.lineTo(0.0, 0.0);
215 clock.moveTo(0.0, 0.0);
216 clock.lineTo(2.732, -0.732);
217 clock.lineTo(24.495, 14.142);
218 clock.lineTo(0.732, 2.732);
219 clock.lineTo(0.0, 0.0);
221 var house = new QPainterPath();
222 house.moveTo(-45.0, -20.0);
223 house.lineTo(0.0, -45.0);
224 house.lineTo(45.0, -20.0);
225 house.lineTo(45.0, 45.0);
226 house.lineTo(-45.0, 45.0);
227 house.lineTo(-45.0, -20.0);
228 house.addRect(15.0, 5.0, 20.0, 35.0);
229 house.addRect(-35.0, -15.0, 25.0, 25.0);
231 var text = new QPainterPath();
233 font.setPixelSize(50);
234 var fontBoundingRect = new QFontMetrics(font).boundingRect(tr("Qt"));
235 text.addText(-new QPointF(fontBoundingRect.center()), font, tr("Qt"));
237 this.shapes = new Array();
238 this.shapes.push(clock);
239 this.shapes.push(house);
240 this.shapes.push(text);
241 this.shapes.push(truck);
243 this.shapeComboBox["activated(int)"].connect(
244 this, this.shapeSelected);
247 Window.prototype.operationChanged = function()
249 var operations = new Array();
250 for (var i = 0; i < Window.NumTransformedAreas; ++i) {
251 var index = this.operationComboBoxes[i].currentIndex;
252 operations.push(index);
253 this.transformedRenderAreas[i].setOperations(operations.slice());
257 Window.prototype.shapeSelected = function(index)
259 var shape = this.shapes[index];
260 this.originalRenderArea.setShape(shape);
261 for (var i = 0; i < Window.NumTransformedAreas; ++i)
262 this.transformedRenderAreas[i].setShape(shape);
265 var win = new Window();
267 QCoreApplication.exec();