#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->setBrush(brush); p->drawRect(rect); if (_hot_index == option.index) { auto pen = p->pen(); pen.setColor(Qt::red); pen.setWidth(10); p->setPen(pen); auto pathx = clipPathGet(rect); p->drawPath(pathx); } auto ft = p->font(); ft.setPixelSize(alen * 0.3); p->setFont(ft); p->setPen(Qt::black); 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); } QPainterPath BasicUnitDelegate::clipPathGet(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); QPainterPath clicp_path; clicp_path.moveTo(points.first()); for (auto idx = 0; idx < points.size(); ++idx) { clicp_path.lineTo(points[idx]); } return clicp_path; } void BasicUnitDelegate::hotClear() { auto prev_idx = _hot_index; _hot_index = PresentIndex(); emit this->updateRequest({ prev_idx }); } void BasicUnitDelegate::hotIndexSet(const PresentIndex& idx) { if (_hot_index != idx) { auto prev_idx = _hot_index; _hot_index = idx; emit this->updateRequest({ prev_idx }); emit this->updateRequest({ idx }); } }