• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.9.4 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 
40 namespace Plasma
41 {
42 
43 class DeclarativeWidgetPrivate
44 {
45 public:
46  DeclarativeWidgetPrivate(DeclarativeWidget *parent)
47  : q(parent),
48  engine(0),
49  component(0),
50  root(0),
51  delay(false)
52  {
53  }
54 
55  ~DeclarativeWidgetPrivate()
56  {
57  }
58 
59  void errorPrint();
60  void execute(const QString &fileName);
61  void finishExecute();
62  void scheduleExecutionEnd();
63  void minimumWidthChanged();
64  void minimumHeightChanged();
65  void maximumWidthChanged();
66  void maximumHeightChanged();
67  void preferredWidthChanged();
68  void preferredHeightChanged();
69 
70 
71  DeclarativeWidget *q;
72 
73  QString qmlPath;
74  QDeclarativeEngine* engine;
75  QScriptEngine *scriptEngine;
76  QDeclarativeComponent* component;
77  QObject *root;
78  bool delay : 1;
79 };
80 
81 void DeclarativeWidgetPrivate::errorPrint()
82 {
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';
89  }
90  }
91  kWarning() << component->url().toString() + '\n' + errorStr;
92 }
93 
94 void DeclarativeWidgetPrivate::execute(const QString &fileName)
95 {
96  if (fileName.isEmpty()) {
97  kDebug() << "File name empty!";
98  return;
99  }
100 
101  KDeclarative kdeclarative;
102  kdeclarative.setDeclarativeEngine(engine);
103  kdeclarative.initialize();
104  //binds things like kconfig and icons
105  kdeclarative.setupBindings();
106 
107  component->loadUrl(fileName);
108 
109  scriptEngine = kdeclarative.scriptEngine();
110  registerDataEngineMetaTypes(scriptEngine);
111 
112  if (delay) {
113  QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));
114  } else {
115  scheduleExecutionEnd();
116  }
117 }
118 
119 void DeclarativeWidgetPrivate::scheduleExecutionEnd()
120 {
121  if (component->isReady() || component->isError()) {
122  finishExecute();
123  } else {
124  QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(finishExecute()));
125  }
126 }
127 
128 void DeclarativeWidgetPrivate::finishExecute()
129 {
130  if (component->isError()) {
131  errorPrint();
132  }
133 
134  root = component->create();
135 
136  if (!root) {
137  errorPrint();
138  }
139 
140  kDebug() << "Execution of QML done!";
141  QGraphicsWidget *widget = dynamic_cast<QGraphicsWidget*>(root);
142  QGraphicsObject *object = dynamic_cast<QGraphicsObject *>(root);
143 
144 
145  if (object) {
146  static_cast<QGraphicsItem *>(object)->setParentItem(q);
147  if (q->scene()) {
148  q->scene()->addItem(object);
149  }
150  }
151 
152  if (widget) {
153  q->setPreferredSize(-1,-1);
154  QGraphicsLinearLayout *lay = static_cast<QGraphicsLinearLayout *>(q->layout());
155  if (!lay) {
156  lay = new QGraphicsLinearLayout(q);
157  lay->setContentsMargins(0, 0, 0, 0);
158  }
159  lay->addItem(widget);
160  } else {
161  q->setLayout(0);
162  qreal minimumWidth = 0;
163  qreal minimumHeight = 0;
164  qreal maximumWidth = 0;
165  qreal maximumHeight = 0;
166  qreal preferredWidth = 0;
167  qreal preferredHeight = 0;
168  if (object) {
169  object->setProperty("width", q->size().width());
170  object->setProperty("height", q->size().height());
171 
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()));
176 
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()));
181 
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()));
186  }
187 
188  if (minimumWidth > 0 && minimumHeight > 0) {
189  q->setMinimumSize(minimumWidth, minimumHeight);
190  } else {
191  q->setMinimumSize(-1, -1);
192  }
193 
194  if (maximumWidth > 0 && maximumHeight > 0) {
195  q->setMaximumSize(maximumWidth, maximumHeight);
196  } else {
197  q->setMaximumSize(-1, -1);
198  }
199 
200  if (preferredWidth > 0 && preferredHeight > 0) {
201  q->setPreferredSize(preferredWidth, preferredHeight);
202  } else {
203  q->setPreferredSize(-1, -1);
204  }
205  }
206  emit q->finished();
207 }
208 
209 void DeclarativeWidgetPrivate::minimumWidthChanged()
210 {
211  qreal minimumWidth = root->property("minimumWidth").toReal();
212  q->setMinimumWidth(minimumWidth);
213 }
214 
215 void DeclarativeWidgetPrivate::minimumHeightChanged()
216 {
217  qreal minimumHeight = root->property("minimumHeight").toReal();
218  q->setMinimumHeight(minimumHeight);
219 }
220 
221 void DeclarativeWidgetPrivate::maximumWidthChanged()
222 {
223  qreal maximumWidth = root->property("maximumWidth").toReal();
224  q->setMaximumWidth(maximumWidth);
225 }
226 
227 void DeclarativeWidgetPrivate::maximumHeightChanged()
228 {
229  qreal maximumHeight = root->property("maximumHeight").toReal();
230  q->setMaximumHeight(maximumHeight);
231 }
232 
233 void DeclarativeWidgetPrivate::preferredWidthChanged()
234 {
235  qreal preferredWidth = root->property("preferredWidth").toReal();
236  q->setPreferredWidth(preferredWidth);
237 }
238 
239 void DeclarativeWidgetPrivate::preferredHeightChanged()
240 {
241  qreal preferredHeight = root->property("preferredHeight").toReal();
242  q->setPreferredHeight(preferredHeight);
243 }
244 
245 DeclarativeWidget::DeclarativeWidget(QGraphicsWidget *parent)
246  : QGraphicsWidget(parent),
247  d(new DeclarativeWidgetPrivate(this))
248 {
249  setFlag(QGraphicsItem::ItemHasNoContents);
250 
251  d->engine = new QDeclarativeEngine(this);
252  d->engine->setNetworkAccessManagerFactory(new DeclarativeNetworkAccessManagerFactory);
253 
254  d->component = new QDeclarativeComponent(d->engine, this);
255 }
256 
257 DeclarativeWidget::~DeclarativeWidget()
258 {
259  QDeclarativeNetworkAccessManagerFactory *factory = d->engine->networkAccessManagerFactory();
260  d->engine->setNetworkAccessManagerFactory(0);
261  delete factory;
262  delete d;
263 }
264 
265 void DeclarativeWidget::setQmlPath(const QString &path)
266 {
267  d->qmlPath = path;
268  d->execute(path);
269 }
270 
271 QString DeclarativeWidget::qmlPath() const
272 {
273  return d->qmlPath;
274 }
275 
276 void DeclarativeWidget::setInitializationDelayed(const bool delay)
277 {
278  d->delay = delay;
279 }
280 
281 bool DeclarativeWidget::isInitializationDelayed() const
282 {
283  return d->delay;
284 }
285 
286 QDeclarativeEngine* DeclarativeWidget::engine()
287 {
288  return d->engine;
289 }
290 
291 QScriptEngine *DeclarativeWidget::scriptEngine() const
292 {
293  return d->scriptEngine;
294 }
295 
296 QObject *DeclarativeWidget::rootObject() const
297 {
298  return d->root;
299 }
300 
301 QDeclarativeComponent *DeclarativeWidget::mainComponent() const
302 {
303  return d->component;
304 }
305 
306 void DeclarativeWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
307 {
308  QGraphicsWidget::resizeEvent(event);
309 
310  if (d->root) {
311  d->root->setProperty("width", size().width());
312  d->root->setProperty("height", size().height());
313  }
314 }
315 
316 
317 } // namespace Plasma
318 
319 #include <declarativewidget.moc>
320 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon Dec 10 2012 13:45:40 by doxygen 1.8.1.2 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.9.4 API Reference

Skip menu "kdelibs-4.9.4 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