File indexing completed on 2024-06-23 05:45:42

0001 /*
0002  * Copyright 2021 Swapnil Tripathi <swapnil06.st@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #pragma once
0008 
0009 #include <QAbstractListModel>
0010 #include <QCoreApplication>
0011 #include <QJsonObject>
0012 #include <QObject>
0013 
0014 #include <KConfigGroup>
0015 #include <KLocalizedString>
0016 #include <KSharedConfig>
0017 
0018 class TimerPreset : public QObject
0019 {
0020     Q_OBJECT
0021     Q_PROPERTY(QString presetName READ presetName WRITE setPresetName NOTIFY propertyChanged)
0022     Q_PROPERTY(int presetDuration READ presetDuration NOTIFY propertyChanged)
0023 
0024 public:
0025     explicit TimerPreset(QObject *parent = nullptr, const QString &presetName = {}, int presetDuration = 0);
0026     explicit TimerPreset(const QJsonObject &obj);
0027 
0028     ~TimerPreset();
0029 
0030     QJsonObject toJson() const;
0031 
0032     QString presetName() const;
0033     int presetDuration() const;
0034     void setPresetName(const QString &presetName);
0035     void setDurationLength(int presetDuration);
0036 
0037 private:
0038     QString m_presetName;
0039     int m_presetDuration;
0040 
0041 Q_SIGNALS:
0042     void propertyChanged();
0043 };
0044 
0045 class TimerPresetModel : public QAbstractListModel
0046 {
0047     Q_OBJECT
0048 
0049 public:
0050     enum Roles { TimerPresetRole = Qt::UserRole };
0051 
0052     static TimerPresetModel *instance();
0053 
0054     void load();
0055     void save();
0056 
0057     QHash<int, QByteArray> roleNames() const override;
0058 
0059     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0060 
0061     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0062 
0063     Q_INVOKABLE void insertPreset(const QString &presetName, int presetDuration);
0064     Q_INVOKABLE void deletePreset(const int index);
0065 
0066 private:
0067     explicit TimerPresetModel(QObject *parent = nullptr);
0068     ~TimerPresetModel() override;
0069 
0070     QList<TimerPreset *> m_presets;
0071 };