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