Skip to content
This repository was archived by the owner on Mar 4, 2023. It is now read-only.

Commit dd9c495

Browse files
committed
made MessageResult threadsafe
and completed message doc
1 parent 7f03b2d commit dd9c495

File tree

5 files changed

+240
-22
lines changed

5 files changed

+240
-22
lines changed

doc/message.dox

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,8 @@ to save content to it.
380380
@sa MessageConfig::type, MessageConfig::subType, MessageConfig::TypeFileDialog
381381
*/
382382

383+
384+
383385
/*!
384386
@class QtMvvm::MessageResult
385387

@@ -423,3 +425,212 @@ dialogDone(), as long as they are direct connected.
423425

424426
@sa MessageResult::complete, MessageResult::dialogDone
425427
*/
428+
429+
/*!
430+
@fn QtMvvm::MessageResult::setCloseTarget(QObject *, const QString &)
431+
432+
@param closeObject The object to call the method on
433+
@param closeMethod The method to be called
434+
435+
@attention This method should be used by the presenter only, not from the core
436+
437+
Use it from the presenter to register a view and a method on the view to be called to close
438+
the view. The `closeMethod` should be a paremeterless method signature (e.g. `close()`). Not
439+
settings a close target means that the dialog cannot be closed from code via discardMessage().
440+
441+
@sa MessageResult::discardMessage
442+
*/
443+
444+
/*!
445+
@fn QtMvvm::MessageResult::complete(QtMvvm::MessageConfig::StandardButton)
446+
447+
@param button The button that was pressed by the user
448+
449+
@attention This method should be used by the presenter only, not from the core
450+
451+
You must use this method from the GUI view code to mark a message result as completed as soon
452+
as the user completed the dialog. The `button` should be the button the user pressed, or
453+
MessageConfig::NoButton if close by another reason. The pressed button should be one that was
454+
in the list of buttons specified by the MessageConfig that created the dialog (with the
455+
NoButton beeing the only exception).
456+
457+
The method automatically emits the dialogDone() signal and handels self deletion in a
458+
threadsafe manner.
459+
460+
@sa MessageResult::dialogDone, MessageResult::result, MessageResult::autoDelete
461+
*/
462+
463+
/*!
464+
@fn QtMvvm::MessageResult::complete(QtMvvm::MessageConfig::StandardButton, const QVariant &)
465+
466+
@param result The input result to be set as MessageResult::result
467+
@copydetails MessageResult::complete(QtMvvm::MessageConfig::StandardButton)
468+
*/
469+
470+
/*!
471+
@fn QtMvvm::MessageResult::discardMessage
472+
473+
Calls the closeMethod set by the presenter (if one was set) to close the dialog. Connect to
474+
the dialogDone() signal to be notified when the dialog was closed. It is typically emitted with
475+
MessageConfig::NoButton when beeing discarded this way.
476+
477+
@sa MessageResult::setCloseTarget, MessageResult::dialogDone
478+
*/
479+
480+
/*!
481+
@fn QtMvvm::MessageResult::dialogDone
482+
483+
@param button The button that was pressed by the user to complete the dialog
484+
485+
Is emitted as soon as the dialog was closed with the button that was pressed, or
486+
MessageConfig::NoButton when closed another way. If the dialog had an input edit for the user
487+
to enter a value, you can retrieve the result via MessageResult::result.
488+
489+
@warning You must always direct connect to this signal, because if the auto deletion is
490+
enabled, the result will be deleted right after the signal was emitted (and thus after all
491+
direct connected slots have been called). You may use MessageResult::result only in a slot
492+
connect to this signal, because auf the reason above, and because before the signal it is
493+
considered invalid.
494+
495+
@sa MessageResult::result, MessageResult::complete
496+
*/
497+
498+
499+
500+
/*!
501+
@fn QtMvvm::information(const QString &, const QString &, const QString &)
502+
503+
@param title The messagebox title (MessageConfig::title)
504+
@param text The primary message text (MessageConfig::text)
505+
@param okText An optional text overwrite for the OK button (MessageConfig::buttonTexts)
506+
507+
Shows a simple informative dialog with a title, a text and the OK button present.
508+
509+
@sa QtMvvm::question, QtMvvm::warning, QtMvvm::critical, QtMvvm::about, MessageConfig,
510+
CoreApp::showDialog
511+
*/
512+
513+
/*!
514+
@fn QtMvvm::information(const QString &, const QString &, const std::function<void()> &, const QString &)
515+
516+
@param onResult A handler to be called when the message box has been closed
517+
@copydetails QtMvvm::information(const QString &, const QString &, const QString &)
518+
*/
519+
520+
/*!
521+
@fn QtMvvm::information(const QString &, const QString &, QObject *, const std::function<void()> &, const QString &)
522+
523+
@param scope A scope to limit to. `onResult` is only called as long as scope has not been deleted
524+
@copydetails QtMvvm::information(const QString &, const QString &, const std::function<void()> &, const QString &)
525+
*/
526+
527+
/*!
528+
@fn QtMvvm::question(const QString &, const QString &, const QString &, const QString &)
529+
530+
@param title The messagebox title (MessageConfig::title)
531+
@param text The primary message text (MessageConfig::text)
532+
@param yesText An optional text overwrite for the YES button (MessageConfig::buttonTexts)
533+
@param noText An optional text overwrite for the NO button (MessageConfig::buttonTexts)
534+
535+
Shows a simple question dialog with a title, a text and two buttons (typically YES and NO) to
536+
ask the user a simple boolean question.
537+
538+
@sa QtMvvm::information, QtMvvm::warning, QtMvvm::critical, MessageConfig,
539+
CoreApp::showDialog
540+
*/
541+
542+
/*!
543+
@fn QtMvvm::question(const QString &, const QString &, const std::function<void(bool)> &, const QString &, const QString &)
544+
545+
@param onResult A handler to be called when the message box has been closed. The parameter is
546+
`true` if the user pressed YES, `false` if he pressed NO or cancled the dialog
547+
@copydetails QtMvvm::question(const QString &, const QString &, const QString &, const QString &)
548+
*/
549+
550+
/*!
551+
@fn QtMvvm::question(const QString &, const QString &, QObject *, const std::function<void(bool)> &, const QString &, const QString &)
552+
553+
@param scope A scope to limit to. `onResult` is only called as long as scope has not been deleted
554+
@copydetails QtMvvm::question(const QString &, const QString &, const std::function<void(bool)> &, const QString &, const QString &)
555+
*/
556+
557+
/*!
558+
@fn QtMvvm::warning(const QString &, const QString &, const QString &)
559+
560+
@param title The messagebox title (MessageConfig::title)
561+
@param text The primary message text (MessageConfig::text)
562+
@param okText An optional text overwrite for the OK button (MessageConfig::buttonTexts)
563+
564+
Shows a simple warning dialog with a title, a text and the OK button present.
565+
566+
@sa QtMvvm::question, QtMvvm::information, QtMvvm::critical, MessageConfig,
567+
CoreApp::showDialog
568+
*/
569+
570+
/*!
571+
@fn QtMvvm::warning(const QString &, const QString &, const std::function<void()> &, const QString &)
572+
573+
@param onResult A handler to be called when the message box has been closed
574+
@copydetails QtMvvm::warning(const QString &, const QString &, const QString &)
575+
*/
576+
577+
/*!
578+
@fn QtMvvm::warning(const QString &, const QString &, QObject *, const std::function<void()> &, const QString &)
579+
580+
@param scope A scope to limit to. `onResult` is only called as long as scope has not been deleted
581+
@copydetails QtMvvm::warning(const QString &, const QString &, const std::function<void()> &, const QString &)
582+
*/
583+
584+
/*!
585+
@fn QtMvvm::critical(const QString &, const QString &, const QString &)
586+
587+
@param title The messagebox title (MessageConfig::title)
588+
@param text The primary message text (MessageConfig::text)
589+
@param okText An optional text overwrite for the OK button (MessageConfig::buttonTexts)
590+
591+
Shows a simple critical error dialog with a title, a text and the OK button present.
592+
593+
@sa QtMvvm::question, QtMvvm::warning, QtMvvm::information, MessageConfig,
594+
CoreApp::showDialog
595+
*/
596+
597+
/*!
598+
@fn QtMvvm::critical(const QString &, const QString &, const std::function<void()> &, const QString &)
599+
600+
@param onResult A handler to be called when the message box has been closed
601+
@copydetails QtMvvm::critical(const QString &, const QString &, const QString &)
602+
*/
603+
604+
/*!
605+
@fn QtMvvm::critical(const QString &, const QString &, QObject *, const std::function<void()> &, const QString &)
606+
607+
@param scope A scope to limit to. `onResult` is only called as long as scope has not been deleted
608+
@copydetails QtMvvm::critical(const QString &, const QString &, const std::function<void()> &, const QString &)
609+
*/
610+
611+
/*!
612+
@fn QtMvvm::about(const QString &, const QUrl &, const QString &, const QUrl &, const QString &, bool, const QStringList &, const QString &)
613+
614+
@param description A short descriptive text what your application is all about
615+
@param websiteUrl The URL to your website
616+
@param licenseName The name of the License you are using
617+
@param licenseUrl A URL to an online (or offline) variant of the license to read
618+
for the user
619+
@param companyName The name of your company or your name as creator of the application
620+
@param addQtVersion Specify whether the Qt Version should be visible as part of the about
621+
dialog
622+
@param extraTopInfos Additional short texts to be shown below the version
623+
@param extraBottomInfos An additional text to be shown at the bottom of the dialog
624+
625+
Creates an advanced about dialog out of all of this information in an organized manner. The
626+
`extraTopInfos` are ment as short extra information about configurations etc, for example a
627+
specific plattform plugin used, as selected style or further important links. The
628+
`extraBottomInfos` can be a long and rich text with anything else you want to put in the about
629+
dialog that is not applicabale for the previous part (for example a list of contributers).
630+
631+
@note All the texts can be richtext (HTML). But be aware that they are already beeing wrapped
632+
in `<p></p>` blocks. Furthermore, QML only supports a small subset of HTML in the default text
633+
mode that is used.
634+
635+
@sa QtMvvm::information, MessageConfig, CoreApp::showDialog
636+
*/

