From 901b660a64ced86142a281ee41605433f20f48ce Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Sat, 11 Oct 2025 22:40:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AD=E8=BE=B9=E5=BD=A2=E5=9F=BA=E5=85=83?= =?UTF-8?q?=E5=B9=B3=E9=93=BAMapWidget?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- MapPresent/MapPresent.cpp | 83 +++++++++++++++++++++++++++------------ MapPresent/MapPresent.h | 6 +++ 2 files changed, 63 insertions(+), 26 deletions(-) diff --git a/MapPresent/MapPresent.cpp b/MapPresent/MapPresent.cpp index de44899..3587aaa 100644 --- a/MapPresent/MapPresent.cpp +++ b/MapPresent/MapPresent.cpp @@ -37,39 +37,66 @@ QRectF MapPresent::outlineGet(const PresentIndex& idx) const void MapPresent::paintEvent(QPaintEvent* ev) { QWidget::paintEvent(ev); - - QPainter p(this); - BasicUnitDelegate t; - p.save(); - p.setPen(Qt::red); - auto rect = outlineGet(_center_index); - p.translate(rect.topLeft()); - rect.moveTo(0, 0); + QPainter p(this); + for (auto opt : _visible_units) { + p.save(); + + auto rect = opt.selfOutline; + p.translate(rect.topLeft()); + rect.moveTo(0, 0); + + auto end_pts = t.endPointsGet(rect); + QPainterPath clicp_path; + clicp_path.moveTo(end_pts.first()); + for (auto idx = 0; idx < end_pts.size(); ++idx) { + clicp_path.lineTo(end_pts[idx]); + } + p.setClipPath(clicp_path); + + opt.selfOutline = rect; + t.paint(&p, opt); + + p.restore(); + } +} + +void MapPresent::resizeEvent(QResizeEvent* event) +{ + QWidget::resizeEvent(event); + + visibleUnitsTidy(); +} + +PresentOption AssumeOpt(PresentIndex index, QRectF rf) { PresentOption opt; - opt.selfIndex = _center_index; - opt.selfOutline = rect; - t.paint(&p, opt); - p.restore(); + opt.selfIndex = index; + opt.selfOutline = rf; + return opt; +} - for (auto idx = 1; idx < 10; ++idx) { - auto siblings = siblingsGet(_center_index, idx); - p.setPen(Qt::green); - for (auto item : siblings) { - auto rect = outlineGet(item); - p.save(); - p.translate(rect.topLeft()); - rect.moveTo(0, 0); - //p.drawRect(rect); +void MapPresent::visibleUnitsTidy() { + _visible_units.clear(); - opt.selfIndex = item; - opt.selfOutline = rect; - t.paint(&p, opt); + QRectF curr_outline = this->rect(); + _visible_units << AssumeOpt(_center_index, outlineGet(_center_index)); - p.restore(); + int visible_count = 1, deduce_x = 1; + while (visible_count) { + visible_count = 0; + + auto siblings = siblingsGet(_center_index, deduce_x++); + for (auto unit : siblings) { + auto visible_rect = outlineGet(unit); + if (curr_outline.intersects(visible_rect)) { + _visible_units << AssumeOpt(unit, visible_rect); + visible_count++; + } } } + + // TODO 构建快速测试组织结构 } QList MapPresent::siblingsGet(const PresentIndex& center, uint16_t dist) const @@ -131,6 +158,9 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option) auto alen = rect.width() / 2; const float xspan = alen * sqrt(3) / 2; + QRadialGradient brush(center, alen); + p->fillRect(rect, brush); + QList points = endPointsGet(rect); QVector lines; @@ -143,6 +173,7 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option) 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.selfIndex.row).arg(option.selfIndex.col); p->drawText(text_rect, Qt::AlignCenter, text_str); @@ -153,7 +184,7 @@ 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); diff --git a/MapPresent/MapPresent.h b/MapPresent/MapPresent.h index 0a48f3a..3218c26 100644 --- a/MapPresent/MapPresent.h +++ b/MapPresent/MapPresent.h @@ -103,8 +103,14 @@ public: protected: void paintEvent(QPaintEvent* ev) override; + void resizeEvent(QResizeEvent* event) override; private: + QList _visible_units; + void visibleUnitsTidy(); + QList siblingsGet(const PresentIndex ¢er, uint16_t dist=1) const; QList itemFills(const PresentIndex &a, const PresentIndex &b) const; + + };