-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoollooparea.cpp
160 lines (133 loc) · 4.14 KB
/
toollooparea.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
#include "pub.h"
#include "toollooparea.h"
#include "videoviewbase.h"
#include "mainwindow.h"
#include "loopdetector.h"
ToolLoopArea::ToolLoopArea(VideoViewBase *parent)
: ToolImage(parent)
{
mPrePoint.setX(-1);
mPrePoint.setY(-1);
}
ToolLoopArea::~ToolLoopArea()
{
}
void ToolLoopArea::mousePress( QMouseEvent *mouseEvent )
{
VideoLoopTrackView *view = static_cast<VideoLoopTrackView*>( mView );
if (Qt::RightButton == mouseEvent->button()){ //# right button click, finished a loop area
LoopDetector *loop_detector = new LoopDetector( view );
loop_detector->setShape( mBoundaryPts );
// set detector background
//
qreal xMin;
qreal xMax;
qreal yMin;
qreal yMax;
loop_detector->mPolygon->boundingRect().getCoords(&xMin, &yMin, &xMax, &yMax);
if( xMin<0 )
xMin = 0;
if( yMin<0 )
yMin = 0;
if ( xMax>= view->mCurrentFrame.cols )
xMax = view->mCurrentFrame.cols;
if ( yMax>= view->mCurrentFrame.rows )
yMax = view->mCurrentFrame.rows;
Mat subM = view->mCurrentFrame( Range(yMin, yMax), Range(xMin, xMax) ).clone(); //row, col
loop_detector->mBackGroundMat = subM;
view->mMainWind->mMatWnd->showMatUp( subM ); //show in up matrix window
// add one loop detector to view
//
loop_detector->updateDetectorMask( view->mCurrentFrame ); //update the polygon area of the deteor area
view->mLoopDetectors.append( loop_detector );
view->mCurLoopDetector = loop_detector;
//# clear temporal points and lines
clearTempPointLine();
}
else if (Qt::LeftButton == mouseEvent->button()) // left mouse button, add point and line
{ //# add one point
//# save point to list
//#
QPointF pt = view->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 );
view->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 );
view->scene()->addItem( line ); //# show line
}
// record new previous point
mPrePoint.setX(pt.x());
mPrePoint.setY(pt.y());
}
}
void ToolLoopArea::keyPress( QKeyEvent *keyEvent )
{
VideoLoopTrackView *view = static_cast<VideoLoopTrackView*>( mView );
//''' 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
view->scene()->removeItem( pt );
}
//# clear temporal lines
//#
if (mLineItems.length() > 0){
QGraphicsLineItem *line = mLineItems.takeLast(); //# remove the last point
view->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 ToolLoopArea::clearTempPointLine()
{
VideoLoopTrackView *view = static_cast<VideoLoopTrackView*>( mView );
//# clear temporal points
//#
for (int i = 0; i < mPointItems.size(); ++i) {
view->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)
{
view->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();
}