~baltix/gcompris-qt/0.97.1

« back to all changes in this revision

Viewing changes to src/core/ApplicationInfo.h

  • Committer: Mantas Kriaučiūnas
  • Date: 2020-07-06 18:07:11 UTC
  • Revision ID: baltix@gmail.com-20200706180711-g254osu02cn8bc8p
GCompris-QT 0.97.1 with Lithuanian translation

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GCompris - ApplicationInfo.h
 
2
 *
 
3
 * Copyright (C) 2014-2015 Bruno Coudoin <bruno.coudoin@gcompris.net>
 
4
 *
 
5
 * Authors:
 
6
 *   Bruno Coudoin <bruno.coudoin@gcompris.net>
 
7
 *
 
8
 * This file was originally created from Digia example code under BSD license
 
9
 * and heavily modified since then.
 
10
 *
 
11
 *   This program is free software; you can redistribute it and/or modify
 
12
 *   it under the terms of the GNU General Public License as published by
 
13
 *   the Free Software Foundation; either version 3 of the License, or
 
14
 *   (at your option) any later version.
 
15
 *
 
16
 *   This program is distributed in the hope that it will be useful,
 
17
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
19
 *   GNU General Public License for more details.
 
20
 *
 
21
 *   You should have received a copy of the GNU General Public License
 
22
 *   along with this program; if not, see <https://www.gnu.org/licenses/>.
 
23
 */
 
24
#ifndef APPLICATIONINFO_H
 
25
#define APPLICATIONINFO_H
 
26
 
 
27
#include <config.h>
 
28
#include "ApplicationSettings.h"
 
29
 
 
30
#include <QObject>
 
31
#include <QQmlEngine>
 
32
#include <QtGlobal>
 
33
 
 
34
class QQuickWindow;
 
35
 
 
36
 
 
37
/**
 
38
 * @class ApplicationInfo
 
39
 * @short A general purpose singleton that exposes miscellaneous native
 
40
 *        functions to the QML layer.
 
41
 * @ingroup infrastructure
 
42
 */
 
43
class ApplicationInfo : public QObject
 
