QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
inputviewfactory.h
1 #ifndef QTMVVM_INPUTVIEWFACTORY_H
2 #define QTMVVM_INPUTVIEWFACTORY_H
3 
4 #include <QtCore/qurl.h>
5 #include <QtCore/qvariant.h>
6 #include <QtCore/qscopedpointer.h>
7 #include <QtCore/qobject.h>
8 #include <QtCore/qmetaobject.h>
9 #include <QtMvvmCore/qtmvvmcore_global.h>
10 
11 #include "QtMvvmQuick/qtmvvmquick_global.h"
12 
13 namespace QtMvvm {
14 
16 class Q_MVVMQUICK_EXPORT Formatter
17 {
18  Q_DISABLE_COPY(Formatter)
19 
20 public:
21  Formatter();
22  virtual ~Formatter();
23 
25  virtual QString format(const QString &formatString,
26  const QVariant &value,
27  const QVariantMap &viewProperties) const = 0;
28 };
29 
31 template <typename T>
32 class SimpleFormatter : public Formatter
33 {
34 public:
35  inline QString format(const QString &formatString,
36  const QVariant &value,
37  const QVariantMap &viewProperties) const override;
38 };
39 
40 class InputViewFactoryPrivate;
42 class Q_MVVMQUICK_EXPORT InputViewFactory : public QObject
43 {
44  Q_OBJECT
45 
46 public:
48  Q_INVOKABLE InputViewFactory(QObject *parent = nullptr);
49  ~InputViewFactory() override;
50 
52  Q_INVOKABLE virtual QUrl getInputUrl(const QByteArray &type, const QVariantMap &viewProperties);
54  Q_INVOKABLE virtual QUrl getDelegate(const QByteArray &type, const QVariantMap &viewProperties);
55 
57  QTMVVM_REVISION_1 Q_INVOKABLE QString format(const QByteArray &type,
58  const QString &formatString,
59  const QVariant &value,
60  const QVariantMap &viewProperties);
61 
63  template <typename TType>
64  inline void addSimpleInput(const QUrl &qmlFileUrl);
66  Q_INVOKABLE virtual void addSimpleInput(const QByteArray &type, const QUrl &qmlFileUrl);
67 
69  template <typename TType>
70  inline void addSimpleDelegate(const QUrl &qmlFileUrl);
72  Q_INVOKABLE virtual void addSimpleDelegate(const QByteArray &type, const QUrl &qmlFileUrl);
73 
75  template <typename TType>
76  inline void addFormatter(Formatter *formatter);
78  void addFormatter(const QByteArray &type, Formatter *formatter); //MAJOR make virtual
79 
81  template <typename TAliasType, typename TTargetType>
82  inline void addInputAlias();
84  Q_INVOKABLE virtual void addInputAlias(const QByteArray &alias, const QByteArray &targetType);
85 
87  template <typename TAliasType, typename TTargetType>
88  inline void addDelegateAlias();
90  Q_INVOKABLE virtual void addDelegateAlias(const QByteArray &alias, const QByteArray &targetType);
91 
93  template <typename TAliasType, typename TTargetType>
94  inline void addFormatterAlias();
96  QTMVVM_REVISION_1 Q_INVOKABLE void addFormatterAlias(const QByteArray &alias, const QByteArray &targetType);
97 
98 private:
100 };
101 
102 template<typename TType>
103 inline void InputViewFactory::addSimpleInput(const QUrl &qmlFileUrl)
104 {
105  addSimpleInput(QMetaType::typeName(qMetaTypeId<TType>()), qmlFileUrl);
106 }
107 
108 template<typename TType>
110 {
111  addSimpleDelegate(QMetaType::typeName(qMetaTypeId<TType>()), qmlFileUrl);
112 }
113 
114 template<typename TType>
116 {
117  addFormatter(QMetaType::typeName(qMetaTypeId<TType>()), formatter);
118 }
119 
120 template<typename TAliasType, typename TTargetType>
122 {
123  addInputAlias(QMetaType::typeName(qMetaTypeId<TAliasType>()), QMetaType::typeName(qMetaTypeId<TTargetType>()));
124 }
125 
126 template<typename TAliasType, typename TTargetType>
128 {
129  addDelegateAlias(QMetaType::typeName(qMetaTypeId<TAliasType>()), QMetaType::typeName(qMetaTypeId<TTargetType>()));
130 }
131 
132 template<typename TAliasType, typename TTargetType>
134 {
135  addFormatterAlias(QMetaType::typeName(qMetaTypeId<TAliasType>()), QMetaType::typeName(qMetaTypeId<TTargetType>()));
136 }
137 
138 
139 
140 template<typename T>
141 QString SimpleFormatter<T>::format(const QString &formatString, const QVariant &value, const QVariantMap &viewProperties) const
142 {
143  Q_UNUSED(viewProperties);
144  return formatString.arg(value.template value<T>());
145 }
146 
147 }
148 
149 #endif // QTMVVM_INPUTVIEWFACTORY_H
A factory class to generate input edit views by their type names.
void addFormatter(Formatter *formatter)
Adds a new Formatter to format delegate preview texts for the given type.
QString format(const QString &formatString, const QVariant &value, const QVariantMap &viewProperties) const override
Is called to format a value to a localized, human readable text.
A very basic formatter that uses QString::arg with the value on the format string.
A class to format the preview text in the delegates.
void addSimpleInput(const QUrl &qmlFileUrl)
Adds a new QML file to create views for the given type.
void addInputAlias()
Adds a type name alias for views.
void addDelegateAlias()
Adds a type name alias for delegates.
const char * typeName(int typeId)
void addFormatterAlias()
Adds a type name alias for formatters.
void addSimpleDelegate(const QUrl &qmlFileUrl)
Adds a new QML file to create delegates for the given type.
The primary namespace of the QtMvvm library.
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const