...
// create QwtPlotPicker to allow user moving the points on the plot
this->canvas());
connect( picker, SIGNAL(moved(const QwtDoublePoint& ) ),
this, SLOT(cursorMoved( const QwtDoublePoint& ) ) );
connect( picker, SIGNAL(selected( const QwtDoublePoint& ) ),
this, SLOT(cursorSelected( const QwtDoublePoint& ) ) );
...
void Plot::cursorSelected(const QwtDoublePoint &Point)
{
//selectedPoint = NULL;
std::cout << "in Plot::cursorSelected, selectedPoint = " << selectedPoint << std::endl;
const QwtPlotItemList& List = itemList();
QwtPlotItemIterator Iter = List.begin();
while(Iter != List.end())
{
double x = this->transform(mark->xAxis(),mark->xValue());
double y = this->transform(mark->yAxis(),mark->yValue());
std::cout << "mark.xValue()=" << mark->xValue() << ", mark.yValue()=" << mark->yValue()<< std::endl;
std::cout << "transformed mark coord = " << x << ", " << y<< std::endl;
QwtDoublePoint trPoint = QwtDoublePoint(x,y);
double xClick = this->transform(mark->xAxis(),Point.x());
double yClick = this->transform(mark->yAxis(),Point.y());
QwtDoubleRect recOfClick(xClick-10, yClick-10, 20,20);
std::cout << "recOfClick(x,y,width, height) =" <<recOfClick.x()<<", " << recOfClick.y() << ", " << recOfClick.width()<< ", "<<recOfClick.height()<< std::endl;
if(recOfClick.contains(trPoint)){
std::cout << "MATCH" << std::endl;
selectedPoint = mark;
std::cout << "selectedPoint = " << selectedPoint << std::endl;
}
/* else{
selectedPoint = NULL; // at the beginning this pointer should be NULL
}*/
}
Iter++;
}
}
//##################################################################
void Plot::cursorMoved( const QwtDoublePoint& Point){
std::cout << "in Plot::cursorMoved, selectedPoint = " << selectedPoint << std::endl;
std::cout << "Point.x()= " << Point.x() <<"Point.y()= " << Point.y() << std::endl;
if(selectedPoint!=NULL){
selectedPoint->setValue(Point); // set the new coordinates to the moved item
//std::cout << "selectedPoint->xValue(): " << selectedPoint->xValue() <<"selectedPoint->yValue(): " << selectedPoint->yValue()<< std::endl;
this->replot(); // replot the plot widget after the selected point was moved
}
}
...
// create QwtPlotPicker to allow user moving the points on the plot
picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
QwtPicker::PointSelection | QwtPicker::DragSelection,
QwtPlotPicker::CrossRubberBand, QwtPicker::AlwaysOn,
this->canvas());
connect( picker, SIGNAL(moved(const QwtDoublePoint& ) ),
this, SLOT(cursorMoved( const QwtDoublePoint& ) ) );
connect( picker, SIGNAL(selected( const QwtDoublePoint& ) ),
this, SLOT(cursorSelected( const QwtDoublePoint& ) ) );
...
void Plot::cursorSelected(const QwtDoublePoint &Point)
{
//selectedPoint = NULL;
std::cout << "in Plot::cursorSelected, selectedPoint = " << selectedPoint << std::endl;
const QwtPlotItemList& List = itemList();
QwtPlotItemIterator Iter = List.begin();
while(Iter != List.end())
{
if((*Iter)->rtti() == QwtPlotItem::Rtti_PlotMarker){
QwtPlotMarker *mark = ((QwtPlotMarker*)(*Iter));
double x = this->transform(mark->xAxis(),mark->xValue());
double y = this->transform(mark->yAxis(),mark->yValue());
std::cout << "mark.xValue()=" << mark->xValue() << ", mark.yValue()=" << mark->yValue()<< std::endl;
std::cout << "transformed mark coord = " << x << ", " << y<< std::endl;
QwtDoublePoint trPoint = QwtDoublePoint(x,y);
double xClick = this->transform(mark->xAxis(),Point.x());
double yClick = this->transform(mark->yAxis(),Point.y());
QwtDoubleRect recOfClick(xClick-10, yClick-10, 20,20);
std::cout << "recOfClick(x,y,width, height) =" <<recOfClick.x()<<", " << recOfClick.y() << ", " << recOfClick.width()<< ", "<<recOfClick.height()<< std::endl;
if(recOfClick.contains(trPoint)){
std::cout << "MATCH" << std::endl;
selectedPoint = mark;
std::cout << "selectedPoint = " << selectedPoint << std::endl;
}
/* else{
selectedPoint = NULL; // at the beginning this pointer should be NULL
}*/
}
Iter++;
}
}
//##################################################################
void Plot::cursorMoved( const QwtDoublePoint& Point){
std::cout << "in Plot::cursorMoved, selectedPoint = " << selectedPoint << std::endl;
std::cout << "Point.x()= " << Point.x() <<"Point.y()= " << Point.y() << std::endl;
if(selectedPoint!=NULL){
selectedPoint->setValue(Point); // set the new coordinates to the moved item
//std::cout << "selectedPoint->xValue(): " << selectedPoint->xValue() <<"selectedPoint->yValue(): " << selectedPoint->yValue()<< std::endl;
this->replot(); // replot the plot widget after the selected point was moved
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks