From f9a5aa14c28298e23f19ce555fcad8cc32d0724e Mon Sep 17 00:00:00 2001 From: Alexis Drai Date: Mon, 12 Jun 2023 10:22:57 +0200 Subject: [PATCH] :necktie: Create models (#1) Co-authored-by: Alexis Drai Reviewed-on: https://codefirst.iut.uca.fr/git/alexis.drai/oh_the_things_you_ll_do/pulls/1 --- oh_the_things_you_ll_do.pro | 8 +++++- src/project.cpp | 49 ++++++++++++++++++++++++++++++++++++ src/project.h | 36 ++++++++++++++++++++++++++ src/task.cpp | 50 +++++++++++++++++++++++++++++++++++++ src/task.h | 40 +++++++++++++++++++++++++++++ 5 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 src/project.cpp create mode 100644 src/project.h create mode 100644 src/task.cpp create mode 100644 src/task.h diff --git a/oh_the_things_you_ll_do.pro b/oh_the_things_you_ll_do.pro index 267418c..ba1ec19 100644 --- a/oh_the_things_you_ll_do.pro +++ b/oh_the_things_you_ll_do.pro @@ -14,7 +14,9 @@ TARGET = oh_the_things_you_ll_do CONFIG += sailfishapp -SOURCES += src/oh_the_things_you_ll_do.cpp +SOURCES += src/oh_the_things_you_ll_do.cpp \ + src/project.cpp \ + src/task.cpp DISTFILES += qml/oh_the_things_you_ll_do.qml \ qml/cover/CoverPage.qml \ @@ -37,3 +39,7 @@ CONFIG += sailfishapp_i18n # following TRANSLATIONS line. And also do not forget to # modify the localized app name in the the .desktop file. TRANSLATIONS += translations/oh_the_things_you_ll_do-de.ts + +HEADERS += \ + src/project.h \ + src/task.h diff --git a/src/project.cpp b/src/project.cpp new file mode 100644 index 0000000..fbd3728 --- /dev/null +++ b/src/project.cpp @@ -0,0 +1,49 @@ +#include "project.h" + +Project::Project(QObject *parent) : QObject(parent) +{ +} + +Project::Project(const QString &title, const QList &tasks, QObject *parent) + : QObject(parent), m_title(title), m_tasks(tasks) +{ +} + +QString Project::title() const +{ + return m_title; +} + +void Project::setTitle(const QString &title) +{ + if (m_title != title) { + m_title = title; + emit titleChanged(); + } +} + +const QList& Project::tasks() const +{ + return m_tasks; +} + +void Project::addTask(Task* task) +{ + m_tasks.append(task); + emit tasksChanged(); +} + +void Project::updateTask(Task* task) +{ + int index = m_tasks.indexOf(task); + if (index != -1) { + m_tasks[index] = task; + emit tasksChanged(); + } +} + +void Project::removeTask(Task* task) +{ + m_tasks.removeOne(task); + emit tasksChanged(); +} diff --git a/src/project.h b/src/project.h new file mode 100644 index 0000000..7a9f582 --- /dev/null +++ b/src/project.h @@ -0,0 +1,36 @@ +#ifndef PROJECT_H +#define PROJECT_H + +#include +#include +#include "Task.h" + +class Project : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(QList tasks READ tasks NOTIFY tasksChanged) + +public: + explicit Project(QObject *parent = nullptr); + Project(const QString &title, const QList &tasks, QObject *parent = nullptr); + + QString title() const; + void setTitle(const QString &title); + + const QList& tasks() const; + + void addTask(Task* task); + void updateTask(Task* task); + void removeTask(Task* task); + +signals: + void titleChanged(); + void tasksChanged(); + +private: + QString m_title; + QList m_tasks; +}; + +#endif // PROJECT_H diff --git a/src/task.cpp b/src/task.cpp new file mode 100644 index 0000000..0ac62f8 --- /dev/null +++ b/src/task.cpp @@ -0,0 +1,50 @@ +#include "task.h" + +Task::Task(QObject *parent) : QObject(parent) +{ +} + +Task::Task(const QString &title, const QString &description, int priority, QObject *parent) + : QObject(parent), m_title(title), m_description(description), m_priority(priority) +{ +} + +QString Task::title() const +{ + return m_title; +} + +void Task::setTitle(const QString &title) +{ + if (m_title != title) { + m_title = title; + emit titleChanged(); + } +} + +QString Task::description() const +{ + return m_description; +} + +void Task::setDescription(const QString &description) +{ + if (m_description != description) { + m_description = description; + emit descriptionChanged(); + } +} + +int Task::priority() const +{ + return m_priority; +} + +void Task::setPriority(int priority) +{ + if (priority >= 0 && priority <= MAX_PRIORITY && m_priority != priority) { + m_priority = priority; + emit priorityChanged(); + } +} + diff --git a/src/task.h b/src/task.h new file mode 100644 index 0000000..23470fe --- /dev/null +++ b/src/task.h @@ -0,0 +1,40 @@ +#ifndef TASK_H +#define TASK_H + +#include + +class Task : public QObject +{ + Q_OBJECT + Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged) + Q_PROPERTY(QString description READ description WRITE setDescription NOTIFY descriptionChanged) + Q_PROPERTY(int priority READ priority WRITE setPriority NOTIFY priorityChanged) + +public: + explicit Task(QObject *parent = nullptr); + Task(const QString &title, const QString &description, int priority, QObject *parent = nullptr); + + QString title() const; + void setTitle(const QString &title); + + QString description() const; + void setDescription(const QString &description); + + int priority() const; + void setPriority(int priority); + + static const int MAX_PRIORITY = 3; + +signals: + void titleChanged(); + void descriptionChanged(); + void priorityChanged(); + +private: + QString m_title; + QString m_description; + int m_priority; +}; + + +#endif // TASK_H