Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Moved boundaries check to ResizeHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
MPIBR-kretschmerf committed Apr 26, 2017
1 parent 4bd5c7f commit e5d19a0
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 42 deletions.
28 changes: 16 additions & 12 deletions ResizeHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,42 +40,46 @@ void ResizeHelper::paint(QPainter * p, const QStyleOptionGraphicsItem *, QWidget

void ResizeHelper::mousePressEvent(QGraphicsSceneMouseEvent * ev) {
m_edges = edgesAt(ev->pos(), m_rect, m_pen.widthF());
if (!m_edges){ /*Moving: Store click location
if (!m_edges){ /*Moving: Store click location*/
//ev->ignore(); /*Ignore if moved*/
clickPos = ev->pos();
}
ev->accept();
}

void ResizeHelper::mouseMoveEvent(QGraphicsSceneMouseEvent * ev) {

auto pos = mapToItem(parentItem(), ev->pos());
auto rect = traits.rectFor(parentItem());

if (m_edges & Qt::LeftEdge) rect.setLeft(pos.x());
if (m_edges & Qt::TopEdge) rect.setTop(pos.y());
if (m_edges & Qt::RightEdge) rect.setRight(pos.x());
if (m_edges & Qt::BottomEdge) rect.setBottom(pos.y());
if (!!m_edges) {
traits.setRectOn(parentItem(), rect);
newGeometry();
}else{ /*Moving*/
QPointF d = clickPos - ev->pos();
/*Edge was clicked -> Resize*/
if (m_edges & Qt::LeftEdge) rect.setLeft(pos.x());
if (m_edges & Qt::TopEdge) rect.setTop(pos.y());
if (m_edges & Qt::RightEdge) rect.setRight(pos.x());
if (m_edges & Qt::BottomEdge) rect.setBottom(pos.y());
}else{ /*No Edge clicked -> Moving*/
QPointF d = clickPos - pos;
rect.setLeft(rect.left()-d.x());
rect.setTop(rect.top()-d.y());
rect.setRight(rect.right()-d.x());
rect.setBottom(rect.bottom()-d.y());
clickPos = pos;
}

/*Make sure that rectangle is within predefined boundaries (if given)*/
if ((!boundaries.isEmpty() & (boundaries.contains(rect))) | boundaries.isEmpty()){
traits.setRectOn(parentItem(), rect);
newGeometry();
clickPos = ev->pos();
emit(itemChanged(parentItem()));
}
}

void ResizeHelper::newGeometry() {
/*Propagate geometry change to bounding rectangle*/
prepareGeometryChange();
auto parentRect = traits.rectFor(parentItem());
m_rect.setTopLeft(mapFromParent(parentRect.topLeft()));
m_rect.setBottomRight(mapFromParent(parentRect.bottomRight()));
m_pen.setWidthF(std::min(m_rect.width(), m_rect.height()) * 0.1);
m_pen.setJoinStyle(Qt::MiterJoin);
emit(itemChanged(parentItem()));
}
1 change: 1 addition & 0 deletions ResizeHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ResizeHelper : public QGraphicsObject
public:
ResizeTraits traits;
QRectF m_rect;
QRectF boundaries;
QPen m_pen;
QPointF clickPos;
QFlags<Qt::Edge> m_edges;
Expand Down
32 changes: 2 additions & 30 deletions TrackerPlugin_EyeTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ void TrackerPlugin_EyeTracker::initializeUI(QLayout *layout, QGraphicsScene *cam
this->cameraScene = cameraScene;

/*Make objects resizeable and moveable*/
resizeHelper->boundaries = QRectF(0,0,cameraResolution.width(),cameraResolution.height());
cameraScene->addItem(resizeHelper);
connect(cameraScene, SIGNAL(selectionChanged()), resizeHelper, SLOT(selectionChanged()));

Expand Down Expand Up @@ -231,39 +232,10 @@ void TrackerPlugin_EyeTracker::onItemChanged(QGraphicsItem *item)
/*If the resized object is a ROI rectangle created by this plugin*/
/*Send message*/
if(rectIdx >= 0){

/*Make sure that roi is within camera window*/
QRectF rect = rectItems.at(rectIdx)->rect();
if(rect.x() < 0){
rect.setRight(rect.right() - rect.x());
rect.setLeft(0);
}else if (rect.right() >= cameraResolution.width()){
rect.setLeft(rect.left() + (cameraResolution.width() - rect.right()));
rect.setRight(cameraResolution.width());
}
if(rect.y() < 0){
rect.setBottom(rect.bottom() - rect.y());
rect.setTop(0);
}else if (rect.bottom() >= cameraResolution.height()){
rect.setTop(rect.top() + (cameraResolution.height() - rect.bottom()));
rect.setBottom(cameraResolution.height());
}
if(rect.width()> cameraResolution.width()){
rect.setLeft(0);
rect.setRight(cameraResolution.width());
}
if(rect.height()>cameraResolution.height()){
rect.setTop(0);
rect.setBottom(cameraResolution.height());
}

rectItems.at(rectIdx)->setRect(rect);

emit(roiChanged(rectIdx, rect.toRect()));
emit(roiChanged(rectIdx, rectItems.at(rectIdx)->rect().toRect()));
}
}


void TrackerPlugin_EyeTracker::on_lineEdit_editingFinished()
{
wndLength = ui->lineEdit->text().toDouble();
Expand Down

0 comments on commit e5d19a0

Please sign in to comment.