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