1
/* GCompris - ApplicationInfo.h
3
* Copyright (C) 2014-2015 Bruno Coudoin <bruno.coudoin@gcompris.net>
6
* Bruno Coudoin <bruno.coudoin@gcompris.net>
8
* This file was originally created from Digia example code under BSD license
9
* and heavily modified since then.
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.
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.
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/>.
24
#ifndef APPLICATIONINFO_H
25
#define APPLICATIONINFO_H
28
#include "ApplicationSettings.h"
38
* @class ApplicationInfo
39
* @short A general purpose singleton that exposes miscellaneous native
40
* functions to the QML layer.
41
* @ingroup infrastructure
43
class ApplicationInfo : public QObject
48
* Width of the application viewport.
50
Q_PROPERTY(int applicationWidth READ applicationWidth WRITE setApplicationWidth NOTIFY applicationWidthChanged)
53
* Platform the application is currently running on.
55
Q_PROPERTY(Platform platform READ platform CONSTANT)
58
* Whether the application is currently running on a mobile platform.
60
* Mobile platforms are Android, Ios (not supported yet),
61
* Blackberry (not supported)
63
Q_PROPERTY(bool isMobile READ isMobile CONSTANT)
66
* Whether the current platform supports fragment shaders.
68
* This flag is used in some core modules to selectively deactivate
69
* particle effects which cause crashes on some Android devices.
71
* cf. https://bugreports.qt.io/browse/QTBUG-44194
73
* For now always set to false, to prevent crashes.
75
Q_PROPERTY(bool hasShader READ hasShader CONSTANT)
78
* Whether currently in portrait mode, on mobile platforms.
80
* Based on viewport geometry.
82
Q_PROPERTY(bool isPortraitMode READ isPortraitMode WRITE setIsPortraitMode NOTIFY portraitModeChanged)
85
* Ratio factor used for scaling of sizes on high-dpi devices.
87
* Must be used by activities as a scaling factor to all pixel values.
89
Q_PROPERTY(qreal ratio READ ratio NOTIFY ratioChanged)
92
* Ratio factor used for font scaling.
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.
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
102
* GCText applies this factor automatically on its new fontSize property.
104
Q_PROPERTY(qreal fontRatio READ fontRatio NOTIFY fontRatioChanged)
107
* Short (2-letter) locale string of the currently active language.
109
Q_PROPERTY(QString localeShort READ localeShort CONSTANT)
112
* GCompris version string (compile time).
114
Q_PROPERTY(QString GCVersion READ GCVersion CONSTANT)
117
* GCompris version code (compile time).
119
Q_PROPERTY(int GCVersionCode READ GCVersionCode CONSTANT)
122
* Qt version string (runtime).
124
Q_PROPERTY(QString QTVersion READ QTVersion CONSTANT)
127
* Audio codec used for voices resources.
129
* This is determined at compile time (ogg for free platforms, aac on
132
Q_PROPERTY(QString CompressedAudio READ CompressedAudio CONSTANT)
137
* This is determined at compile time. If set to NO GCompris will
138
* never download anything.
140
Q_PROPERTY(bool isDownloadAllowed READ isDownloadAllowed CONSTANT)
143
* Whether the application is currently using OpenGL or not.
145
* Use to deactivate some effects if OpenGL not used.
147
Q_PROPERTY(bool useOpenGL READ useOpenGL WRITE setUseOpenGL NOTIFY useOpenGLChanged)
150
* Whether Box2D is installed or not.
152
* Use to disable activities that use Box2D when it's not installed.
154
Q_PROPERTY(bool isBox2DInstalled READ isBox2DInstalled NOTIFY isBox2DInstalledChanged)
159
* Known host platforms.
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 */
174
* Returns an absolute and platform independent path to the passed @p file
176
* @param file A relative filename.
177
* @returns Absolute path to the file.
179
static QString getFilePath(const QString &file);
182
* Returns the short locale name for the passed @p locale.
184
* Handles also 'system' (GC_DEFAULT_LOCALE) correctly which resolves to
185
* QLocale::system().name().
187
* @param locale A locale string of the form \<language\>_\<country\>
188
* @returns A short locale string of the form \<language\>
190
static QString localeShort(const QString &locale) {
191
QString _locale = locale;
192
if(_locale == GC_DEFAULT_LOCALE) {
193
_locale = QLocale::system().name();
195
// Can't use left(2) because of Asturian where there are 3 chars
196
return _locale.left(_locale.indexOf('_'));
200
* Returns a locale string that can be used in voices filenames.
202
* @param locale A locale string of the form \<language\>_\<country\>
203
* @returns A locale string as used in voices filenames.
205
Q_INVOKABLE QString getVoicesLocale(const QString &locale);
208
* Request GCompris to take the Audio Focus at the system level.
210
* On systems that support it, it will mute a running audio player.
212
Q_INVOKABLE bool requestAudioFocus() const;
215
* Abandon the Audio Focus.
217
* On systems that support it, it will let an audio player start again.
219
Q_INVOKABLE void abandonAudioFocus() const;
222
* Compare two strings respecting locale specific sort order.
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
231
Q_INVOKABLE int localeCompare(const QString& a, const QString& b, const QString& locale = "") const;
234
* Sort a list of strings respecting locale specific sort order.
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.
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.
245
Q_INVOKABLE QVariantList localeSort(QVariantList list, const QString& locale = "") const;
247
/// @cond INTERNAL_DOCS
249
static ApplicationInfo *getInstance() {
251
m_instance = new ApplicationInfo();
255
static QObject *applicationInfoProvider(QQmlEngine *engine,
256
QJSEngine *scriptEngine);
257
static void setWindow(QQuickWindow *window);
258
explicit ApplicationInfo(QObject *parent = 0);
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)
273
qreal ratio() const { return m_ratio; }
274
qreal fontRatio() const { return m_fontRatio; }
275
QString localeShort() const {
276
return localeShort( ApplicationSettings::getInstance()->locale() );
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; }
286
bool isBox2DInstalled() const { return m_isBox2DInstalled; }
287
void setBox2DInstalled(const QQmlEngine &engine);
290
* Returns the native screen orientation.
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.
297
* The native orientation is a property of the hardware, and does not change
299
Q_INVOKABLE Qt::ScreenOrientation getNativeOrientation();
302
* Change the desired orientation of the application.
304
* Android specific function, cf. http://developer.android.com/reference/android/app/Activity.html#setRequestedOrientation(int)
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
317
Q_INVOKABLE void setRequestedOrientation(int orientation);
320
* Query the desired orientation of the application.
322
* @sa setRequestedOrientation
324
Q_INVOKABLE int getRequestedOrientation();
327
* Checks whether a sensor type from the QtSensor module is supported on
328
* the current platform.
330
* @param sensorType Classname of a sensor from the QtSensor module
331
* to be checked (e.g. "QTiltSensor").
333
Q_INVOKABLE bool sensorIsSupported(const QString& sensorType);
336
* Toggles activation of screensaver on android
338
* @param value Whether screensaver should be disabled (true) or
341
Q_INVOKABLE void setKeepScreenOn(bool value);
347
* Returns the resource root-path used for GCompris resources.
349
QString getResourceDataPath();
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.
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.
363
Q_INVOKABLE QString getAudioFilePath(const QString &file);
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.
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.
378
Q_INVOKABLE QString getAudioFilePathForLocale(const QString &file,
379
const QString &localeName);
382
* Returns an absolute path to a language specific resource file.
384
* Generalization of getAudioFilePath().
385
* @sa getAudioFilePath
387
Q_INVOKABLE QString getLocaleFilePath(const QString &file);
390
* @returns A list of systems-fonts that should be excluded from font
393
Q_INVOKABLE QStringList getSystemExcludedFonts();
396
* @returns A list of fonts contained in the fonts resources.
398
Q_INVOKABLE QStringList getFontsFromRcc();
400
* @returns A list of background music contained in the background music resources.
402
Q_INVOKABLE QStringList getBackgroundMusicFromRcc();
404
* Stores a screenshot in the passed @p file.
406
* @param file Absolute destination filename.
408
Q_INVOKABLE void screenshot(const QString &file);
410
void notifyPortraitMode();
411
Q_INVOKABLE void notifyFullscreenChanged();
414
qreal getSizeWithRatio(const qreal height) { return ratio() * height; }
417
void applicationWidthChanged();
418
void portraitModeChanged();
420
void fontRatioChanged();
421
void applicationSettingsChanged();
422
void fullscreenChanged();
423
void useOpenGLChanged();
424
void isBox2DInstalledChanged();
427
static ApplicationInfo *m_instance;
428
int m_applicationWidth;
430
bool m_isPortraitMode;
433
bool m_isBox2DInstalled;
437
// Symbols fonts that user can't see
438
QStringList m_excludedFonts;
439
QStringList m_fontsFromRcc;
440
QStringList m_backgroundMusicFromRcc;
442
static QQuickWindow *m_window;
445
#endif // APPLICATIONINFO_H