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

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • 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 
40 namespace Plasma
41 {
42 
43 class DeclarativeWidgetPrivate
44 {
45 public:
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 
82 void 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 
95 void 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 
120 void 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 
129 void 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 
219 void DeclarativeWidgetPrivate::minimumWidthChanged()
220 {
221  qreal minimumWidth = root->property("minimumWidth").toReal();
222  q->setMinimumWidth(minimumWidth);
223 }
224 
225 void DeclarativeWidgetPrivate::minimumHeightChanged()
226 {
227  qreal minimumHeight = root->property("minimumHeight").toReal();
228  q->setMinimumHeight(minimumHeight);
229 }
230 
231 void DeclarativeWidgetPrivate::maximumWidthChanged()
232 {
233  qreal maximumWidth = root->property("maximumWidth").toReal();
234  q->setMaximumWidth(maximumWidth);
235 }
236 
237 void DeclarativeWidgetPrivate::maximumHeightChanged()
238 {
239  qreal maximumHeight = root->property("maximumHeight").toReal();
240  q->setMaximumHeight(maximumHeight);
241 }
242 
243 void DeclarativeWidgetPrivate::implicitWidthChanged()
244 {
245  qreal implicitWidth = root->property("implicitWidth").toReal();
246  q->setPreferredWidth(implicitWidth);
247 }
248 
249 void DeclarativeWidgetPrivate::implicitHeightChanged()
250 {
251  qreal implicitHeight = root->property("implicitHeight").toReal();
252  q->setPreferredHeight(implicitHeight);
253 }
254 
255 DeclarativeWidget::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 
267 DeclarativeWidget::~DeclarativeWidget()
268 {
269  QDeclarativeNetworkAccessManagerFactory *factory = d->engine->networkAccessManagerFactory();
270  d->engine->setNetworkAccessManagerFactory(0);
271  delete factory;
272  delete d;
273 }
274 
275 void DeclarativeWidget::setQmlPath(const QString &path)
276 {
277  d->qmlPath = path;
278  d->execute(path);
279 }
280 
281 QString DeclarativeWidget::qmlPath() const
282 {
283  return d->qmlPath;
284 }
285 
286 void DeclarativeWidget::setInitializationDelayed(const bool delay)
287 {
288  d->delay = delay;
289 }
290 
291 bool DeclarativeWidget::isInitializationDelayed() const
292 {
293  return d->delay;
294 }
295 
296 QDeclarativeEngine* DeclarativeWidget::engine()
297 {
298  return d->engine;
299 }
300 
301 QScriptEngine *DeclarativeWidget::scriptEngine() const
302 {
303  return d->scriptEngine;
304 }
305 
306 QObject *DeclarativeWidget::rootObject() const
307 {
308  return d->root;
309 }
310 
311 QDeclarativeComponent *DeclarativeWidget::mainComponent() const
312 {
313  return d->component;
314 }
315 
316 void 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 
Plasma::DeclarativeWidget::~DeclarativeWidget
~DeclarativeWidget()
Definition: declarativewidget.cpp:267
Plasma::DeclarativeWidget::scriptEngine
QScriptEngine * scriptEngine() const
Definition: declarativewidget.cpp:301
Plasma::DeclarativeWidget::isInitializationDelayed
bool isInitializationDelayed() const
Definition: declarativewidget.cpp:291
QObject
Plasma::DeclarativeWidget::DeclarativeWidget
DeclarativeWidget(QGraphicsWidget *parent=0)
Constructs a new DeclarativeWidget.
Definition: declarativewidget.cpp:255
Plasma::DeclarativeWidget::setQmlPath
void setQmlPath(const QString &path)
Sets the path of the QML file to parse and execute.
Definition: declarativewidget.cpp:275
Plasma::DeclarativeWidget::mainComponent
QDeclarativeComponent * mainComponent() const
Definition: declarativewidget.cpp:311
Plasma::DeclarativeWidget::engine
QDeclarativeEngine * engine()
Definition: declarativewidget.cpp:296
Plasma::DeclarativeWidget::rootObject
QObject * rootObject() const
Plasma::DeclarativeWidget::qmlPath
QString qmlPath() const
Plasma::DeclarativeWidget::setInitializationDelayed
void setInitializationDelayed(const bool delay)
Sets whether the execution of the QML file has to be delayed later in the event loop.
Definition: declarativewidget.cpp:286
Plasma::DeclarativeWidget::resizeEvent
void resizeEvent(QGraphicsSceneResizeEvent *event)
Definition: declarativewidget.cpp:316
declarativewidget.h
QGraphicsWidget
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:33 by doxygen 1.8.7 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 API Reference

Skip menu "kdelibs 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
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



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