ActWorld/MapPresent/UnitDelegate.cpp

54 lines
1.5 KiB
C++

#include "UnitDelegate.h"
#include <QPainter>
int BasicUnitDelegate::unitType() const
{
return 0;
}
void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option)
{
auto rect = option.outline;
auto center = rect.center();
auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2;
QRadialGradient brush(center, alen);
p->fillRect(rect, brush);
QList<QPointF> points = endPointsGet(rect);
QVector<QLineF> lines;
for (auto idx = 0; idx < 6; ++idx) {
lines << QLineF(points[idx], points[idx + 1]);
}
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<QPointF> BasicUnitDelegate::endPointsGet(const QRectF& rect)
{
auto center = rect.center();
auto alen = rect.width() / 2;
const float xspan = alen * sqrt(3) / 2;
QList<QPointF> 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;
}