Skip to content

Commit

Permalink
initial event filter map for #25, also more efficient point mapping f…
Browse files Browse the repository at this point in the history
…or cursor tracking in image implemented in image widget. to be transferred to event filter for #25 later.
  • Loading branch information
mattrussmill committed Sep 8, 2019
1 parent 0f49e03 commit 1381a7b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 57 deletions.
82 changes: 28 additions & 54 deletions imagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ void ImageWidget::mouseReleaseEvent(QMouseEvent *event)
{
if(retrieveCoordinateMode_m == RectROI)
{
region_m.setBottomRight(getPointInImage());
region_m.setBottomRight(getPointInImage(event));
region_m = getAdjustedRegion();
selectRegionOnPixmap();
emit imageRectRegionSelected(region_m);
Expand All @@ -399,7 +399,7 @@ void ImageWidget::mouseReleaseEvent(QMouseEvent *event)
}
else
{
emit imagePointSelected(getPointInImage());
emit imagePointSelected(getPointInImage(event));
}
}
}
Expand All @@ -419,7 +419,7 @@ void ImageWidget::mousePressEvent(QMouseEvent *event)
{
if(retrieveCoordinateMode_m == RectROI || retrieveCoordinateMode_m == DragROI)
{
dragStart_m = getPointInImage();
dragStart_m = getPointInImage(event);

//if point not within region_m, select and draw the ROI in RectROI mode.
//else keep the starting point and move to DragROI mode shifting region_m
Expand All @@ -440,7 +440,7 @@ void ImageWidget::mousePressEvent(QMouseEvent *event)
}
else
{
emit imagePointSelected(getPointInImage());
emit imagePointSelected(getPointInImage(event));
}
}
}
Expand All @@ -460,13 +460,13 @@ void ImageWidget::mouseMoveEvent(QMouseEvent *event)
{
if(retrieveCoordinateMode_m == RectROI)
{
region_m.setBottomRight(getPointInImage());
region_m.setBottomRight(getPointInImage(event));
selectRegionOnPixmap();
}
//this state's boundaries are checked in mouse press event, cant enter directly
else if(retrieveCoordinateMode_m == DragROI)
{
QPoint endPoint = getPointInImage();
QPoint endPoint = getPointInImage(event);
QPoint dragDistance = dragStart_m - endPoint;
dragStart_m = endPoint;
region_m.setTopLeft(region_m.topLeft() - dragDistance);
Expand All @@ -475,7 +475,7 @@ void ImageWidget::mouseMoveEvent(QMouseEvent *event)
}
else
{
emit imagePointSelected(getPointInImage());
emit imagePointSelected(getPointInImage(event));
}
}
}
Expand Down Expand Up @@ -581,46 +581,20 @@ void ImageWidget::dropEvent(QDropEvent *event)
}
}

