#include <qapplication.h>
#include <qevent.h>
#include <qwhatsthis.h>
#include <qpainter.h>
#include <qwt_plot.h>
#include <qwt_symbol.h>
#include <qwt_scale_map.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_marker.h>
#include "canvaspicker.h"
#include <iostream>
#include "point.h"
CanvasPicker
::CanvasPicker(QwtPlot *plot
): d_selectedMarker(NULL),
d_selectedPoint(-1)
{
canvas->installEventFilter(this);
// We want the focus, but no focus rect. The
// selected point will be highlighted instead.
#if QT_VERSION >= 0x040000
canvas->setFocusPolicy(Qt::StrongFocus);
#ifndef QT_NO_CURSOR
canvas->setCursor(Qt::PointingHandCursor);
#endif
#else
canvas
->setFocusPolicy
(QWidget::StrongFocus);
#ifndef QT_NO_CURSOR
canvas->setCursor(Qt::pointingHandCursor);
#endif
#endif
canvas->setFocus();
shiftCurveCursor(true);
}
bool CanvasPicker
::event(QEvent *e
) {
if ( e
->type
() == QEvent::User ) {
showCursor(true);
return true;
}
}
{
std::cout << "in CanvasPicker::eventFilter \n" ;
if ( object
!= (QObject *)plot
()->canvas
() ) return false;
switch(e->type())
{
showCursor(true);
showCursor(false);
{
break;
}
case QEvent::MouseButtonPress: {
return true;
}
{
return true;
}
default:
break;
}
return QObject::eventFilter(object, e
);
}
// Select the point at a position. If there is no point
// deselect the selected point
void CanvasPicker
::select(const QPoint &pos
) {
std::cout << "in CanvasPicker::select \n" ;
double dist = 1;
int index = -1;
const QwtPlotItemList& itmList = plot()->itemList();
for ( QwtPlotItemIterator it = itmList.begin();
it != itmList.end(); ++it )
{
std::cout << "(*it) "<< (*it) << std::endl;
{
showCursor(true);
double x_t = plot()->invTransform(mark->xAxis(), pos.x());
double y_t = plot()->invTransform(mark->yAxis(), pos.y());
std::cout << "x_t: " <<x_t<< "y_t: "<< y_t << std::endl;
double d = pow(x_t-mark->xValue(),2) + pow(y_t-mark->yValue(),2);
//int idx = mark->closestPoint(pos, &d);
if ( d < dist )
{
marker = mark;
// index = idx;
dist = d;
}
}
}
showCursor(false);
d_selectedMarker = NULL;
d_selectedPoint = -1;
if ( marker && dist < 0.1 ) // 10 pixels tolerance
{
d_selectedMarker = marker;
std::cout << "d_selectedMarker: " << d_selectedMarker << std::endl;
// d_selectedPoint = index;
showCursor(true);
}
}
// Move the selected point
void CanvasPicker::moveBy(int dx, int dy)
{
std::cout << "in CanvasPicker::moveBy \n" ;
if ( dx == 0 && dy == 0 )
return;
if ( !d_selectedMarker )
return;
const double x = d_selectedMarker->xValue() + dx;
const double y = d_selectedMarker->yValue() + dy;
}
// Move the selected point
void CanvasPicker
::move(const QPoint &pos
) {
std::cout << "in CanvasPicker::move \n" ;
if ( !d_selectedMarker )
return;
double xData = plot()->invTransform(d_selectedMarker->xAxis(), pos.x());;
double yData = plot()->invTransform(d_selectedMarker->yAxis(), pos.y());;
std::cout << "xData: " << xData <<"yData: " << yData<< std::endl;
d_selectedMarker->setXValue(xData);
d_selectedMarker->setYValue(yData);
std::cout << "d_selectedMarker->xValue(): " << d_selectedMarker->xValue() <<"d_selectedMarker->yValue(): " << d_selectedMarker->yValue()<< std::endl;
PointItem *item = new PointItem(QwtDoubleRect(0.5, 0.5 , 0.0001, 0.0001));
item->attach(plot());
plot()->replot();
showCursor(true);
}
// Hightlight the selected point
void CanvasPicker::showCursor(bool showIt)
{
std::cout << "in CanvasPicker::showCursor \n" ;
if ( !d_selectedMarker )
return;
const QwtSymbol symbol
= d_selectedMarker
->symbol
();
if ( showIt )
newSymbol.setBrush(symbol.brush().color().dark(150));
const bool doReplot = plot()->autoReplot();
plot()->setAutoReplot(false);
d_selectedMarker->setSymbol(newSymbol);
//d_selectedMarker->draw(painter,const xMap,const yMap,const canvasRect) );
d_selectedMarker->setSymbol(symbol);
plot()->setAutoReplot(doReplot);
}
// Select the next/previous curve
void CanvasPicker::shiftCurveCursor(bool up)
{
std::cout << "in CanvasPicker::shiftPointCursor \n" ;
QwtPlotItemIterator it;
const QwtPlotItemList &itemList = plot()->itemList();
QwtPlotItemList markerList;
for ( it = itemList.begin(); it != itemList.end(); ++it )
{
markerList += *it;
}
if ( markerList.isEmpty() )
return;
it = markerList.begin();
if ( d_selectedMarker )
{
for ( it = markerList.begin(); it != markerList.end(); ++it )
{
if ( d_selectedMarker == *it )
break;
}
if ( it == markerList.end() ) // not found
it = markerList.begin();
if ( up )
{
++it;
if ( it == markerList.end() )
it = markerList.begin();
}
else
{
if ( it == markerList.begin() )
it = markerList.end();
--it;
}
}
showCursor(false);
d_selectedPoint = 0;
showCursor(true);
}
// Select the next/previous neighbour of the selected point
void CanvasPicker::shiftPointCursor(bool up)
{
if ( !d_selectedMarker )
return;
std::cout << "in CanvasPicker::shiftPointCursor \n" ;
}
Bookmarks