]> Johnzone git - learnqt-cm.git/commitdiff
Chapter 5 decorators done
authorJohn Janus <mail@johnzone.org>
Thu, 6 Sep 2018 23:17:43 +0000 (01:17 +0200)
committerJohn Janus <mail@johnzone.org>
Thu, 6 Sep 2018 23:17:43 +0000 (01:17 +0200)
13 files changed:
cm-lib/cm-lib.pro
cm-lib/src/data/datadecorator.cpp [new file with mode: 0644]
cm-lib/src/data/datadecorator.h [new file with mode: 0644]
cm-lib/src/data/datetimedecorator.cpp [new file with mode: 0644]
cm-lib/src/data/datetimedecorator.h [new file with mode: 0644]
cm-lib/src/data/entity.cpp [new file with mode: 0644]
cm-lib/src/data/entity.h [new file with mode: 0644]
cm-lib/src/data/enumeratordecorator.cpp [new file with mode: 0644]
cm-lib/src/data/enumeratordecorator.h [new file with mode: 0644]
cm-lib/src/data/intdecorator.cpp [new file with mode: 0644]
cm-lib/src/data/intdecorator.h [new file with mode: 0644]
cm-lib/src/data/stringdecorator.cpp [new file with mode: 0644]
cm-lib/src/data/stringdecorator.h [new file with mode: 0644]

index b37202131805548bb127b56b210aa6a82e1a1047..b554948e4d03a7424e824cf0127aeb98b5cb902a 100644 (file)
@@ -33,7 +33,13 @@ SOURCES += \
         src/models/client.cpp \
     src/controllers/master-controller.cpp \
     src/framework/command.cpp \
-    src/controllers/commandcontroller.cpp
+    src/controllers/commandcontroller.cpp \
+    src/data/datadecorator.cpp \
+    src/data/stringdecorator.cpp \
+    src/data/intdecorator.cpp \
+    src/data/datetimedecorator.cpp \
+    src/data/enumeratordecorator.cpp \
+    src/data/entity.cpp
 
 HEADERS += \
         src/models/client.h \
@@ -41,7 +47,13 @@ HEADERS += \
     src/controllers/master-controller.h \
     src/controllers/navigation-controller.h \
     src/framework/command.h \
