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
)
31 Q_DECLARE_METATYPE(QCursor
)
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
)
52 DECLARE_GET_SET_METHODS(QGraphicsItem
, QCursor
, cursor
, setCursor
)
53 DECLARE_BOOLEAN_GET_METHOD(QGraphicsItem
, hasCursor
)
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
)
69 DECLARE_STRING_GET_SET_METHODS(QGraphicsItem
, toolTip
, setToolTip
)
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
)
86 DECLARE_VOID_METHOD(QGraphicsItem
, unsetCursor
)
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()));
107 BEGIN_DECLARE_METHOD(QGraphicsItem
, advance
) {
108 self
->advance(ctx
->argument(0).toInt32());
109 return eng
->undefinedValue();
112 BEGIN_DECLARE_METHOD(QGraphicsItem
, collidesWithItem
) {
113 QGraphicsItem
*other
= qscriptvalue_cast
<QGraphicsItem
*>(ctx
->argument(0));
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
));
121 return QScriptValue(eng
, self
->collidesWithItem(other
, static_cast<Qt::ItemSelectionMode
>(ctx
->argument(1).toInt32())));
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
));
129 return QScriptValue(eng
, self
->collidesWithPath(path
, static_cast<Qt::ItemSelectionMode
>(ctx
->argument(1).toInt32())));
132 BEGIN_DECLARE_METHOD(QGraphicsItem
, collidingItems
) {
133 if (ctx
->argument(0).isUndefined())
134 return qScriptValueFromValue(eng
, self
->collidingItems());
136 return qScriptValueFromValue(eng
, self
->collidingItems(static_cast<Qt::ItemSelectionMode
>(ctx
->argument(0).toInt32())));
139 BEGIN_DECLARE_METHOD(QGraphicsItem
, data
) {
140 return eng
->newVariant(self
->data(ctx
->argument(0).toInt32()));
143 BEGIN_DECLARE_METHOD(QGraphicsItem
, ensureVisible
) {
145 return ctx
->throwError("QGraphicsItem.prototype.ensureVisible is not implemented");
148 BEGIN_DECLARE_METHOD(QGraphicsItem
, flags
) {
149 return QScriptValue(eng
, static_cast<int>(self
->flags()));
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()));
161 return QScriptValue(eng
, self
->isObscured(qscriptvalue_cast
<QRectF
>(ctx
->argument(0))));
165 BEGIN_DECLARE_METHOD(QGraphicsItem
, mapFromItem
) {
167 return ctx
->throwError("QGraphicsItem.prototype.mapFromItem is not implemented");
170 BEGIN_DECLARE_METHOD(QGraphicsItem
, mapFromParent
) {
172 return ctx
->throwError("QGraphicsItem.prototype.mapFromParent is not implemented");
175 BEGIN_DECLARE_METHOD(QGraphicsItem
, mapFromScene
) {
177 return ctx
->throwError("QGraphicsItem.prototype.mapFromScene is not implemented");
180 BEGIN_DECLARE_METHOD(QGraphicsItem
, mapToItem
) {
182 return ctx
->throwError("QGraphicsItem.prototype.mapToItem is not implemented");
185 BEGIN_DECLARE_METHOD(QGraphicsItem
, mapToParent
) {
187 return ctx
->throwError("QGraphicsItem.prototype.mapToParent is not implemented");
190 BEGIN_DECLARE_METHOD(QGraphicsItem
, mapToScene
) {
192 return ctx
->throwError("QGraphicsItem.prototype.mapToScene is not implemented");
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();
202 BEGIN_DECLARE_METHOD(QGraphicsItem
, parentItem
) {
203 QGraphicsItem
*parent
= self
->parentItem();
205 return eng
->nullValue();
206 QScriptValue ret
= qScriptValueFromValue(eng
, parent
);
208 switch (parent
->type()) {
210 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsPathItem
*>());
213 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsRectItem
*>());
216 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsEllipseItem
*>());
219 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsPolygonItem
*>());
222 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsLineItem
*>());
225 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsPixmapItem
*>());
228 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsTextItem
*>());
231 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsSimpleTextItem
*>());
234 proto
= eng
->defaultPrototype(qMetaTypeId
<QGraphicsItemGroup
*>());
238 ret
.setPrototype(proto
);
242 BEGIN_DECLARE_METHOD(QGraphicsItem
, setAcceptedMouseButtons
) {
243 self
->setAcceptedMouseButtons(static_cast<Qt::MouseButtons
>(ctx
->argument(0).toInt32()));
244 return eng
->undefinedValue();
247 BEGIN_DECLARE_METHOD(QGraphicsItem
, setData
) {
248 self
->setData(ctx
->argument(0).toInt32(), ctx
->argument(1).toVariant());
249 return eng
->undefinedValue();
252 BEGIN_DECLARE_METHOD(QGraphicsItem
, setFlag
) {
253 QGraphicsItem::GraphicsItemFlag flag
= static_cast<QGraphicsItem::GraphicsItemFlag
>(ctx
->argument(0).toInt32());
254 if (ctx
->argument(1).isUndefined())
257 self
->setFlag(flag
, ctx
->argument(1).toBoolean());
258 return eng
->undefinedValue();
261 BEGIN_DECLARE_METHOD(QGraphicsItem
, setFlags
) {
262 self
->setFlags(static_cast<QGraphicsItem::GraphicsItemFlags
>(ctx
->argument(0).toInt32()));
263 return eng
->undefinedValue();
266 BEGIN_DECLARE_METHOD(QGraphicsItem
, setFocus
) {
267 if (ctx
->argument(0).isUndefined())
270 self
->setFocus(static_cast<Qt::FocusReason
>(ctx
->argument(0).toInt32()));
271 return eng
->undefinedValue();
274 BEGIN_DECLARE_METHOD(QGraphicsItem
, setParentItem
) {
275 QScriptValue arg
= ctx
->argument(0);
276 QGraphicsItem
*item
= qscriptvalue_cast
<QGraphicsItem
*>(arg
);
277 self
->setParentItem(item
);
279 QScript::maybeReleaseOwnership(ctx
->thisObject());
280 else if (!self
->scene())
281 QScript::maybeTakeOwnership(ctx
->thisObject());
282 return eng
->undefinedValue();
285 BEGIN_DECLARE_METHOD(QGraphicsItem
, setPos
) {
286 if (ctx
->argumentCount() > 1)
287 self
->setPos(ctx
->argument(0).toNumber(), ctx
->argument(1).toNumber());
289 self
->setPos(qscriptvalue_cast
<QPointF
>(ctx
->argument(0)));
290 return eng
->undefinedValue();
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());
300 self
->update(qscriptvalue_cast
<QRectF
>(ctx
->argument(0)));
302 return eng
->undefinedValue();
305 BEGIN_DECLARE_METHOD(QGraphicsItem
, toString
) {
306 return QScriptValue(eng
, "QGraphicsItem");
309 /////////////////////////////////////////////////////////////
311 class PrototypeGraphicsItem
: public QGraphicsItem
314 PrototypeGraphicsItem()
316 QRectF
boundingRect() const
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
);
331 ADD_GET_SET_METHODS(proto
, cursor
, setCursor
);
332 ADD_GET_METHOD(proto
, hasCursor
);
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
);
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
);
397 ADD_METHOD(proto
, unsetCursor
);
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
);