// Select the point at a position. void XtalOptPlot::select(const QPoint& pos) { QwtPlotMarker* selection = nullptr; // Must be within 10 pixels at least double dist = 10.0; const QwtPlotItemList& itmList = itemList(); for (QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); ++it) { if ((*it)->rtti() == QwtPlotItem::Rtti_PlotMarker) { QwtPlotMarker* m = static_cast<QwtPlotMarker*>(*it); // We have to map the plot coordinates onto widget coordinates before // comparing const QwtScaleMap& xMap = canvasMap(m->xAxis()); const QwtScaleMap& yMap = canvasMap(m->yAxis()); double mx = xMap.transform(m->xValue()); double my = yMap.transform(m->yValue()); double d = distance(QPointF(mx, my), pos); if (d < dist) { selection = m; dist = d; } } } selectMarker(selection); }
//! Redraw grid, curves, and markers. The draw code // does not clear clipRegion prior to painting. // \param p painter used for drawing void QwtPlot::drawCanvas(QPainter *p) { QwtDiMap map[axisCnt]; for ( int axis = 0; axis < axisCnt; axis++ ) map[axis] = canvasMap(axis); QRect rect = d_canvas->contentsRect(); // // draw grid // if ( d_grid.enabled() && axisEnabled( d_grid.xAxis() ) && axisEnabled( d_grid.yAxis() ) ) { d_grid.draw(p, rect, map[d_grid.xAxis()], map[d_grid.yAxis()]); } // // draw curves // QIntDictIterator<QwtPlotCurve> itc(*d_curves); for (QwtPlotCurve *curve = itc.toFirst(); curve != 0; curve = ++itc ) { if ( curve->enabled() && axisEnabled( curve->xAxis() ) && axisEnabled( curve->yAxis() ) ) { curve->draw(p, map[curve->xAxis()], map[curve->yAxis()]); } } // // draw markers // QIntDictIterator<QwtPlotMarker> itm(*d_markers); for (QwtPlotMarker *marker = itm.toFirst(); marker != 0; marker = ++itm ) { if ( marker->enabled() && axisEnabled( marker->xAxis() ) && axisEnabled( marker->yAxis() ) ) { marker->draw(p, map[marker->xAxis()].transform(marker->xValue()), map[marker->yAxis()].transform(marker->yValue()), rect); } } }
void QwtPlot::markerPos(long key, double &mx, double &my ) const { QwtPlotMarker *m = d_markers->find(key); if (m) { mx = m->xValue(); my = m->yValue(); } else { mx = 0; my = 0; } }
bool Annotations::interference( double x, double y, const QwtText& label, Qt::Alignment align ) const { QRectF rc = boundingRect( x, y, label, align ); auto it = std::find_if( vec_.begin(), vec_.end(), [=]( const Annotation& a ){ QwtPlotMarker * marker = a.getPlotMarker(); QRectF rhs = boundingRect( marker->xValue(), marker->yValue(), marker->label(), align ); QRectF lhs = rc.intersected( rhs ); return lhs.width() || lhs.height(); }); if ( it == vec_.end() ) return false; return true; }
void QwtPlot::drawCanvasItems(QPainter *painter, const QRect &rect, const QwtArray<QwtDiMap> &map, const QwtPlotPrintFilter &pfilter) const { // // draw grid // if ( pfilter.options() & QwtPlotPrintFilter::PrintGrid ) { if ( d_grid->enabled() ) { d_grid->draw(painter, rect, map[d_grid->xAxis()], map[d_grid->yAxis()]); } } // // draw curves // QwtPlotCurveIterator itc = curveIterator(); for (QwtPlotCurve *curve = itc.toFirst(); curve != 0; curve = ++itc ) { if ( curve->enabled() ) { curve->draw(painter, map[curve->xAxis()], map[curve->yAxis()]); } } // // draw markers // QwtPlotMarkerIterator itm = markerIterator(); for (QwtPlotMarker *marker = itm.toFirst(); marker != 0; marker = ++itm ) { if ( marker->enabled() ) { marker->draw(painter, map[marker->xAxis()].transform(marker->xValue()), map[marker->yAxis()].transform(marker->yValue()), rect); } } }
/*! \brief Find the marker which is closest to a given point. \param xpos \param ypos coordinates of a point in the plotting region \retval dist Distance in points between the marker and the specified point. \return Key of the closest marker or 0 if no marker was found */ long QwtPlot::closestMarker(int xpos, int ypos, int &dist) const { QwtDiMap map[axisCnt]; for ( int axis = 0; axis < axisCnt; axis++ ) map[axis] = canvasMap(axis); long rv = 0; double dmin = 1.0e10; QwtPlotMarkerIterator itm = markerIterator(); for (QwtPlotMarker *m = itm.toFirst(); m != 0; m = ++itm ) { double cx = map[m->xAxis()].xTransform(m->xValue()); double cy = map[m->yAxis()].xTransform(m->yValue()); if (m->lineStyle() == QwtMarker::HLine) { if (m->symbol().style() == QwtSymbol::None) cx = double(xpos); } else if (m->lineStyle() == QwtMarker::VLine) { if (m->symbol().style() == QwtSymbol::None) cy = double(ypos); } double f = qwtSqr(cx - double(xpos)) + qwtSqr(cy - double(ypos)); if (f < dmin) { dmin = f; rv = itm.currentKey(); } } dist = int(sqrt(dmin)); return rv; }
QRectF Annotations::boundingRect( const Annotation& a, Qt::Alignment align ) const { QwtPlotMarker * marker = a.getPlotMarker(); return boundingRect( marker->xValue(), marker->yValue(), marker->label(), align ); }
// Select the next/previous neighbour of the selected point // 0 = up // 1 = down // 2 = left // 3 = right void XtalOptPlot::shiftMarkerCursor(int direction) { if (!m_selectedMarker) return; QwtPlotMarker* selection = nullptr; double dist = 1e300; const QwtPlotItemList& itmList = itemList(); for (QwtPlotItemIterator it = itmList.begin(); it != itmList.end(); ++it) { if ((*it)->rtti() == QwtPlotItem::Rtti_PlotMarker) { QwtPlotMarker* m = static_cast<QwtPlotMarker*>(*it); // Skip over the point that is already selected if (m == m_selectedMarker) continue; // Find the distance and see if it is the shortest so far // If we ever want to do this with canvas coordinates, this is how // it is done: // double x1 = canvasMap(m->xAxis()).transform(m->xValue()); // double y1 = canvasMap(m->yAxis()).transform(m->yValue()); // double x2 = canvasMap(m_selectedMarker->xAxis()) // .transform(m_selectedMarker->xValue()); // double y2 = canvasMap(m_selectedMarker->yAxis()) // .transform(m_selectedMarker->yValue()); // double d = distance(QPointF(x1, y1), QPointF(x2, y2)); double d = distance(m->value(), m_selectedMarker->value()); if (d > dist) continue; // Let's make sure the direction is correct as well switch (direction) { // up case 0: { // Check to make sure this is actually up if (m->yValue() - m_selectedMarker->yValue() > 0.0) { selection = m; dist = d; } break; } // down case 1: { // Check to make sure this is actually down if (m_selectedMarker->yValue() - m->yValue() > 0.0) { selection = m; dist = d; } break; } // left case 2: { // Check to make sure this is actually left if (m_selectedMarker->xValue() - m->xValue() > 0.0) { selection = m; dist = d; } break; } // right case 3: { // Check to make sure this is actually right if (m->xValue() - m_selectedMarker->xValue() > 0.0) { selection = m; dist = d; } break; } } } } selectMarker(selection); }