From 6376ae1ed6a45c0e4aa1411e89ea886d124483c7 Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Sun, 12 Oct 2025 13:20:15 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BD=93=E5=89=8D=E5=8D=95=E5=85=83=E9=AB=98?= =?UTF-8?q?=E4=BA=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapPresent/MapPresent.cpp | 15 +++++++++++++-- MapPresent/MapPresent.h | 5 ++++- MapPresent/UnitDelegate.cpp | 29 +++++++++++++++++++++++++++++ MapPresent/UnitDelegate.h | 11 +++++++++-- 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/MapPresent/MapPresent.cpp b/MapPresent/MapPresent.cpp index 721d3d4..ee87047 100644 --- a/MapPresent/MapPresent.cpp +++ b/MapPresent/MapPresent.cpp @@ -14,6 +14,12 @@ MapPresent::MapPresent(QWidget* parent /*= nullptr*/) _center_index.row = 0; _center_index.col = 0; + auto delegate = new BasicUnitDelegate(this); + _present_delegate[delegate->unitType()] = delegate; + connect(this, &MapPresent::mouseOut, delegate, &BasicUnitDelegate::hotClear); + connect(this, &MapPresent::mouseHover, delegate, &BasicUnitDelegate::hotIndexSet); + connect(delegate, &BasicUnitDelegate::updateRequest, [=](){ this->update(); }); + this->setMouseTracking(true); } @@ -66,7 +72,7 @@ QRectF MapPresent::outlineGet(const PresentIndex& idx) const void MapPresent::paintEvent(QPaintEvent* ev) { QWidget::paintEvent(ev); - BasicUnitDelegate t; + UnitPresentDelegate &t = *_present_delegate[0]; QPainter p(this); for (auto opt : _visible_units) { @@ -76,7 +82,7 @@ void MapPresent::paintEvent(QPaintEvent* ev) p.translate(rect.topLeft()); rect.moveTo(0, 0); - auto end_pts = t.endPointsGet(rect); + auto end_pts = BasicUnitDelegate::endPointsGet(rect); QPainterPath clicp_path; clicp_path.moveTo(end_pts.first()); for (auto idx = 0; idx < end_pts.size(); ++idx) { @@ -205,6 +211,11 @@ bool PresentIndex::isValid() const return row != INT_MAX && col != INT_MAX; } +bool PresentIndex::operator==(const PresentIndex& other) const +{ + return row == other.row && col == other.col; +} + UnitPresentDelegate::UnitPresentDelegate(QObject* parent /*= nullptr*/) :QObject(parent) { } diff --git a/MapPresent/MapPresent.h b/MapPresent/MapPresent.h index 1968ed1..ddd8601 100644 --- a/MapPresent/MapPresent.h +++ b/MapPresent/MapPresent.h @@ -20,6 +20,7 @@ struct MAPPRESENT_EXPORT PresentIndex { PresentIndex& operator+=(const PresentIndex& other); bool operator!=(const PresentIndex& other) const; + bool operator==(const PresentIndex& other) const; }; /// @@ -64,7 +65,7 @@ signals: /// /// 更新通知/重绘请求 /// - void updateRequest(); + void updateRequest(const PresentIndex &idx); }; /// @@ -94,6 +95,8 @@ private: QList siblingsGet(const PresentIndex& center, uint16_t dist = 1) const; QList itemFills(const PresentIndex& a, const PresentIndex& b) const; + // =================================== + QHash _present_delegate; public: diff --git a/MapPresent/UnitDelegate.cpp b/MapPresent/UnitDelegate.cpp index 8f48217..8921403 100644 --- a/MapPresent/UnitDelegate.cpp +++ b/MapPresent/UnitDelegate.cpp @@ -1,6 +1,10 @@ #include "UnitDelegate.h" #include +BasicUnitDelegate::BasicUnitDelegate(QObject* parent /*= nullptr*/) + :UnitPresentDelegate(parent) { +} + int BasicUnitDelegate::unitType() const { return 0; @@ -8,12 +12,16 @@ int BasicUnitDelegate::unitType() const 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); @@ -22,8 +30,15 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option) 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); @@ -51,3 +66,17 @@ QList BasicUnitDelegate::endPointsGet(const QRectF& rect) 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); + } +} diff --git a/MapPresent/UnitDelegate.h b/MapPresent/UnitDelegate.h index e7a5e39..91a52a3 100644 --- a/MapPresent/UnitDelegate.h +++ b/MapPresent/UnitDelegate.h @@ -6,8 +6,15 @@ /// class BasicUnitDelegate : public UnitPresentDelegate { public: - int unitType() const override; + BasicUnitDelegate(QObject *parent = nullptr); + int unitType() const override; void paint(QPainter* p, const PresentOption& option) override; - QList endPointsGet(const QRectF& rect); + static QList endPointsGet(const QRectF& rect); + + void hotClear(); + void hotIndexSet(const PresentIndex &idx); + +private: + PresentIndex _hot_index; }; \ No newline at end of file