-    src/controllers/commandcontroller.h
+    src/controllers/commandcontroller.h \
+    src/data/datadecorator.h \
+    src/data/stringdecorator.h \
+    src/data/intdecorator.h \
+    src/data/datetimedecorator.h \
+    src/data/enumeratordecorator.h \
+    src/data/entity.h
 
 unix {
     target.path = /usr/lib
diff --git a/cm-lib/src/data/datadecorator.cpp b/cm-lib/src/data/datadecorator.cpp
new file mode 100644 (file)
index 0000000..64f9e8e
--- /dev/null
@@ -0,0 +1,42 @@
+#include "datadecorator.h"
+
+namespace cm::data {
+
+  class DataDecorator::Implementation {
+  public:
+    Implementation(Entity* _parent, const QString& _key, const QString& _label)
+      : parentEntity(_parent)
+      , key(_key)
+      , label(_label)
+    {
+    }
+
+    Entity* parentEntity{nullptr};
+    QString key;
+    QString label;
+
+  };
+
+  DataDecorator::DataDecorator(Entity* parent, const QString& key, const QString& label)
+    : QObject(dynamic_cast<QObject*>(parent))
+  {
+    implementation.reset(new Implementation(parent, key, label));
+  }
+
+  DataDecorator::~DataDecorator()
+  {}
+
+  const QString& DataDecorator::key() const
+  {
+    return implementation->key;
+  }
+
+  const QString& DataDecorator::label() const
+  {
+    return implementation->label;
+  }
+
+  Entity* DataDecorator::parentEntity() {
+    return implementation->parentEntity;
+  }
+}
diff --git a/cm-lib/src/data/datadecorator.h b/cm-lib/src/data/datadecorator.h
new file mode 100644 (file)
index 0000000..0d6e7ba
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef DATADECORATOR_H
+#define DATADECORATOR_H
+
+#include <QObject>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QScopedPointer>
+
+#include "cm-lib_global.h"
+#include "entity.h"
+
+namespace cm::data {
+
+  class CMLIBSHARED_EXPORT DataDecorator : public QObject
+  {
+    Q_OBJECT
+    Q_PROPERTY( QString ui_label READ label CONSTANT )
+  public:
+    DataDecorator(Entity* parent = nullptr, const QString& key = "SomeItemKey", const QString& label = "");
+
+    virtual ~DataDecorator();
+
+    const QString& key() const;
+    const QString& label() const;
+
+    Entity* parentEntity();
+
+    virtual QJsonValue jsonValue() const = 0;
+    virtual void update(const QJsonObject& jsonObject) = 0;
+
+  private:
+    class Implementation;
+    QScopedPointer<Implementation> implementation;
+  };
+}
+#endif // DATADECORATOR_H
diff --git a/cm-lib/src/data/datetimedecorator.cpp b/cm-lib/src/data/datetimedecorator.cpp
new file mode 100644 (file)
index 0000000..043b6eb
--- /dev/null
@@ -0,0 +1,80 @@
+#include "datetimedecorator.h"
+
+#include <QVariant>
+
+namespace cm::data {
+
+  class DateTimeDecorator::Implementation {
+   public:
+    Implementation(DateTimeDecorator* _dateTimeDecorator, const QDateTime& _value)
+      : dateTimeDecorator(_dateTimeDecorator)
+      , value(_value)
+    {}
+
+    DateTimeDecorator* dateTimeDecorator{nullptr};
+    QDateTime value;
+  };
+
+  DateTimeDecorator::DateTimeDecorator(Entity* parentEntity, const QString& key, const QString& label, const QDateTime& value)
+    : DataDecorator(parentEntity, key, label)
+  {
+    implementation.reset(new Implementation(this, value));
+  }
+
+  DateTimeDecorator::~DateTimeDecorator()
+  {}
+
+  const QDateTime& DateTimeDecorator::value() const
+  {
+    return implementation->value;
+  }
+
+  DateTimeDecorator& DateTimeDecorator::setValue(const QDateTime& value)
+  {
+    if (value != implementation->value) {
+      // ... Validation required ...
+      implementation->value = value;
+      emit valueChanged();
+    }
+    return *this;
+  }
+
+  QJsonValue DateTimeDecorator::jsonValue() const
+  {
+    return QJsonValue::fromVariant(QVariant(implementation->value));
+  }
+
+  void DateTimeDecorator::update(const QJsonObject& _jsonObject)
+  {
+    if (_jsonObject.contains(key())) {
+      setValue(QDateTime::fromString(_jsonObject.value(key()).toString()));
+    } else {
+      setValue(QDateTime());
+    }
+  }
+
+  QString DateTimeDecorator::toIso8601String() const
+  {
+    if (implementation->value.isNull()) return "Not Set";
+    return implementation->value.toString(Qt::ISODate);
+  }
+
+  QString DateTimeDecorator::toPrettyString() const
+  {
+    if (implementation->value.isNull()) return "Not Set";
+    return implementation->value.toString("ddd d MMM yyyy @ HH:mm:ss");
+  }
+
+  QString DateTimeDecorator::toPrettyDateString() const
+  {
+    if (implementation->value.isNull()) return "Not Set";
+    return implementation->value.toString("d MMM yyyy");
+  }
+
+  QString DateTimeDecorator::toPrettyTimeString() const
+  {
+    if (implementation->value.isNull()) return "Not Set";
+    return implementation->value.toString("hh:mm ap");
+  }
+
+}
diff --git a/cm-lib/src/data/datetimedecorator.h b/cm-lib/src/data/datetimedecorator.h
new file mode 100644 (file)
index 0000000..52ba543
--- /dev/null
@@ -0,0 +1,48 @@
+#ifndef DATETIMEDECORATOR_H
+#define DATETIMEDECORATOR_H
+
+#include <QObject>
+#include <QString>
+#include <QDateTime>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QScopedPointer>
+
+#include "cm-lib_global.h"
+#include "data/datadecorator.h"
+#include "data/entity.h"
+
+namespace cm::data {
+
+  class CMLIBSHARED_EXPORT DateTimeDecorator : public DataDecorator
+  {
+    Q_OBJECT
+    Q_PROPERTY(QDateTime ui_value READ value WRITE setValue NOTIFY valueChanged)
+    Q_PROPERTY(QString ui_iso8601String READ toIso8601String NOTIFY valueChanged)
+    Q_PROPERTY(QString ui_prettyDateString READ toPrettyDateString NOTIFY valueChanged)
+    Q_PROPERTY(QString ui_prettyTimeStringString READ toPrettyTimeString NOTIFY valueChanged)
+    Q_PROPERTY(QString ui_prettyString READ toPrettyString NOTIFY valueChanged)
+  public:
+    DateTimeDecorator(Entity* parentEntity = nullptr,
+                    const QString& key = "SomeItemKey", const QString& label = "", const QDateTime& value = QDateTime());
+
+    ~DateTimeDecorator() override;
+
+    DateTimeDecorator& setValue(const QDateTime& value);
+    const QDateTime& value() const;
+    QJsonValue jsonValue() const override;
+    void update(const QJsonObject& jsonObject) override;
+    QString toIso8601String() const;
+    QString toPrettyDateString() const;
+    QString toPrettyTimeString() const;
+    QString toPrettyString() const;
+
+  signals:
+    void valueChanged();
+
+  private:
+    class Implementation;
+    QScopedPointer<Implementation> implementation;
+  };
+}
+#endif // DATETIMEDECORATOR_H
diff --git a/cm-lib/src/data/entity.cpp b/cm-lib/src/data/entity.cpp
new file mode 100644 (file)
index 0000000..e74d956
--- /dev/null
@@ -0,0 +1,6 @@
+#include "entity.h"
+
+Entity::Entity(QObject *parent) : QObject(parent)
+{
+
+}
diff --git a/cm-lib/src/data/entity.h b/cm-lib/src/data/entity.h
new file mode 100644 (file)
index 0000000..f0d628d
--- /dev/null
@@ -0,0 +1,17 @@
+#ifndef ENTITY_H
+#define ENTITY_H
+
+#include <QObject>
+
+class Entity : public QObject
+{
+  Q_OBJECT
+public:
+  explicit Entity(QObject *parent = nullptr);
+
+signals:
+
+public slots:
+};
+
+#endif // ENTITY_H
\ No newline at end of file
diff --git a/cm-lib/src/data/enumeratordecorator.cpp b/cm-lib/src/data/enumeratordecorator.cpp
new file mode 100644 (file)
index 0000000..bafbc14
--- /dev/null
@@ -0,0 +1,67 @@
+#include "enumeratordecorator.h"
+
+#include <QVariant>
+
+namespace cm::data {
+
+  class EnumeratorDecorator::Implementation {
+   public:
+    Implementation(EnumeratorDecorator* _enumeratorDecorator, const int _value, const std::map<int, QString>& _descriptionMapper)
+      : enumeratorDecorator(_enumeratorDecorator)
+      , value(_value)
+      , descriptionMapper(_descriptionMapper)
+    {}
+
+    EnumeratorDecorator* enumeratorDecorator{nullptr};
+    int value;
+    std::map<int, QString> descriptionMapper;
+  };
+
+  EnumeratorDecorator::EnumeratorDecorator(Entity* parentEntity, const QString& key, const QString& label,
+                                           const int value, const std::map<int, QString>& descriptionMapper)
+    : DataDecorator(parentEntity, key, label)
+  {
+    implementation.reset(new Implementation(this, value, descriptionMapper));
+  }
+
+  EnumeratorDecorator::~EnumeratorDecorator()
+  {}
+
+  int EnumeratorDecorator::value() const
+  {
+    return implementation->value;
+  }
+
+  EnumeratorDecorator& EnumeratorDecorator::setValue(const int value)
+  {
+    if (value != implementation->value) {
+      // ... Validation required ...
+      implementation->value = value;
+      emit valueChanged();
+    }
+    return *this;
+  }
+
+  QJsonValue EnumeratorDecorator::jsonValue() const
+  {
+    return QJsonValue::fromVariant(QVariant(implementation->value));
+  }
+
+  void EnumeratorDecorator::update(const QJsonObject& _jsonObject)
+  {
+    if (_jsonObject.contains(key())) {
+      setValue(_jsonObject.value(key()).toInt());
+    } else {
+      setValue(0);
+    }
+  }
+
+  QString EnumeratorDecorator::valueDescription() const
+  {
+    if (implementation->descriptionMapper.find(implementation->value) != implementation->descriptionMapper.end()) {
+      return implementation->descriptionMapper.at(implementation->value);
+    }
+    return {};
+  }
+
+}
diff --git a/cm-lib/src/data/enumeratordecorator.h b/cm-lib/src/data/enumeratordecorator.h
new file mode 100644 (file)
index 0000000..6081515
--- /dev/null
@@ -0,0 +1,45 @@
+#ifndef ENUMERATORDECORATOR_H
+#define ENUMERATORDECORATOR_H
+
+#include <map>
+
+#include <QObject>
+#include <QScopedPointer>
+#include <QJsonObject>
+#include <QJsonValue>
+
+#include "data/datadecorator.h"
+#include "cm-lib_global.h"
+
+namespace cm::data {
+
+  class CMLIBSHARED_EXPORT EnumeratorDecorator : public DataDecorator
+  {
+    Q_OBJECT
+    Q_PROPERTY(int ui_value READ value WRITE setValue NOTIFY valueChanged)
+    Q_PROPERTY(QString ui_valueDescription READ valueDescription NOTIFY valueChanged)
+
+  public:
+    EnumeratorDecorator(Entity* parentEntity = nullptr,
+                        const QString& key = "SomeItemKey", const QString& label = "", int value = 0,
+                        const std::map<int, QString>& descriptionMapper = std::map<int, QString>());
+
+    ~EnumeratorDecorator() override;
+
+    EnumeratorDecorator& setValue(int value);
+    int value() const;
+    QString valueDescription() const;
+
+    QJsonValue jsonValue() const override;
+    void update(const QJsonObject& jsonObject) override;
+
+  signals:
+    void valueChanged();
+
+  private:
+    class Implementation;
+    QScopedPointer<Implementation> implementation;
+  };
+}
+
+#endif // ENUMERATORDECORATOR_H
diff --git a/cm-lib/src/data/intdecorator.cpp b/cm-lib/src/data/intdecorator.cpp
new file mode 100644 (file)
index 0000000..e39aed8
--- /dev/null
@@ -0,0 +1,56 @@
+#include "intdecorator.h"
+
+#include <QVariant>
+
+namespace cm::data {
+
+  class IntDecorator::Implementation {
+   public:
+    Implementation(IntDecorator* _intDecorator, const int _value)
+      : intDecorator(_intDecorator)
+      , value(_value)
+    {}
+
+    IntDecorator* intDecorator{nullptr};
+    int value;
+  };
+
+  IntDecorator::IntDecorator(Entity* parentEntity, const QString& key, const QString& label, const int value)
+    : DataDecorator(parentEntity, key, label)
+  {
+    implementation.reset(new Implementation(this, value));
+  }
+
+  IntDecorator::~IntDecorator()
+  {}
+
+  int IntDecorator::value() const
+  {
+    return implementation->value;
+  }
+
+  IntDecorator& IntDecorator::setValue(const int& value)
+  {
+    if (value != implementation->value) {
+      // ... Validation required ...
+      implementation->value = value;
+      emit valueChanged();
+    }
+    return *this;
+  }
+
+  QJsonValue IntDecorator::jsonValue() const
+  {
+    return QJsonValue::fromVariant(QVariant(implementation->value));
+  }
+
+  void IntDecorator::update(const QJsonObject& _jsonObject)
+  {
+    if (_jsonObject.contains(key())) {
+      setValue(_jsonObject.value(key()).toInt());
+    } else {
+      setValue(0);
+    }
+  }
+
+}
diff --git a/cm-lib/src/data/intdecorator.h b/cm-lib/src/data/intdecorator.h
new file mode 100644 (file)
index 0000000..0174347
--- /dev/null
@@ -0,0 +1,39 @@
+#ifndef INTDECORATOR_H
+#define INTDECORATOR_H
+
+#include <QObject>
+#include <QString>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QScopedPointer>
+
+#include "cm-lib_global.h"
+#include "data/datadecorator.h"
+#include "data/entity.h"
+
+namespace cm::data {
+
+  class CMLIBSHARED_EXPORT IntDecorator : public DataDecorator
+  {
+    Q_OBJECT
+    Q_PROPERTY(int ui_value READ value WRITE setValue NOTIFY valueChanged)
+  public:
+    IntDecorator(Entity* parentEntity = nullptr,
+                    const QString& key = "SomeItemKey", const QString& label = "", const int value = 0);
+
+    ~IntDecorator() override;
+
+    IntDecorator& setValue(const int& value);
+    int value() const;
+    QJsonValue jsonValue() const override;
+    void update(const QJsonObject& jsonObject) override;
+
+  signals:
+    void valueChanged();
+
+  private:
+    class Implementation;
+    QScopedPointer<Implementation> implementation;
+  };
+}
+#endif // INTDECORATOR_H
diff --git a/cm-lib/src/data/stringdecorator.cpp b/cm-lib/src/data/stringdecorator.cpp
new file mode 100644 (file)
index 0000000..6f9e0f1
--- /dev/null
@@ -0,0 +1,56 @@
+#include "stringdecorator.h"
+
+#include <QVariant>
+
+namespace cm::data {
+
+  class StringDecorator::Implementation {
+   public:
+    Implementation(StringDecorator* _stringDecorator, const QString& _value)
+      : stringDecorator(_stringDecorator)
+      , value(_value)
+    {}
+
+    StringDecorator* stringDecorator{nullptr};
+    QString value;
+  };
+
+  StringDecorator::StringDecorator(Entity* parentEntity, const QString& key, const QString& label, const QString& value)
+    : DataDecorator(parentEntity, key, label)
+  {
+    implementation.reset(new Implementation(this, value));
+  }
+
+  StringDecorator::~StringDecorator()
+  {}
+
+  const QString& StringDecorator::value() const
+  {
+    return implementation->value;
+  }
+
+  StringDecorator& StringDecorator::setValue(const QString& value)
+  {
+    if (value != implementation->value) {
+      // ... Validation required ...
+      implementation->value = value;
+      emit valueChanged();
+    }
+    return *this;
+  }
+
+  QJsonValue StringDecorator::jsonValue() const
+  {
+    return QJsonValue::fromVariant(QVariant(implementation->value));
+  }
+
+  void StringDecorator::update(const QJsonObject& _jsonObject)
+  {
+    if (_jsonObject.contains(key())) {
+      setValue(_jsonObject.value(key()).toString());
+    } else {
+      setValue("");
+    }
+  }
+
+}
diff --git a/cm-lib/src/data/stringdecorator.h b/cm-lib/src/data/stringdecorator.h
new file mode 100644 (file)
index 0000000..a62b56c
--- /dev/null
@@ -0,0 +1,40 @@
+#ifndef STRINGDECORATOR_H
+#define STRINGDECORATOR_H
+
+#include <QObject>
+#include <QString>
+#include <QJsonObject>
+#include <QJsonValue>
+#include <QScopedPointer>
+
+#include "cm-lib_global.h"
+#include "data/datadecorator.h"
+#include "data/entity.h"
+
+namespace cm::data {
+
+  class CMLIBSHARED_EXPORT StringDecorator : public DataDecorator
+  {
+    Q_OBJECT
+    Q_PROPERTY(QString ui_value READ value WRITE setValue NOTIFY valueChanged)
+  public:
+    StringDecorator(Entity* parentEntity = nullptr,
+                    const QString& key = "SomeItemKey", const QString& label = "", const QString& value = "");
+
+    ~StringDecorator() override;
+
+    StringDecorator& setValue(const QString& value);
+    const QString& value() const;
+    QJsonValue jsonValue() const override;
+    void update(const QJsonObject& jsonObject) override;
+
+  signals:
+    void valueChanged();
+
+  private:
+    class Implementation;
+    QScopedPointer<Implementation> implementation;
+  };
+}
+
+#endif // STRINGDECORATOR_H