2009-11-18 23:53:41 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2007 Richard J. Moore <rich@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QtScript/QScriptValue>
|
|
|
|
#include <QtScript/QScriptEngine>
|
|
|
|
#include <QtScript/QScriptContext>
|
2010-02-23 10:41:14 +01:00
|
|
|
#include <QtGui/QGraphicsAnchorLayout>
|
2009-11-29 05:17:30 +01:00
|
|
|
#include <QtGui/QGraphicsLayout>
|
2010-02-23 10:41:14 +01:00
|
|
|
#include <QtGui/QGraphicsLinearLayout>
|
|
|
|
#include <QtGui/QGraphicsGridLayout>
|
|
|
|
#include <QtGui/QGraphicsWidget>
|
2009-11-18 23:53:41 +01:00
|
|
|
|
|
|
|
#include <Plasma/Applet>
|
|
|
|
|
2010-08-06 04:01:40 +02:00
|
|
|
#include "backportglobal.h"
|
|
|
|
#include "plasmoid/appletinterface.h"
|
2009-11-18 23:53:41 +01:00
|
|
|
|
|
|
|
Q_DECLARE_METATYPE(QScript::Pointer<QGraphicsItem>::wrapped_pointer_type)
|
|
|
|
Q_DECLARE_METATYPE(QGraphicsWidget*)
|
2009-11-29 05:17:30 +01:00
|
|
|
Q_DECLARE_METATYPE(QGraphicsLayout*)
|
2009-11-18 23:53:41 +01:00
|
|
|
Q_DECLARE_METATYPE(QGraphicsLayoutItem*)
|
2010-02-23 10:41:14 +01:00
|
|
|
Q_DECLARE_METATYPE(QGraphicsAnchorLayout*)
|
|
|
|
Q_DECLARE_METATYPE(QGraphicsGridLayout*)
|
2009-11-18 23:53:41 +01:00
|
|
|
DECLARE_POINTER_METATYPE(QGraphicsLinearLayout)
|
|
|
|
|
|
|
|
DECLARE_VOID_NUMBER_METHOD(QGraphicsLinearLayout, removeAt)
|
|
|
|
DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsLinearLayout, insertStretch)
|
|
|
|
DECLARE_VOID_NUMBER_NUMBER_METHOD(QGraphicsLinearLayout, setItemSpacing)
|
|
|
|
DECLARE_VOID_QUAD_NUMBER_METHOD(QGraphicsLinearLayout, setContentsMargins)
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
|
2010-02-23 10:41:14 +01:00
|
|
|
// Q_DECLARE_METATYPE(QGraphicsLayoutItem*)
|
2010-02-28 16:32:21 +01:00
|
|
|
QGraphicsLayoutItem *extractLayoutItem(QScriptContext *ctx, int index = 0, bool noExistingLayout = false)
|
2009-11-18 23:53:41 +01:00
|
|
|
{
|
2010-02-23 10:41:14 +01:00
|
|
|
QScriptValue v = ctx->argument(index);
|
|
|
|
if (ctx->argumentCount() == 0 || v.isQObject()) {
|
|
|
|
QObject *object = v.toQObject();
|
|
|
|
QGraphicsWidget *w = qobject_cast<QGraphicsWidget *>(object);
|
|
|
|
if (!w) {
|
|
|
|
AppletInterface *interface = qobject_cast<AppletInterface*>(object);
|
|
|
|
if (!interface) {
|
|
|
|
interface = qobject_cast<AppletInterface*>(ctx->engine()->globalObject().property("plasmoid").toQObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (interface) {
|
|
|
|
w = interface->applet();
|
|
|
|
}
|
|
|
|
}
|
2009-11-18 23:53:41 +01:00
|
|
|
|
2010-02-28 16:32:21 +01:00
|
|
|
if (noExistingLayout && w->layout()) {
|
2010-02-28 16:13:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-02-23 10:41:14 +01:00
|
|
|
return w;
|
2009-11-18 23:53:41 +01:00
|
|
|
}
|
|
|
|
|
2010-02-23 10:41:14 +01:00
|
|
|
QVariant variant = v.toVariant();
|
|
|
|
QGraphicsLayoutItem *item = variant.value<QGraphicsLayoutItem *>();
|
|
|
|
//this is horribly ugly code, but when a QLinearLayout* is stuffed into a QVariant,
|
|
|
|
//QVariant does not know that it is a QGraphicsLayoutItem. repeat for all subclasses
|
|
|
|
//of same
|
2009-11-18 23:53:41 +01:00
|
|
|
if (!item) {
|
2010-02-23 10:41:14 +01:00
|
|
|
item = variant.value<QGraphicsLayout *>();
|
2009-11-18 23:53:41 +01:00
|
|
|
|
2010-02-23 10:41:14 +01:00
|
|
|
if (!item) {
|
|
|
|
item = variant.value<QGraphicsLinearLayout *>();
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
item = variant.value<QGraphicsGridLayout *>();
|
2009-11-18 23:53:41 +01:00
|
|
|
|
2010-02-23 10:41:14 +01:00
|
|
|
if (!item) {
|
|
|
|
item = variant.value<QGraphicsAnchorLayout *>();
|
|
|
|
}
|
|
|
|
}
|
2009-11-18 23:53:41 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-28 16:13:54 +01:00
|
|
|
QGraphicsWidget *w = dynamic_cast<QGraphicsWidget *>(item);
|
2010-02-28 16:32:21 +01:00
|
|
|
if (noExistingLayout && w && w->layout()) {
|
2010-02-28 16:13:54 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-11-18 23:53:41 +01:00
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QScriptValue ctor(QScriptContext *ctx, QScriptEngine *eng)
|
|
|
|
{
|
2010-02-28 16:32:21 +01:00
|
|
|
QGraphicsLayoutItem *parent = extractLayoutItem(ctx, 0, true);
|
2010-02-28 16:13:54 +01:00
|
|
|
//FIXME: don't leak memory when parent is 0
|
2009-11-18 23:53:41 +01:00
|
|
|
return qScriptValueFromValue(eng, new QGraphicsLinearLayout(parent));
|
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, orientation) {
|
2010-01-06 00:48:56 +01:00
|
|
|
if (ctx->argumentCount() > 0) {
|
|
|
|
self->setOrientation(static_cast<Qt::Orientation>(ctx->argument(0).toInt32()));
|
|
|
|
}
|
2009-11-18 23:53:41 +01:00
|
|
|
|
2010-01-06 00:48:56 +01:00
|
|
|
return QScriptValue(eng, static_cast<int>(self->orientation()));
|
2009-11-18 23:53:41 +01:00
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, setAlignment) {
|
2010-02-23 10:41:14 +01:00
|
|
|
QGraphicsLayoutItem *item = extractLayoutItem(ctx);
|
2009-11-18 23:53:41 +01:00
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return eng->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
self->setAlignment(item, static_cast<Qt::Alignment>(ctx->argument(1).toInt32()));
|
|
|
|
return eng->undefinedValue();
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, insertItem) {
|
2010-02-23 10:41:14 +01:00
|
|
|
QGraphicsLayoutItem *item = extractLayoutItem(ctx, 1);
|
2009-11-18 23:53:41 +01:00
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return eng->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
self->insertItem(ctx->argument(0).toInt32(), item);
|
|
|
|
return eng->undefinedValue();
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, removeItem) {
|
2010-02-23 10:41:14 +01:00
|
|
|
QGraphicsLayoutItem *item = extractLayoutItem(ctx);
|
2009-11-18 23:53:41 +01:00
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return eng->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
self->removeItem(item);
|
|
|
|
return eng->undefinedValue();
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
2010-04-01 21:57:13 +02:00
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, addStretch) {
|
|
|
|
self->addStretch(qMax(1, ctx->argument(0).toInt32()));
|
|
|
|
return eng->undefinedValue();
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
2009-11-18 23:53:41 +01:00
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, setStretchFactor) {
|
|
|
|
QGraphicsLayoutItem *item = ctx->argument(0).toVariant().value<QGraphicsLayoutItem*>();
|
|
|
|
|
|
|
|
if (!item) {
|
|
|
|
return eng->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
self->setStretchFactor(item, static_cast<Qt::Orientation>(ctx->argument(1).toInt32()));
|
|
|
|
return eng->undefinedValue();
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, addItem) {
|
2010-02-23 10:41:14 +01:00
|
|
|
QGraphicsLayoutItem *item = extractLayoutItem(ctx);
|
2009-11-18 23:53:41 +01:00
|
|
|
if (!item) {
|
|
|
|
return ctx->throwError(QScriptContext::TypeError,
|
|
|
|
"QGraphicsLinearLayout.prototype.addItem: argument is not a GraphicsLayoutItem");
|
|
|
|
}
|
|
|
|
|
|
|
|
self->addItem(item);
|
|
|
|
return eng->undefinedValue();
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsItem, toString) {
|
|
|
|
return QScriptValue(eng, "QGraphicsLinearLayout");
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
2010-01-06 00:48:56 +01:00
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, count) {
|
|
|
|
return QScriptValue(eng, self->count());
|
|
|
|
} END_DECLARE_METHOD
|
2009-11-18 23:53:41 +01:00
|
|
|
|
2010-01-06 00:48:56 +01:00
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, itemAt) {
|
|
|
|
if (ctx->argumentCount() < 1) {
|
|
|
|
return eng->undefinedValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
int index = ctx->argument(0).toInt32();
|
|
|
|
return qScriptValueFromValue(eng, self->itemAt(index));
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
|
|
|
BEGIN_DECLARE_METHOD(QGraphicsLinearLayout, spacing) {
|
|
|
|
if (ctx->argumentCount() > 0) {
|
|
|
|
int pixels = ctx->argument(0).toInt32();
|
|
|
|
self->setSpacing(pixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
return QScriptValue(eng, self->spacing());
|
|
|
|
} END_DECLARE_METHOD
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////
|
2009-11-18 23:53:41 +01:00
|
|
|
|
|
|
|
QScriptValue constructLinearLayoutClass(QScriptEngine *eng)
|
|
|
|
{
|
|
|
|
// QScriptValue proto = QScript::wrapGVPointer<QGraphicsLinearLayout>(eng, new QGraphicsLinearLayout(), );
|
2010-02-23 10:41:14 +01:00
|
|
|
qRegisterMetaType<QGraphicsLayoutItem*>();
|
|
|
|
QGraphicsLayoutItem * i = new QGraphicsLinearLayout;
|
|
|
|
QVariant v;
|
|
|
|
///v.setValue<QGraphicsLayoutItem*>(i);
|
|
|
|
v.setValue<void*>(i);
|
2009-11-18 23:53:41 +01:00
|
|
|
QScriptValue proto = QScript::wrapPointer<QGraphicsLinearLayout>(eng, new QGraphicsLinearLayout(), QScript::UserOwnership);
|
2009-11-27 22:15:25 +01:00
|
|
|
const QScriptValue::PropertyFlags getter = QScriptValue::PropertyGetter;
|
|
|
|
const QScriptValue::PropertyFlags setter = QScriptValue::PropertySetter;
|
2010-01-06 00:48:56 +01:00
|
|
|
proto.setProperty("count", eng->newFunction(count), getter);
|
|
|
|
proto.setProperty("spacing", eng->newFunction(spacing), getter | setter);
|
|
|
|
proto.setProperty("orientation", eng->newFunction(orientation), getter | setter);
|
|
|
|
ADD_METHOD(proto, itemAt);
|
2009-11-18 23:53:41 +01:00
|
|
|
ADD_METHOD(proto, removeAt);
|
|
|
|
ADD_METHOD(proto, addStretch);
|
|
|
|
ADD_METHOD(proto, setStretchFactor);
|
|
|
|
ADD_METHOD(proto, setAlignment);
|
|
|
|
ADD_METHOD(proto, insertStretch);
|
|
|
|
ADD_METHOD(proto, setItemSpacing);
|
|
|
|
ADD_METHOD(proto, setContentsMargins);
|
|
|
|
ADD_METHOD(proto, addItem);
|
|
|
|
ADD_METHOD(proto, removeItem);
|
|
|
|
ADD_METHOD(proto, insertItem);
|
|
|
|
ADD_METHOD(proto, toString);
|
|
|
|
|
|
|
|
QScript::registerPointerMetaType<QGraphicsLinearLayout>(eng, proto);
|
|
|
|
|
|
|
|
QScriptValue ctorFun = eng->newFunction(ctor, proto);
|
|
|
|
//ADD_ENUM_VALUE(ctorFun, QGraphicsItem, ItemIsMovable);
|
|
|
|
|
|
|
|
return ctorFun;
|
|
|
|
}
|