File indexing completed on 2024-05-12 17:24:10

0001 // SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QObject>
0007 #include <QUrl>
0008 #include <QDateTime>
0009 #include "database.h"
0010 #include "plantsmodel.h"
0011 
0012 /// This class represent a plant and is directly usable from QML
0013 class Plant : public QObject
0014 {
0015     Q_OBJECT
0016 
0017     /// This property holds the database id of the plant
0018     Q_PROPERTY(int plantId READ plantId WRITE setPlantId NOTIFY plantIdChanged)
0019 
0020     /// This property holds the name of the plant
0021     Q_PROPERTY(QString name MEMBER m_name NOTIFY nameChanged)
0022 
0023     /// This property holds the species of the plant
0024     Q_PROPERTY(QString species MEMBER m_species NOTIFY speciesChanged)
0025 
0026     /// This property holds the img url of the plant
0027     Q_PROPERTY(QUrl imgUrl MEMBER m_imgUrl NOTIFY imgUrlChanged)
0028 
0029     /// This property holds the intervall in which this plant should be watered
0030     Q_PROPERTY(int waterIntervall MEMBER m_waterIntervall NOTIFY waterIntervallChanged)
0031 
0032     /// This property holds the interval in which this plant should be watered
0033     Q_PROPERTY(QString location MEMBER m_location NOTIFY locationChanged)
0034 
0035     /// This property holds the date of birth of this plant
0036     Q_PROPERTY(QDate dateOfBirth MEMBER m_dateOfBirth NOTIFY dateOfBirthChanged)
0037 
0038     /// This property holds the time when this plant was last watered
0039     Q_PROPERTY(QDate lastWatered MEMBER m_lastWatered NOTIFY lastWateredChanged)
0040 
0041     /// This property holds the time when this plant want to be watered next
0042     Q_PROPERTY(int wantsToBeWateredIn READ wantsToBeWateredIn NOTIFY lastWateredChanged)
0043 
0044     /// This property holds the time when this plant want to be watered next
0045     Q_PROPERTY(int currentHealth MEMBER m_currentHealth NOTIFY currentHealthChanged)
0046 
0047 public:
0048     explicit Plant(QObject *parent = nullptr);
0049 
0050     DB::Plant::Id plantId() const;
0051     void setPlantId(const DB::Plant::Id plantId);
0052 
0053     int wantsToBeWateredIn() const;
0054 
0055 Q_SIGNALS:
0056     void plantIdChanged();
0057     void locationChanged();
0058     void waterIntervallChanged();
0059     void nameChanged();
0060     void speciesChanged();
0061     void imgUrlChanged();
0062     void dateOfBirthChanged();
0063     void lastWateredChanged();
0064     void currentHealthChanged();
0065 
0066 private:
0067     void refresh();
0068 
0069     DB::Plant::Id m_plantId;
0070     QString m_name;
0071     QString m_species;
0072     QUrl m_imgUrl;
0073     QString m_location;
0074     QDate m_dateOfBirth;
0075     QDate m_lastWatered;
0076     int m_waterIntervall = 2;
0077     int m_currentHealth = 50;
0078 
0079     friend class PlantEditor;
0080 };
0081 
0082 class PlantEditor : public QObject
0083 {
0084     Q_OBJECT
0085 
0086     Q_PROPERTY(Mode mode MEMBER m_mode NOTIFY modeChanged REQUIRED)
0087     Q_PROPERTY(int plantId READ plantId WRITE setPlantId NOTIFY plantIdChanged)
0088     Q_PROPERTY(Plant *plant READ plant CONSTANT)
0089     Q_PROPERTY(PlantsModel *plantsModel MEMBER m_plantsModel NOTIFY plantsModelChanged REQUIRED)
0090 
0091 public:
0092     enum Mode {
0093         Editor,
0094         Creator
0095     };
0096     Q_ENUM(Mode);
0097 
0098     explicit PlantEditor(QObject *parent = nullptr);
0099 
0100     DB::Plant::Id plantId() const;
0101     void setPlantId(const DB::Plant::Id plantId);
0102 
0103     Plant *plant() const;
0104 
0105     Q_INVOKABLE void save();
0106 
0107 Q_SIGNALS:
0108     void modeChanged();
0109     void plantIdChanged();
0110     void plantsModelChanged();
0111 
0112 private:
0113     enum SpecialValues {
0114         InvalidPlantId = -1,
0115     };
0116 
0117     Mode m_mode = Creator;
0118     DB::Plant::Id m_plantId = InvalidPlantId;
0119     Plant *const m_plant;
0120     PlantsModel *m_plantsModel = nullptr;
0121 };