#include "squaretraybutton.h" #include #include SquareTrayButton::SquareTrayButton(int trayIndex, int loadedCount, QWidget *parent) : QAbstractButton(parent) , m_trayId(0) { 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 present) { m_trayId = trayId; const QString displayCode = code.isEmpty() ? QString("托盘 %1").arg(trayId) : code; const QString displayStatus = present ? status : "离位"; setText(QString("%1\n7 x 7\n%2 %3/49") .arg(displayCode) .arg(displayStatus) .arg(loadedCount)); setEnabled(present); 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 = isChecked() ? QColor("#00897b") : QColor("#eceff1"); const QColor border = isChecked() ? QColor("#004d40") : QColor("#b0bec5"); const QColor textColor = 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()); }