3#include "resources/config.h"
13#include <QStringBuilder>
15#if defined(QT_UTILITIES_GUI_QTWIDGETS)
16#include <QApplication>
17#elif defined(QT_UTILITIES_GUI_QTQUICK)
18#include <QGuiApplication>
20#include <QCoreApplication>
28inline void initResources()
30 Q_INIT_RESOURCE(qtutilsicons);
33inline void cleanupResources()
35 Q_CLEANUP_RESOURCE(qtutilsicons);
45namespace QtUtilitiesResources {
69namespace TranslationFiles {
74static QList<QTranslator *> translators;
122 const auto debugTranslations = qEnvironmentVariableIsSet(
"QT_DEBUG_TRANSLATIONS");
123 for (
const auto &repoName : repositoryNames) {
124 auto *
const qtTranslator =
new QTranslator(QCoreApplication::instance());
125 const auto fileName = QString(repoName % QChar(
'_') % localeName);
129 || qtTranslator->load(fileName,
131#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
132 QLibraryInfo::location(QLibraryInfo::TranslationsPath)
134 QLibraryInfo::path(QLibraryInfo::TranslationsPath)
137 || qtTranslator->load(fileName, path = QStringLiteral(
"../share/qt/translations"))
138 || qtTranslator->load(fileName, path = QStringLiteral(
":/translations"))) {
139 QCoreApplication::installTranslator(qtTranslator);
140 translators.append(qtTranslator);
141 if (debugTranslations) {
142 cerr <<
"Loading translation file for Qt repository \"" << repoName.toLocal8Bit().data() <<
"\" and the locale \""
143 << localeName.toLocal8Bit().data() <<
"\" from \"" << path.toLocal8Bit().data() <<
"\"." << endl;
147 if (localeName.startsWith(QLatin1String(
"en"))) {
151 cerr <<
"Unable to load translation file for Qt repository \"" << repoName.toLocal8Bit().data() <<
"\" and locale "
152 << localeName.toLocal8Bit().data() <<
"." << endl;
181 const auto defaultLocale(QLocale().name());
182 if (defaultLocale != QLatin1String(
"en_US")) {
188void logTranslationEvent(
189 const char *event,
const QString &configName,
const QString &applicationName,
const QString &localeName,
const QString &path = QString())
191 cerr <<
event <<
" translation file for \"" << applicationName.toLocal8Bit().data() <<
"\"";
192 if (!configName.isEmpty()) {
193 cerr <<
" (config \"" << configName.toLocal8Bit().data() <<
"\")";
195 cerr <<
" and locale \"" << localeName.toLocal8Bit().data() <<
'\"';
196 if (!path.isEmpty()) {
197 cerr <<
" from \"" << path.toLocal8Bit().data() <<
'\"';
225 auto *
const appTranslator =
new QTranslator(QCoreApplication::instance());
226 const auto fileName = QString(applicationName % QChar(
'_') % localeName);
227 const auto directoryName = configName.isEmpty() ? applicationName : QString(applicationName % QChar(
'-') % configName);
231 || appTranslator->load(fileName, path = QStringLiteral(
".")) || appTranslator->load(fileName, path = QStringLiteral(
"../") % directoryName)
232 || appTranslator->load(fileName, path = QStringLiteral(
"../../") % directoryName)
233 || appTranslator->load(fileName, path = QStringLiteral(
"./translations"))
234 || appTranslator->load(fileName, path = QStringLiteral(
"../share/") % directoryName % QStringLiteral(
"/translations"))
235 || appTranslator->load(fileName, path = QStringLiteral(APP_INSTALL_PREFIX
"/share/") % directoryName % QStringLiteral(
"/translations"))
236 || appTranslator->load(fileName, path = QStringLiteral(
":/translations"))) {
237 QCoreApplication::installTranslator(appTranslator);
238 translators.append(appTranslator);
239 if (qEnvironmentVariableIsSet(
"QT_DEBUG_TRANSLATIONS")) {
240 logTranslationEvent(
"Loading", configName, applicationName, localeName, path);
243 delete appTranslator;
244 if (localeName.startsWith(QLatin1String(
"en"))) {
248 logTranslationEvent(
"Unable to load", configName, applicationName, localeName);
259 for (
const QString &applicationName : applicationNames) {
272 for (
const QString &applicationName : applicationNames) {
282 for (
auto *
const translator : translators) {
283 QCoreApplication::removeTranslator(translator);
295namespace ApplicationInstances {
297#if defined(QT_UTILITIES_GUI_QTWIDGETS)
303 return qobject_cast<QApplication *>(QCoreApplication::instance()) !=
nullptr;
307#if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
313 return qobject_cast<QGuiApplication *>(QCoreApplication::instance()) !=
nullptr;
322 return qobject_cast<QCoreApplication *>(QCoreApplication::instance()) !=
nullptr;
338#if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 5, 0))
339 if (
const auto qtVersion = QLibraryInfo::version();
340 qtVersion >= QVersionNumber(6, 4, 0) && qtVersion < QVersionNumber(6, 5, 0) && !qEnvironmentVariableIsSet(
"QT_QPA_PLATFORM")) {
341 qputenv(
"QT_QPA_PLATFORM",
"windows:darkmode=1");
346#ifdef QT_FEATURE_fontdialog
347 if (!qEnvironmentVariableIsSet(
"FONTCONFIG_PATH") && QDir(QStringLiteral(
"/etc/fonts")).exists()) {
348 qputenv(
"FONTCONFIG_PATH",
"/etc/fonts");
353#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
354 if (!QCoreApplication::instance()) {
355 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling,
true);
357 QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps,
true);
372std::unique_ptr<QSettings>
getSettings(
const QString &organization,
const QString &application)
374 auto settings = std::unique_ptr<QSettings>();
375 if (
const auto portableFile
376 = QFile(application.isEmpty() ? organization + QStringLiteral(
".ini") : organization % QChar(
'/') % application % QStringLiteral(
".ini"));
377 portableFile.exists()) {
378 settings = std::make_unique<QSettings>(portableFile.fileName(), QSettings::IniFormat);
380 settings = std::make_unique<QSettings>(QSettings::IniFormat, QSettings::UserScope, organization, application);
382 if (organization != QCoreApplication::organizationName() || application != QCoreApplication::applicationName()) {
384 = QSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName())
386 QFile::rename(oldConfig, settings->fileName()) || QFile::remove(oldConfig);
398 auto errorMessage = QString();
399 switch (settings.status()) {
400 case QSettings::NoError:
402 case QSettings::AccessError:
403 errorMessage = QCoreApplication::translate(
"QtUtilities",
"unable to access file");
405 case QSettings::FormatError:
406 errorMessage = QCoreApplication::translate(
"QtUtilities",
"file has invalid format");
409 errorMessage = QCoreApplication::translate(
"QtUtilities",
"unknown error");
411 return QCoreApplication::translate(
"QtUtilities",
"Unable to sync settings from \"%1\": %2").arg(settings.fileName(), errorMessage);
QT_UTILITIES_EXPORT bool hasCoreApp()
Returns whether a QCoreApplication has been instantiated yet.
QT_UTILITIES_EXPORT void init()
Initiates the resources used and provided by this library.
QT_UTILITIES_EXPORT void cleanup()
Frees the resources used and provided by this library.
QT_UTILITIES_EXPORT void loadQtTranslationFile(std::initializer_list< QString > repositoryNames)
Loads and installs the appropriate Qt translation file for the current locale.
QT_UTILITIES_EXPORT void clearTranslationFiles()
Clears all translation files previously loaded via the load-functions in this namespace.
QT_UTILITIES_EXPORT void loadApplicationTranslationFile(const QString &configName, const QString &applicationName)
Loads and installs the appropriate application translation file for the current locale.
QT_UTILITIES_EXPORT QString & additionalTranslationFilePath()
Allows to set an additional search path for translation files.
QT_UTILITIES_EXPORT std::unique_ptr< QSettings > getSettings(const QString &organization, const QString &application=QString())
Returns the settings object for the specified organization and application.
QT_UTILITIES_EXPORT void setupCommonQtApplicationAttributes()
Sets Qt application attributes which are commonly used within my Qt applications.
QT_UTILITIES_EXPORT QString errorMessageForSettings(const QSettings &settings)
Returns an error message for the specified settings or an empty string if there's no error.