From 58da47ff69932c24974936f13f224fdb056d888d Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Mon, 26 Feb 2018 17:50:59 +0100 Subject: [PATCH] implemented cli version/help/error handling --- .../mvvmcore/SampleCore/samplecoreapp.cpp | 22 +++++++++++-- examples/mvvmcore/SampleCore/samplecoreapp.h | 16 ++++++++++ .../mvvmquick/SampleQuick/SampleQuick.pro | 32 ++----------------- examples/mvvmquick/SampleQuick/main.cpp | 6 ++-- src/imports/mvvmquick/PresentingStackView.qml | 3 +- src/mvvmcore/coreapp.cpp | 31 ++++++++++++++---- 6 files changed, 67 insertions(+), 43 deletions(-) diff --git a/examples/mvvmcore/SampleCore/samplecoreapp.cpp b/examples/mvvmcore/SampleCore/samplecoreapp.cpp index 22bd191..ba41a3a 100644 --- a/examples/mvvmcore/SampleCore/samplecoreapp.cpp +++ b/examples/mvvmcore/SampleCore/samplecoreapp.cpp @@ -1,16 +1,32 @@ #include "samplecoreapp.h" #include "sampleviewmodel.h" +#include "drawerviewmodel.h" #include #include SampleCoreApp::SampleCoreApp(QObject *parent) : - CoreApp(parent) + CoreApp(parent), + _showDrawer(false) { QCoreApplication::setApplicationVersion(QStringLiteral(QTMVVMCORE_VERSION_STR)); QCoreApplication::setOrganizationName(QStringLiteral("Skycoder42")); } +bool SampleCoreApp::showDrawer() const +{ + return _showDrawer; +} + +void SampleCoreApp::setShowDrawer(bool showDrawer) +{ + if (_showDrawer == showDrawer) + return; + + _showDrawer = showDrawer; + emit showDrawerChanged(_showDrawer); +} + void SampleCoreApp::performRegistrations() { Q_INIT_RESOURCE(sample_core); @@ -30,7 +46,7 @@ int SampleCoreApp::startApp(const QStringList &arguments) }); parser.addPositionalArgument(QStringLiteral("names"), QStringLiteral("A list of names, joined together as one string"), - QStringLiteral("[name...")); + QStringLiteral("[names...]")); if(!autoParse(parser, arguments)) return EXIT_SUCCESS; @@ -39,5 +55,7 @@ int SampleCoreApp::startApp(const QStringList &arguments) args.insert(SampleViewModel::KeyActive, parser.isSet(QStringLiteral("active"))); args.insert(SampleViewModel::KeyNames, parser.positionalArguments()); show(args); + if(_showDrawer) + show(); return EXIT_SUCCESS; } diff --git a/examples/mvvmcore/SampleCore/samplecoreapp.h b/examples/mvvmcore/SampleCore/samplecoreapp.h index 51cc8a5..e1cde99 100644 --- a/examples/mvvmcore/SampleCore/samplecoreapp.h +++ b/examples/mvvmcore/SampleCore/samplecoreapp.h @@ -7,12 +7,28 @@ class SampleCoreApp : public QtMvvm::CoreApp { Q_OBJECT + Q_PROPERTY(bool showDrawer READ showDrawer WRITE setShowDrawer NOTIFY showDrawerChanged) + public: SampleCoreApp(QObject *parent = nullptr); + bool showDrawer() const; + +public Q_SLOTS: + void setShowDrawer(bool showDrawer); + +Q_SIGNALS: + void showDrawerChanged(bool showDrawer); + protected: void performRegistrations() override; int startApp(const QStringList &arguments) override; + +private: + bool _showDrawer; }; +#undef coreApp +#define coreApp static_cast(QtMvvm::CoreApp::instance()) + #endif // SAMPLECOREAPP_H diff --git a/examples/mvvmquick/SampleQuick/SampleQuick.pro b/examples/mvvmquick/SampleQuick/SampleQuick.pro index b145764..6a5cc1d 100644 --- a/examples/mvvmquick/SampleQuick/SampleQuick.pro +++ b/examples/mvvmquick/SampleQuick/SampleQuick.pro @@ -37,37 +37,9 @@ else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PW else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../mvvmcore/SampleCore/debug/SampleCore.lib else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../mvvmcore/SampleCore/libSampleCore.a -#hacky code to make it possible to use the example from within a shadowed build +# thanks to qmlc files, only the import path must be fixed samples_in_build { - # first, create a fake qml imports dir - FAKEPATH = qml/de/skycoder42/QtMvvm/Quick - ORIGPATH = ../../../../../../../../qml/de/skycoder42/QtMvvm/Quick - QMLDEPPATH = $$PWD/../../../src/imports/mvvmquick - system($$QMAKE_MKDIR $$shell_quote($$shell_path($$FAKEPATH))) - - # next, symlink all "compiled" files (whole dir for core, as it has no qml files - build_symlink_target.target = create_qml_build_symlinks - build_symlink_target.commands += $$QMAKE_SYMBOLIC_LINK $$shell_path(../../../../../../../qml/de/skycoder42/QtMvvm/Core) $$shell_path(qml/de/skycoder42/QtMvvm/Core) \ - $$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/libdeclarative_mvvmquick.so) $$shell_path($$FAKEPATH/libdeclarative_mvvmquick.so) \ - $$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/plugins.qmltypes) $$shell_path($$FAKEPATH/plugins.qmltypes) \ - $$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/qmldir) $$shell_path($$FAKEPATH/qmldir) - QMAKE_EXTRA_TARGETS += build_symlink_target - - # next, prepare compiler to symlink all the qml files - qml_symlink_compiler.name = symlink ${QMAKE_FILE_IN} - qml_symlink_compiler.input = QML_MODULE_DEPFILES - qml_symlink_compiler.variable_out = QML_MODULE_DEPFILES_LINKS - qml_symlink_compiler.output = $$FAKEPATH/${QMAKE_FILE_BASE}.qml - qml_symlink_compiler.commands = $$QMAKE_SYMBOLIC_LINK $$QMLDEPPATH/${QMAKE_FILE_BASE}.qml ${QMAKE_FILE_OUT} - qml_symlink_compiler.depends = create_qml_build_symlinks - qml_symlink_compiler.CONFIG += no_link target_predeps - QMAKE_EXTRA_COMPILERS += qml_symlink_compiler - - #finally, add the files to make it work - QML_MODULE_DEPFILES += $$files($$QMLDEPPATH/*.qml) - - # also, add it as import path for QtCreator and the src files - QML_IMPORT_PATH = $$OUT_PWD/qml/ + QML_IMPORT_PATH = $$OUT_PWD/../../../qml DEFINES += QML_PATH=\\\"$$QML_IMPORT_PATH\\\" } diff --git a/examples/mvvmquick/SampleQuick/main.cpp b/examples/mvvmquick/SampleQuick/main.cpp index 2805ed1..5fc2820 100644 --- a/examples/mvvmquick/SampleQuick/main.cpp +++ b/examples/mvvmquick/SampleQuick/main.cpp @@ -29,6 +29,8 @@ int main(int argc, char *argv[]) QApplication app(argc, argv); qDebug() << QQuickStyle::availableStyles() << QQuickStyle::name(); + coreApp->setShowDrawer(true); + qmlRegisterUncreatableType("de.skycoder42.QtMvvm.Sample", 1, 0, "SampleViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType("de.skycoder42.QtMvvm.Sample", 1, 0, "ResultViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType("de.skycoder42.QtMvvm.Sample", 1, 0, "DrawerViewModel", QStringLiteral("ViewModels cannot be created")); @@ -45,9 +47,5 @@ int main(int argc, char *argv[]) if (engine.rootObjects().isEmpty()) return -1; - QObject::connect(coreApp, &QtMvvm::CoreApp::appStarted, coreApp, []() { - coreApp->show(); - }); - return app.exec(); } diff --git a/src/imports/mvvmquick/PresentingStackView.qml b/src/imports/mvvmquick/PresentingStackView.qml index caccad5..00585f4 100644 --- a/src/imports/mvvmquick/PresentingStackView.qml +++ b/src/imports/mvvmquick/PresentingStackView.qml @@ -24,7 +24,8 @@ StackView { } function closeAction() { - if(typeof _presenterStack.currentItem.closeAction == "function") { + if(_presenterStack.currentItem && + typeof _presenterStack.currentItem.closeAction == "function") { if(_presenterStack.currentItem.closeAction()) return true; } diff --git a/src/mvvmcore/coreapp.cpp b/src/mvvmcore/coreapp.cpp index 5073ef1..04a43a8 100644 --- a/src/mvvmcore/coreapp.cpp +++ b/src/mvvmcore/coreapp.cpp @@ -106,18 +106,37 @@ bool CoreApp::autoParse(QCommandLineParser &parser, const QStringList &arguments { if(parser.parse(arguments)) { if(parser.isSet(QStringLiteral("help"))) { - Q_UNIMPLEMENTED(); + MessageConfig helpConfig {MessageConfig::TypeMessageBox, MessageConfig::SubTypeQuestion}; + helpConfig.setTitle(tr("Help")); + helpConfig.setText(parser.helpText()); + helpConfig.setButtons(MessageConfig::Ok); + auto res = showDialog(helpConfig); + connect(res, &MessageResult::dialogDone, + qApp, &QCoreApplication::quit); return false; } else if(parser.isSet(QStringLiteral("version"))) { - auto text = QGuiApplication::applicationDisplayName() + - QLatin1Char(' ') + - QCoreApplication::applicationVersion(); - Q_UNIMPLEMENTED(); + MessageConfig versionConfig {MessageConfig::TypeMessageBox, MessageConfig::SubTypeInformation}; + versionConfig.setTitle(tr("Application Version")); + versionConfig.setText(QGuiApplication::applicationDisplayName() + + QLatin1Char(' ') + + QCoreApplication::applicationVersion()); + versionConfig.setButtons(MessageConfig::Ok); + auto res = showDialog(versionConfig); + connect(res, &MessageResult::dialogDone, + qApp, &QCoreApplication::quit); return false; } else return true; } else { - Q_UNIMPLEMENTED(); + MessageConfig errorConfig {MessageConfig::TypeMessageBox, MessageConfig::SubTypeCritical}; + errorConfig.setTitle(tr("Invalid Arguments")); + errorConfig.setText(parser.errorText()); + errorConfig.setButtons(MessageConfig::Ok); + auto res = showDialog(errorConfig); + connect(res, &MessageResult::dialogDone, + qApp, [](){ + qApp->exit(EXIT_FAILURE); + }); return false; } }