on xenial help sign in
Move handling of command line options to a separate class

Therefore leaving main.cpp less cluttered. Approved by: Albert Astals Cid

commit153c2106e13697489a824a92c00659bc8d2aee6f authorDaniel d'Andrada <daniel.dandrada@canonical.com> dateWed 22 Apr 2015, 14:45 (11 years ago) parent7b417f91efe4de
6 files changed+180−69
src/ApplicationArguments.h+2−3 src/CMakeLists.txt+1 src/Dash/main.cpp+1−1 src/UnityCommandLineParser.cpp+118 src/UnityCommandLineParser.h+50 src/main.cpp+8−65
src/ApplicationArguments.h+2−3
@@ -29,9 +29,8 @@ class ApplicationArguments : public QObject
2929 Q_OBJECT
3030public:
3131 // Not exposed to the app as setSize isn't invokable
32− void setSize(int width, int height) {
33− m_size.rwidth() = width;
34− m_size.rheight() = height;
32+ void setSize(const QSize &size) {
33+ m_size = size;
3534 }
3635
3736 Q_INVOKABLE bool hasGeometry() const { return m_size.isValid(); }
src/CMakeLists.txt+1
@@ -21,6 +21,7 @@ add_executable(${SHELL_APP}
2121 main.cpp
2222 MouseTouchAdaptor.cpp
2323 CachingNetworkManagerFactory.cpp
24+ UnityCommandLineParser.cpp
2425 ${QML_FILES} # This is to make qml and image files appear in the IDE's project tree
2526)
2627
src/Dash/main.cpp+1−1
@@ -65,7 +65,7 @@ int main(int argc, const char *argv[])
6565 parser.value(windowGeometryOption).split('x').size() == 2)
6666 {
6767 QStringList geom = parser.value(windowGeometryOption).split('x');
68− qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
68+ qmlArgs.setSize(QSize(geom.at(0).toInt(), geom.at(1).toInt()));
6969 }
7070
7171 if (getenv("QT_LOAD_TESTABILITY")) {
src/UnityCommandLineParser.cppnew file+118
@@ -0,0 +1,118 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#include "UnityCommandLineParser.h"
18+
19+#include <QDebug>
20+
21+#define ENV_GRID_UNIT_PX "GRID_UNIT_PX"
22+#define DEFAULT_GRID_UNIT_PX 8
23+
24+UnityCommandLineParser::UnityCommandLineParser(const QCoreApplication &app)
25+{
26+ m_gridUnit = getenvFloat(ENV_GRID_UNIT_PX, DEFAULT_GRID_UNIT_PX);
27+
28+ QCommandLineParser parser;
29+ parser.setApplicationDescription("Description: Unity 8 Shell");
30+ parser.addHelpOption();
31+
32+ QCommandLineOption fullscreenOption("fullscreen",
33+ "Run in fullscreen");
34+ parser.addOption(fullscreenOption);
35+
36+ QCommandLineOption framelessOption("frameless",
37+ "Run without window borders");
38+ parser.addOption(framelessOption);
39+
40+ QCommandLineOption mousetouchOption("mousetouch",
41+ "Allow the mouse to provide touch input");
42+ parser.addOption(mousetouchOption);
43+
44+ QCommandLineOption windowGeometryOption(QStringList() << "windowgeometry",
45+ "Specify the window geometry as [<width>x<height>]", "windowgeometry", "1");
46+ parser.addOption(windowGeometryOption);
47+
48+ QCommandLineOption testabilityOption("testability",
49+ "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead.\nLoad the testability driver");
50+ parser.addOption(testabilityOption);
51+
52+ QCommandLineOption modeOption("mode",
53+ "Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]",
54+ "mode", "full-greeter");
55+ parser.addOption(modeOption);
56+
57+ // Treat args with single dashes the same as arguments with two dashes
58+ // Ex: -fullscreen == --fullscreen
59+ parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
60+
61+ parser.process(app);
62+
63+ if (parser.isSet(windowGeometryOption))
64+ {
65+ QStringList geom = parser.value(windowGeometryOption).split('x');
66+ if (geom.count() == 2) {
67+ m_windowGeometry.rwidth() = parsePixelsValue(geom[0]);
68+ m_windowGeometry.rheight() = parsePixelsValue(geom[1]);
69+ }
70+ }
71+
72+ m_hasTestability = parser.isSet(testabilityOption);
73+ m_hasFrameless = parser.isSet(framelessOption);
74+ m_hasMouseToTouch = parser.isSet(mousetouchOption);
75+ m_hasFullscreen = parser.isSet(fullscreenOption);
76+ resolveMode(parser, modeOption);
77+}
78+
79+int UnityCommandLineParser::parsePixelsValue(const QString &str)
80+{
81+ if (str.endsWith("gu", Qt::CaseInsensitive)) {
82+ QString numStr = str;
83+ numStr.remove(numStr.size() - 2, 2);
84+ return numStr.toInt() * m_gridUnit;
85+ } else {
86+ return str.toInt();
87+ }
88+}
89+
90+float UnityCommandLineParser::getenvFloat(const char* name, float defaultValue)
91+{
92+ QByteArray stringValue = qgetenv(name);
93+ bool ok;
94+ float value = stringValue.toFloat(&ok);
95+ return ok ? value : defaultValue;
96+}
97+
98+void UnityCommandLineParser::resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption)
99+{
100+ // If an invalid option was specified, set it to the default
101+ // If no default was provided in the QCommandLineOption constructor, abort.
102+ if (!parser.isSet(modeOption) ||
103+ (parser.value(modeOption) != "full-greeter" &&
104+ parser.value(modeOption) != "full-shell" &&
105+ parser.value(modeOption) != "greeter" &&
106+ parser.value(modeOption) != "shell")) {
107+
108+ if (modeOption.defaultValues().first() != nullptr) {
109+ m_mode = modeOption.defaultValues().first();
110+ qWarning() << "Mode argument was not provided or was set to an illegal value."
111+ " Using default value of --mode=" << m_mode;
112+ } else {
113+ qFatal("Shell mode argument was not provided and there is no default mode.");
114+ }
115+ } else {
116+ m_mode = parser.value(modeOption);
117+ }
118+}
src/UnityCommandLineParser.hnew file+50
@@ -0,0 +1,50 @@
1+/*
2+ * Copyright (C) 2015 Canonical, Ltd.
3+ *
4+ * This program is free software; you can redistribute it and/or modify
5+ * it under the terms of the GNU General Public License as published by
6+ * the Free Software Foundation; version 3.
7+ *
8+ * This program is distributed in the hope that it will be useful,
9+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
10+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+ * GNU General Public License for more details.
12+ *
13+ * You should have received a copy of the GNU General Public License
14+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
15+ */
16+
17+#ifndef UNITY_COMMAND_LINE_PARSER_H
18+#define UNITY_COMMAND_LINE_PARSER_H
19+
20+#include <QCommandLineParser>
21+#include <QSize>
22+#include <QString>
23+
24+class UnityCommandLineParser {
25+public:
26+ UnityCommandLineParser(const QCoreApplication &app);
27+
28+ QSize windowGeometry() const { return m_windowGeometry; }
29+ bool hasTestability() const { return m_hasTestability; }
30+ bool hasFrameless() const { return m_hasFrameless; }
31+ bool hasMouseToTouch() const { return m_hasMouseToTouch; }
32+ bool hasFullscreen() const { return m_hasFullscreen; }
33+ QString mode() const { return m_mode; }
34+private:
35+
36+ int parsePixelsValue(const QString &str);
37+ static float getenvFloat(const char* name, float defaultValue);
38+ void resolveMode(QCommandLineParser &parser, QCommandLineOption &modeOption);
39+
40+ float m_gridUnit;
41+
42+ QSize m_windowGeometry;
43+ bool m_hasTestability;
44+ bool m_hasFrameless;
45+ bool m_hasMouseToTouch;
46+ bool m_hasFullscreen;
47+ QString m_mode;
48+};
49+
50+#endif // UNITY_COMMAND_LINE_PARSER_H
src/main.cpp+8−65
@@ -30,6 +30,7 @@
3030#include "MouseTouchAdaptor.h"
3131#include "ApplicationArguments.h"
3232#include "CachingNetworkManagerFactory.h"
33+#include "UnityCommandLineParser.h"
3334
3435// Ubuntu Gestures
3536#include <TouchRegistry.h>
@@ -45,42 +46,9 @@ int main(int argc, const char *argv[])
4546 QGuiApplication::setApplicationName("unity8");
4647 QGuiApplication *application;
4748
48− QCommandLineParser parser;
49− parser.setApplicationDescription("Description: Unity 8 Shell");
50− parser.addHelpOption();
51−
52− QCommandLineOption fullscreenOption("fullscreen",
53− "Run in fullscreen");
54− parser.addOption(fullscreenOption);
55−
56− QCommandLineOption framelessOption("frameless",
57− "Run without window borders");
58− parser.addOption(framelessOption);
59−
60− QCommandLineOption mousetouchOption("mousetouch",
61− "Allow the mouse to provide touch input");
62− parser.addOption(mousetouchOption);
63−
64− QCommandLineOption windowGeometryOption(QStringList() << "windowgeometry",
65− "Specify the window geometry as [<width>x<height>]", "windowgeometry", "1");
66− parser.addOption(windowGeometryOption);
67−
68− QCommandLineOption testabilityOption("testability",
69− "DISCOURAGED: Please set QT_LOAD_TESTABILITY instead. \n \
70−Load the testability driver");
71− parser.addOption(testabilityOption);
72−
73− QCommandLineOption modeOption("mode",
74− "Whether to run greeter and/or shell [full-greeter, full-shell, greeter, shell]",
75− "mode", "full-greeter");
76− parser.addOption(modeOption);
77−
7849 application = new QGuiApplication(argc, (char**)argv);
7950
80− // Treat args with single dashes the same as arguments with two dashes
81− // Ex: -fullscreen == --fullscreen
82− parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);
83− parser.process(*application);
51+ UnityCommandLineParser parser(*application);
8452
8553 QString indicatorProfile = qgetenv("UNITY_INDICATOR_PROFILE");
8654 if (indicatorProfile.isEmpty()) {
@@ -88,36 +56,11 @@ Load the testability driver");
8856 }
8957
9058 ApplicationArguments qmlArgs;
91− if (parser.isSet(windowGeometryOption) &&
92− parser.value(windowGeometryOption).split('x').size() == 2)
93− {
94− QStringList geom = parser.value(windowGeometryOption).split('x');
95− qmlArgs.setSize(geom.at(0).toInt(), geom.at(1).toInt());
96− }
97−
98− // If an invalid option was specified, set it to the default
99− // If no default was provided in the QCommandLineOption constructor, abort.
100− QString shellMode;
101− if(!parser.isSet(modeOption) ||
102− (parser.value(modeOption) != "full-greeter" &&
103− parser.value(modeOption) != "full-shell" &&
104− parser.value(modeOption) != "greeter" &&
105− parser.value(modeOption) != "shell"))
106− {
107− if (modeOption.defaultValues().first() != nullptr) {
108− shellMode = modeOption.defaultValues().first();
109− qWarning() << "Mode argument was not provided or was set to an illegal value. Using default value of --mode=" << shellMode;
110− } else {
111− qFatal("Shell mode argument was not provided and there is no default mode,");
112− }
113−
114− } else {
115− shellMode = parser.value(modeOption);
116− }
59+ qmlArgs.setSize(parser.windowGeometry());
11760
11861 // The testability driver is only loaded by QApplication but not by QGuiApplication.
11962 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
120− if (parser.isSet(testabilityOption) || getenv("QT_LOAD_TESTABILITY")) {
63+ if (parser.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
12164 QLibrary testLib(QLatin1String("qttestability"));
12265 if (testLib.load()) {
12366 typedef void (*TasInitialize)(void);
@@ -142,8 +85,8 @@ Load the testability driver");
14285 view->engine()->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
14386 view->rootContext()->setContextProperty("applicationArguments", &qmlArgs);
14487 view->rootContext()->setContextProperty("indicatorProfile", indicatorProfile);
145− view->rootContext()->setContextProperty("shellMode", shellMode);
146− if (parser.isSet(framelessOption)) {
88+ view->rootContext()->setContextProperty("shellMode", parser.mode());
89+ if (parser.hasFrameless()) {
14790 view->setFlags(Qt::FramelessWindowHint);
14891 }
14992 TouchRegistry touchRegistry;
@@ -152,7 +95,7 @@ Load the testability driver");
15295 // You will need this if you want to interact with touch-only components using a mouse
15396 // Needed only when manually testing on a desktop.
15497 MouseTouchAdaptor *mouseTouchAdaptor = 0;
155− if (parser.isSet(mousetouchOption)) {
98+ if (parser.hasMouseToTouch()) {
15699 mouseTouchAdaptor = MouseTouchAdaptor::instance();
157100 }
158101
@@ -169,7 +112,7 @@ Load the testability driver");
169112 view->setSource(source);
170113 QObject::connect(view->engine(), SIGNAL(quit()), application, SLOT(quit()));
171114
172− if (isMirServer || parser.isSet(fullscreenOption)) {
115+ if (isMirServer || parser.hasFullscreen()) {
173116 view->showFullScreen();
174117 } else {
175118 view->show();