/* If imageLabel is the same size or smaller, shift the origin of the ImageWidget to the origin of the
* imageLabel. Else shift the origin based on the scrollBar positions using the image scrollArea as
* the ROI window. Adjusts for scrollBar space as well. QLabel could have been overloaded to avoid this
* hell of compensating for image position within the scroll area.*/
QPoint ImageWidget::getPointInImage()
/* If an image is attached, translates the widget coordinates from the ImageWidget to the
* imageLabel_m and scales the point to the appropriate position based on the image zoom.*/
QPoint ImageWidget::getPointInImage(QMouseEvent *event)
{
if(imageAttached())
{
QPoint mousePosition = mapFromGlobal(QCursor::pos());
QPoint mousePosition = imageLabel_m->mapFromParent(event->pos());

//x coordinate adjustment
float scalar = attachedImage_m->width() / static_cast<float>(imageLabel_m->width());
if(imageLabel_m->width() <= this->width())
{
mousePosition += QPoint((imageLabel_m->width() - scrollArea_m->width()) / 2, 0);
}
else
{
float scrollBarPosition = scrollArea_m->horizontalScrollBar()->value()
/ static_cast<float>(scrollArea_m->horizontalScrollBar()->maximum());

mousePosition += QPoint(scrollBarPosition * (imageLabel_m->width() - scrollArea_m->width()
+ scrollArea_m->verticalScrollBar()->width() + 1), 0);
}
mousePosition.setX(mousePosition.x() * scalar);

//y coordinate adjustment
scalar = attachedImage_m->height() / static_cast<float>(imageLabel_m->height());
if(imageLabel_m->height() <= this->height())
{
mousePosition += QPoint(0, (imageLabel_m->height() - scrollArea_m->height()) / 2);
}
else
{
float scrollBarPosition = scrollArea_m->verticalScrollBar()->value()
/ static_cast<float>(scrollArea_m->verticalScrollBar()->maximum());

mousePosition += QPoint(0, scrollBarPosition * (imageLabel_m->height() - scrollArea_m->height()
+ scrollArea_m->horizontalScrollBar()->height() + 1));
}
mousePosition.setY(mousePosition.y() * scalar);

//qDebug() << mousePosition;
Expand Down Expand Up @@ -714,24 +688,24 @@ QRect ImageWidget::getAdjustedRegion()
return QRect(QPoint(topLeftX, topLeftY), QPoint(bottomRightX, bottomRightY));
}

void ImageWidget::displayBrushOnPixmap()
{
//while waiting for mutex, process main event loop to keep gui responsive
if(mutex_m)
{
while(!mutex_m->tryLock())
QApplication::processEvents(QEventLoop::AllEvents, 100);
}
painterBuffer_m = QPixmap::fromImage(*attachedImage_m);
if(mutex_m) mutex_m->unlock();
//void ImageWidget::displayBrushOnPixmap()
//{
// //while waiting for mutex, process main event loop to keep gui responsive
// if(mutex_m)
// {
// while(!mutex_m->tryLock())
// QApplication::processEvents(QEventLoop::AllEvents, 100);
// }
// painterBuffer_m = QPixmap::fromImage(*attachedImage_m);
// if(mutex_m) mutex_m->unlock();

QPainter painter(&painterBuffer_m);
painter.setBrush(QColor(50, 50, 50));
painter.setPen(QColor(50, 50, 50));
painter.setCompositionMode(QPainter::CompositionMode_Darken);
// QPainter painter(&painterBuffer_m);
// painter.setBrush(QColor(50, 50, 50));
// painter.setPen(QColor(50, 50, 50));
// painter.setCompositionMode(QPainter::CompositionMode_Darken);

//bound this somehow?
painter.drawEllipse(getPointInImage(), brushRadius_m, brushRadius_m);
// //bound this somehow?
// painter.drawEllipse(getPointInImage(), brushRadius_m, brushRadius_m);

}
//}

2 changes: 1 addition & 1 deletion imagewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public slots:
void displayBrushOnPixmap();
void initializePaintMembers();
void zoomAgain();
QPoint getPointInImage();
QPoint getPointInImage(QMouseEvent *event);
QRect getAdjustedRegion();
QAction *zoomInAction_m;
QAction *zoomOutAction_m;
Expand Down
9 changes: 7 additions & 2 deletions mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,6 @@
</property>
<addaction name="actionAbout"/>
<addaction name="separator"/>
<addaction name="actionOpenCV"/>
<addaction name="actionQt"/>
</widget>
<widget class="QMenu" name="menuTools">
<property name="title">
Expand All @@ -322,6 +320,8 @@
<addaction name="actionZoom_In"/>
<addaction name="actionZoom_Out"/>
<addaction name="actionZoom_Actual"/>
<addaction name="separator"/>
<addaction name="actionTracking"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEdit"/>
Expand Down Expand Up @@ -431,6 +431,11 @@
<string>Transform...</string>
</property>
</action>
<action name="actionTracking">
<property name="text">
<string>Tracking</string>
</property>
</action>
</widget>
<layoutdefault spacing="6" margin="11"/>
<customwidgets>
Expand Down
2 changes: 2 additions & 0 deletions qcvTouchUp.pro
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ SOURCES += \
bufferwrappersqcv.cpp \
filtermenu.cpp \
temperaturemenu.cpp \
transformimagewidgetcropeventfilter.cpp \
transformmenu.cpp \
signalsuppressor.cpp \
focusindetectoreventfilter.cpp \
Expand All @@ -62,6 +63,7 @@ HEADERS += \
bufferwrappersqcv.h \
filtermenu.h \
temperaturemenu.h \
transformimagewidgetcropeventfilter.h \
transformmenu.h \
signalsuppressor.h \
focusindetectoreventfilter.h \
Expand Down
14 changes: 14 additions & 0 deletions transformimagewidgetcropeventfilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "transformimagewidgetcropeventfilter.h"
#include <QObject>
#include <QEvent>

TransformImageWidgetCropEventFilter::TransformImageWidgetCropEventFilter(QObject *parent) : QObject(parent)
{

}

//eventFilter returns true to disable further processing ....
bool TransformImageWidgetCropEventFilter::eventFilter(QObject *watched, QEvent *event)
{

}
39 changes: 39 additions & 0 deletions transformimagewidgetcropeventfilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//started 9/7/19 // install directly to ImageLabel -> Have slot to "installEventFilterToImage()" or something, then have removeEventFilterFromImageLabel() to be called during radio switch context etc.

#ifndef TRANSFORMIMAGEWIDGETCROPEVENTFILTER_H
#define TRANSFORMIMAGEWIDGETCROPEVENTFILTER_H

#include <QObject>
#include <QPoint>
class QLabel;
class QPoint;

class TransformImageWidgetCropEventFilter : public QObject
{
Q_OBJECT
public:
explicit TransformImageWidgetCropEventFilter(QObject *parent = nullptr);

signals:

public slots:

protected:
bool eventFilter(QObject *watched, QEvent *event);

private:
//mouseMoveEvent, mousePressEvent, mouseReleaseEvent, paintEvent
//call redraw after each mouse event
//initializePaintMembers in constructor
//selectRegionOnPixmap

void setPointFromImage(); //getPointInImage from ImageWidget
QPoint pointInImage_m; //store pointInImage here to not reallocate every time
QPoint dragStart_m;
QLabel *ImageWidget;



};

#endif // TRANSFORMIMAGEWIDGETCROPEVENTFILTER_H

0 comments on commit 1381a7b

Please sign in to comment.