qtutilities/resources/qtconfigarguments.cpp

193 lines
8.3 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>
#include <c++utilities/io/ansiescapecodes.h>
2015-09-01 20:08:43 +02:00
2015-04-22 18:57:44 +02:00
#include <QFont>
#include <QIcon>
2017-05-01 03:16:25 +02:00
#include <QLocale>
#include <QString>
#ifdef QT_UTILITIES_GUI_QTWIDGETS
2017-05-01 03:16:25 +02:00
#include <QApplication>
2018-06-05 22:49:08 +02:00
#include <QStyle>
2017-05-01 03:16:25 +02:00
#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;
using namespace CppUtilities::EscapeCodes;
2015-04-22 18:57:44 +02:00
/*!
* \brief The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities library.
*/
namespace CppUtilities {
2015-04-22 18:57:44 +02:00
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")
, m_qmlDebuggerArg("qmljsdebugger", 'q',
"enables QML debugging (see "
"http://doc.qt.io/qt-5/"
"qtquick-debugging.html)")
2019-05-04 23:34:19 +02:00
, m_widgetsStyleArg("widgets-style", '\0', "sets the Qt Widgets style")
, m_quickControls2StyleArg("qqc2-style", '\0', "sets the Qt Quick Controls 2 style")
, 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")
, 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")
, m_sceneGraphRenderLoopArg("scene-graph-render-loop", '\0', "sets the loop for the Qt Quick Scene Graph OpenGL Renderer")
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
2019-05-04 23:34:19 +02:00
m_widgetsStyleArg.setValueNames({ "breeze/cleanlooks/fusion/kvantum/oxygen/adwaita/windows/..." });
m_widgetsStyleArg.setRequiredValueCount(1);
m_widgetsStyleArg.setCombinable(true);
m_widgetsStyleArg.setEnvironmentVariable("QT_STYLE_OVERRIDE");
m_quickControls2StyleArg.setValueNames({ "default/material/universal/org.kde.desktop/..." });
m_quickControls2StyleArg.setRequiredValueCount(1);
m_quickControls2StyleArg.setCombinable(true);
m_quickControls2StyleArg.setEnvironmentVariable("QT_QUICK_CONTROLS_STYLE");
2017-05-01 03:16:25 +02:00
m_iconThemeArg.setValueNames({ "theme name", "search path 1", "search path 2" });
m_iconThemeArg.setRequiredValueCount(Argument::varValueCount);
2015-11-25 22:48:24 +01:00
m_iconThemeArg.setCombinable(true);
m_iconThemeArg.setEnvironmentVariable("ICON_THEME_SEARCH_PATH and ICON_THEME");
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" });
m_libraryPathsArg.setRequiredValueCount(Argument::varValueCount);
m_libraryPathsArg.setCombinable(true);
m_platformThemeArg.setRequiredValueCount(1);
m_platformThemeArg.setCombinable(true);
2017-05-01 03:16:25 +02:00
m_platformThemeArg.setValueNames({ "qt5ct/kde/..." });
m_platformThemeArg.setPreDefinedCompletionValues("kde gnome "
#if QT_VERSION_MAJOR == 5
"qt5ct"
2020-12-20 17:56:35 +01:00
#elif QT_VERSION_MAJOR == 6
"qt6ct"
#endif
2020-12-20 17:56:35 +01:00
);
m_platformThemeArg.setEnvironmentVariable("QT_QPA_PLATFORMTHEME");
m_sceneGraphRenderLoopArg.setRequiredValueCount(1);
m_sceneGraphRenderLoopArg.setCombinable(true);
m_sceneGraphRenderLoopArg.setValueNames({ "basic/windows/threaded" });
m_sceneGraphRenderLoopArg.setPreDefinedCompletionValues("basic windows threaded");
m_sceneGraphRenderLoopArg.setEnvironmentVariable("QSG_RENDER_LOOP");
2017-05-01 03:16:25 +02:00
m_qtWidgetsGuiArg.setSubArguments(
2019-05-04 23:34:19 +02:00
{ &m_lngArg, &m_qmlDebuggerArg, &m_widgetsStyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
m_qtQuickGuiArg.setSubArguments({ &m_lngArg, &m_qmlDebuggerArg, &m_quickControls2StyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg,
&m_platformThemeArg, &m_sceneGraphRenderLoopArg });
2015-09-01 20:08:43 +02:00
m_qtWidgetsGuiArg.setDenotesOperation(true);
m_qtQuickGuiArg.setDenotesOperation(true);
2018-12-08 19:08:30 +01:00
#if defined(QT_UTILITIES_GUI_QTWIDGETS)
2016-06-14 00:46:26 +02:00
m_qtWidgetsGuiArg.setImplicit(true);
2018-12-08 19:08:30 +01: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
*/
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
}
#ifdef QT_UTILITIES_GUI_QTWIDGETS
2019-05-04 23:34:19 +02:00
if (m_widgetsStyleArg.isPresent()) {
if (QStyle *const style = QStyleFactory::create(QString::fromLocal8Bit(m_widgetsStyleArg.values().front()))) {
QApplication::setStyle(style);
} else {
cerr << Phrases::Warning << "Can not find the specified Qt Widgets style." << Phrases::EndFlush;
2015-04-22 18:57:44 +02:00
}
2015-09-01 20:08:43 +02:00
}
#endif
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) {
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;
2021-03-20 21:02:00 +01:00
searchPaths.reserve(static_cast<QStringList::size_type>(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
}
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"));
QIcon::setThemeSearchPaths(QStringList({ path, QStringLiteral(":/icons") }));
} else {
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 {
font.setPointSize(stringToNumber<int>(m_fontArg.values().back()));
2017-05-01 03:16:25 +02:00
} catch (const ConversionException &) {
cerr << Phrases::Warning << "The specified font size is no number and will be ignored." << Phrases::EndFlush;
2015-09-01 20:08:43 +02:00
}
QGuiApplication::setFont(font);
}
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));
}
#else
2019-06-12 20:39:02 +02:00
CPP_UTILITIES_UNUSED(preventApplyingDefaultFont)
#endif
2017-05-01 03:16:25 +02:00
if (m_libraryPathsArg.isPresent()) {
QStringList libraryPaths;
2021-03-20 21:02:00 +01:00
libraryPaths.reserve(static_cast<QStringList::size_type>(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);
}
QCoreApplication::setLibraryPaths(libraryPaths);
}
if (m_sceneGraphRenderLoopArg.isPresent()) {
qputenv(m_sceneGraphRenderLoopArg.environmentVariable(), QByteArray(m_sceneGraphRenderLoopArg.firstValue()));
}
2015-04-22 18:57:44 +02:00
}
} // namespace CppUtilities