From 79fac33d42d4cefbfea4bbf85e9d3065bffc181e Mon Sep 17 00:00:00 2001 From: John Janus Date: Fri, 7 Sep 2018 01:17:43 +0200 Subject: [PATCH] Chapter 5 decorators done --- cm-lib/cm-lib.pro | 16 ++++- cm-lib/src/data/datadecorator.cpp | 42 +++++++++++++ cm-lib/src/data/datadecorator.h | 36 +++++++++++ cm-lib/src/data/datetimedecorator.cpp | 80 +++++++++++++++++++++++++ cm-lib/src/data/datetimedecorator.h | 48 +++++++++++++++ cm-lib/src/data/entity.cpp | 6 ++ cm-lib/src/data/entity.h | 17 ++++++ cm-lib/src/data/enumeratordecorator.cpp | 67 +++++++++++++++++++++ cm-lib/src/data/enumeratordecorator.h | 45 ++++++++++++++ cm-lib/src/data/intdecorator.cpp | 56 +++++++++++++++++ cm-lib/src/data/intdecorator.h | 39 ++++++++++++ cm-lib/src/data/stringdecorator.cpp | 56 +++++++++++++++++ cm-lib/src/data/stringdecorator.h | 40 +++++++++++++ 13 files changed, 546 insertions(+), 2 deletions(-) create mode 100644 cm-lib/src/data/datadecorator.cpp create mode 100644 cm-lib/src/data/datadecorator.h create mode 100644 cm-lib/src/data/datetimedecorator.cpp create mode 100644 cm-lib/src/data/datetimedecorator.h create mode 100644 cm-lib/src/data/entity.cpp create mode 100644 cm-lib/src/data/entity.h create mode 100644 cm-lib/src/data/enumeratordecorator.cpp create mode 100644 cm-lib/src/data/enumeratordecorator.h create mode 100644 cm-lib/src/data/intdecorator.cpp create mode 100644 cm-lib/src/data/intdecorator.h create mode 100644 cm-lib/src/data/stringdecorator.cpp create mode 100644 cm-lib/src/data/stringdecorator.h diff --git a/cm-lib/cm-lib.pro b/cm-lib/cm-lib.pro index b372021..b554948 100644 --- a/cm-lib/cm-lib.pro +++ b/cm-lib/cm-lib.pro @@ -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 index 0000000..64f9e8e --- /dev/null +++ b/cm-lib/src/data/datadecorator.cpp @@ -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(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 index 0000000..0d6e7ba --- /dev/null +++ b/cm-lib/src/data/datadecorator.h @@ -0,0 +1,36 @@ +#ifndef DATADECORATOR_H +#define DATADECORATOR_H + +#include +#include +#include +#include + +#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; + }; +} +#endif // DATADECORATOR_H diff --git a/cm-lib/src/data/datetimedecorator.cpp b/cm-lib/src/data/datetimedecorator.cpp new file mode 100644 index 0000000..043b6eb --- /dev/null +++ b/cm-lib/src/data/datetimedecorator.cpp @@ -0,0 +1,80 @@ +#include "datetimedecorator.h" + +#include + +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 index 0000000..52ba543 --- /dev/null +++ b/cm-lib/src/data/datetimedecorator.h @@ -0,0 +1,48 @@ +#ifndef DATETIMEDECORATOR_H +#define DATETIMEDECORATOR_H + +#include +#include +#include +#include +#include +#include + +#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; + }; +} +#endif // DATETIMEDECORATOR_H diff --git a/cm-lib/src/data/entity.cpp b/cm-lib/src/data/entity.cpp new file mode 100644 index 0000000..e74d956 --- /dev/null +++ b/cm-lib/src/data/entity.cpp @@ -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 index 0000000..f0d628d --- /dev/null +++ b/cm-lib/src/data/entity.h @@ -0,0 +1,17 @@ +#ifndef ENTITY_H +#define ENTITY_H + +#include + +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 index 0000000..bafbc14 --- /dev/null +++ b/cm-lib/src/data/enumeratordecorator.cpp @@ -0,0 +1,67 @@ +#include "enumeratordecorator.h" + +#include + +namespace cm::data { + + class EnumeratorDecorator::Implementation { + public: + Implementation(EnumeratorDecorator* _enumeratorDecorator, const int _value, const std::map& _descriptionMapper) + : enumeratorDecorator(_enumeratorDecorator) + , value(_value) + , descriptionMapper(_descriptionMapper) + {} + + EnumeratorDecorator* enumeratorDecorator{nullptr}; + int value; + std::map descriptionMapper; + }; + + EnumeratorDecorator::EnumeratorDecorator(Entity* parentEntity, const QString& key, const QString& label, + const int value, const std::map& 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 index 0000000..6081515 --- /dev/null +++ b/cm-lib/src/data/enumeratordecorator.h @@ -0,0 +1,45 @@ +#ifndef ENUMERATORDECORATOR_H +#define ENUMERATORDECORATOR_H + +#include + +#include +#include +#include +#include + +#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& descriptionMapper = std::map()); + + ~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; + }; +} + +#endif // ENUMERATORDECORATOR_H diff --git a/cm-lib/src/data/intdecorator.cpp b/cm-lib/src/data/intdecorator.cpp new file mode 100644 index 0000000..e39aed8 --- /dev/null +++ b/cm-lib/src/data/intdecorator.cpp @@ -0,0 +1,56 @@ +#include "intdecorator.h" + +#include + +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 index 0000000..0174347 --- /dev/null +++ b/cm-lib/src/data/intdecorator.h @@ -0,0 +1,39 @@ +#ifndef INTDECORATOR_H +#define INTDECORATOR_H + +#include +#include +#include +#include +#include + +#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; + }; +} +#endif // INTDECORATOR_H diff --git a/cm-lib/src/data/stringdecorator.cpp b/cm-lib/src/data/stringdecorator.cpp new file mode 100644 index 0000000..6f9e0f1 --- /dev/null +++ b/cm-lib/src/data/stringdecorator.cpp @@ -0,0 +1,56 @@ +#include "stringdecorator.h" + +#include + +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 index 0000000..a62b56c --- /dev/null +++ b/cm-lib/src/data/stringdecorator.h @@ -0,0 +1,40 @@ +#ifndef STRINGDECORATOR_H +#define STRINGDECORATOR_H + +#include +#include +#include +#include +#include + +#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; + }; +} + +#endif // STRINGDECORATOR_H -- 2.47.1