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>
|
2018-06-05 22:49:08 +02:00
|
|
|
#include <QFont>
|
|
|
|
#include <QIcon>
|
2017-05-01 03:16:25 +02:00
|
|
|
#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>
|
2017-04-27 22:03:37 +02:00
|
|
|
#elif defined(QT_UTILITIES_GUI_QTQUICK)
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QGuiApplication>
|
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
|
|
|
|
2019-06-10 21:57:46 +02:00
|
|
|
namespace QtUtilities {
|
|
|
|
|
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
|
|
|
}
|
2017-09-17 21:45:49 +02:00
|
|
|
} // namespace QtUtilitiesResources
|
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 {
|
|
|
|
|
2023-07-01 23:43:05 +02:00
|
|
|
/*!
|
|
|
|
* \brief The translators installed via the load-functions in this namespace.
|
|
|
|
*/
|
|
|
|
static QList<QTranslator *> translators;
|
|
|
|
|
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
|
|
|
{
|
2019-06-16 16:56:38 +02:00
|
|
|
const auto debugTranslations = qEnvironmentVariableIsSet("QT_DEBUG_TRANSLATIONS");
|
|
|
|
for (const auto &repoName : repositoryNames) {
|
2021-03-23 16:46:55 +01:00
|
|
|
auto *const qtTranslator = new QTranslator(QCoreApplication::instance());
|
2019-06-17 00:43:44 +02:00
|
|
|
const auto fileName = QString(repoName % QChar('_') % localeName);
|
2019-06-16 16:56:38 +02:00
|
|
|
|
|
|
|
QString path;
|
|
|
|
if ((!additionalTranslationFilePath().isEmpty() && qtTranslator->load(fileName, path = additionalTranslationFilePath()))
|
2020-10-05 18:37:42 +02:00
|
|
|
|| qtTranslator->load(fileName,
|
|
|
|
path =
|
2020-09-18 18:28:35 +02:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
2020-10-05 18:37:42 +02:00
|
|
|
QLibraryInfo::location(QLibraryInfo::TranslationsPath)
|
2020-09-18 18:28:35 +02:00
|
|
|
#else
|
2020-10-05 18:37:42 +02:00
|
|
|
QLibraryInfo::path(QLibraryInfo::TranslationsPath)
|
2020-09-18 18:28:35 +02:00
|
|
|
#endif
|
2020-10-05 18:37:42 +02:00
|
|
|
)
|
2019-06-16 16:56:38 +02:00
|
|
|
|| qtTranslator->load(fileName, path = QStringLiteral("../share/qt/translations"))
|
|
|
|
|| qtTranslator->load(fileName, path = QStringLiteral(":/translations"))) {
|
2016-07-27 21:42:27 +02:00
|
|
|
QCoreApplication::installTranslator(qtTranslator);
|
2023-07-01 23:43:05 +02:00
|
|
|
translators.append(qtTranslator);
|
2019-06-16 16:56:38 +02:00
|
|
|
if (debugTranslations) {
|
|
|
|
cerr << "Loading translation file for Qt repository \"" << repoName.toLocal8Bit().data() << "\" and the locale \""
|
|
|
|
<< localeName.toLocal8Bit().data() << "\" from \"" << path.toLocal8Bit().data() << "\"." << endl;
|
|
|
|
}
|
2016-07-27 21:42:27 +02:00
|
|
|
} 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:
|
2019-06-16 16:56:38 +02:00
|
|
|
* * ./
|
|
|
|
* * ../$application
|
|
|
|
* * ../../$application
|
2016-07-16 23:12:02 +02:00
|
|
|
* * ./translations
|
2019-06-16 16:56:38 +02:00
|
|
|
* * ../share/$application/translations
|
|
|
|
* * $install_prefix/share/$application/translations
|
2016-07-16 23:12:02 +02:00
|
|
|
* - 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
|
|
|
*/
|
2019-08-07 22:57:17 +02:00
|
|
|
void loadApplicationTranslationFile(const QString &configName, const QString &applicationName)
|
2015-04-22 18:57:44 +02:00
|
|
|
{
|
2015-08-08 01:38:48 +02:00
|
|
|
// load English translation files as fallback
|
2019-08-07 22:57:17 +02:00
|
|
|
loadApplicationTranslationFile(configName, applicationName, QStringLiteral("en_US"));
|
2015-08-08 01:38:48 +02:00
|
|
|
// load translation files for current locale
|
2019-04-26 17:37:20 +02:00
|
|
|
const auto defaultLocale(QLocale().name());
|
|
|
|
if (defaultLocale != QLatin1String("en_US")) {
|
2019-08-07 22:57:17 +02:00
|
|
|
loadApplicationTranslationFile(configName, applicationName, defaultLocale);
|
2016-08-14 22:44:25 +02:00
|
|
|
}
|
2015-06-21 21:45:58 +02:00
|
|
|
}
|
|
|
|
|
2019-08-07 22:57:17 +02:00
|
|
|
/// \cond
|
|
|
|
void logTranslationEvent(
|
|
|
|
const char *event, const QString &configName, const QString &applicationName, const QString &localeName, const QString &path = QString())
|
|
|
|
{
|
|
|
|
cerr << event << " translation file for \"" << applicationName.toLocal8Bit().data() << "\"";
|
|
|
|
if (!configName.isEmpty()) {
|
2019-08-10 00:10:16 +02:00
|
|
|
cerr << " (config \"" << configName.toLocal8Bit().data() << "\")";
|
2019-08-07 22:57:17 +02:00
|
|
|
}
|
|
|
|
cerr << " and locale \"" << localeName.toLocal8Bit().data() << '\"';
|
|
|
|
if (!path.isEmpty()) {
|
|
|
|
cerr << " from \"" << path.toLocal8Bit().data() << '\"';
|
|
|
|
}
|
|
|
|
cerr << '.' << endl;
|
|
|
|
}
|
|
|
|
/// \endcond
|
|
|
|
|
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:
|
2019-06-16 16:56:38 +02:00
|
|
|
* * ./
|
|
|
|
* * ../$application
|
|
|
|
* * ../../$application
|
2016-07-16 23:12:02 +02:00
|
|
|
* * ./translations
|
2019-06-16 16:56:38 +02:00
|
|
|
* * ../share/$application/translations
|
|
|
|
* * $install_prefix/share/$application/translations
|
2016-07-16 23:12:02 +02:00
|
|
|
* - 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
|
|
|
*/
|
2019-08-07 22:57:17 +02:00
|
|
|
void loadApplicationTranslationFile(const QString &configName, const QString &applicationName, const QString &localeName)
|
2015-06-21 21:45:58 +02:00
|
|
|
{
|
2021-03-23 16:46:55 +01:00
|
|
|
auto *const appTranslator = new QTranslator(QCoreApplication::instance());
|
2019-06-17 00:43:44 +02:00
|
|
|
const auto fileName = QString(applicationName % QChar('_') % localeName);
|
2019-08-07 22:57:17 +02:00
|
|
|
const auto directoryName = configName.isEmpty() ? applicationName : QString(applicationName % QChar('-') % configName);
|
2019-06-16 16:56:38 +02:00
|
|
|
|
|
|
|
QString path;
|
|
|
|
if ((!additionalTranslationFilePath().isEmpty() && appTranslator->load(fileName, path = additionalTranslationFilePath()))
|
2019-08-07 22:57:17 +02:00
|
|
|
|| appTranslator->load(fileName, path = QStringLiteral(".")) || appTranslator->load(fileName, path = QStringLiteral("../") % directoryName)
|
|
|
|
|| appTranslator->load(fileName, path = QStringLiteral("../../") % directoryName)
|
2019-06-16 16:56:38 +02:00
|
|
|
|| appTranslator->load(fileName, path = QStringLiteral("./translations"))
|
2019-08-07 22:57:17 +02:00
|
|
|
|| appTranslator->load(fileName, path = QStringLiteral("../share/") % directoryName % QStringLiteral("/translations"))
|
|
|
|
|| appTranslator->load(fileName, path = QStringLiteral(APP_INSTALL_PREFIX "/share/") % directoryName % QStringLiteral("/translations"))
|
2019-06-16 16:56:38 +02:00
|
|
|
|| appTranslator->load(fileName, path = QStringLiteral(":/translations"))) {
|
2016-07-16 23:12:02 +02:00
|
|
|
QCoreApplication::installTranslator(appTranslator);
|
2023-07-01 23:43:05 +02:00
|
|
|
translators.append(appTranslator);
|
2019-06-16 16:56:38 +02:00
|
|
|
if (qEnvironmentVariableIsSet("QT_DEBUG_TRANSLATIONS")) {
|
2019-08-07 22:57:17 +02:00
|
|
|
logTranslationEvent("Loading", configName, applicationName, localeName, path);
|
2019-06-16 16:56:38 +02:00
|
|
|
}
|
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;
|
|
|
|
}
|
2019-08-07 22:57:17 +02:00
|
|
|
logTranslationEvent("Unable to load", configName, applicationName, localeName);
|
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.
|
|
|
|
*/
|
2019-08-07 22:57:17 +02:00
|
|
|
void loadApplicationTranslationFile(const QString &configName, const std::initializer_list<QString> &applicationNames)
|
2017-01-07 16:09:38 +01:00
|
|
|
{
|
2017-05-01 03:16:25 +02:00
|
|
|
for (const QString &applicationName : applicationNames) {
|
2019-08-07 22:57:17 +02:00
|
|
|
loadApplicationTranslationFile(configName, applicationName);
|
2017-01-07 16:09:38 +01: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 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.
|
|
|
|
*/
|
2019-08-07 22:57:17 +02:00
|
|
|
void loadApplicationTranslationFile(const QString &configName, const std::initializer_list<QString> &applicationNames, const QString &localeName)
|
2017-01-07 16:09:38 +01:00
|
|
|
{
|
2017-05-01 03:16:25 +02:00
|
|
|
for (const QString &applicationName : applicationNames) {
|
2019-08-07 22:57:17 +02:00
|
|
|
loadApplicationTranslationFile(configName, applicationName, localeName);
|
2017-01-07 16:09:38 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-01 23:43:05 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Clears all translation files previously loaded via the load-functions in this namespace.
|
|
|
|
*/
|
|
|
|
void clearTranslationFiles()
|
|
|
|
{
|
|
|
|
for (auto *const translator : translators) {
|
|
|
|
QCoreApplication::removeTranslator(translator);
|
|
|
|
}
|
|
|
|
translators.clear();
|
|
|
|
}
|
|
|
|
|
2017-09-17 21:45:49 +02:00
|
|
|
} // namespace TranslationFiles
|
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
|
|
|
}
|
2018-03-24 17:03:46 +01:00
|
|
|
} // namespace ApplicationInstances
|
2015-09-16 17:30:25 +02:00
|
|
|
|
2020-07-12 21:15:48 +02:00
|
|
|
/*!
|
|
|
|
* \brief Sets Qt application attributes which are commonly used within my Qt applications.
|
|
|
|
* \remarks
|
|
|
|
* - So far this enables High-DPI support.
|
|
|
|
* - The exact attributes are unspecified and might change to whatever makes sense in the future.
|
|
|
|
*/
|
|
|
|
void setupCommonQtApplicationAttributes()
|
|
|
|
{
|
2023-03-13 20:34:08 +01:00
|
|
|
// enable dark window frame on Windows if the configured color palette is dark
|
|
|
|
// - supported as of Qt 6.4; no longer required as of Qt 6.5
|
|
|
|
// - see https://bugreports.qt.io/browse/QTBUG-72028?focusedCommentId=677819#comment-677819
|
|
|
|
// and https://www.qt.io/blog/dark-mode-on-windows-11-with-qt-6.5
|
|
|
|
#if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 5, 0))
|
|
|
|
if (const auto qtVersion = QLibraryInfo::version();
|
2023-03-25 18:50:08 +01:00
|
|
|
qtVersion >= QVersionNumber(6, 4, 0) && qtVersion < QVersionNumber(6, 5, 0) && !qEnvironmentVariableIsSet("QT_QPA_PLATFORM")) {
|
2022-09-05 23:22:13 +02:00
|
|
|
qputenv("QT_QPA_PLATFORM", "windows:darkmode=1");
|
|
|
|
}
|
|
|
|
#endif
|
2022-09-05 23:25:04 +02:00
|
|
|
|
|
|
|
// ensure FONTCONFIG_PATH is set (mainly required for static GNU/Linux builds)
|
2022-05-01 22:35:24 +02:00
|
|
|
#ifdef QT_FEATURE_fontdialog
|
|
|
|
if (!qEnvironmentVariableIsSet("FONTCONFIG_PATH") && QDir(QStringLiteral("/etc/fonts")).exists()) {
|
|
|
|
qputenv("FONTCONFIG_PATH", "/etc/fonts");
|
|
|
|
}
|
|
|
|
#endif
|
2022-09-05 23:25:04 +02:00
|
|
|
|
|
|
|
// enable settings for High-DPI scaling
|
2020-09-15 18:10:15 +02:00
|
|
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
2021-11-04 00:10:13 +01:00
|
|
|
if (!QCoreApplication::instance()) {
|
|
|
|
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling, true);
|
|
|
|
}
|
2020-07-12 21:15:48 +02:00
|
|
|
QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps, true);
|
2020-09-15 18:10:15 +02:00
|
|
|
#endif
|
2020-07-12 21:15:48 +02:00
|
|
|
}
|
|
|
|
|
2022-08-20 16:16:40 +02:00
|
|
|
/*!
|
|
|
|
* \brief Returns the settings object for the specified \a organization and \a application.
|
|
|
|
* \remarks
|
|
|
|
* - This function always uses INI as that's what I'd like to use in all of my applications consistently, regardless of the platform.
|
|
|
|
* - The parameter \a application might be empty. In fact, most of my applications use just `getSettings(QStringLiteral(PROJECT_NAME))`.
|
|
|
|
* - This function first checks whether a file called `$organization/$application.ini` exists in the current working directory (or just
|
|
|
|
* `$organization.ini` if \a application is empty) and uses that if it exists. That allows having a portable installation.
|
|
|
|
* - Some of my apps where using values from QCoreApplication for \a organization and \a application in the beginning. This function
|
|
|
|
* moves those old config files to their new location if needed. This extra handling will likely removed at some point. Note that
|
|
|
|
* I moved away from using values from QCoreApplication to avoid having spaces and additional config suffixes in the file name.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<QSettings> getSettings(const QString &organization, const QString &application)
|
|
|
|
{
|
|
|
|
auto settings = std::unique_ptr<QSettings>();
|
|
|
|
if (const auto portableFile
|
|
|
|
= QFile(application.isEmpty() ? organization + QStringLiteral(".ini") : organization % QChar('/') % application % QStringLiteral(".ini"));
|
|
|
|
portableFile.exists()) {
|
|
|
|
settings = std::make_unique<QSettings>(portableFile.fileName(), QSettings::IniFormat);
|
|
|
|
} else {
|
|
|
|
settings = std::make_unique<QSettings>(QSettings::IniFormat, QSettings::UserScope, organization, application);
|
|
|
|
// move config created by older versions to new location
|
|
|
|
if (organization != QCoreApplication::organizationName() || application != QCoreApplication::applicationName()) {
|
|
|
|
const auto oldConfig
|
|
|
|
= QSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName())
|
|
|
|
.fileName();
|
|
|
|
QFile::rename(oldConfig, settings->fileName()) || QFile::remove(oldConfig);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
settings->sync();
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
|
2023-02-17 19:17:17 +01:00
|
|
|
/*!
|
|
|
|
* \brief Returns an error message for the specified \a settings or an empty string if there's no error.
|
|
|
|
*/
|
|
|
|
QString errorMessageForSettings(const QSettings &settings)
|
|
|
|
{
|
|
|
|
auto errorMessage = QString();
|
|
|
|
switch (settings.status()) {
|
|
|
|
case QSettings::NoError:
|
|
|
|
return QString();
|
|
|
|
case QSettings::AccessError:
|
|
|
|
errorMessage = QCoreApplication::translate("QtUtilities", "unable to access file");
|
|
|
|
break;
|
|
|
|
case QSettings::FormatError:
|
|
|
|
errorMessage = QCoreApplication::translate("QtUtilities", "file has invalid format");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errorMessage = QCoreApplication::translate("QtUtilities", "unknown error");
|
|
|
|
}
|
|
|
|
return QCoreApplication::translate("QtUtilities", "Unable to sync settings from \"%1\": %2").arg(settings.fileName(), errorMessage);
|
|
|
|
}
|
|
|
|
|
2020-07-12 21:15:48 +02:00
|
|
|
// namespace ApplicationInstances
|
|
|
|
|
2019-06-10 21:57:46 +02:00
|
|
|
} // namespace QtUtilities
|