qtutilities/resources/resources.cpp

172 lines
5.5 KiB
C++
Raw Normal View History

2015-04-22 18:57:44 +02:00
#include "resources.h"
#include <QString>
#include <QLocale>
#include <QTranslator>
#include <QLibraryInfo>
#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;
void qInitResources_qtutilsicons();
void qCleanupResources_qtutilsicons();
namespace QtUtilitiesResources {
/*!
* \brief Initiates the resources used and provided by this library.
*/
void init()
{
qInitResources_qtutilsicons();
}
/*!
* \brief Frees the resources used and provided by this library.
*/
void cleanup()
{
qCleanupResources_qtutilsicons();
}
}
namespace TranslationFiles {
bool hasEnglishTranslator = false;
2015-04-22 18:57:44 +02:00
/*!
2015-06-21 21:45:58 +02:00
* \brief Loads and installs the appropriate Qt translation file for the current locale.
2015-04-22 18:57:44 +02:00
*/
void loadQtTranslationFile()
{
QLocale locale;
if(locale.language() != QLocale::English) {
QTranslator *qtTranslator = new QTranslator;
if(qtTranslator->load(QStringLiteral("qt_%1").arg(locale.name()),
QLibraryInfo::location(QLibraryInfo::TranslationsPath))) {
QCoreApplication::installTranslator(qtTranslator);
2015-08-25 19:16:43 +02:00
} else if(qtTranslator->load(QStringLiteral("qt_%1").arg(locale.name()), QStringLiteral("../share/qt/translations"))) {
// used in Windows
QCoreApplication::installTranslator(qtTranslator);
2015-04-22 18:57:44 +02:00
} else {
2015-07-14 18:33:44 +02:00
delete qtTranslator;
2015-04-22 18:57:44 +02:00
cout << "Unable to load Qt translation file for the language " << locale.name().toStdString() << "." << endl;
}
}
}
/*!
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.
* \remarks Translation files have to be placed in one of the following
* locations:
* - ./translations
2015-08-25 19:16:43 +02:00
* - /usr/share/$application/translations (used in UNIX)
* - ../share/$application/translations (used in Windows)
* - ./projects/%1/translations (used during developement with subdirs project)
2015-04-22 18:57:44 +02:00
* Translation files must be named using the following scheme:
* - $application_$language.qm
*/
void loadApplicationTranslationFile(const QString &applicationName)
{
// load English translation files as fallback
loadApplicationTranslationFile(applicationName, QStringLiteral("en_US"));
hasEnglishTranslator = true;
// 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.
* \param localName Specifies the name of the locale.
* \remarks Translation files have to be placed in one of the following
* locations:
* - ./translations
2015-08-25 19:16:43 +02:00
* - /usr/share/$application/translations (used in UNIX)
* - ../share/$application/translations (used in Windows)
* - ./projects/%1/translations (used during developement with subdirs project)
2015-06-21 21:45:58 +02:00
* Translation files must be named using the following scheme:
* - $application_$language.qm
*/
void loadApplicationTranslationFile(const QString &applicationName, const QString &localeName)
{
2015-04-22 18:57:44 +02:00
QTranslator *appTranslator = new QTranslator;
2015-06-21 21:45:58 +02:00
QString fileName = QStringLiteral("%1_%2").arg(applicationName, localeName);
2015-04-22 18:57:44 +02:00
if(appTranslator->load(fileName, QStringLiteral("./translations"))) {
QCoreApplication::installTranslator(appTranslator);
} else if(appTranslator->load(fileName, QStringLiteral("./projects/%1/translations").arg(applicationName))) {
QCoreApplication::installTranslator(appTranslator);
2015-04-22 18:57:44 +02:00
} else if(appTranslator->load(fileName, QStringLiteral("/usr/share/%1/translations").arg(applicationName))) {
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);
2015-04-22 18:57:44 +02:00
} else {
2015-07-14 18:33:44 +02:00
delete appTranslator;
2015-06-21 21:45:58 +02:00
if(localeName != QStringLiteral("en_US")) {
cout << "Unable to load application translation file for the language \"" << localeName.toStdString() << "\", falling back to language \"en_US\"." << endl;
if(!hasEnglishTranslator) {
loadApplicationTranslationFile(applicationName, QStringLiteral("en_US"));
hasEnglishTranslator = true;
}
2015-06-21 21:45:58 +02:00
} else {
cout << "Unable to load application translation file for the language \"" << localeName.toStdString() << "\"." << endl;
}
2015-04-22 18:57:44 +02:00
}
}
}
namespace Theme {
/*!
* \brief Sets the default icon theme.
*/
#if defined(GUI_QTWIDGETS) || defined(GUI_QTQUICK)
void setup()
{
if(QIcon::themeName().isEmpty()) {
QIcon::setThemeName(QStringLiteral("oxygen"));
}
}
#endif
}
namespace ApplicationInstances {
#if defined(GUI_QTWIDGETS)
bool hasWidgetsApp()
{
return qobject_cast<QApplication*>(QCoreApplication::instance()) != nullptr;
}
#endif
#if defined(GUI_QTWIDGETS) || defined(GUI_QTQUICK)
bool hasGuiApp()
{
return qobject_cast<QGuiApplication*>(QCoreApplication::instance()) != nullptr;
}
#endif
bool hasCoreApp()
{
return qobject_cast<QCoreApplication*>(QCoreApplication::instance()) != nullptr;
}
}