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 #include "kclockformat.h"
0009 
0010 #include "settingsmodel.h"
0011 #include "utilmodel.h"
0012 
0013 #include <QLocale>
0014 #include <QTime>
0015 
0016 KclockFormat::KclockFormat(QObject *parent)
0017     : QObject(parent)
0018     , m_timer(new QTimer(this))
0019 {
0020     connect(SettingsModel::instance(), &SettingsModel::timeFormatChanged, this, &KclockFormat::updateCurrentTime);
0021     connect(m_timer, &QTimer::timeout, this, &KclockFormat::updateCurrentTime);
0022 
0023     this->startTimer();
0024 }
0025 
0026 QString KclockFormat::currentTime() const
0027 {
0028     return m_currentTime;
0029 }
0030 
0031 void KclockFormat::updateCurrentTime()
0032 {
0033     m_currentTime = QLocale::system().toString(QTime::currentTime(), UtilModel::instance()->timeFormat());
0034     Q_EMIT timeChanged();
0035 }
0036 
0037 QString KclockFormat::formatTimeString(int hours, int minutes)
0038 {
0039     return QLocale::system().toString(QTime(hours, minutes), UtilModel::instance()->timeFormat());
0040 }
0041 
0042 bool KclockFormat::isChecked(int dayIndex, int daysOfWeek)
0043 {
0044     // convert start day to standard start day of week, Monday
0045     dayIndex += QLocale::system().firstDayOfWeek() - 1;
0046     while (dayIndex > 6)
0047         dayIndex -= 7;
0048 
0049     int day = 1;
0050     day <<= dayIndex;
0051     return daysOfWeek & day;
0052 }
0053 
0054 void KclockFormat::startTimer()
0055 {
0056     updateCurrentTime();
0057     m_timer->start(1000);
0058 }
0059 
0060 WeekModel::WeekModel(QObject *parent)
0061     : QAbstractListModel(parent)
0062 {
0063     // init week and flag value
0064     int dayFlag[] = {1, 2, 4, 8, 16, 32, 64}, i = 0;
0065     for (int j = QLocale::system().firstDayOfWeek(); j <= 7; j++) {
0066         m_listItem[i++] = std::tuple<QString, int>(QLocale::system().dayName(j, QLocale::LongFormat), dayFlag[j - 1]);
0067     }
0068 
0069     for (int j = 1; j < QLocale::system().firstDayOfWeek(); j++) {
0070         m_listItem[i++] = std::tuple<QString, int>(QLocale::system().dayName(j, QLocale::LongFormat), dayFlag[j - 1]);
0071     }
0072 }
0073 
0074 int WeekModel::rowCount(const QModelIndex &parent) const
0075 {
0076     return 7;
0077 }
0078 
0079 QVariant WeekModel::data(const QModelIndex &index, int role) const
0080 {
0081     if (!index.isValid() || index.row() >= 7) {
0082         return QVariant();
0083     }
0084     if (role == NameRole)
0085         return std::get<0>(m_listItem[index.row()]);
0086     else if (role == FlagRole)
0087         return std::get<1>(m_listItem[index.row()]);
0088     else
0089         return QVariant();
0090 }
0091 
0092 QHash<int, QByteArray> WeekModel::roleNames() const
0093 {
0094     return {{NameRole, "name"}, {FlagRole, "flag"}};
0095 }