qtutilities/resources/qtconfigarguments.cpp

160 lines
5.8 KiB
C++
Raw Normal View History

2015-09-06 20:19:21 +02:00
#include "./qtconfigarguments.h"
2015-04-22 18:57:44 +02:00
2015-09-01 20:08:43 +02:00
#include <c++utilities/conversion/stringconversion.h>
2015-04-22 18:57:44 +02:00
#include <QString>
#include <QLocale>
#include <QFont>
#include <QIcon>
#ifdef GUI_QTWIDGETS
# include <QApplication>
# include <QStyleFactory>
#else
# include <QGuiApplication>
#endif
#include <initializer_list>
#include <iostream>
using namespace std;
using namespace ConversionUtilities;
2015-04-22 18:57:44 +02:00
namespace ApplicationUtilities {
2015-09-01 20:08:43 +02:00
/*!
* \brief Constructs new Qt config arguments.
*/
2015-04-22 18:57:44 +02:00
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)"),
2016-05-26 02:27:12 +02:00
m_styleArg("style", nullptr, "sets the Qt widgets style"),
m_iconThemeArg("icon-theme", nullptr, "sets the icon theme and additional theme search paths for the Qt GUI"),
m_fontArg("font", nullptr, "sets the font family and size (point) for the Qt GUI"),
m_libraryPathsArg("library-paths", nullptr, "sets the list of directories to search when loading libraries (all existing paths will be deleted)")
2015-04-22 18:57:44 +02:00
{
// language
m_lngArg.setValueNames({"language"});
m_lngArg.setRequiredValueCount(1);
m_lngArg.setRequired(false);
2015-11-25 22:48:24 +01:00
m_lngArg.setCombinable(true);
2015-04-22 18:57:44 +02:00
// 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);
2015-11-25 22:48:24 +01:00
m_qmlDebuggerArg.setCombinable(true);
2015-04-22 18:57:44 +02:00
// appearance
m_styleArg.setValueNames({"style name"});
m_styleArg.setRequiredValueCount(1);
2015-11-25 22:48:24 +01:00
m_styleArg.setCombinable(true);
2015-09-01 20:08:43 +02:00
m_iconThemeArg.setValueNames({"theme name", "search path 1", "search path 2"});
m_iconThemeArg.setRequiredValueCount(-1);
2015-11-25 22:48:24 +01:00
m_iconThemeArg.setCombinable(true);
2015-09-01 20:08:43 +02:00
m_fontArg.setValueNames({"name", "size"});
m_fontArg.setRequiredValueCount(2);
2015-11-25 22:48:24 +01:00
m_fontArg.setCombinable(true);
m_libraryPathsArg.setValueNames({"path 1", "path 2"});
m_libraryPathsArg.setRequiredValueCount(-1);
m_libraryPathsArg.setCombinable(true);
m_qtWidgetsGuiArg.setSecondaryArguments({&m_lngArg, &m_qmlDebuggerArg, &m_styleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg});
m_qtQuickGuiArg.setSecondaryArguments({&m_lngArg, &m_qmlDebuggerArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg});
2015-09-01 20:08:43 +02:00
m_qtWidgetsGuiArg.setDenotesOperation(true);
m_qtQuickGuiArg.setDenotesOperation(true);
#if defined GUI_QTWIDGETS
m_qtWidgetsGuiArg.setDefault(true);
#elif defined GUI_QTQUICK
m_qtQuickGuiArg.setDefault(true);
#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()) {
2015-04-22 18:57:44 +02:00
#ifdef GUI_QTWIDGETS
2015-09-01 20:08:43 +02:00
if(QStyle *style = QStyleFactory::create(QString::fromLocal8Bit(m_styleArg.values().front().data()))) {
2015-04-22 18:57:44 +02:00
QApplication::setStyle(style);
} else {
cerr << "Warning: Can not find the specified style." << endl;
2015-04-22 18:57:44 +02:00
}
#else
# ifdef GUI_QTQUICK
cerr << "Warning: Can not set a style for the Qt Quick GUI." << endl;
# endif
2015-04-22 18:57:44 +02:00
#endif
2015-09-01 20:08:43 +02:00
}
2015-09-22 01:52:08 +02:00
#ifdef Q_OS_WIN32
bool searchPathsPresent = false;
#endif
2015-09-01 20:08:43 +02:00
if(m_iconThemeArg.isPresent()) {
auto i = m_iconThemeArg.values().cbegin(), end = m_iconThemeArg.values().end();
if(i != end) {
if(++i != end) {
QStringList searchPaths;
searchPaths.reserve(m_iconThemeArg.values().size() - 1);
for(; i != end; ++i) {
searchPaths << QString::fromLocal8Bit(i->data());
}
QIcon::setThemeSearchPaths(searchPaths);
2015-04-22 18:57:44 +02:00
#ifdef Q_OS_WIN32
2015-09-22 01:52:08 +02:00
searchPathsPresent = !searchPaths.isEmpty();
2015-04-22 18:57:44 +02:00
#endif
}
2015-09-22 01:52:08 +02:00
QIcon::setThemeName(QString::fromLocal8Bit(i->data()));
2015-04-22 18:57:44 +02:00
}
2015-09-01 20:08:43 +02:00
} else {
if(qEnvironmentVariableIsSet("ICON_THEME_SEARCH_PATH")) {
QString path;
path.append(qgetenv("ICON_THEME_SEARCH_PATH"));
QIcon::setThemeSearchPaths(QStringList() << path);
2015-09-22 01:52:08 +02:00
#ifdef Q_OS_WIN32
searchPathsPresent = true;
#endif
}
if(qEnvironmentVariableIsSet("ICON_THEME")) {
QString themeName;
themeName.append(qgetenv("ICON_THEME"));
QIcon::setThemeName(themeName);
2015-09-01 20:08:43 +02:00
}
}
2015-09-22 01:52:08 +02:00
#ifdef Q_OS_WIN32
// default configuration under Windows
if(!searchPathsPresent) {
QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << QStringLiteral("../share/icons"));
}
if(QIcon::themeName().isEmpty()) {
QIcon::setThemeName(QStringLiteral("default"));
}
#endif
2015-09-01 20:08:43 +02:00
if(m_fontArg.isPresent()) {
QFont font;
font.setFamily(QString::fromLocal8Bit(m_fontArg.values().front().data()));
try {
font.setPointSize(stringToNumber<int>(m_fontArg.values().back()));
} catch(const ConversionException &) {
cerr << "Warning: The specified font size is no number and will be ignored." << endl;
2015-09-01 20:08:43 +02:00
}
QGuiApplication::setFont(font);
} else {
#ifdef Q_OS_WIN32
QGuiApplication::setFont(QFont(QStringLiteral("Segoe UI"), 9));
2015-04-22 18:57:44 +02:00
#endif
2015-09-01 20:08:43 +02:00
}
if(m_libraryPathsArg.isPresent()) {
QStringList libraryPaths;
libraryPaths.reserve(m_libraryPathsArg.values().size());
for(const auto &path : m_libraryPathsArg.values()) {
libraryPaths << QString::fromLocal8Bit(path.data());
}
QCoreApplication::setLibraryPaths(libraryPaths);
}
2015-04-22 18:57:44 +02:00
}
}