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/QGraphicsWidget>
23 #include <QtGui/QGraphicsGridLayout>
24 #include <QtGui/QGraphicsLinearLayout>
26 #include <Plasma/Applet>
28 #include "../backportglobal.h"
29 #include "../appletinterface.h"
31 Q_DECLARE_METATYPE(QScript::Pointer
<QGraphicsItem
>::wrapped_pointer_type
)
32 Q_DECLARE_METATYPE(QGraphicsWidget
*)
33 Q_DECLARE_METATYPE(QGraphicsGridLayout
*)
34 DECLARE_POINTER_METATYPE(QGraphicsLinearLayout
)
36 DECLARE_VOID_NUMBER_METHOD(QGraphicsLinearLayout
, removeAt
)
37 DECLARE_VOID_NUMBER_METHOD(QGraphicsLinearLayout
, addStretch
)
38 DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsLinearLayout
, insertStretch
)
39 DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsLinearLayout
, setItemSpacing
)
40 DECLARE_VOID_QUAD_NUMBER_METHOD(QGraphicsLinearLayout
, setContentsMargins
)
41 DECLARE_NUMBER_GET_SET_METHODS(QGraphicsLinearLayout
, spacing
, setSpacing
)
43 /////////////////////////////////////////////////////////////
45 QGraphicsLayoutItem
*layoutItem(QScriptContext
*ctx
, int index
)
47 QGraphicsLayoutItem
*item
= qscriptvalue_cast
<QGraphicsWidget
*>(ctx
->argument(0));
50 item
= qscriptvalue_cast
<QGraphicsLinearLayout
*>(ctx
->argument(0));
54 item
= qscriptvalue_cast
<QGraphicsGridLayout
*>(ctx
->argument(0));
57 QObject
*appletObject
= ctx
->argument(0).toQObject();
59 AppletInterface
*interface
= qobject_cast
<AppletInterface
*>(appletObject
);
61 item
= interface
->applet();
68 static QScriptValue
ctor(QScriptContext
*ctx
, QScriptEngine
*eng
)
70 if (ctx
->argumentCount() == 0) {
71 return ctx
->throwError(i18n("LinearLayout requires a parent"));
74 QGraphicsLayoutItem
*parent
= layoutItem(ctx
, 0);
77 return ctx
->throwError(i18n("The parent must be a QGraphicsLayoutItem"));
80 return qScriptValueFromValue(eng
, new QGraphicsLinearLayout(parent
));
83 BEGIN_DECLARE_METHOD(QGraphicsLinearLayout
, orientation
) {
84 return QScriptValue(eng
, static_cast<int>(self
->orientation()));
87 BEGIN_DECLARE_METHOD(QGraphicsLinearLayout
, setOrientation
) {
88 self
->setOrientation(static_cast<Qt::Orientation
>(ctx
->argument(0).toInt32()));
89 return eng
->undefinedValue();
93 void insertItem(int index, QGraphicsLayoutItem * item)
94 void removeItem(QGraphicsLayoutItem * item)
96 void setAlignment(QGraphicsLayoutItem * item, Qt::Alignment alignment)
97 void setStretchFactor(QGraphicsLayoutItem * item, int stretch)
100 BEGIN_DECLARE_METHOD(QGraphicsLinearLayout
, addItem
) {
101 QGraphicsLayoutItem
*item
= layoutItem(ctx
, 0);
103 return ctx
->throwError(QScriptContext::TypeError
,
104 "QGraphicsLinearLayout.prototype.addItem: argument is not a GraphicsLayoutItem");
108 return eng
->undefinedValue();
111 BEGIN_DECLARE_METHOD(QGraphicsItem
, toString
) {
112 return QScriptValue(eng
, "QGraphicsLinearLayout");
115 /////////////////////////////////////////////////////////////
117 class PrototypeLinearLayout
: public QGraphicsLinearLayout
120 PrototypeLinearLayout()
124 QScriptValue
constructLinearLayoutClass(QScriptEngine
*eng
)
126 // QScriptValue proto = QScript::wrapGVPointer<QGraphicsLinearLayout>(eng, new QGraphicsLinearLayout(), );
127 QScriptValue proto
= QScript::wrapPointer
<QGraphicsLinearLayout
>(eng
, new QGraphicsLinearLayout(), QScript::UserOwnership
);
128 ADD_GET_SET_METHODS(proto
, spacing
, setSpacing
);
129 ADD_GET_SET_METHODS(proto
, orientation
, setOrientation
);
130 //ADD_GET_METHOD(proto, y);
131 ADD_METHOD(proto
, removeAt
);
132 ADD_METHOD(proto
, addStretch
);
133 ADD_METHOD(proto
, insertStretch
);
134 ADD_METHOD(proto
, setItemSpacing
);
135 ADD_METHOD(proto
, setContentsMargins
);
136 ADD_METHOD(proto
, addItem
);
137 ADD_METHOD(proto
, toString
);
139 QScript::registerPointerMetaType
<QGraphicsLinearLayout
>(eng
, proto
);
141 QScriptValue ctorFun
= eng
->newFunction(ctor
, proto
);
142 //ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemIsMovable);