153 lines
3.2 KiB
C++
153 lines
3.2 KiB
C++
#pragma once
|
|
|
|
#include "mappresent_global.h"
|
|
#include <QWidget>
|
|
#include <memory>
|
|
|
|
|
|
/// <summary>
|
|
/// 绘制索引
|
|
/// </summary>
|
|
struct MAPPRESENT_EXPORT PresentIndex {
|
|
/// <summary>
|
|
/// 行号
|
|
/// </summary>
|
|
int row = INT_MAX;
|
|
/// <summary>
|
|
/// 列号
|
|
/// </summary>
|
|
int col = INT_MAX;
|
|
|
|
bool isValid() const;
|
|
|
|
PresentIndex& operator+=(const PresentIndex& other);
|
|
bool operator!=(const PresentIndex& other) const;
|
|
bool operator==(const PresentIndex& other) const;
|
|
};
|
|
|
|
/// <summary>
|
|
/// 绘制选项
|
|
/// </summary>
|
|
struct MAPPRESENT_EXPORT PresentOption {
|
|
/// <summary>
|
|
/// 当前所绘制索引
|
|
/// </summary>
|
|
PresentIndex index;
|
|
/// <summary>
|
|
/// 当前绘制外缘
|
|
/// </summary>
|
|
QRectF outline;
|
|
|
|
PresentOption& operator=(const PresentOption& other);
|
|
};
|
|
|
|
/// <summary>
|
|
/// 单元绘制委托
|
|
/// </summary>
|
|
class MAPPRESENT_EXPORT UnitPresentDelegate : public QObject {
|
|
Q_OBJECT
|
|
public:
|
|
UnitPresentDelegate(QObject* parent = nullptr);
|
|
virtual ~UnitPresentDelegate() = default;
|
|
|
|
/// <summary>
|
|
/// 匹配单元类型
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
virtual int unitType() const = 0;
|
|
/// <summary>
|
|
/// 绘制本单元瓦片
|
|
/// </summary>
|
|
/// <param name="p"></param>
|
|
/// <param name="option"></param>
|
|
virtual void paint(QPainter* p, const PresentOption& option) = 0;
|
|
|
|
signals:
|
|
/// <summary>
|
|
/// 更新通知/重绘请求
|
|
/// </summary>
|
|
void updateRequest(const PresentIndex &idx);
|
|
};
|
|
|
|
/// <summary>
|
|
/// 基础瓦片地图绘制
|
|
/// </summary>
|
|
class MAPPRESENT_EXPORT MapPresent : public QWidget
|
|
{
|
|
Q_OBJECT
|
|
private:
|
|
/// <summary>
|
|
/// 初级区域矩形变长
|
|
/// </summary>
|
|
const float _primitive_region_len = 100;
|
|
/// <summary>
|
|
/// 缩放倍数
|
|
/// </summary>
|
|
short _scale_times = 1;
|
|
/// <summary>
|
|
/// 当前瓦片地图中心索引
|
|
/// </summary>
|
|
PresentIndex _center_index;
|
|
|
|
// ===================================
|
|
/// <summary>
|
|
/// 试图核心索引
|
|
/// </summary>
|
|
QList<PresentOption> _visible_units;
|
|
/// <summary>
|
|
/// 可视化单元整理
|
|
/// </summary>
|
|
void visible_units_tidy();
|
|
/// <summary>
|
|
/// 可视化索引填充
|
|
/// </summary>
|
|
/// <param name="a"></param>
|
|
/// <param name="b"></param>
|
|
/// <returns></returns>
|
|
QList<PresentIndex> item_supply(const PresentIndex& a, const PresentIndex& b) const;
|
|
|
|
// ===================================
|
|
/// <summary>
|
|
/// 类型化绘制委托
|
|
/// </summary>
|
|
QHash<int, UnitPresentDelegate*> _type_present_delegate;
|
|
|
|
|
|
public:
|
|
MapPresent(QWidget* parent = nullptr);
|
|
|
|
/// <summary>
|
|
/// 通过widget上定位获取绘制索引
|
|
/// </summary>
|
|
/// <param name="pos"></param>
|
|
/// <returns></returns>
|
|
PresentIndex indexGet(const QPointF& pos) const;
|
|
/// <summary>
|
|
/// 通过绘制索引获取指定单元外接矩形
|
|
/// </summary>
|
|
/// <param name="idx"></param>
|
|
/// <returns></returns>
|
|
QRectF outlineGet(const PresentIndex& idx) const;
|
|
/// <summary>
|
|
/// 获取周围包裹单元
|
|
/// </summary>
|
|
/// <param name="center">核心单元索引</param>
|
|
/// <param name="dist">单元距离</param>
|
|
/// <returns></returns>
|
|
QList<PresentIndex> siblingsGet(const PresentIndex& center, uint16_t dist = 1) const;
|
|
|
|
signals:
|
|
void mouseIn();
|
|
void mouseOut();
|
|
void mouseHover(const PresentIndex& unit_index);
|
|
|
|
protected:
|
|
void paintEvent(QPaintEvent* ev) override;
|
|
void resizeEvent(QResizeEvent* event) override;
|
|
|
|
void mouseMoveEvent(QMouseEvent* event) override;
|
|
void enterEvent(QEvent* event) override;
|
|
void leaveEvent(QEvent* event) override;
|
|
|
|
};
|