add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / javascript / qtgui / graphicsitem.cpp
blob09592b0d32b8bd3613ee8628d224a05412ce0822
1 /*
2 * Copyright 2007 Richard J. Moore <rich@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License version 2 as
6 * published by the Free Software Foundation
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include <QtScript/QScriptValue>
20 #include <QtScript/QScriptEngine>
21 #include <QtScript/QScriptContext>
22 #include <QtGui/QCursor>
23 #include <QtGui/QGraphicsItem>
24 #include <QtGui/QGraphicsScene>
25 #include "../backportglobal.h"
27 Q_DECLARE_METATYPE(QScript::Pointer<QGraphicsItem>::wrapped_pointer_type)
28 Q_DECLARE_METATYPE(QList<QGraphicsItem*>)
29 Q_DECLARE_METATYPE(QPainterPath)
30 #ifndef QT_NO_CURSOR
31 Q_DECLARE_METATYPE(QCursor)
32 #endif
33 Q_DECLARE_METATYPE(QGraphicsItemGroup*)
34 Q_DECLARE_METATYPE(QPainter*)
35 Q_DECLARE_METATYPE(QStyleOptionGraphicsItem*)
37 Q_DECLARE_METATYPE(QGraphicsPathItem*)
38 Q_DECLARE_METATYPE(QGraphicsRectItem*)
39 Q_DECLARE_METATYPE(QGraphicsEllipseItem*)
40 Q_DECLARE_METATYPE(QGraphicsPolygonItem*)
41 Q_DECLARE_METATYPE(QGraphicsLineItem*)
42 Q_DECLARE_METATYPE(QGraphicsPixmapItem*)
43 Q_DECLARE_METATYPE(QGraphicsTextItem*)
44 Q_DECLARE_METATYPE(QGraphicsSimpleTextItem*)
46 DECLARE_BOOLEAN_GET_SET_METHODS(QGraphicsItem, acceptDrops, setAcceptDrops)
47 DECLARE_BOOLEAN_GET_SET_METHODS(QGraphicsItem, acceptsHoverEvents, setAcceptsHoverEvents)
48 DECLARE_GET_METHOD(QGraphicsItem, boundingRect)
49 DECLARE_GET_METHOD(QGraphicsItem, children)
50 DECLARE_GET_METHOD(QGraphicsItem, childrenBoundingRect)
51 #ifndef QT_NO_CURSOR
52 DECLARE_GET_SET_METHODS(QGraphicsItem, QCursor, cursor, setCursor)
53 DECLARE_BOOLEAN_GET_METHOD(QGraphicsItem, hasCursor)
54 #endif
55 DECLARE_GET_SET_METHODS(QGraphicsItem, QGraphicsItemGroup*, group, setGroup)
56 DECLARE_BOOLEAN_GET_SET_METHODS(QGraphicsItem, handlesChildEvents, setHandlesChildEvents)
57 DECLARE_BOOLEAN_GET_METHOD(QGraphicsItem, hasFocus)
58 DECLARE_BOOLEAN_GET_SET_METHODS(QGraphicsItem, isEnabled, setEnabled)
59 DECLARE_BOOLEAN_GET_SET_METHODS(QGraphicsItem, isSelected, setSelected)
60 DECLARE_BOOLEAN_GET_SET_METHODS(QGraphicsItem, isVisible, setVisible)
61 DECLARE_GET_METHOD(QGraphicsItem, opaqueArea)
62 DECLARE_GET_METHOD(QGraphicsItem, pos)
63 DECLARE_QOBJECT_GET_METHOD(QGraphicsItem, scene)
64 DECLARE_GET_METHOD(QGraphicsItem, sceneBoundingRect)
65 DECLARE_GET_METHOD(QGraphicsItem, scenePos)
66 DECLARE_GET_METHOD(QGraphicsItem, sceneTransform)
67 DECLARE_GET_METHOD(QGraphicsItem, shape)
68 #ifndef QT_NO_TOOLTIP
69 DECLARE_STRING_GET_SET_METHODS(QGraphicsItem, toolTip, setToolTip)
70 #endif
71 DECLARE_GET_METHOD(QGraphicsItem, topLevelItem)
72 DECLARE_GET_SET_METHODS(QGraphicsItem, QTransform, transform, setTransform)
73 DECLARE_NUMBER_GET_METHOD(QGraphicsItem, type)
74 DECLARE_NUMBER_GET_METHOD(QGraphicsItem, x)
75 DECLARE_NUMBER_GET_METHOD(QGraphicsItem, y)
76 DECLARE_NUMBER_GET_SET_METHODS(QGraphicsItem, zValue, setZValue)
78 DECLARE_BOOLEAN_1ARG_METHOD(QGraphicsItem, QPointF, contains)
79 DECLARE_VOID_METHOD(QGraphicsItem, clearFocus)
80 DECLARE_VOID_METHOD(QGraphicsItem, hide)
81 DECLARE_BOOLEAN_1ARG_METHOD(QGraphicsItem, QGraphicsItem*, isAncestorOf)
82 DECLARE_BOOLEAN_1ARG_METHOD(QGraphicsItem, QGraphicsItem*, isObscuredBy)
83 DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsItem, moveBy)
84 DECLARE_VOID_METHOD(QGraphicsItem, resetTransform)
85 #ifndef QT_NO_CURSOR
86 DECLARE_VOID_METHOD(QGraphicsItem, unsetCursor)
87 #endif
88 DECLARE_VOID_METHOD(QGraphicsItem, show)
89 DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsItem, translate)
90 DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsItem, scale)
91 DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsItem, shear)
92 DECLARE_VOID_1ARG_METHOD(QGraphicsItem, QGraphicsItem*, installSceneEventFilter)
93 DECLARE_VOID_1ARG_METHOD(QGraphicsItem, QGraphicsItem*, removeSceneEventFilter)
94 DECLARE_VOID_NUMBER_METHOD(QGraphicsItem, rotate)
96 /////////////////////////////////////////////////////////////
98 static QScriptValue ctor(QScriptContext *ctx, QScriptEngine *)
100 return ctx->throwError("QGraphicsItem cannot be instantiated");
103 BEGIN_DECLARE_METHOD(QGraphicsItem, acceptedMouseButtons) {
104 return QScriptValue(eng, static_cast<int>(self->acceptedMouseButtons()));
105 } END_DECLARE_METHOD
107 BEGIN_DECLARE_METHOD(QGraphicsItem, advance) {
108 self->advance(ctx->argument(0).toInt32());
109 return eng->undefinedValue();
110 } END_DECLARE_METHOD
112 BEGIN_DECLARE_METHOD(QGraphicsItem, collidesWithItem) {
113 QGraphicsItem *other = qscriptvalue_cast<QGraphicsItem*>(ctx->argument(0));
114 if (!other) {
115 return ctx->throwError(QScriptContext::TypeError,
116 "QGraphicsItem.prototype.collidesWithItem: argument is not a GraphicsItem");
118 if (ctx->argument(1).isUndefined())
119 return QScriptValue(eng, self->collidesWithItem(other));
120 else
121 return QScriptValue(eng, self->collidesWithItem(other, static_cast<Qt::ItemSelectionMode>(ctx->argument(1).toInt32())));
122 } END_DECLARE_METHOD
124 BEGIN_DECLARE_METHOD(QGraphicsItem, collidesWithPath) {
125 QPainterPath path = qscriptvalue_cast<QPainterPath>(ctx->argument(0));
126 if (ctx->argument(1).isUndefined())
127 return QScriptValue(eng, self->collidesWithPath(path));
128 else
129 return QScriptValue(eng, self->collidesWithPath(path, static_cast<Qt::ItemSelectionMode>(ctx->argument(1).toInt32())));
130 } END_DECLARE_METHOD
132 BEGIN_DECLARE_METHOD(QGraphicsItem, collidingItems) {
133 if (ctx->argument(0).isUndefined())
134 return qScriptValueFromValue(eng, self->collidingItems());
135 else
136 return qScriptValueFromValue(eng, self->collidingItems(static_cast<Qt::ItemSelectionMode>(ctx->argument(0).toInt32())));
137 } END_DECLARE_METHOD
139 BEGIN_DECLARE_METHOD(QGraphicsItem, data) {
140 return eng->newVariant(self->data(ctx->argument(0).toInt32()));
141 } END_DECLARE_METHOD
143 BEGIN_DECLARE_METHOD(QGraphicsItem, ensureVisible) {
144 Q_UNUSED(eng);
145 return ctx->throwError("QGraphicsItem.prototype.ensureVisible is not implemented");
146 } END_DECLARE_METHOD
148 BEGIN_DECLARE_METHOD(QGraphicsItem, flags) {
149 return QScriptValue(eng, static_cast<int>(self->flags()));
150 } END_DECLARE_METHOD
152 BEGIN_DECLARE_METHOD(QGraphicsItem, isObscured) {
153 if (ctx->argumentCount() == 0) {
154 return QScriptValue(eng, self->isObscured());
155 } else if (ctx->argumentCount() > 1) {
156 return QScriptValue(eng, self->isObscured(ctx->argument(0).toInt32(),
157 ctx->argument(1).toInt32(),
158 ctx->argument(2).toInt32(),
159 ctx->argument(3).toInt32()));
160 } else {
161 return QScriptValue(eng, self->isObscured(qscriptvalue_cast<QRectF>(ctx->argument(0))));
163 } END_DECLARE_METHOD
165 BEGIN_DECLARE_METHOD(QGraphicsItem, mapFromItem) {
166 Q_UNUSED(eng);
167 return ctx->throwError("QGraphicsItem.prototype.mapFromItem is not implemented");
168 } END_DECLARE_METHOD
170 BEGIN_DECLARE_METHOD(QGraphicsItem, mapFromParent) {
171 Q_UNUSED(eng);
172 return ctx->throwError("QGraphicsItem.prototype.mapFromParent is not implemented");
173 } END_DECLARE_METHOD
175 BEGIN_DECLARE_METHOD(QGraphicsItem, mapFromScene) {
176 Q_UNUSED(eng);
177 return ctx->throwError("QGraphicsItem.prototype.mapFromScene is not implemented");
178 } END_DECLARE_METHOD
180 BEGIN_DECLARE_METHOD(QGraphicsItem, mapToItem) {
181 Q_UNUSED(eng);
182 return ctx->throwError("QGraphicsItem.prototype.mapToItem is not implemented");
183 } END_DECLARE_METHOD
185 BEGIN_DECLARE_METHOD(QGraphicsItem, mapToParent) {
186 Q_UNUSED(eng);
187 return ctx->throwError("QGraphicsItem.prototype.mapToParent is not implemented");
188 } END_DECLARE_METHOD
190 BEGIN_DECLARE_METHOD(QGraphicsItem, mapToScene) {
191 Q_UNUSED(eng);
192 return ctx->throwError("QGraphicsItem.prototype.mapToScene is not implemented");
193 } END_DECLARE_METHOD
195 BEGIN_DECLARE_METHOD(QGraphicsItem, paint) {
196 self->paint(qscriptvalue_cast<QPainter*>(ctx->argument(0)),
197 qscriptvalue_cast<QStyleOptionGraphicsItem*>(ctx->argument(1)),
198 qscriptvalue_cast<QWidget*>(ctx->argument(2)));
199 return eng->undefinedValue();
200 } END_DECLARE_METHOD
202 BEGIN_DECLARE_METHOD(QGraphicsItem, parentItem) {
203 QGraphicsItem *parent = self->parentItem();
204 if (!parent)
205 return eng->nullValue();
206 QScriptValue ret = qScriptValueFromValue(eng, parent);
207 QScriptValue proto;
208 switch (parent->type()) {
209 case 2:
210 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsPathItem*>());
211 break;
212 case 3:
213 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsRectItem*>());
214 break;
215 case 4:
216 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsEllipseItem*>());
217 break;
218 case 5:
219 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsPolygonItem*>());
220 break;
221 case 6:
222 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsLineItem*>());
223 break;
224 case 7:
225 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsPixmapItem*>());
226 break;
227 case 8:
228 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsTextItem*>());
229 break;
230 case 9:
231 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsSimpleTextItem*>());
232 break;
233 case 10:
234 proto = eng->defaultPrototype(qMetaTypeId<QGraphicsItemGroup*>());
235 break;
237 if (proto.isValid())
238 ret.setPrototype(proto);
239 return ret;
240 } END_DECLARE_METHOD
242 BEGIN_DECLARE_METHOD(QGraphicsItem, setAcceptedMouseButtons) {
243 self->setAcceptedMouseButtons(static_cast<Qt::MouseButtons>(ctx->argument(0).toInt32()));
244 return eng->undefinedValue();
245 } END_DECLARE_METHOD
247 BEGIN_DECLARE_METHOD(QGraphicsItem, setData) {
248 self->setData(ctx->argument(0).toInt32(), ctx->argument(1).toVariant());
249 return eng->undefinedValue();
250 } END_DECLARE_METHOD
252 BEGIN_DECLARE_METHOD(QGraphicsItem, setFlag) {
253 QGraphicsItem::GraphicsItemFlag flag = static_cast<QGraphicsItem::GraphicsItemFlag>(ctx->argument(0).toInt32());
254 if (ctx->argument(1).isUndefined())
255 self->setFlag(flag);
256 else
257 self->setFlag(flag, ctx->argument(1).toBoolean());
258 return eng->undefinedValue();
259 } END_DECLARE_METHOD
261 BEGIN_DECLARE_METHOD(QGraphicsItem, setFlags) {
262 self->setFlags(static_cast<QGraphicsItem::GraphicsItemFlags>(ctx->argument(0).toInt32()));
263 return eng->undefinedValue();
264 } END_DECLARE_METHOD
266 BEGIN_DECLARE_METHOD(QGraphicsItem, setFocus) {
267 if (ctx->argument(0).isUndefined())
268 self->setFocus();
269 else
270 self->setFocus(static_cast<Qt::FocusReason>(ctx->argument(0).toInt32()));
271 return eng->undefinedValue();
272 } END_DECLARE_METHOD
274 BEGIN_DECLARE_METHOD(QGraphicsItem, setParentItem) {
275 QScriptValue arg = ctx->argument(0);
276 QGraphicsItem *item = qscriptvalue_cast<QGraphicsItem*>(arg);
277 self->setParentItem(item);
278 if (item)
279 QScript::maybeReleaseOwnership(ctx->thisObject());
280 else if (!self->scene())
281 QScript::maybeTakeOwnership(ctx->thisObject());
282 return eng->undefinedValue();
283 } END_DECLARE_METHOD
285 BEGIN_DECLARE_METHOD(QGraphicsItem, setPos) {
286 if (ctx->argumentCount() > 1)
287 self->setPos(ctx->argument(0).toNumber(), ctx->argument(1).toNumber());
288 else
289 self->setPos(qscriptvalue_cast<QPointF>(ctx->argument(0)));
290 return eng->undefinedValue();
291 } END_DECLARE_METHOD
293 BEGIN_DECLARE_METHOD(QGraphicsItem, update) {
294 if (ctx->argumentCount() > 1) {
295 self->update(ctx->argument(0).toNumber(),
296 ctx->argument(1).toNumber(),
297 ctx->argument(2).toNumber(),
298 ctx->argument(3).toNumber());
299 } else {
300 self->update(qscriptvalue_cast<QRectF>(ctx->argument(0)));
302 return eng->undefinedValue();
303 } END_DECLARE_METHOD
305 BEGIN_DECLARE_METHOD(QGraphicsItem, toString) {
306 return QScriptValue(eng, "QGraphicsItem");
307 } END_DECLARE_METHOD
309 /////////////////////////////////////////////////////////////
311 class PrototypeGraphicsItem : public QGraphicsItem
313 public:
314 PrototypeGraphicsItem()
316 QRectF boundingRect() const
317 { return QRectF(); }
318 void paint(QPainter *, const QStyleOptionGraphicsItem *, QWidget *)
322 QScriptValue constructGraphicsItemClass(QScriptEngine *eng)
324 QScriptValue proto = QScript::wrapGVPointer<QGraphicsItem>(eng, new PrototypeGraphicsItem());
325 ADD_GET_SET_METHODS(proto, acceptDrops, setAcceptDrops);
326 ADD_GET_SET_METHODS(proto, acceptsHoverEvents, setAcceptsHoverEvents);
327 ADD_GET_METHOD(proto, boundingRect);
328 ADD_GET_METHOD(proto, children);
329 ADD_GET_METHOD(proto, childrenBoundingRect);
330 #ifndef QT_NO_CURSOR
331 ADD_GET_SET_METHODS(proto, cursor, setCursor);
332 ADD_GET_METHOD(proto, hasCursor);
333 #endif
334 ADD_GET_SET_METHODS(proto, group, setGroup);
335 ADD_GET_SET_METHODS(proto, handlesChildEvents, setHandlesChildEvents);
336 ADD_GET_METHOD(proto, hasFocus);
337 ADD_GET_SET_METHODS(proto, isEnabled, setEnabled);
338 ADD_GET_SET_METHODS(proto, isSelected, setSelected);
339 ADD_GET_SET_METHODS(proto, isVisible, setVisible);
340 ADD_GET_METHOD(proto, opaqueArea);
341 ADD_GET_METHOD(proto, pos);
342 ADD_GET_METHOD(proto, scene);
343 ADD_GET_METHOD(proto, sceneBoundingRect);
344 ADD_GET_METHOD(proto, scenePos);
345 ADD_GET_METHOD(proto, sceneTransform);
346 ADD_GET_METHOD(proto, shape);
347 #ifndef QT_NO_TOOLTIP
348 ADD_GET_SET_METHODS(proto, toolTip, setToolTip);
349 #endif
350 ADD_GET_METHOD(proto, topLevelItem);
351 ADD_GET_SET_METHODS(proto, transform, setTransform);
352 ADD_GET_METHOD(proto, type);
353 ADD_GET_METHOD(proto, x);
354 ADD_GET_METHOD(proto, y);
355 ADD_GET_SET_METHODS(proto, zValue, setZValue);
357 ADD_METHOD(proto, acceptedMouseButtons);
358 ADD_METHOD(proto, advance);
359 ADD_METHOD(proto, clearFocus);
360 ADD_METHOD(proto, collidesWithItem);
361 ADD_METHOD(proto, collidesWithPath);
362 ADD_METHOD(proto, collidingItems);
363 ADD_METHOD(proto, contains);
364 ADD_METHOD(proto, data);
365 ADD_METHOD(proto, ensureVisible);
366 ADD_METHOD(proto, flags);
367 ADD_METHOD(proto, hide);
368 ADD_METHOD(proto, installSceneEventFilter);
369 ADD_METHOD(proto, isAncestorOf);
370 ADD_METHOD(proto, isObscured);
371 ADD_METHOD(proto, isObscuredBy);
372 ADD_METHOD(proto, mapFromItem);
373 ADD_METHOD(proto, mapFromParent);
374 ADD_METHOD(proto, mapFromScene);
375 ADD_METHOD(proto, mapToItem);
376 ADD_METHOD(proto, mapToParent);
377 ADD_METHOD(proto, mapToScene);
378 ADD_METHOD(proto, moveBy);
379 ADD_METHOD(proto, paint);
380 ADD_METHOD(proto, parentItem);
381 ADD_METHOD(proto, removeSceneEventFilter);
382 ADD_METHOD(proto, resetTransform);
383 ADD_METHOD(proto, rotate);
384 ADD_METHOD(proto, scale);
385 ADD_METHOD(proto, setAcceptedMouseButtons);
386 ADD_METHOD(proto, setData);
387 ADD_METHOD(proto, setFlag);
388 ADD_METHOD(proto, setFlags);
389 ADD_METHOD(proto, setFocus);
390 ADD_METHOD(proto, setParentItem);
391 ADD_METHOD(proto, setPos);
392 ADD_METHOD(proto, shear);
393 ADD_METHOD(proto, show);
394 ADD_METHOD(proto, toString);
395 ADD_METHOD(proto, translate);
396 #ifndef QT_NO_CURSOR
397 ADD_METHOD(proto, unsetCursor);
398 #endif
399 ADD_METHOD(proto, update);
401 QScript::registerPointerMetaType<QGraphicsItem>(eng, proto);
403 QScriptValue ctorFun = eng->newFunction(ctor, proto);
404 ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemIsMovable);
405 ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemIsSelectable);
406 ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemIsFocusable);
407 ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemClipsToShape);
408 ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemClipsChildrenToShape);
409 ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemIgnoresTransformations);
411 return ctorFun;