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