diff --git a/MapPresent/MapPresent.cpp b/MapPresent/MapPresent.cpp index 3b8f526..5eff978 100644 --- a/MapPresent/MapPresent.cpp +++ b/MapPresent/MapPresent.cpp @@ -21,8 +21,8 @@ MapPresent::MapPresent(QWidget* parent /*= nullptr*/) connect(this, &MapPresent::mouseOutNotify, delegate, &BasicUnitDelegate::hotClear); connect(this, &MapPresent::mouseHoverNotify, delegate, &BasicUnitDelegate::hotIndexSet); connect(delegate, &BasicUnitDelegate::updateRequest, [=](const PresentIndex& idx) { - if (idx.isValid() && !_updated_units.contains(idx)) { - _updated_units.append(idx); + if (idx.isValid() && !_updated_index_list.contains(idx)) { + _updated_index_list.append(idx); } this->update(); @@ -44,6 +44,25 @@ double MapPresent::zoomTimes() const return _scale_times; } +bool MapPresent::setDelegate(UnitPresentDelegate* ins) +{ + if (_type_present_delegate.contains(ins->unitType())) + return false; + _type_present_delegate[ins->unitType()] = ins; + + this->visible_units_tidy(); + this->update(); + return true; +} + +void MapPresent::setDataModel(MapDataModel* model) +{ + this->_map_data_model = model; + + this->visible_units_tidy(); + this->update(); +} + PresentIndex MapPresent::indexGet(const QPointF& pos) const { QList templist; @@ -95,13 +114,13 @@ void MapPresent::paintEvent(QPaintEvent* ev) { QWidget::paintEvent(ev); - if (_updated_units.size()) { + if (_updated_index_list.size()) { QPainter pic_painter(&_paint_buffer); - for (auto idx : _updated_units) { + for (auto unit_idx : _updated_index_list) { pic_painter.save(); - auto opt = _visible_units[idx]; + auto opt = _visible_units[unit_idx]; auto rect = opt.outline; pic_painter.translate(rect.topLeft()); rect.moveTo(0, 0); @@ -110,13 +129,19 @@ void MapPresent::paintEvent(QPaintEvent* ev) pic_painter.setClipPath(clip_path); opt.outline = rect; - UnitPresentDelegate& t = *_type_present_delegate[0]; + + int unit_type = 0; + if (_map_data_model) { + unit_type = _map_data_model->mapData(MapDataModel::DataType::UnitType, unit_idx).toInt(); + } + + UnitPresentDelegate& t = *_type_present_delegate[unit_type]; t.paint(&pic_painter, opt); pic_painter.restore(); } - _updated_units.clear(); + _updated_index_list.clear(); } QPainter widget_painter(this); @@ -197,7 +222,7 @@ void MapPresent::visible_units_tidy() { } for (auto optx : _visible_units) - _updated_units.append(optx.index); + _updated_index_list.append(optx.index); } QList MapPresent::siblingsGet(const PresentIndex& center, uint16_t dist) const diff --git a/MapPresent/MapPresent.h b/MapPresent/MapPresent.h index 1d316e2..9c8013a 100644 --- a/MapPresent/MapPresent.h +++ b/MapPresent/MapPresent.h @@ -4,6 +4,7 @@ #include #include #include +#include /// @@ -27,6 +28,24 @@ struct MAPPRESENT_EXPORT PresentIndex { bool operator<(const PresentIndex& other) const; }; +/// +/// 地图数据模型 +/// +class MapDataModel : public QObject { + Q_OBJECT +public: + enum class DataType { + UnitType, + AltMeters + }; + + virtual QVariant mapData(DataType type, const PresentIndex& idx) const = 0; + virtual void mapDataSet(DataType type, const PresentIndex& idx, const QVariant& val) = 0; + +signals: + void dataChanged(const PresentIndex& idx); +}; + /// /// 绘制选项 /// @@ -39,6 +58,10 @@ struct MAPPRESENT_EXPORT PresentOption { /// 当前绘制外缘 /// QRectF outline; + /// + /// 数据模型指针 + /// + MapDataModel* dataModel = nullptr; PresentOption& operator=(const PresentOption& other); }; @@ -68,7 +91,7 @@ signals: /// /// 更新通知/重绘请求 /// - void updateRequest(const PresentIndex &idx); + void updateRequest(const PresentIndex& idx); }; /// @@ -78,6 +101,12 @@ class MAPPRESENT_EXPORT MapPresent : public QWidget { Q_OBJECT private: + /// + /// 地图数据模型 + /// + MapDataModel* _map_data_model = nullptr; + + // =================================== /// /// 初级区域矩形变长 /// @@ -103,7 +132,7 @@ private: /// /// 被更新的单元索引 /// - QList _updated_units; + QList _updated_index_list; /// /// 可视化单元整理 @@ -130,6 +159,18 @@ public: void zoomTo(double percent); double zoomTimes() const; + /// + /// 设置绘制委托 + /// + /// + /// + bool setDelegate(UnitPresentDelegate* ins); + /// + /// 设置地图数据模型 + /// + /// + void setDataModel(MapDataModel* model); + /// /// 通过widget上定位获取绘制索引 ///