-
Notifications
You must be signed in to change notification settings - Fork 7
/
FrameLabel.cpp
139 lines (131 loc) · 5.28 KB
/
FrameLabel.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
/************************************************************************/
/* qt-opencv-multithreaded: */
/* A multithreaded OpenCV application using the Qt framework. */
/* */
/* FrameLabel.cpp */
/* */
/* Nick D'Ademo <[email protected]> */
/* */
/* Copyright (c) 2012 Nick D'Ademo */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#include "FrameLabel.h"
FrameLabel::FrameLabel(QWidget *parent) : QLabel(parent)
{
// Initialize variables
startPoint.setX(0);
startPoint.setY(0);
mouseCursorPos.setX(0);
mouseCursorPos.setY(0);
drawBox=false;
// Initialize MouseData structure
mouseData.leftButtonRelease=false;
mouseData.rightButtonRelease=false;
} // FrameLabel constructor
void FrameLabel::mouseMoveEvent(QMouseEvent *ev)
{
// Save mouse cursor position
setMouseCursorPos(ev->pos());
// Update box width and height if box drawing is in progress
if(drawBox)
{
box->setWidth(getMouseCursorPos().x()-startPoint.x());
box->setHeight(getMouseCursorPos().y()-startPoint.y());
}
// Inform main window of mouse move event
emit onMouseMoveEvent();
} // mouseMoveEvent()
void FrameLabel::setMouseCursorPos(QPoint input)
{
mouseCursorPos=input;
} // setMouseCursorPos()
QPoint FrameLabel::getMouseCursorPos()
{
return mouseCursorPos;
} // getMouseXPos()
void FrameLabel::mouseReleaseEvent(QMouseEvent *ev)
{
// Update cursor position
setMouseCursorPos(ev->pos());
// On left mouse button release
if(ev->button()==Qt::LeftButton)
{
// Set leftButtonRelease flag to TRUE
mouseData.leftButtonRelease=true;
if(drawBox)
{
// Stop drawing box
drawBox=false;
// Save box dimensions
mouseData.selectionBox.setX(box->left());
mouseData.selectionBox.setY(box->top());
mouseData.selectionBox.setWidth(box->width());
mouseData.selectionBox.setHeight(box->height());
// Set leftButtonRelease flag to TRUE
mouseData.leftButtonRelease=true;
// Inform main window of event
emit newMouseData(mouseData);
}
// Set leftButtonRelease flag to FALSE
mouseData.leftButtonRelease=false;
}
// On right mouse button release
else if(ev->button()==Qt::RightButton)
{
// If user presses (and then releases) the right mouse button while drawing box, stop drawing box
if(drawBox)
drawBox=false;
else
{
// Set rightButtonRelease flag to TRUE
mouseData.rightButtonRelease=true;
// Inform main window of event
emit newMouseData(mouseData);
// Set rightButtonRelease flag to FALSE
mouseData.rightButtonRelease=false;
}
}
} // mouseReleaseEvent()
void FrameLabel::mousePressEvent(QMouseEvent *ev)
{
// Update cursor position
setMouseCursorPos(ev->pos());;
if(ev->button()==Qt::LeftButton)
{
// Start drawing box
startPoint=ev->pos();
box=new QRect(startPoint.x(),startPoint.y(),0,0);
drawBox=true;
}
} // mousePressEvent()
void FrameLabel::paintEvent(QPaintEvent *ev)
{
QLabel::paintEvent(ev);
QPainter painter(this);
// Draw box
if(drawBox)
{
painter.setPen(Qt::blue);
painter.drawRect(*box);
}
} // paintEvent()