first commit
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
#include "PicScheduleShiftItem.h"
|
||||
#include <algorithm>
|
||||
#include "LogInfo.h"
|
||||
|
||||
PicScheduleShiftItem::PicScheduleShiftItem(QGraphicsItem *parent, QGraphicsScene *scene, const SourceType type)
|
||||
:PicTextItem(parent, scene, type)
|
||||
{
|
||||
m_updateTimer.setInterval(20);
|
||||
connect(&m_updateTimer, &QTimer::timeout, [this](){
|
||||
updateScheduleName();
|
||||
});
|
||||
m_updateTimer.start();
|
||||
}
|
||||
|
||||
QVariant PicScheduleShiftItem::source()
|
||||
{
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
void PicScheduleShiftItem::setSource(QVariant var)
|
||||
{
|
||||
m_currScheduleName = findScheduleName(QTime::currentTime());
|
||||
applyModify();
|
||||
}
|
||||
|
||||
const QVector<ScheduleModel>& PicScheduleShiftItem::schedules() const
|
||||
{
|
||||
return m_schedules;
|
||||
}
|
||||
|
||||
void PicScheduleShiftItem::setSchedules(QVector<ScheduleModel> schedules)
|
||||
{
|
||||
m_schedules = std::move(schedules);
|
||||
m_currScheduleName = findScheduleName(QTime::currentTime());
|
||||
applyModify();
|
||||
}
|
||||
|
||||
bool PicScheduleShiftItem::updateScheduleName()
|
||||
{
|
||||
if(m_currScheduleName.isEmpty()) {
|
||||
m_currScheduleName = findScheduleName(QTime::currentTime());
|
||||
applyModify();
|
||||
return true;
|
||||
}
|
||||
|
||||
auto str = findScheduleName(QTime::currentTime());
|
||||
if(str == m_currScheduleName)
|
||||
return false;
|
||||
|
||||
LogInfo::setRunStatusText("schedule old: " + str + ", old str: " + m_currScheduleName);
|
||||
m_currScheduleName = str;
|
||||
applyModify();
|
||||
return true;
|
||||
}
|
||||
|
||||
void PicScheduleShiftItem::setCustomAttrs(const QXmlStreamAttributes &attrs)
|
||||
{
|
||||
auto tableStr = attrs.value("schedule_table").toString();
|
||||
if(tableStr.isEmpty())
|
||||
return;
|
||||
|
||||
QVector<ScheduleModel> models;
|
||||
auto table = tableStr.split("<br>");
|
||||
models.reserve(table.size());
|
||||
for(auto scheduleStr : table) {
|
||||
auto pair = scheduleStr.split("<sub_br>");
|
||||
if(pair.size() != 2)
|
||||
continue;
|
||||
|
||||
ScheduleModel model;
|
||||
model.startTime = QTime::fromString(pair[0], "hh:mm");
|
||||
model.name = pair[1];
|
||||
|
||||
auto it = std::upper_bound(models.begin(), models.end(), model);
|
||||
models.insert(it, model);
|
||||
}
|
||||
|
||||
setSchedules(std::move(models));
|
||||
}
|
||||
|
||||
void PicScheduleShiftItem::saveCustomAttrs(QXmlStreamAttributes &attrs)
|
||||
{
|
||||
QString tableStr;
|
||||
for(auto& model : m_schedules) {
|
||||
QString pair = model.startTime.toString("hh:mm") + "<sub_br>" + model.name;
|
||||
tableStr += pair;
|
||||
tableStr += "<br>";
|
||||
}
|
||||
|
||||
attrs.append("schedule_table", tableStr);
|
||||
}
|
||||
|
||||
QImage PicScheduleShiftItem::itemImage()
|
||||
{
|
||||
auto str = m_currScheduleName;
|
||||
|
||||
if(str.isEmpty()) {
|
||||
QImage();
|
||||
}
|
||||
return strToImage(str);
|
||||
}
|
||||
|
||||
QString PicScheduleShiftItem::findScheduleName(const QTime &time)
|
||||
{
|
||||
if(m_schedules.isEmpty())
|
||||
return QString();
|
||||
|
||||
auto it = std::upper_bound(m_schedules.begin(), m_schedules.end(), time, [](const QTime& time, const ScheduleModel& value){
|
||||
return value.startTime > time;
|
||||
});
|
||||
|
||||
if(it == m_schedules.begin())
|
||||
return m_schedules.rbegin()->name;
|
||||
else
|
||||
return std::prev(it)->name;
|
||||
|
||||
return QString();
|
||||
}
|
||||
Reference in New Issue
Block a user