//! 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); } } }
/** * Returns the index of the closest curve to a point on the canvas. * Also returns index of the nearest data point on that curve. * @param xpos :: x coordinate of a point on the canvas in pixels. * @param ypos :: y coordinate of a point on the canvas in pixels. * @param dist :: ? * @param point :: Output index of the nearest data point to the point with coordinates (xpos,ypos) */ int Plot::closestCurve(int xpos, int ypos, int &dist, int &point) { QwtScaleMap map[QwtPlot::axisCnt]; for ( int axis = 0; axis < QwtPlot::axisCnt; axis++ ) map[axis] = canvasMap(axis); double dmin = std::numeric_limits<double>::max(); int key = -1; for (QMap<int, QwtPlotItem *>::iterator iter = d_curves.begin(); iter != d_curves.end(); ++iter ) { QwtPlotItem *item = (QwtPlotItem *)iter.data(); if (!item) continue; if(item->rtti() != QwtPlotItem::Rtti_PlotSpectrogram) { PlotCurve *c = (PlotCurve *)item; DataCurve *dc = dynamic_cast<DataCurve *>(item); if (dc) { if (c->type() != Graph::Function && dc->hasLabels() && dc->selectedLabels(QPoint(xpos, ypos))){ dist = 0; return iter.key(); } else dc->setLabelsSelected(false); } for (int i=0; i<c->dataSize(); i++) { double cx = map[c->xAxis()].xTransform(c->x(i)) - double(xpos); double cy = map[c->yAxis()].xTransform(c->y(i)) - double(ypos); double f = qwtSqr(cx) + qwtSqr(cy); if (f < dmin && c->type() != Graph::ErrorBars) { dmin = f; key = iter.key(); point = i; } } } } dist = static_cast<int>(sqrt(dmin)); return key; }
/*! \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; }
/*! \brief Update the canvas margins Plot items might indicate, that they need some extra space at the borders of the canvas by the QwtPlotItem::Margins flag. getCanvasMarginsHint(), QwtPlotItem::getCanvasMarginHint() */ void QwtPlot::updateCanvasMargins() { QwtScaleMap maps[axisCnt]; for ( int axisId = 0; axisId < axisCnt; axisId++ ) maps[axisId] = canvasMap( axisId ); double margins[axisCnt]; getCanvasMarginsHint( maps, canvas()->contentsRect(), margins[yLeft], margins[xTop], margins[yRight], margins[xBottom] ); bool doUpdate = false; for ( int axisId = 0; axisId < axisCnt; axisId++ ) { if ( margins[axisId] >= 0.0 ) { const int m = qCeil( margins[axisId] ); plotLayout()->setCanvasMargin( m, axisId); doUpdate = true; } } if ( doUpdate ) updateLayout(); }
void SingleCellViewGraphPanelPlotWidget::drawCoordinates(QPainter *pPainter, const QPointF &pCoordinates, const QColor &pBackgroundColor, const QColor &pForegroundColor, const Location &pLocation, const bool &pCanMoveLocation) { // Retrieve the size of coordinates as they will appear on the screen, // which means using the same font as the one used for the axes pPainter->setFont(axisFont(QwtPlot::xBottom)); QString coords = QString("(%1, %2)").arg(QString::number(pCoordinates.x()), QString::number(pCoordinates.y())); QRect desktopGeometry = qApp->desktop()->availableGeometry(); QRectF coordsRect = pPainter->boundingRect(QRectF(0.0, 0.0, desktopGeometry.width(), desktopGeometry.height()), coords); // Determine where the coordinates and its background should be drawn QPoint coordinates = QPoint(canvasMap(QwtPlot::xBottom).transform(pCoordinates.x()), canvasMap(QwtPlot::yLeft).transform(pCoordinates.y())); switch (pLocation) { case TopLeft: coordsRect.moveTo(coordinates.x()-coordsRect.right()-1, coordinates.y()-coordsRect.bottom()-1); break; case TopRight: coordsRect.moveTo(coordinates.x()+2, coordinates.y()-coordsRect.bottom()-1); break; case BottomLeft: coordsRect.moveTo(coordinates.x()-coordsRect.right()-1, coordinates.y()+2); break; case BottomRight: coordsRect.moveTo(coordinates.x()+2, coordinates.y()+2); break; } if (pCanMoveLocation) { if (coordsRect.top() < 0) coordsRect.moveTop(coordinates.y()+2); if (coordsRect.left() < 0) coordsRect.moveLeft(coordinates.x()+2); if (coordsRect.bottom() > plotLayout()->canvasRect().height()) coordsRect.moveTop(coordinates.y()-coordsRect.height()-1); if (coordsRect.right() > plotLayout()->canvasRect().width()) coordsRect.moveLeft(coordinates.x()-coordsRect.width()-1); } // Draw a filled rectangle to act as the background of the coordinates // we are to show pPainter->fillRect(coordsRect, pBackgroundColor); // Draw the text for the coordinates, using a white pen QPen pen = pPainter->pen(); pen.setColor(pForegroundColor); pPainter->setPen(pen); pPainter->drawText(coordsRect, coords); }
void SingleCellViewGraphPanelPlotWidget::drawCanvas(QPainter *pPainter) { foreach (SingleCellViewGraphPanelPlotCurve *curve, mCurves) static_cast<SingleCellViewQwtCurveDataAdaptor*>(curve->data())->updateSize(); switch (mAction) { case ShowCoordinates: { // We are showing some coordinates, so start by drawing our pixmap pPainter->drawPixmap(0, 0, mCanvasPixmap); // Draw the two dashed lines that show the coordinates, using a dark // cyan pen QPen pen = pPainter->pen(); QColor backgroundColor = Qt::darkCyan; backgroundColor.setAlphaF(0.69); pen.setColor(backgroundColor); pen.setStyle(Qt::DashLine); pPainter->setPen(pen); QPointF coordinates = QPointF(canvasMap(QwtPlot::xBottom).transform(mOriginPoint.x()), canvasMap(QwtPlot::yLeft).transform(mOriginPoint.y())); pPainter->drawLine(0.0, coordinates.y(), plotLayout()->canvasRect().width(), coordinates.y()); pPainter->drawLine(coordinates.x(), 0.0, coordinates.x(), plotLayout()->canvasRect().height()); // Draw the coordinates drawCoordinates(pPainter, mOriginPoint, backgroundColor, Qt::white); break; } case ZoomRegion: { // We are zooming a region, so start by drawing our pixmap pPainter->drawPixmap(0, 0, mCanvasPixmap); // Retrieve the coordinates of the region to be zoomed QRectF zoomRegionRect = zoomRegion(); // Now, draw the region to be zoomed QColor penColor = Qt::darkRed; QColor brushColor = Qt::yellow; penColor.setAlphaF(0.69); brushColor.setAlphaF(0.19); pPainter->setPen(penColor); QwtScaleMap canvasMapX = canvasMap(QwtPlot::xBottom); QwtScaleMap canvasMapY = canvasMap(QwtPlot::yLeft); double left = canvasMapX.transform(zoomRegionRect.left()); double top = canvasMapY.transform(zoomRegionRect.top()); QRectF unmappedZoomRegionRect = QRectF(left, top, canvasMapX.transform(zoomRegionRect.right())-left, canvasMapY.transform(zoomRegionRect.bottom())-top); pPainter->fillRect(unmappedZoomRegionRect, brushColor); pPainter->drawRect(unmappedZoomRegionRect); // Draw the two sets of coordinates drawCoordinates(pPainter, zoomRegionRect.topLeft(), penColor, Qt::white, BottomRight, false); drawCoordinates(pPainter, zoomRegionRect.bottomRight(), penColor, Qt::white, TopLeft, false); break; } default: // We aren't doing anything special, so just draw our canvas normally QwtPlot::drawCanvas(pPainter); } }
void QxrdImagePlot::contextMenuEvent(QContextMenuEvent * event) { if (m_ContextMenuEnabled) { QxrdImagePlotSettingsPtr set(m_ImagePlotSettings); if (set) { QMenu plotMenu(NULL, NULL); QAction *auSc = plotMenu.addAction("Autoscale"); QAction *prGr = plotMenu.addAction("Print Graph..."); plotMenu.addSeparator(); QxrdDataProcessorPtr dp(m_DataProcessor); if (dp) { QxrdCenterFinderPtr cf(dp->centerFinder()); if (cf) { QwtScaleMap xMap = canvasMap(QwtPlot::xBottom); QwtScaleMap yMap = canvasMap(QwtPlot::yLeft); QWidget *canv = canvas(); QPoint evlocal = canv->mapFromParent(event->pos()); double x = xMap.invTransform(evlocal.x()); double y = yMap.invTransform(evlocal.y()); QxrdPowderPoint nearest = cf->nearestPowderPoint(x, y); QAction *fitCircle = plotMenu.addAction(tr("Fit Circle Center from Points on Ring %1").arg(nearest.n1())); QAction *fitEllipse = plotMenu.addAction(tr("Fit Ellipse from Points on Ring %1").arg(nearest.n1())); QAction *fitEllipses = plotMenu.addAction(tr("Fit Ellipses to all powder rings")); QAction *delPoint = plotMenu.addAction(tr("Delete point at (%1,%2)").arg(nearest.x()).arg(nearest.y())); QAction *delRing = plotMenu.addAction(tr("Delete Ring %1").arg(nearest.n1())); QAction *deleteAllPoints = plotMenu.addAction(tr("Delete all Rings")); QAction *disableRing = plotMenu.addAction(tr("Disable Ring %1").arg(nearest.n1())); QAction *enableRing = plotMenu.addAction(tr("Enable Ring %1").arg(nearest.n1())); QAction *normalizeRings = plotMenu.addAction(tr("Normalize Powder Rings")); QAction *fitPeakNear = plotMenu.addAction(tr("Fit Diffracted Peak near (%1,%2) [%3,%4]").arg(x).arg(y).arg(event->x()).arg(event->y())); QAction *fitRingNear = plotMenu.addAction(tr("Fit Point on Diffracted Ring near (%1,%2) [%3,%4]").arg(x).arg(y).arg(event->x()).arg(event->y())); QAction *traceRingClockwise = plotMenu.addAction(tr("Trace Diffracted Ring starting at (%1,%2) [%3,%4]").arg(x).arg(y).arg(event->x()).arg(event->y())); QAction *missingRing = plotMenu.addAction(tr("Missing Diffracted Ring near (%1,%2)").arg(x).arg(y)); // QAction *traceRingParallel = plotMenu.addAction(tr("Trace Diffracted Ring starting at (%1,%2) [%3,%4] in parallel").arg(x).arg(y).arg(event->x()).arg(event->y())); QAction *zapPixel = plotMenu.addAction(tr("Zap (replace with avg of neighboring values) pixel [%1,%2]").arg((int)x).arg(int(y))); QAction *action = plotMenu.exec(event->globalPos()); if (action == auSc) { autoScale(); } else if (action == prGr) { printGraph(); } else if (action == fitCircle) { cf->fitPowderCircle(nearest.n1()); } else if (action == fitEllipse) { cf->fitPowderEllipse(nearest.n1()); } else if (action == fitEllipses) { cf->fitPowderEllipses(); } else if (action == delPoint) { cf->deletePowderPointNear(x,y); } else if (action == delRing) { cf->deletePowderRing(nearest.n1()); } else if (action == deleteAllPoints) { cf->deletePowderPoints(); } else if (action == enableRing) { cf->enablePowderRing(nearest.n1()); } else if (action == disableRing) { cf->disablePowderRing(nearest.n1()); } else if (action == normalizeRings) { cf->normalizePowderRings(); } else if (action == fitPeakNear) { QMetaObject::invokeMethod(cf.data(), "fitPeakNear", Q_ARG(double,x), Q_ARG(double,y)); } else if (action == fitRingNear) { QMetaObject::invokeMethod(cf.data(), "fitRingNear", Q_ARG(double,x), Q_ARG(double,y)); } else if (action == traceRingClockwise) { QMetaObject::invokeMethod(cf.data(), "traceRingNear", Q_ARG(double,x), Q_ARG(double,y), Q_ARG(double,25.0)); } else if (action == missingRing) {