updated application startup

This commit is contained in:
Martchus 2015-09-01 20:08:43 +02:00
parent b96c01dc23
commit 4047cdd14f
4 changed files with 91 additions and 69 deletions

View File

@ -1,5 +1,7 @@
#include "qtconfigarguments.h"
#include <c++utilities/conversion/stringconversion.h>
#include <QString>
#include <QLocale>
#include <QFont>
@ -18,68 +20,32 @@ using namespace std;
namespace ApplicationUtilities {
/*!
* \brief Constructs new Qt config arguments.
*/
QtConfigArguments::QtConfigArguments() :
m_qtWidgetsGuiArg("qt-widgets-gui", "g", "shows a Qt widgets based graphical user interface"),
m_qtQuickGuiArg("qt-quick-gui", "q", "shows a Qt quick based graphical user interface"),
m_lngArg("lang", "l", "sets the language for the Qt GUI"),
m_qmlDebuggerArg("qmljsdebugger", "qmljsdebugger", "enables QML debugging (see http://doc.qt.io/qt-5/qtquick-debugging.html)"),
m_styleArg("style", string(), "sets the Qt widgets style"),
m_iconThemeArg("icon-theme", string(), "sets the icon theme for the Qt GUI"),
m_iconThemeArg("icon-theme", string(), "sets the icon theme and additional theme search paths for the Qt GUI"),
m_fontArg("font", string(), "sets the font family and size (point) for the Qt GUI")
{
// language
m_lngArg.setValueNames({"language"});
m_lngArg.setRequiredValueCount(1);
m_lngArg.setRequired(false);
m_lngArg.setCallback([] (const StringVector &values) {
QLocale::setDefault(QLocale(QString::fromLocal8Bit(values.front().c_str())));
});
// qml debugger (handled by Qt, just to let the parser know of it)
m_qmlDebuggerArg.setValueNames({"port:<port_from>[,port_to][,host:<ip address>][,block]"});
m_qmlDebuggerArg.setRequiredValueCount(1);
// appearance
m_styleArg.setValueNames({"style name"});
m_styleArg.setRequiredValueCount(1);
m_styleArg.setCallback([] (const StringVector &values) {
#ifdef GUI_QTWIDGETS
if(QStyle *style = QStyleFactory::create(QString::fromLocal8Bit(values.front().c_str()))) {
QApplication::setStyle(style);
} else {
cout << "Warning: Can not find the specified style." << endl;
}
#endif
#ifdef GUI_QTQUICK
Q_UNUSED(values)
cout << "Warning: Can not set a style for the Qt Quick GUI." << endl;
#endif
});
m_iconThemeArg.setValueNames({"theme name"});
m_iconThemeArg.setRequiredValueCount(1);
m_iconThemeArg.setCallback([] (const StringVector &values) {
QIcon::setThemeName(QString::fromLocal8Bit(values.front().c_str()));
});
m_iconThemeArg.setValueNames({"theme name", "search path 1", "search path 2"});
m_iconThemeArg.setRequiredValueCount(-1);
m_fontArg.setValueNames({"name", "size"});
m_fontArg.setRequiredValueCount(2);
m_fontArg.setDefault(true);
#ifdef Q_OS_WIN32
m_fontArg.setDefaultValues({"Segoe UI", "9"});
#else
m_fontArg.setDefaultValues({string(), string()});
#endif
m_fontArg.setCallback([] (const StringVector &values) {
if(!values.front().empty()) {
QFont font;
font.setFamily(QString::fromLocal8Bit(values.front().c_str()));
bool ok;
int size = QString::fromLocal8Bit(values.back().c_str()).toInt(&ok);
if(ok) {
font.setPointSize(size);
} else {
cout << "Warning: Can not parse specified font size. It will be ignored." << endl;
}
QGuiApplication::setFont(font);
}
});
m_qtWidgetsGuiArg.setSecondaryArguments({&m_lngArg, &m_qmlDebuggerArg, &m_styleArg, &m_iconThemeArg, &m_fontArg});
m_qtQuickGuiArg.setSecondaryArguments({&m_lngArg, &m_qmlDebuggerArg, &m_iconThemeArg, &m_fontArg});
m_qtWidgetsGuiArg.setDenotesOperation(true);
@ -91,4 +57,71 @@ QtConfigArguments::QtConfigArguments() :
#endif
}
/*!
* \brief Applies the settings from the arguments.
* \remarks Also checks environment variables for the icon theme.
*/
void QtConfigArguments::applySettings() const
{
if(m_lngArg.isPresent()) {
QLocale::setDefault(QLocale(QString::fromLocal8Bit(m_lngArg.values().front().data())));
}
if(m_styleArg.isPresent()) {
#ifdef GUI_QTWIDGETS
if(QStyle *style = QStyleFactory::create(QString::fromLocal8Bit(m_styleArg.values().front().data()))) {
QApplication::setStyle(style);
} else {
cout << "Warning: Can not find the specified style." << endl;
}
#endif
#ifdef GUI_QTQUICK
Q_UNUSED(values)
cout << "Warning: Can not set a style for the Qt Quick GUI." << endl;
#endif
}
if(m_iconThemeArg.isPresent()) {
auto i = m_iconThemeArg.values().cbegin(), end = m_iconThemeArg.values().end();
if(i != end) {
QIcon::setThemeName(QString::fromLocal8Bit(i->data()));
if(++i != end) {
QStringList searchPaths;
searchPaths.reserve(m_iconThemeArg.values().size() - 1);
for(; i != end; ++i) {
searchPaths << QString::fromLocal8Bit(i->data());
}
QIcon::setThemeSearchPaths(searchPaths);
} else {
#ifdef Q_OS_WIN32
QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << QStringLiteral("../share/icons"));
#endif
}
}
} else {
if(qEnvironmentVariableIsSet("ICON_THEME")) {
QString themeName;
themeName.append(qgetenv("ICON_THEME"));
QIcon::setThemeName(themeName);
}
if(qEnvironmentVariableIsSet("ICON_THEME_SEARCH_PATH")) {
QString path;
path.append(qgetenv("ICON_THEME_SEARCH_PATH"));
QIcon::setThemeSearchPaths(QStringList() << path);
}
}
if(m_fontArg.isPresent()) {
QFont font;
font.setFamily(QString::fromLocal8Bit(m_fontArg.values().front().data()));
try {
font.setPointSize(ConversionUtilities::stringToNumber<int>(m_fontArg.values().back()));
} catch(const ConversionUtilities::ConversionException &) {
cout << "Warning: The specified font size is no number and will be ignored." << endl;
}
QGuiApplication::setFont(font);
} else {
#ifdef Q_OS_WIN32
QGuiApplication::setFont(QFont(QStringLiteral("Segoe UI"), 9));
#endif
}
}
}

