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