Qt-based BB10 API Examples Documentation

applicationui.cpp Example File

datamanagerusage/src/applicationui.cpp
    /*
     * Copyright (c) 2013 BlackBerry Limited.
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     *
     * http://www.apache.org/licenses/LICENSE-2.0
     *
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */
    #include "applicationui.hpp"
    #include "SqlDataQuery.hpp"
    #include "SqlHeaderDataQuery.hpp"
    #include "QueryExec.hpp"

    #include <bb/cascades/Application>
    #include <bb/cascades/QmlDocument>
    #include <bb/cascades/AbstractPane>
    #include <bb/cascades/LocaleHandler>
    #include <QDebug>

    using namespace bb::cascades;

    ApplicationUI::ApplicationUI(bb::cascades::Application *app) :
            QObject(app)
    {
        QueryExec::registerQmlTypes();
        // prepare the localization
        m_pTranslator = new QTranslator(this);
        m_pLocaleHandler = new LocaleHandler(this);
        if(!QObject::connect(m_pLocaleHandler, SIGNAL(systemLanguageChanged()), this, SLOT(onSystemLanguageChanged()))) {
            // This is an abnormal situation! Something went wrong!
            // Add own code to recover here
            qWarning() << "Recovering from a failed connect()";
        }
        // initial load
        onSystemLanguageChanged();

        // Create scene document from main.qml asset, the parent is set
        // to ensure the document gets destroyed properly at shut down.
        QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);

        const QString workingDir = QDir::currentPath();
        _dirPaths = QString::fromLatin1(
                "file://%1/app/public/data/").arg(workingDir);

        qml->documentContext()->setContextProperty("_dataDir", _dirPaths);
        qml->documentContext()->setContextProperty("_app", this);

        // Create root object for the UI
        AbstractPane *root = qml->createRootObject<AbstractPane>();

        // Set created root object as the application scene
        app->setScene(root);
    }

    void ApplicationUI::onSystemLanguageChanged()
    {
        QCoreApplication::instance()->removeTranslator(m_pTranslator);
        // Initiate, load and install the application translation files.
        QString locale_string = QLocale().name();
        QString file_name = QString("datamanagerusage_%1").arg(locale_string);
        if (m_pTranslator->load(file_name, "app/native/qm")) {
            QCoreApplication::instance()->installTranslator(m_pTranslator);
        }
    }

    bool ApplicationUI::fileExists(const QString file) {
        QDir imgDir(QDir::currentPath() + "/app/public/data/images");
        qDebug() << "looking in " << imgDir.absolutePath();
        qDebug() << "for file " << file;
        qDebug() << "found: " << imgDir.exists(file);
        return imgDir.exists(file);
    }