move into libplasma

svn path=/trunk/KDE/kdelibs/; revision=1183873
This commit is contained in:
Marco Martin 2010-10-08 14:48:03 +00:00
parent bd5dd2a688
commit 90165f119a
2 changed files with 353 additions and 0 deletions

View File

@ -0,0 +1,225 @@
/*
* Copyright 2010 Marco Martin <mart@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 as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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 "declarativewidget.h"
#include <QtDeclarative/QDeclarativeComponent>
#include <QtDeclarative/QDeclarativeItem>
#include <QtDeclarative/QDeclarativeEngine>
#include <QtDeclarative/QDeclarativeContext>
#include <QGraphicsLinearLayout>
#include <QGraphicsScene>
#include <QTimer>
#include <KDebug>
namespace Plasma
{
class DeclarativeWidgetPrivate
{
public:
DeclarativeWidgetPrivate(DeclarativeWidget *parent)
: q(parent),
engine(0),
component(0),
root(0),
delay(false)
{
}
~DeclarativeWidgetPrivate()
{
}
void errorPrint();
void execute(const QString &fileName);
void finishExecute();
void scheduleExecutionEnd();
DeclarativeWidget *q;
QString qmlPath;
QDeclarativeEngine* engine;
QDeclarativeComponent* component;
QObject *root;
bool delay : 1;
};
void DeclarativeWidgetPrivate::errorPrint()
{
QString errorStr = "Error loading QML file.\n";
if(component->isError()){
QList<QDeclarativeError> errors = component->errors();
foreach (const QDeclarativeError &error, errors) {
errorStr += (error.line()>0?QString::number(error.line()) + ": ":"")
+ error.description() + '\n';
}
}
kWarning() << component->url().toString() + '\n' + errorStr;
}
void DeclarativeWidgetPrivate::execute(const QString &fileName)
{
if (fileName.isEmpty()) {
kDebug() << "File name empty!";
return;
}
if (engine) {
delete engine;
}
if (component) {
delete component;
}
engine = new QDeclarativeEngine(q);
component = new QDeclarativeComponent(engine, fileName, q);
if (delay) {
QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));
} else {
scheduleExecutionEnd();
}
}
void DeclarativeWidgetPrivate::scheduleExecutionEnd()
{
if (component->isReady() || component->isError()) {
finishExecute();
} else {
QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(finishExecute()));
}
}
void DeclarativeWidgetPrivate::finishExecute()
{
if (component->isError()) {
errorPrint();
}
root = component->create();
if (!root) {
errorPrint();
}
kDebug() << "Execution of QML done!";
QGraphicsWidget *widget = dynamic_cast<QGraphicsWidget*>(root);
QGraphicsObject *object = dynamic_cast<QGraphicsObject *>(root);
if (object) {
static_cast<QGraphicsItem *>(object)->setParentItem(q);
if (q->scene()) {
q->scene()->addItem(object);
}
}
if (widget) {
q->setPreferredSize(-1,-1);
QGraphicsLinearLayout *lay = static_cast<QGraphicsLinearLayout *>(q->layout());
if (!lay) {
lay = new QGraphicsLinearLayout(q);
lay->setContentsMargins(0, 0, 0, 0);
}
lay->addItem(widget);
} else {
q->setLayout(0);
const qreal width = object->property("width").toReal();
const qreal height = object->property("height").toReal();
if (width > 0 && height > 0) {
q->setPreferredSize(width, height);
} else {
q->setPreferredSize(-1, -1);
}
}
emit q->finished();
}
DeclarativeWidget::DeclarativeWidget(QGraphicsWidget *parent)
: QGraphicsWidget(parent),
d(new DeclarativeWidgetPrivate(this))
{
setFlag(QGraphicsItem::ItemHasNoContents);
}
DeclarativeWidget::~DeclarativeWidget()
{
delete d;
}
void DeclarativeWidget::setQmlPath(const QString &path)
{
d->qmlPath = path;
d->execute(path);
}
QString DeclarativeWidget::qmlPath() const
{
return d->qmlPath;
}
void DeclarativeWidget::setInitializationDelayed(const bool delay)
{
d->delay = delay;
}
bool DeclarativeWidget::isInitializationDelayed() const
{
return d->delay;
}
QDeclarativeEngine* DeclarativeWidget::engine()
{
return d->engine;
}
QObject *DeclarativeWidget::rootObject() const
{
return d->root;
}
QDeclarativeComponent *DeclarativeWidget::mainComponent() const
{
return d->component;
}
void DeclarativeWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
{
QGraphicsWidget::resizeEvent(event);
if (d->root) {
d->root->setProperty("width", size().width());
d->root->setProperty("height", size().height());
}
}
} // namespace Plasma
#include <declarativewidget.moc>

128
widgets/declarativewidget.h Normal file
View File

@ -0,0 +1,128 @@
/*
* Copyright 2010 Marco Martin <mart@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 as
* published by the Free Software Foundation; either version 2, or
* (at your option) any later version.
*
* 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.
*/
#ifndef PLASMA_DECLARATIVEWIDGET_H
#define PLASMA_DECLARATIVEWIDGET_H
#include <QtGui/QGraphicsWidget>
class QDeclarativeEngine;
class QDeclarativeComponent;
namespace Plasma
{
class DeclarativeWidgetPrivate;
/**
* @class DeclarativeWidget plasma/declarativewidget.h <Plasma/DeclarativeWidget>
*
* @author Marco Martin <mart@kde.org>
*
* @short A widget that contains an entire QML context, with its own declarative engine
*
* Plasma::DeclarativeWidget provides a class for conveniently use QML based
* declarative user interfaces inside Plasma widgets.
* To one DeclarativeWidget corresponds one QML file (that can eventually include others)
* tere will be its own QDeclarativeEngine with a single root object,
* described in the QML file.
*/
class DeclarativeWidget : public QGraphicsWidget
{
Q_OBJECT
Q_PROPERTY(QString qmlPath READ qmlPath WRITE setQmlPath)
Q_PROPERTY(bool initializationDelayed READ isInitializationDelayed WRITE setInitializationDelayed)
Q_PROPERTY(QObject * rootObject READ rootObject)
public:
/**
* Constructs a new DeclarativeWidget
*
* @param parent the parent of this widget
*/
explicit DeclarativeWidget(QGraphicsWidget *parent = 0);
~DeclarativeWidget();
/**
* Sets the path of the QML file to parse and execute
*
* @param path the absolute path of a QML file
*/
void setQmlPath(const QString &path);
/**
* @return the absolute path of the current QML file
*/
QString qmlPath() const;
/**
* Sets whether the execution of the QML file has to be delayed later in the event loop. It has to be called before setQmlPath().
* In this case will be possible to assign new objects in the main engine context
* before the main component gets initialized.
* So it will be possible to access it immediately from the QML code.
*
* @param delay if true the initilization of the QML file will be delayed
* at the end of the event loop
*/
void setInitializationDelayed(const bool delay);
/**
* @return true if the initilization of the QML file will be delayed
* at the end of the event loop
*/
bool isInitializationDelayed() const;
/**
* @return the declarative engine that runs the qml file assigned to this widget.
*/
QDeclarativeEngine* engine();
/**
* @return the root object of the declarative object tree
*/
QObject *rootObject() const;
/**
* @return the main QDeclarativeComponent of the engine
*/
QDeclarativeComponent *mainComponent() const;
protected:
void resizeEvent(QGraphicsSceneResizeEvent *event);
Q_SIGNALS:
/**
* Emitted when the parsing and execution of the QML file is terminated
*/
void finished();
private:
friend class DeclarativeWidgetPrivate;
DeclarativeWidgetPrivate * const d;
Q_PRIVATE_SLOT(d, void finishExecute())
Q_PRIVATE_SLOT(d, void scheduleExecutionEnd())
};
} // namespace Plasma
#endif // multiple inclusion guard