diff --git a/src/mvvmcore/message.cpp b/src/mvvmcore/message.cpp index b694809..2d353c4 100644 --- a/src/mvvmcore/message.cpp +++ b/src/mvvmcore/message.cpp @@ -190,6 +190,8 @@ MessageConfig &MessageConfig::resetButtons() } else if(d->type == TypeInputDialog || d->type == TypeColorDialog) d->buttons = Ok | Cancel; + else if(d->type == TypeProgressDialog) + d->buttons = Cancel; else d->buttons = Ok; @@ -733,53 +735,55 @@ void QtMvvm::getColor(const std::function &onResult, const QStrin getColor(CoreApp::instance(), onResult, title, color, argb); } -MessageResult *QtMvvm::showProgress(const QString &title, const QString &label, ProgressControl *control, bool allowCancel, bool isBusy) +MessageResult *QtMvvm::showProgress(const QString &title, const QString &label, ProgressControl *control, bool allowCancel, bool isBusy, const QString &cancelText) { MessageConfig config(MessageConfig::TypeProgressDialog, isBusy ? MessageConfig::SubTypeBusy : MessageConfig::SubTypeProgress); config.setTitle(title); config.setText(label); config.setDefaultValue(QVariant::fromValue(control)); config.setButtons(allowCancel ? MessageConfig::Cancel : MessageConfig::NoButton); + if(allowCancel && !cancelText.isNull()) + config.setButtonText(MessageConfig::Cancel, cancelText); return CoreApp::showDialog(config); } -ProgressControl *QtMvvm::showProgress(QObject *scope, const QString &title, const QString &label, int maximum, int minimum, bool allowCancel, int value) +ProgressControl *QtMvvm::showProgress(QObject *scope, const QString &title, const QString &label, int maximum, int minimum, bool allowCancel, int value, const QString &cancelText) { auto control = new ProgressControl{scope}; control->setMaximum(maximum); control->setMinimum(minimum); control->setProgress(value); - showProgress(title, label, control, allowCancel, false); + showProgress(title, label, control, allowCancel, false, cancelText); return control; } -ProgressControl *QtMvvm::showProgress(const QString &title, const QString &label, int maximum, int minimum, bool allowCancel, int value) +ProgressControl *QtMvvm::showProgress(const QString &title, const QString &label, int maximum, int minimum, bool allowCancel, int value, const QString &cancelText) { - return showProgress(nullptr, title, label, maximum, minimum, allowCancel, value); + return showProgress(nullptr, title, label, maximum, minimum, allowCancel, value, cancelText); } -ProgressControl *QtMvvm::showIndeterminateProgress(QObject *scope, const QString &title, const QString &label, bool allowCancel) +ProgressControl *QtMvvm::showIndeterminateProgress(QObject *scope, const QString &title, const QString &label, bool allowCancel, const QString &cancelText) { auto control = new ProgressControl{scope}; control->setIndeterminate(true); - showProgress(title, label, control, allowCancel, false); + showProgress(title, label, control, allowCancel, false, cancelText); return control; } -ProgressControl *QtMvvm::showIndeterminateProgress(const QString &title, const QString &label, bool allowCancel) +ProgressControl *QtMvvm::showIndeterminateProgress(const QString &title, const QString &label, bool allowCancel, const QString &cancelText) { - return showIndeterminateProgress(nullptr, title, label, allowCancel); + return showIndeterminateProgress(nullptr, title, label, allowCancel, cancelText); } -ProgressControl *QtMvvm::showBusy(QObject *scope, const QString &title, const QString &label, bool allowCancel) +ProgressControl *QtMvvm::showBusy(QObject *scope, const QString &title, const QString &label, bool allowCancel, const QString &cancelText) { auto control = new ProgressControl{scope}; control->setIndeterminate(true); - showProgress(title, label, control, allowCancel, true); + showProgress(title, label, control, allowCancel, true, cancelText); return control; } -ProgressControl *QtMvvm::showBusy(const QString &title, const QString &label, bool allowCancel) +ProgressControl *QtMvvm::showBusy(const QString &title, const QString &label, bool allowCancel, const QString &cancelText) { - return showBusy(nullptr, title, label, allowCancel); + return showBusy(nullptr, title, label, allowCancel, cancelText); } diff --git a/src/mvvmcore/message.h b/src/mvvmcore/message.h index 51bf748..5c8b053 100644 --- a/src/mvvmcore/message.h +++ b/src/mvvmcore/message.h @@ -530,34 +530,41 @@ Q_MVVMCORE_EXPORT MessageResult *showProgress(const QString &title, const QString &label, ProgressControl *control, bool allowCancel = true, - bool isBusy = false); + bool isBusy = false, + const QString &cancelText = {}); Q_MVVMCORE_EXPORT ProgressControl *showProgress(QObject *scope, const QString &title = {}, const QString &label = {}, int maximum = 100, int minimum = 0, bool allowCancel = true, - int value = 0); + int value = 0, + const QString &cancelText = {}); Q_MVVMCORE_EXPORT ProgressControl *showProgress(const QString &title = {}, const QString &label = {}, int maximum = 100, int minimum = 0, bool allowCancel = true, - int value = 0); + int value = 0, + const QString &cancelText = {}); Q_MVVMCORE_EXPORT ProgressControl *showIndeterminateProgress(QObject *scope, const QString &title = {}, const QString &label = {}, - bool allowCancel = true); + bool allowCancel = true, + const QString &cancelText = {}); Q_MVVMCORE_EXPORT ProgressControl *showIndeterminateProgress(const QString &title = {}, const QString &label = {}, - bool allowCancel = true); + bool allowCancel = true, + const QString &cancelText = {}); Q_MVVMCORE_EXPORT ProgressControl *showBusy(QObject *scope, const QString &title = {}, const QString &label = {}, - bool allowCancel = true); + bool allowCancel = true, + const QString &cancelText = {}); Q_MVVMCORE_EXPORT ProgressControl *showBusy(const QString &title = {}, const QString &label = {}, - bool allowCancel = true); + bool allowCancel = true, + const QString &cancelText = {}); } diff --git a/tests/auto/mvvmcore/coreapp/tst_coreapp.cpp b/tests/auto/mvvmcore/coreapp/tst_coreapp.cpp index 43559eb..d43adff 100644 --- a/tests/auto/mvvmcore/coreapp/tst_coreapp.cpp +++ b/tests/auto/mvvmcore/coreapp/tst_coreapp.cpp @@ -30,14 +30,13 @@ private Q_SLOTS: void testPresentDialog(); void testPresentMessage_data(); void testPresentMessage(); - void testAboutMessage(); - void testProgressMessage(); Q_SIGNALS: void msgUnblock(bool success); private: bool wasClosed = false; + QPointer lastProgress; Q_INVOKABLE void doCloseTest(); }; @@ -505,6 +504,20 @@ void CoreAppTest::testPresentMessage_data() << static_cast(MessageConfig::Ok) << QVariant{}; + QTest::newRow("msgbox.about") << MsgFn{[this](){ + auto res = about({}); + connect(res, &MessageResult::dialogDone, + this, [this](MessageConfig::StandardButton btn){ + QCOMPARE(btn, MessageConfig::Ok); + emit msgUnblock(true); + }); + }} << MessageConfig{MessageConfig::TypeMessageBox, MessageConfig::SubTypeAbout} + .setTitle(QStringLiteral("%1 — Version %2") + .arg(QGuiApplication::applicationDisplayName(), QCoreApplication::applicationVersion())) + .setViewProperty(QStringLiteral("addQtVersion"), true) + << static_cast(MessageConfig::Ok) + << QVariant{}; + QTest::newRow("input") << MsgFn{[this](){ getInput(QStringLiteral("title"), QStringLiteral("text"), @@ -591,6 +604,87 @@ void CoreAppTest::testPresentMessage_data() .setDefaultValue(QColor{0x12, 0x34, 0x56, 0x78}) << static_cast(MessageConfig::Ok) << QVariant{QColor{0xab, 0xcd, 0xef, 0x01}}; + + QTest::newRow("progress.progress") << MsgFn{[this](){ + lastProgress = showProgress(this, + QStringLiteral("title"), + QStringLiteral("label"), + 42, 24, true, 33, + QStringLiteral("break")); + QVERIFY(lastProgress->autoDelete()); + QVERIFY(!lastProgress->isIndeterminate()); + QCOMPARE(lastProgress->minimum(), 24); + QCOMPARE(lastProgress->maximum(), 42); + QCOMPARE(lastProgress->progress(), 33); + // triggered from gui in core + QString called; + connect(lastProgress, &ProgressControl::changeLabel, + this, [&](QString label){ + called = std::move(label); + }, Qt::DirectConnection); + lastProgress->updateLabel(QStringLiteral("update")); + QCOMPARE(called, QStringLiteral("update")); + + connect(lastProgress, &ProgressControl::closed, + this, [this](){ + emit msgUnblock(true); + }, Qt::QueuedConnection); + // triggered from core in gui + connect(lastProgress, &ProgressControl::closeRequested, + this, [this](){ + lastProgress->notifyClosed(); + }, Qt::QueuedConnection); + // triggered from gui in core + connect(lastProgress, &ProgressControl::canceled, + lastProgress, [this](MessageConfig::StandardButton btn) { + QCOMPARE(btn, MessageConfig::Cancel); + lastProgress->close(); + }, Qt::QueuedConnection); + }} << MessageConfig{MessageConfig::TypeProgressDialog, MessageConfig::SubTypeProgress} + .setTitle(QStringLiteral("title")) + .setText(QStringLiteral("label")) + .setButtonText(MessageConfig::Cancel, QStringLiteral("break")) + << static_cast(MessageConfig::Cancel) + << QVariant{}; + + QTest::newRow("progress.indeterminate") << MsgFn{[this](){ + lastProgress = showIndeterminateProgress(this, + QStringLiteral("title"), + QStringLiteral("label"), + false); + QVERIFY(lastProgress->autoDelete()); + QVERIFY(lastProgress->isIndeterminate()); + // triggered from gui in core + connect(lastProgress, &ProgressControl::closed, + this, [this](){ + emit msgUnblock(true); + }, Qt::QueuedConnection); + }} << MessageConfig{MessageConfig::TypeProgressDialog, MessageConfig::SubTypeProgress} + .setTitle(QStringLiteral("title")) + .setText(QStringLiteral("label")) + .setButtons(MessageConfig::NoButton) + << static_cast(MessageConfig::NoButton) + << QVariant{}; + + QTest::newRow("progress.busy") << MsgFn{[this](){ + lastProgress = showBusy(this, + QStringLiteral("title"), + QStringLiteral("label"), + false, + QStringLiteral("break")); + QVERIFY(lastProgress->autoDelete()); + QVERIFY(lastProgress->isIndeterminate()); + // triggered from gui in core + connect(lastProgress, &ProgressControl::closed, + this, [this](){ + emit msgUnblock(true); + }, Qt::QueuedConnection); + }} << MessageConfig{MessageConfig::TypeProgressDialog, MessageConfig::SubTypeBusy} + .setTitle(QStringLiteral("title")) + .setText(QStringLiteral("label")) + .setButtons(MessageConfig::NoButton) + << static_cast(MessageConfig::NoButton) + << QVariant{}; } void CoreAppTest::testPresentMessage() @@ -613,11 +707,19 @@ void CoreAppTest::testPresentMessage() auto conf = std::get<0>(presenter->dialogs[0]); auto res = std::get<1>(presenter->dialogs[0]); + bool isProgress = false; + if(config.type() == MessageConfig::TypeProgressDialog && + !config.defaultValue().isValid()) { + isProgress = true; + config.setDefaultValue(QVariant::fromValue(lastProgress)); + } + // test message conf QCOMPARE(conf.type(), config.type()); QCOMPARE(conf.subType(), config.subType()); QCOMPARE(conf.title(), config.title()); - QCOMPARE(conf.text(), config.text()); + if(conf.subType() != MessageConfig::SubTypeAbout) + QCOMPARE(conf.text(), config.text()); QCOMPARE(conf.buttons(), config.buttons()); QCOMPARE(conf.buttonTexts(), config.buttonTexts()); QCOMPARE(conf.defaultValue(), config.defaultValue()); @@ -625,22 +727,18 @@ void CoreAppTest::testPresentMessage() //complete it QSignalSpy unblockSpy{this, &CoreAppTest::msgUnblock}; - res->complete(static_cast(btn), result); + if(isProgress){ + if(config.buttons().testFlag(MessageConfig::Cancel)) + lastProgress->requestCancel(MessageConfig::Cancel); + else + lastProgress->notifyClosed(); + } else + res->complete(static_cast(btn), result); QVERIFY(unblockSpy.wait()); QCOMPARE(unblockSpy.size(), 1); QVERIFY(unblockSpy.takeFirst()[0].toBool()); } -void CoreAppTest::testAboutMessage() -{ - Q_UNIMPLEMENTED(); -} - -void CoreAppTest::testProgressMessage() -{ - Q_UNIMPLEMENTED(); -} - void CoreAppTest::doCloseTest() { wasClosed = true;