44
{
 
45
        Q_OBJECT
 
46
 
 
47
    /**
 
48
     * Width of the application viewport.
 
49
     */
 
50
    Q_PROPERTY(int applicationWidth READ applicationWidth WRITE setApplicationWidth NOTIFY applicationWidthChanged)
 
51
 
 
52
    /**
 
53
     * Platform the application is currently running on.
 
54
     */
 
55
    Q_PROPERTY(Platform platform READ platform CONSTANT)
 
56
 
 
57
    /**
 
58
     * Whether the application is currently running on a mobile platform.
 
59
     *
 
60
     * Mobile platforms are Android, Ios (not supported yet),
 
61
     * Blackberry (not supported)
 
62
     */
 
63
    Q_PROPERTY(bool isMobile READ isMobile CONSTANT)
 
64
 
 
65
    /**
 
66
     * Whether the current platform supports fragment shaders.
 
67
     *
 
68
     * This flag is used in some core modules to selectively deactivate
 
69
     * particle effects which cause crashes on some Android devices.
 
70
     *
 
71
     * cf. https://bugreports.qt.io/browse/QTBUG-44194
 
72
     *
 
73
     * For now always set to false, to prevent crashes.
 
74
     */
 
75
    Q_PROPERTY(bool hasShader READ hasShader CONSTANT)
 
76
 
 
77
    /**
 
78
     * Whether currently in portrait mode, on mobile platforms.
 
79
     *
 
80
     * Based on viewport geometry.
 
81
     */
 
82
    Q_PROPERTY(bool isPortraitMode READ isPortraitMode WRITE setIsPortraitMode NOTIFY portraitModeChanged)
 
83
 
 
84
    /**
 
85
     * Ratio factor used for scaling of sizes on high-dpi devices.
 
86
     *
 
87
     * Must be used by activities as a scaling factor to all pixel values.
 
88
     */
 
89
    Q_PROPERTY(qreal ratio READ ratio NOTIFY ratioChanged)
 
90
 
 
91
    /**
 
92
     * Ratio factor used for font scaling.
 
93
     *
 
94
     * On some low-dpi Android devices with high res (e.g. Galaxy Tab 4) the
 
95
     * fonts in Text-like elements appear too small with respect to the other
 
96
     * graphics -- also if we are using font.pointSize.
 
97
     *
 
98
     * For these cases we calculate a fontRatio in ApplicationInfo that takes
 
99
     * dpi information into account, as proposed on
 
100
     * http://doc.qt.io/qt-5/scalability.html#calculating-scaling-ratio
 
101
     *
 
102
     * GCText applies this factor automatically on its new fontSize property.
 
103
     */
 
104
    Q_PROPERTY(qreal fontRatio READ fontRatio NOTIFY fontRatioChanged)
 
105
 
 
106
    /**
 
107
     * Short (2-letter) locale string of the currently active language.
 
108
     */
 
109
    Q_PROPERTY(QString localeShort READ localeShort CONSTANT)
 
110
 
 
111
    /**
 
112
     * GCompris version string (compile time).
 
113
     */
 
114
    Q_PROPERTY(QString GCVersion READ GCVersion CONSTANT)
 
115
 
 
116
    /**
 
117
     * GCompris version code (compile time).
 
118
     */
 
119
    Q_PROPERTY(int GCVersionCode READ GCVersionCode CONSTANT)
 
120
 
 
121
    /**
 
122
     * Qt version string (runtime).
 
123
     */
 
124
    Q_PROPERTY(QString QTVersion READ QTVersion CONSTANT)
 
125
 
 
126
    /**
 
127
     * Audio codec used for voices resources.
 
128
     *
 
129
     * This is determined at compile time (ogg for free platforms, aac on
 
130
     * MacOSX and IOS).
 
131
     */
 
132
    Q_PROPERTY(QString CompressedAudio READ CompressedAudio CONSTANT)
 
133
 
 
134
    /**
 
135
     * Download allowed
 
136
     *
 
137
     * This is determined at compile time. If set to NO GCompris will
 
138
     * never download anything.
 
139
     */
 
140
    Q_PROPERTY(bool isDownloadAllowed READ isDownloadAllowed CONSTANT)
 
141
 
 
142
    /**
 
143
     * Whether the application is currently using OpenGL or not.
 
144
     *
 
145
     * Use to deactivate some effects if OpenGL not used.
 
146
     */
 
147
    Q_PROPERTY(bool useOpenGL READ useOpenGL WRITE setUseOpenGL NOTIFY useOpenGLChanged)
 
148
 
 
149
    /**
 
150
     * Whether Box2D is installed or not.
 
151
     *
 
152
     * Use to disable activities that use Box2D when it's not installed.
 
153
     */
 
154
    Q_PROPERTY(bool isBox2DInstalled READ isBox2DInstalled NOTIFY isBox2DInstalledChanged)
 
155
        
 
156
public:
 
157
 
 
158
        /**
 
159
         * Known host platforms.
 
160
         */
 
161
    enum Platform {
 
162
        Linux,      /**< Linux (except Android) */
 
163
        Windows,    /**< Windows */
 
164
        MacOSX,     /**< MacOSX */
 
165
        Android,    /**< Android */
 
166
        Ios,        /**< IOS (not supported) */
 
167
        Blackberry, /**< Blackberry (not supported) */
 
168
        SailfishOS  /**< SailfishOS */
 
169
    };
 
170
 
 
171
    Q_ENUM(Platform)
 
172
 
 
173
    /**
 
174
     * Returns an absolute and platform independent path to the passed @p file
 
175
     *
 
176
     * @param file A relative filename.
 
177
     * @returns Absolute path to the file.
 
178
     */
 
179
    static QString getFilePath(const QString &file);
 
180
 
 
181
    /**
 
182
     * Returns the short locale name for the passed @p locale.
 
183
     *
 
184
     * Handles also 'system' (GC_DEFAULT_LOCALE) correctly which resolves to
 
185
     * QLocale::system().name().
 
186
     *
 
187
     * @param locale A locale string of the form \<language\>_\<country\>
 
188
     * @returns A short locale string of the form \<language\>
 
189
     */
 
190
    static QString localeShort(const QString &locale) {
 
191
        QString _locale = locale;
 
192
        if(_locale == GC_DEFAULT_LOCALE) {
 
193
            _locale = QLocale::system().name();
 
194
        }
 
195
        // Can't use left(2) because of Asturian where there are 3 chars
 
196
        return _locale.left(_locale.indexOf('_'));
 
197
    }
 
198
 
 
199
    /**
 
200
     * Returns a locale string that can be used in voices filenames.
 
201
     *
 
202
     * @param locale A locale string of the form \<language\>_\<country\>
 
203
     * @returns A locale string as used in voices filenames.
 
204
     */
 
205
    Q_INVOKABLE QString getVoicesLocale(const QString &locale);
 
206
 
 
207
    /**
 
208
     * Request GCompris to take the Audio Focus at the system level.
 
209
     *
 
210
     * On systems that support it, it will mute a running audio player.
 
211
     */
 
212
    Q_INVOKABLE bool requestAudioFocus() const;
 
213
 
 
214
    /**
 
215
     * Abandon the Audio Focus.
 
216
     *
 
217
     * On systems that support it, it will let an audio player start again.
 
218
     */
 
219
    Q_INVOKABLE void abandonAudioFocus() const;
 
220
 
 
221
    /**
 
222
     * Compare two strings respecting locale specific sort order.
 
223
     *
 
224
     * @param a         First string to compare
 
225
     * @param b         Second string to compare
 
226
     * @param locale    Locale to respect for comparison in any of the forms
 
227
     *                  used in GCompris xx[_XX][.codeset]. Defaults to currently
 
228
     *                  set language from global configuration.
 
229
     * @returns         -1, 0 or 1 if a is less than, equal to or greater than b
 
230
     */
 
231
    Q_INVOKABLE int localeCompare(const QString& a, const QString& b, const QString& locale = "") const;
 
232
 
 
233
    /**
 
234
     * Sort a list of strings respecting locale specific sort order.
 
235
     *
 
236
     * This function is supposed to be called from QML/JS. As there are still
 
237
     * problems marshalling QStringList from C++ to QML/JS we use QVariantList
 
238
     * both for argument and return value.
 
239
     *
 
240
     * @param list      List of strings to be sorted.
 
241
     * @param locale    Locale to respect for sorting in any of the forms
 
242
     *                  used in GCompris xx[_XX][.codeset].
 
243
     * @returns         List sorted by the sort order of the passed locale.
 
244
     */
 
245
    Q_INVOKABLE QVariantList localeSort(QVariantList list, const QString& locale = "") const;
 
246
 
 
247
    /// @cond INTERNAL_DOCS
 
248
 
 
249
    static ApplicationInfo *getInstance() {
 
250
        if(!m_instance) {
 
251
            m_instance = new ApplicationInfo();
 
252
        }
 
253
        return m_instance;
 
254
    }
 
255
    static QObject *applicationInfoProvider(QQmlEngine *engine,
 
256
                                            QJSEngine *scriptEngine);
 
257
    static void setWindow(QQuickWindow *window);
 
258
    explicit ApplicationInfo(QObject *parent = 0);
 
259
    ~ApplicationInfo();
 
260
    int applicationWidth() const { return m_applicationWidth; }
 
261
    void setApplicationWidth(const int newWidth);
 
262
    Platform platform() const { return m_platform; }
 
263
    bool isPortraitMode() const { return m_isPortraitMode; }
 
264
    void setIsPortraitMode(const bool newMode);
 
265
    bool isMobile() const { return m_isMobile; }
 
266
    bool hasShader() const {
 
267
#if defined(Q_OS_ANDROID)
 
268
        return false;
 
269
#else
 
270
        return true;
 
271
#endif
 
272
    }
 
273
    qreal ratio() const { return m_ratio; }
 
274
    qreal fontRatio() const { return m_fontRatio; }
 
275
    QString localeShort() const {
 
276
        return localeShort( ApplicationSettings::getInstance()->locale() );
 
277
    }
 
278
    static QString GCVersion() { return VERSION; }
 
279
    static int GCVersionCode() { return VERSION_CODE; }
 
280
    static QString QTVersion() { return qVersion(); }
 
281
    static QString CompressedAudio() { return COMPRESSED_AUDIO; }
 
282
    static bool isDownloadAllowed() { return QString(DOWNLOAD_ALLOWED) == "ON"; }
 
283
    bool useOpenGL() const { return m_useOpenGL; }
 
284
    void setUseOpenGL(bool useOpenGL) { m_useOpenGL = useOpenGL; }
 
285
 
 
286
    bool isBox2DInstalled() const { return m_isBox2DInstalled; }
 
287
    void setBox2DInstalled(const QQmlEngine &engine);
 
288
 
 
289
    /**
 
290
     * Returns the native screen orientation.
 
291
     *
 
292
     * Wraps QScreen::nativeOrientation: The native orientation of the screen
 
293
     * is the orientation where the logo sticker of the device appears the
 
294
     * right way up, or Qt::PrimaryOrientation if the platform does not support
 
295
     * this functionality.
 
296
     *
 
297
     * The native orientation is a property of the hardware, and does not change
 
298
     */
 
299
    Q_INVOKABLE Qt::ScreenOrientation getNativeOrientation();
 
300
 
 
301
    /**
 
302
     * Change the desired orientation of the application.
 
303
     *
 
304
     * Android specific function, cf. http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)
 
305
     *
 
306
     * @param orientation Desired orientation of the application. For possible
 
307
     *                    values cf. http://developer.android.com/reference/android/content/pm/ActivityInfo.html#screenOrientation .
 
308
     *                    Some useful values:
 
309
     *                    - -1: SCREEN_ORIENTATION_UNSPECIFIED
 
310
     *                    -  0: SCREEN_ORIENTATION_LANDSCAPE: forces landscape
 
311
     *                    -  1: SCREEN_ORIENTATION_PORTRAIT: forces portrait
 
312
     *                    -  5: SCREEN_ORIENTATION_NOSENSOR: forces native
 
313
     *                          orientation mode on each device (portrait on
 
314
     *                          smartphones, landscape on tablet)
 
315
     *                    - 14: SCREEN_ORIENTATION_LOCKED: lock current orientation
 
316
     */
 
317
    Q_INVOKABLE void setRequestedOrientation(int orientation);
 
318
 
 
319
    /**
 
320
     * Query the desired orientation of the application.
 
321
     *
 
322
     * @sa setRequestedOrientation
 
323
     */
 
324
    Q_INVOKABLE int getRequestedOrientation();
 
325
 
 
326
    /**
 
327
     * Checks whether a sensor type from the QtSensor module is supported on
 
328
     * the current platform.
 
329
     *
 
330
     * @param sensorType  Classname of a sensor from the QtSensor module
 
331
     *                    to be checked (e.g. "QTiltSensor").
 
332
     */
 
333
    Q_INVOKABLE bool sensorIsSupported(const QString& sensorType);
 
334
 
 
335
    /**
 
336
     * Toggles activation of screensaver on android
 
337
     *
 
338
     * @param value Whether screensaver should be disabled (true) or
 
339
     *              enabled (false).
 
340
     */
 
341
    Q_INVOKABLE void setKeepScreenOn(bool value);
 
342
 
 
343
    /// @endcond
 
344
 
 
345
public slots:
 
346
    /**
 
347
     * Returns the resource root-path used for GCompris resources.
 
348
     */
 
349
    QString getResourceDataPath();
 
350
 
 
351
    /**
 
352
     * Returns an absolute path to a language specific sound/voices file. If
 
353
     * the file is already absolute only the token replacement is applied.
 
354
     *
 
355
     * @param file A templated relative path to a language specific file. Any
 
356
     *             occurrence of the '$LOCALE' placeholder will be replaced by
 
357
     *             the currently active locale string.
 
358
     *             Any occurrence of '$CA' placeholder will be replaced by
 
359
     *             the current compressed audio format ('ogg' or 'aac).
 
360
     *             Example: 'voices-$CA/$LOCALE/misc/click_on_letter.$CA'
 
361
     * @returns An absolute path to the corresponding resource file.
 
362
     */
 
363
    Q_INVOKABLE QString getAudioFilePath(const QString &file);
 
364
 
 
365
    /**
 
366
     * Returns an absolute path to a language specific sound/voices file. If
 
367
     * the file is already absolute only the token replacement is applied.
 
368
     *
 
369
     * @param file A templated relative path to a language specific file. Any
 
370
     *             occurrence of the '$LOCALE' placeholder will be replaced by
 
371
     *             the currently active locale string.
 
372
     *             Any occurrence of '$CA' placeholder will be replaced by
 
373
     *             the current compressed audio format ('ogg' or 'aac).
 
374
     *             Example: 'voices-$CA/$LOCALE/misc/click_on_letter.$CA'
 
375
     * @param localeName the locale for which to get this audio file
 
376
     * @returns An absolute path to the corresponding resource file.
 
377
     */
 
378
    Q_INVOKABLE QString getAudioFilePathForLocale(const QString &file,
 
379
                                                  const QString &localeName);
 
380
 
 
381
    /**
 
382
     * Returns an absolute path to a language specific resource file.
 
383
     *
 
384
     * Generalization of getAudioFilePath().
 
385
     * @sa getAudioFilePath
 
386
     */
 
387
    Q_INVOKABLE QString getLocaleFilePath(const QString &file);
 
388
 
 
389
    /**
 
390
     * @returns A list of systems-fonts that should be excluded from font
 
391
     *          selection.
 
392
     */
 
393
    Q_INVOKABLE QStringList getSystemExcludedFonts();
 
394
 
 
395
    /**
 
396
     * @returns A list of fonts contained in the fonts resources.
 
397
     */
 
398
    Q_INVOKABLE QStringList getFontsFromRcc();
 
399
    /**
 
400
     * @returns A list of background music contained in the background music resources.
 
401
     */
 
402
    Q_INVOKABLE QStringList getBackgroundMusicFromRcc();
 
403
    /**
 
404
     * Stores a screenshot in the passed @p file.
 
405
     *
 
406
     * @param file Absolute destination filename.
 
407
     */
 
408
    Q_INVOKABLE void screenshot(const QString &file);
 
409
 
 
410
    void notifyPortraitMode();
 
411
    Q_INVOKABLE void notifyFullscreenChanged();
 
412
 
 
413
protected:
 
414
    qreal getSizeWithRatio(const qreal height) { return ratio() * height; }
 
415
 
 
416
signals:
 
417
    void applicationWidthChanged();
 
418
    void portraitModeChanged();
 
419
    void ratioChanged();
 
420
    void fontRatioChanged();
 
421
    void applicationSettingsChanged();
 
422
    void fullscreenChanged();
 
423
    void useOpenGLChanged();
 
424
    void isBox2DInstalledChanged();
 
425
 
 
426
private:
 
427
    static ApplicationInfo *m_instance;
 
428
    int m_applicationWidth;
 
429
    Platform m_platform;
 
430
    bool m_isPortraitMode;
 
431
    bool m_isMobile;
 
432
    bool m_useOpenGL;
 
433
    bool m_isBox2DInstalled;
 
434
    qreal m_ratio;
 
435
    qreal m_fontRatio;
 
436
 
 
437
    // Symbols fonts that user can't see
 
438
    QStringList m_excludedFonts;
 
439
    QStringList m_fontsFromRcc;
 
440
    QStringList m_backgroundMusicFromRcc;
 
441
 
 
442
    static QQuickWindow *m_window;
 
443
};
 
444
 
 
445
#endif // APPLICATIONINFO_H