• Skip to content
  • Skip to link menu
KDE API Documentation - ApplicationInfo.cpp Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
ApplicationInfo.cpp
1 /* GCompris - ApplicationSettingsDefault.cpp
2  *
3  * Copyright (C) 2014 Bruno Coudoin <bruno.coudoin@gcompris.net>
4  *
5  * Authors:
6  * Bruno Coudoin <bruno.coudoin@gcompris.net>
7  *
8  * This file was originaly created from Digia example code under BSD licence
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 <http://www.gnu.org/licenses/>.
23  */
24 
25 #include "ApplicationInfo.h"
26 
27 #include <QtCore/QtMath>
28 #include <QtCore/QUrl>
29 #include <QtCore/QUrlQuery>
30 #include <QtGui/QGuiApplication>
31 #include <QtGui/QScreen>
32 #include <QtCore/QLocale>
33 #include <QtQuick/QQuickWindow>
34 
35 #include <qmath.h>
36 #include <QDebug>
37 
38 #include <QFontDatabase>
39 #include <QDir>
40 
41 QQuickWindow *ApplicationInfo::m_window = NULL;
42 ApplicationInfo *ApplicationInfo::m_instance = NULL;
43 
44 ApplicationInfo::ApplicationInfo(QObject *parent): QObject(parent)
45 {
46 
47  m_isMobile = false;
48 #if defined(Q_OS_ANDROID) || defined(Q_OS_IOS) || defined(Q_OS_BLACKBERRY) || defined(SAILFISHOS)
49  m_isMobile = true;
50 #endif
51 
52 #if defined(Q_OS_ANDROID)
53  // Put android before checking linux/unix as it is also a linux
54  m_platform = Android;
55 #elif (defined(Q_OS_LINUX) || defined(Q_OS_UNIX))
56  m_platform = Linux;
57 #elif defined(Q_OS_WIN)
58  m_platform = Windows;
59 #elif defined(Q_OS_MAC)
60  m_platform = MacOSX;
61 #elif defined(Q_OS_IOS)
62  m_platform = Ios;
63 #elif defined(Q_OS_BLACKBERRY)
64  m_platform = Blackberry;
65 #elif defined(SAILFISHOS)
66  m_platform = SailfishOS;
67 #endif
68 
69  QRect rect = qApp->primaryScreen()->geometry();
70 // m_ratio = 2;
71 // m_ratio = 2.56;
72  m_ratio = m_isMobile ? qMin(qMax(rect.width(), rect.height())/800. , qMin(rect.width(), rect.height())/520.) : 1;
73  // calculate a factor for font-scaling, cf.
74  // http://doc.qt.io/qt-5/scalability.html#calculating-scaling-ratio
75  qreal refDpi = 216.;
76  qreal refHeight = 1776.;
77  qreal refWidth = 1080.;
78  qreal height = qMax(rect.width(), rect.height());
79  qreal width = qMin(rect.width(), rect.height());
80  qreal dpi = qApp->primaryScreen()->logicalDotsPerInch();
81  m_fontRatio = m_isMobile ? qMax(qreal(1.0), qMin(height*refDpi/(dpi*refHeight), width*refDpi/(dpi*refWidth))) : 1;
82  m_isPortraitMode = m_isMobile ? rect.height() > rect.width() : false;
83  m_applicationWidth = m_isMobile ? rect.width() : 1120;
84 
85  if (m_isMobile)
86  connect(qApp->primaryScreen(), SIGNAL(physicalSizeChanged(QSizeF)), this, SLOT(notifyPortraitMode()));
87 
88  // Get all symbol fonts to remove them
89  QFontDatabase database;
90  m_excludedFonts = database.families(QFontDatabase::Symbol);
91 
92  // Get fonts from rcc
93  const QStringList fontFilters = {"*.otf", "*.ttf"};
94  m_fontsFromRcc = QDir(":/gcompris/src/core/resource/fonts").entryList(fontFilters);
95 
96 }
97 
98 ApplicationInfo::~ApplicationInfo()
99 {
100  m_instance = NULL;
101 }
102 
103 void ApplicationInfo::setApplicationWidth(const int newWidth)
104 {
105  if (newWidth != m_applicationWidth) {
106  m_applicationWidth = newWidth;
107  emit applicationWidthChanged();
108  }
109 }
110 
111 QString ApplicationInfo::getResourceDataPath()
112 {
113  return QString("qrc:/gcompris/data");
114 }
115 
116 QString ApplicationInfo::getFilePath(const QString &file)
117 {
118 #if defined(Q_OS_ANDROID)
119  return QString("assets:/%1").arg(file);
120 #elif defined(Q_OS_MAC)
121  return QString("%1/rcc/%2").arg(QCoreApplication::applicationDirPath(), file);
122 #else
123  return QString("%1/%2/rcc/%3").arg(QCoreApplication::applicationDirPath(), GCOMPRIS_DATA_FOLDER, file);
124 #endif
125 }
126 
127 QString ApplicationInfo::getAudioFilePath(const QString &file)
128 {
129  QString localeName = getVoicesLocale(ApplicationSettings::getInstance()->locale());
130 
131  QString filename = file;
132  filename.replace("$LOCALE", localeName);
133  filename.replace("$CA", COMPRESSED_AUDIO);
134 
135  if(file.startsWith("/") || file.startsWith("qrc:") || file.startsWith(":"))
136  return filename;
137  else
138  return getResourceDataPath() + '/' + filename;
139 }
140 
141 QString ApplicationInfo::getLocaleFilePath(const QString &file)
142 {
143  QString localeShortName = localeShort();
144 
145  QString filename = file;
146  filename.replace("$LOCALE", localeShortName);
147  return filename;
148 }
149 
150 QStringList ApplicationInfo::getSystemExcludedFonts()
151 {
152  return m_excludedFonts;
153 }
154 
155 QStringList ApplicationInfo::getFontsFromRcc()
156 {
157  return m_fontsFromRcc;
158 }
159 
160 void ApplicationInfo::notifyPortraitMode()
161 {
162  int width = qApp->primaryScreen()->geometry().width();
163  int height = qApp->primaryScreen()->geometry().height();
164  setIsPortraitMode(height > width);
165 }
166 
167 void ApplicationInfo::setIsPortraitMode(const bool newMode)
168 {
169  if (m_isPortraitMode != newMode) {
170  m_isPortraitMode = newMode;
171  emit portraitModeChanged();
172  }
173 }
174 
175 void ApplicationInfo::setWindow(QQuickWindow *window)
176 {
177  m_window = window;
178 }
179 
180 void ApplicationInfo::screenshot(QString const &path)
181 {
182  QImage img = m_window->grabWindow();
183  img.save(path);
184 }
185 
186 void ApplicationInfo::notifyFullscreenChanged()
187 {
188  if(ApplicationSettings::getInstance()->isFullscreen())
189  m_window->showFullScreen();
190  else
191  m_window->showNormal();
192 }
193 
194 // return the shortest possible locale name for the given locale, describing
195 // a unique voices dataset
196 QString ApplicationInfo::getVoicesLocale(const QString &locale)
197 {
198  QString _locale = locale;
199  if(_locale == GC_DEFAULT_LOCALE) {
200  _locale = QLocale::system().name();
201  }
202  // locales we have country-specific voices for:
203  if (_locale.startsWith(QLatin1String("pt_BR")) || _locale.startsWith(QLatin1String("zh_CN")))
204  return QLocale(_locale).name();
205  // short locale for all the rest:
206  return localeShort(_locale);
207 }
208 
209 QObject *ApplicationInfo::systeminfoProvider(QQmlEngine *engine,
210  QJSEngine *scriptEngine)
211 {
212  Q_UNUSED(engine)
213  Q_UNUSED(scriptEngine)
214  /*
215  * Connect the fullscreen change signal to applicationInfo in order to change
216  * the QQuickWindow value
217  */
218  ApplicationInfo* appInfo = getInstance();
219  connect(ApplicationSettings::getInstance(), SIGNAL(fullscreenChanged()), appInfo,
220  SLOT(notifyFullscreenChanged()));
221  return appInfo;
222 }
223 
224 void ApplicationInfo::init()
225 {
226  qmlRegisterSingletonType<ApplicationInfo>("GCompris", 1, 0,
227  "ApplicationInfo", systeminfoProvider);
228 }
ApplicationInfo::SailfishOS
SailfishOS.
Definition: ApplicationInfo.h:151
ApplicationInfo::MacOSX
MacOSX.
Definition: ApplicationInfo.h:147
ApplicationInfo::Windows
Windows.
Definition: ApplicationInfo.h:146
ApplicationInfo::getFilePath
static QString getFilePath(const QString &file)
Returns an absolute and platform independent path to the passed file.
Definition: ApplicationInfo.cpp:116
ApplicationInfo::getSystemExcludedFonts
Q_INVOKABLE QStringList getSystemExcludedFonts()
Definition: ApplicationInfo.cpp:150
ApplicationInfo::getLocaleFilePath
Q_INVOKABLE QString getLocaleFilePath(const QString &file)
Returns an absolute path to a langauge specific resource file.
Definition: ApplicationInfo.cpp:141
ApplicationInfo::getFontsFromRcc
Q_INVOKABLE QStringList getFontsFromRcc()
Definition: ApplicationInfo.cpp:155
ApplicationInfo::Ios
IOS (not supported)
Definition: ApplicationInfo.h:149
ApplicationInfo::getAudioFilePath
Q_INVOKABLE QString getAudioFilePath(const QString &file)
Returns an absolute path to a language specific sound/voices file.
Definition: ApplicationInfo.cpp:127
ApplicationInfo::Linux
Linux (except Android)
Definition: ApplicationInfo.h:145
ApplicationInfo::Blackberry
Blackberry (not supported)
Definition: ApplicationInfo.h:150
ApplicationInfo::localeShort
QString localeShort
Short (2-letter) locale string of the currently active language.
Definition: ApplicationInfo.h:111
ApplicationInfo::getResourceDataPath
QString getResourceDataPath()
Returns the resource root-path used for GCompris resources.
Definition: ApplicationInfo.cpp:111
ApplicationInfo::screenshot
Q_INVOKABLE void screenshot(const QString &file)
Stores a screenshot in the passed file.
Definition: ApplicationInfo.cpp:180
ApplicationSettings
Singleton that contains GCompris' persistent settings.
Definition: ApplicationSettings.h:63
ApplicationInfo::Android
Android.
Definition: ApplicationInfo.h:148
ApplicationInfo::init
static void init()
Registers singleton in the QML engine.
Definition: ApplicationInfo.cpp:224
ApplicationInfo
A general purpose singleton that exposes miscellaneous native functions to the QML layer...
Definition: ApplicationInfo.h:43
ApplicationInfo::getVoicesLocale
Q_INVOKABLE QString getVoicesLocale(const QString &locale)
Returns a locale string that can be used in voices filenames.
Definition: ApplicationInfo.cpp:196
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal