|
@@ -12,6 +12,7 @@
|
|
|
#include <QMouseEvent>
|
|
|
#include <QPainter>
|
|
|
#include <QListWidgetItem>
|
|
|
+#include <qmath.h>
|
|
|
#include <QDebug>
|
|
|
|
|
|
ImageHolder::ImageHolder(QWidget *aParent)
|
|
@@ -23,7 +24,11 @@ ImageHolder::ImageHolder(QWidget *aParent)
|
|
|
keyboard_modifier_ = Qt::NoModifier;
|
|
|
|
|
|
focused_selection_ = -1;
|
|
|
- focused_selection_type_ = NoTool;
|
|
|
+ focused_selection_type_ = NoFigure;
|
|
|
+
|
|
|
+ hovered_point_.figure = NoFigure;
|
|
|
+ hovered_point_.figureID = -1;
|
|
|
+ hovered_point_.pointID = -1;
|
|
|
|
|
|
list_bounding_box_ = 0;
|
|
|
main_label_ = 0;
|
|
@@ -31,7 +36,10 @@ ImageHolder::ImageHolder(QWidget *aParent)
|
|
|
|
|
|
scale_ = 1;
|
|
|
|
|
|
+ point_radius_ = 6;
|
|
|
+
|
|
|
setScaledContents(true);
|
|
|
+ setMouseTracking(true);
|
|
|
}
|
|
|
|
|
|
ImageHolder::~ImageHolder()
|
|
@@ -115,7 +123,7 @@ ImageHolder::drawBoundingBoxes(
|
|
|
int width = 1;
|
|
|
/* confirmed boxes */
|
|
|
for (int i = 0; i < list_bounding_box_->size(); i++) {
|
|
|
- int labelID = list_bounding_box_->at(i).label_ID_;
|
|
|
+ int labelID = list_bounding_box_->at(i)->label_ID_;
|
|
|
|
|
|
if (labelID < list_label_color_->count())
|
|
|
aPen->setColor(QColor(list_label_color_->at(labelID)));
|
|
@@ -128,24 +136,61 @@ ImageHolder::drawBoundingBoxes(
|
|
|
else
|
|
|
width = 1;
|
|
|
|
|
|
- if (BoundingBoxTool == focused_selection_type_ &&
|
|
|
+ if (RectFigure == focused_selection_type_ &&
|
|
|
focused_selection_ == i) {
|
|
|
penStyle = Qt::DotLine;
|
|
|
width = 2;
|
|
|
}
|
|
|
|
|
|
- aPen->setWidth(width);
|
|
|
- aPen->setStyle(penStyle);
|
|
|
- aPainter->setPen(*aPen);
|
|
|
-
|
|
|
/* scaling */
|
|
|
- QRect rect = list_bounding_box_->at(i).rect.normalized();
|
|
|
+ QRect rect = list_bounding_box_->at(i)->rect.normalized();
|
|
|
QPoint topLeft = rect.topLeft() * scale_;
|
|
|
QPoint bottomRight = rect.bottomRight() * scale_;
|
|
|
|
|
|
rect.setTopLeft(topLeft);
|
|
|
rect.setBottomRight(bottomRight);
|
|
|
|
|
|
+ if (focused_selection_ == i &&
|
|
|
+ focused_selection_type_ == RectFigure) {
|
|
|
+ QPen circPen;
|
|
|
+ circPen.setWidth(2);
|
|
|
+ circPen.setStyle(Qt::SolidLine);
|
|
|
+ circPen.setColor(aPen->color());
|
|
|
+ aPainter->setPen(circPen);
|
|
|
+ for (int j = 0; j < 4; j++) {
|
|
|
+ QPoint point;
|
|
|
+ if (!j) {
|
|
|
+ point = rect.topLeft();
|
|
|
+ }
|
|
|
+ else if (1 == j)
|
|
|
+ {
|
|
|
+ point = rect.topRight();
|
|
|
+ }
|
|
|
+ else if (2 == j)
|
|
|
+ {
|
|
|
+ point = rect.bottomRight();
|
|
|
+ }
|
|
|
+ else if (3 == j)
|
|
|
+ {
|
|
|
+ point = rect.bottomLeft();
|
|
|
+ }
|
|
|
+ if (i == hovered_point_.figureID &&
|
|
|
+ j == hovered_point_.pointID &&
|
|
|
+ RectFigure == hovered_point_.figure) {
|
|
|
+ QBrush brush;
|
|
|
+ brush.setColor(aPen->color());
|
|
|
+ brush.setStyle(Qt::SolidPattern);
|
|
|
+ aPainter->setBrush(brush);
|
|
|
+ }
|
|
|
+ aPainter->drawEllipse(point, point_radius_, point_radius_);
|
|
|
+ aPainter->setBrush(Qt::NoBrush);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ aPen->setWidth(width);
|
|
|
+ aPen->setStyle(penStyle);
|
|
|
+ aPainter->setPen(*aPen);
|
|
|
+
|
|
|
aPainter->drawRect(rect);
|
|
|
/* drawing label ids of these boxes */
|
|
|
QString labelIDText =
|
|
@@ -182,7 +227,7 @@ ImageHolder::drawPolygons(
|
|
|
/* confirmed polygons */
|
|
|
for (int i = 0; i < list_polygon_->size(); i++) {
|
|
|
penStyle = Qt::SolidLine;
|
|
|
- int labelID = list_polygon_->at(i).label_ID_;
|
|
|
+ int labelID = list_polygon_->at(i)->label_ID_;
|
|
|
|
|
|
if (labelID < list_label_color_->count())
|
|
|
aPen->setColor(QColor(list_label_color_->at(labelID)));
|
|
@@ -195,27 +240,45 @@ ImageHolder::drawPolygons(
|
|
|
else
|
|
|
width = 1;
|
|
|
|
|
|
- if (PolygonTool == focused_selection_type_ &&
|
|
|
+ if (PolyFigure == focused_selection_type_ &&
|
|
|
focused_selection_ == i) {
|
|
|
penStyle = Qt::DotLine;
|
|
|
width = 2;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- aPen->setWidth(width);
|
|
|
- aPen->setStyle(penStyle);
|
|
|
- aPainter->setPen(*aPen);
|
|
|
-
|
|
|
QPoint point;
|
|
|
- QPolygon poly = list_polygon_->at(i).poly;
|
|
|
+ QPolygon poly = list_polygon_->at(i)->poly;
|
|
|
for (int j = 0; j < poly.size(); j++) {
|
|
|
point.setX(poly.at(j).x());
|
|
|
point.setY(poly.at(j).y());
|
|
|
point *= scale_;
|
|
|
poly.remove(j);
|
|
|
poly.insert(j, point);
|
|
|
+
|
|
|
+ if (focused_selection_ == i &&
|
|
|
+ focused_selection_type_ == PolyFigure) {
|
|
|
+ QPen circPen;
|
|
|
+ circPen.setWidth(2);
|
|
|
+ circPen.setStyle(Qt::SolidLine);
|
|
|
+ circPen.setColor(aPen->color());
|
|
|
+ aPainter->setPen(circPen);
|
|
|
+ if (j == hovered_point_.pointID &&
|
|
|
+ i == hovered_point_.figureID &&
|
|
|
+ PolyFigure == hovered_point_.figure) {
|
|
|
+ QBrush brush;
|
|
|
+ brush.setColor(aPen->color());
|
|
|
+ brush.setStyle(Qt::SolidPattern);
|
|
|
+ aPainter->setBrush(brush);
|
|
|
+ }
|
|
|
+ aPainter->drawEllipse(point, point_radius_, point_radius_);
|
|
|
+ aPainter->setBrush(Qt::NoBrush);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ aPen->setWidth(width);
|
|
|
+ aPen->setStyle(penStyle);
|
|
|
+ aPainter->setPen(*aPen);
|
|
|
+
|
|
|
aPainter->drawPolygon(poly);
|
|
|
/* drawing label ids of these polys */
|
|
|
QString labelIDText =
|
|
@@ -283,20 +346,20 @@ ImageHolder::focusOnArea(QListWidgetItem *anItem)
|
|
|
|
|
|
if (!ok) {
|
|
|
focused_selection_ = -1;
|
|
|
- focused_selection_type_ = NoTool;
|
|
|
+ focused_selection_type_ = NoFigure;
|
|
|
return;
|
|
|
/* NOTREACHED */
|
|
|
}
|
|
|
|
|
|
switch (tool) {
|
|
|
case BoundingBoxTool:
|
|
|
- focused_selection_type_ = BoundingBoxTool;
|
|
|
+ focused_selection_type_ = RectFigure;
|
|
|
break;
|
|
|
case PolygonTool:
|
|
|
- focused_selection_type_ = PolygonTool;
|
|
|
+ focused_selection_type_ = PolyFigure;
|
|
|
break;
|
|
|
default:
|
|
|
- focused_selection_type_ = NoTool;
|
|
|
+ focused_selection_type_ = NoFigure;
|
|
|
break;
|
|
|
}
|
|
|
|
|
@@ -345,7 +408,8 @@ void
|
|
|
ImageHolder::clearFocusOnArea()
|
|
|
{
|
|
|
focused_selection_ = -1;
|
|
|
- focused_selection_type_ = NoTool;
|
|
|
+ focused_selection_type_ = NoFigure;
|
|
|
+ update();
|
|
|
}
|
|
|
|
|
|
void
|
|
@@ -368,7 +432,7 @@ ImageHolder::setTool(Tool aTool)
|
|
|
}
|
|
|
|
|
|
void
|
|
|
-ImageHolder::setBoundingBoxList(QList< BoundingBox > *aBBoxList)
|
|
|
+ImageHolder::setBoundingBoxList(QList< BoundingBox * > *aBBoxList)
|
|
|
{
|
|
|
if (0 == aBBoxList) {
|
|
|
return;
|
|
@@ -379,7 +443,7 @@ ImageHolder::setBoundingBoxList(QList< BoundingBox > *aBBoxList)
|
|
|
}
|
|
|
|
|
|
void
|
|
|
-ImageHolder::setPolygonList(QList< Polygon > *aPolygonList)
|
|
|
+ImageHolder::setPolygonList(QList< Polygon * > *aPolygonList)
|
|
|
{
|
|
|
if (0 == aPolygonList) {
|
|
|
return;
|
|
@@ -464,20 +528,21 @@ ImageHolder::confirmSelection()
|
|
|
/* NOTREACHED */
|
|
|
}
|
|
|
|
|
|
- switch (tool_) {
|
|
|
- case BoundingBoxTool:
|
|
|
- list_bounding_box_->append(bounding_box_);
|
|
|
+ if (BoundingBoxTool == tool_) {
|
|
|
+ BoundingBox *bbox = new BoundingBox;
|
|
|
+ *bbox = bounding_box_;
|
|
|
+ list_bounding_box_->append(bbox);
|
|
|
bounding_box_.rect.setRect(-1, -1, 0, 0);
|
|
|
- break;
|
|
|
- case PolygonTool:
|
|
|
- list_polygon_->append(polygon_);
|
|
|
+ }
|
|
|
+ else if (PolygonTool == tool_) {
|
|
|
+ Polygon *poly = new Polygon;
|
|
|
+ *poly = polygon_;
|
|
|
+ list_polygon_->append(poly);
|
|
|
polygon_.poly.clear();
|
|
|
- break;
|
|
|
- default:
|
|
|
- tool_ = NoTool;
|
|
|
- break;
|
|
|
}
|
|
|
|
|
|
+ list_poly_history_.clear();
|
|
|
+
|
|
|
state_ = StandBy;
|
|
|
update();
|
|
|
}
|
|
@@ -494,6 +559,17 @@ ImageHolder::tool()
|
|
|
return tool_;
|
|
|
}
|
|
|
|
|
|
+int
|
|
|
+ImageHolder::focusedSelection()
|
|
|
+{
|
|
|
+ return focused_selection_;
|
|
|
+}
|
|
|
+Figure
|
|
|
+ImageHolder::focusedSelectionType()
|
|
|
+{
|
|
|
+ return focused_selection_type_;
|
|
|
+}
|
|
|
+
|
|
|
void
|
|
|
ImageHolder::undo()
|
|
|
{
|
|
@@ -530,6 +606,82 @@ ImageHolder::redo()
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void
|
|
|
+ImageHolder::checkForPoints(QPoint *aPos)
|
|
|
+{
|
|
|
+ if ((!list_polygon_->count() &&
|
|
|
+ !list_bounding_box_->count()) ||
|
|
|
+ !aPos) {
|
|
|
+ return;
|
|
|
+ /* NOTREACHED */
|
|
|
+ }
|
|
|
+
|
|
|
+ int newRadius = 0;
|
|
|
+ int x = aPos->x();
|
|
|
+ int y = aPos->y();
|
|
|
+ /* center coordinates */
|
|
|
+ int xc = 0;
|
|
|
+ int yc = 0;
|
|
|
+ for (int i = 0; i < list_polygon_->count(); i++) {
|
|
|
+ QPolygon poly = list_polygon_->at(i)->poly;
|
|
|
+ for (int j = 0; j < poly.count(); j++) {
|
|
|
+ xc = poly.at(j).x();
|
|
|
+ yc = poly.at(j).y();
|
|
|
+ newRadius = qSqrt(qPow(x - xc, 2) + qPow(y - yc, 2));
|
|
|
+ if (newRadius <= point_radius_) {
|
|
|
+ hovered_point_.figure = PolyFigure;
|
|
|
+ hovered_point_.figureID = i;
|
|
|
+ hovered_point_.pointID = j;
|
|
|
+ repaint_needed_ = 1;
|
|
|
+ return;
|
|
|
+ /* NOTREACHED */
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ for (int i = 0; i < list_bounding_box_->count(); i++) {
|
|
|
+ QRect rect = list_bounding_box_->at(i)->rect;
|
|
|
+
|
|
|
+ for (int j = 0; j < 4; j++) {
|
|
|
+ if (!j) {
|
|
|
+ xc = rect.left();
|
|
|
+ yc = rect.top();
|
|
|
+ }
|
|
|
+ else if (1 == j)
|
|
|
+ {
|
|
|
+ xc = rect.right();
|
|
|
+ yc = rect.top();
|
|
|
+ }
|
|
|
+ else if (2 == j)
|
|
|
+ {
|
|
|
+ xc = rect.right();
|
|
|
+ yc = rect.bottom();
|
|
|
+ }
|
|
|
+ else if (3 == j)
|
|
|
+ {
|
|
|
+ xc = rect.left();
|
|
|
+ yc = rect.bottom();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ newRadius = qSqrt(qPow(x - xc, 2) + qPow(y - yc, 2));
|
|
|
+ if (newRadius <= point_radius_) {
|
|
|
+ hovered_point_.figure = RectFigure;
|
|
|
+ hovered_point_.figureID = i;
|
|
|
+ hovered_point_.pointID = j;
|
|
|
+ repaint_needed_ = 1;
|
|
|
+ return;
|
|
|
+ /* NOTREACHED */
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ hovered_point_.figure = NoFigure;
|
|
|
+ hovered_point_.figureID = -1;
|
|
|
+ hovered_point_.pointID = -1;
|
|
|
+ repaint_needed_ = 1;
|
|
|
+}
|
|
|
+
|
|
|
void
|
|
|
ImageHolder::keyPressEvent(QKeyEvent *anEvent)
|
|
|
{
|
|
@@ -570,8 +722,46 @@ ImageHolder::mouseMoveEvent(QMouseEvent *anEvent)
|
|
|
}
|
|
|
|
|
|
if (PolygonTool == tool_ &&
|
|
|
- NewSelection == state_) {
|
|
|
+ NewSelection == state_ &&
|
|
|
+ (anEvent->buttons() & Qt::LeftButton))
|
|
|
+ {
|
|
|
polygon_.poly.setPoint(polygon_.poly.count() - 1, pos);
|
|
|
+ //triggerPolygon(pos, &(polygon_.poly));
|
|
|
+ repaint_needed_ = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (-1 != focused_selection_ &&
|
|
|
+ !(anEvent->buttons() & Qt::LeftButton)) {
|
|
|
+ checkForPoints(&pos);
|
|
|
+ }
|
|
|
+
|
|
|
+ /* editing polygons */
|
|
|
+ if (-1 != hovered_point_.figureID &&
|
|
|
+ !list_polygon_->isEmpty() &&
|
|
|
+ PolyFigure == hovered_point_.figure &&
|
|
|
+ (anEvent->buttons() & Qt::LeftButton))
|
|
|
+ {
|
|
|
+ Polygon *poly = list_polygon_->at(hovered_point_.figureID);
|
|
|
+ poly->poly.setPoint(hovered_point_.pointID, pos);
|
|
|
+ repaint_needed_ = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* editing bounding boxes */
|
|
|
+ if (-1 != hovered_point_.figureID &&
|
|
|
+ !list_bounding_box_->isEmpty() &&
|
|
|
+ RectFigure == hovered_point_.figure &&
|
|
|
+ (anEvent->buttons() & Qt::LeftButton))
|
|
|
+ {
|
|
|
+ BoundingBox *rect = list_bounding_box_->at(hovered_point_.figureID);
|
|
|
+ if (0 == hovered_point_.pointID)
|
|
|
+ rect->rect.setTopLeft(pos);
|
|
|
+ else if (1 == hovered_point_.pointID)
|
|
|
+ rect->rect.setTopRight(pos);
|
|
|
+ else if (2 == hovered_point_.pointID)
|
|
|
+ rect->rect.setBottomRight(pos);
|
|
|
+ else if (3 == hovered_point_.pointID)
|
|
|
+ rect->rect.setBottomLeft(pos);
|
|
|
+
|
|
|
repaint_needed_ = 1;
|
|
|
}
|
|
|
|
|
@@ -591,6 +781,8 @@ ImageHolder::mousePressEvent(QMouseEvent *anEvent)
|
|
|
prev_cursor_pos_ = anEvent->pos() / scale_;
|
|
|
}
|
|
|
|
|
|
+ QPoint pos = anEvent->pos() / scale_;
|
|
|
+
|
|
|
if (anEvent->buttons() & Qt::LeftButton) {
|
|
|
/* clearing the selected area if it is not confirmed */
|
|
|
if (NewSelection == state_ && BoundingBoxTool == tool_) {
|
|
@@ -603,11 +795,12 @@ ImageHolder::mousePressEvent(QMouseEvent *anEvent)
|
|
|
NewSelection == state_ &&
|
|
|
Qt::NoModifier == keyboard_modifier_)
|
|
|
{
|
|
|
- triggerPolygon(anEvent->pos() / scale_, &(polygon_.poly));
|
|
|
+ triggerPolygon(pos, &(polygon_.poly));
|
|
|
}
|
|
|
|
|
|
/* starting new selection by click */
|
|
|
- if (StandBy == state_ && NoTool != tool_) {
|
|
|
+ if (StandBy == state_ && NoTool != tool_ &&
|
|
|
+ -1 == focused_selection_) {
|
|
|
state_ = NewSelection;
|
|
|
emit selectionStarted();
|
|
|
|
|
@@ -628,6 +821,20 @@ void
|
|
|
ImageHolder::mouseReleaseEvent(QMouseEvent *anEvent)
|
|
|
{
|
|
|
Q_UNUSED(anEvent)
|
|
|
+
|
|
|
+ if (-1 != hovered_point_.figureID)
|
|
|
+ emit areaEdited();
|
|
|
+
|
|
|
+ if (RectFigure == hovered_point_.figure &&
|
|
|
+ -1 != hovered_point_.figureID &&
|
|
|
+ !list_bounding_box_->
|
|
|
+ at(hovered_point_.figureID)->
|
|
|
+ rect.isValid()
|
|
|
+ )
|
|
|
+ {
|
|
|
+ BoundingBox *rect = list_bounding_box_->at(hovered_point_.figureID);
|
|
|
+ rect->rect = rect->rect.normalized();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/*
|