Qt-based BB10 API Examples Documentation

Contents

Orientation Example

Files:

Description

The Orientation example demonstrates how to use sensors from the QtSensors module to find out about the current orientation of the device.

Overview

In this example we'll learn how to use the QOrientationSensor, QOrientationFilter and QOrientationReading classes to retrieve the current orientation from the orientation sensor of the device. Depending of the current orientation we show a different text in the UI.

The UI

The UI of this sample application is really simple and just consists of a Label that shows the current orientation as text.

The business logic of the application is encapsulated in the OrientationSensor class which is made available to the UI under the name '_sensor'.

    Label {
        horizontalAlignment: HorizontalAlignment.Center
        verticalAlignment: VerticalAlignment.Center

        // Use a different text depending on current orientation
        text: _sensor.orientation == OrientationSensor.TopUp ? qsTr ("Top Up") :
              _sensor.orientation == OrientationSensor.TopDown ? qsTr ("Top Down") :
              _sensor.orientation == OrientationSensor.LeftUp ? qsTr ("Left Up") :
              _sensor.orientation == OrientationSensor.RightUp ? qsTr ("Right Up") :
              _sensor.orientation == OrientationSensor.FaceUp ? qsTr ("Face Up") :
              _sensor.orientation == OrientationSensor.FaceDown ? qsTr ("Face Down") : ""

        textStyle {
            base: SystemDefaults.TextStyles.BigText
            color: Color.White
            fontWeight: FontWeight.Bold
        }

        scaleX: 0
        scaleY: 0

        animations: ScaleTransition {
            id: animation

            fromX: 0
            fromY: 0
            toX: 1.5
            toY: 1.5
        }
    }

The text property of the Label is bound against the 'orientation' property of the OrientationSensor object, so that it is updated automatically whenever the orientation changes. To make it a bit more fancy we use a zoom-in animation when changing the text.

    // Trigger the animation whenever the orientation has changed
    onCreationCompleted: _sensor.orientationChanged.connect(animation.play)

To execute the animation at the right time we connect the orientationChanged() signal of the OrientationSensor against the play() function of the ScaleTransition.

OrientationSensor

The OrientationSensor class encapsulates the business logic of the application. It contains a QOrientationSensor object, which does the low-level communication with the orientation sensor of the device, and provides a property 'orientation' to make the current orientation value available to the UI. It inherits from QOrientationFilter and reimplements the 'bool filter(QOrientationReading*)' method to retrieve the sensor data from the QOrientationSensor object.

    class OrientationSensor : public QObject, QOrientationFilter
    {
        Q_OBJECT

        // The property to access the orientation value of the sensor
        Q_PROPERTY(Orientation orientation READ orientation NOTIFY orientationChanged)

    public:
        enum Orientation {
            Undefined = 0,
            TopUp,
            TopDown,
            LeftUp,
            RightUp,
            FaceUp,
            FaceDown
        };
        Q_ENUMS(Orientation);

        OrientationSensor(QObject *parent = 0);

        // The accessor method for the orientation property
        Orientation orientation() const;

    Q_SIGNALS:
        // The change notification signal of the orientation property
        void orientationChanged();

    protected:
        /**
         * This method is reimplemented from the QOrientationFilter interface and is
         * called by the QOrientationFilter whenever new values are available.
         */
        bool filter(QOrientationReading *reading);

    private:
        // The orientation sensor
        QOrientationSensor m_sensor;

        // The orientation value
        Orientation m_orientation;
    };

Inside the constructor we try to connect the QOrientationSensor object to the hardware backend. If that's successful, we register the OrientationSensor class as filter for the QOrientationSensor object and start the sensor to gather data.

    OrientationSensor::OrientationSensor(QObject *parent)
        : QObject(parent)
        , m_orientation(Undefined)
    {

        // At first we have to connect to the sensor backend...
        if (!m_sensor.connectToBackend()) {
            qWarning() << "Cannot connect to orientation sensor backend!";
        }

        // ... and then add a filter that will process the read data
        m_sensor.addFilter(this);

        // Start gathering the data
        m_sensor.start();
    }

The 'bool filter(QOrientationReading*)' method is called whenever the QOrientationSensor object retrieved new data from the hardware sensor. Inside this method we simply map the sensor values to our property and notify the UI about possible changes.

    bool OrientationSensor::filter(QOrientationReading *reading)
    {
        // Store the previous orientation
        const Orientation oldOrientation = m_orientation;

        switch (reading->orientation()) {
        case QOrientationReading::Undefined:
            m_orientation = Undefined;
            break;
        case QOrientationReading::TopUp:
            m_orientation = TopUp;
            break;
        case QOrientationReading::TopDown:
            m_orientation = TopDown;
            break;
        case QOrientationReading::LeftUp:
            m_orientation = LeftUp;
            break;
        case QOrientationReading::RightUp:
            m_orientation = RightUp;
            break;
        case QOrientationReading::FaceUp:
            m_orientation = FaceUp;
            break;
        case QOrientationReading::FaceDown:
            m_orientation = FaceDown;
            break;
        }

        // Emit changed signal if orientation has changed
        if (m_orientation != oldOrientation)
            emit orientationChanged();

        // Do no further processing of the sensor data
        return false;
    }