ActWorld/MapPresent/UnitDelegate.cpp

82 lines
2.1 KiB
C++

#include "UnitDelegate.h"
#include <QPainter>
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);
auto pathx = clipPathGet(rect);
if (_hot_index == option.index) {
auto pen = p->pen();
pen.setColor(Qt::red);
pen.setWidth(10);
p->setPen(pen);
}
p->drawPath(pathx);
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);
}
QPainterPath BasicUnitDelegate::clipPathGet(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);
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()
{
_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);
}
}