2015-09-06 20:19:21 +02:00
# include "./resources.h"
2015-04-22 18:57:44 +02:00
2016-07-16 23:12:02 +02:00
# include "resources/config.h"
2015-04-22 18:57:44 +02:00
# include <QString>
# include <QLocale>
# include <QTranslator>
# include <QLibraryInfo>
2015-09-16 17:30:25 +02:00
# include <QFile>
# include <QDir>
# include <QStringBuilder>
# include <QSettings>
2015-04-22 18:57:44 +02:00
# if defined(GUI_QTWIDGETS)
# include <QApplication>
# include <QIcon>
# include <QFont>
# include <QStyleFactory>
# elif defined(GUI_QTQUICK)
# include <QGuiApplication>
# include <QIcon>
# include <QFont>
# else
# include <QCoreApplication>
# endif
# include <iostream>
using namespace std ;
2016-07-27 18:52:59 +02:00
///! \cond
inline void initResources ( ) {
2016-07-27 21:42:27 +02:00
Q_INIT_RESOURCE ( qtutilsicons ) ;
2016-07-27 18:52:59 +02:00
}
inline void cleanupResources ( ) {
2016-07-27 21:42:27 +02:00
Q_CLEANUP_RESOURCE ( qtutilsicons ) ;
2016-07-27 18:52:59 +02:00
}
///! \endcond
2015-04-22 18:57:44 +02:00
2016-07-16 23:12:02 +02:00
/*!
* \ brief Functions for using the resources provided by this library .
*/
2015-04-22 18:57:44 +02:00
namespace QtUtilitiesResources {
/*!
* \ brief Initiates the resources used and provided by this library .
*/
void init ( )
{
2016-07-27 18:52:59 +02:00
initResources ( ) ;
2015-04-22 18:57:44 +02:00
}
/*!
* \ brief Frees the resources used and provided by this library .
*/
void cleanup ( )
{
2016-07-27 18:52:59 +02:00
cleanupResources ( ) ;
2015-04-22 18:57:44 +02:00
}
}
2016-07-16 23:12:02 +02:00
/*!
* \ brief Convenience functions to load translations for Qt and the application .
*/
2015-04-22 18:57:44 +02:00
namespace TranslationFiles {
/*!
2015-06-21 21:45:58 +02:00
* \ brief Loads and installs the appropriate Qt translation file for the current locale .
2016-07-27 21:42:27 +02:00
* \ param repositoryNames Specifies the names of the Qt repositories to load translations for ( eg . qtbase , qtscript , . . . ) .
2016-07-16 23:12:02 +02:00
* \ remarks
* - Translation files have to be placed in one of the following locations :
* * QLibraryInfo : : location ( QLibraryInfo : : TranslationsPath ) ( used in UNIX )
* * . . / share / qt / translations ( used in Windows )
* - Translation files can also be built - in using by setting the CMake variable BUILTIN_TRANSLATIONS .
2016-07-27 18:52:59 +02:00
* - Currently this loads only the translations file for the modules in the base repository .
* TODO : load translations for further modules
2015-04-22 18:57:44 +02:00
*/
2016-07-27 21:42:27 +02:00
void loadQtTranslationFile ( std : : initializer_list < QString > repositoryNames )
2015-04-22 18:57:44 +02:00
{
2016-07-27 21:42:27 +02:00
loadQtTranslationFile ( repositoryNames , QLocale ( ) . name ( ) ) ;
2016-07-16 23:12:02 +02:00
}
/*!
* \ brief Loads and installs the appropriate Qt translation file for the specified locale .
2016-07-27 21:42:27 +02:00
* \ param repositoryNames Specifies the names of the Qt repositories to load translations for ( eg . qtbase , qtscript , . . . ) .
2016-07-16 23:12:02 +02:00
* \ param localeName Specifies the name of the locale .
* \ remarks
* - Translation files have to be placed in one of the following locations :
* * QLibraryInfo : : location ( QLibraryInfo : : TranslationsPath ) ( used in UNIX )
* * . . / share / qt / translations ( used in Windows )
* - Translation files can also be built - in using by setting the CMake variable BUILTIN_TRANSLATIONS .
2016-07-27 18:52:59 +02:00
* - Currently this loads only the translations file for the modules in the base repository .
2016-07-16 23:12:02 +02:00
*/
2016-07-27 21:42:27 +02:00
void loadQtTranslationFile ( initializer_list < QString > repositoryNames , const QString & localeName )
2016-07-16 23:12:02 +02:00
{
2016-07-27 21:42:27 +02:00
for ( const QString & repoName : repositoryNames ) {
QTranslator * qtTranslator = new QTranslator ;
const QString fileName ( repoName % QChar ( ' _ ' ) % localeName ) ;
if ( qtTranslator - > load ( fileName ,
QLibraryInfo : : location ( QLibraryInfo : : TranslationsPath ) ) ) {
QCoreApplication : : installTranslator ( qtTranslator ) ;
} else if ( qtTranslator - > load ( fileName , QStringLiteral ( " ../share/qt/translations " ) ) ) {
// used in Windows
QCoreApplication : : installTranslator ( qtTranslator ) ;
} else if ( qtTranslator - > load ( fileName , QStringLiteral ( " :/translations " ) ) ) {
QCoreApplication : : installTranslator ( qtTranslator ) ;
} else {
delete qtTranslator ;
cerr < < " Unable to load translation file for Qt repository " < < repoName . toLocal8Bit ( ) . data ( ) < < " and language " < < localeName . toLocal8Bit ( ) . data ( ) < < " . " < < endl ;
}
2015-04-22 18:57:44 +02:00
}
}
/*!
2015-06-21 21:45:58 +02:00
* \ brief Loads and installs the appropriate application translation file for the current locale .
2015-04-22 18:57:44 +02:00
* \ param applicationName Specifies the name of the application .
2016-07-16 23:12:02 +02:00
* \ remarks
* - Translation files have to be placed in one of the following locations :
* * . / translations
* * / usr / share / $ application / translations ( used in UNIX )
* * . . / share / $ application / translations ( used in Windows )
* - Translation files must be named using the following scheme :
* * $ application_ $ language . qm
* - Translation files can also be built - in using by setting the CMake variable BUILTIN_TRANSLATIONS .
2015-04-22 18:57:44 +02:00
*/
void loadApplicationTranslationFile ( const QString & applicationName )
{
2015-08-08 01:38:48 +02:00
// load English translation files as fallback
loadApplicationTranslationFile ( applicationName , QStringLiteral ( " en_US " ) ) ;
// load translation files for current locale
2015-06-21 21:45:58 +02:00
loadApplicationTranslationFile ( applicationName , QLocale ( ) . name ( ) ) ;
}
/*!
* \ brief Loads and installs the appropriate application translation file for the specified locale .
* \ param applicationName Specifies the name of the application .
2016-06-10 23:05:43 +02:00
* \ param localeName Specifies the name of the locale .
2016-07-16 23:12:02 +02:00
* \ remarks
* - Translation files have to be placed in one of the following locations :
* * . / translations
* * / usr / share / $ application / translations ( used in UNIX )
* * . . / share / $ application / translations ( used in Windows )
* - Translation files must be named using the following scheme :
* * $ application_ $ language . qm
* - Translation files can also be built - in using by setting the CMake variable BUILTIN_TRANSLATIONS .
2015-06-21 21:45:58 +02:00
*/
void loadApplicationTranslationFile ( const QString & applicationName , const QString & localeName )
{
2015-04-22 18:57:44 +02:00
QTranslator * appTranslator = new QTranslator ;
2016-07-16 23:12:02 +02:00
const QString fileName ( QStringLiteral ( " %1_%2 " ) . arg ( applicationName , localeName ) ) ;
2016-04-24 20:53:14 +02:00
if ( appTranslator - > load ( fileName , QStringLiteral ( " . " ) ) ) {
2015-04-22 18:57:44 +02:00
QCoreApplication : : installTranslator ( appTranslator ) ;
2016-04-24 20:53:14 +02:00
} else if ( appTranslator - > load ( fileName , QStringLiteral ( " ./translations " ) ) ) {
2015-08-08 01:38:48 +02:00
QCoreApplication : : installTranslator ( appTranslator ) ;
2016-07-16 23:12:02 +02:00
} else if ( appTranslator - > load ( fileName , QStringLiteral ( APP_INSTALL_PREFIX " /share/%1/translations " ) . arg ( applicationName ) ) ) {
2015-04-22 18:57:44 +02:00
QCoreApplication : : installTranslator ( appTranslator ) ;
2015-08-25 19:16:43 +02:00
} else if ( appTranslator - > load ( fileName , QStringLiteral ( " ../share/%1/translations " ) . arg ( applicationName ) ) ) {
QCoreApplication : : installTranslator ( appTranslator ) ;
2016-07-16 23:12:02 +02:00
} else if ( appTranslator - > load ( fileName , QStringLiteral ( " :/translations " ) ) ) {
QCoreApplication : : installTranslator ( appTranslator ) ;
2015-04-22 18:57:44 +02:00
} else {
2015-07-14 18:33:44 +02:00
delete appTranslator ;
2016-07-16 23:12:02 +02:00
cerr < < " Unable to load application translation file for the language \" " < < localeName . toLocal8Bit ( ) . data ( ) < < " \" . " < < endl ;
2015-04-22 18:57:44 +02:00
}
}
}
2016-07-16 23:12:02 +02:00
/*!
* \ brief Convenience functions to check whether a QCoreApplication / QGuiApplication / QApplication singleton has been instantiated yet .
*/
2015-04-22 18:57:44 +02:00
namespace ApplicationInstances {
# if defined(GUI_QTWIDGETS)
2016-07-16 23:12:02 +02:00
/*!
* \ brief Returns whether a QApplication has been instantiated yet .
*/
2015-04-22 18:57:44 +02:00
bool hasWidgetsApp ( )
{
2015-09-01 20:08:43 +02:00
return qobject_cast < QApplication * > ( QCoreApplication : : instance ( ) ) ! = nullptr ;
2015-04-22 18:57:44 +02:00
}
# endif
# if defined(GUI_QTWIDGETS) || defined(GUI_QTQUICK)
2016-07-16 23:12:02 +02:00
/*!
* \ brief Returns whether a QGuiApplication has been instantiated yet .
*/
2015-04-22 18:57:44 +02:00
bool hasGuiApp ( )
{
2015-09-01 20:08:43 +02:00
return qobject_cast < QGuiApplication * > ( QCoreApplication : : instance ( ) ) ! = nullptr ;
2015-04-22 18:57:44 +02:00
}
# endif
2016-07-16 23:12:02 +02:00
/*!
* \ brief Returns whether a QCoreApplication has been instantiated yet .
*/
2015-04-22 18:57:44 +02:00
bool hasCoreApp ( )
{
2015-09-01 20:08:43 +02:00
return qobject_cast < QCoreApplication * > ( QCoreApplication : : instance ( ) ) ! = nullptr ;
2015-04-22 18:57:44 +02:00
}
}
2015-09-16 17:30:25 +02:00
2016-07-16 23:12:02 +02:00
/*!
* \ brief Provides convenience functions for handling config files .
*/
2015-09-16 17:30:25 +02:00
namespace ConfigFile {
2016-07-16 23:12:02 +02:00
/*!
* \ brief Locates the config file with the specified \ a fileName for the application with the specified \ a applicationName .
* \ remarks If \ a settings is not nullptr , the path provided by that object is also considered .
*/
2015-09-16 17:30:25 +02:00
QString locateConfigFile ( const QString & applicationName , const QString & fileName , const QSettings * settings )
{
// check whether the file is in the current working directory
if ( QFile : : exists ( fileName ) ) {
return fileName ;
} else {
// check whether the file is in the settings directory used by QSettings
QString path ;
if ( settings ) {
path = QFileInfo ( settings - > fileName ( ) ) . absoluteDir ( ) . absoluteFilePath ( fileName ) ;
if ( QFile : : exists ( path ) ) {
return path ;
}
}
// check whether there is a user created version of the file under /etc/app/
2015-09-22 01:53:38 +02:00
# ifdef Q_OS_WIN32
2015-09-16 17:30:25 +02:00
// use relative paths on Windows
path = QStringLiteral ( " ../etc/ " ) % applicationName % QChar ( ' / ' ) % fileName ;
if ( QFile : : exists ( path ) ) {
return path ;
} else {
// check whether there is the default version of the file under /usr/share/app/
path = QStringLiteral ( " ../share/ " ) % applicationName % QChar ( ' / ' ) % fileName ;
if ( QFile : : exists ( path ) ) {
return path ;
} else {
return QString ( ) ; // file is not present
}
}
# else
path = QStringLiteral ( " /etc/ " ) % applicationName % QChar ( ' / ' ) % fileName ;
if ( QFile : : exists ( path ) ) {
return path ;
} else {
// check whether there is the default version of the file under /usr/share/app/
path = QStringLiteral ( " /usr/share/ " ) % applicationName % QChar ( ' / ' ) % fileName ;
if ( QFile : : exists ( path ) ) {
return path ;
} else {
return QString ( ) ; // file is not present
}
}
# endif
}
}
}