From 1df45ea003ebbc7dbf240e12b3492134c87b6cee Mon Sep 17 00:00:00 2001 From: yonghye Date: Wed, 7 Nov 2018 14:45:20 +0900 Subject: [PATCH] drag and drop => two click event, tableWidget Horizontol Size Error Fix --- label_img.cpp | 92 +++++++++++++++++++++++++++----------------------- label_img.h | 3 +- mainwindow.cpp | 4 ++- mainwindow.ui | 8 ++++- 4 files changed, 62 insertions(+), 45 deletions(-) diff --git a/label_img.cpp b/label_img.cpp index 27e98d7..527c6f5 100644 --- a/label_img.cpp +++ b/label_img.cpp @@ -15,11 +15,9 @@ QColor label_img::BOX_COLORS[10] ={ Qt::green, Qt::darkCyan}; label_img::label_img(QWidget *parent) - :QLabel(parent), - m_bMouseLeftButtonClicked(false), - m_focusedObjectLabel(0) - + :QLabel(parent) { + init(); } void label_img::mouseMoveEvent(QMouseEvent *ev) @@ -45,8 +43,31 @@ void label_img::mousePressEvent(QMouseEvent *ev) } else if(ev->button() == Qt::LeftButton) { - m_bMouseLeftButtonClicked = true; - m_relatvie_mouse_pos_LBtnClicked_in_ui = m_relative_mouse_pos_in_ui; + if(m_bLabelingStarted == false) + { + m_relatvie_mouse_pos_LBtnClicked_in_ui = m_relative_mouse_pos_in_ui; + m_bLabelingStarted = true; + } + else + { + setMousePosition(ev->x(), ev->y()); + + ObjectLabelingBox objBoundingbox; + + objBoundingbox.label = m_focusedObjectLabel; + objBoundingbox.box = getRelativeRectFromTwoPoints(m_relative_mouse_pos_in_ui, + m_relatvie_mouse_pos_LBtnClicked_in_ui); + + bool width_is_too_small = objBoundingbox.box.width() < 0.01; + bool height_is_too_small = objBoundingbox.box.height() < 0.01; + + if(!width_is_too_small && !height_is_too_small) + m_objBoundingBoxes.push_back(objBoundingbox); + + m_bLabelingStarted = false; + + showImage(); + } } emit Mouse_Pressed(); @@ -55,30 +76,29 @@ void label_img::mousePressEvent(QMouseEvent *ev) void label_img::mouseReleaseEvent(QMouseEvent *ev) { std::cout<< "released"<< std::endl; - - if(ev->button() == Qt::LeftButton && m_bMouseLeftButtonClicked == true) - { - setMousePosition(ev->x(), ev->y()); - - ObjectLabelingBox objBoundingbox; - - objBoundingbox.label = m_focusedObjectLabel; - objBoundingbox.box = getRelativeRectFromTwoPoints(m_relative_mouse_pos_in_ui, - m_relatvie_mouse_pos_LBtnClicked_in_ui); - - bool width_is_too_small = objBoundingbox.box.width() < 0.01; - bool height_is_too_small = objBoundingbox.box.height() < 0.01; - - if(!width_is_too_small && !height_is_too_small) - m_objBoundingBoxes.push_back(objBoundingbox); - - m_bMouseLeftButtonClicked = false; - } - - showImage(); emit Mouse_Release(); } +void label_img::init() +{ + m_objBoundingBoxes.clear(); + + m_bLabelingStarted = false; + m_focusedObjectLabel = 0; + + QPoint mousePosInUi = this->mapFromGlobal(QCursor::pos()); + bool mouse_is_in_image = QRect(0, 0, this->width(), this->height()).contains(mousePosInUi); + + if (mouse_is_in_image) + { + setMousePosition(mousePosInUi.x(), mousePosInUi.y()); + } + else + { + setMousePosition(0., 0.); + } +} + void label_img::setMousePosition(int x, int y) { if(x < 0) x = 0; @@ -95,19 +115,7 @@ void label_img::openImage(const QString &qstrImg) m_inputImg = QImage(qstrImg); m_inputImg = m_inputImg.convertToFormat(QImage::Format_RGB888); - m_objBoundingBoxes.clear(); - - m_relatvie_mouse_pos_LBtnClicked_in_ui = QPointF(); - - m_bMouseLeftButtonClicked = false; - - QPoint mousePosInUi = this->mapFromGlobal(QCursor::pos()); - bool mouse_is_in_image = QRect(0, 0, this->width(), this->height()).contains(mousePosInUi); - - if (mouse_is_in_image) - { - setMousePosition(mousePosInUi.x(), mousePosInUi.y()); - } + init(); } void label_img::showImage() @@ -187,7 +195,7 @@ QImage label_img::crop(QRect rect) void label_img::drawCrossLine(QPainter& painter, QColor color, int thickWidth) { - if(m_relative_mouse_pos_in_ui == QPointF()) return; + if(m_relative_mouse_pos_in_ui == QPointF(0., 0.)) return; QPen pen; pen.setWidth(thickWidth); @@ -207,7 +215,7 @@ void label_img::drawCrossLine(QPainter& painter, QColor color, int thickWidth) void label_img::drawFocusedObjectBox(QPainter& painter, Qt::GlobalColor color, int thickWidth) { - if(m_bMouseLeftButtonClicked) + if(m_bLabelingStarted == true) { QPen pen; pen.setWidth(thickWidth); diff --git a/label_img.h b/label_img.h index 6c6c34b..928c90d 100644 --- a/label_img.h +++ b/label_img.h @@ -32,12 +32,13 @@ public: int m_imgX; int m_imgY; - bool m_bMouseLeftButtonClicked; + bool m_bLabelingStarted; static QColor BOX_COLORS[10]; QVector m_objBoundingBoxes; + void init(); void openImage(const QString &); void showImage(); diff --git a/mainwindow.cpp b/mainwindow.cpp index 9742b98..abd75df 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -326,8 +326,9 @@ void MainWindow::keyPressEvent(QKeyEvent * event) { cout << "key pressed" <key(); + bool graveAccentKeyIsPressed = (nKey == Qt::Key_QuoteLeft); - bool numKeyIsPressed = (nKey >= Qt::Key_0 && nKey <= Qt::Key_9 ); + bool numKeyIsPressed = (nKey >= Qt::Key_0 && nKey <= Qt::Key_9 ); if(graveAccentKeyIsPressed) { @@ -397,6 +398,7 @@ void MainWindow::init_tableWidget() { ui->tableWidget_label->horizontalHeader()->setVisible(true); ui->tableWidget_label->horizontalHeader()->setStyleSheet(""); + ui->tableWidget_label->horizontalHeader()->setStretchLastSection(true); disconnect(ui->tableWidget_label->horizontalHeader(), SIGNAL(sectionPressed(int)),ui->tableWidget_label, SLOT(selectColumn(int))); } diff --git a/mainwindow.ui b/mainwindow.ui index e18c764..9b95bff 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -604,7 +604,7 @@ border-color: rgb(0, 255, 255); - 204 + 210 16777215 @@ -671,9 +671,15 @@ QTableView { false + + 40 + true + + true + false