// create QwtPlotPicker to allow user moving the points on the plot
this->canvas());
// connect the signals coming from QwtPlotPicker with the member functions
connect( picker, SIGNAL(moved(const QwtDoublePoint& ) ),
this, SLOT(cursorMoved( const QwtDoublePoint& ) ) );
connect( picker, SIGNAL(selected (const QwtDoubleRect &) ),
this, SLOT(RectangleSelected( const QwtDoubleRect& ) ) );
// create QwtPlotPicker to allow user moving the points on the plot
picker = new QwtPlotPicker(QwtPlot::xBottom, QwtPlot::yLeft,
QwtPicker::RectSelection | QwtPicker::DragSelection,
QwtPlotPicker::RectRubberBand, QwtPicker::AlwaysOn,
this->canvas());
// connect the signals coming from QwtPlotPicker with the member functions
connect( picker, SIGNAL(moved(const QwtDoublePoint& ) ),
this, SLOT(cursorMoved( const QwtDoublePoint& ) ) );
connect( picker, SIGNAL(selected (const QwtDoubleRect &) ),
this, SLOT(RectangleSelected( const QwtDoubleRect& ) ) );
To copy to clipboard, switch view to plain text mode
// this function provides the ability to select several items on the plot by dragging a rectangle around them
void Plot::RectangleSelected( const QwtDoubleRect& rect){
std::cout << "in Plot::RectangleSelected" << std::endl;
//============= clear the list with selected items, before start new selection
if(!selected_Points.empty()){
while(!selected_Points.empty()){
newSymbol.setBrush(symbol.brush().color().light(150)); // turn back the highlighten symbol
tempMark->setSymbol(newSymbol);
selected_Points.pop_back(); // delete the selected items from the list
}
}
//============= end of clear the list
const QwtPlotItemList& List = itemList(); // get all items from the plot
QwtPlotItemIterator Iter = List.begin();
// get the two vertice of the selection rectangle
QwtDoublePoint tl = rect.topLeft();
QwtDoublePoint br = rect.bottomRight();
std::cout << "recOfClick(x,y,width, height) =" << rect.x()<<", " << rect.y() << ", " << rect.width()<< ", "<<rect.height()<< std::endl;
double TRtlX = this->transform(picker->xAxis(),tl.x()); // transform the rectangles coordinates to plot's coordinates
double TRtlY = this->transform(picker->yAxis(),tl.y());
double TRbrX = this->transform(picker->xAxis(),br.x());
double TRbrY= this->transform(picker->yAxis(),br.y());
QwtDoubleRect
*trRect
= new QwtDoubleRect
(QRect(QPoint(TRtlX,TRtlY
),
QPoint(TRbrX,TRbrY
)));
// build a rectangle with the new coordinates std::cout << "recOfClick(x,y,width, height) =" << trRect->x()<<", " << trRect->y() << ", " << trRect->width()<< ", "<<trRect->height()<< std::endl;
if( trRect->width()<5 && trRect->height()<5){ // if it was just a click and not drugged rectangle, then...
trRect->setHeight(20); // ...generate a rectangle around the click's position
trRect->setWidth(20);
}
while(Iter != List.end()) // and iterate through them...
{
if((*Iter
)->rtti
() == QwtPlotItem::Rtti_PlotMarker){ // ... until you find the item from class QwtPLotMarker double x = this->transform(mark->xAxis(),mark->xValue()); // transform the item's coordinates to plot's coordinates
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); // create a point consisting of transformed coordinates
if(trRect->contains(trPoint)){ // if click's rectangle includes the QwtPlotMarker, then...
std::cout << "MATCH" << std::endl;
//selectedPoint = mark; // ... save the pointer to this item into variable "selectedPoint"
std::cout << "selectedPoint = " << mark << std::endl;
newSymbol.setBrush(symbol.brush().color().dark(150)); // highlighten the selected item by darken its color
mark->setSymbol(newSymbol);
selected_Points.push_back(mark);
}
}
Iter++;
}
replot();
std::cout << "selected_Points.size()= " << selected_Points.size() << std::endl;
}
//##################################################################
// this function provides the ability to move items on the plot
void Plot::cursorMoved( const QwtDoublePoint& Point){
std::cout << "in Plot::cursorMoved " << std::endl;
std::cout << "Point.x()= " << Point.x() <<"Point.y()= " << Point.y() << std::endl;
if(!selected_Points.empty()){ //if the list with selected items is not empty, then...
for(unsigned i=0; i< selected_Points.size(); ++i) {
std::cout << "loop " << i << std::endl;
selected_Points[i]->setValue(Point); // set the new coordinates to the moved item
}
this->replot(); // replot the plot widget after the selected point was moved
}
replot();
}
// this function provides the ability to select several items on the plot by dragging a rectangle around them
void Plot::RectangleSelected( const QwtDoubleRect& rect){
std::cout << "in Plot::RectangleSelected" << std::endl;
//============= clear the list with selected items, before start new selection
if(!selected_Points.empty()){
while(!selected_Points.empty()){
QwtPlotMarker *tempMark = selected_Points.back();
const QwtSymbol symbol = tempMark->symbol();
QwtSymbol newSymbol = symbol;
newSymbol.setBrush(symbol.brush().color().light(150)); // turn back the highlighten symbol
tempMark->setSymbol(newSymbol);
selected_Points.pop_back(); // delete the selected items from the list
}
}
//============= end of clear the list
const QwtPlotItemList& List = itemList(); // get all items from the plot
QwtPlotItemIterator Iter = List.begin();
// get the two vertice of the selection rectangle
QwtDoublePoint tl = rect.topLeft();
QwtDoublePoint br = rect.bottomRight();
std::cout << "recOfClick(x,y,width, height) =" << rect.x()<<", " << rect.y() << ", " << rect.width()<< ", "<<rect.height()<< std::endl;
double TRtlX = this->transform(picker->xAxis(),tl.x()); // transform the rectangles coordinates to plot's coordinates
double TRtlY = this->transform(picker->yAxis(),tl.y());
double TRbrX = this->transform(picker->xAxis(),br.x());
double TRbrY= this->transform(picker->yAxis(),br.y());
QwtDoubleRect *trRect= new QwtDoubleRect(QRect(QPoint(TRtlX,TRtlY),QPoint(TRbrX,TRbrY))); // build a rectangle with the new coordinates
std::cout << "recOfClick(x,y,width, height) =" << trRect->x()<<", " << trRect->y() << ", " << trRect->width()<< ", "<<trRect->height()<< std::endl;
if( trRect->width()<5 && trRect->height()<5){ // if it was just a click and not drugged rectangle, then...
trRect->setHeight(20); // ...generate a rectangle around the click's position
trRect->setWidth(20);
}
while(Iter != List.end()) // and iterate through them...
{
if((*Iter)->rtti() == QwtPlotItem::Rtti_PlotMarker){ // ... until you find the item from class QwtPLotMarker
QwtPlotMarker *mark = ((QwtPlotMarker*)(*Iter));
double x = this->transform(mark->xAxis(),mark->xValue()); // transform the item's coordinates to plot's coordinates
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); // create a point consisting of transformed coordinates
if(trRect->contains(trPoint)){ // if click's rectangle includes the QwtPlotMarker, then...
std::cout << "MATCH" << std::endl;
//selectedPoint = mark; // ... save the pointer to this item into variable "selectedPoint"
std::cout << "selectedPoint = " << mark << std::endl;
const QwtSymbol symbol = mark->symbol();
QwtSymbol newSymbol = symbol;
newSymbol.setBrush(symbol.brush().color().dark(150)); // highlighten the selected item by darken its color
mark->setSymbol(newSymbol);
selected_Points.push_back(mark);
}
}
Iter++;
}
replot();
std::cout << "selected_Points.size()= " << selected_Points.size() << std::endl;
}
//##################################################################
// this function provides the ability to move items on the plot
void Plot::cursorMoved( const QwtDoublePoint& Point){
std::cout << "in Plot::cursorMoved " << std::endl;
std::cout << "Point.x()= " << Point.x() <<"Point.y()= " << Point.y() << std::endl;
if(!selected_Points.empty()){ //if the list with selected items is not empty, then...
for(unsigned i=0; i< selected_Points.size(); ++i) {
std::cout << "loop " << i << std::endl;
selected_Points[i]->setValue(Point); // set the new coordinates to the moved item
}
this->replot(); // replot the plot widget after the selected point was moved
}
replot();
}
To copy to clipboard, switch view to plain text mode
Bookmarks