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-09-16 17:30:25 +02:00
|
|
|
#include <QDir>
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QFile>
|
|
|
|
#include <QLibraryInfo>
|
|
|
|
#include <QLocale>
|
2015-09-16 17:30:25 +02:00
|
|
|
#include <QSettings>
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QString>
|
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QTranslator>
|
2017-04-27 22:03:37 +02:00
|
|
|
#if defined(QT_UTILITIES_GUI_QTWIDGETS)
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QFont>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QStyleFactory>
|
2017-04-27 22:03:37 +02:00
|
|
|
#elif defined(QT_UTILITIES_GUI_QTQUICK)
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QFont>
|
|
|
|
#include <QGuiApplication>
|
|
|
|
#include <QIcon>
|
2015-04-22 18:57:44 +02:00
|
|
|
#else
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QCoreApplication>
|
2015-04-22 18:57:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2016-07-27 18:52:59 +02:00
|
|
|
///! \cond
|
2017-05-01 03:16:25 +02:00
|
|
|
inline void initResources()
|
|
|
|
{
|
2016-07-27 21:42:27 +02:00
|
|
|
Q_INIT_RESOURCE(qtutilsicons);
|
2016-07-27 18:52:59 +02:00
|
|
|
}
|
|
|
|
|
2017-05-01 03:16:25 +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.
|
2017-02-11 02:04:12 +01:00
|
|
|
* \deprecated Replaced by ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES macro.
|
2016-07-16 23:12:02 +02:00
|
|
|
*/
|
2015-04-22 18:57:44 +02:00
|
|
|
namespace QtUtilitiesResources {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Initiates the resources used and provided by this library.
|
2017-02-11 02:04:12 +01:00
|
|
|
* \deprecated Replaced by ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES macro.
|
2015-04-22 18:57:44 +02:00
|
|
|
*/
|
|
|
|
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.
|
2017-02-11 02:04:12 +01:00
|
|
|
* \deprecated Replaced by ENABLE_QT_RESOURCES_OF_STATIC_DEPENDENCIES macro.
|
2015-04-22 18:57:44 +02:00
|
|
|
*/
|
|
|
|
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 {
|
|
|
|
|
2016-08-14 22:44:25 +02:00
|
|
|
/*!
|
|
|
|
* \brief Allows to set an additional search path for translation files.
|
|
|
|
* \remarks This path is considered *before* the default directories.
|
|
|
|
*/
|
|
|
|
QString &additionalTranslationFilePath()
|
|
|
|
{
|
|
|
|
static QString path;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2015-04-22 18:57:44 +02:00
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Loads and installs the appropriate Qt translation file for the current
|
|
|
|
* locale.
|
|
|
|
* \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)
|
2017-05-04 22:46:37 +02:00
|
|
|
* - Translation files can also be built-in using by setting the CMake variable
|
2017-08-15 01:08:08 +02:00
|
|
|
* BUILTIN_TRANSLATIONS.
|
2017-05-04 22:46:37 +02:00
|
|
|
* In this case it is also necessary to load the translations using this
|
2017-08-15 01:08:08 +02:00
|
|
|
* function.
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Loads and installs the appropriate Qt translation file for the
|
|
|
|
* specified locale.
|
|
|
|
* \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)
|
2017-05-04 22:46:37 +02:00
|
|
|
* - Translation files can also be built-in using by setting the CMake variable
|
2017-08-15 01:08:08 +02:00
|
|
|
* BUILTIN_TRANSLATIONS.
|
2017-05-04 22:46:37 +02:00
|
|
|
* In this case it is also necessary to load the translations using this
|
2017-08-15 01:08:08 +02:00
|
|
|
* function.
|
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
|
|
|
{
|
2017-05-01 03:16:25 +02:00
|
|
|
for (const QString &repoName : repositoryNames) {
|
2016-07-27 21:42:27 +02:00
|
|
|
QTranslator *qtTranslator = new QTranslator;
|
|
|
|
const QString fileName(repoName % QChar('_') % localeName);
|
2017-05-01 03:16:25 +02:00
|
|
|
if (!additionalTranslationFilePath().isEmpty() && qtTranslator->load(fileName, additionalTranslationFilePath())) {
|
2016-08-14 22:44:25 +02:00
|
|
|
QCoreApplication::installTranslator(qtTranslator);
|
2017-05-01 03:16:25 +02:00
|
|
|
} else if (qtTranslator->load(fileName, QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
|
2016-07-27 21:42:27 +02:00
|
|
|
QCoreApplication::installTranslator(qtTranslator);
|
2017-05-01 03:16:25 +02:00
|
|
|
} else if (qtTranslator->load(fileName, QStringLiteral("../share/qt/translations"))) {
|
2016-07-27 21:42:27 +02:00
|
|
|
QCoreApplication::installTranslator(qtTranslator);
|
2017-05-01 03:16:25 +02:00
|
|
|
} else if (qtTranslator->load(fileName, QStringLiteral(":/translations"))) {
|
2016-07-27 21:42:27 +02:00
|
|
|
QCoreApplication::installTranslator(qtTranslator);
|
|
|
|
} else {
|
|
|
|
delete qtTranslator;
|
2017-08-15 01:08:08 +02:00
|
|
|
if (localeName.startsWith(QLatin1String("en"))) {
|
|
|
|
// the translation file is probably just empty (English is built-in and usually only used for plural forms)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cerr << "Unable to load translation file for Qt repository \"" << repoName.toLocal8Bit().data() << "\" and locale "
|
2017-05-01 03:16:25 +02:00
|
|
|
<< localeName.toLocal8Bit().data() << "." << endl;
|
2016-07-27 21:42:27 +02:00
|
|
|
}
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-05-04 22:46:37 +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
|
2017-05-04 22:46:37 +02:00
|
|
|
* - Translation files can also be built-in using by setting the CMake variable
|
2017-08-15 01:08:08 +02:00
|
|
|
* BUILTIN_TRANSLATIONS.
|
2017-05-04 22:46:37 +02:00
|
|
|
* In this case it is also necessary to load the translations using this
|
2017-08-15 01:08:08 +02:00
|
|
|
* function.
|
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
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QLocale().name() != QLatin1String("en_US")) {
|
2016-08-14 22:44:25 +02:00
|
|
|
loadApplicationTranslationFile(applicationName, QLocale().name());
|
|
|
|
}
|
2015-06-21 21:45:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Loads and installs the appropriate application translation file for
|
|
|
|
* the specified locale.
|
2015-06-21 21:45:58 +02:00
|
|
|
* \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
|
2017-05-04 22:46:37 +02:00
|
|
|
* - Translation files can also be built-in using by setting the CMake variable
|
2017-08-15 01:08:08 +02:00
|
|
|
* BUILTIN_TRANSLATIONS.
|
2017-05-04 22:46:37 +02:00
|
|
|
* In this case it is also necessary to load the translations using this
|
2017-08-15 01:08:08 +02:00
|
|
|
* function.
|
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));
|
2017-05-01 03:16:25 +02:00
|
|
|
if (!additionalTranslationFilePath().isEmpty() && appTranslator->load(fileName, additionalTranslationFilePath())) {
|
2016-08-14 22:44:25 +02:00
|
|
|
QCoreApplication::installTranslator(appTranslator);
|
2017-05-01 03:16:25 +02:00
|
|
|
} else if (appTranslator->load(fileName, QStringLiteral("."))) {
|
2015-04-22 18:57:44 +02:00
|
|
|
QCoreApplication::installTranslator(appTranslator);
|
2017-05-01 03:16:25 +02:00
|
|
|
} else if (appTranslator->load(fileName, QStringLiteral("./translations"))) {
|
2015-08-08 01:38:48 +02:00
|
|
|
QCoreApplication::installTranslator(appTranslator);
|
2017-05-01 03:16:25 +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);
|
2017-05-01 03:16:25 +02:00
|
|
|
} else if (appTranslator->load(fileName, QStringLiteral("../share/%1/translations").arg(applicationName))) {
|
2015-08-25 19:16:43 +02:00
|
|
|
QCoreApplication::installTranslator(appTranslator);
|
2017-05-01 03:16:25 +02:00
|
|
|
} else if (appTranslator->load(fileName, QStringLiteral(":/translations"))) {
|
2016-07-16 23:12:02 +02:00
|
|
|
QCoreApplication::installTranslator(appTranslator);
|
2015-04-22 18:57:44 +02:00
|
|
|
} else {
|
2015-07-14 18:33:44 +02:00
|
|
|
delete appTranslator;
|
2017-08-15 01:08:08 +02:00
|
|
|
if (localeName.startsWith(QLatin1String("en"))) {
|
|
|
|
// the translation file is probably just empty (English is built-in and usually only used for plural forms)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cerr << "Unable to load translation file for \"" << applicationName.toLocal8Bit().data() << "\" and the locale \""
|
2017-05-01 03:16:25 +02:00
|
|
|
<< localeName.toLocal8Bit().data() << "\"." << endl;
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:08:45 +01:00
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Loads and installs the appropriate application translation file for
|
|
|
|
* the current locale.
|
2017-03-06 22:08:45 +01:00
|
|
|
* \param applicationNames Specifies the names of the applications.
|
|
|
|
*/
|
2017-01-07 16:09:38 +01:00
|
|
|
void loadApplicationTranslationFile(const std::initializer_list<QString> &applicationNames)
|
|
|
|
{
|
2017-05-01 03:16:25 +02:00
|
|
|
for (const QString &applicationName : applicationNames) {
|
2017-01-07 16:09:38 +01:00
|
|
|
loadApplicationTranslationFile(applicationName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-06 22:08:45 +01:00
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Loads and installs the appropriate application translation file for
|
|
|
|
* the specified locale.
|
2017-03-06 22:08:45 +01:00
|
|
|
* \param applicationNames Specifies the names of the applications.
|
|
|
|
* \param localeName Specifies the name of the locale.
|
|
|
|
*/
|
2017-01-07 16:09:38 +01:00
|
|
|
void loadApplicationTranslationFile(const std::initializer_list<QString> &applicationNames, const QString &localeName)
|
|
|
|
{
|
2017-05-01 03:16:25 +02:00
|
|
|
for (const QString &applicationName : applicationNames) {
|
2017-01-07 16:09:38 +01:00
|
|
|
loadApplicationTranslationFile(applicationName, localeName);
|
|
|
|
}
|
|
|
|
}
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
|
|
|
|
2016-07-16 23:12:02 +02:00
|
|
|
/*!
|
2017-05-04 22:46:37 +02:00
|
|
|
* \brief Convenience functions to check whether a
|
|
|
|
* QCoreApplication/QGuiApplication/QApplication singleton has been instantiated
|
|
|
|
* yet.
|
2016-07-16 23:12:02 +02:00
|
|
|
*/
|
2015-04-22 18:57:44 +02:00
|
|
|
namespace ApplicationInstances {
|
|
|
|
|
2017-04-27 22:03:37 +02:00
|
|
|
#if defined(QT_UTILITIES_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
|
|
|
|
|
2017-04-27 22:03:37 +02:00
|
|
|
#if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_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
|
|
|
/*!
|
2017-05-04 22:46:37 +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.
|
2016-07-16 23:12:02 +02:00
|
|
|
*/
|
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
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QFile::exists(fileName)) {
|
2015-09-16 17:30:25 +02:00
|
|
|
return fileName;
|
|
|
|
} else {
|
|
|
|
// check whether the file is in the settings directory used by QSettings
|
|
|
|
QString path;
|
2017-05-01 03:16:25 +02:00
|
|
|
if (settings) {
|
2015-09-16 17:30:25 +02:00
|
|
|
path = QFileInfo(settings->fileName()).absoluteDir().absoluteFilePath(fileName);
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QFile::exists(path)) {
|
2015-09-16 17:30:25 +02:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
2017-05-01 03:16:25 +02:00
|
|
|
// 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;
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QFile::exists(path)) {
|
2015-09-16 17:30:25 +02:00
|
|
|
return path;
|
|
|
|
} else {
|
2017-05-04 22:46:37 +02:00
|
|
|
// check whether there is the default version of the file under
|
|
|
|
// /usr/share/app/
|
2015-09-16 17:30:25 +02:00
|
|
|
path = QStringLiteral("../share/") % applicationName % QChar('/') % fileName;
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QFile::exists(path)) {
|
2015-09-16 17:30:25 +02:00
|
|
|
return path;
|
|
|
|
} else {
|
|
|
|
return QString(); // file is not present
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
path = QStringLiteral("/etc/") % applicationName % QChar('/') % fileName;
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QFile::exists(path)) {
|
2015-09-16 17:30:25 +02:00
|
|
|
return path;
|
|
|
|
} else {
|
2017-05-04 22:46:37 +02:00
|
|
|
// check whether there is the default version of the file under
|
|
|
|
// /usr/share/app/
|
2015-09-16 17:30:25 +02:00
|
|
|
path = QStringLiteral("/usr/share/") % applicationName % QChar('/') % fileName;
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QFile::exists(path)) {
|
2015-09-16 17:30:25 +02:00
|
|
|
return path;
|
|
|
|
} else {
|
|
|
|
return QString(); // file is not present
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|