QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
message.h
1 #ifndef QTMVVM_MESSAGE_H
2 #define QTMVVM_MESSAGE_H
3 
4 #include <functional>
5 
6 #include <QtCore/qcoreapplication.h>
7 #include <QtCore/qshareddata.h>
8 #include <QtCore/qscopedpointer.h>
9 #include <QtCore/qurl.h>
10 #include <QtCore/qvariant.h>
11 
12 #include <QtGui/qcolor.h>
13 
14 #include "QtMvvmCore/qtmvvmcore_global.h"
15 
16 namespace QtMvvm {
17 
18 class MessageConfigPrivate;
20 class Q_MVVMCORE_EXPORT MessageConfig
21 {
22  Q_GADGET
23  Q_DECLARE_TR_FUNCTIONS(MessageConfig)
24 
25 
26  Q_PROPERTY(QByteArray type READ type WRITE setType)
28  Q_PROPERTY(QByteArray subType READ subType WRITE setSubType RESET resetSubType)
29 
31  Q_PROPERTY(QString title READ title WRITE setTitle)
33  Q_PROPERTY(QString text READ text WRITE setText)
35  Q_PROPERTY(StandardButtons buttons READ buttons WRITE setButtons RESET resetButtons)
37  Q_PROPERTY(QVariantMap buttonTexts READ buttonTextsMap WRITE setButtonTextsMap RESET resetButtons)
38 
40  Q_PROPERTY(QVariant defaultValue READ defaultValue WRITE setDefaultValue)
42  Q_PROPERTY(QVariantMap viewProperties READ viewProperties WRITE setViewProperties)
43 
44 public:
47  // keep this in sync with QPlatformDialogHelper::StandardButton
48  NoButton = 0x00000000,
49  Ok = 0x00000400,
50  Save = 0x00000800,
51  SaveAll = 0x00001000,
52  Open = 0x00002000,
53  Yes = 0x00004000,
54  YesToAll = 0x00008000,
55  No = 0x00010000,
56  NoToAll = 0x00020000,
57  Abort = 0x00040000,
58  Retry = 0x00080000,
59  Ignore = 0x00100000,
60  Close = 0x00200000,
61  Cancel = 0x00400000,
62  Discard = 0x00800000,
63  Help = 0x01000000,
64  Apply = 0x02000000,
65  Reset = 0x04000000,
66  RestoreDefaults = 0x08000000
67  };
68  Q_DECLARE_FLAGS(StandardButtons, StandardButton)
69  Q_FLAG(StandardButtons)
70 
71 
75  static const QByteArray TypeMessageBox;
80  static const QByteArray TypeFileDialog;
86 
91  static const QByteArray SubTypeInformation;
94  static const QByteArray SubTypeWarning;
100  static const QByteArray SubTypeAbout;
102 
107  static const QByteArray SubTypeDir;
116 
121  static const QByteArray SubTypeRgb;
124  static const QByteArray SubTypeArgb;
126 
131  static const QByteArray SubTypeProgress;
134  static const QByteArray SubTypeBusy;
136 
138  MessageConfig(const QByteArray &type = TypeMessageBox, const QByteArray &subType = {});
140  MessageConfig(const MessageConfig &other);
142  MessageConfig(MessageConfig &&other) noexcept;
143  ~MessageConfig();
144 
146  MessageConfig &operator=(const MessageConfig &other);
148  MessageConfig &operator=(MessageConfig &&other) noexcept;
149 
151  QByteArray type() const;
153  QByteArray subType() const;
155  QString title() const;
157  QString text() const;
159  StandardButtons buttons() const;
161  QHash<StandardButton, QString> buttonTexts() const;
163  QVariant defaultValue() const;
165  QVariantMap viewProperties() const;
166 
168  MessageConfig &setType(const QByteArray &type);
170  MessageConfig &setSubType(const QByteArray &subType);
172  MessageConfig &setTitle(const QString &title);
174  MessageConfig &setText(const QString &text);
176  MessageConfig &setButtons(StandardButtons buttons);
178  MessageConfig &addButton(StandardButton button);
180  MessageConfig &setButtonTexts(const QHash<StandardButton, QString> &buttonTexts);
182  MessageConfig &setButtonText(StandardButton button, const QString &text);
184  MessageConfig &setDefaultValue(const QVariant &defaultValue);
186  MessageConfig &setViewProperties(const QVariantMap &viewProperties);
188  MessageConfig &setViewProperty(const QString &key, const QVariant &value);
189 
191  MessageConfig &resetSubType();
193  MessageConfig &resetButtons();
194 
195 private:
197 
198  QVariantMap buttonTextsMap() const;
199  void setButtonTextsMap(const QVariantMap &buttonTexts);
200 };
201 
202 class MessageResultPrivate;
204 class Q_MVVMCORE_EXPORT MessageResult : public QObject
205 {
206  Q_OBJECT
207 
209  Q_PROPERTY(QVariant result READ result WRITE setResult)
211  Q_PROPERTY(bool autoDelete READ autoDelete WRITE setAutoDelete NOTIFY autoDeleteChanged)
212 
213 public:
214  explicit MessageResult();
215  ~MessageResult() override;
216 
218  bool hasResult() const;
220  QVariant result() const;
222  bool autoDelete() const;
223 
229  Q_INVOKABLE void setCloseTarget(QObject *closeObject, const QString &closeMethod);
232  Q_INVOKABLE void setCloseTarget(QObject *closeObject, const QMetaMethod &closeMethod);
234  Q_INVOKABLE void complete(QtMvvm::MessageConfig::StandardButton button);
236  Q_INVOKABLE inline void complete(QtMvvm::MessageConfig::StandardButton button, const QVariant &result) {
237  setResult(result);
238  complete(button);
239  }
241 
242 public Q_SLOTS:
244  void discardMessage();
245 
247  void setResult(QVariant result);
249  void setAutoDelete(bool autoDelete);
250 
251 Q_SIGNALS:
253  void dialogDone(QtMvvm::MessageConfig::StandardButton button, QPrivateSignal);
254 
256  void autoDeleteChanged(bool autoDelete, QPrivateSignal);
257 
258 private:
260 };
261 
262 class ProgressControlPrivate;
264 class Q_MVVMCORE_EXPORT ProgressControl : public QObject
265 {
266  Q_OBJECT
267 
269  Q_PROPERTY(bool autoDelete READ autoDelete WRITE setAutoDelete NOTIFY autoDeleteChanged)
270 
271 
272  Q_PROPERTY(bool indeterminate READ isIndeterminate WRITE setIndeterminate NOTIFY indeterminateChanged)
274  Q_PROPERTY(int minimum READ minimum WRITE setMinimum NOTIFY minimumChanged)
276  Q_PROPERTY(int maximum READ maximum WRITE setMaximum NOTIFY maximumChanged)
278  Q_PROPERTY(int progress READ progress WRITE setProgress NOTIFY progressChanged)
279 
280 public:
282  explicit ProgressControl(QObject *parent = nullptr);
283  ~ProgressControl() override;
284 
286  bool autoDelete() const;
288  bool isIndeterminate() const;
290  int minimum() const;
292  int maximum() const;
294  int progress() const;
295 
301  Q_INVOKABLE void requestCancel(QtMvvm::MessageConfig::StandardButton btn);
304  Q_INVOKABLE void notifyClosed();
306 
307 public Q_SLOTS:
309  void close();
311  void updateLabel(const QString &text);
312 
314  void setAutoDelete(bool autoDelete);
316  void setIndeterminate(bool indeterminate);
318  void setMinimum(int minimum);
320  void setMaximum(int maximum);
322  void setProgress(int progress);
324  void setProgress(double progressPercent);
325 
326 Q_SIGNALS:
328  void autoDeleteChanged(bool autoDelete, QPrivateSignal);
330  void indeterminateChanged(bool indeterminate, QPrivateSignal);
332  void minimumChanged(int minimum, QPrivateSignal);
334  void maximumChanged(int maximum, QPrivateSignal);
336  void progressChanged(int progress, QPrivateSignal);
337 
339  void canceled(QtMvvm::MessageConfig::StandardButton btn, QPrivateSignal);
341  void closed(QPrivateSignal);
342 
348  void changeLabel(const QString &text, QPrivateSignal);
351  void closeRequested(QPrivateSignal);
353 
354 private:
355  QScopedPointer<ProgressControlPrivate> d;
356 };
357 
362 Q_MVVMCORE_EXPORT MessageResult *information(const QString &title,
364  const QString &text,
365  const QString &okText = {});
367 Q_MVVMCORE_EXPORT void information(const QString &title,
368  const QString &text,
369  QObject *scope,
370  const std::function<void()> &onResult,
371  const QString &okText = {});
373 Q_MVVMCORE_EXPORT void information(const QString &title,
374  const QString &text,
375  const std::function<void()> &onResult,
376  const QString &okText = {});
377 
379 Q_MVVMCORE_EXPORT MessageResult *question(const QString &title,
380  const QString &text,
381  const QString &yesText = {},
382  const QString &noText = {});
384 Q_MVVMCORE_EXPORT void question(const QString &title,
385  const QString &text,
386  QObject *scope,
387  const std::function<void(bool)> &onResult,
388  const QString &yesText = {},
389  const QString &noText = {});
391 Q_MVVMCORE_EXPORT void question(const QString &title,
392  const QString &text,
393  const std::function<void(bool)> &onResult,
394  const QString &yesText = {},
395  const QString &noText = {});
396 
398 Q_MVVMCORE_EXPORT MessageResult *warning(const QString &title,
399  const QString &text,
400  const QString &okText = {});
402 Q_MVVMCORE_EXPORT void warning(const QString &title,
403  const QString &text,
404  QObject *scope,
405  const std::function<void()> &onResult,
406  const QString &okText = {});
408 Q_MVVMCORE_EXPORT void warning(const QString &title,
409  const QString &text,
410  const std::function<void()> &onResult,
411  const QString &okText = {});
412 
414 Q_MVVMCORE_EXPORT MessageResult *critical(const QString &title,
415  const QString &text,
416  const QString &okText = {});
418 Q_MVVMCORE_EXPORT void critical(const QString &title,
419  const QString &text,
420  QObject *scope,
421  const std::function<void()> &onResult,
422  const QString &okText = {});
424 Q_MVVMCORE_EXPORT void critical(const QString &title,
425  const QString &text,
426  const std::function<void()> &onResult,
427  const QString &okText = {});
428 
430 Q_MVVMCORE_EXPORT MessageResult *about(const QString &description,
431  const QUrl &websiteUrl = QUrl(),
432  const QString &licenseName = QString(),
433  const QUrl &licenseUrl = QUrl(),
434  const QString &companyName = QString(),
435  bool addQtVersion = true,
436  const QStringList &extraTopInfos = QStringList(),
437  const QString &extraBottomInfos = QString());
439 
444 Q_MVVMCORE_EXPORT MessageResult *getInput(const QString &title,
446  const QString &text,
447  const char *inputType,
448  const QVariant &defaultValue = {},
449  const QVariantMap &viewProperties = {},
450  const QString &okText = {},
451  const QString &cancelText = {});
453 Q_MVVMCORE_EXPORT void getInput(const QString &title,
454  const QString &text,
455  const char *inputType,
456  QObject *scope,
457  const std::function<void(QVariant)> &onResult,
458  const QVariant &defaultValue = {},
459  const QVariantMap &viewProperties = {},
460  const QString &okText = {},
461  const QString &cancelText = {});
463 Q_MVVMCORE_EXPORT void getInput(const QString &title,
464  const QString &text,
465  const char *inputType,
466  const std::function<void(QVariant)> &onResult,
467  const QVariant &defaultValue = {},
468  const QVariantMap &viewProperties = {},
469  const QString &okText = {},
470  const QString &cancelText = {});
471 
473 template <typename TEdit>
474 inline MessageResult *getInput(const QString &title,
475  const QString &text,
476  const TEdit &defaultValue = {},
477  const QVariantMap &viewProperties = {},
478  const QString &okText = {},
479  const QString &cancelText = {}) {
480  return getInput(title, text, QMetaType::typeName(qMetaTypeId<TEdit>()), QVariant::fromValue(defaultValue), viewProperties, okText, cancelText);
481 }
483 template <typename TEdit>
484 inline void getInput(const QString &title,
485  const QString &text,
486  QObject *scope,
487  const std::function<void(TEdit, bool)> &onResult,
488  const TEdit &defaultValue = {},
489  const QVariantMap &viewProperties = {},
490  const QString &okText = {},
491  const QString &cancelText = {}) {
492  getInput(title, text, QMetaType::typeName(qMetaTypeId<TEdit>()), scope, [onResult](const QVariant &v) {
493  onResult(v.template value<TEdit>(), v.isValid());
494  }, QVariant::fromValue(defaultValue), viewProperties, okText, cancelText);
495 }
497 template <typename TEdit>
498 inline void getInput(const QString &title,
499  const QString &text,
500  const std::function<void(TEdit, bool)> &onResult,
501  const TEdit &defaultValue = {},
502  const QVariantMap &viewProperties = {},
503  const QString &okText = {},
504  const QString &cancelText = {}) {
505  getInput(title, text, QMetaType::typeName(qMetaTypeId<TEdit>()), [onResult](const QVariant &v) {
506  onResult(v.template value<TEdit>(), v.isValid());
507  }, QVariant::fromValue(defaultValue), viewProperties, okText, cancelText);
508 }
510 
515 Q_MVVMCORE_EXPORT MessageResult *getExistingDirectory(const QString &title = {},
517  const QUrl &dir = {});
519 Q_MVVMCORE_EXPORT void getExistingDirectory(QObject *scope,
520  const std::function<void(QUrl)> &onResult,
521  const QString &title = {},
522  const QUrl &dir = {});
524 Q_MVVMCORE_EXPORT void getExistingDirectory(const std::function<void(QUrl)> &onResult,
525  const QString &title = {},
526  const QUrl &dir = {});
527 
529 Q_MVVMCORE_EXPORT MessageResult *getOpenFile(const QString &title = {},
530  const QStringList &supportedMimeTypes = {},
531  const QUrl &dir = {});
533 Q_MVVMCORE_EXPORT void getOpenFile(QObject *scope,
534  const std::function<void(QUrl)> &onResult,
535  const QString &title = {},
536  const QStringList &supportedMimeTypes = {},
537  const QUrl &dir = {});
539 Q_MVVMCORE_EXPORT void getOpenFile(const std::function<void(QUrl)> &onResult,
540  const QString &title = {},
541  const QStringList &supportedMimeTypes = {},
542  const QUrl &dir = {});
543 
545 Q_MVVMCORE_EXPORT MessageResult *getOpenFiles(const QString &title = {},
546  const QStringList &supportedMimeTypes = {},
547  const QUrl &dir = {});
549 Q_MVVMCORE_EXPORT void getOpenFiles(QObject *scope,
550  const std::function<void(QList<QUrl>)> &onResult,
551  const QString &title = {},
552  const QStringList &supportedMimeTypes = {},
553  const QUrl &dir = {});
555 Q_MVVMCORE_EXPORT void getOpenFiles(const std::function<void(QList<QUrl>)> &onResult,
556  const QString &title = {},
557  const QStringList &supportedMimeTypes = {},
558  const QUrl &dir = {});
559 
561 Q_MVVMCORE_EXPORT MessageResult *getSaveFile(const QString &title = {},
562  const QStringList &supportedMimeTypes = {},
563  const QUrl &dir = {});
565 Q_MVVMCORE_EXPORT void getSaveFile(QObject *scope,
566  const std::function<void(QUrl)> &onResult,
567  const QString &title = {},
568  const QStringList &supportedMimeTypes = {},
569  const QUrl &dir = {});
571 Q_MVVMCORE_EXPORT void getSaveFile(const std::function<void(QUrl)> &onResult,
572  const QString &title = {},
573  const QStringList &supportedMimeTypes = {},
574  const QUrl &dir = {});
576 
581 Q_MVVMCORE_EXPORT MessageResult *getColor(const QString &title = {},
583  const QColor &color = {},
584  bool argb = false);
586 Q_MVVMCORE_EXPORT void getColor(QObject *scope,
587  const std::function<void(QColor)> &onResult,
588  const QString &title = {},
589  const QColor &color = {},
590  bool argb = false);
592 Q_MVVMCORE_EXPORT void getColor(const std::function<void(QColor)> &onResult,
593  const QString &title = {},
594  const QColor &color = {},
595  bool argb = false);
597 
602 Q_MVVMCORE_EXPORT MessageResult *showProgress(const QString &title,
604  const QString &label,
605  ProgressControl *control,
606  bool allowCancel = true,
607  bool isBusy = false,
608  const QString &cancelText = {});
609 
611 Q_MVVMCORE_EXPORT ProgressControl *showProgress(QObject *scope,
612  const QString &title = {},
613  const QString &label = {},
614  int maximum = 100,
615  int minimum = 0,
616  bool allowCancel = true,
617  int value = 0,
618  const QString &cancelText = {});
620 Q_MVVMCORE_EXPORT ProgressControl *showProgress(const QString &title = {},
621  const QString &label = {},
622  int maximum = 100,
623  int minimum = 0,
624  bool allowCancel = true,
625  int value = 0,
626  const QString &cancelText = {});
627 
629 Q_MVVMCORE_EXPORT ProgressControl *showIndeterminateProgress(QObject *scope,
630  const QString &title = {},
631  const QString &label = {},
632  bool allowCancel = true,
633  const QString &cancelText = {});
635 Q_MVVMCORE_EXPORT ProgressControl *showIndeterminateProgress(const QString &title = {},
636  const QString &label = {},
637  bool allowCancel = true,
638  const QString &cancelText = {});
639 
641 Q_MVVMCORE_EXPORT ProgressControl *showBusy(QObject *scope,
642  const QString &title = {},
643  const QString &label = {},
644  bool allowCancel = true,
645  const QString &cancelText = {});
647 Q_MVVMCORE_EXPORT ProgressControl *showBusy(const QString &title = {},
648  const QString &label = {},
649  bool allowCancel = true,
650  const QString &cancelText = {});
652 
653 }
654 
655 Q_DECLARE_METATYPE(QtMvvm::MessageConfig)
656 Q_DECLARE_METATYPE(QtMvvm::MessageConfig::StandardButton)
657 Q_DECLARE_TYPEINFO(QtMvvm::MessageConfig, Q_MOVABLE_TYPE);
658 Q_DECLARE_METATYPE(QtMvvm::MessageResult*)
659 Q_DECLARE_OPERATORS_FOR_FLAGS(QtMvvm::MessageConfig::StandardButtons)
660 
661 #endif // QTMVVM_MESSAGE_H
Q_MVVMCORE_EXPORT MessageResult * question(const QString &title, const QString &text, const QString &yesText={}, const QString &noText={})
A shortcut to show a simple question messagebox.
static const QByteArray TypeFileDialog
A type to show a generic file dialog.
Definition: message.h:80
static const QByteArray SubTypeSaveFile
A subType for to show a save file dialog.
Definition: message.h:114
Q_MVVMCORE_EXPORT MessageResult * getColor(const QString &title={}, const QColor &color={}, bool argb=false)
A shortcut to show a color dialog to select a color.
static const QByteArray SubTypeOpenFile
A subType for to show an open file dialog.
Definition: message.h:110
Q_MVVMCORE_EXPORT MessageResult * getOpenFiles(const QString &title={}, const QStringList &supportedMimeTypes={}, const QUrl &dir={})
A shortcut to show a file dialog to select multiple existing files.
StandardButton
The Possible Buttons a dialog can have.
Definition: message.h:46
A Helper class to control a generic progress dialog.
Definition: message.h:264
static const QByteArray TypeInputDialog
A type to show a generic input dialog.
Definition: message.h:78
Q_MVVMCORE_EXPORT MessageResult * getSaveFile(const QString &title={}, const QStringList &supportedMimeTypes={}, const QUrl &dir={})
A shortcut to show a file dialog to select a new file for saving.
Q_MVVMCORE_EXPORT MessageResult * getInput(const QString &title, const QString &text, const char *inputType, const QVariant &defaultValue={}, const QVariantMap &viewProperties={}, const QString &okText={}, const QString &cancelText={})
A shortcut to show an input dialog based of a type name.
Q_MVVMCORE_EXPORT ProgressControl * showBusy(QObject *scope, const QString &title={}, const QString &label={}, bool allowCancel=true, const QString &cancelText={})
A shortcut to show a busy indicator dialog.
static const QByteArray TypeColorDialog
A type to show a generic color dialog.
Definition: message.h:82
A configuration for a simple dialog to be shown from the core code.
Definition: message.h:20
static const QByteArray SubTypeOpenFiles
A subType for to show an open files dialog.
Definition: message.h:112
Q_MVVMCORE_EXPORT MessageResult * getOpenFile(const QString &title={}, const QStringList &supportedMimeTypes={}, const QUrl &dir={})
A shortcut to show a file dialog to select an existing file.
Q_MVVMCORE_EXPORT MessageResult * showProgress(const QString &title, const QString &label, ProgressControl *control, bool allowCancel=true, bool isBusy=false, const QString &cancelText={})
A shortcut to show a general progress dialog.
static const QByteArray SubTypeQuestion
A subType to show a question message.
Definition: message.h:98
static const QByteArray SubTypeAbout
A subType to show an about dialog.
Definition: message.h:100
Q_MVVMCORE_EXPORT MessageResult * about(const QString &description, const QUrl &websiteUrl=QUrl(), const QString &licenseName=QString(), const QUrl &licenseUrl=QUrl(), const QString &companyName=QString(), bool addQtVersion=true, const QStringList &extraTopInfos=QStringList(), const QString &extraBottomInfos=QString())
A shortcut to show a rich but simple about dialog.
static const QByteArray SubTypeArgb
A subType to show a color dialog with an alpha channel.
Definition: message.h:124
static const QByteArray SubTypeCritical
A subType to show a critical error message.
Definition: message.h:96
QVariant fromValue(const T &value)
static const QByteArray TypeProgressDialog
A type to show a generic progress dialog.
Definition: message.h:84
const char * typeName(int typeId)
Q_MVVMCORE_EXPORT ProgressControl * showIndeterminateProgress(QObject *scope, const QString &title={}, const QString &label={}, bool allowCancel=true, const QString &cancelText={})
A shortcut to show an indetermiante progress dialog.
Q_MVVMCORE_EXPORT MessageResult * critical(const QString &title, const QString &text, const QString &okText={})
A shortcut to show a simple critical error messagebox.
A result watcher to get the result once a dialog has finished.
Definition: message.h:204
bool isValid() const const
The primary namespace of the QtMvvm library.
static const QByteArray SubTypeWarning
A subType to show a warning message.
Definition: message.h:94
Q_MVVMCORE_EXPORT MessageResult * warning(const QString &title, const QString &text, const QString &okText={})
A shortcut to show a simple warning messagebox.
Q_MVVMCORE_EXPORT MessageResult * getExistingDirectory(const QString &title={}, const QUrl &dir={})
A shortcut to show a file dialog to select an existing directory.
Q_MVVMCORE_EXPORT MessageResult * information(const QString &title, const QString &text, const QString &okText={})
A shortcut to show a simple information messagebox.
static const QByteArray SubTypeBusy
A subType to show a dialog with a busy indicator.
Definition: message.h:134