-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrameLabel.cpp
More file actions
186 lines (177 loc) · 6.47 KB
/
Copy pathFrameLabel.cpp
File metadata and controls
186 lines (177 loc) · 6.47 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/************************************************************************/
/* qt-opencv-multithreaded: */
/* A multithreaded OpenCV application using the Qt framework. */
/* */
/* FrameLabel.cpp */
/* */
/* Nick D'Ademo <nickdademo@gmail.com> */
/* */
/* Copyright (c) 2012-2013 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"
// Qt
#include <QtGui/QPainter>
#include <QtGui/QMouseEvent>
FrameLabel::FrameLabel(QWidget *parent) : QLabel(parent)
{
startPoint.setX(0);
startPoint.setY(0);
mouseCursorPos.setX(0);
mouseCursorPos.setY(0);
drawBox=false;
mouseData.leftButtonRelease=false;
mouseData.rightButtonRelease=false;
createContextMenu();
}
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();
}
void FrameLabel::setMouseCursorPos(QPoint input)
{
mouseCursorPos=input;
}
QPoint FrameLabel::getMouseCursorPos()
{
return mouseCursorPos;
}
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
{
// Show context menu
menu->exec(ev->globalPos());
}
}
}
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;
}
}
void FrameLabel::paintEvent(QPaintEvent *ev)
{
QLabel::paintEvent(ev);
QPainter painter(this);
// Draw box
if(drawBox)
{
painter.setPen(Qt::blue);
painter.drawRect(*box);
}
}
void FrameLabel::createContextMenu()
{
// Create top-level menu object
menu = new QMenu(this);
// Add actions
QAction *action;
action = new QAction(this);
action->setText(tr("Reset ROI"));
menu->addAction(action);
action = new QAction(this);
action->setText(tr("Scale to Fit Frame"));
action->setCheckable(true);
menu->addAction(action);
menu->addSeparator();
// Create image processing menu object
QMenu* menu_imgProc = new QMenu(this);
menu_imgProc->setTitle("Image Processing");
menu->addMenu(menu_imgProc);
// Add actions
action = new QAction(this);
action->setText(tr("Grayscale"));
action->setCheckable(true);
menu_imgProc->addAction(action);
action = new QAction(this);
action->setText(tr("Smooth"));
action->setCheckable(true);
menu_imgProc->addAction(action);
action = new QAction(this);
action->setText(tr("Dilate"));
action->setCheckable(true);
menu_imgProc->addAction(action);
action = new QAction(this);
action->setText(tr("Erode"));
action->setCheckable(true);
menu_imgProc->addAction(action);
action = new QAction(this);
action->setText(tr("Flip"));
action->setCheckable(true);
menu_imgProc->addAction(action);
action = new QAction(this);
action->setText(tr("Canny"));
action->setCheckable(true);
menu_imgProc->addAction(action);
menu_imgProc->addSeparator();
action = new QAction(this);
action->setText(tr("Settings..."));
menu_imgProc->addAction(action);
}