From 03512aae5bbeb4b7fbf0e58eec2bbf590740c59a Mon Sep 17 00:00:00 2001 From: codeboss <2422523675@qq.com> Date: Sun, 12 Oct 2025 20:21:45 +0800 Subject: [PATCH] update --- MapPresent/MapPresent.cpp | 90 +++++++++++++++++++++++++++---------- MapPresent/MapPresent.h | 8 ++-- MapPresent/UnitDelegate.cpp | 21 +++++---- MapPresent/UnitDelegate.h | 2 +- 4 files changed, 82 insertions(+), 39 deletions(-) diff --git a/MapPresent/MapPresent.cpp b/MapPresent/MapPresent.cpp index ee87047..6bb9e4f 100644 --- a/MapPresent/MapPresent.cpp +++ b/MapPresent/MapPresent.cpp @@ -8,6 +8,26 @@ #include +class OptionGroup { +private: + PresentOption _bind_opt; + std::shared_ptr _next_group; + +public: + OptionGroup(const PresentOption& opt, std::shared_ptr next = nullptr) + :_bind_opt(opt), _next_group(next) { + } + + std::shared_ptr nextGet() const { + return _next_group; + } + const PresentOption& optionGet() const { + return _bind_opt; + } +}; + + + MapPresent::MapPresent(QWidget* parent /*= nullptr*/) :QWidget(parent) { @@ -18,17 +38,23 @@ MapPresent::MapPresent(QWidget* parent /*= nullptr*/) _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(); }); + connect(delegate, &BasicUnitDelegate::updateRequest, [=]() { this->update(); }); this->setMouseTracking(true); } PresentIndex MapPresent::indexGet(const QPointF& pos) const { - decltype(_visible_units) templist; - for (auto& unit : _visible_units) { - if (unit.outline.contains(pos)) { - templist << unit; + QList templist; + for (auto unit_g : _visible_units) { + while (unit_g) { + auto unit = unit_g->optionGet(); + + if (unit.outline.contains(pos)) { + templist << unit; + } + + unit_g = unit_g->nextGet(); } } @@ -69,31 +95,33 @@ QRectF MapPresent::outlineGet(const PresentIndex& idx) const return QRectF(target_center.toPointF(), QSizeF(ap_len * 2, ap_len * 2)); } +#include +#include void MapPresent::paintEvent(QPaintEvent* ev) { QWidget::paintEvent(ev); - UnitPresentDelegate &t = *_present_delegate[0]; + UnitPresentDelegate& t = *_present_delegate[0]; QPainter p(this); - for (auto opt : _visible_units) { - p.save(); + for (auto opt_g : _visible_units) { + while (opt_g) { + p.save(); - auto rect = opt.outline; - p.translate(rect.topLeft()); - rect.moveTo(0, 0); + auto opt = opt_g->optionGet(); + auto rect = opt.outline; + p.translate(rect.topLeft()); + rect.moveTo(0, 0); - auto end_pts = BasicUnitDelegate::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]); + auto clip_path = BasicUnitDelegate::clipPathGet(rect); + p.setClipPath(clip_path); + + opt.outline = rect; + t.paint(&p, opt); + + p.restore(); + + opt_g = opt_g->nextGet(); } - p.setClipPath(clicp_path); - - opt.outline = rect; - t.paint(&p, opt); - - p.restore(); } } @@ -140,20 +168,27 @@ void MapPresent::visibleUnitsTidy() { _visible_units.clear(); QRectF curr_outline = this->rect(); - _visible_units << AssumeOpt(_center_index, outlineGet(_center_index)); + _visible_units << std::make_shared( + AssumeOpt(_center_index, outlineGet(_center_index)) + ); int visible_count = 1, deduce_x = 1; while (visible_count) { visible_count = 0; + std::shared_ptr prev_g = nullptr; 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); + prev_g = std::make_shared(AssumeOpt(unit, visible_rect), prev_g); visible_count++; } } + + if (prev_g) { + _visible_units << prev_g; + } } // TODO 构建快速测试组织结构 @@ -219,3 +254,10 @@ bool PresentIndex::operator==(const PresentIndex& other) const UnitPresentDelegate::UnitPresentDelegate(QObject* parent /*= nullptr*/) :QObject(parent) { } + +PresentOption& PresentOption::operator=(const PresentOption& other) +{ + index = other.index; + outline = other.outline; + return *this; +} diff --git a/MapPresent/MapPresent.h b/MapPresent/MapPresent.h index ddd8601..6d90c2f 100644 --- a/MapPresent/MapPresent.h +++ b/MapPresent/MapPresent.h @@ -2,6 +2,8 @@ #include "mappresent_global.h" #include +#include + /// /// 绘制索引 @@ -36,8 +38,7 @@ struct PresentOption { /// QRectF outline; - bool isMouseOver = false; - bool isSelected = false; + PresentOption& operator=(const PresentOption& other); }; /// @@ -68,6 +69,7 @@ signals: void updateRequest(const PresentIndex &idx); }; +class OptionGroup; /// /// 基础地图绘制 /// @@ -89,7 +91,7 @@ private: PresentIndex _center_index; // =================================== - QList _visible_units; + QList> _visible_units; void visibleUnitsTidy(); QList siblingsGet(const PresentIndex& center, uint16_t dist = 1) const; diff --git a/MapPresent/UnitDelegate.cpp b/MapPresent/UnitDelegate.cpp index 8921403..b6c63b7 100644 --- a/MapPresent/UnitDelegate.cpp +++ b/MapPresent/UnitDelegate.cpp @@ -22,22 +22,16 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option) QRadialGradient brush(center, alen); brush.setColorAt(0, Qt::gray); brush.setColorAt(1, Qt::white); - p->fillRect(rect, brush); + p->setBrush(brush); - QList points = endPointsGet(rect); - - QVector lines; - for (auto idx = 0; idx < 6; ++idx) { - lines << QLineF(points[idx], points[idx + 1]); - } + auto pathx = clipPathGet(rect); if (_hot_index == option.index) { auto pen = p->pen(); pen.setColor(Qt::red); pen.setWidth(10); p->setPen(pen); } - p->drawLines(lines); - + p->drawPath(pathx); auto ft = p->font(); ft.setPixelSize(alen * 0.3); @@ -49,7 +43,7 @@ void BasicUnitDelegate::paint(QPainter* p, const PresentOption& option) p->drawText(text_rect, Qt::AlignCenter, text_str); } -QList BasicUnitDelegate::endPointsGet(const QRectF& rect) +QPainterPath BasicUnitDelegate::clipPathGet(const QRectF& rect) { auto center = rect.center(); auto alen = rect.width() / 2; @@ -64,7 +58,12 @@ QList BasicUnitDelegate::endPointsGet(const QRectF& rect) points << QPointF(center.x() - xspan, center.y() + alen / 2); points << QPointF(center.x() - xspan, center.y() - alen / 2); - return points; + 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() diff --git a/MapPresent/UnitDelegate.h b/MapPresent/UnitDelegate.h index 91a52a3..0044f5d 100644 --- a/MapPresent/UnitDelegate.h +++ b/MapPresent/UnitDelegate.h @@ -10,7 +10,7 @@ public: int unitType() const override; void paint(QPainter* p, const PresentOption& option) override; - static QList endPointsGet(const QRectF& rect); + static QPainterPath clipPathGet(const QRectF& rect); void hotClear(); void hotIndexSet(const PresentIndex &idx);