#include "UnitDelegate.h" #include BasicUnitDelegate::BasicUnitDelegate(QObject* parent /*= nullptr*/) :UnitPresentDelegate(parent) { } int BasicUnitDelegate::unitType() const { return 0; } void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option) { p->setRenderHint(QPainter::Antialiasing, true); auto rect = option.outline; auto center = rect.center(); auto alen = rect.width() / 2; const float xspan = alen * sqrt(3) / 2; QRadialGradient brush(center, alen); brush.setColorAt(0, Qt::gray); brush.setColorAt(1, Qt::white); p->fillRect(rect, brush); QList points = endPointsGet(rect); QVector lines; for (auto idx = 0; idx < 6; ++idx) { lines << QLineF(points[idx], points[idx + 1]); } if (_hot_index == option.index) { auto pen = p->pen(); pen.setColor(Qt::red); pen.setWidth(10); p->setPen(pen); } p->drawLines(lines); auto ft = p->font(); ft.setPixelSize(alen * 0.3); p->setFont(ft); p->setPen(Qt::white); auto text_rect = QRectF(center.x() - xspan, center.y() - alen / 2, xspan * 2, alen); auto text_str = QString("(%1,%2)").arg(option.index.row).arg(option.index.col); p->drawText(text_rect, Qt::AlignCenter, text_str); } QList BasicUnitDelegate::endPointsGet(const QRectF& rect) { auto center = rect.center(); auto alen = rect.width() / 2; const float xspan = alen * sqrt(3) / 2; QList points; points << QPointF(center.x() - xspan, center.y() - alen / 2); points << QPointF(center.x(), 0); points << QPointF(center.x() + xspan, center.y() - alen / 2); points << QPointF(center.x() + xspan, center.y() + alen / 2); points << QPointF(center.x(), rect.height()); points << QPointF(center.x() - xspan, center.y() + alen / 2); points << QPointF(center.x() - xspan, center.y() - alen / 2); return points; } void BasicUnitDelegate::hotClear() { _hot_index = PresentIndex(); emit this->updateRequest(_hot_index); } void BasicUnitDelegate::hotIndexSet(const PresentIndex& idx) { if (_hot_index != idx) { _hot_index = idx; emit this->updateRequest(idx); } }