summaryrefslogtreecommitdiffstats
path: root/doc/src/publishsubscribe/examples/battery-charge.qdoc
blob: ace755c2e44887a3744d6a089364c3c83b03bbf7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/****************************************************************************
**
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the documentation of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
    \example battery-charge
    \ingroup publishsubscribe-examples
    \title Accessing Publish and Subscribe from QML

    This examples shows how to access published context values from within QML.  The example
    consists of two programs.  The first, battery-publisher, is a standard Qt Widgets application
    to emulate a battery for the sole purpose of demonstrating the second program.  It publishes
    the keys

    \code
        /power/battery/charge
        /power/battery/charging
    \endcode

    and provides controls for modifying their values.

    \image battery-publisher.png

    The second program, battery-subscriber, is implemented in  pure QML.  It has the following
    features: A rectangular area representing the percent charge of the battery.  It indicates a
    low battery state by changing the color to red, it is green otherwise.  An animation is shown
    to indicate that the battery is being recharged.

    \image battery-subscriber.png

    \section2 Battery Subscriber

    First, import the plugin library that provides the ValueSpaceSubscriber QML element.

    \snippet battery-charge/battery-subscriber/battery-subscriber.qml 4

    Two ValueSpaceSubscriber instances are created, one for each of the battery values.  We give
    each object a unique id so that we can reference it from elsewhere in the QML.  We set the
    \e path properties to the Value Space path of the keys.

    \snippet battery-charge/battery-subscriber/battery-subscriber.qml 0

    The default state of the rectangle used to visualize the battery charge uses the \e charge
    property of our BatteryCharge class in the expression for its height.

    \snippet battery-charge/battery-subscriber/battery-subscriber.qml 1

    When the battery charge changes the height of the rectangle will automatically change.

    Next we define two additional states.  The \e low state is entered when the battery charge
    drops below 25% and the battery is not being recharged.  When in this state the color is set to
    red.

    \snippet battery-charge/battery-subscriber/battery-subscriber.qml 2

    The \e charging state is entered when the battery is being recharged.  When in this state a
    particle effect animation is enabled.

    \snippet battery-charge/battery-subscriber/battery-subscriber.qml 3
*/