147 lines
4.0 KiB
C++
147 lines
4.0 KiB
C++
#include "TimeSlotListModel.h"
|
|
#include <QJsonObject>
|
|
|
|
TimeSlotListModel::TimeSlotListModel(QObject *parent)
|
|
: QAbstractListModel(parent)
|
|
{
|
|
}
|
|
|
|
int TimeSlotListModel::rowCount(const QModelIndex &parent) const
|
|
{
|
|
if (parent.isValid()) {
|
|
return 0;
|
|
}
|
|
return m_timeSlots.size();
|
|
}
|
|
|
|
QVariant TimeSlotListModel::data(const QModelIndex &index, int role) const
|
|
{
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_timeSlots.size()) {
|
|
return {};
|
|
}
|
|
|
|
const TimeSlot &slot = m_timeSlots.at(index.row());
|
|
switch (role) {
|
|
case NameRole: return slot.name;
|
|
case StartTimeRole: return slot.startTime.toString("HH:mm");
|
|
case EndTimeRole: return slot.endTime.toString("HH:mm");
|
|
case EnabledRole: return slot.enabled;
|
|
default: return {};
|
|
}
|
|
}
|
|
|
|
bool TimeSlotListModel::setData(const QModelIndex &index, const QVariant &value, int role)
|
|
{
|
|
if (!index.isValid() || index.row() < 0 || index.row() >= m_timeSlots.size()) {
|
|
return false;
|
|
}
|
|
|
|
TimeSlot &slot = m_timeSlots[index.row()];
|
|
switch (role) {
|
|
case NameRole: slot.name = value.toString(); break;
|
|
case StartTimeRole: slot.startTime = QTime::fromString(value.toString(), "HH:mm"); break;
|
|
case EndTimeRole: slot.endTime = QTime::fromString(value.toString(), "HH:mm"); break;
|
|
case EnabledRole: slot.enabled = value.toBool(); break;
|
|
default: return false;
|
|
}
|
|
|
|
emit dataChanged(index, index, {role});
|
|
return true;
|
|
}
|
|
|
|
Qt::ItemFlags TimeSlotListModel::flags(const QModelIndex &index) const
|
|
{
|
|
if (!index.isValid()) {
|
|
return Qt::NoItemFlags;
|
|
}
|
|
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
|
|
}
|
|
|
|
QHash<int, QByteArray> TimeSlotListModel::roleNames() const
|
|
{
|
|
return {
|
|
{NameRole, "name"},
|
|
{StartTimeRole, "startTime"},
|
|
{EndTimeRole, "endTime"},
|
|
{EnabledRole, "enabled"}
|
|
};
|
|
}
|
|
|
|
void TimeSlotListModel::setTimeSlots(const QVector<TimeSlot> &timeSlots)
|
|
{
|
|
beginResetModel();
|
|
m_timeSlots = timeSlots;
|
|
endResetModel();
|
|
}
|
|
|
|
QVector<TimeSlot> TimeSlotListModel::timeSlots() const
|
|
{
|
|
return m_timeSlots;
|
|
}
|
|
|
|
QJsonArray TimeSlotListModel::toJsonArray() const
|
|
{
|
|
QJsonArray array;
|
|
for (const TimeSlot &slot : m_timeSlots) {
|
|
QJsonObject object;
|
|
object["name"] = slot.name;
|
|
object["start_time"] = slot.startTime.toString("HH:mm");
|
|
object["end_time"] = slot.endTime.toString("HH:mm");
|
|
object["enabled"] = slot.enabled;
|
|
array.append(object);
|
|
}
|
|
return array;
|
|
}
|
|
|
|
QVariantMap TimeSlotListModel::get(int index) const
|
|
{
|
|
if (index < 0 || index >= m_timeSlots.size()) {
|
|
return {};
|
|
}
|
|
|
|
const TimeSlot &slot = m_timeSlots.at(index);
|
|
return {
|
|
{"name", slot.name},
|
|
{"startTime", slot.startTime.toString("HH:mm")},
|
|
{"endTime", slot.endTime.toString("HH:mm")},
|
|
{"enabled", slot.enabled}
|
|
};
|
|
}
|
|
|
|
void TimeSlotListModel::addTimeSlot(const QString &name, const QString &startTime, const QString &endTime, bool enabled)
|
|
{
|
|
beginInsertRows(QModelIndex(), m_timeSlots.size(), m_timeSlots.size());
|
|
m_timeSlots.push_back({name.trimmed(), QTime::fromString(startTime, "HH:mm"), QTime::fromString(endTime, "HH:mm"), enabled});
|
|
endInsertRows();
|
|
}
|
|
|
|
void TimeSlotListModel::updateTimeSlot(int index, const QString &name, const QString &startTime, const QString &endTime, bool enabled)
|
|
{
|
|
if (index < 0 || index >= m_timeSlots.size()) {
|
|
return;
|
|
}
|
|
|
|
TimeSlot &slot = m_timeSlots[index];
|
|
slot.name = name.trimmed();
|
|
slot.startTime = QTime::fromString(startTime, "HH:mm");
|
|
slot.endTime = QTime::fromString(endTime, "HH:mm");
|
|
slot.enabled = enabled;
|
|
emit dataChanged(this->index(index), this->index(index));
|
|
}
|
|
|
|
void TimeSlotListModel::removeTimeSlot(int index)
|
|
{
|
|
if (index < 0 || index >= m_timeSlots.size()) {
|
|
return;
|
|
}
|
|
|
|
beginRemoveRows(QModelIndex(), index, index);
|
|
m_timeSlots.removeAt(index);
|
|
endRemoveRows();
|
|
}
|
|
|
|
void TimeSlotListModel::resetToDefault()
|
|
{
|
|
setTimeSlots(defaultTimeSlots());
|
|
}
|