ActivityBase.qml 5.91 KB
Newer Older
1
2
/* GCompris - ActivityBase.qml
 *
3
 * Copyright (C) 2014 Bruno Coudoin <bruno.coudoin@gcompris.net>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * Authors:
 *   Bruno Coudoin <bruno.coudoin@gcompris.net>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, see <http://www.gnu.org/licenses/>.
 */
Bruno Coudoin's avatar
Bruno Coudoin committed
21
import QtQuick 2.2
22
import GCompris 1.0
23
import "qrc:/gcompris/src/core/core.js" as Core
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
/**
 * The base QML component for activities in GCompris.
 * @ingroup components
 *
 * Each activity should be derived from this component. It is responsible for
 *
 * * basic common key handling,
 * * unified audio handling,
 * * screen switching dynamics (from/to Menu/DialogHelp/etc.)
 *
 * The following common keys are handled so far:
 *
 * * @c Ctrl+q: Exit the application.
 * * @c Ctrl+b: Toggle the bar.
 * * @c Ctrl+f: Toggle fullscreen.
 * * @c Ctrl+m: Toggle audio effects.
 * * @c Ctrl+w: Exit the current activity and return to the menu.
 * * @c Ctrl+p: Make a screenshot.
 * * @c Back:   Return to the home screen (corresponds to the 'Back' button on
 *              Android).
 *
 * Cf. Template.qml for a sample skeleton activity.
 *
 * Cf.
 * [the wiki](http://gcompris.net/wiki/Qt_Quick_development_process#Adding_a_new_activity)
 * for further information about creating a new activity.
 *
 * @inherit QtQuick.Item
 */
54
55
Item {
    id: page
56
57
58
59
60

    /**
     * type:Item
     * Parent object.
     */
61
    property Item main: parent;
62
63
64
65
66
67
68
69
70
71
72
73
74

    /**
     * type:Component
     * The top-level component containing the visible viewport of an activity.
     *
     * Put all you want to present the user into this container. Mostly
     * implemented using a Rectangle or Image component, itself
     * containing further graphical elements. You are pretty free of doing
     * whatever you want inside this component.
     *
     * Also common elements as Bar, Score, DialogHelp, etc. should be placed
     * inside this element.
     */
75
    property Component pageComponent
76
77
78
79
80
81
82

    /**
     * type:QtObject
     * Reference to the menu activity.
     *
     * Populated automatically during activity-loading.
     */
83
    property QtObject menu
84
85
86
87
88
89
90

    /**
     * type:QtObject
     * Reference to the ActivityInfo object of the activity.
     *
     * Populated automatically during activity-loading.
     */
91
    property QtObject activityInfo
92
93
94
95
96
97
98
99
100
101
102
103
104

    /**
     * type:GCAudio
     * The global audio item for voices.
     *
     * Because of problems synchronizing multiple Audio objects between
     * global/menu/main and individual activities, activities should refrain
     * from implementing additional Audio elements.
     *
     * Instead append to this global object to play your voices after the
     * intro music.
     * @sa GCAudio audioEffects
     */
Bruno Coudoin's avatar
Bruno Coudoin committed
105
    property GCAudio audioVoices
106
107
108
109
110
111
112
113

    /**
     * type:GCAudio
     * The global audio item for audio effects.
     *
     * Append to it to play your effects.
     * @sa GCAudio audioEffects
     */
Bruno Coudoin's avatar
Bruno Coudoin committed
114
    property GCAudio audioEffects
115
116
117
118

    /**
     * Emitted when the user wants to return to the Home/Menu screen.
     */
119
    signal home
120
121
122
123
124
125

    /**
     * Emitted every time the activity has been started.
     *
     * Initialize your activity upon this signal.
     */
126
    signal start
127
128
129
130
131
132

    /**
     * Emitted when the activity is about to stop
     *
     * Shutdown whatever you need to upon this signal.
     */
133
    signal stop
134
135
136
137
138
139
140
141
142

    /**
     * Emitted when dialog @p dialog should be shown
     *
     * Emit this signal when you want to show another dialog, e.g. on
     * Bar.onHelpClicked
     *
     * @param dialog Dialog to show.
     */
143
144
    signal displayDialog(Item dialog)

145
146
    onHome: menu ? menu.home() : ""
    onDisplayDialog: menu ? menu.displayDialog(dialog) : ""
147

148
    Keys.forwardTo: activity.children
149
150
151
152
    Keys.onEscapePressed: home()
    Keys.onPressed: {
        if (event.modifiers === Qt.ControlModifier &&
                event.key === Qt.Key_Q) {
153
            // Ctrl+Q exit the application
154
            Core.quit(main);
155
156
        } else if (event.modifiers === Qt.ControlModifier &&
                event.key === Qt.Key_B) {
157
            // Ctrl+B toggle the bar
158
            ApplicationSettings.isBarHidden = !ApplicationSettings.isBarHidden;
159
160
        } else if (event.modifiers === Qt.ControlModifier &&
                event.key === Qt.Key_F) {
161
            // Ctrl+F toggle fullscreen
Johnny Jazeix's avatar
Johnny Jazeix committed
162
            ApplicationSettings.isFullscreen = !ApplicationSettings.isFullscreen
163
164
165
166
167
168
169
170
        } else if (event.modifiers === Qt.ControlModifier &&
                   event.key === Qt.Key_M) {
            // Ctrl+M toggle sound
            ApplicationSettings.isAudioEffectsEnabled = !ApplicationSettings.isAudioEffectsEnabled
        } else if (event.modifiers === Qt.ControlModifier &&
                   event.key === Qt.Key_W) {
            // Ctrl+W exit the current activity
            home()
171
172
173
174
        } else if (event.modifiers === Qt.ControlModifier &&
                   event.key === Qt.Key_P) {
            // Ctrl+P Screenshot
            ApplicationInfo.screenshot("/tmp/" + activityInfo.name.split('/')[0] + ".png")
175
176
        }
    }
177
178
179
180
181
182
    Keys.onReleased: {
        if (event.key === Qt.Key_Back) {
            event.accepted = true
            home()
        }
    }
183
184

    Loader {
185
        id: activity
186
187
188
        sourceComponent: pageComponent
        anchors.fill: parent
    }
189
190
191

    Loader {
        id: demoPageLoader
192
        source: "BuyMeOverlay.qml"
193
194
195
        anchors.fill: parent
        active: !activityInfo.demo && ApplicationSettings.isDemoMode
    }
196
}