Qt Utilities 6.12.0
Common Qt related C++ classes and routines used by my applications such as dialogs, widgets and models
Loading...
Searching...
No Matches
qtconfigarguments.cpp
Go to the documentation of this file.
2
3#include "../misc/compat.h"
4
5#include <c++utilities/conversion/stringconversion.h>
6#include <c++utilities/io/ansiescapecodes.h>
7
8#include <QFont>
9#include <QIcon>
10#include <QLocale>
11#include <QString>
12#ifdef QT_UTILITIES_GUI_QTWIDGETS
13#include <QApplication>
14#include <QStyle>
15#include <QStyleFactory>
16#else
17#include <QGuiApplication>
18#endif
19
20#include <initializer_list>
21#include <iostream>
22
23using namespace std;
24using namespace CppUtilities::EscapeCodes;
25
29namespace CppUtilities {
30
35 : m_qtWidgetsGuiArg("qt-widgets-gui", 'g', "shows a Qt widgets based graphical user interface")
36 , m_qtQuickGuiArg("qt-quick-gui", 'q', "shows a Qt quick based graphical user interface")
37 , m_lngArg("lang", 'l', "sets the language for the Qt GUI")
38 , m_qmlDebuggerArg("qmljsdebugger", 'q',
39 "enables QML debugging (see "
40 "http://doc.qt.io/qt-5/"
41 "qtquick-debugging.html)")
42 , m_widgetsStyleArg("widgets-style", '\0', "sets the Qt Widgets style")
43 , m_quickControls2StyleArg("qqc2-style", '\0', "sets the Qt Quick Controls 2 style")
44 , m_iconThemeArg("icon-theme", '\0',
45 "sets the icon theme and additional "
46 "theme search paths for the Qt GUI")
47 , m_fontArg("font", '\0', "sets the font family and size (point) for the Qt GUI")
48 , m_libraryPathsArg("library-paths", '\0',
49 "sets the list of directories to search when loading "
50 "libraries (all existing paths will be deleted)")
51 , m_platformThemeArg("platformtheme", '\0', "specifies the Qt platform theme to be used")
52 , m_sceneGraphRenderLoopArg("scene-graph-render-loop", '\0', "sets the loop for the Qt Quick Scene Graph OpenGL Renderer")
53{
54 // language
55 m_lngArg.setValueNames({ "language" });
56 m_lngArg.setRequiredValueCount(1);
57 m_lngArg.setRequired(false);
58 m_lngArg.setCombinable(true);
59 // qml debugger (handled by Qt, just to let the parser know of it)
60 m_qmlDebuggerArg.setValueNames({ "port:<port_from>[,port_to][,host:<ip address>][,block]" });
61 m_qmlDebuggerArg.setRequiredValueCount(1);
62 m_qmlDebuggerArg.setCombinable(true);
63 // appearance
64 m_widgetsStyleArg.setValueNames({ "breeze/cleanlooks/fusion/kvantum/oxygen/adwaita/windows/..." });
65 m_widgetsStyleArg.setRequiredValueCount(1);
66 m_widgetsStyleArg.setCombinable(true);
67 m_widgetsStyleArg.setEnvironmentVariable("QT_STYLE_OVERRIDE");
68 m_quickControls2StyleArg.setValueNames({ "default/material/universal/org.kde.desktop/..." });
69 m_quickControls2StyleArg.setRequiredValueCount(1);
70 m_quickControls2StyleArg.setCombinable(true);
71 m_quickControls2StyleArg.setEnvironmentVariable("QT_QUICK_CONTROLS_STYLE");
72 m_iconThemeArg.setValueNames({ "theme name", "search path 1", "search path 2" });
73 m_iconThemeArg.setRequiredValueCount(Argument::varValueCount);
74 m_iconThemeArg.setCombinable(true);
75 m_iconThemeArg.setEnvironmentVariable("ICON_THEME_SEARCH_PATH and ICON_THEME");
76 m_fontArg.setValueNames({ "name", "size" });
77 m_fontArg.setRequiredValueCount(2);
78 m_fontArg.setCombinable(true);
79 m_libraryPathsArg.setValueNames({ "path 1", "path 2" });
80 m_libraryPathsArg.setRequiredValueCount(Argument::varValueCount);
81 m_libraryPathsArg.setCombinable(true);
82 m_platformThemeArg.setRequiredValueCount(1);
83 m_platformThemeArg.setCombinable(true);
84 m_platformThemeArg.setValueNames({ "qt5ct/kde/..." });
85 m_platformThemeArg.setPreDefinedCompletionValues("kde gnome "
86#if QT_VERSION_MAJOR == 5
87 "qt5ct"
88#elif QT_VERSION_MAJOR == 6
89 "qt6ct"
90#endif
91 );
92 m_platformThemeArg.setEnvironmentVariable("QT_QPA_PLATFORMTHEME");
93 m_sceneGraphRenderLoopArg.setRequiredValueCount(1);
94 m_sceneGraphRenderLoopArg.setCombinable(true);
95 m_sceneGraphRenderLoopArg.setValueNames({ "basic/windows/threaded" });
96 m_sceneGraphRenderLoopArg.setPreDefinedCompletionValues("basic windows threaded");
97 m_sceneGraphRenderLoopArg.setEnvironmentVariable("QSG_RENDER_LOOP");
98 m_qtWidgetsGuiArg.setSubArguments(
99 { &m_lngArg, &m_qmlDebuggerArg, &m_widgetsStyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
100 m_qtQuickGuiArg.setSubArguments({ &m_lngArg, &m_qmlDebuggerArg, &m_quickControls2StyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg,
101 &m_platformThemeArg, &m_sceneGraphRenderLoopArg });
102 m_qtWidgetsGuiArg.setDenotesOperation(true);
103 m_qtQuickGuiArg.setDenotesOperation(true);
104#if defined(QT_UTILITIES_GUI_QTWIDGETS)
105 m_qtWidgetsGuiArg.setImplicit(true);
106#elif defined(QT_UTILITIES_GUI_QTQUICK)
107 m_qtQuickGuiArg.setImplicit(true);
108#endif
109}
110
117void QtConfigArguments::applySettings(bool preventApplyingDefaultFont) const
118{
119 if (m_lngArg.isPresent()) {
120 QLocale::setDefault(QLocale(QString::fromLocal8Bit(m_lngArg.values().front())));
121 }
122#ifdef QT_UTILITIES_GUI_QTWIDGETS
123 if (m_widgetsStyleArg.isPresent()) {
124 if (QStyle *const style = QStyleFactory::create(QString::fromLocal8Bit(m_widgetsStyleArg.values().front()))) {
125 QApplication::setStyle(style);
126 } else {
127 cerr << Phrases::Warning << "Can not find the specified Qt Widgets style." << Phrases::EndFlush;
128 }
129 }
130#endif
131 if (m_iconThemeArg.isPresent()) {
132 // set first value of m_iconThemeArg as icon theme and add further values as search paths
133 if (auto i = m_iconThemeArg.values().cbegin(), end = m_iconThemeArg.values().cend(); i != end) {
134 QIcon::setThemeName(QString::fromUtf8(*i));
135 if (++i != end) {
136 auto searchPaths = QStringList();
137 searchPaths.reserve(static_cast<QStringList::size_type>(m_iconThemeArg.values().size()));
138 for (; i != end; ++i) {
139 searchPaths << QString::fromUtf8(*i);
140 }
141 searchPaths << QStringLiteral(":/icons");
142 QIcon::setThemeSearchPaths(searchPaths);
143 }
144 }
145 } else {
146 if (qEnvironmentVariableIsSet("ICON_THEME_SEARCH_PATH")) {
147 QIcon::setThemeSearchPaths(QStringList({ qEnvironmentVariable("ICON_THEME_SEARCH_PATH"), QStringLiteral(":/icons") }));
148 } else {
149 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << QStringLiteral("../share/icons") << QStringLiteral(":/icons"));
150 }
151 if (qEnvironmentVariableIsSet("ICON_THEME")) {
152 QIcon::setThemeName(qEnvironmentVariable("ICON_THEME"));
153 }
154 }
155 if (m_fontArg.isPresent()) {
156 QFont font;
157 font.setFamily(QString::fromLocal8Bit(m_fontArg.values().front()));
158 try {
159 font.setPointSize(stringToNumber<int>(m_fontArg.values().back()));
160 } catch (const ConversionException &) {
161 cerr << Phrases::Warning << "The specified font size is no number and will be ignored." << Phrases::EndFlush;
162 }
163 QGuiApplication::setFont(font);
164 }
165#ifdef Q_OS_WIN32
166 else if (!preventApplyingDefaultFont) {
167 QGuiApplication::setFont(QFont(QStringLiteral("Segoe UI"), 9));
168 }
169#else
170 CPP_UTILITIES_UNUSED(preventApplyingDefaultFont)
171#endif
172 if (m_libraryPathsArg.isPresent()) {
173 QStringList libraryPaths;
174 libraryPaths.reserve(static_cast<QStringList::size_type>(m_libraryPathsArg.values().size()));
175 for (const auto &path : m_libraryPathsArg.values()) {
176 libraryPaths << QString::fromLocal8Bit(path);
177 }
178 QCoreApplication::setLibraryPaths(libraryPaths);
179 }
180 if (m_sceneGraphRenderLoopArg.isPresent()) {
181 qputenv(m_sceneGraphRenderLoopArg.environmentVariable(), QByteArray(m_sceneGraphRenderLoopArg.firstValue()));
182 }
183}
184} // namespace CppUtilities
QtConfigArguments()
Constructs new Qt config arguments.
void applySettings(bool preventApplyingDefaultFont=false) const
Applies the settings from the arguments.
The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities li...