| Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/qtdeclarative/src/qtdeclarative/src/qml/qml/qqmlapplicationengine.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||
| 2 | ** | - | ||||||||||||
| 3 | ** Copyright (C) 2016 Research In Motion. | - | ||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||
| 5 | ** | - | ||||||||||||
| 6 | ** This file is part of the QtQml module of the Qt Toolkit. | - | ||||||||||||
| 7 | ** | - | ||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||
| 16 | ** | - | ||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||
| 24 | ** | - | ||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||
| 35 | ** | - | ||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||
| 37 | ** | - | ||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||
| 39 | - | |||||||||||||
| 40 | #include <QtCore/QCoreApplication> | - | ||||||||||||
| 41 | #include <QtCore/QTranslator> | - | ||||||||||||
| 42 | #include <QQmlComponent> | - | ||||||||||||
| 43 | #include "qqmlapplicationengine.h" | - | ||||||||||||
| 44 | #include "qqmlapplicationengine_p.h" | - | ||||||||||||
| 45 | #include "qqmlfileselector.h" | - | ||||||||||||
| 46 | - | |||||||||||||
| 47 | QT_BEGIN_NAMESPACE | - | ||||||||||||
| 48 | - | |||||||||||||
| 49 | QQmlApplicationEnginePrivate::QQmlApplicationEnginePrivate(QQmlEngine *e) | - | ||||||||||||
| 50 | : QQmlEnginePrivate(e) | - | ||||||||||||
| 51 | { | - | ||||||||||||
| 52 | } executed 148 times by 7 tests: end of blockExecuted by:
| 148 | ||||||||||||
| 53 | - | |||||||||||||
| 54 | QQmlApplicationEnginePrivate::~QQmlApplicationEnginePrivate() | - | ||||||||||||
| 55 | { | - | ||||||||||||
| 56 | } | - | ||||||||||||
| 57 | - | |||||||||||||
| 58 | void QQmlApplicationEnginePrivate::cleanUp() | - | ||||||||||||
| 59 | { | - | ||||||||||||
| 60 | Q_Q(QQmlApplicationEngine); | - | ||||||||||||
| 61 | for (auto obj : qAsConst(objects)) | - | ||||||||||||
| 62 | obj->disconnect(q); executed 178 times by 5 tests: obj->disconnect(q);Executed by:
| 178 | ||||||||||||
| 63 | - | |||||||||||||
| 64 | qDeleteAll(objects); | - | ||||||||||||
| 65 | #if QT_CONFIG(translation) | - | ||||||||||||
| 66 | qDeleteAll(translators); | - | ||||||||||||
| 67 | #endif | - | ||||||||||||
| 68 | } executed 148 times by 7 tests: end of blockExecuted by:
| 148 | ||||||||||||
| 69 | - | |||||||||||||
| 70 | void QQmlApplicationEnginePrivate::init() | - | ||||||||||||
| 71 | { | - | ||||||||||||
| 72 | Q_Q(QQmlApplicationEngine); | - | ||||||||||||
| 73 | q->connect(q, SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit())); | - | ||||||||||||
| 74 | q->connect(q, &QQmlApplicationEngine::exit, QCoreApplication::instance(), &QCoreApplication::exit); | - | ||||||||||||
| 75 | #if QT_CONFIG(translation) | - | ||||||||||||
| 76 | QTranslator* qtTranslator = new QTranslator; | - | ||||||||||||
| 77 | if (qtTranslator->load(QLatin1String("qt_") + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
| 0-148 | ||||||||||||
| 78 | QCoreApplication::installTranslator(qtTranslator); never executed: QCoreApplication::installTranslator(qtTranslator); | 0 | ||||||||||||
| 79 | translators << qtTranslator; | - | ||||||||||||
| 80 | #endif | - | ||||||||||||
| 81 | new QQmlFileSelector(q,q); | - | ||||||||||||
| 82 | QCoreApplication::instance()->setProperty("__qml_using_qqmlapplicationengine", QVariant(true)); | - | ||||||||||||
| 83 | } executed 148 times by 7 tests: end of blockExecuted by:
| 148 | ||||||||||||
| 84 | - | |||||||||||||
| 85 | void QQmlApplicationEnginePrivate::loadTranslations(const QUrl &rootFile) | - | ||||||||||||
| 86 | { | - | ||||||||||||
| 87 | #if QT_CONFIG(translation) | - | ||||||||||||
| 88 | if (rootFile.scheme() != QLatin1String("file") && rootFile.scheme() != QLatin1String("qrc"))
| 2-162 | ||||||||||||
| 89 | return; executed 16 times by 1 test: return;Executed by:
| 16 | ||||||||||||
| 90 | - | |||||||||||||
| 91 | QFileInfo fi(rootFile.toLocalFile()); | - | ||||||||||||
| 92 | - | |||||||||||||
| 93 | QTranslator *translator = new QTranslator; | - | ||||||||||||
| 94 | if (translator->load(QLatin1String("qml_") + QLocale::system().name(), fi.path() + QLatin1String("/i18n"))) {
| 0-164 | ||||||||||||
| 95 | QCoreApplication::installTranslator(translator); | - | ||||||||||||
| 96 | translators << translator; | - | ||||||||||||
| 97 | } else { never executed: end of block | 0 | ||||||||||||
| 98 | delete translator; | - | ||||||||||||
| 99 | } executed 164 times by 4 tests: end of blockExecuted by:
| 164 | ||||||||||||
| 100 | #else | - | ||||||||||||
| 101 | Q_UNUSED(rootFile) | - | ||||||||||||
| 102 | #endif | - | ||||||||||||
| 103 | } | - | ||||||||||||
| 104 | - | |||||||||||||
| 105 | void QQmlApplicationEnginePrivate::startLoad(const QUrl &url, const QByteArray &data, bool dataFlag) | - | ||||||||||||
| 106 | { | - | ||||||||||||
| 107 | Q_Q(QQmlApplicationEngine); | - | ||||||||||||
| 108 | - | |||||||||||||
| 109 | loadTranslations(url); //Translations must be loaded before the QML file is | - | ||||||||||||
| 110 | QQmlComponent *c = new QQmlComponent(q, q); | - | ||||||||||||
| 111 | - | |||||||||||||
| 112 | if (dataFlag)
| 20-160 | ||||||||||||
| 113 | c->setData(data,url); executed 20 times by 3 tests: c->setData(data,url);Executed by:
| 20 | ||||||||||||
| 114 | else | - | ||||||||||||
| 115 | c->loadUrl(url); executed 160 times by 4 tests: c->loadUrl(url);Executed by:
| 160 | ||||||||||||
| 116 | - | |||||||||||||
| 117 | if (!c->isLoading()) {
| 0-180 | ||||||||||||
| 118 | finishLoad(c); | - | ||||||||||||
| 119 | return; executed 180 times by 5 tests: return;Executed by:
| 180 | ||||||||||||
| 120 | } | - | ||||||||||||
| 121 | QObject::connect(c, &QQmlComponent::statusChanged, q, [this, c] { this->finishLoad(c); }); never executed: end of block | 0 | ||||||||||||
| 122 | } never executed: end of block | 0 | ||||||||||||
| 123 | - | |||||||||||||
| 124 | void QQmlApplicationEnginePrivate::finishLoad(QQmlComponent *c) | - | ||||||||||||
| 125 | { | - | ||||||||||||
| 126 | Q_Q(QQmlApplicationEngine); | - | ||||||||||||
| 127 | switch (c->status()) { | - | ||||||||||||
| 128 | case QQmlComponent::Error: never executed: case QQmlComponent::Error: | 0 | ||||||||||||
| 129 | qWarning() << "QQmlApplicationEngine failed to load component"; | - | ||||||||||||
| 130 | qWarning() << qPrintable(c->errorString()); | - | ||||||||||||
| 131 | q->objectCreated(nullptr, c->url()); | - | ||||||||||||
| 132 | break; never executed: break; | 0 | ||||||||||||
| 133 | case QQmlComponent::Ready: { executed 180 times by 5 tests: case QQmlComponent::Ready:Executed by:
| 180 | ||||||||||||
| 134 | auto newObj = c->create(); | - | ||||||||||||
| 135 | objects << newObj; | - | ||||||||||||
| 136 | QObject::connect(newObj, &QObject::destroyed, q, [&](QObject *obj) { objects.removeAll(obj); }); executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||
| 137 | q->objectCreated(objects.constLast(), c->url()); | - | ||||||||||||
| 138 | } | - | ||||||||||||
| 139 | break; executed 180 times by 5 tests: break;Executed by:
| 180 | ||||||||||||
| 140 | case QQmlComponent::Loading: never executed: case QQmlComponent::Loading: | 0 | ||||||||||||
| 141 | case QQmlComponent::Null: never executed: case QQmlComponent::Null: | 0 | ||||||||||||
| 142 | return; //These cases just wait for the next status update never executed: return; | 0 | ||||||||||||
| 143 | } | - | ||||||||||||
| 144 | - | |||||||||||||
| 145 | c->deleteLater(); | - | ||||||||||||
| 146 | } executed 180 times by 5 tests: end of blockExecuted by:
| 180 | ||||||||||||
| 147 | - | |||||||||||||
| 148 | /*! | - | ||||||||||||
| 149 | \class QQmlApplicationEngine | - | ||||||||||||
| 150 | \since 5.1 | - | ||||||||||||
| 151 | \inmodule QtQml | - | ||||||||||||
| 152 | \brief QQmlApplicationEngine provides a convenient way to load an application from a single QML file. | - | ||||||||||||
| 153 | - | |||||||||||||
| 154 | This class combines a QQmlEngine and QQmlComponent to provide a convenient way to load a single QML file. It also exposes some central application functionality to QML, which a C++/QML hybrid application would normally control from C++. | - | ||||||||||||
| 155 | - | |||||||||||||
| 156 | It can be used like so: | - | ||||||||||||
| 157 | - | |||||||||||||
| 158 | \code | - | ||||||||||||
| 159 | #include <QGuiApplication> | - | ||||||||||||
| 160 | #include <QQmlApplicationEngine> | - | ||||||||||||
| 161 | - | |||||||||||||
| 162 | int main(int argc, char *argv[]) | - | ||||||||||||
| 163 | { | - | ||||||||||||
| 164 | QGuiApplication app(argc, argv); | - | ||||||||||||
| 165 | QQmlApplicationEngine engine("main.qml"); | - | ||||||||||||
| 166 | return app.exec(); | - | ||||||||||||
| 167 | } | - | ||||||||||||
| 168 | \endcode | - | ||||||||||||
| 169 | - | |||||||||||||
| 170 | Unlike QQuickView, QQmlApplicationEngine does not automatically create a root | - | ||||||||||||
| 171 | window. If you are using visual items from Qt Quick, you will need to place | - | ||||||||||||
| 172 | them inside of a \l [QML] {Window}. | - | ||||||||||||
| 173 | - | |||||||||||||
| 174 | You can also use QCoreApplication with QQmlApplicationEngine, if you are not using any QML modules which require a QGuiApplication (such as \c QtQuick). | - | ||||||||||||
| 175 | - | |||||||||||||
| 176 | List of configuration changes from a default QQmlEngine: | - | ||||||||||||
| 177 | - | |||||||||||||
| 178 | \list | - | ||||||||||||
| 179 | \li Connecting Qt.quit() to QCoreApplication::quit() | - | ||||||||||||
| 180 | \li Automatically loads translation files from an i18n directory adjacent to the main QML file. | - | ||||||||||||
| 181 | \li Automatically sets an incubation controller if the scene contains a QQuickWindow. | - | ||||||||||||
| 182 | \li Automatically sets a \c QQmlFileSelector as the url interceptor, applying file selectors to all | - | ||||||||||||
| 183 | QML files and assets. | - | ||||||||||||
| 184 | \endlist | - | ||||||||||||
| 185 | - | |||||||||||||
| 186 | The engine behavior can be further tweaked by using the inherited methods from QQmlEngine. | - | ||||||||||||
| 187 | - | |||||||||||||
| 188 | */ | - | ||||||||||||
| 189 | - | |||||||||||||
| 190 | /*! | - | ||||||||||||
| 191 | \fn QQmlApplicationEngine::objectCreated(QObject *object, const QUrl &url) | - | ||||||||||||
| 192 | - | |||||||||||||
| 193 | This signal is emitted when an object finishes loading. If loading was | - | ||||||||||||
| 194 | successful, \a object contains a pointer to the loaded object, otherwise | - | ||||||||||||
| 195 | the pointer is NULL. | - | ||||||||||||
| 196 | - | |||||||||||||
| 197 | The \a url to the component the \a object came from is also provided. | - | ||||||||||||
| 198 | - | |||||||||||||
| 199 | \note If the path to the component was provided as a QString containing a | - | ||||||||||||
| 200 | relative path, the \a url will contain a fully resolved path to the file. | - | ||||||||||||
| 201 | */ | - | ||||||||||||
| 202 | - | |||||||||||||
| 203 | /*! | - | ||||||||||||
| 204 | Create a new QQmlApplicationEngine with the given \a parent. You will have to call load() later in | - | ||||||||||||
| 205 | order to load a QML file. | - | ||||||||||||
| 206 | */ | - | ||||||||||||
| 207 | QQmlApplicationEngine::QQmlApplicationEngine(QObject *parent) | - | ||||||||||||
| 208 | : QQmlEngine(*(new QQmlApplicationEnginePrivate(this)), parent) | - | ||||||||||||
| 209 | { | - | ||||||||||||
| 210 | Q_D(QQmlApplicationEngine); | - | ||||||||||||
| 211 | d->init(); | - | ||||||||||||
| 212 | QJSEnginePrivate::addToDebugServer(this); | - | ||||||||||||
| 213 | } executed 148 times by 7 tests: end of blockExecuted by:
| 148 | ||||||||||||
| 214 | - | |||||||||||||
| 215 | /*! | - | ||||||||||||
| 216 | Create a new QQmlApplicationEngine and loads the QML file at the given \a url. | - | ||||||||||||
| 217 | This is provided as a convenience, and is the same as using the empty constructor and calling load afterwards. | - | ||||||||||||
| 218 | */ | - | ||||||||||||
| 219 | QQmlApplicationEngine::QQmlApplicationEngine(const QUrl &url, QObject *parent) | - | ||||||||||||
| 220 | : QQmlApplicationEngine(parent) | - | ||||||||||||
| 221 | { | - | ||||||||||||
| 222 | load(url); | - | ||||||||||||
| 223 | } executed 14 times by 3 tests: end of blockExecuted by:
| 14 | ||||||||||||
| 224 | - | |||||||||||||
| 225 | /*! | - | ||||||||||||
| 226 | Create a new QQmlApplicationEngine and loads the QML file at the given | - | ||||||||||||
| 227 | \a filePath, which must be a local file path. If a relative path is | - | ||||||||||||
| 228 | given then it will be interpreted as relative to the working directory of the | - | ||||||||||||
| 229 | application. | - | ||||||||||||
| 230 | - | |||||||||||||
| 231 | This is provided as a convenience, and is the same as using the empty constructor and calling load afterwards. | - | ||||||||||||
| 232 | */ | - | ||||||||||||
| 233 | QQmlApplicationEngine::QQmlApplicationEngine(const QString &filePath, QObject *parent) | - | ||||||||||||
| 234 | : QQmlApplicationEngine(QUrl::fromUserInput(filePath, QLatin1String("."), QUrl::AssumeLocalFile), parent) | - | ||||||||||||
| 235 | { | - | ||||||||||||
| 236 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||
| 237 | - | |||||||||||||
| 238 | /*! | - | ||||||||||||
| 239 | Destroys the QQmlApplicationEngine and all QML objects it loaded. | - | ||||||||||||
| 240 | */ | - | ||||||||||||
| 241 | QQmlApplicationEngine::~QQmlApplicationEngine() | - | ||||||||||||
| 242 | { | - | ||||||||||||
| 243 | Q_D(QQmlApplicationEngine); | - | ||||||||||||
| 244 | QJSEnginePrivate::removeFromDebugServer(this); | - | ||||||||||||
| 245 | d->cleanUp();//Instantiated root objects must be deleted before the engine | - | ||||||||||||
| 246 | } executed 148 times by 7 tests: end of blockExecuted by:
| 148 | ||||||||||||
| 247 | - | |||||||||||||
| 248 | /*! | - | ||||||||||||
| 249 | Loads the root QML file located at \a url. The object tree defined by the file | - | ||||||||||||
| 250 | is created immediately for local file urls. Remote urls are loaded asynchronously, | - | ||||||||||||
| 251 | listen to the objectCreated signal to determine when the object | - | ||||||||||||
| 252 | tree is ready. | - | ||||||||||||
| 253 | - | |||||||||||||
| 254 | If an error occurs, error messages are printed with qWarning. | - | ||||||||||||
| 255 | */ | - | ||||||||||||
| 256 | void QQmlApplicationEngine::load(const QUrl &url) | - | ||||||||||||
| 257 | { | - | ||||||||||||
| 258 | Q_D(QQmlApplicationEngine); | - | ||||||||||||
| 259 | d->startLoad(url); | - | ||||||||||||
| 260 | } executed 158 times by 4 tests: end of blockExecuted by:
| 158 | ||||||||||||
| 261 | - | |||||||||||||
| 262 | /*! | - | ||||||||||||
| 263 | Loads the root QML file located at \a filePath. \a filePath must be a path to | - | ||||||||||||
| 264 | a local file. If \a filePath is a relative path, it is taken as relative to | - | ||||||||||||
| 265 | the application's working directory. The object tree defined by the file is | - | ||||||||||||
| 266 | instantiated immediately. | - | ||||||||||||
| 267 | - | |||||||||||||
| 268 | If an error occurs, error messages are printed with qWarning. | - | ||||||||||||
| 269 | */ | - | ||||||||||||
| 270 | void QQmlApplicationEngine::load(const QString &filePath) | - | ||||||||||||
| 271 | { | - | ||||||||||||
| 272 | Q_D(QQmlApplicationEngine); | - | ||||||||||||
| 273 | d->startLoad(QUrl::fromUserInput(filePath, QLatin1String("."), QUrl::AssumeLocalFile)); | - | ||||||||||||
| 274 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||
| 275 | - | |||||||||||||
| 276 | /*! | - | ||||||||||||
| 277 | Loads the QML given in \a data. The object tree defined by \a data is | - | ||||||||||||
| 278 | instantiated immediately. | - | ||||||||||||
| 279 | - | |||||||||||||
| 280 | If a \a url is specified it is used as the base url of the component. This affects | - | ||||||||||||
| 281 | relative paths within the data and error messages. | - | ||||||||||||
| 282 | - | |||||||||||||
| 283 | If an error occurs, error messages are printed with qWarning. | - | ||||||||||||
| 284 | */ | - | ||||||||||||
| 285 | void QQmlApplicationEngine::loadData(const QByteArray &data, const QUrl &url) | - | ||||||||||||
| 286 | { | - | ||||||||||||
| 287 | Q_D(QQmlApplicationEngine); | - | ||||||||||||
| 288 | d->startLoad(url, data, true); | - | ||||||||||||
| 289 | } executed 20 times by 3 tests: end of blockExecuted by:
| 20 | ||||||||||||
| 290 | - | |||||||||||||
| 291 | /*! | - | ||||||||||||
| 292 | Returns a list of all the root objects instantiated by the | - | ||||||||||||
| 293 | QQmlApplicationEngine. This will only contain objects loaded via load() or a | - | ||||||||||||
| 294 | convenience constructor. | - | ||||||||||||
| 295 | - | |||||||||||||
| 296 | \note In Qt versions prior to 5.9, this function is marked as non-\c{const}. | - | ||||||||||||
| 297 | */ | - | ||||||||||||
| 298 | - | |||||||||||||
| 299 | QList<QObject *> QQmlApplicationEngine::rootObjects() const | - | ||||||||||||
| 300 | { | - | ||||||||||||
| 301 | Q_D(const QQmlApplicationEngine); | - | ||||||||||||
| 302 | return d->objects; executed 232 times by 4 tests: return d->objects;Executed by:
| 232 | ||||||||||||
| 303 | } | - | ||||||||||||
| 304 | - | |||||||||||||
| 305 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) | - | ||||||||||||
| 306 | /*! | - | ||||||||||||
| 307 | \overload | - | ||||||||||||
| 308 | \internal | - | ||||||||||||
| 309 | */ | - | ||||||||||||
| 310 | QList<QObject *> QQmlApplicationEngine::rootObjects() | - | ||||||||||||
| 311 | { | - | ||||||||||||
| 312 | return qAsConst(*this).rootObjects(); executed 232 times by 4 tests: return qAsConst(*this).rootObjects();Executed by:
| 232 | ||||||||||||
| 313 | } | - | ||||||||||||
| 314 | #endif // < Qt 6 | - | ||||||||||||
| 315 | - | |||||||||||||
| 316 | QT_END_NAMESPACE | - | ||||||||||||
| 317 | - | |||||||||||||
| 318 | #include "moc_qqmlapplicationengine.cpp" | - | ||||||||||||
| Source code | Switch to Preprocessed file |