• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.8.5 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 
66 
67  DeclarativeWidget *q;
68 
69  QString qmlPath;
70  QDeclarativeEngine* engine;
71  QScriptEngine *scriptEngine;
72  QDeclarativeComponent* component;
73  QObject *root;
74  bool delay : 1;
75 };
76 
77 void DeclarativeWidgetPrivate::errorPrint()
78 {
79  QString errorStr = "Error loading QML file.\n";
80  if(component->isError()){
81  QList<QDeclarativeError> errors = component->errors();
82  foreach (const QDeclarativeError &error, errors) {
83  errorStr += (error.line()>0?QString(QString::number(error.line()) + QLatin1String(": ")):QLatin1String(""))
84  + error.description() + '\n';
85  }
86  }
87  kWarning() << component->url().toString() + '\n' + errorStr;
88 }
89 
90 void DeclarativeWidgetPrivate::execute(const QString &fileName)
91 {
92  if (fileName.isEmpty()) {
93  kDebug() << "File name empty!";
94  return;
95  }
96 
97  KDeclarative kdeclarative;
98  kdeclarative.setDeclarativeEngine(engine);
99  kdeclarative.initialize();
100  //binds things like kconfig and icons
101  kdeclarative.setupBindings();
102 
103  component->loadUrl(fileName);
104 
105  scriptEngine = kdeclarative.scriptEngine();
106  registerDataEngineMetaTypes(scriptEngine);
107 
108  if (delay) {
109  QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));
110  } else {
111  scheduleExecutionEnd();
112  }
113 }
114 
115 void DeclarativeWidgetPrivate::scheduleExecutionEnd()
116 {
117  if (component->isReady() || component->isError()) {
118  finishExecute();
119  } else {
120  QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(finishExecute()));
121  }
122 }
123 
124 void DeclarativeWidgetPrivate::finishExecute()
125 {
126  if (component->isError()) {
127  errorPrint();
128  }
129 
130  root = component->create();
131 
132  if (!root) {
133  errorPrint();
134  }
135 
136  kDebug() << "Execution of QML done!";
137  QGraphicsWidget *widget = dynamic_cast<QGraphicsWidget*>(root);
138  QGraphicsObject *object = dynamic_cast<QGraphicsObject *>(root);
139 
140 
141  if (object) {
142  static_cast<QGraphicsItem *>(object)->setParentItem(q);
143  if (q->scene()) {
144  q->scene()->addItem(object);
145  }
146  }
147 
148  if (widget) {
149  q->setPreferredSize(-1,-1);
150  QGraphicsLinearLayout *lay = static_cast<QGraphicsLinearLayout *>(q->layout());
151  if (!lay) {
152  lay = new QGraphicsLinearLayout(q);
153  lay->setContentsMargins(0, 0, 0, 0);
154  }
155  lay->addItem(widget);
156  } else {
157  q->setLayout(0);
158  qreal minimumWidth = 0;
159  qreal minimumHeight = 0;
160  if (object) {
161  minimumWidth = object->property("minimumWidth").toReal();
162  minimumHeight = object->property("minimumHeight").toReal();
163  object->setProperty("width", q->size().width());
164  object->setProperty("height", q->size().height());
165  QObject::connect(object, SIGNAL(minimumWidthChanged()), q, SLOT(minimumWidthChanged()));
166  QObject::connect(object, SIGNAL(minimumHeightChanged()), q, SLOT(minimumHeightChanged()));
167  }
168 
169  if (minimumWidth > 0 && minimumHeight > 0) {
170  q->setMinimumSize(minimumWidth, minimumHeight);
171  } else {
172  q->setMinimumSize(-1, -1);
173  }
174  }
175  emit q->finished();
176 }
177 
178 void DeclarativeWidgetPrivate::minimumWidthChanged()
179 {
180  qreal minimumWidth = root->property("minimumWidth").toReal();
181  q->setMinimumWidth(minimumWidth);
182 }
183 
184 void DeclarativeWidgetPrivate::minimumHeightChanged()
185 {
186  qreal minimumHeight = root->property("minimumHeight").toReal();
187  q->setMinimumHeight(minimumHeight);
188 }
189 
190 DeclarativeWidget::DeclarativeWidget(QGraphicsWidget *parent)
191  : QGraphicsWidget(parent),
192  d(new DeclarativeWidgetPrivate(this))
193 {
194  setFlag(QGraphicsItem::ItemHasNoContents);
195 
196  d->engine = new QDeclarativeEngine(this);
197  d->engine->setNetworkAccessManagerFactory(new DeclarativeNetworkAccessManagerFactory);
198 
199  d->component = new QDeclarativeComponent(d->engine, this);
200 }
201 
202 DeclarativeWidget::~DeclarativeWidget()
203 {
204  QDeclarativeNetworkAccessManagerFactory *factory = d->engine->networkAccessManagerFactory();
205  d->engine->setNetworkAccessManagerFactory(0);
206  delete factory;
207  delete d;
208 }
209 
210 void DeclarativeWidget::setQmlPath(const QString &path)
211 {
212  d->qmlPath = path;
213  d->execute(path);
214 }
215 
216 QString DeclarativeWidget::qmlPath() const
217 {
218  return d->qmlPath;
219 }
220 
221 void DeclarativeWidget::setInitializationDelayed(const bool delay)
222 {
223  d->delay = delay;
224 }
225 
226 bool DeclarativeWidget::isInitializationDelayed() const
227 {
228  return d->delay;
229 }
230 
231 QDeclarativeEngine* DeclarativeWidget::engine()
232 {
233  return d->engine;
234 }
235 
236 QScriptEngine *DeclarativeWidget::scriptEngine() const
237 {
238  return d->scriptEngine;
239 }
240 
241 QObject *DeclarativeWidget::rootObject() const
242 {
243  return d->root;
244 }
245 
246 QDeclarativeComponent *DeclarativeWidget::mainComponent() const
247 {
248  return d->component;
249 }
250 
251 void DeclarativeWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
252 {
253  QGraphicsWidget::resizeEvent(event);
254 
255  if (d->root) {
256  d->root->setProperty("width", size().width());
257  d->root->setProperty("height", size().height());
258  }
259 }
260 
261 
262 } // namespace Plasma
263 
264 #include <declarativewidget.moc>
265 
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Fri Dec 7 2012 16:02:03 by doxygen 1.8.1 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.8.5 API Reference

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