127 lines
3.6 KiB
C++
127 lines
3.6 KiB
C++
#ifndef MYGRAPHICSSCENE_H
|
||
#define MYGRAPHICSSCENE_H
|
||
|
||
#include <QObject>
|
||
#include <QGraphicsScene>
|
||
#include <QVector>
|
||
#include <QPainter>
|
||
#include <QGraphicsItem>
|
||
#include <QGraphicsScene>
|
||
#include <QGraphicsSceneMouseEvent>
|
||
#include <QPixmap>
|
||
#include <QPicture>
|
||
#include <QList>
|
||
#include <QtGui>
|
||
#include <QList>
|
||
|
||
#include "mygraphicsgriditem.h"
|
||
#include "machine/comm/datadef.h"
|
||
#include "datafile/dataoperat.h"
|
||
#include "main.h"
|
||
|
||
#define DRAWMARGINS 200.0 //item在场景中绘制时的边距(留边)
|
||
|
||
#define TYPE_DRAG -1 //拖拽
|
||
#define TYPE_LINE 0 //直线
|
||
#define TYPE_ARC 1 //三点圆
|
||
|
||
struct GLineItem
|
||
{
|
||
u16 order;//顺序
|
||
WORD type;
|
||
QGraphicsPolygonItem* item;
|
||
QList<QPointF> points;
|
||
};
|
||
|
||
struct GPathItem
|
||
{
|
||
u16 order;//顺序
|
||
WORD type;
|
||
QGraphicsPathItem* item;
|
||
QList<QPointF> points;
|
||
};
|
||
|
||
struct SelectPoint
|
||
{
|
||
int type;//选中点类型 0:起点 1:终点 2:中间点
|
||
int idx;//图元索引
|
||
};
|
||
|
||
///
|
||
/// \brief The MyGraphicsScene class
|
||
///场景层 记录实际数据
|
||
///
|
||
class MyGraphicsScene: public QGraphicsScene
|
||
{
|
||
Q_OBJECT
|
||
public:
|
||
MyGraphicsScene();
|
||
~MyGraphicsScene();
|
||
|
||
protected:
|
||
void mousePressEvent(QGraphicsSceneMouseEvent *event);
|
||
void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
|
||
void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
|
||
// void drawBackground(QPainter *painter, const QRectF &rect);
|
||
|
||
private:
|
||
QByteArray m_dispDat;
|
||
MyGraphicsGridItem *m_grid;
|
||
qreal m_radius = 0.5;
|
||
QBrush m_selectedBrush;
|
||
QBrush m_defaultBrush;
|
||
QList<QGraphicsEllipseItem *> m_ellipseItemList; //顶点圆圈的元素
|
||
QList<GLineItem> m_lineItemList; //直线元素
|
||
QList<GPathItem> m_pathItemList; //路径元素
|
||
QPointF m_dragOffset; //拖拽点的坐标
|
||
QGraphicsEllipseItem *m_draggingPoint; //拖拽点的元素
|
||
int m_ArcCount = 0; //圆弧记录坐标个数 个数在0-2之间
|
||
int m_shapeType = TYPE_DRAG;
|
||
bool m_isAdsorption = true;
|
||
int m_maxSegmentLength = 20;
|
||
int m_addOrder;//绘制图元时的绘制顺序
|
||
|
||
private:
|
||
// 计算两点之间的中点
|
||
QPointF midpoint(const QPointF &p1, const QPointF &p2);
|
||
// 计算两点之间连线的斜率
|
||
double slope(const QPointF &p1, const QPointF &p2);
|
||
// 计算圆心
|
||
bool findCircleCenter(const QPointF &p1, const QPointF &p2, const QPointF &p3, QPointF ¢er, double &radius);
|
||
// 计算半径
|
||
double findRadius(const QPointF ¢er, const QPointF &point);
|
||
// 计算两点之间的距离平方
|
||
double distanceSquared(QPointF p1, QPointF p2);
|
||
|
||
public:
|
||
QList<QGraphicsEllipseItem *> getPointList();
|
||
QList<GLineItem> getLineItemList();
|
||
QList<GPathItem> getPathItemList();
|
||
void addItemToScene(QByteArray ary);
|
||
void addItemListToScene(QList<drawItem> list);
|
||
void addGrid(double gridSpacing,QRectF rect);
|
||
void cleanScene();
|
||
bool getCurSelectState();
|
||
bool initAPoint();
|
||
void forwardAndBackward(int idx);
|
||
void insertPoint(int dir);
|
||
void removeSelectPoint();
|
||
void setShapeType(int type);
|
||
void setAdsorption(bool adsorption);
|
||
void setLastPointSelected();
|
||
void setMaxSegmentLength(int value);
|
||
int getMaxSegmentLength();
|
||
int getArcItem(QList<QPointF> points, QGraphicsPathItem* item, double threex[], double threey[]);
|
||
int getGLineItem(QGraphicsEllipseItem* p,QPointF p1);
|
||
int getGPathItem(QGraphicsEllipseItem*p,QPointF p1);
|
||
void setXYMove(double valX,double valY);
|
||
void revokeLastItem();//撤销最后一个图元
|
||
|
||
signals:
|
||
void siCurrentIndex(int idx);
|
||
|
||
public slots:
|
||
};
|
||
|
||
#endif // MYGRAPHICSSCENE_H
|