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

Plasma

declarativewidget.cpp
Go to the documentation of this file.
00001 /*
00002  *   Copyright 2010 Marco Martin <mart@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License as
00006  *   published by the Free Software Foundation; either version 2, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details
00013  *
00014  *   You should have received a copy of the GNU Library General Public
00015  *   License along with this program; if not, write to the
00016  *   Free Software Foundation, Inc.,
00017  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00018  */
00019 
00020 #include "declarativewidget.h"
00021 
00022 
00023 #include <QtDeclarative/QDeclarativeComponent>
00024 #include <QtDeclarative/QDeclarativeItem>
00025 #include <QtDeclarative/QDeclarativeEngine>
00026 #include <QtDeclarative/QDeclarativeContext>
00027 #include <QScriptEngine>
00028 #include <QGraphicsLinearLayout>
00029 #include <QGraphicsScene>
00030 #include <QTimer>
00031 
00032 #include <kdebug.h>
00033 #include <kdeclarative.h>
00034 #include <kglobal.h>
00035 #include <kstandarddirs.h>
00036 
00037 #include "private/declarative/declarativenetworkaccessmanagerfactory_p.h"
00038 #include "private/dataenginebindings_p.h"
00039 
00040 namespace Plasma
00041 {
00042 
00043 class DeclarativeWidgetPrivate
00044 {
00045 public:
00046     DeclarativeWidgetPrivate(DeclarativeWidget *parent)
00047         : q(parent),
00048           engine(0),
00049           component(0),
00050           root(0),
00051           delay(false)
00052     {
00053     }
00054 
00055     ~DeclarativeWidgetPrivate()
00056     {
00057     }
00058 
00059     void errorPrint();
00060     void execute(const QString &fileName);
00061     void finishExecute();
00062     void scheduleExecutionEnd();
00063     void minimumWidthChanged();
00064     void minimumHeightChanged();
00065     void maximumWidthChanged();
00066     void maximumHeightChanged();
00067     void preferredWidthChanged();
00068     void preferredHeightChanged();
00069 
00070 
00071     DeclarativeWidget *q;
00072 
00073     QString qmlPath;
00074     QDeclarativeEngine* engine;
00075     QScriptEngine *scriptEngine;
00076     QDeclarativeComponent* component;
00077     QObject *root;
00078     bool delay : 1;
00079 };
00080 
00081 void DeclarativeWidgetPrivate::errorPrint()
00082 {
00083     QString errorStr = "Error loading QML file.\n";
00084     if(component->isError()){
00085         QList<QDeclarativeError> errors = component->errors();
00086         foreach (const QDeclarativeError &error, errors) {
00087             errorStr += (error.line()>0?QString(QString::number(error.line()) + QLatin1String(": ")):QLatin1String(""))
00088                 + error.description() + '\n';
00089         }
00090     }
00091     kWarning() << component->url().toString() + '\n' + errorStr;
00092 }
00093 
00094 void DeclarativeWidgetPrivate::execute(const QString &fileName)
00095 {
00096     if (fileName.isEmpty()) {
00097         kDebug() << "File name empty!";
00098         return;
00099     }
00100 
00101     KDeclarative kdeclarative;
00102     kdeclarative.setDeclarativeEngine(engine);
00103     kdeclarative.initialize();
00104     //binds things like kconfig and icons
00105     kdeclarative.setupBindings();
00106 
00107     component->loadUrl(fileName);
00108 
00109     scriptEngine = kdeclarative.scriptEngine();
00110     registerDataEngineMetaTypes(scriptEngine);
00111 
00112     if (delay) {
00113         QTimer::singleShot(0, q, SLOT(scheduleExecutionEnd()));
00114     } else {
00115         scheduleExecutionEnd();
00116     }
00117 }
00118 
00119 void DeclarativeWidgetPrivate::scheduleExecutionEnd()
00120 {
00121     if (component->isReady() || component->isError()) {
00122         finishExecute();
00123     } else {
00124         QObject::connect(component, SIGNAL(statusChanged(QDeclarativeComponent::Status)), q, SLOT(finishExecute()));
00125     }
00126 }
00127 
00128 void DeclarativeWidgetPrivate::finishExecute()
00129 {
00130     if (component->isError()) {
00131         errorPrint();
00132     }
00133 
00134     root = component->create();
00135 
00136     if (!root) {
00137         errorPrint();
00138     }
00139 
00140     kDebug() << "Execution of QML done!";
00141     QGraphicsWidget *widget = dynamic_cast<QGraphicsWidget*>(root);
00142     QGraphicsObject *object = dynamic_cast<QGraphicsObject *>(root);
00143 
00144 
00145     if (object) {
00146         static_cast<QGraphicsItem *>(object)->setParentItem(q);
00147         if (q->scene()) {
00148             q->scene()->addItem(object);
00149         }
00150     }
00151 
00152     if (widget) {
00153         q->setPreferredSize(-1,-1);
00154         QGraphicsLinearLayout *lay = static_cast<QGraphicsLinearLayout *>(q->layout());
00155         if (!lay) {
00156             lay = new QGraphicsLinearLayout(q);
00157             lay->setContentsMargins(0, 0, 0, 0);
00158         }
00159         lay->addItem(widget);
00160     } else {
00161         q->setLayout(0);
00162         qreal minimumWidth = 0;
00163         qreal minimumHeight = 0;
00164         qreal maximumWidth = 0;
00165         qreal maximumHeight = 0;
00166         qreal preferredWidth = 0;
00167         qreal preferredHeight = 0;
00168         if (object) {
00169             object->setProperty("width", q->size().width());
00170             object->setProperty("height", q->size().height());
00171 
00172             minimumWidth = object->property("minimumWidth").toReal();
00173             minimumHeight = object->property("minimumHeight").toReal();
00174             QObject::connect(object, SIGNAL(minimumWidthChanged()), q, SLOT(minimumWidthChanged()));
00175             QObject::connect(object, SIGNAL(minimumHeightChanged()), q, SLOT(minimumHeightChanged()));
00176 
00177             maximumWidth = object->property("maximumWidth").toReal();
00178             maximumHeight = object->property("maximumHeight").toReal();
00179             QObject::connect(object, SIGNAL(maximumWidthChanged()), q, SLOT(maximumWidthChanged()));
00180             QObject::connect(object, SIGNAL(maximumHeightChanged()), q, SLOT(maximumHeightChanged()));
00181 
00182             preferredWidth = object->property("preferredWidth").toReal();
00183             preferredHeight = object->property("preferredHeight").toReal();
00184             QObject::connect(object, SIGNAL(preferredWidthChanged()), q, SLOT(preferredWidthChanged()));
00185             QObject::connect(object, SIGNAL(preferredHeightChanged()), q, SLOT(preferredHeightChanged()));
00186         }
00187 
00188         if (minimumWidth > 0 && minimumHeight > 0) {
00189             q->setMinimumSize(minimumWidth, minimumHeight);
00190         } else {
00191             q->setMinimumSize(-1, -1);
00192         }
00193 
00194         if (maximumWidth > 0 && maximumHeight > 0) {
00195             q->setMaximumSize(maximumWidth, maximumHeight);
00196         } else {
00197             q->setMaximumSize(-1, -1);
00198         }
00199 
00200         if (preferredWidth > 0 && preferredHeight > 0) {
00201             q->setPreferredSize(preferredWidth, preferredHeight);
00202         } else {
00203             q->setPreferredSize(-1, -1);
00204         }
00205     }
00206     emit q->finished();
00207 }
00208 
00209 void DeclarativeWidgetPrivate::minimumWidthChanged()
00210 {
00211     qreal minimumWidth = root->property("minimumWidth").toReal();
00212     q->setMinimumWidth(minimumWidth);
00213 }
00214 
00215 void DeclarativeWidgetPrivate::minimumHeightChanged()
00216 {
00217     qreal minimumHeight = root->property("minimumHeight").toReal();
00218     q->setMinimumHeight(minimumHeight);
00219 }
00220 
00221 void DeclarativeWidgetPrivate::maximumWidthChanged()
00222 {
00223     qreal maximumWidth = root->property("maximumWidth").toReal();
00224     q->setMaximumWidth(maximumWidth);
00225 }
00226 
00227 void DeclarativeWidgetPrivate::maximumHeightChanged()
00228 {
00229     qreal maximumHeight = root->property("maximumHeight").toReal();
00230     q->setMaximumHeight(maximumHeight);
00231 }
00232 
00233 void DeclarativeWidgetPrivate::preferredWidthChanged()
00234 {
00235     qreal preferredWidth = root->property("preferredWidth").toReal();
00236     q->setPreferredWidth(preferredWidth);
00237 }
00238 
00239 void DeclarativeWidgetPrivate::preferredHeightChanged()
00240 {
00241     qreal preferredHeight = root->property("preferredHeight").toReal();
00242     q->setPreferredHeight(preferredHeight);
00243 }
00244 
00245 DeclarativeWidget::DeclarativeWidget(QGraphicsWidget *parent)
00246     : QGraphicsWidget(parent),
00247       d(new DeclarativeWidgetPrivate(this))
00248 {
00249     setFlag(QGraphicsItem::ItemHasNoContents);
00250 
00251     d->engine = new QDeclarativeEngine(this);
00252     d->engine->setNetworkAccessManagerFactory(new DeclarativeNetworkAccessManagerFactory);
00253 
00254     d->component = new QDeclarativeComponent(d->engine, this);
00255 }
00256 
00257 DeclarativeWidget::~DeclarativeWidget()
00258 {
00259     QDeclarativeNetworkAccessManagerFactory *factory = d->engine->networkAccessManagerFactory();
00260     d->engine->setNetworkAccessManagerFactory(0);
00261     delete factory;
00262     delete d;
00263 }
00264 
00265 void DeclarativeWidget::setQmlPath(const QString &path)
00266 {
00267     d->qmlPath = path;
00268     d->execute(path);
00269 }
00270 
00271 QString DeclarativeWidget::qmlPath() const
00272 {
00273     return d->qmlPath;
00274 }
00275 
00276 void DeclarativeWidget::setInitializationDelayed(const bool delay)
00277 {
00278     d->delay = delay;
00279 }
00280 
00281 bool DeclarativeWidget::isInitializationDelayed() const
00282 {
00283     return d->delay;
00284 }
00285 
00286 QDeclarativeEngine* DeclarativeWidget::engine()
00287 {
00288     return d->engine;
00289 }
00290 
00291 QScriptEngine *DeclarativeWidget::scriptEngine() const
00292 {
00293     return d->scriptEngine;
00294 }
00295 
00296 QObject *DeclarativeWidget::rootObject() const
00297 {
00298     return d->root;
00299 }
00300 
00301 QDeclarativeComponent *DeclarativeWidget::mainComponent() const
00302 {
00303     return d->component;
00304 }
00305 
00306 void DeclarativeWidget::resizeEvent(QGraphicsSceneResizeEvent *event)
00307 {
00308     QGraphicsWidget::resizeEvent(event);
00309 
00310     if (d->root) {
00311         d->root->setProperty("width", size().width());
00312         d->root->setProperty("height", size().height());
00313     }
00314 }
00315 
00316 
00317 } // namespace Plasma
00318 
00319 #include <declarativewidget.moc>
00320 
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Mon Jan 21 2019 12:30:43 by doxygen 1.7.5.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.9.5 API Reference

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