-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoolcoordinateaxis.cpp
149 lines (124 loc) · 3.71 KB
/
toolcoordinateaxis.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
#include "toolcoordinateaxis.h"
#include "pub.h"
#include "toollooparea.h"
#include "videoviewbase.h"
#include "mainwindow.h"
#include "loopdetector.h"
ToolCoordinateAxis::ToolCoordinateAxis(VideoViewBase *parent)
: ToolImage(parent)
{
mPrePoint.setX(-1);
mPrePoint.setY(-1);
}
ToolCoordinateAxis::~ToolCoordinateAxis()
{
}
void ToolCoordinateAxis::mousePress( QMouseEvent *mouseEvent )
{
if (Qt::RightButton == mouseEvent->button()){ //# right button click, finished a loop area
// draw last line segment
QPointF pt = mBoundaryPts.first();
// draw temporal point
//
QGraphicsEllipseItem *pointItem = new QGraphicsEllipseItem( pt.x(), pt.y(), 1, 1);
pointItem->setPen( QPen(Qt::red) );
mPointItems.append( pointItem );
mView->scene()->addItem( pointItem ); //# show line
// draw temporal line
//
if ( mPrePoint.x()>=0 && mPrePoint.y()>=0 ){
QGraphicsLineItem *line = new QGraphicsLineItem( mPrePoint.x(), mPrePoint.y(), pt.x(), pt.y() );
line->setPen( QPen(Qt::green) );
mLineItems.append( line );
mView->scene()->addItem( line ); //# show line
}
// start another line
//
mPrePoint.setX(-1);
mPrePoint.setY(-1);
mBoundaryPts.clear();
}
else if (Qt::LeftButton == mouseEvent->button()) // left mouse button, add point and line
{ // add one point
// save point to list
QPointF pt = mView->mapToScene( QPoint(mouseEvent->x(), mouseEvent->y()) );
mBoundaryPts.append( pt );
// draw temporal point
//
QGraphicsEllipseItem *pointItem = new QGraphicsEllipseItem( pt.x(), pt.y(), 1, 1);
pointItem->setPen( QPen(Qt::red) );
mPointItems.append( pointItem );
mView->scene()->addItem( pointItem ); //# show line
// draw temporal line
//
if ( mPrePoint.x()>=0 && mPrePoint.y()>=0 ){
QGraphicsLineItem *line = new QGraphicsLineItem( mPrePoint.x(), mPrePoint.y(), pt.x(), pt.y() );
line->setPen( QPen(Qt::green) );
mLineItems.append( line );
mView->scene()->addItem( line ); //# show line
}
// record new previouse point
mPrePoint.setX(pt.x());
mPrePoint.setY(pt.y());
}
}
void ToolCoordinateAxis::keyPress( QKeyEvent *keyEvent )
{
//''' remove last added detector area
// keyEvent - QKeyEvent
// '''
//# ESC, delete the just added detector rectangle boundary point
//#
if (keyEvent->key() == Qt::Key_Escape) {
if (mPointItems.length() > 0) {
QGraphicsEllipseItem *pt = mPointItems.takeLast(); //# remove the last point
mView->scene()->removeItem( pt );
}
//# clear temporal lines
//#
if (mLineItems.length() > 0){
QGraphicsLineItem *line = mLineItems.takeLast(); //# remove the last point
mView->scene()->removeItem( line );
}
//# remove the previous boundary point
//#
if ( mBoundaryPts.count() > 0){
mBoundaryPts.pop_back();
//# if boundary point list is not empty, change pre-point
//#
if (mBoundaryPts.count() > 0 ){
mPrePoint = QPoint( mBoundaryPts.last().x(), mBoundaryPts.last().y() ); //# new pre-point for draw new line
} else {
mPrePoint.setX(-1);
mPrePoint.setY(-1);
}
}
} // end if (keyEvent->key() == Qt::Key_Escape) {
}
void ToolCoordinateAxis::clearTempPointLine()
{
//# clear temporal points
//#
for (int i = 0; i < mPointItems.size(); ++i) {
mView->scene()->removeItem( mPointItems.at(i) );
QGraphicsEllipseItem *pPt = mPointItems.at(i);
if (pPt != NULL)
delete pPt;
}
mPointItems.clear();
//# clear temporal lines
//#
for (int i = 0; i < mLineItems.size(); ++i)
{
mView->scene()->removeItem( mLineItems.at(i) );
QGraphicsLineItem *pLine = mLineItems.at(i);
if (pLine != NULL)
delete pLine;
}
mLineItems.clear();
//# clear points and prepoint for starting new a detector
//#
mPrePoint.setX(-1);
mPrePoint.setY(-1);
mBoundaryPts.clear();
}