Qt Utilities  6.1.0
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.
1 #include "./qtconfigarguments.h"
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 
21 using namespace std;
22 using namespace CppUtilities::EscapeCodes;
23 
27 namespace CppUtilities {
28 
32 QtConfigArguments::QtConfigArguments()
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("qt5ct kde gnome");
84  m_platformThemeArg.setEnvironmentVariable("QT_QPA_PLATFORMTHEME");
85  m_sceneGraphRenderLoopArg.setRequiredValueCount(1);
86  m_sceneGraphRenderLoopArg.setCombinable(true);
87  m_sceneGraphRenderLoopArg.setValueNames({ "basic/windows/threaded" });
88  m_sceneGraphRenderLoopArg.setPreDefinedCompletionValues("basic windows threaded");
89  m_sceneGraphRenderLoopArg.setEnvironmentVariable("QSG_RENDER_LOOP");
90  m_qtWidgetsGuiArg.setSubArguments(
91  { &m_lngArg, &m_qmlDebuggerArg, &m_widgetsStyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
92  m_qtQuickGuiArg.setSubArguments({ &m_lngArg, &m_qmlDebuggerArg, &m_quickControls2StyleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg,
93  &m_platformThemeArg, &m_sceneGraphRenderLoopArg });
94  m_qtWidgetsGuiArg.setDenotesOperation(true);
95  m_qtQuickGuiArg.setDenotesOperation(true);
96 #if defined(QT_UTILITIES_GUI_QTWIDGETS)
97  m_qtWidgetsGuiArg.setImplicit(true);
98 #elif defined(QT_UTILITIES_GUI_QTQUICK)
99  m_qtQuickGuiArg.setImplicit(true);
100 #endif
101 }
102 
109 void QtConfigArguments::applySettings(bool preventApplyingDefaultFont) const
110 {
111  if (m_lngArg.isPresent()) {
112  QLocale::setDefault(QLocale(QString::fromLocal8Bit(m_lngArg.values().front())));
113  }
114 #ifdef QT_UTILITIES_GUI_QTWIDGETS
115  if (m_widgetsStyleArg.isPresent()) {
116  if (QStyle *const style = QStyleFactory::create(QString::fromLocal8Bit(m_widgetsStyleArg.values().front()))) {
117  QApplication::setStyle(style);
118  } else {
119  cerr << Phrases::Warning << "Can not find the specified Qt Widgets style." << Phrases::EndFlush;
120  }
121  }
122 #endif
123  if (m_iconThemeArg.isPresent()) {
124  auto i = m_iconThemeArg.values().cbegin(), end = m_iconThemeArg.values().end();
125  if (i != end) {
126  QIcon::setThemeName(QString::fromLocal8Bit(*i));
127  if (++i != end) {
128  QStringList searchPaths;
129  searchPaths.reserve(m_iconThemeArg.values().size() - 1);
130  for (; i != end; ++i) {
131  searchPaths << QString::fromLocal8Bit(*i);
132  }
133  searchPaths << QStringLiteral(":/icons");
134  QIcon::setThemeSearchPaths(searchPaths);
135  }
136  }
137  } else {
138  if (qEnvironmentVariableIsSet("ICON_THEME_SEARCH_PATH")) {
139  QString path;
140  path.append(qgetenv("ICON_THEME_SEARCH_PATH"));
141  QIcon::setThemeSearchPaths(QStringList({ path, QStringLiteral(":/icons") }));
142  } else {
143  QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << QStringLiteral("../share/icons") << QStringLiteral(":/icons"));
144  }
145  if (qEnvironmentVariableIsSet("ICON_THEME")) {
146  QString themeName;
147  themeName.append(qgetenv("ICON_THEME"));
148  QIcon::setThemeName(themeName);
149  }
150  }
151 #ifdef Q_OS_WIN32
152  // default configuration under Windows
153  if (QIcon::themeName().isEmpty()) {
154  QIcon::setThemeName(QStringLiteral("default"));
155  }
156 #endif
157  if (m_fontArg.isPresent()) {
158  QFont font;
159  font.setFamily(QString::fromLocal8Bit(m_fontArg.values().front()));
160  try {
161  font.setPointSize(stringToNumber<int>(m_fontArg.values().back()));
162  } catch (const ConversionException &) {
163  cerr << Phrases::Warning << "The specified font size is no number and will be ignored." << Phrases::EndFlush;
164  }
165  QGuiApplication::setFont(font);
166  }
167 #ifdef Q_OS_WIN32
168  else if (!preventApplyingDefaultFont) {
169  QGuiApplication::setFont(QFont(QStringLiteral("Segoe UI"), 9));
170  }
171 #else
172  CPP_UTILITIES_UNUSED(preventApplyingDefaultFont)
173 #endif
174  if (m_libraryPathsArg.isPresent()) {
175  QStringList libraryPaths;
176  libraryPaths.reserve(m_libraryPathsArg.values().size());
177  for (const auto &path : m_libraryPathsArg.values()) {
178  libraryPaths << QString::fromLocal8Bit(path);
179  }
180  QCoreApplication::setLibraryPaths(libraryPaths);
181  }
182  if (m_sceneGraphRenderLoopArg.isPresent()) {
183  qputenv(m_sceneGraphRenderLoopArg.environmentVariable(), QByteArray(m_sceneGraphRenderLoopArg.firstValue()));
184  }
185 }
186 } // namespace CppUtilities
CppUtilities
The CppUtilities namespace contains addons to the c++utilities library provided by the qtutilities li...
Definition: qtconfigarguments.h:19
qtconfigarguments.h
CppUtilities::QtConfigArguments::applySettings
void applySettings(bool preventApplyingDefaultFont=false) const
Applies the settings from the arguments.
Definition: qtconfigarguments.cpp:109