PolarMouseTrackerTool
::PolarMouseTrackerTool(QwtPolarCanvas
*canv
) : QObject(canv
){
isActive = true;
canvas = canv;
canvas->setMouseTracking(true);
canvas->installEventFilter(this);
}
PolarMouseTrackerTool::PolarMouseTrackerTool(QwtPolarCanvas *canv) : QObject(canv)
{
isActive = true;
canvas = canv;
canvas->setMouseTracking(true);
canvas->installEventFilter(this);
}
To copy to clipboard, switch view to plain text mode
bool PolarMouseTrackerTool
::eventFilter(QObject *obj,
QEvent *event
) {
if ((event
->type
() != QEvent::MouseMove && event
->type
() != QEvent::MouseButtonRelease && event
->type
() != QEvent::MouseButtonDblClick) ||
!isActive
) {
return QObject::eventFilter(obj, event
);
}
QMouseEvent *mouseEvent
= dynamic_cast<QMouseEvent
*>
(event
);
if (mouseEvent == NULL)
return false;
QPoint mousePos
= mouseEvent
->pos
();
QwtPolarPlot *polarPlot = canvas->plot();
if (polarPlot == NULL)
return false;
QwtScaleMap radiusMap
= polarPlot
->scaleMap
(QwtPolar
::ScaleRadius);
double sDist = radiusMap.s2() - radiusMap.s1();
double dDist = radiusMap.p2() - radiusMap.p1();
//The pole center (in pixel)
QPoint pole
= polarPlot
->plotRect
().
center().
toPoint();
//translate
translatePosWithCanvasCenter(pole, mousePos);
//Pitagora
double a = qAbs(qAtan(mousePos.y() / (mousePos.x() * 1.0)));
double r = qSqrt((qPow(mousePos.x(), 2)) + qPow(mousePos.y(), 2));
translateAzimuthWithCanvasCenter(mousePos, a);
//radix mapping
r = (r * sDist) / dDist;
if (event
->type
() == QEvent::MouseMove) emit signalMoved(a, r);
if (event
->type
() == QEvent::MouseButtonDblClick) {
emit signalDblClicked(a, r, mouseEvent->button());
emit signalDblClicked(mouseEvent->pos(), mouseEvent->button());
}
if (event
->type
() == QEvent::MouseButtonRelease) emit signalClicked(a, r, mouseEvent->button());
return true;
}
void PolarMouseTrackerTool
::translatePosWithCanvasCenter(const QPoint &canvasCenter,
QPoint &pos
) {
pos.setX(pos.x() - canvasCenter.x());
pos.setY(canvasCenter.y() - pos.y());
}
void PolarMouseTrackerTool
::translateAzimuthWithCanvasCenter(const QPoint &pos,
double &azimuth
) {
if (pos.x() >= 0 && pos.y() >= 0) //Quadrant 1
{
//Nothing to do
}
else if (pos.x() < 0 && pos.y() > 0) //Quadrant 4
{
azimuth = (M_PI / 2.0) + ((M_PI / 2.0) - azimuth);
}
else if (pos.x() <= 0 && pos.y() <= 0) //Quadrant 3
{
azimuth = M_PI + azimuth;
}
else //Quadrant 2
{
azimuth = (M_PI * (3.0 / 2.0)) + ((M_PI / 2.0) - azimuth);
}
}
bool PolarMouseTrackerTool::eventFilter(QObject *obj, QEvent *event)
{
if ((event->type() != QEvent::MouseMove && event->type() != QEvent::MouseButtonRelease && event->type() != QEvent::MouseButtonDblClick) || !isActive)
{
return QObject::eventFilter(obj, event);
}
QMouseEvent *mouseEvent = dynamic_cast<QMouseEvent*>(event);
if (mouseEvent == NULL)
return false;
QPoint mousePos = mouseEvent->pos();
QwtPolarPlot *polarPlot = canvas->plot();
if (polarPlot == NULL)
return false;
QwtScaleMap radiusMap = polarPlot->scaleMap(QwtPolar::ScaleRadius);
double sDist = radiusMap.s2() - radiusMap.s1();
double dDist = radiusMap.p2() - radiusMap.p1();
//The pole center (in pixel)
QPoint pole = polarPlot->plotRect().center().toPoint();
//translate
translatePosWithCanvasCenter(pole, mousePos);
//Pitagora
double a = qAbs(qAtan(mousePos.y() / (mousePos.x() * 1.0)));
double r = qSqrt((qPow(mousePos.x(), 2)) + qPow(mousePos.y(), 2));
translateAzimuthWithCanvasCenter(mousePos, a);
//radix mapping
r = (r * sDist) / dDist;
if (event->type() == QEvent::MouseMove)
emit signalMoved(a, r);
if (event->type() == QEvent::MouseButtonDblClick)
{
emit signalDblClicked(a, r, mouseEvent->button());
emit signalDblClicked(mouseEvent->pos(), mouseEvent->button());
}
if (event->type() == QEvent::MouseButtonRelease)
emit signalClicked(a, r, mouseEvent->button());
return true;
}
void PolarMouseTrackerTool::translatePosWithCanvasCenter(const QPoint &canvasCenter, QPoint &pos)
{
pos.setX(pos.x() - canvasCenter.x());
pos.setY(canvasCenter.y() - pos.y());
}
void PolarMouseTrackerTool::translateAzimuthWithCanvasCenter(const QPoint &pos, double &azimuth)
{
if (pos.x() >= 0 && pos.y() >= 0) //Quadrant 1
{
//Nothing to do
}
else if (pos.x() < 0 && pos.y() > 0) //Quadrant 4
{
azimuth = (M_PI / 2.0) + ((M_PI / 2.0) - azimuth);
}
else if (pos.x() <= 0 && pos.y() <= 0) //Quadrant 3
{
azimuth = M_PI + azimuth;
}
else //Quadrant 2
{
azimuth = (M_PI * (3.0 / 2.0)) + ((M_PI / 2.0) - azimuth);
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks