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

0001 /*
0002  * Copyright 2020 Han Young <hanyoung@protonmail.com>
0003  * Copyright 2020 Devin Lin <espidev@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #pragma once
0009 
0010 #include <QAbstractListModel>
0011 #include <QObject>
0012 #include <QTimer>
0013 
0014 #include <array>
0015 #include <tuple>
0016 
0017 class WeekModel;
0018 class KclockFormat : public QObject
0019 {
0020     Q_OBJECT
0021     Q_PROPERTY(QString currentTime READ currentTime NOTIFY timeChanged)
0022 
0023 public:
0024     explicit KclockFormat(QObject *parent = nullptr);
0025 
0026     static KclockFormat *instance()
0027     {
0028         static KclockFormat *singleton = new KclockFormat();
0029         return singleton;
0030     };
0031 
0032     QString currentTime() const;
0033     QString formatTimeString(int hours, int minutes);
0034     Q_INVOKABLE bool isChecked(int dayIndex, int daysOfWeek);
0035 
0036 private Q_SLOTS:
0037     void updateCurrentTime();
0038 
0039 Q_SIGNALS:
0040     void timeChanged();
0041 
0042 private:
0043     void startTimer(); // basic settings for updating time display
0044 
0045     QTimer *m_timer;
0046     WeekModel *m_weekModel;
0047     QString m_currentTime;
0048 };
0049 
0050 using weekListItem = std::array<std::tuple<QString, int>, 7>;
0051 
0052 class WeekModel : public QAbstractListModel
0053 {
0054     Q_OBJECT
0055 
0056 public:
0057     enum { NameRole = Qt::DisplayRole, FlagRole = Qt::UserRole + 1 };
0058 
0059     explicit WeekModel(QObject *parent = nullptr);
0060 
0061     int rowCount(const QModelIndex &parent) const override;
0062     QVariant data(const QModelIndex &index, int role) const override;
0063     QHash<int, QByteArray> roleNames() const override;
0064 
0065 private:
0066     weekListItem m_listItem;
0067 };