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();
82 void DeclarativeWidgetPrivate::errorPrint()
84 QString errorStr =
"Error loading QML file.\n";
85 if(component->isError()){
92 kWarning() << component->url().toString() +
'\n' + errorStr;
95 void DeclarativeWidgetPrivate::execute(
const QString &fileName)
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);
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!";
151 q->setPreferredSize(-1,-1);
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());
171 minimumWidth =
object->property(
"minimumWidth").toReal();
172 QObject::connect(
object, SIGNAL(minimumWidthChanged()), q, SLOT(minimumWidthChanged()));
175 minimumHeight =
object->property(
"minimumHeight").toReal();
176 QObject::connect(
object, SIGNAL(minimumHeightChanged()), q, SLOT(minimumHeightChanged()));
180 maximumWidth =
object->property(
"maximumWidth").toReal();
181 QObject::connect(
object, SIGNAL(maximumWidthChanged()), q, SLOT(maximumWidthChanged()));
184 maximumHeight =
object->property(
"maximumHeight").toReal();
185 QObject::connect(
object, SIGNAL(maximumHeightChanged()), q, SLOT(maximumHeightChanged()));
189 implicitWidth =
object->property(
"implicitWidth").toReal();
190 QObject::connect(
object, SIGNAL(implicitWidthChanged()), q, SLOT(implicitWidthChanged()));
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);
262 d->engine->setNetworkAccessManagerFactory(
new DeclarativeNetworkAccessManagerFactory);
270 d->engine->setNetworkAccessManagerFactory(0);
303 return d->scriptEngine;
321 d->root->setProperty(
"width",
size().width());
322 d->root->setProperty(
"height",
size().height());
329 #include <declarativewidget.moc>
void setContentsMargins(qreal left, qreal top, qreal right, qreal bottom)
void setFlag(GraphicsItemFlag flag, bool enabled)
QString number(int n, int base)
QString description() const
void addItem(QGraphicsLayoutItem *item)
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)