• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • plasma
  • widgets
declarativewidget.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2010 Marco Martin <mart@kde.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Library General Public License as
6 * published by the Free Software Foundation; either version 2, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this program; if not, write to the
16 * Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19
20#include "declarativewidget.h"
21
22
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>
30#include <QTimer>
31
32#include <kdebug.h>
33#include <kdeclarative.h>
34#include <kglobal.h>
35#include <kstandarddirs.h>
36
37#include "private/declarative/declarativenetworkaccessmanagerfactory_p.h"
38#include "private/dataenginebindings_p.h"
39
40namespace Plasma
41{
42
43class DeclarativeWidgetPrivate
44{
45public:
46 DeclarativeWidgetPrivate(DeclarativeWidget *parent)
47 : q(parent),
48 engine(0),
49 scriptEngine(0),
50 component(0),
51 root(0),
52 delay(false)
53 {
54 }
55
56 ~DeclarativeWidgetPrivate()
57 {
58 }
59
60 void errorPrint();
61 void execute(const QString &fileName);
62 void finishExecute();
63 void scheduleExecutionEnd();
64 void minimumWidthChanged();
65 void minimumHeightChanged();
66 void maximumWidthChanged();
67 void maximumHeightChanged();
68 void implicitWidthChanged();
69 void implicitHeightChanged();
70
71
72 DeclarativeWidget *q;
73
74 QString qmlPath;
75 QDeclarativeEngine* engine;
76 QScriptEngine *scriptEngine;
77 QDeclarativeComponent* component;
78 QObject *root;
79 bool delay : 1;
80};
81
82void DeclarativeWidgetPrivate::errorPrint()
83{
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';
90 }
91 }
92 kWarning() << component->url().toString() + '\n' + errorStr;
93}
94
95void DeclarativeWidgetPrivate::execute(const QString &fileName)
96{
97 if (fileName.isEmpty()) {
98 kDebug() << "File name empty!";
99 return;
100 }
101
102 KDeclarative kdeclarative;
103 kdeclarative.setDeclarativeEngine(engine);
104 kdeclarative.initialize();
105 //binds things like kconfig and icons
106 kdeclarative.setupBindings();
107
108 component->loadUrl(fileName);
109
110 scriptEngine = kdeclarative.scriptEngine();
111 registerDataEngineMetaTypes(scriptEngine);
112
113 if (delay) {
114 QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));
115 } else {
116 scheduleExecutionEnd();
117 }
118}
119
120void DeclarativeWidgetPrivate::scheduleExecutionEnd()
121{
122 if (component->isReady() || component->isError()) {
123 finishExecute();
124 } else {
125 QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(finishExecute()));
126 }
127}
128
129void DeclarativeWidgetPrivate::finishExecute()
130{
131 if (component->isError()) {
132 errorPrint();
133 }
134
135 root = component->create();
136
137 if (!root) {
138 errorPrint();
139 }
140
141 kDebug() << "Execution of QML done!";
142 QGraphicsWidget *widget = dynamic_cast<QGraphicsWidget*>(root);
143 QGraphicsObject *object = dynamic_cast<QGraphicsObject *>(root);
144
145
146 if (object) {
147 static_cast<QGraphicsItem *>(object)->setParentItem(q);
148 }
149
150 if (widget) {
151 q->setPreferredSize(-1,-1);
152 QGraphicsLinearLayout *lay = static_cast<QGraphicsLinearLayout *>(q->layout());
153 if (!lay) {
154 lay = new QGraphicsLinearLayout(q);
155 lay->setContentsMargins(0, 0, 0, 0);
156 }
157 lay->addItem(widget);
158 } else {
159 q->setLayout(0);
160 qreal minimumWidth = 0;
161 qreal minimumHeight = 0;
162 qreal maximumWidth = 0;
163 qreal maximumHeight = 0;
164 qreal implicitWidth = 0;
165 qreal implicitHeight = 0;
166 if (object) {
167 object->setProperty("width", q->size().width());
168 object->setProperty("height", q->size().height());
169
170 if (object->metaObject()->indexOfProperty("minimumWidth")>=0) {
171 minimumWidth = object->property("minimumWidth").toReal();
172 QObject::connect(object, SIGNAL(minimumWidthChanged()), q, SLOT(minimumWidthChanged()));
173 }
174 if (object->metaObject()->indexOfProperty("minimumHeight")>=0) {
175 minimumHeight = object->property("minimumHeight").toReal();
176 QObject::connect(object, SIGNAL(minimumHeightChanged()), q, SLOT(minimumHeightChanged()));
177 }
178
179 if (object->metaObject()->indexOfProperty("maximumWidth")>=0) {
180 maximumWidth = object->property("maximumWidth").toReal();
181 QObject::connect(object, SIGNAL(maximumWidthChanged()), q, SLOT(maximumWidthChanged()));
182 }
183 if (object->metaObject()->indexOfProperty("maximumHeight")>=0) {
184 maximumHeight = object->property("maximumHeight").toReal();
185 QObject::connect(object, SIGNAL(maximumHeightChanged()), q, SLOT(maximumHeightChanged()));
186 }
187
188 if (object->metaObject()->indexOfProperty("implicitWidth")>=0) {
189 implicitWidth = object->property("implicitWidth").toReal();
190 QObject::connect(object, SIGNAL(implicitWidthChanged()), q, SLOT(implicitWidthChanged()));
191 }
192 if (object->metaObject()->indexOfProperty("implicitHeight")>=0) {
193 implicitHeight = object->property("implicitHeight").toReal();
194 QObject::connect(object, SIGNAL(implicitHeightChanged()), q, SLOT(implicitHeightChanged()));
195 }
196 }
197
198 if (minimumWidth > 0 && minimumHeight > 0) {
199 q->setMinimumSize(minimumWidth, minimumHeight);
200 } else {
201 q->setMinimumSize(-1, -1);
202 }
203
204 if (maximumWidth > 0 && maximumHeight > 0) {
205 q->setMaximumSize(maximumWidth, maximumHeight);
206 } else {
207 q->setMaximumSize(-1, -1);
208 }
209
210 if (implicitWidth > 0 && implicitHeight > 0) {
211 q->setPreferredSize(implicitWidth, implicitHeight);
212 } else {
213 q->setPreferredSize(-1, -1);
214 }
215 }
216 emit q->finished();
217}
218
219void DeclarativeWidgetPrivate::minimumWidthChanged()
220{
221 qreal minimumWidth = root->property("minimumWidth").toReal();
222 q->setMinimumWidth(minimumWidth);
223}
224
225void DeclarativeWidgetPrivate::minimumHeightChanged()
226{
227 qreal minimumHeight = root->property("minimumHeight").toReal();
228 q->setMinimumHeight(minimumHeight);
229}
230
231void DeclarativeWidgetPrivate::maximumWidthChanged()
232{
233 qreal maximumWidth = root->property("maximumWidth").toReal();
234 q->setMaximumWidth(maximumWidth);
235}
236
237void DeclarativeWidgetPrivate::maximumHeightChanged()
238{
239 qreal maximumHeight = root->property("maximumHeight").toReal();
240 q->setMaximumHeight(maximumHeight);
241}
242
243void DeclarativeWidgetPrivate::implicitWidthChanged()
244{
245 qreal implicitWidth = root->property("implicitWidth").toReal();
246 q->setPreferredWidth(implicitWidth);
247}
248
249void DeclarativeWidgetPrivate::implicitHeightChanged()
250{
251 qreal implicitHeight = root->property("implicitHeight").toReal();
252 q->setPreferredHeight(implicitHeight);
253}
254
255DeclarativeWidget::DeclarativeWidget(QGraphicsWidget *parent)
256 : QGraphicsWidget(parent),
257 d(new DeclarativeWidgetPrivate(this))
258{
259 setFlag(QGraphicsItem::ItemHasNoContents);
260
261 d->engine = new QDeclarativeEngine(this);
262 d->engine->setNetworkAccessManagerFactory(new DeclarativeNetworkAccessManagerFactory);
263
264 d->component = new QDeclarativeComponent(d->engine, this);
265}
266
267DeclarativeWidget::~DeclarativeWidget()
268{
269 QDeclarativeNetworkAccessManagerFactory *factory = d->engine->networkAccessManagerFactory();
270 d->engine->setNetworkAccessManagerFactory(0);
271 delete factory;
272 delete d;
273}
274
275void DeclarativeWidget::setQmlPath(const QString &path)
276{
277 d->qmlPath = path;
278 d->execute(path);
279}
280
281QString DeclarativeWidget::qmlPath() const
282{
283 return d->qmlPath;
284}
285
286void DeclarativeWidget::setInitializationDelayed(const bool delay)
287{
288 d->delay = delay;
289}
290
291bool DeclarativeWidget::isInitializationDelayed() const
292{
293 return d->delay;
294}
295
296QDeclarativeEngine* DeclarativeWidget::engine()
297{
298 return d->engine;
299}
300
301QScriptEngine *DeclarativeWidget::scriptEngine() const
302{
303 return d->scriptEngine;
304}
305
306QObject *DeclarativeWidget::rootObject() const
307{
308 return d->root;
309}
310
311QDeclarativeComponent *DeclarativeWidget::mainComponent() const
312{
313 return d->component;
314}
315
316void DeclarativeWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
317{
318 QGraphicsWidget::resizeEvent(event);
319
320 if (d->root) {
321 d->root->setProperty("width", size().width());
322 d->root->setProperty("height", size().height());
323 }
324}
325
326
327} // namespace Plasma
328
329#include <declarativewidget.moc>
330
QGraphicsWidget
QObject
declarativewidget.h
Plasma
Namespace for everything in libplasma.
Definition: abstractdialogmanager.cpp:25
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal