Files
ssp/pages/squaretraybutton.cpp
T

75 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "squaretraybutton.h"
#include <QPainter>
#include <QPaintEvent>
SquareTrayButton::SquareTrayButton(int trayIndex, int loadedCount, QWidget *parent)
: QAbstractButton(parent)
, m_trayId(0)
, m_planned(false)
, m_sensorPresent(true)
{
setCheckable(true);
setCursor(Qt::PointingHandCursor);
setText(QString("托盘 %1\n7 x 7\n已装 %2/49").arg(trayIndex + 1).arg(loadedCount));
}
void SquareTrayButton::setTrayInfo(int trayId, const QString &code, int loadedCount,
const QString &status, bool databasePresent,
bool sensorPresent)
{
m_trayId = trayId;
m_sensorPresent = sensorPresent;
const QString displayCode = code.isEmpty() ? QString("托盘 %1").arg(trayId) : code;
setText(sensorPresent
? QString("%1\n7 x 7\n%2:%3/49")
.arg(displayCode).arg(status).arg(loadedCount)
: QString("%1\n7 x 7\n不在位:0/49").arg(displayCode));
setEnabled(databasePresent);
update();
}
void SquareTrayButton::setPlanned(bool planned)
{
m_planned = planned;
update();
}
QSize SquareTrayButton::sizeHint() const
{
return QSize(120, 120);
}
QSize SquareTrayButton::minimumSizeHint() const
{
return QSize(70, 70);
}
void SquareTrayButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
const QRectF rect = QRectF(1, 1, width() - 2, height() - 2);
const QColor background = !m_sensorPresent ? QColor("#90a4ae")
: isChecked() ? QColor("#00897b")
: m_planned ? QColor("#f9a825") : QColor("#eceff1");
const QColor border = !m_sensorPresent ? QColor("#78909c")
: isChecked() ? QColor("#004d40")
: m_planned ? QColor("#ef6c00") : QColor("#b0bec5");
const QColor textColor = !m_sensorPresent || isChecked()
? QColor("#ffffff") : QColor("#263238");
painter.setPen(QPen(border, 2));
painter.setBrush(background);
painter.drawRoundedRect(rect, 6, 6);
QFont font = painter.font();
font.setBold(isChecked());
painter.setFont(font);
painter.setPen(textColor);
painter.drawText(rect.adjusted(6, 6, -6, -6), Qt::AlignCenter | Qt::TextWordWrap, text());
}