From: John Janus Date: Thu, 13 Sep 2018 21:25:25 +0000 (+0200) Subject: c++ part of data models done X-Git-Url: https://git.johnzone.org/?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=learnqt-cm.git c++ part of data models done --- diff --git a/cm-lib/cm-lib.pro b/cm-lib/cm-lib.pro index 0aaaec7..f8d6815 100644 --- a/cm-lib/cm-lib.pro +++ b/cm-lib/cm-lib.pro @@ -39,7 +39,10 @@ SOURCES += \ src/data/intdecorator.cpp \ src/data/datetimedecorator.cpp \ src/data/enumeratordecorator.cpp \ - src/data/entity.cpp + src/data/entity.cpp \ + src/models/address.cpp \ + src/models/appointment.cpp \ + src/models/contact.cpp HEADERS += \ src/models/client.h \ @@ -54,7 +57,10 @@ HEADERS += \ src/data/datetimedecorator.h \ src/data/enumeratordecorator.h \ src/data/entity.h \ - src/data/entity-collection.h + src/data/entity-collection.h \ + src/models/address.h \ + src/models/appointment.h \ + src/models/contact.h unix { target.path = /usr/lib diff --git a/cm-lib/src/controllers/master-controller.cpp b/cm-lib/src/controllers/master-controller.cpp index f6b95ac..2e812db 100644 --- a/cm-lib/src/controllers/master-controller.cpp +++ b/cm-lib/src/controllers/master-controller.cpp @@ -10,11 +10,13 @@ namespace cm::controllers { { navigationController = new NavigationController(_masterController); commandController = new CommandController(_masterController); + newClient = new models::Client(masterController); } MasterController* masterController{nullptr}; NavigationController* navigationController{nullptr}; CommandController* commandController{nullptr}; + models::Client* newClient{nullptr}; QString welcomeMessage = "This is MasterController to Major Tom"; }; @@ -38,6 +40,11 @@ namespace cm::controllers { return implementation->commandController; } + models::Client* MasterController::newClient() + { + return implementation->newClient; + } + const QString& MasterController::welcomeMessage() const { return implementation->welcomeMessage; diff --git a/cm-lib/src/controllers/master-controller.h b/cm-lib/src/controllers/master-controller.h index bf2db84..33fe2b1 100644 --- a/cm-lib/src/controllers/master-controller.h +++ b/cm-lib/src/controllers/master-controller.h @@ -8,15 +8,17 @@ #include "cm-lib_global.h" #include "controllers/navigation-controller.h" #include "controllers/commandcontroller.h" +#include "models/client.h" namespace cm::controllers { class CMLIBSHARED_EXPORT MasterController : public QObject { Q_OBJECT - Q_PROPERTY( QString ui_welcomeMessage READ welcomeMessage CONSTANT ) - Q_PROPERTY( cm::controllers::NavigationController* ui_navigationController READ navigationController CONSTANT ) - Q_PROPERTY( cm::controllers::CommandController* ui_commandController READ commandController CONSTANT ) + Q_PROPERTY( QString ui_welcomeMessage READ welcomeMessage CONSTANT ) + Q_PROPERTY( cm::controllers::NavigationController* ui_navigationController READ navigationController CONSTANT ) + Q_PROPERTY( cm::controllers::CommandController* ui_commandController READ commandController CONSTANT ) + Q_PROPERTY( cm::models::Client* ui_newClient READ newClient CONSTANT ) public: explicit MasterController(QObject *parent = nullptr); @@ -24,6 +26,7 @@ public: NavigationController* navigationController(); CommandController* commandController(); + models::Client* newClient(); const QString& welcomeMessage() const; private: diff --git a/cm-lib/src/data/entity-collection.h b/cm-lib/src/data/entity-collection.h index 5b8901e..d6381f8 100644 --- a/cm-lib/src/data/entity-collection.h +++ b/cm-lib/src/data/entity-collection.h @@ -67,7 +67,7 @@ namespace cm::data { void clear() override { for (auto entity : collection) { - entity->deletaLater(); + entity->deleteLater(); } collection.clear(); } diff --git a/cm-lib/src/models/address.cpp b/cm-lib/src/models/address.cpp new file mode 100644 index 0000000..9739267 --- /dev/null +++ b/cm-lib/src/models/address.cpp @@ -0,0 +1,24 @@ +#include "address.h" + +namespace cm::models { + + Address::Address(QObject* parent) + : Entity(parent, "address") + { + building = static_cast(addDataItem(new data::StringDecorator(this, "building", "Building"))); + street = static_cast(addDataItem(new data::StringDecorator(this, "stree", "Street"))); + city = static_cast(addDataItem(new data::StringDecorator(this, "city", "City"))); + postcode = static_cast(addDataItem(new data::StringDecorator(this, "postcode", "Post Code"))); + } + + Address::Address(QObject* parent, const QJsonObject& json) + : Address(parent) + { + update(json); + } + + QString Address::fullAddress() const + { + return QString("%1 %2\n%3\n%4").arg(building->value(), street->value(), city->value(), postcode->value()); + } +} diff --git a/cm-lib/src/models/address.h b/cm-lib/src/models/address.h new file mode 100644 index 0000000..d24be49 --- /dev/null +++ b/cm-lib/src/models/address.h @@ -0,0 +1,36 @@ +#ifndef ADDRESS_H +#define ADDRESS_H + +#include +#include +#include + +#include "data/entity.h" +#include "data/stringdecorator.h" +#include "cm-lib_global.h" + +namespace cm::models { + + class CMLIBSHARED_EXPORT Address : public data::Entity + { + Q_OBJECT + Q_PROPERTY(cm::data::StringDecorator* ui_building MEMBER building CONSTANT) + Q_PROPERTY(cm::data::StringDecorator* ui_street MEMBER street CONSTANT) + Q_PROPERTY(cm::data::StringDecorator* ui_city MEMBER city CONSTANT) + Q_PROPERTY(cm::data::StringDecorator* ui_postcode MEMBER postcode CONSTANT) + Q_PROPERTY(QString ui_fullAddress READ fullAddress CONSTANT) + + public: + explicit Address(QObject* parent = nullptr); + Address(QObject* parent, const QJsonObject& json); + + data::StringDecorator* building{nullptr}; + data::StringDecorator* street{nullptr}; + data::StringDecorator* city{nullptr}; + data::StringDecorator* postcode{nullptr}; + + QString fullAddress() const; + }; +} + +#endif // ADDRESS_H diff --git a/cm-lib/src/models/appointment.cpp b/cm-lib/src/models/appointment.cpp new file mode 100644 index 0000000..1d70596 --- /dev/null +++ b/cm-lib/src/models/appointment.cpp @@ -0,0 +1,21 @@ +#include "appointment.h" + +namespace cm::models { + + Appointment::Appointment(QObject* parent) + : Entity(parent, "appointment") + { + startAt = static_cast( + addDataItem(new data::DateTimeDecorator(this,"startAt", "Starts at"))); + endAt = static_cast( + addDataItem(new data::DateTimeDecorator(this,"endAt", "Ends at"))); + notes = static_cast( + addDataItem(new data::StringDecorator(this, "notes", "Notes"))); + } + + Appointment::Appointment(QObject* parent, const QJsonObject& json) + : Appointment(parent) + { + update(json); + } +} diff --git a/cm-lib/src/models/appointment.h b/cm-lib/src/models/appointment.h new file mode 100644 index 0000000..2ee7e72 --- /dev/null +++ b/cm-lib/src/models/appointment.h @@ -0,0 +1,29 @@ +#ifndef APPOINTMENT_H +#define APPOINTMENT_H + +#include +#include + +#include "data/entity.h" +#include "data/stringdecorator.h" +#include "data/datetimedecorator.h" +#include "cm-lib_global.h" + +namespace cm::models { + + class CMLIBSHARED_EXPORT Appointment : public data::Entity + { + Q_OBJECT + Q_PROPERTY(cm::data::DateTimeDecorator* ui_startAt MEMBER startAt CONSTANT) + Q_PROPERTY(cm::data::DateTimeDecorator* ui_endAt MEMBER endAt CONSTANT) + Q_PROPERTY(cm::data::StringDecorator* ui_notes MEMBER notes CONSTANT) + public: + explicit Appointment(QObject* parent = nullptr); + Appointment(QObject* parent, const QJsonObject& json); + + data::DateTimeDecorator* startAt{nullptr}; + data::DateTimeDecorator* endAt{nullptr}; + data::StringDecorator* notes{nullptr}; + }; +} +#endif // APPOINTMENT_H diff --git a/cm-lib/src/models/client.cpp b/cm-lib/src/models/client.cpp index e20037a..11ebc8b 100644 --- a/cm-lib/src/models/client.cpp +++ b/cm-lib/src/models/client.cpp @@ -1,7 +1,37 @@ #include "client.h" namespace cm::models { - Client::Client() + + Client::Client(QObject * parent) + : Entity(parent, "client") + { + reference = static_cast( + addDataItem(new data::StringDecorator(this, "reference", "Client Ref"))); + name = static_cast( + addDataItem(new data::StringDecorator(this, "name", "Name"))); + supplyAddress = static_cast( + addChild(new Address(this), "supplyAddress")); + billingAddress = static_cast( + addChild(new Address(this), "billingAddress")); + appointments = static_cast*>( + addChildCollection(new data::EntityCollection(this, "appointments"))); + contacts = static_cast*>( + addChildCollection(new data::EntityCollection(this, "contacts"))); + } + + Client::Client(QObject* parent, const QJsonObject& json) + : Client(parent) + { + update(json); + } + + QQmlListProperty Client::ui_appointments() + { + return QQmlListProperty(this, appointments->derivedEntities()); + } + + QQmlListProperty Client::ui_contacts() { + return QQmlListProperty(this, contacts->derivedEntities()); } } diff --git a/cm-lib/src/models/client.h b/cm-lib/src/models/client.h index 098d11b..fa0eee4 100644 --- a/cm-lib/src/models/client.h +++ b/cm-lib/src/models/client.h @@ -1,15 +1,47 @@ #ifndef CLIENT_H #define CLIENT_H +#include +#include +#include + #include "cm-lib_global.h" +#include "data/stringdecorator.h" +#include "data/entity.h" +#include "data/entity-collection.h" +#include "models/address.h" +#include "models/appointment.h" +#include "models/contact.h" namespace cm::models { -class CMLIBSHARED_EXPORT Client +class CMLIBSHARED_EXPORT Client : public data::Entity { + Q_OBJECT + Q_PROPERTY(cm::data::StringDecorator* ui_reference MEMBER reference CONSTANT ) + Q_PROPERTY(cm::data::StringDecorator* ui_name MEMBER name CONSTANT ) + Q_PROPERTY(cm::models::Address* ui_supplyAddress MEMBER supplyAddress CONSTANT ) + Q_PROPERTY(cm::models::Address* ui_billingAddress MEMBER billingAddress CONSTANT ) + Q_PROPERTY(QQmlListProperty ui_appointments READ ui_appointments NOTIFY appointmentsChanged ) + Q_PROPERTY(QQmlListProperty ui_contacts READ ui_contacts NOTIFY contactsChanged ) public: - Client(); + explicit Client(QObject* parent = nullptr); + Client(QObject* parent, const QJsonObject& json); + + data::StringDecorator* reference{nullptr}; + data::StringDecorator* name{nullptr}; + Address* supplyAddress{nullptr}; + Address* billingAddress{nullptr}; + data::EntityCollection* appointments{nullptr}; + data::EntityCollection* contacts{nullptr}; + + QQmlListProperty ui_appointments(); + QQmlListProperty ui_contacts(); + +signals: + void appointmentsChanged(); + void contactsChanged(); }; } #endif // CLIENT_H diff --git a/cm-lib/src/models/contact.cpp b/cm-lib/src/models/contact.cpp new file mode 100644 index 0000000..a70917f --- /dev/null +++ b/cm-lib/src/models/contact.cpp @@ -0,0 +1,19 @@ +#include "contact.h" + +namespace cm::models { + + Contact::Contact(QObject* parent) + : data::Entity(parent, "contact") + { + type = static_cast( + addDataItem(new data::EnumeratorDecorator(this, "type", "Contact Type", 0, contactTypeMapper))); + address = static_cast( + addDataItem(new data::StringDecorator(this, "address", "Address"))); + } + + Contact::Contact(QObject* parent, const QJsonObject& json) + : Contact(parent) + { + update(json); + } +} diff --git a/cm-lib/src/models/contact.h b/cm-lib/src/models/contact.h new file mode 100644 index 0000000..6afc49a --- /dev/null +++ b/cm-lib/src/models/contact.h @@ -0,0 +1,45 @@ +#ifndef CONTACT_H +#define CONTACT_H + +#include + +#include + +#include "data/entity.h" +#include "data/stringdecorator.h" +#include "data/enumeratordecorator.h" +#include "cm-lib_global.h" + +namespace cm::models { + + class CMLIBSHARED_EXPORT Contact : public data::Entity + { + Q_OBJECT + Q_PROPERTY(cm::data::EnumeratorDecorator* ui_type MEMBER type CONSTANT) + Q_PROPERTY(cm::data::StringDecorator* ui_address MEMBER address CONSTANT) + + public: + explicit Contact(QObject* parent = nullptr); + Contact(QObject* parent, const QJsonObject& json); + + data::EnumeratorDecorator* type{nullptr}; + data::StringDecorator* address{nullptr}; + + private: + enum eContactType { + Unknown = 0, + Telephone, + Email, + Fax + }; + + std::map contactTypeMapper = std::map + { + { Contact::eContactType::Unknown, "" }, + { Contact::eContactType::Telephone, "Telephone" }, + { Contact::eContactType::Email, "Email" }, + { Contact::eContactType::Fax, "Fax" } + }; + }; +} +#endif // CONTACT_H diff --git a/cm-ui/src/main.cpp b/cm-ui/src/main.cpp index c2e2059..6666ecb 100644 --- a/cm-ui/src/main.cpp +++ b/cm-ui/src/main.cpp @@ -1,10 +1,23 @@ #include #include #include + #include "controllers/master-controller.h" #include "controllers/commandcontroller.h" + #include "framework/command.h" +#include "data/datetimedecorator.h" +#include "data/enumeratordecorator.h" +#include "data/intdecorator.h" +#include "data/stringdecorator.h" + +#include "models/address.h" +#include "models/appointment.h" +#include "models/contact.h" +#include "models/client.h" + + int main(int argc, char *argv[]) { #if defined(Q_OS_WIN) @@ -15,8 +28,19 @@ int main(int argc, char *argv[]) qmlRegisterType("CM", 1, 0, "MasterController"); qmlRegisterType("CM", 1, 0, "NavigationController"); qmlRegisterType("CM", 1, 0, "CommandController"); + qmlRegisterType("CM", 1, 0, "Command"); + qmlRegisterType("CM", 1, 0, "DateTimeDecorator"); + qmlRegisterType("CM", 1, 0, "EnumeratorDecorator"); + qmlRegisterType("CM", 1, 0, "IntDecorator"); + qmlRegisterType("CM", 1, 0, "StringDecorator"); + + qmlRegisterType("CM", 1, 0, "Address"); + qmlRegisterType("CM", 1, 0, "Appointment"); + qmlRegisterType("CM", 1, 0, "Contact"); + qmlRegisterType("CM", 1, 0, "Client"); + cm::controllers::MasterController masterController; QQmlApplicationEngine engine;