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 <QFont>
|
|
|
|
#include <QIcon>
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QLocale>
|
|
|
|
#include <QString>
|
2017-04-27 22:03:37 +02:00
|
|
|
#ifdef QT_UTILITIES_GUI_QTWIDGETS
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QApplication>
|
|
|
|
#include <QStyleFactory>
|
2015-04-22 18:57:44 +02:00
|
|
|
#else
|
2017-05-01 03:16:25 +02:00
|
|
|
#include <QGuiApplication>
|
2015-04-22 18:57:44 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
using namespace std;
|
2016-02-09 01:06:50 +01:00
|
|
|
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.
|
|
|
|
*/
|
2017-05-01 03:16:25 +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")
|
2017-05-04 22:46:37 +02:00
|
|
|
, m_qmlDebuggerArg("qmljsdebugger", '\0', "enables QML debugging (see "
|
|
|
|
"http://doc.qt.io/qt-5/"
|
|
|
|
"qtquick-debugging.html)")
|
2017-05-01 03:16:25 +02:00
|
|
|
, m_styleArg("style", '\0', "sets the Qt widgets style")
|
2017-05-04 22:46:37 +02:00
|
|
|
, m_iconThemeArg("icon-theme", '\0', "sets the icon theme and additional "
|
|
|
|
"theme search paths for the Qt GUI")
|
2017-05-01 03:16:25 +02:00
|
|
|
, m_fontArg("font", '\0', "sets the font family and size (point) for the Qt GUI")
|
2017-05-04 22:46:37 +02:00
|
|
|
, m_libraryPathsArg("library-paths", '\0', "sets the list of directories to search when loading "
|
|
|
|
"libraries (all existing paths will be deleted)")
|
2017-05-01 03:16:25 +02:00
|
|
|
, m_platformThemeArg("platformtheme", '\0', "specifies the Qt platform theme to be used")
|
2015-04-22 18:57:44 +02:00
|
|
|
{
|
|
|
|
// language
|
2017-05-01 03:16:25 +02:00
|
|
|
m_lngArg.setValueNames({ "language" });
|
2015-04-22 18:57:44 +02:00
|
|
|
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)
|
2017-05-01 03:16:25 +02:00
|
|
|
m_qmlDebuggerArg.setValueNames({ "port:<port_from>[,port_to][,host:<ip address>][,block]" });
|
2015-04-22 18:57:44 +02:00
|
|
|
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
|
2017-05-01 03:16:25 +02:00
|
|
|
m_styleArg.setValueNames({ "style name" });
|
2015-04-22 18:57:44 +02:00
|
|
|
m_styleArg.setRequiredValueCount(1);
|
2015-11-25 22:48:24 +01:00
|
|
|
m_styleArg.setCombinable(true);
|
2017-05-01 03:16:25 +02:00
|
|
|
m_iconThemeArg.setValueNames({ "theme name", "search path 1", "search path 2" });
|
2015-09-01 20:08:43 +02:00
|
|
|
m_iconThemeArg.setRequiredValueCount(-1);
|
2015-11-25 22:48:24 +01:00
|
|
|
m_iconThemeArg.setCombinable(true);
|
2017-05-01 03:16:25 +02:00
|
|
|
m_fontArg.setValueNames({ "name", "size" });
|
2015-09-01 20:08:43 +02:00
|
|
|
m_fontArg.setRequiredValueCount(2);
|
2015-11-25 22:48:24 +01:00
|
|
|
m_fontArg.setCombinable(true);
|
2017-05-01 03:16:25 +02:00
|
|
|
m_libraryPathsArg.setValueNames({ "path 1", "path 2" });
|
2016-02-09 01:06:50 +01:00
|
|
|
m_libraryPathsArg.setRequiredValueCount(-1);
|
|
|
|
m_libraryPathsArg.setCombinable(true);
|
2016-08-16 19:35:40 +02:00
|
|
|
m_platformThemeArg.setRequiredValueCount(1);
|
|
|
|
m_platformThemeArg.setCombinable(true);
|
2017-05-01 03:16:25 +02:00
|
|
|
m_platformThemeArg.setValueNames({ "qt5ct/kde/..." });
|
2016-08-16 19:35:40 +02:00
|
|
|
m_platformThemeArg.setPreDefinedCompletionValues("qt5ct kde gnome");
|
2017-05-01 03:16:25 +02:00
|
|
|
m_qtWidgetsGuiArg.setSubArguments(
|
|
|
|
{ &m_lngArg, &m_qmlDebuggerArg, &m_styleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
|
|
|
|
m_qtQuickGuiArg.setSubArguments({ &m_lngArg, &m_qmlDebuggerArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
|
2015-09-01 20:08:43 +02:00
|
|
|
m_qtWidgetsGuiArg.setDenotesOperation(true);
|
|
|
|
m_qtQuickGuiArg.setDenotesOperation(true);
|
2017-04-27 22:03:37 +02:00
|
|
|
#if defined QT_UTILITIES_GUI_QTWIDGETS
|
2016-06-14 00:46:26 +02:00
|
|
|
m_qtWidgetsGuiArg.setImplicit(true);
|
2017-04-27 22:03:37 +02:00
|
|
|
#elif defined QT_UTILITIES_GUI_QTQUICK
|
2016-06-14 00:46:26 +02:00
|
|
|
m_qtQuickGuiArg.setImplicit(true);
|
2015-09-01 20:08:43 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Applies the settings from the arguments.
|
|
|
|
* \remarks Also checks environment variables for the icon theme.
|
2017-05-04 22:46:37 +02:00
|
|
|
* \param preventApplyingDefaultFont If true, the font will not be updated to
|
|
|
|
* some default value if no font has been specified explicitly.
|
2015-09-01 20:08:43 +02:00
|
|
|
*/
|
2016-08-14 22:44:25 +02:00
|
|
|
void QtConfigArguments::applySettings(bool preventApplyingDefaultFont) const
|
2015-09-01 20:08:43 +02:00
|
|
|
{
|
2017-05-01 03:16:25 +02:00
|
|
|
if (m_lngArg.isPresent()) {
|
2016-06-14 00:46:26 +02:00
|
|
|
QLocale::setDefault(QLocale(QString::fromLocal8Bit(m_lngArg.values().front())));
|
2015-09-01 20:08:43 +02:00
|
|
|
}
|
2017-05-01 03:16:25 +02:00
|
|
|
if (m_styleArg.isPresent()) {
|
2017-04-27 22:03:37 +02:00
|
|
|
#ifdef QT_UTILITIES_GUI_QTWIDGETS
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QStyle *style = QStyleFactory::create(QString::fromLocal8Bit(m_styleArg.values().front()))) {
|
2015-04-22 18:57:44 +02:00
|
|
|
QApplication::setStyle(style);
|
|
|
|
} else {
|
2016-04-09 02:33:09 +02:00
|
|
|
cerr << "Warning: Can not find the specified style." << endl;
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
2016-04-09 02:33:09 +02:00
|
|
|
#else
|
2017-05-01 03:16:25 +02:00
|
|
|
#ifdef QT_UTILITIES_GUI_QTQUICK
|
2016-04-09 02:33:09 +02:00
|
|
|
cerr << "Warning: Can not set a style for the Qt Quick GUI." << endl;
|
2017-05-01 03:16:25 +02:00
|
|
|
#endif
|
2015-04-22 18:57:44 +02:00
|
|
|
#endif
|
2015-09-01 20:08:43 +02:00
|
|
|
}
|
2017-05-01 03:16:25 +02:00
|
|
|
if (m_iconThemeArg.isPresent()) {
|
2015-09-01 20:08:43 +02:00
|
|
|
auto i = m_iconThemeArg.values().cbegin(), end = m_iconThemeArg.values().end();
|
2017-05-01 03:16:25 +02:00
|
|
|
if (i != end) {
|
2016-08-16 20:16:11 +02:00
|
|
|
QIcon::setThemeName(QString::fromLocal8Bit(*i));
|
2017-05-01 03:16:25 +02:00
|
|
|
if (++i != end) {
|
2015-09-01 20:08:43 +02:00
|
|
|
QStringList searchPaths;
|
|
|
|
searchPaths.reserve(m_iconThemeArg.values().size() - 1);
|
2017-05-01 03:16:25 +02:00
|
|
|
for (; i != end; ++i) {
|
2016-06-14 00:46:26 +02:00
|
|
|
searchPaths << QString::fromLocal8Bit(*i);
|
2015-09-01 20:08:43 +02:00
|
|
|
}
|
2016-07-16 23:12:02 +02:00
|
|
|
searchPaths << QStringLiteral(":/icons");
|
|
|
|
QIcon::setThemeSearchPaths(searchPaths);
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
|
|
|
}
|
2015-09-01 20:08:43 +02:00
|
|
|
} else {
|
2017-05-01 03:16:25 +02:00
|
|
|
if (qEnvironmentVariableIsSet("ICON_THEME_SEARCH_PATH")) {
|
2015-09-01 20:08:43 +02:00
|
|
|
QString path;
|
|
|
|
path.append(qgetenv("ICON_THEME_SEARCH_PATH"));
|
2016-07-16 23:12:02 +02:00
|
|
|
QIcon::setThemeSearchPaths(QStringList() << path << QStringLiteral(":/icons"));
|
|
|
|
} else {
|
2016-08-16 20:16:11 +02:00
|
|
|
QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << QStringLiteral("../share/icons") << QStringLiteral(":/icons"));
|
2015-09-22 01:52:08 +02:00
|
|
|
}
|
2017-05-01 03:16:25 +02:00
|
|
|
if (qEnvironmentVariableIsSet("ICON_THEME")) {
|
2015-09-22 01:52:08 +02:00
|
|
|
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
|
2017-05-01 03:16:25 +02:00
|
|
|
if (QIcon::themeName().isEmpty()) {
|
2015-09-22 01:52:08 +02:00
|
|
|
QIcon::setThemeName(QStringLiteral("default"));
|
|
|
|
}
|
|
|
|
#endif
|
2017-05-01 03:16:25 +02:00
|
|
|
if (m_fontArg.isPresent()) {
|
2015-09-01 20:08:43 +02:00
|
|
|
QFont font;
|
2016-06-14 00:46:26 +02:00
|
|
|
font.setFamily(QString::fromLocal8Bit(m_fontArg.values().front()));
|
2015-09-01 20:08:43 +02:00
|
|
|
try {
|
2016-02-09 01:06:50 +01:00
|
|
|
font.setPointSize(stringToNumber<int>(m_fontArg.values().back()));
|
2017-05-01 03:16:25 +02:00
|
|
|
} catch (const ConversionException &) {
|
2017-05-04 22:46:37 +02:00
|
|
|
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);
|
2016-08-14 22:44:25 +02:00
|
|
|
}
|
2015-09-01 20:08:43 +02:00
|
|
|
#ifdef Q_OS_WIN32
|
2017-05-01 03:16:25 +02:00
|
|
|
else if (!preventApplyingDefaultFont) {
|
2015-09-01 20:08:43 +02:00
|
|
|
QGuiApplication::setFont(QFont(QStringLiteral("Segoe UI"), 9));
|
|
|
|
}
|
2016-08-14 22:44:25 +02:00
|
|
|
#else
|
|
|
|
VAR_UNUSED(preventApplyingDefaultFont)
|
|
|
|
#endif
|
2017-05-01 03:16:25 +02:00
|
|
|
if (m_libraryPathsArg.isPresent()) {
|
2016-02-09 01:06:50 +01:00
|
|
|
QStringList libraryPaths;
|
|
|
|
libraryPaths.reserve(m_libraryPathsArg.values().size());
|
2017-05-01 03:16:25 +02:00
|
|
|
for (const auto &path : m_libraryPathsArg.values()) {
|
2016-06-14 00:46:26 +02:00
|
|
|
libraryPaths << QString::fromLocal8Bit(path);
|
2016-02-09 01:06:50 +01:00
|
|
|
}
|
|
|
|
QCoreApplication::setLibraryPaths(libraryPaths);
|
|
|
|
}
|
2015-04-22 18:57:44 +02:00
|
|
|
}
|
|
|
|
}
|