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)
56 ~DeclarativeWidgetPrivate()
61 void execute(
const QString &fileName);
63 void scheduleExecutionEnd();
64 void minimumWidthChanged();
65 void minimumHeightChanged();
66 void maximumWidthChanged();
67 void maximumHeightChanged();
68 void implicitWidthChanged();
69 void implicitHeightChanged();
75 QDeclarativeEngine* engine;
76 QScriptEngine *scriptEngine;
77 QDeclarativeComponent* component;
82 void DeclarativeWidgetPrivate::errorPrint()
84 QString errorStr =
"Error loading QML file.\n";
85 if(component->isError()){
86 QList<QDeclarativeError> errors = component->errors();
87 foreach (
const QDeclarativeError &error, errors) {
88 errorStr += (error.line()>0?QString(QString::number(error.line()) + QLatin1String(
": ")):QLatin1String(
""))
89 + error.description() +
'\n';
92 kWarning() << component->url().toString() +
'\n' + errorStr;
95 void DeclarativeWidgetPrivate::execute(
const QString &fileName)
97 if (fileName.isEmpty()) {
98 kDebug() <<
"File name empty!";
102 KDeclarative kdeclarative;
103 kdeclarative.setDeclarativeEngine(engine);
104 kdeclarative.initialize();
106 kdeclarative.setupBindings();
108 component->loadUrl(fileName);
110 scriptEngine = kdeclarative.scriptEngine();
111 registerDataEngineMetaTypes(scriptEngine);
114 QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));
116 scheduleExecutionEnd();
120 void DeclarativeWidgetPrivate::scheduleExecutionEnd()
122 if (component->isReady() || component->isError()) {
125 QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(finishExecute()));
129 void DeclarativeWidgetPrivate::finishExecute()
131 if (component->isError()) {
135 root = component->create();
141 kDebug() <<
"Execution of QML done!";
143 QGraphicsObject *
object =
dynamic_cast<QGraphicsObject *
>(root);
147 static_cast<QGraphicsItem *
>(object)->setParentItem(q);
151 q->setPreferredSize(-1,-1);
152 QGraphicsLinearLayout *lay =
static_cast<QGraphicsLinearLayout *
>(q->layout());
154 lay =
new QGraphicsLinearLayout(q);
155 lay->setContentsMargins(0, 0, 0, 0);
157 lay->addItem(widget);
160 qreal minimumWidth = 0;
161 qreal minimumHeight = 0;
162 qreal maximumWidth = 0;
163 qreal maximumHeight = 0;
164 qreal implicitWidth = 0;
165 qreal implicitHeight = 0;
167 object->setProperty(
"width", q->size().width());
168 object->setProperty(
"height", q->size().height());
170 if (object->metaObject()->indexOfProperty(
"minimumWidth")>=0) {
171 minimumWidth =
object->property(
"minimumWidth").toReal();
172 QObject::connect(
object, SIGNAL(minimumWidthChanged()), q, SLOT(minimumWidthChanged()));
174 if (object->metaObject()->indexOfProperty(
"minimumHeight")>=0) {
175 minimumHeight =
object->property(
"minimumHeight").toReal();
176 QObject::connect(
object, SIGNAL(minimumHeightChanged()), q, SLOT(minimumHeightChanged()));
179 if (object->metaObject()->indexOfProperty(
"maximumWidth")>=0) {
180 maximumWidth =
object->property(
"maximumWidth").toReal();
181 QObject::connect(
object, SIGNAL(maximumWidthChanged()), q, SLOT(maximumWidthChanged()));
183 if (object->metaObject()->indexOfProperty(
"maximumHeight")>=0) {
184 maximumHeight =
object->property(
"maximumHeight").toReal();
185 QObject::connect(
object, SIGNAL(maximumHeightChanged()), q, SLOT(maximumHeightChanged()));
188 if (object->metaObject()->indexOfProperty(
"implicitWidth")>=0) {
189 implicitWidth =
object->property(
"implicitWidth").toReal();
190 QObject::connect(
object, SIGNAL(implicitWidthChanged()), q, SLOT(implicitWidthChanged()));
192 if (object->metaObject()->indexOfProperty(
"implicitHeight")>=0) {
193 implicitHeight =
object->property(
"implicitHeight").toReal();
194 QObject::connect(
object, SIGNAL(implicitHeightChanged()), q, SLOT(implicitHeightChanged()));
198 if (minimumWidth > 0 && minimumHeight > 0) {
199 q->setMinimumSize(minimumWidth, minimumHeight);
201 q->setMinimumSize(-1, -1);
204 if (maximumWidth > 0 && maximumHeight > 0) {
205 q->setMaximumSize(maximumWidth, maximumHeight);
207 q->setMaximumSize(-1, -1);
210 if (implicitWidth > 0 && implicitHeight > 0) {
211 q->setPreferredSize(implicitWidth, implicitHeight);
213 q->setPreferredSize(-1, -1);
219 void DeclarativeWidgetPrivate::minimumWidthChanged()
221 qreal minimumWidth = root->property(
"minimumWidth").toReal();
222 q->setMinimumWidth(minimumWidth);
225 void DeclarativeWidgetPrivate::minimumHeightChanged()
227 qreal minimumHeight = root->property(
"minimumHeight").toReal();
228 q->setMinimumHeight(minimumHeight);
231 void DeclarativeWidgetPrivate::maximumWidthChanged()
233 qreal maximumWidth = root->property(
"maximumWidth").toReal();
234 q->setMaximumWidth(maximumWidth);
237 void DeclarativeWidgetPrivate::maximumHeightChanged()
239 qreal maximumHeight = root->property(
"maximumHeight").toReal();
240 q->setMaximumHeight(maximumHeight);
243 void DeclarativeWidgetPrivate::implicitWidthChanged()
245 qreal implicitWidth = root->property(
"implicitWidth").toReal();
246 q->setPreferredWidth(implicitWidth);
249 void DeclarativeWidgetPrivate::implicitHeightChanged()
251 qreal implicitHeight = root->property(
"implicitHeight").toReal();
252 q->setPreferredHeight(implicitHeight);
257 d(new DeclarativeWidgetPrivate(this))
259 setFlag(QGraphicsItem::ItemHasNoContents);
261 d->engine =
new QDeclarativeEngine(
this);
262 d->engine->setNetworkAccessManagerFactory(
new DeclarativeNetworkAccessManagerFactory);
264 d->component =
new QDeclarativeComponent(d->engine,
this);
269 QDeclarativeNetworkAccessManagerFactory *factory = d->engine->networkAccessManagerFactory();
270 d->engine->setNetworkAccessManagerFactory(0);
303 return d->scriptEngine;
318 QGraphicsWidget::resizeEvent(event);
321 d->root->setProperty(
"width", size().width());
322 d->root->setProperty(
"height", size().height());
329 #include <declarativewidget.moc>