159 lines
4.0 KiB
C++
159 lines
4.0 KiB
C++
#include "PicScheduleShiftItem.h"
|
|
#include <algorithm>
|
|
#include "LogInfo.h"
|
|
#include <QFontMetrics>
|
|
#include <QPainter>
|
|
|
|
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();
|
|
}
|
|
|
|
const QVector<ScheduleModel>& PicScheduleShiftItem::schedules() const
|
|
{
|
|
return m_schedules;
|
|
}
|
|
|
|
void PicScheduleShiftItem::setSchedules(QVector<ScheduleModel> schedules)
|
|
{
|
|
m_schedules = std::move(schedules);
|
|
m_maxSize = calcMaxSize();
|
|
|
|
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()) {
|
|
return QImage();
|
|
}
|
|
auto forceImg = strToImage(str);
|
|
QImage backgroundImg(m_maxSize.width(), forceImg.height(), QImage::Format_ARGB32);
|
|
backgroundImg.fill(0x00000000);
|
|
|
|
QPainter p;
|
|
p.begin(&backgroundImg);
|
|
p.drawImage(QPoint(0, 0), forceImg);
|
|
p.end();
|
|
|
|
return backgroundImg;
|
|
}
|
|
|
|
void PicScheduleShiftItem::letterSpaceChanged()
|
|
{
|
|
setSchedules(schedules());
|
|
}
|
|
|
|
QSize PicScheduleShiftItem::calcMaxSize()
|
|
{
|
|
if (m_schedules.isEmpty())
|
|
return QSize();
|
|
|
|
QFontMetrics fm(font());
|
|
int bestW = 0;
|
|
for (const auto &mdl : m_schedules) {
|
|
int w = fm.horizontalAdvance(mdl.name);
|
|
if (w > bestW) bestW = w;
|
|
}
|
|
return QSize(bestW, fm.height());
|
|
}
|
|
|
|
QRectF PicScheduleShiftItem::boundingRect() const
|
|
{
|
|
auto r = PicTextItem::boundingRect();
|
|
r.setWidth(m_maxSize.width());
|
|
return r;
|
|
|
|
if (!m_itemImage.isNull()) {
|
|
return QRectF(0, 0, m_itemImage.width(), m_itemImage.height());
|
|
}
|
|
|
|
int w = m_maxSize.width();
|
|
int h = m_maxSize.height();
|
|
if (w <= 0 || h <= 0)
|
|
return QGraphicsTextItem::boundingRect();
|
|
|
|
qreal bw = qreal(w) * qMax(1, m_boldMultiple);
|
|
return QRectF(0, 0, bw, qreal(h));
|
|
}
|
|
|
|
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();
|
|
}
|