src/imports/mvvmcore/plugins.qmltypes

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Module {
5656
Property { name: "autoDelete"; type: "bool" }
5757
Signal {
5858
name: "dialogDone"
59-
Parameter { name: "result"; type: "QtMvvm::MessageConfig::StandardButton" }
59+
Parameter { name: "button"; type: "QtMvvm::MessageConfig::StandardButton" }
6060
}
6161
Signal {
6262
name: "autoDeleteChanged"
@@ -83,12 +83,12 @@ Module {
8383
}
8484
Method {
8585
name: "complete"
86-
Parameter { name: "result"; type: "QtMvvm::MessageConfig::StandardButton" }
86+
Parameter { name: "button"; type: "QtMvvm::MessageConfig::StandardButton" }
8787
}
8888
Method {
8989
name: "complete"
90-
Parameter { name: "result"; type: "QtMvvm::MessageConfig::StandardButton" }
91-
Parameter { name: "resultValue"; type: "QVariant" }
90+
Parameter { name: "button"; type: "QtMvvm::MessageConfig::StandardButton" }
91+
Parameter { name: "result"; type: "QVariant" }
9292
}
9393
}
9494
Component {

src/mvvmcore/message.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,13 @@ MessageResult::~MessageResult() {}
198198

199199
bool MessageResult::hasResult() const
200200
{
201+
QMutexLocker lock(&d->mutex);
201202
return d->result.isValid();
202203
}
203204

204205
QVariant MessageResult::result() const
205206
{
207+
QMutexLocker lock(&d->mutex);
206208
return d->result;
207209
}
208210

@@ -227,29 +229,33 @@ void MessageResult::setCloseTarget(QObject *closeObject, const QString &closeMet
227229
void MessageResult::setCloseTarget(QObject *closeObject, const QMetaMethod &closeMethod)
228230
{
229231
Q_ASSERT_X(closeObject, Q_FUNC_INFO, "closeObject must not be null");
232+
QMutexLocker lock(&d->mutex);
230233
d->closeObject = closeObject;
231234
d->closeMethod = closeMethod;
232235
if(d->closeRequested)
233236
d->closeMethod.invoke(d->closeObject, Qt::QueuedConnection);
234237
}
235238

236-
void MessageResult::complete(MessageConfig::StandardButton result)
239+
void MessageResult::complete(MessageConfig::StandardButton button)
237240
{
238-
//TODO make async
239-
emit dialogDone(result);
241+
QMetaObject::invokeMethod(this, "dialogDone", Qt::QueuedConnection,
242+
Q_ARG(QtMvvm::MessageConfig::StandardButton, button));
243+
QMutexLocker lock(&d->mutex);
240244
if(d->autoDelete)
241-
deleteLater();
245+
QMetaObject::invokeMethod(this, "deleteLater", Qt::QueuedConnection);
242246
}
243247

244248
void MessageResult::discardMessage()
245249
{
250+
QMutexLocker lock(&d->mutex);
246251
if(d->closeObject && !d->closeRequested)
247252
d->closeMethod.invoke(d->closeObject, Qt::QueuedConnection);
248253
d->closeRequested = true;
249254
}
250255

251256
void MessageResult::setResult(QVariant result)
252257
{
258+
QMutexLocker lock(&d->mutex);
253259
d->result = result;
254260
}
255261

src/mvvmcore/message.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,6 @@ class Q_MVVMCORE_EXPORT MessageResult : public QObject
189189
//! @readAcFn{MessageResult::autoDelete}
190190
bool autoDelete() const;
191191

192-
//USE IN GUI ONLY
193192
/**
194193
* @name Presenter-Only methods
195194
* @details The following methods should be used by the presenter only, not from the core
@@ -200,11 +199,11 @@ class Q_MVVMCORE_EXPORT MessageResult : public QObject
200199
//! @copydoc MessageResult::setCloseTarget(QObject *, const QString &)
201200
Q_INVOKABLE void setCloseTarget(QObject *closeObject, const QMetaMethod &closeMethod);
202201
//! Completes the dialog and tells the result that it is finished
203-
Q_INVOKABLE void complete(QtMvvm::MessageConfig::StandardButton result);
202+
Q_INVOKABLE void complete(QtMvvm::MessageConfig::StandardButton button);
204203
//! @copybrief MessageResult::complete(QtMvvm::MessageConfig::StandardButton)
205-
Q_INVOKABLE inline void complete(QtMvvm::MessageConfig::StandardButton result, const QVariant &resultValue) {
206-
setResult(resultValue);
207-
complete(result);
204+
Q_INVOKABLE inline void complete(QtMvvm::MessageConfig::StandardButton button, const QVariant &result) {
205+
setResult(result);
206+
complete(button);
208207
}
209208
//! @}
210209

@@ -219,7 +218,7 @@ public Q_SLOTS:
219218

220219
Q_SIGNALS:
221220
//! Is emitted as soon as the dialog was completed
222-
void dialogDone(QtMvvm::MessageConfig::StandardButton result);
221+
void dialogDone(QtMvvm::MessageConfig::StandardButton button);
223222

224223
//! @notifyAcFn{MessageResult::autoDelete}
225224
void autoDeleteChanged(bool autoDelete);
@@ -236,13 +235,13 @@ public Q_SLOTS:
236235
Q_MVVMCORE_EXPORT MessageResult *information(const QString &title,
237236
const QString &text,
238237
const QString &okText = {});
239-
//! @copydoc QtMvvm::information(const QString &, const QString &, const std::function<void()> &, const QString &)
238+
//! @copybrief QtMvvm::information(const QString &, const QString &, const std::function<void()> &, const QString &)
240239
Q_MVVMCORE_EXPORT void information(const QString &title,
241240
const QString &text,
242241
QObject *scope,
243242
const std::function<void()> &onResult,
244243
const QString &okText = {});
245-
//! @copydoc QtMvvm::information(const QString &, const QString &, const QString &)
244+
//! @copybrief QtMvvm::information(const QString &, const QString &, const QString &)
246245
Q_MVVMCORE_EXPORT void information(const QString &title,
247246
const QString &text,
248247
const std::function<void()> &onResult,
@@ -253,14 +252,14 @@ Q_MVVMCORE_EXPORT MessageResult *question(const QString &title,
253252
const QString &text,
254253
const QString &yesText = {},
255254
const QString &noText = {});
256-
//! @copydoc QtMvvm::question(const QString &, const QString &, const std::function<void(bool)> &, const QString &, const QString &)
255+
//! @copybrief QtMvvm::question(const QString &, const QString &, const std::function<void(bool)> &, const QString &, const QString &)
257256
Q_MVVMCORE_EXPORT void question(const QString &title,
258257
const QString &text,
259258
QObject *scope,
260259
const std::function<void(bool)> &onResult,
261260
const QString &yesText = {},
262261
const QString &noText = {});
263-
//! @copydoc QtMvvm::question(const QString &, const QString &, const QString &, const QString &)
262+
//! @copybrief QtMvvm::question(const QString &, const QString &, const QString &, const QString &)
264263
Q_MVVMCORE_EXPORT void question(const QString &title,
265264
const QString &text,
266265
const std::function<void(bool)> &onResult,
@@ -271,13 +270,13 @@ Q_MVVMCORE_EXPORT void question(const QString &title,
271270
Q_MVVMCORE_EXPORT MessageResult *warning(const QString &title,
272271
const QString &text,
273272
const QString &okText = {});
274-
//! @copydoc QtMvvm::warning(const QString &, const QString &, const std::function<void()> &, const QString &)
273+
//! @copybrief QtMvvm::warning(const QString &, const QString &, const std::function<void()> &, const QString &)
275274
Q_MVVMCORE_EXPORT void warning(const QString &title,
276275
const QString &text,
277276
QObject *scope,
278277
const std::function<void()> &onResult,
279278
const QString &okText = {});
280-
//! @copydoc QtMvvm::warning(const QString &, const QString &, const QString &)
279+
//! @copybrief QtMvvm::warning(const QString &, const QString &, const QString &)
281280
Q_MVVMCORE_EXPORT void warning(const QString &title,
282281
const QString &text,
283282
const std::function<void()> &onResult,
@@ -287,13 +286,13 @@ Q_MVVMCORE_EXPORT void warning(const QString &title,
287286
Q_MVVMCORE_EXPORT MessageResult *critical(const QString &title,
288287
const QString &text,
289288
const QString &okText = {});
290-
//! @copydoc QtMvvm::critical(const QString &, const QString &, const std::function<void()> &, const QString &)
289+
//! @copybrief QtMvvm::critical(const QString &, const QString &, const std::function<void()> &, const QString &)
291290
Q_MVVMCORE_EXPORT void critical(const QString &title,
292291
const QString &text,
293292
QObject *scope,
294293
const std::function<void()> &onResult,
295294
const QString &okText = {});
296-
//! @copydoc QtMvvm::critical(const QString &, const QString &, const QString &)
295+
//! @copybrief QtMvvm::critical(const QString &, const QString &, const QString &)
297296
Q_MVVMCORE_EXPORT void critical(const QString &title,
298297
const QString &text,
299298
const std::function<void()> &onResult,

0 commit comments

Comments
 (0)