diff --git a/include/roiItem.h b/include/roiItem.h index 06306b482ffa69f69d533cef22ba1c9af42e1e20..5ad0847100bff90243b10584307aa8b0d6f9f4ef 100644 --- a/include/roiItem.h +++ b/include/roiItem.h @@ -25,7 +25,6 @@ #include <QObject> class Petrack; -class Control; class RoiItem : public QObject, public QGraphicsRectItem { @@ -34,7 +33,7 @@ class RoiItem : public QObject, public QGraphicsRectItem inline static constexpr int DISTANCE_TO_BORDER = 5; inline static constexpr int MIN_SIZE = 10; - enum pressLocation + enum class PressLocation { inside, top, @@ -48,26 +47,23 @@ class RoiItem : public QObject, public QGraphicsRectItem }; private: - Petrack *mMainWindow; - Control *mControlWidget; - QRect mPressRect; - QPointF mPressPos; - enum pressLocation mPressLocation; - bool mIsFixed; + Petrack *mMainWindow; + QRect mPressRect; + QPointF mPressPos; + PressLocation mPressLocation{PressLocation::inside}; + bool mIsFixed{false}; public: - RoiItem(QWidget *wParent, QColor color, QGraphicsItem *parent = nullptr); - void mousePressEvent(QGraphicsSceneMouseEvent *event); - void mouseReleaseEvent(QGraphicsSceneMouseEvent *event); - void mouseMoveEvent(QGraphicsSceneMouseEvent *event); - void hoverMoveEvent(QGraphicsSceneHoverEvent *event); - void checkRect(); - -public slots: + RoiItem(QWidget *wParent, const QColor &color); + void mousePressEvent(QGraphicsSceneMouseEvent *event) override; + void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override; + void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override; + void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override; + void restoreSize(); void setFixed(bool fixed) { mIsFixed = fixed; } signals: - void changed(); // NOLINT + void changed(); }; #endif // ROIITEM_H diff --git a/src/petrack.cpp b/src/petrack.cpp index 0959c5599e4bc2994f6e892683a272058cd79596..fdd5a873dd8395adb9a97da9d4bc9c85d7a1846a 100644 --- a/src/petrack.cpp +++ b/src/petrack.cpp @@ -3675,7 +3675,7 @@ void Petrack::updateImage(bool imageChanged) // default = false (only true for n size.height = mImgFiltered.rows; mTracker->resize(size); - mTrackingRoiItem->checkRect(); + mTrackingRoiItem->restoreSize(); } #ifndef STEREO_DISABLED // buildt disparity picture if it should be used for height detection @@ -3731,7 +3731,7 @@ void Petrack::updateImage(bool imageChanged) // default = false (only true for n #endif if(borderChanged) { - mRecognitionRoiItem->checkRect(); + mRecognitionRoiItem->restoreSize(); } if(mControlWidget->performRecognition->checkState() == Qt::Checked) diff --git a/src/roiItem.cpp b/src/roiItem.cpp index 9bef483e995b7c69ce3be121d9b8684fff73e511..49ddc6545d88c549f806a5a455448fbe285839cb 100644 --- a/src/roiItem.cpp +++ b/src/roiItem.cpp @@ -20,17 +20,13 @@ #include "roiItem.h" -#include "control.h" #include "petrack.h" -#include "view.h" -#include <QtWidgets> -RoiItem::RoiItem(QWidget *wParent, QColor color, QGraphicsItem *parent) : QObject(wParent), QGraphicsRectItem(parent) +RoiItem::RoiItem(QWidget *wParent, const QColor &color) : QObject(wParent) { - mMainWindow = (class Petrack *) wParent; - mControlWidget = mMainWindow->getControlWidget(); - setRect(0, 0, 0, 0); // qreal x, qreal y, qreal width, qreal height + mMainWindow = dynamic_cast<class Petrack *>(wParent); + setRect(0, 0, 0, 0); QPen pen(color); setPen(pen); setAcceptHoverEvents(true); @@ -49,17 +45,17 @@ void RoiItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if((event->pos()).y() < DISTANCE_TO_BORDER + mPressRect.y()) { - mPressLocation = topLeft; + mPressLocation = PressLocation::topLeft; setCursor(Qt::SizeFDiagCursor); } else if((event->pos()).y() > mPressRect.height() + mPressRect.y() - DISTANCE_TO_BORDER) { - mPressLocation = bottomLeft; + mPressLocation = PressLocation::bottomLeft; setCursor(Qt::SizeBDiagCursor); } else { - mPressLocation = left; + mPressLocation = PressLocation::left; setCursor(Qt::SizeHorCursor); } } @@ -67,33 +63,33 @@ void RoiItem::mousePressEvent(QGraphicsSceneMouseEvent *event) { if((event->pos()).y() < DISTANCE_TO_BORDER + mPressRect.y()) { - mPressLocation = topRight; + mPressLocation = PressLocation::topRight; setCursor(Qt::SizeBDiagCursor); } else if((event->pos()).y() > mPressRect.height() + mPressRect.y() - DISTANCE_TO_BORDER) { - mPressLocation = bottomRight; + mPressLocation = PressLocation::bottomRight; setCursor(Qt::SizeFDiagCursor); } else { - mPressLocation = right; + mPressLocation = PressLocation::right; setCursor(Qt::SizeHorCursor); } } else if((event->pos()).y() < DISTANCE_TO_BORDER + mPressRect.y()) { - mPressLocation = top; + mPressLocation = PressLocation::top; setCursor(Qt::SizeVerCursor); } else if((event->pos()).y() > mPressRect.height() + mPressRect.y() - DISTANCE_TO_BORDER) { - mPressLocation = bottom; + mPressLocation = PressLocation::bottom; setCursor(Qt::SizeVerCursor); } else { - mPressLocation = inside; + mPressLocation = PressLocation::inside; setCursor(Qt::ClosedHandCursor); } } @@ -113,132 +109,127 @@ void RoiItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event) QGraphicsRectItem::mouseReleaseEvent(event); } -// event, of moving mouse while mouse button is pressed void RoiItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event) { + // event, of moving mouse while mouse button is pressed if(!mIsFixed) { QImage *img = mMainWindow->getImage(); QPoint diff = QPoint(myRound((event->pos() - mPressPos).x()), myRound((event->pos() - mPressPos).y())); - // raender des bildes nicht ueberscheiten - // swappen des rechtecks vermeiden, damit keine negativen width... + // do not extend over the border of the image + // do not swap the rectangle, to avoid negative width if(img != nullptr) { - if(mPressLocation == inside || mPressLocation == topLeft || mPressLocation == left || - mPressLocation == bottomLeft) + if(mPressLocation == PressLocation::inside || mPressLocation == PressLocation::topLeft || + mPressLocation == PressLocation::left || mPressLocation == PressLocation::bottomLeft) { if(mPressRect.x() + diff.x() < -mMainWindow->getImageBorderSize()) { diff.setX(-mPressRect.x() - mMainWindow->getImageBorderSize()); } - if(mPressLocation != inside && mPressRect.width() - diff.x() < MIN_SIZE) + if(mPressLocation != PressLocation::inside && mPressRect.width() - diff.x() < MIN_SIZE) { diff.setX(mPressRect.width() - MIN_SIZE); } } - if(mPressLocation == inside || mPressLocation == topLeft || mPressLocation == top || - mPressLocation == topRight) + if(mPressLocation == PressLocation::inside || mPressLocation == PressLocation::topLeft || + mPressLocation == PressLocation::top || mPressLocation == PressLocation::topRight) { if(mPressRect.y() + diff.y() < -mMainWindow->getImageBorderSize()) { diff.setY(-mPressRect.y() - mMainWindow->getImageBorderSize()); } - if(mPressLocation != inside && mPressRect.height() - diff.y() < MIN_SIZE) + if(mPressLocation != PressLocation::inside && mPressRect.height() - diff.y() < MIN_SIZE) { diff.setY(mPressRect.height() - MIN_SIZE); } } - if(mPressLocation == inside || mPressLocation == topRight || mPressLocation == right || - mPressLocation == bottomRight) + if(mPressLocation == PressLocation::inside || mPressLocation == PressLocation::topRight || + mPressLocation == PressLocation::right || mPressLocation == PressLocation::bottomRight) { if(mPressRect.x() + diff.x() + mPressRect.width() > img->width() - mMainWindow->getImageBorderSize()) { diff.setX(img->width() - mPressRect.x() - mPressRect.width() - mMainWindow->getImageBorderSize()); } - if(mPressLocation != inside && mPressRect.width() + diff.x() < MIN_SIZE) + if(mPressLocation != PressLocation::inside && mPressRect.width() + diff.x() < MIN_SIZE) { diff.setX(-mPressRect.width() + MIN_SIZE); } } - if(mPressLocation == inside || mPressLocation == bottomLeft || mPressLocation == bottom || - mPressLocation == bottomRight) + if(mPressLocation == PressLocation::inside || mPressLocation == PressLocation::bottomLeft || + mPressLocation == PressLocation::bottom || mPressLocation == PressLocation::bottomRight) { if(mPressRect.y() + diff.y() + mPressRect.height() > img->height() - mMainWindow->getImageBorderSize()) { diff.setY(img->height() - mPressRect.y() - mPressRect.height() - mMainWindow->getImageBorderSize()); } - if(mPressLocation != inside && mPressRect.height() + diff.y() < MIN_SIZE) + if(mPressLocation != PressLocation::inside && mPressRect.height() + diff.y() < MIN_SIZE) { diff.setY(-mPressRect.height() + MIN_SIZE); } } } - if(mPressLocation == topLeft) - { - setRect( - mPressRect.x() + diff.x(), - mPressRect.y() + diff.y(), - mPressRect.width() - diff.x(), - mPressRect.height() - diff.y()); - } - else if(mPressLocation == topRight) - { - setRect( - mPressRect.x(), - mPressRect.y() + diff.y(), - mPressRect.width() + diff.x(), - mPressRect.height() - diff.y()); - } - else if(mPressLocation == bottomLeft) - { - setRect( - mPressRect.x() + diff.x(), - mPressRect.y(), - mPressRect.width() - diff.x(), - mPressRect.height() + diff.y()); - } - else if(mPressLocation == bottomRight) - { - setRect(mPressRect.x(), mPressRect.y(), mPressRect.width() + diff.x(), mPressRect.height() + diff.y()); - } - else if(mPressLocation == left) - { - setRect(mPressRect.x() + diff.x(), mPressRect.y(), mPressRect.width() - diff.x(), mPressRect.height()); - } - else if(mPressLocation == right) - { - setRect(mPressRect.x(), mPressRect.y(), mPressRect.width() + diff.x(), mPressRect.height()); - } - else if(mPressLocation == top) - { - setRect(mPressRect.x(), mPressRect.y() + diff.y(), mPressRect.width(), mPressRect.height() - diff.y()); - } - else if(mPressLocation == bottom) - { - setRect(mPressRect.x(), mPressRect.y(), mPressRect.width(), mPressRect.height() + diff.y()); - } - else // entspricht: if (mPressLocation == inside) + + switch(mPressLocation) { - setRect(mPressRect.x() + diff.x(), mPressRect.y() + diff.y(), mPressRect.width(), mPressRect.height()); + case PressLocation::topLeft: + setRect( + mPressRect.x() + diff.x(), + mPressRect.y() + diff.y(), + mPressRect.width() - diff.x(), + mPressRect.height() - diff.y()); + break; + case PressLocation::topRight: + setRect( + mPressRect.x(), + mPressRect.y() + diff.y(), + mPressRect.width() + diff.x(), + mPressRect.height() - diff.y()); + break; + case PressLocation::bottomLeft: + setRect( + mPressRect.x() + diff.x(), + mPressRect.y(), + mPressRect.width() - diff.x(), + mPressRect.height() + diff.y()); + break; + case PressLocation::bottomRight: + setRect(mPressRect.x(), mPressRect.y(), mPressRect.width() + diff.x(), mPressRect.height() + diff.y()); + break; + case PressLocation::left: + setRect(mPressRect.x() + diff.x(), mPressRect.y(), mPressRect.width() - diff.x(), mPressRect.height()); + break; + case PressLocation::right: + setRect(mPressRect.x(), mPressRect.y(), mPressRect.width() + diff.x(), mPressRect.height()); + break; + case PressLocation::top: + setRect(mPressRect.x(), mPressRect.y() + diff.y(), mPressRect.width(), mPressRect.height() - diff.y()); + break; + case PressLocation::bottom: + setRect(mPressRect.x(), mPressRect.y(), mPressRect.width(), mPressRect.height() + diff.y()); + break; + case PressLocation::inside: + setRect(mPressRect.x() + diff.x(), mPressRect.y() + diff.y(), mPressRect.width(), mPressRect.height()); + break; + // do not use QGraphicsRectItem::mouseMoveEvent(event) as it also moves the coordinate system } - // nicht, da sonst koordinatensystem verschoben wird: QGraphicsRectItem::mouseMoveEvent(event); // drag } - else // drag mach ich selber + else { QGraphicsRectItem::mouseMoveEvent(event); } } -// event, of moving mouse void RoiItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) { QPointF pos = event->scenePos(); pos.setX(pos.x() + mMainWindow->getImageBorderSize()); pos.setY(pos.y() + mMainWindow->getImageBorderSize()); - // abfrage auf width() ..., da durch rectLinie die recoBox etwas groesser ist als das Bild und - // es bei mMainWindow->setMousePosOnImage(pos); zum fehler beim bildzugriff kommen kann!!! - if(mMainWindow->getImage() && //(pos.x() > 0) && (pos.y() > 0) && - (pos.x() < mMainWindow->getImage()->width()) && (pos.y() < mMainWindow->getImage()->height())) + + // due to the line width of the QRect, the QRect might get slightly larger than the image. To avoid errors then + // accessing the image mMainWindow->setMousePosOnImage(pos) also width() is checked here + if((mMainWindow->getImage() != nullptr) && (pos.x() < mMainWindow->getImage()->width()) && + (pos.y() < mMainWindow->getImage()->height())) { mMainWindow->setMousePosOnImage(pos); @@ -287,10 +278,10 @@ void RoiItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) setCursor(Qt::OpenHandCursor); } } - else // wird nur einmal durchaufen - ruecksetzen in control.cpp + else // will only be executed once - reset in control.cpp { - setAcceptHoverEvents(false); // verhoindert nicht, dass wenn objekt darunter liegt, was andereen cursor - // haette - cursor wird weiterhin beim drueberfahren auf cross gesetzt + // if an object underneath would have a different cursor, the cursor will still be set to cross + setAcceptHoverEvents(false); setCursor(Qt::CrossCursor); } } @@ -298,8 +289,10 @@ void RoiItem::hoverMoveEvent(QGraphicsSceneHoverEvent *event) QGraphicsRectItem::hoverMoveEvent(event); } -// check rect because bordersize changes and without mouse event nothing changes the rect -void RoiItem::checkRect() +/** + * @brief Restores the size of the ROI if the image sized changed + */ +void RoiItem::restoreSize() { cv::Mat img = mMainWindow->getImageFiltered();