- Examples
- Qt Bubble Level Example

# Qt Bubble Level Example

Qt Bubble Level is a simple application that uses accelerometer sensor information to calculate the inclination of the device and presents this as a traditional Bubble level. The application provides a calibration feature to handle any possible errors in accelerometer readings.

## Initialising the application

All of the initialisations are done in the main function.

First the QDeclarativeView is created to intepret the QML files. The QML file given is found from Qt resource. The root QML element is set to resize to the view whenever the view is resized.

            QDeclarativeView view;
            view.setSource(QUrl("qrc:/qml/BubbleLevel.qml"));
            view.setResizeMode(QDeclarativeView::SizeRootObjectToView);

The Settings object will handle the loading / storing of the calibration value. Then we create a instances from QAccelerometer and AccelerometerFilter and attach the filter into the sensor.

            Settings settings;

            QAccelerometer sensor;
            AccelerometerFilter filter;
            sensor.addFilter(&filter);

The Qt code is then connected to QML code by using Qt Signals and Slots connections. First the rootobject is retrieved QDeclarativeEngine which is retrieved from QDeclarativeView. The root object represents now the Qt object of the QML's root element.

The saveCorrectionAngle signal of the QML root element is connected to Qt slot saveCorrectionAngle. The rotationChanged and correctionAngle Qt signals are connected to handleRotation and setCorrectionAngle slot of QML root element. Finally the quit signal of the QDeclarativeEngine is connected to QApplication's quit slot.

            QObject *rootObject = dynamic_cast<QObject*>(view.rootObject());

            // Associate Qt / QML signals and slots
            QObject::connect(rootObject, SIGNAL(saveCorrectionAngle(const QVariant&)),
                             &settings, SLOT(saveCorrectionAngle(const QVariant&)));

            QObject::connect(&filter, SIGNAL(rotationChanged(const QVariant&)),
                             rootObject, SLOT(handleRotation(const QVariant&)));

            QObject::connect(&settings, SIGNAL(correctionAngle(const QVariant&)),
                             rootObject, SLOT(setCorrectionAngle(const QVariant&)));

            QObject::connect((QObject*)view.engine(), SIGNAL(quit()),
                             &app, SLOT(quit()));

On the Maemo target the application needs an minimize button so we connect one additional QML signal to Qt slot. The minimize button is made visible by setting the QML root element's taskSwitcherVisible property to value true.

        #ifdef Q_OS_MAEMO_5
            TaskSwitcher taskSwitcher;

            QObject::connect(rootObject, SIGNAL(minimizeApplication()),
                             &taskSwitcher, SLOT(minimizeApplication()));

            // Show the task switcher button
            rootObject->setProperty("taskSwitcherVisible", true);
        #endif

The correction factor of the accelerometer is retrieved from persistent storage by using QSettings. The correction factor is signalled to QML side by using function setCorrectionAngle. The accelerometer sensor is started and it will eventually begin to signal the changes in accelerometer readings.

            // Read correction factor from permanent storage and emit it to QML side
            settings.loadAndEmitCorrectionAngle();

            // Begin measuring of accelerometer sensor
            sensor.start();

Finally, in the end of the function the view is shown in full screen in mobile devices. In other targets the application is shown as 800 x 480 resolution on the 100, 100 position from the top-left corner of the desktop.

        #if defined(Q_WS_MAEMO_5) || defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
            view.setGeometry(QApplication::desktop()->screenGeometry());
            view.showFullScreen();
        #else
            view.setGeometry((QRect(100, 100, 800, 480)));
            view.show();
        #endif

## Accessing the accelerometer information

The inclination of the device is resolved by using QAccelerometer sensor of the QtMobility. We already created the sensor in the main function and attached our self derived AccelerometerFilter object to it. Here is the definition of the AccelerometerFilter class:

        #include <QAccelerometerFilter>
        #include <QVariant>

        QTM_USE_NAMESPACE

        class AccelerometerFilter
            : public QObject, public QAccelerometerFilter
        {
            Q_OBJECT

        protected:
            qreal x;
            qreal y;
            qreal z;

        public:
            AccelerometerFilter();
            bool filter(QAccelerometerReading *reading);

        signals:
            void rotationChanged(const QVariant &deg);
        };

The class is multiderived from QObject and QAccelerometerFilter classes. The QAccelerometerFilter class is derived from QObject because we want to use Qt Signals and Slots to signal changes of the accelerometer readings.

The members x, y and z stores the previous values of the sensor reading in order to implement low pass filter to the values.

