Support for Anchor Layout (Qt 4.6)

Now we have support for anchor layout on javascript
plasmoids. This is at 90%. It's not working properly yet
as we need either a patch in Qt (being discussed) or another
not so beautiful approach. After this it will work 98%, and
I'll just need to do the QGraphicsAnchor bindings to support
spacing and size policies of anchors.


svn path=/trunk/KDE/kdebase/runtime/; revision=1044107
This commit is contained in:
Artur Duque de Souza 2009-11-03 03:06:04 +00:00
parent 9d6f1e8a8f
commit ad5e59fdeb
4 changed files with 137 additions and 0 deletions

View File

@ -7,6 +7,7 @@ set(simple_javascript_engine_SRCS
qtgui/font.cpp
qtgui/graphicsitem.cpp
qtgui/linearlayout.cpp
qtgui/anchorlayout.cpp
qtgui/painter.cpp
qtgui/pixmap.cpp
qtgui/point.cpp

View File

@ -44,6 +44,7 @@ class AppletInterface : public QObject
Q_ENUMS(Location)
Q_ENUMS(AspectRatioMode)
Q_ENUMS(QtOrientation)
Q_ENUMS(QtAnchorPoint)
Q_ENUMS(QtAlignment)
Q_PROPERTY(QString activeConfig WRITE setActiveConfig READ activeConfig)
Q_PROPERTY(bool busy WRITE setBusy READ isBusy)
@ -101,6 +102,15 @@ enum QtOrientation {
QtVertical = Qt::Vertical
};
enum QtAnchorPoint {
QtAnchorLeft = Qt::AnchorLeft,
QtAnchorRight = Qt::AnchorRight,
QtAnchorBottom = Qt::AnchorBottom,
QtAnchorTop = Qt::AnchorTop,
QtAnchorHorizontalCenter = Qt::AnchorHorizontalCenter,
QtAnchorVerticalCenter = Qt::AnchorVerticalCenter
};
enum QtAlignment {
QtAlignLeft = 0x0001,
QtAlignRight = 0x0002,
@ -110,6 +120,7 @@ enum QtAlignment {
QtAlignBottom = 0x0020,
QtAlignVCenter = 0x0080
};
//-------------------------------------------------------------------
Q_INVOKABLE FormFactor formFactor();

View File

@ -0,0 +1,123 @@
/*
* Copyright 2007 Richard J. Moore <rich@kde.org>
* Copyright 2009 Artur Duque de Souza <asouza@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>
#include <QtGui/QGraphicsWidget>
#include <QtGui/QGraphicsAnchorLayout>
#include <Plasma/Applet>
#include "../backportglobal.h"
#include "../appletinterface.h"
Q_DECLARE_METATYPE(QScript::Pointer<QGraphicsItem>::wrapped_pointer_type)
Q_DECLARE_METATYPE(QGraphicsWidget*)
Q_DECLARE_METATYPE(QGraphicsAnchor*)
Q_DECLARE_METATYPE(QGraphicsLayoutItem*)
DECLARE_POINTER_METATYPE(QGraphicsAnchorLayout)
// QGraphicsAnchorLayout
DECLARE_VOID_NUMBER_METHOD(QGraphicsAnchorLayout, setSpacing)
DECLARE_NUMBER_GET_SET_METHODS(QGraphicsAnchorLayout, horizontalSpacing, setHorizontalSpacing);
DECLARE_NUMBER_GET_SET_METHODS(QGraphicsAnchorLayout, verticalSpacing, setVerticalSpacing);
DECLARE_VOID_NUMBER_METHOD(QGraphicsAnchorLayout, removeAt)
/////////////////////////////////////////////////////////////
QGraphicsLayoutItem *convertToLayoutItem (QScriptContext *ctx, int index = 0)
{
QObject *object = ctx->argument(index).toQObject();
QGraphicsLayoutItem *item = qobject_cast<QGraphicsWidget*>(object);
if (!item) {
item = qscriptvalue_cast<QGraphicsAnchorLayout*>(ctx->argument(index));
}
if (!item) {
AppletInterface *interface = qobject_cast<AppletInterface*>(object);
if (!interface) {
interface = qobject_cast<AppletInterface*>(ctx->engine()->globalObject().property("plasmoid").toQObject());
}
if (interface) {
item = interface->applet();
}
}
return item;
}
static QScriptValue ctor(QScriptContext *ctx, QScriptEngine *eng)
{
QGraphicsLayoutItem *parent = convertToLayoutItem(ctx);
if (!parent) {
return ctx->throwError(i18n("The parent must be a QGraphicsLayoutItem"));
}
return qScriptValueFromValue(eng, new QGraphicsAnchorLayout(parent));
}
BEGIN_DECLARE_METHOD(QGraphicsAnchorLayout, addAnchor) {
QGraphicsLayoutItem *item1 = convertToLayoutItem(ctx, 0);
QGraphicsLayoutItem *item2 = convertToLayoutItem(ctx, 2);
if (!item1 || !item2) {
return eng->undefinedValue();
}
QGraphicsAnchor *anchor = self->addAnchor(item1, static_cast<Qt::AnchorPoint>(ctx->argument(1).toInt32()),
item2, static_cast<Qt::AnchorPoint>(ctx->argument(3).toInt32()));
return qScriptValueFromValue(eng, anchor);
} END_DECLARE_METHOD
BEGIN_DECLARE_METHOD(QGraphicsItem, toString) {
return QScriptValue(eng, "QGraphicsAnchorLayout");
} END_DECLARE_METHOD
/////////////////////////////////////////////////////////////
class PrototypeAnchorLayout : public QGraphicsAnchorLayout
{
public:
PrototypeAnchorLayout()
{ }
};
QScriptValue constructAnchorLayoutClass(QScriptEngine *eng)
{
QScriptValue proto = QScript::wrapPointer<QGraphicsAnchorLayout>(eng, new QGraphicsAnchorLayout(), QScript::UserOwnership);
ADD_METHOD(proto, setSpacing);
ADD_GET_SET_METHODS(proto, horizontalSpacing, setHorizontalSpacing);
ADD_GET_SET_METHODS(proto, verticalSpacing, setVerticalSpacing);
ADD_METHOD(proto, removeAt);
ADD_METHOD(proto, addAnchor);
ADD_METHOD(proto, toString);
QScript::registerPointerMetaType<QGraphicsAnchorLayout>(eng, proto);
QScriptValue ctorFun = eng->newFunction(ctor, proto);
return ctorFun;
}

View File

@ -61,6 +61,7 @@ QScriptValue constructFontClass(QScriptEngine *engine);
QScriptValue constructGraphicsItemClass(QScriptEngine *engine);
QScriptValue constructKUrlClass(QScriptEngine *engine);
QScriptValue constructLinearLayoutClass(QScriptEngine *engine);
QScriptValue constructAnchorLayoutClass(QScriptEngine *engine);
QScriptValue constructPainterClass(QScriptEngine *engine);
QScriptValue constructQPixmapClass(QScriptEngine *engine);
QScriptValue constructQPointClass(QScriptEngine *engine);
@ -462,6 +463,7 @@ void SimpleJavaScriptApplet::setupObjects()
global.setProperty("QSizeF", constructQSizeFClass(m_engine));
global.setProperty("QPoint", constructQPointClass(m_engine));
global.setProperty("LinearLayout", constructLinearLayoutClass(m_engine));
global.setProperty("AnchorLayout", constructAnchorLayoutClass(m_engine));
// Add stuff from KDE libs
global.setProperty("Url", constructKUrlClass(m_engine));