View File

@ -15,6 +15,7 @@ public:
Argument &languageArg();
bool areQtGuiArgsPresent() const;
void applySettings() const;
private:
Argument m_qtWidgetsGuiArg;
@ -26,21 +27,33 @@ private:
Argument m_fontArg;
};
/*!
* \brief Returns the argument for the Qt Widgets GUI.
*/
inline Argument &QtConfigArguments::qtWidgetsGuiArg()
{
return m_qtWidgetsGuiArg;
}
/*!
* \brief Returns the argument for the Qt Quick GUI.
*/
inline Argument &QtConfigArguments::qtQuickGuiArg()
{
return m_qtQuickGuiArg;
}
/*!
* \brief Returns the language argument.
*/
inline Argument &QtConfigArguments::languageArg()
{
return m_lngArg;
}
/*!
* \brief Returns whether at least one of the GUI arguments is present.
*/
inline bool QtConfigArguments::areQtGuiArgsPresent() const
{
return m_qtWidgetsGuiArg.isPresent() || m_qtQuickGuiArg.isPresent();

View File

@ -131,41 +131,25 @@ void loadApplicationTranslationFile(const QString &applicationName, const QStrin
}
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;
return qobject_cast<QApplication *>(QCoreApplication::instance()) != nullptr;
}
#endif
#if defined(GUI_QTWIDGETS) || defined(GUI_QTQUICK)
bool hasGuiApp()
{
return qobject_cast<QGuiApplication*>(QCoreApplication::instance()) != nullptr;
return qobject_cast<QGuiApplication *>(QCoreApplication::instance()) != nullptr;
}
#endif
bool hasCoreApp()
{
return qobject_cast<QCoreApplication*>(QCoreApplication::instance()) != nullptr;
return qobject_cast<QCoreApplication *>(QCoreApplication::instance()) != nullptr;
}
}

View File

@ -35,14 +35,6 @@ LIB_EXPORT void loadApplicationTranslationFile(const QString &applicationName, c
}
namespace Theme {
#if defined(GUI_QTWIDGETS) || defined(GUI_QTQUICK)
LIB_EXPORT void setup();
#endif
}
namespace ApplicationInstances {
#if defined(GUI_QTWIDGETS)