-
Notifications
You must be signed in to change notification settings - Fork 0
/
CREATEARC.CPP
167 lines (144 loc) · 4.11 KB
/
CREATEARC.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include "math.h"
#include "Command.h"
#include "CreateCmd.h"
#include "diagramscene.h"
#include <QPainter>
#include <QGraphicsPathItem>
#include <QRectF>
#include <QGraphicsLineItem>
#include <QLineF>
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////
/*
* CLASS CCreateArc
*/
CCreateArc::CCreateArc(DiagramScene* pDiagramScene)
: m_center(0,0), m_begin(0,0), m_end(0,0)
{
m_nStep = 0; // 初始化操作步为 0
m_pDiagramScene = pDiagramScene;
}
CCreateArc::~CCreateArc()
{
}
int CCreateArc::GetType()
{
return ctCreateArc;
}
int CCreateArc::OnLButtonDown(QGraphicsSceneMouseEvent* mouseEvent)
{
m_nStep ++; // 每次单击鼠标左键时操作步加 1
QPainterPath path;
switch(m_nStep)
{
case 1:
{
m_center = mouseEvent->scenePos();
Prompt(QStringLiteral("请输入圆弧的起始点:"));
path.arcMoveTo(m_center.x(), m_center.y(),0,0,0);
path.arcTo(m_center.x(), m_center.y(), 0, 0, 0,0);
m_pArc = new QGraphicsPathItem;
m_pArc->setPath(path);
//pArc->setPen();
m_pDiagramScene->addItem(m_pArc);
//辅助线
QLineF line(m_center, m_center);
m_pLineItem = new QGraphicsLineItem();
m_pLineItem->setLine(line);
m_pDiagramScene->addItem(m_pLineItem);
break;
}
case 2:
{
m_begin = mouseEvent->scenePos();
Prompt(QStringLiteral("请输入圆弧的终点:"));
break;
}
case 3:
{
m_end = mouseEvent->scenePos();
//qreal r; //半径
////r = sqrt((m_center.x() - m_end.x()) * (m_center.x() - m_end.x()) + (m_center.y() - m_end.y()) * (m_center.y() - m_end.y()));
//qreal delX = m_begin.x() - m_center.x();
//qreal delY = m_begin.y() - m_center.y();
//r = sqrt(delX * delX + delY * delY);
//qreal startAng = acos(delX / r);
//delX = m_end.x() - m_center.x();
//delY = m_end.y() - m_center.y();
//r = sqrt(delX * delX + delY * delY);
//qreal endAng = acos(delX / r);
//path.arcMoveTo(m_center.x(), m_center.y(), 2*r, 2*r, startAng*57);
//path.arcTo(m_center.x(), m_center.y(), 2*r, 2*r, startAng, endAng*57);
////QGraphicsPathItem* pArc = new QGraphicsPathItem;
//m_pArc->setPath(path);
m_pDiagramScene->removeItem(m_pLineItem);
delete m_pLineItem;
m_nStep = 0;
Prompt(QStringLiteral("请输入圆弧的中心点:"));
break;
}
}
return 0;
}
int CCreateArc::OnMouseMove(QGraphicsSceneMouseEvent* mouseEvent)
{
//::SetCursor(AfxGetApp()->LoadCursor(IDC_DRAW_ARC));
switch(m_nStep)
{
case 0:
Prompt(QStringLiteral("请输入圆的中心点:"));
break;
case 1:
{
QLineF line(m_center, mouseEvent->scenePos());
m_pLineItem->setLine(line);
Prompt(QStringLiteral("请输入圆弧的起点:"));
break;
}
case 2:
{
Prompt(QStringLiteral("请输入圆弧的终点:"));
m_end = mouseEvent->scenePos();
qreal r; //半径
//r = sqrt((m_center.x() - m_end.x()) * (m_center.x() - m_end.x()) + (m_center.y() - m_end.y()) * (m_center.y() - m_end.y()));
qreal delX = m_begin.x() - m_center.x();
qreal delY = m_begin.y() - m_center.y();
r = sqrt(delX * delX + delY * delY);
QPointF leftBottom(m_center.x() - r, m_center.y() - r);
QPointF rightTop(m_center.x() + r, m_center.y() + r);
QRectF rect(leftBottom, rightTop);
qreal startAng = acos(delX / r);
delX = m_end.x() - m_center.x();
delY = m_end.y() - m_center.y();
r = sqrt(delX * delX + delY * delY);
qreal endAng = acos(delX / r);
QPainterPath path;
path.arcMoveTo(rect, startAng*180/3.14);
path.arcTo(rect, startAng* 180 / 3.14, (endAng-startAng)* 180 / 3.14);
//QGraphicsPathItem* pArc = new QGraphicsPathItem;
m_pArc->setPath(path);
break;
}
}
return 0;
}
int CCreateArc::OnRButtonDown(QGraphicsSceneMouseEvent* mouseEvent)
{
// 如果当前的操作步为 2 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线
if(m_nStep == 2){
}
m_nStep = 0; // 将操作步重置为 0
Prompt(QStringLiteral("请输入圆弧的中心点:"));
return 0;
}
// 调用Cancel 函数取消本次操作
int CCreateArc::Cancel()
{
// 如果当前的操作步为 2 ,那么要在结束本次操作前擦除上次鼠标移动时绘制的橡皮线
if(m_nStep == 2){
;
}
m_nStep = 0; // 将操作步重置为 0
Prompt(QStringLiteral("就绪")); // 等待提示新类型的命令操作
return 0;
}