QtMvvm  1.0.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 "QtMvvmCore/qtmvvmcore_global.h"
13 
14 namespace QtMvvm {
15 
16 class MessageConfigPrivate;
18 class Q_MVVMCORE_EXPORT MessageConfig
19 {
20  Q_GADGET
21  Q_DECLARE_TR_FUNCTIONS(MessageConfig)
22 
23 
24  Q_PROPERTY(QByteArray type READ type WRITE setType)
26  Q_PROPERTY(QByteArray subType READ subType WRITE setSubType RESET resetSubType)
27 
29  Q_PROPERTY(QString title READ title WRITE setTitle)
31  Q_PROPERTY(QString text READ text WRITE setText)
33  Q_PROPERTY(StandardButtons buttons READ buttons WRITE setButtons RESET resetButtons)
35  Q_PROPERTY(QVariantMap buttonTexts READ buttonTextsMap WRITE setButtonTextsMap RESET resetButtons)
36 
38  Q_PROPERTY(QVariant defaultValue READ defaultValue WRITE setDefaultValue)
40  Q_PROPERTY(QVariantMap viewProperties READ viewProperties WRITE setViewProperties)
41 
42 public:
45  // keep this in sync with QPlatformDialogHelper::StandardButton
46  NoButton = 0x00000000,
47  Ok = 0x00000400,
48  Save = 0x00000800,
49  SaveAll = 0x00001000,
50  Open = 0x00002000,
51  Yes = 0x00004000,
52  YesToAll = 0x00008000,
53  No = 0x00010000,
54  NoToAll = 0x00020000,
55  Abort = 0x00040000,
56  Retry = 0x00080000,
57  Ignore = 0x00100000,
58  Close = 0x00200000,
59  Cancel = 0x00400000,
60  Discard = 0x00800000,
61  Help = 0x01000000,
62  Apply = 0x02000000,
63  Reset = 0x04000000,
64  RestoreDefaults = 0x08000000
65  };
66  Q_DECLARE_FLAGS(StandardButtons, StandardButton)
67  Q_FLAG(StandardButtons)
68 
69 
73  static const QByteArray TypeMessageBox;
78  static const QByteArray TypeFileDialog;
80 
85  static const QByteArray SubTypeInformation;
88  static const QByteArray SubTypeWarning;
94  static const QByteArray SubTypeAbout;
96 
101  static const QByteArray SubTypeDir;
110 
112  MessageConfig(const QByteArray &type = TypeMessageBox, const QByteArray &subType = {});
114  MessageConfig(const MessageConfig &other);
115  ~MessageConfig();
116 
118  MessageConfig &operator=(const MessageConfig &other);
119 
121  QByteArray type() const;
123  QByteArray subType() const;
125  QString title() const;
127  QString text() const;
129  StandardButtons buttons() const;
131  QHash<StandardButton, QString> buttonTexts() const;
133  QVariant defaultValue() const;
135  QVariantMap viewProperties() const;
136 
138  MessageConfig &setType(const QByteArray &type);
140  MessageConfig &setSubType(const QByteArray &subType);
142  MessageConfig &setTitle(const QString &title);
144  MessageConfig &setText(const QString &text);
146  MessageConfig &setButtons(StandardButtons buttons);
148  MessageConfig &setButtonTexts(const QHash<StandardButton, QString> &buttonTexts);
150  MessageConfig &setButtonText(StandardButton button, const QString &text);
152  MessageConfig &setDefaultValue(const QVariant &defaultValue);
154  MessageConfig &setViewProperties(const QVariantMap &viewProperties);
156  MessageConfig &setViewProperty(const QString &key, const QVariant &value);
157 
159  MessageConfig &resetSubType();
161  MessageConfig &resetButtons();
162 
163 private:
165 
166  QVariantMap buttonTextsMap() const;
167  void setButtonTextsMap(const QVariantMap &buttonTexts);
168 };
169 
170 class MessageResultPrivate;
172 class Q_MVVMCORE_EXPORT MessageResult : public QObject
173 {
174  Q_OBJECT
175 
177  Q_PROPERTY(QVariant result READ result WRITE setResult)
179  Q_PROPERTY(bool autoDelete READ autoDelete WRITE setAutoDelete NOTIFY autoDeleteChanged)
180 
181 public:
182  explicit MessageResult();
183  ~MessageResult();
184 
186  bool hasResult() const;
188  QVariant result() const;
190  bool autoDelete() const;
191 
197  Q_INVOKABLE void setCloseTarget(QObject *closeObject, const QString &closeMethod);
200  Q_INVOKABLE void setCloseTarget(QObject *closeObject, const QMetaMethod &closeMethod);
202  Q_INVOKABLE void complete(QtMvvm::MessageConfig::StandardButton button);
204  Q_INVOKABLE inline void complete(QtMvvm::MessageConfig::StandardButton button, const QVariant &result) {
205  setResult(result);
206  complete(button);
207  }
209 
210 public Q_SLOTS:
212  void discardMessage();
213 
215  void setResult(QVariant result);
217  void setAutoDelete(bool autoDelete);
218 
219 Q_SIGNALS:
221  void dialogDone(QtMvvm::MessageConfig::StandardButton button, QPrivateSignal);
222 
224  void autoDeleteChanged(bool autoDelete, QPrivateSignal);
225 
226 private:
228 };
229 
234 Q_MVVMCORE_EXPORT MessageResult *information(const QString &title,
236  const QString &text,
237  const QString &okText = {});
239 Q_MVVMCORE_EXPORT void information(const QString &title,
240  const QString &text,
241  QObject *scope,
242  const std::function<void()> &onResult,
243  const QString &okText = {});
245 Q_MVVMCORE_EXPORT void information(const QString &title,
246  const QString &text,
247  const std::function<void()> &onResult,
248  const QString &okText = {});
249 
251 Q_MVVMCORE_EXPORT MessageResult *question(const QString &title,
252  const QString &text,
253  const QString &yesText = {},
254  const QString &noText = {});
256 Q_MVVMCORE_EXPORT void question(const QString &title,
257  const QString &text,
258  QObject *scope,
259  const std::function<void(bool)> &onResult,
260  const QString &yesText = {},
261  const QString &noText = {});
263 Q_MVVMCORE_EXPORT void question(const QString &title,
264  const QString &text,
265  const std::function<void(bool)> &onResult,
266  const QString &yesText = {},
267  const QString &noText = {});
268 
270 Q_MVVMCORE_EXPORT MessageResult *warning(const QString &title,
271  const QString &text,
272  const QString &okText = {});
274 Q_MVVMCORE_EXPORT void warning(const QString &title,
275  const QString &text,
276  QObject *scope,
277  const std::function<void()> &onResult,
278  const QString &okText = {});
280 Q_MVVMCORE_EXPORT void warning(const QString &title,
281  const QString &text,
282  const std::function<void()> &onResult,
283  const QString &okText = {});
284 
286 Q_MVVMCORE_EXPORT MessageResult *critical(const QString &title,
287  const QString &text,
288  const QString &okText = {});
290 Q_MVVMCORE_EXPORT void critical(const QString &title,
291  const QString &text,
292  QObject *scope,
293  const std::function<void()> &onResult,
294  const QString &okText = {});
296 Q_MVVMCORE_EXPORT void critical(const QString &title,
297  const QString &text,
298  const std::function<void()> &onResult,
299  const QString &okText = {});
300 
302 Q_MVVMCORE_EXPORT MessageResult *about(const QString &description,
303  const QUrl &websiteUrl = QUrl(),
304  const QString &licenseName = QString(),
305  const QUrl &licenseUrl = QUrl(),
306  const QString &companyName = QString(),
307  bool addQtVersion = true,
308  const QStringList &extraTopInfos = QStringList(),
309  const QString &extraBottomInfos = QString());
311 
316 Q_MVVMCORE_EXPORT MessageResult *getInput(const QString &title,
318  const QString &text,
319  const char *inputType,
320  const QVariant &defaultValue = {},
321  const QVariantMap &viewProperties = {},
322  const QString &okText = {},
323  const QString &cancelText = {});
325 Q_MVVMCORE_EXPORT void getInput(const QString &title,
326  const QString &text,
327  const char *inputType,
328  QObject *scope,
329  const std::function<void(QVariant)> &onResult,
330  const QVariant &defaultValue = {},
331  const QVariantMap &viewProperties = {},
332  const QString &okText = {},
333  const QString &cancelText = {});
335 Q_MVVMCORE_EXPORT void getInput(const QString &title,
336  const QString &text,
337  const char *inputType,
338  const std::function<void(QVariant)> &onResult,
339  const QVariant &defaultValue = {},
340  const QVariantMap &viewProperties = {},
341  const QString &okText = {},
342  const QString &cancelText = {});
343 
345 template <typename TEdit>
346 inline MessageResult *getInput(const QString &title,
347  const QString &text,
348  const TEdit &defaultValue = {},
349  const QVariantMap &viewProperties = {},
350  const QString &okText = {},
351  const QString &cancelText = {}) {
352  return getInput(title, text, QMetaType::typeName(qMetaTypeId<TEdit>()), QVariant::fromValue(defaultValue), viewProperties, okText, cancelText);
353 }
355 template <typename TEdit>
356 inline void getInput(const QString &title,
357  const QString &text,
358  QObject *scope,
359  const std::function<void(TEdit, bool)> &onResult,
360  const TEdit &defaultValue = {},
361  const QVariantMap &viewProperties = {},
362  const QString &okText = {},
363  const QString &cancelText = {}) {
364  getInput(title, text, QMetaType::typeName(qMetaTypeId<TEdit>()), scope, [onResult](QVariant v) {
365  onResult(v.template value<TEdit>(), v.isValid());
366  }, QVariant::fromValue(defaultValue), viewProperties, okText, cancelText);
367 }
369 template <typename TEdit>
370 inline void getInput(const QString &title,
371  const QString &text,
372  const std::function<void(TEdit, bool)> &onResult,
373  const TEdit &defaultValue = {},
374  const QVariantMap &viewProperties = {},
375  const QString &okText = {},
376  const QString &cancelText = {}) {
377  getInput(title, text, QMetaType::typeName(qMetaTypeId<TEdit>()), [onResult](QVariant v) {
378  onResult(v.template value<TEdit>(), v.isValid());
379  }, QVariant::fromValue(defaultValue), viewProperties, okText, cancelText);
380 }
382 
387 Q_MVVMCORE_EXPORT MessageResult *getExistingDirectory(const QString &title = {},
389  const QUrl &dir = {});
391 Q_MVVMCORE_EXPORT void getExistingDirectory(QObject *scope,
392  const std::function<void(QUrl)> &onResult,
393  const QString &title = {},
394  const QUrl &dir = {});
396 Q_MVVMCORE_EXPORT void getExistingDirectory(const std::function<void(QUrl)> &onResult,
397  const QString &title = {},
398  const QUrl &dir = {});
399 
401 Q_MVVMCORE_EXPORT MessageResult *getOpenFile(const QString &title = {},
402  const QStringList &supportedMimeTypes = {},
403  const QUrl &dir = {});
405 Q_MVVMCORE_EXPORT void getOpenFile(QObject *scope,
406  const std::function<void(QUrl)> &onResult,
407  const QString &title = {},
408  const QStringList &supportedMimeTypes = {},
409  const QUrl &dir = {});
411 Q_MVVMCORE_EXPORT void getOpenFile(const std::function<void(QUrl)> &onResult,
412  const QString &title = {},
413  const QStringList &supportedMimeTypes = {},
414  const QUrl &dir = {});
415 
417 Q_MVVMCORE_EXPORT MessageResult *getOpenFiles(const QString &title = {},
418  const QStringList &supportedMimeTypes = {},
419  const QUrl &dir = {});
421 Q_MVVMCORE_EXPORT void getOpenFiles(QObject *scope,
422  const std::function<void(QList<QUrl>)> &onResult,
423  const QString &title = {},
424  const QStringList &supportedMimeTypes = {},
425  const QUrl &dir = {});
427 Q_MVVMCORE_EXPORT void getOpenFiles(const std::function<void(QList<QUrl>)> &onResult,
428  const QString &title = {},
429  const QStringList &supportedMimeTypes = {},
430  const QUrl &dir = {});
431 
433 Q_MVVMCORE_EXPORT MessageResult *getSaveFile(const QString &title = {},
434  const QStringList &supportedMimeTypes = {},
435  const QUrl &dir = {});
437 Q_MVVMCORE_EXPORT void getSaveFile(QObject *scope,
438  const std::function<void(QUrl)> &onResult,
439  const QString &title = {},
440  const QStringList &supportedMimeTypes = {},
441  const QUrl &dir = {});
443 Q_MVVMCORE_EXPORT void getSaveFile(const std::function<void(QUrl)> &onResult,
444  const QString &title = {},
445  const QStringList &supportedMimeTypes = {},
446  const QUrl &dir = {});
448 
449 }
450 
451 Q_DECLARE_METATYPE(QtMvvm::MessageConfig)
452 Q_DECLARE_METATYPE(QtMvvm::MessageConfig::StandardButton)
453 Q_DECLARE_TYPEINFO(QtMvvm::MessageConfig, Q_MOVABLE_TYPE);
454 Q_DECLARE_METATYPE(QtMvvm::MessageResult*)
455 Q_DECLARE_OPERATORS_FOR_FLAGS(QtMvvm::MessageConfig::StandardButtons)
456 
457 #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:78
static const QByteArray SubTypeSaveFile
A subType for to show a save file dialog.
Definition: message.h:108
static const QByteArray SubTypeOpenFile
A subType for to show an open file dialog.
Definition: message.h:104
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:44
static const QByteArray TypeInputDialog
A type to show a generic input dialog.
Definition: message.h:76
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.
A configuration for a simple dialog to be shown from the core code.
Definition: message.h:18
static const QByteArray SubTypeOpenFiles
A subType for to show an open files dialog.
Definition: message.h:106
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.
static const QByteArray SubTypeQuestion
A subType to show a question message.
Definition: message.h:92
static const QByteArray SubTypeAbout
A subType to show an about dialog.
Definition: message.h:94
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 SubTypeCritical
A subType to show a critical error message.
Definition: message.h:90
QVariant fromValue(const T &value)
const char * typeName(int typeId)
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:172
bool isValid() const
The primary namespace of the QtMvvm library.
Definition: binding.h:10
static const QByteArray SubTypeWarning
A subType to show a warning message.
Definition: message.h:88
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.