Qt Utilities  5.12.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 ConversionUtilities;
23 using namespace EscapeCodes;
24 
25 namespace ApplicationUtilities {
26 
31 QtConfigArguments::QtConfigArguments()
32  : m_qtWidgetsGuiArg("qt-widgets-gui", 'g', "shows a Qt widgets based graphical user interface")
33  , m_qtQuickGuiArg("qt-quick-gui", 'q', "shows a Qt quick based graphical user interface")
34  , m_lngArg("lang", 'l', "sets the language for the Qt GUI")
35  , m_qmlDebuggerArg("qmljsdebugger", 'q',
36  "enables QML debugging (see "
37  "http://doc.qt.io/qt-5/"
38  "qtquick-debugging.html)")
39  , m_styleArg("style", '\0', "sets the Qt Widgets or Qt Quick style")
40  , m_iconThemeArg("icon-theme", '\0',
41  "sets the icon theme and additional "
42  "theme search paths for the Qt GUI")
43  , m_fontArg("font", '\0', "sets the font family and size (point) for the Qt GUI")
44  , m_libraryPathsArg("library-paths", '\0',
45  "sets the list of directories to search when loading "
46  "libraries (all existing paths will be deleted)")
47  , m_platformThemeArg("platformtheme", '\0', "specifies the Qt platform theme to be used")
48 {
49  // language
50  m_lngArg.setValueNames({ "language" });
51  m_lngArg.setRequiredValueCount(1);
52  m_lngArg.setRequired(false);
53  m_lngArg.setCombinable(true);
54  // qml debugger (handled by Qt, just to let the parser know of it)
55  m_qmlDebuggerArg.setValueNames({ "port:<port_from>[,port_to][,host:<ip address>][,block]" });
56  m_qmlDebuggerArg.setRequiredValueCount(1);
57  m_qmlDebuggerArg.setCombinable(true);
58  // appearance
59  m_styleArg.setValueNames({ "breeze/cleanlooks/fusion/kvantum/oxygen/adwaita/windows/... or default/material/universal/org.kde.desktop" });
60  m_styleArg.setRequiredValueCount(1);
61  m_styleArg.setCombinable(true);
62  m_styleArg.setEnvironmentVariable("QT_STYLE_OVERRIDE for Qt Widgets and QT_QUICK_CONTROLS_STYLE for Qt Quick");
63  m_iconThemeArg.setValueNames({ "theme name", "search path 1", "search path 2" });
64  m_iconThemeArg.setRequiredValueCount(Argument::varValueCount);
65  m_iconThemeArg.setCombinable(true);
66  m_iconThemeArg.setEnvironmentVariable("ICON_THEME_SEARCH_PATH and ICON_THEME");
67  m_fontArg.setValueNames({ "name", "size" });
68  m_fontArg.setRequiredValueCount(2);
69  m_fontArg.setCombinable(true);
70  m_libraryPathsArg.setValueNames({ "path 1", "path 2" });
71  m_libraryPathsArg.setRequiredValueCount(Argument::varValueCount);
72  m_libraryPathsArg.setCombinable(true);
73  m_platformThemeArg.setRequiredValueCount(1);
74  m_platformThemeArg.setCombinable(true);
75  m_platformThemeArg.setValueNames({ "qt5ct/kde/..." });
76  m_platformThemeArg.setPreDefinedCompletionValues("qt5ct kde gnome");
77  m_platformThemeArg.setEnvironmentVariable("QT_QPA_PLATFORMTHEME");
78  m_qtWidgetsGuiArg.setSubArguments(
79  { &m_lngArg, &m_qmlDebuggerArg, &m_styleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
80  m_qtQuickGuiArg.setSubArguments(
81  { &m_lngArg, &m_qmlDebuggerArg, &m_styleArg, &m_iconThemeArg, &m_fontArg, &m_libraryPathsArg, &m_platformThemeArg });
82  m_qtWidgetsGuiArg.setDenotesOperation(true);
83  m_qtQuickGuiArg.setDenotesOperation(true);
84 #if defined QT_UTILITIES_GUI_QTWIDGETS
85  m_qtWidgetsGuiArg.setImplicit(true);
86 #elif defined QT_UTILITIES_GUI_QTQUICK
87  m_qtQuickGuiArg.setImplicit(true);
88 #endif
89 }
90 
97 void QtConfigArguments::applySettings(bool preventApplyingDefaultFont) const
98 {
99  if (m_lngArg.isPresent()) {
100  QLocale::setDefault(QLocale(QString::fromLocal8Bit(m_lngArg.values().front())));
101  }
102  if (m_styleArg.isPresent()) {
103 #ifdef QT_UTILITIES_GUI_QTWIDGETS
104  if (m_qtWidgetsGuiArg.isPresent()) {
105  if (QStyle *const style = QStyleFactory::create(QString::fromLocal8Bit(m_styleArg.values().front()))) {
106  QApplication::setStyle(style);
107  } else {
108  cerr << Phrases::Warning << "Can not find the specified Qt Widgets style." << Phrases::EndFlush;
109  }
110  }
111 #endif
112  }
113  if (m_iconThemeArg.isPresent()) {
114  auto i = m_iconThemeArg.values().cbegin(), end = m_iconThemeArg.values().end();
115  if (i != end) {
116  QIcon::setThemeName(QString::fromLocal8Bit(*i));
117  if (++i != end) {
118  QStringList searchPaths;
119  searchPaths.reserve(m_iconThemeArg.values().size() - 1);
120  for (; i != end; ++i) {
121  searchPaths << QString::fromLocal8Bit(*i);
122  }
123  searchPaths << QStringLiteral(":/icons");
124  QIcon::setThemeSearchPaths(searchPaths);
125  }
126  }
127  } else {
128  if (qEnvironmentVariableIsSet("ICON_THEME_SEARCH_PATH")) {
129  QString path;
130  path.append(qgetenv("ICON_THEME_SEARCH_PATH"));
131  QIcon::setThemeSearchPaths(QStringList({ path, QStringLiteral(":/icons") }));
132  } else {
133  QIcon::setThemeSearchPaths(QIcon::themeSearchPaths() << QStringLiteral("../share/icons") << QStringLiteral(":/icons"));
134  }
135  if (qEnvironmentVariableIsSet("ICON_THEME")) {
136  QString themeName;
137  themeName.append(qgetenv("ICON_THEME"));
138  QIcon::setThemeName(themeName);
139  }
140  }
141 #ifdef Q_OS_WIN32
142  // default configuration under Windows
143  if (QIcon::themeName().isEmpty()) {
144  QIcon::setThemeName(QStringLiteral("default"));
145  }
146 #endif
147  if (m_fontArg.isPresent()) {
148  QFont font;
149  font.setFamily(QString::fromLocal8Bit(m_fontArg.values().front()));
150  try {
151  font.setPointSize(stringToNumber<int>(m_fontArg.values().back()));
152  } catch (const ConversionException &) {
153  cerr << Phrases::Warning << "The specified font size is no number and will be ignored." << Phrases::EndFlush;
154  }
155  QGuiApplication::setFont(font);
156  }
157 #ifdef Q_OS_WIN32
158  else if (!preventApplyingDefaultFont) {
159  QGuiApplication::setFont(QFont(QStringLiteral("Segoe UI"), 9));
160  }
161 #else
162  VAR_UNUSED(preventApplyingDefaultFont)
163 #endif
164  if (m_libraryPathsArg.isPresent()) {
165  QStringList libraryPaths;
166  libraryPaths.reserve(m_libraryPathsArg.values().size());
167  for (const auto &path : m_libraryPathsArg.values()) {
168  libraryPaths << QString::fromLocal8Bit(path);
169  }
170  QCoreApplication::setLibraryPaths(libraryPaths);
171  }
172 }
173 } // namespace ApplicationUtilities
void applySettings(bool preventApplyingDefaultFont=false) const
Applies the settings from the arguments.
STL namespace.