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 {
117 const auto debugTranslations = qEnvironmentVariableIsSet(
"QT_DEBUG_TRANSLATIONS");
118 for (
const auto &repoName : repositoryNames) {
119 auto *
const qtTranslator =
new QTranslator(QCoreApplication::instance());
120 const auto fileName = QString(repoName % QChar(
'_') % localeName);
124 || qtTranslator->load(fileName,
126#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
127 QLibraryInfo::location(QLibraryInfo::TranslationsPath)
129 QLibraryInfo::path(QLibraryInfo::TranslationsPath)
132 || qtTranslator->load(fileName, path = QStringLiteral(
"../share/qt/translations"))
133 || qtTranslator->load(fileName, path = QStringLiteral(
":/translations"))) {
134 QCoreApplication::installTranslator(qtTranslator);
135 if (debugTranslations) {
136 cerr <<
"Loading translation file for Qt repository \"" << repoName.toLocal8Bit().data() <<
"\" and the locale \""
137 << localeName.toLocal8Bit().data() <<
"\" from \"" << path.toLocal8Bit().data() <<
"\"." << endl;
141 if (localeName.startsWith(QLatin1String(
"en"))) {
145 cerr <<
"Unable to load translation file for Qt repository \"" << repoName.toLocal8Bit().data() <<
"\" and locale "
146 << localeName.toLocal8Bit().data() <<
"." << endl;
175 const auto defaultLocale(QLocale().name());
176 if (defaultLocale != QLatin1String(
"en_US")) {
182void logTranslationEvent(
183 const char *event,
const QString &configName,
const QString &applicationName,
const QString &localeName,
const QString &path = QString())
185 cerr <<
event <<
" translation file for \"" << applicationName.toLocal8Bit().data() <<
"\"";
186 if (!configName.isEmpty()) {
187 cerr <<
" (config \"" << configName.toLocal8Bit().data() <<
"\")";
189 cerr <<
" and locale \"" << localeName.toLocal8Bit().data() <<
'\"';
190 if (!path.isEmpty()) {
191 cerr <<
" from \"" << path.toLocal8Bit().data() <<
'\"';
219 auto *
const appTranslator =
new QTranslator(QCoreApplication::instance());
220 const auto fileName = QString(applicationName % QChar(
'_') % localeName);
221 const auto directoryName = configName.isEmpty() ? applicationName : QString(applicationName % QChar(
'-') % configName);
225 || appTranslator->load(fileName, path = QStringLiteral(
".")) || appTranslator->load(fileName, path = QStringLiteral(
"../") % directoryName)
226 || appTranslator->load(fileName, path = QStringLiteral(
"../") % directoryName)
227 || appTranslator->load(fileName, path = QStringLiteral(
"../../") % directoryName)
228 || appTranslator->load(fileName, path = QStringLiteral(
"./translations"))
229 || appTranslator->load(fileName, path = QStringLiteral(
"../share/") % directoryName % QStringLiteral(
"/translations"))
230 || appTranslator->load(fileName, path = QStringLiteral(APP_INSTALL_PREFIX
"/share/") % directoryName % QStringLiteral(
"/translations"))
231 || appTranslator->load(fileName, path = QStringLiteral(
":/translations"))) {
232 QCoreApplication::installTranslator(appTranslator);
233 if (qEnvironmentVariableIsSet(
"QT_DEBUG_TRANSLATIONS")) {
234 logTranslationEvent(
"Loading", configName, applicationName, localeName, path);
237 delete appTranslator;
238 if (localeName.startsWith(QLatin1String(
"en"))) {
242 logTranslationEvent(
"Unable to load", configName, applicationName, localeName);
253 for (
const QString &applicationName : applicationNames) {
266 for (
const QString &applicationName : applicationNames) {
277namespace ApplicationInstances {
279#if defined(QT_UTILITIES_GUI_QTWIDGETS)
285 return qobject_cast<QApplication *>(QCoreApplication::instance()) !=
nullptr;
289#if defined(QT_UTILITIES_GUI_QTWIDGETS) || defined(QT_UTILITIES_GUI_QTQUICK)
295 return qobject_cast<QGuiApplication *>(QCoreApplication::instance()) !=
nullptr;
304 return qobject_cast<QCoreApplication *>(QCoreApplication::instance()) !=
nullptr;
320#if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)) && (QT_VERSION < QT_VERSION_CHECK(6, 5, 0))
321 if (
const auto qtVersion = QLibraryInfo::version();
322 qtVersion >= QVersionNumber(6, 4, 0) && qtVersion < QVersionNumber(6, 5, 0) && !qEnvironmentVariableIsSet(
"QT_QPA_PLATFORM")) {
323 qputenv(
"QT_QPA_PLATFORM",
"windows:darkmode=1");
328#ifdef QT_FEATURE_fontdialog
329 if (!qEnvironmentVariableIsSet(
"FONTCONFIG_PATH") && QDir(QStringLiteral(
"/etc/fonts")).exists()) {
330 qputenv(
"FONTCONFIG_PATH",
"/etc/fonts");
335#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
336 if (!QCoreApplication::instance()) {
337 QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling,
true);
339 QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps,
true);
354std::unique_ptr<QSettings>
getSettings(
const QString &organization,
const QString &application)
356 auto settings = std::unique_ptr<QSettings>();
357 if (
const auto portableFile
358 = QFile(application.isEmpty() ? organization + QStringLiteral(
".ini") : organization % QChar(
'/') % application % QStringLiteral(
".ini"));
359 portableFile.exists()) {
360 settings = std::make_unique<QSettings>(portableFile.fileName(), QSettings::IniFormat);
362 settings = std::make_unique<QSettings>(QSettings::IniFormat, QSettings::UserScope, organization, application);
364 if (organization != QCoreApplication::organizationName() || application != QCoreApplication::applicationName()) {
366 = QSettings(QSettings::IniFormat, QSettings::UserScope, QCoreApplication::organizationName(), QCoreApplication::applicationName())
368 QFile::rename(oldConfig, settings->fileName()) || QFile::remove(oldConfig);
380 auto errorMessage = QString();
381 switch (settings.status()) {
382 case QSettings::NoError:
384 case QSettings::AccessError:
385 errorMessage = QCoreApplication::translate(
"QtUtilities",
"unable to access file");
387 case QSettings::FormatError:
388 errorMessage = QCoreApplication::translate(
"QtUtilities",
"file has invalid format");
391 errorMessage = QCoreApplication::translate(
"QtUtilities",
"unknown error");
393 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 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.