23 #include <QtDeclarative/QDeclarativeComponent>
24 #include <QtDeclarative/QDeclarativeItem>
25 #include <QtDeclarative/QDeclarativeEngine>
26 #include <QtDeclarative/QDeclarativeContext>
27 #include <QScriptEngine>
28 #include <QGraphicsLinearLayout>
29 #include <QGraphicsScene>
33 #include <kdeclarative.h>
35 #include <kstandarddirs.h>
37 #include "private/declarative/declarativenetworkaccessmanagerfactory_p.h"
38 #include "private/dataenginebindings_p.h"
43 class DeclarativeWidgetPrivate
46 DeclarativeWidgetPrivate(DeclarativeWidget *parent)
55 ~DeclarativeWidgetPrivate()
60 void execute(
const QString &fileName);
62 void scheduleExecutionEnd();
63 void minimumWidthChanged();
64 void minimumHeightChanged();
65 void maximumWidthChanged();
66 void maximumHeightChanged();
67 void preferredWidthChanged();
68 void preferredHeightChanged();
74 QDeclarativeEngine* engine;
75 QScriptEngine *scriptEngine;
76 QDeclarativeComponent* component;
81 void DeclarativeWidgetPrivate::errorPrint()
83 QString errorStr =
"Error loading QML file.\n";
84 if(component->isError()){
85 QList<QDeclarativeError> errors = component->errors();
86 foreach (
const QDeclarativeError &error, errors) {
87 errorStr += (error.line()>0?QString(QString::number(error.line()) + QLatin1String(
": ")):QLatin1String(
""))
88 + error.description() +
'\n';
91 kWarning() << component->url().toString() +
'\n' + errorStr;
94 void DeclarativeWidgetPrivate::execute(
const QString &fileName)
96 if (fileName.isEmpty()) {
97 kDebug() <<
"File name empty!";
101 KDeclarative kdeclarative;
102 kdeclarative.setDeclarativeEngine(engine);
103 kdeclarative.initialize();
105 kdeclarative.setupBindings();
107 component->loadUrl(fileName);
109 scriptEngine = kdeclarative.scriptEngine();
110 registerDataEngineMetaTypes(scriptEngine);
113 QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));
115 scheduleExecutionEnd();
119 void DeclarativeWidgetPrivate::scheduleExecutionEnd()
121 if (component->isReady() || component->isError()) {
124 QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(finishExecute()));
128 void DeclarativeWidgetPrivate::finishExecute()
130 if (component->isError()) {
134 root = component->create();
140 kDebug() <<
"Execution of QML done!";
142 QGraphicsObject *
object =
dynamic_cast<QGraphicsObject *
>(root);
146 static_cast<QGraphicsItem *
>(object)->setParentItem(q);
148 q->scene()->addItem(
object);
153 q->setPreferredSize(-1,-1);
154 QGraphicsLinearLayout *lay =
static_cast<QGraphicsLinearLayout *
>(q->layout());
156 lay =
new QGraphicsLinearLayout(q);
157 lay->setContentsMargins(0, 0, 0, 0);
159 lay->addItem(widget);
162 qreal minimumWidth = 0;
163 qreal minimumHeight = 0;
164 qreal maximumWidth = 0;
165 qreal maximumHeight = 0;
166 qreal preferredWidth = 0;
167 qreal preferredHeight = 0;
169 object->setProperty(
"width", q->size().width());
170 object->setProperty(
"height", q->size().height());
172 minimumWidth =
object->property(
"minimumWidth").toReal();
173 minimumHeight =
object->property(
"minimumHeight").toReal();
174 QObject::connect(
object, SIGNAL(minimumWidthChanged()), q, SLOT(minimumWidthChanged()));
175 QObject::connect(
object, SIGNAL(minimumHeightChanged()), q, SLOT(minimumHeightChanged()));
177 maximumWidth =
object->property(
"maximumWidth").toReal();
178 maximumHeight =
object->property(
"maximumHeight").toReal();
179 QObject::connect(
object, SIGNAL(maximumWidthChanged()), q, SLOT(maximumWidthChanged()));
180 QObject::connect(
object, SIGNAL(maximumHeightChanged()), q, SLOT(maximumHeightChanged()));
182 preferredWidth =
object->property(
"preferredWidth").toReal();
183 preferredHeight =
object->property(
"preferredHeight").toReal();
184 QObject::connect(
object, SIGNAL(preferredWidthChanged()), q, SLOT(preferredWidthChanged()));
185 QObject::connect(
object, SIGNAL(preferredHeightChanged()), q, SLOT(preferredHeightChanged()));
188 if (minimumWidth > 0 && minimumHeight > 0) {
189 q->setMinimumSize(minimumWidth, minimumHeight);
191 q->setMinimumSize(-1, -1);
194 if (maximumWidth > 0 && maximumHeight > 0) {
195 q->setMaximumSize(maximumWidth, maximumHeight);
197 q->setMaximumSize(-1, -1);
200 if (preferredWidth > 0 && preferredHeight > 0) {
201 q->setPreferredSize(preferredWidth, preferredHeight);
203 q->setPreferredSize(-1, -1);
209 void DeclarativeWidgetPrivate::minimumWidthChanged()
211 qreal minimumWidth = root->property(
"minimumWidth").toReal();
212 q->setMinimumWidth(minimumWidth);
215 void DeclarativeWidgetPrivate::minimumHeightChanged()
217 qreal minimumHeight = root->property(
"minimumHeight").toReal();
218 q->setMinimumHeight(minimumHeight);
221 void DeclarativeWidgetPrivate::maximumWidthChanged()
223 qreal maximumWidth = root->property(
"maximumWidth").toReal();
224 q->setMaximumWidth(maximumWidth);
227 void DeclarativeWidgetPrivate::maximumHeightChanged()
229 qreal maximumHeight = root->property(
"maximumHeight").toReal();
230 q->setMaximumHeight(maximumHeight);
233 void DeclarativeWidgetPrivate::preferredWidthChanged()
235 qreal preferredWidth = root->property(
"preferredWidth").toReal();
236 q->setPreferredWidth(preferredWidth);
239 void DeclarativeWidgetPrivate::preferredHeightChanged()
241 qreal preferredHeight = root->property(
"preferredHeight").toReal();
242 q->setPreferredHeight(preferredHeight);
247 d(new DeclarativeWidgetPrivate(this))
249 setFlag(QGraphicsItem::ItemHasNoContents);
251 d->engine =
new QDeclarativeEngine(
this);
252 d->engine->setNetworkAccessManagerFactory(
new DeclarativeNetworkAccessManagerFactory);
254 d->component =
new QDeclarativeComponent(d->engine,
this);
259 QDeclarativeNetworkAccessManagerFactory *factory = d->engine->networkAccessManagerFactory();
260 d->engine->setNetworkAccessManagerFactory(0);
293 return d->scriptEngine;
311 d->root->setProperty(
"width", size().width());
312 d->root->setProperty(
"height", size().height());
319 #include <declarativewidget.moc>