添加Model设置接口

This commit is contained in:
codeboss 2025-10-12 23:56:25 +08:00
parent 4b04982fc9
commit 529abd14b4
2 changed files with 76 additions and 10 deletions

View File

@ -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<PresentOption> 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<PresentIndex> MapPresent::siblingsGet(const PresentIndex& center, uint16_t dist) const

View File

@ -4,6 +4,7 @@
#include <QWidget>
#include <memory>
#include <QMap>
#include <QVariant>
/// <summary>
@ -27,6 +28,24 @@ struct MAPPRESENT_EXPORT PresentIndex {
bool operator<(const PresentIndex& other) const;
};
/// <summary>
/// 地图数据模型
/// </summary>
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);
};
/// <summary>
/// 绘制选项
/// </summary>
@ -39,6 +58,10 @@ struct MAPPRESENT_EXPORT PresentOption {
/// 当前绘制外缘
/// </summary>
QRectF outline;
/// <summary>
/// 数据模型指针
/// </summary>
MapDataModel* dataModel = nullptr;
PresentOption& operator=(const PresentOption& other);
};
@ -78,6 +101,12 @@ class MAPPRESENT_EXPORT MapPresent : public QWidget
{
Q_OBJECT
private:
/// <summary>
/// 地图数据模型
/// </summary>
MapDataModel* _map_data_model = nullptr;
// ===================================
/// <summary>
/// 初级区域矩形变长
/// </summary>
@ -103,7 +132,7 @@ private:
/// <summary>
/// 被更新的单元索引
/// </summary>
QList<PresentIndex> _updated_units;
QList<PresentIndex> _updated_index_list;
/// <summary>
/// 可视化单元整理
@ -130,6 +159,18 @@ public:
void zoomTo(double percent);
double zoomTimes() const;
/// <summary>
/// 设置绘制委托
/// </summary>
/// <param name="ins"></param>
/// <returns></returns>
bool setDelegate(UnitPresentDelegate* ins);
/// <summary>
/// 设置地图数据模型
/// </summary>
/// <param name="model"></param>
void setDataModel(MapDataModel* model);
/// <summary>
/// 通过widget上定位获取绘制索引
/// </summary>