Debian Codegraph @

resources/qtconfigarguments.cpp

raw symbols SCIP
1 #include "./qtconfigarguments.h"
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 #if defined(Q_OS_WINDOWS) && (QT_VERSION >= QT_VERSION_CHECK(6, 3, 0))
21 #define QT_UTILITIES_CHECK_WINDOWS_VERSION
22 #include <QMessageBox>
23 #include <QOperatingSystemVersion>
24 #endif
25
26 #include <initializer_list>
27 #include <iostream>
28
29 using namespace std;
30 using namespace CppUtilities::EscapeCodes;
31
32 /*!
33 * \brief The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities library.
34 */
35 namespace CppUtilities {
36
37 /*!
38 * \brief Constructs new Qt config arguments.
39 */
40 QtConfigArguments::QtConfigArguments()
41 : m_qtWidgetsGuiArg("qt-widgets-gui", 'g', "shows a Qt widgets based graphical user interface")
42 , m_qtQuickGuiArg("qt-quick-gui", 'q', "shows a Qt quick based graphical user interface")
43 , m_lngArg("lang", 'l', "sets the language for the Qt GUI")
44 , m_qmlDebuggerArg("qmljsdebugger", 'q',
45 "enables QML debugging (see "
46 "http://doc.qt.io/qt-5/"
47 "qtquick-debugging.html)")
48 , m_widgetsStyleArg("widgets-style", '\0', "sets the Qt Widgets style")
49 , m_quickControls2StyleArg("qqc2-style", '\0', "sets the Qt Quick Controls 2 style")
50 , m_iconThemeArg("icon-theme", '\0',
51 "sets the icon theme and additional "
52 "theme search paths for the Qt GUI")
53 , m_fontArg("font", '\0', "sets the font family and size (point) for the Qt GUI")
54 , m_libraryPathsArg("library-paths", '\0',
55 "sets the list of directories to search when loading "
56 "libraries (all existing paths will be deleted)")
57 , m_platformThemeArg("platformtheme", '\0', "specifies the Qt platform theme to be used")
58 , m_sceneGraphRenderLoopArg("scene-graph-render-loop", '\0', "sets the loop for the Qt Quick Scene Graph OpenGL Renderer")
59 {
60 // language
61 m_lngArg.setValueNames({ "language" });
62 m_lngArg.setRequiredValueCount(1);
63 m_lngArg.setRequired(false);
64 m_lngArg.setCombinable(true);
65 // qml debugger (handled by Qt, just to let the parser know of it)
66 m_qmlDebuggerArg.setValueNames({ "port:<port_from>[,port_to][,host:<ip address>][,block]" });
67 m_qmlDebuggerArg.setRequiredValueCount(1);
68 m_qmlDebuggerArg.setCombinable(true);
69 // appearance
70 m_widgetsStyleArg.setValueNames({ "breeze/cleanlooks/fusion/kvantum/oxygen/adwaita/windows/..." });
71 m_widgetsStyleArg.setRequiredValueCount(1);
72 m_widgetsStyleArg.setCombinable(true);
73 m_widgetsStyleArg.setEnvironmentVariable("QT_STYLE_OVERRIDE");
74 m_quickControls2StyleArg.setValueNames({ "default/material/universal/org.kde.desktop/..." });
75 m_quickControls2StyleArg.setRequiredValueCount(1);
76 m_quickControls2StyleArg.setCombinable(true);
77 m_quickControls2StyleArg.setEnvironmentVariable("QT_QUICK_CONTROLS_STYLE");
78 m_iconThemeArg.setValueNames({ "theme name", "search path 1", "search path 2" });
79 m_iconThemeArg.setRequiredValueCount(Argument::varValueCount);
80 m_iconThemeArg.setCombinable(true);
81 m_iconThemeArg.setEnvironmentVariable("ICON_THEME_SEARCH_PATH and ICON_THEME");
82 m_fontArg.setValueNames({ "name", "size" });
83 m_fontArg.setRequiredValueCount(2);
84 m_fontArg.setCombinable(true);
85 m_libraryPathsArg.setValueNames({ "path 1", "path 2" });
86 m_libraryPathsArg.setRequiredValueCount(Argument::varValueCount);
87 m_libraryPathsArg.setCombinable(true);
88 m_platformThemeArg.setRequiredValueCount(1);
89 m_platformThemeArg.setCombinable(true);
90 m_platformThemeArg.setValueNames({ "qt5ct/kde/..." });
91 m_platformThemeArg.setPreDefinedCompletionValues("kde gnome "
92 #if QT_VERSION_MAJOR == 5
93 "qt5ct"
94 #elif QT_VERSION_MAJOR == 6
95 "qt6ct"
96 #endif
97 );
98 m_platformThemeArg.setEnvironmentVariable("QT_QPA_PLATFORMTHEME");
99 m_sceneGraphRenderLoopArg.setRequiredValueCount(1);
100 m_sceneGraphRenderLoopArg.setCombinable(true);
101 m_sceneGraphRenderLoopArg.setValueNames({ "basic/windows/threaded" });
102 m_sceneGraphRenderLoopArg.setPreDefinedCompletionValues("basic windows threaded");
103 m_sceneGraphRenderLoopArg.setEnvironmentVariable("QSG_RENDER_LOOP");
104 m_qtWidgetsGuiArg.setSubArguments(
105 { &m_lngArg, &m_qmlDebuggerArg, &m_widgetsStyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
106 m_qtQuickGuiArg.setSubArguments({ &m_lngArg, &m_qmlDebuggerArg, &m_quickControls2StyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg,
107 &m_platformThemeArg, &m_sceneGraphRenderLoopArg });
108 m_qtWidgetsGuiArg.setDenotesOperation(true);
109 m_qtQuickGuiArg.setDenotesOperation(true);
110 #if defined(QT_UTILITIES_GUI_QTWIDGETS)
111 m_qtWidgetsGuiArg.setImplicit(true);
112 #elif defined(QT_UTILITIES_GUI_QTQUICK)
113 m_qtQuickGuiArg.setImplicit(true);
114 #endif
115 }
116
117 /*!
118 * \brief Applies the settings from the arguments.
119 * \remarks Also checks environment variables for the icon theme.
120 * \param preventApplyingDefaultFont If true, the font will not be updated to
121 * some default value if no font has been specified explicitly.
122 */
123 void QtConfigArguments::applySettings(bool preventApplyingDefaultFont) const
124 {
125 if (m_lngArg.isPresent()) {
126 QLocale::setDefault(QLocale(QString::fromLocal8Bit(m_lngArg.values().front())));
127 }
128 #ifdef QT_UTILITIES_GUI_QTWIDGETS
129 if (m_widgetsStyleArg.isPresent()) {
130 if (QStyle *const style = QStyleFactory::create(QString::fromLocal8Bit(m_widgetsStyleArg.values().front()))) {
131 QApplication::setStyle(style);
132 } else {
133 cerr << Phrases::Warning << "Can not find the specified Qt Widgets style." << Phrases::EndFlush;
134 }
135 }
136 #endif
137 if (m_iconThemeArg.isPresent()) {
138 // set first value of m_iconThemeArg as icon theme and add further values as search paths
139 if (auto i = m_iconThemeArg.values().cbegin(), end = m_iconThemeArg.values().cend(); i != end) {
140 QIcon::setThemeName(QString::fromUtf8(*i));
141 if (++i != end) {
142 auto searchPaths = QStringList();
143 searchPaths.reserve(static_cast<QStringList::size_type>(m_iconThemeArg.values().size()));
144 for (; i != end; ++i) {
145 searchPaths << QString::fromUtf8(*i);
146 }
147 searchPaths << QStringLiteral(":/icons");
148 QIcon::setThemeSearchPaths(searchPaths);
149 }
150 }
151 } else {
152 if (qEnvironmentVariableIsSet("ICON_THEME_SEARCH_PATH")) {
153 QIcon::setThemeSearchPaths(QStringList({ qEnvironmentVariable("ICON_THEME_SEARCH_PATH"), QStringLiteral(":/icons") }));
154 } else {
155 QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << QStringLiteral("../share/icons") << QStringLiteral(":/icons"));
156 }
157 if (qEnvironmentVariableIsSet("ICON_THEME")) {
158 QIcon::setThemeName(qEnvironmentVariable("ICON_THEME"));
159 }
160 }
161 if (m_fontArg.isPresent()) {
162 QFont font;
163 font.setFamily(QString::fromLocal8Bit(m_fontArg.values().front()));
164 try {
165 font.setPointSize(stringToNumber<int>(m_fontArg.values().back()));
166 } catch (const ConversionException &) {
167 cerr << Phrases::Warning << "The specified font size is no number and will be ignored." << Phrases::EndFlush;
168 }
169 QGuiApplication::setFont(font);
170 }
171 #ifdef Q_OS_WIN32
172 else if (!preventApplyingDefaultFont) {
173 QGuiApplication::setFont(QFont(QStringLiteral("Segoe UI"), 9));
174 }
175 #else
176 CPP_UTILITIES_UNUSED(preventApplyingDefaultFont)
177 #endif
178 if (m_libraryPathsArg.isPresent()) {
179 QStringList libraryPaths;
180 libraryPaths.reserve(static_cast<QStringList::size_type>(m_libraryPathsArg.values().size()));
181 for (const auto &path : m_libraryPathsArg.values()) {
182 libraryPaths << QString::fromLocal8Bit(path);
183 }
184 QCoreApplication::setLibraryPaths(libraryPaths);
185 }
186 if (m_sceneGraphRenderLoopArg.isPresent()) {
187 qputenv(m_sceneGraphRenderLoopArg.environmentVariable(), QByteArray(m_sceneGraphRenderLoopArg.firstValue()));
188 }
189
190 #ifdef QT_UTILITIES_CHECK_WINDOWS_VERSION
191 if (QOperatingSystemVersion::current() < QOperatingSystemVersion::Windows10_1809) {
192 QMessageBox::warning(nullptr, QCoreApplication::applicationName(),
193 QCoreApplication::translate("QtConfigArguments",
194 "This application requires Windows 10, version 1809 or newer. The current Windows version is older so the application might not work "
195 "correctly."));
196 }
197 #endif
198 }
199 } // namespace CppUtilities