Browse Source

implemented cli version/help/error handling

pull/2/head
Skycoder42 7 years ago
parent
commit
58da47ff69
  1. 22
      examples/mvvmcore/SampleCore/samplecoreapp.cpp
  2. 16
      examples/mvvmcore/SampleCore/samplecoreapp.h
  3. 32
      examples/mvvmquick/SampleQuick/SampleQuick.pro
  4. 6
      examples/mvvmquick/SampleQuick/main.cpp
  5. 3
      src/imports/mvvmquick/PresentingStackView.qml
  6. 31
      src/mvvmcore/coreapp.cpp

22
examples/mvvmcore/SampleCore/samplecoreapp.cpp

@ -1,16 +1,32 @@
#include "samplecoreapp.h" #include "samplecoreapp.h"
#include "sampleviewmodel.h" #include "sampleviewmodel.h"
#include "drawerviewmodel.h"
#include <QtCore/QCommandLineParser> #include <QtCore/QCommandLineParser>
#include <QtMvvmCore/QtMvvmCoreVersion> #include <QtMvvmCore/QtMvvmCoreVersion>
SampleCoreApp::SampleCoreApp(QObject *parent) : SampleCoreApp::SampleCoreApp(QObject *parent) :
CoreApp(parent) CoreApp(parent),
_showDrawer(false)
{ {
QCoreApplication::setApplicationVersion(QStringLiteral(QTMVVMCORE_VERSION_STR)); QCoreApplication::setApplicationVersion(QStringLiteral(QTMVVMCORE_VERSION_STR));
QCoreApplication::setOrganizationName(QStringLiteral("Skycoder42")); 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() void SampleCoreApp::performRegistrations()
{ {
Q_INIT_RESOURCE(sample_core); Q_INIT_RESOURCE(sample_core);
@ -30,7 +46,7 @@ int SampleCoreApp::startApp(const QStringList &arguments)
}); });
parser.addPositionalArgument(QStringLiteral("names"), parser.addPositionalArgument(QStringLiteral("names"),
QStringLiteral("A list of names, joined together as one string"), QStringLiteral("A list of names, joined together as one string"),
QStringLiteral("[name...")); QStringLiteral("[names...]"));
if(!autoParse(parser, arguments)) if(!autoParse(parser, arguments))
return EXIT_SUCCESS; return EXIT_SUCCESS;
@ -39,5 +55,7 @@ int SampleCoreApp::startApp(const QStringList &arguments)
args.insert(SampleViewModel::KeyActive, parser.isSet(QStringLiteral("active"))); args.insert(SampleViewModel::KeyActive, parser.isSet(QStringLiteral("active")));
args.insert(SampleViewModel::KeyNames, parser.positionalArguments()); args.insert(SampleViewModel::KeyNames, parser.positionalArguments());
show<SampleViewModel>(args); show<SampleViewModel>(args);
if(_showDrawer)
show<DrawerViewModel>();
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

16
examples/mvvmcore/SampleCore/samplecoreapp.h

@ -7,12 +7,28 @@ class SampleCoreApp : public QtMvvm::CoreApp
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(bool showDrawer READ showDrawer WRITE setShowDrawer NOTIFY showDrawerChanged)
public: public:
SampleCoreApp(QObject *parent = nullptr); SampleCoreApp(QObject *parent = nullptr);
bool showDrawer() const;
public Q_SLOTS:
void setShowDrawer(bool showDrawer);
Q_SIGNALS:
void showDrawerChanged(bool showDrawer);
protected: protected:
void performRegistrations() override; void performRegistrations() override;
int startApp(const QStringList &arguments) override; int startApp(const QStringList &arguments) override;
private:
bool _showDrawer;
}; };
#undef coreApp
#define coreApp static_cast<SampleCoreApp*>(QtMvvm::CoreApp::instance())
#endif // SAMPLECOREAPP_H #endif // SAMPLECOREAPP_H

32
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: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 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 { samples_in_build {
# first, create a fake qml imports dir QML_IMPORT_PATH = $$OUT_PWD/../../../qml
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/
DEFINES += QML_PATH=\\\"$$QML_IMPORT_PATH\\\" DEFINES += QML_PATH=\\\"$$QML_IMPORT_PATH\\\"
} }

6
examples/mvvmquick/SampleQuick/main.cpp

@ -29,6 +29,8 @@ int main(int argc, char *argv[])
QApplication app(argc, argv); QApplication app(argc, argv);
qDebug() << QQuickStyle::availableStyles() << QQuickStyle::name(); qDebug() << QQuickStyle::availableStyles() << QQuickStyle::name();
coreApp->setShowDrawer(true);
qmlRegisterUncreatableType<SampleViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "SampleViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType<SampleViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "SampleViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<ResultViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "ResultViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType<ResultViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "ResultViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<DrawerViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "DrawerViewModel", QStringLiteral("ViewModels cannot be created")); qmlRegisterUncreatableType<DrawerViewModel>("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()) if (engine.rootObjects().isEmpty())
return -1; return -1;
QObject::connect(coreApp, &QtMvvm::CoreApp::appStarted, coreApp, []() {
coreApp->show<DrawerViewModel>();
});
return app.exec(); return app.exec();
} }

3
src/imports/mvvmquick/PresentingStackView.qml

@ -24,7 +24,8 @@ StackView {
} }
function closeAction() { function closeAction() {
if(typeof _presenterStack.currentItem.closeAction == "function") { if(_presenterStack.currentItem &&
typeof _presenterStack.currentItem.closeAction == "function") {
if(_presenterStack.currentItem.closeAction()) if(_presenterStack.currentItem.closeAction())
return true; return true;
} }

31
src/mvvmcore/coreapp.cpp

@ -106,18 +106,37 @@ bool CoreApp::autoParse(QCommandLineParser &parser, const QStringList &arguments
{ {
if(parser.parse(arguments)) { if(parser.parse(arguments)) {
if(parser.isSet(QStringLiteral("help"))) { 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; return false;
} else if(parser.isSet(QStringLiteral("version"))) { } else if(parser.isSet(QStringLiteral("version"))) {
auto text = QGuiApplication::applicationDisplayName() + MessageConfig versionConfig {MessageConfig::TypeMessageBox, MessageConfig::SubTypeInformation};
QLatin1Char(' ') + versionConfig.setTitle(tr("Application Version"));
QCoreApplication::applicationVersion(); versionConfig.setText(QGuiApplication::applicationDisplayName() +
Q_UNIMPLEMENTED(); QLatin1Char(' ') +
QCoreApplication::applicationVersion());
versionConfig.setButtons(MessageConfig::Ok);
auto res = showDialog(versionConfig);
connect(res, &MessageResult::dialogDone,
qApp, &QCoreApplication::quit);
return false; return false;
} else } else
return true; return true;
} else { } 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; return false;
} }
} }

Loading…
Cancel
Save