CustomPlotTracer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "CustomplotTracer.h"
  2. CustomPlotTracer::CustomPlotTracer(QCustomPlot *parentPlot) : QCPLayerable(parentPlot)
  3. {
  4. showLineh = false;
  5. showLinev = false;
  6. lineWidth = 3;
  7. lineColor = QColor(255, 0, 0);
  8. movePos = QPoint(100, 50);
  9. customPlot = parentPlot;
  10. connect(parentPlot, SIGNAL(mouseMove(QMouseEvent *)), this, SLOT(mouseMove(QMouseEvent *)));
  11. }
  12. void CustomPlotTracer::applyDefaultAntialiasingHint(QCPPainter *painter) const
  13. {
  14. Q_UNUSED(painter);
  15. }
  16. void CustomPlotTracer::draw(QCPPainter *painter)
  17. {
  18. QPen pen;
  19. pen.setWidth(lineWidth);
  20. pen.setColor(lineColor);
  21. painter->setPen(pen);
  22. if (showLinev) {
  23. painter->drawLine(QPointF(x, dHeight), QPointF(x, dY));
  24. pen.setColor(Qt::white);
  25. painter->setPen(pen);
  26. QFontMetrics fm = painter->fontMetrics();
  27. QString year = QString::number(QDate::currentDate().year());
  28. #if (QT_VERSION >= QT_VERSION_CHECK(5,11,0))
  29. int width = fm.horizontalAdvance(year);
  30. #else
  31. int width = fm.width(year);
  32. #endif
  33. painter->drawText(QPointF(x - width / 2, dHeight - 3), year);
  34. }
  35. if (showLineh) {
  36. pen.setColor(lineColor);
  37. painter->setPen(pen);
  38. painter->drawLine(QPointF(dX, y), QPointF(dWidth, y));
  39. }
  40. }
  41. bool CustomPlotTracer::getShowLineh() const
  42. {
  43. return this->showLineh;
  44. }
  45. bool CustomPlotTracer::getShowLinev() const
  46. {
  47. return this->showLinev;
  48. }
  49. int CustomPlotTracer::getLineWidth() const
  50. {
  51. return this->lineWidth;
  52. }
  53. QColor CustomPlotTracer::getLineColor() const
  54. {
  55. return this->lineColor;
  56. }
  57. void CustomPlotTracer::mouseMove(QMouseEvent *event)
  58. {
  59. Q_UNUSED(event);
  60. movePos = event->pos();
  61. drawLine();
  62. }
  63. void CustomPlotTracer::setShowLineh(bool showLineh)
  64. {
  65. if (this->showLineh != showLineh) {
  66. this->showLineh = showLineh;
  67. customPlot->replot();
  68. }
  69. }
  70. void CustomPlotTracer::setShowLinev(bool showLinev)
  71. {
  72. if (this->showLinev != showLinev) {
  73. this->showLinev = showLinev;
  74. customPlot->replot();
  75. }
  76. }
  77. void CustomPlotTracer::setLineWidth(int lineWidth)
  78. {
  79. if (this->lineWidth != lineWidth) {
  80. this->lineWidth = lineWidth;
  81. customPlot->replot();
  82. }
  83. }
  84. void CustomPlotTracer::setLineColor(const QColor &lineColor)
  85. {
  86. if (this->lineColor != lineColor) {
  87. this->lineColor = lineColor;
  88. customPlot->replot();
  89. }
  90. }
  91. void CustomPlotTracer::drawLine()
  92. {
  93. x = movePos.x();
  94. y = movePos.y();
  95. //取出当前XY轴的范围值
  96. QCPRange rangex = customPlot->xAxis->range();
  97. QCPRange rangey = customPlot->yAxis->range();
  98. //qDebug() << rangex.lower << rangex.upper << rangey.lower << rangey.upper;
  99. //取出左下角坐标轴原点(0,0)在整个画布的屏幕坐标位置
  100. dX = customPlot->xAxis->coordToPixel(rangex.lower);
  101. dY = customPlot->yAxis->coordToPixel(rangey.lower);
  102. dWidth = customPlot->xAxis->coordToPixel(rangex.upper);
  103. dHeight = customPlot->yAxis->coordToPixel(rangey.upper);
  104. //必须在曲线区域内
  105. if (x >= dX && x <= dWidth && y <= dY && y >= dHeight) {
  106. customPlot->replot();
  107. }
  108. }