In the implementation of the AccelerometerFilter class, we first read the value of the each axis from the QAccelerometerReading object. Then the values are converted from radians to degrees and applied the low pass filter to reduce the noise in the sensor readings. Different low pass factors are used depending on the platform (these were found out to be good by experimenting). Finally the calculated value is emitted.

Note that the accelerometer sensors are oriented differently in Symbian and Maemo devices and we must differentiate this by using platform specific code.

        bool AccelerometerFilter::filter(QAccelerometerReading *reading)
        {
            qreal rx = reading->x();
            qreal ry = reading->y();
            qreal rz = reading->z();

            qreal divider = sqrt(rx * rx + ry * ry + rz * rz);

            // Lowpass factor
        #ifdef Q_OS_SYMBIAN
            float lowPassFactor = 0.10;
        #else
            float lowPassFactor = 0.05;
        #endif

            // Calculate the axis angles in degrees and reduce the noise in sensor
            // readings.
            x += (acos(rx / divider) * RADIANS_TO_DEGREES - 90 - x) * lowPassFactor;
            y += (acos(ry / divider) * RADIANS_TO_DEGREES - 90 - y) * lowPassFactor;
            z += (acos(rz / divider) * RADIANS_TO_DEGREES - 90 - z) * lowPassFactor;

            // The orientations of the accelerometers are different between
            // Symbian and Maemo devices so we use the different axes
            // depending on the platform.
        #if defined(Q_OS_SYMBIAN)
            emit rotationChanged(-y);
        #else
            emit rotationChanged(x);
        #endif

            // Don't store the reading in the sensor.
            return false;
        }

## The Qt Quick UI

The BubbleLevel.qml is the main QML element. It represents the wooden board of the bubble level and it also acts as connection point between QML and Qt side. In the beginning of the element there exists the two signals, two functions and one property. All of these define the interface between Qt and QML.

The minimizeApplication is signalled when the application is wanted to be minimized on Maemo platform. The saveCorrectionAngle is signalled when a new calibration factor is required to be stored into the devices memory.

The handleRotation function acts as Qt slot which the AccelerometerFilters signal rotationChanged is connected. Similarly the setCorrectionAngle function acts also as Qt slot to which the Settings objects signal correctionAngle is connected.

The property alias taskSwitcherVisible is provided to allow the Qt model to show or hide the task switcher button which minimises the application. This is only meaningful in Maemo platforms, where every application normally has a task switcher button.

            // Signaled when task switcher button is pressed
            signal minimizeApplication()

            // Signaled when correction angle is saved
            signal saveCorrectionAngle(variant angle)

            // These functions are used as Qt slots
            function handleRotation(deg) {
                horTube.rawangle = deg
            }

            function setCorrectionAngle(deg) {
                calibrationDialog.angleconstant = deg
            }

            // Used to show the task switcher button in Maemo targets
            property alias taskSwitcherVisible: taskSwitcher.visible

The Tube element represents the the glass tube of the bubble level. It is anchored to the center of the wooden board. The width and height are calculated with a specific factors to make the glass tube to scale to a different resolutions.

            Tube {
                id: horTube

                property real rawangle: 0

                anchors.horizontalCenter: parent.horizontalCenter
                width: parent.width * 0.775; height: parent.height * 0.15625
                deg: rawangle - calibrationDialog.angleconstant
            }

In the implementation of the Tube.qml the property deg represents the current inclination. The x-position of the bubble is binded to JavaScript function calX which is called every time when the property deg, center or bubblCenter is changed. The function places the bubble to the corresponding place on its parent.

        Item {
            id: tube

            property real deg

            Image {
                id: bubble

                property real center: tube.width / 2
                property real bubbleCenter: bubble.width / 2

                function calX() {
                    var newX = center + tube.deg / -20 * center

                    if((newX - bubbleCenter) < 0) {
                        return 0
                    }
                    else if((newX + bubbleCenter) > tube.width) {
                        return tube.width - 2 * bubbleCenter
                    }

                    return newX - bubbleCenter;
                }

                x: calX()
                width: 0.16129032 * parent.width; height: 0.66666667 * parent.height
                source: "images/bubble.png"
                smooth: true
            }

            Image {
                anchors.horizontalCenter: parent.horizontalCenter
                width: 0.36451613 * parent.width; height: 0.66666667 * parent.height
                source: "images/scale.png"
            }

            Image {
                width: parent.width; height:  0.32 * parent.height
                opacity: 0.8
                source: "images/reflection.png"
            }
        }