graph_tracer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #include "graph_tracer.h"
  2. PlotTimeTracer::PlotTimeTracer(AxisRect *axisrect) : QObject(axisrect),
  3. mAxisRect(axisrect),
  4. mVerticalLine(nullptr),
  5. mTimeText(nullptr),
  6. mVisible(false),
  7. mIsInitialized(false)
  8. {
  9. }
  10. PlotTimeTracer::~PlotTimeTracer()
  11. {
  12. if(mAxisRect) {
  13. mAxisRect->parentPlot()->removeItem(mVerticalLine);
  14. for(auto tracer : mTracers)
  15. mAxisRect->parentPlot()->removeItem(tracer);
  16. for(auto arrow : mArrows)
  17. mAxisRect->parentPlot()->removeItem(arrow);
  18. for(auto text : mTexts)
  19. mAxisRect->parentPlot()->removeItem(text);
  20. mAxisRect->parentPlot()->removeItem(mTimeText);
  21. }
  22. }
  23. void PlotTimeTracer::initialize()
  24. {
  25. if(!mIsInitialized && mAxisRect) {
  26. mVerticalLine = new QCPItemStraightLine(mAxisRect->parentPlot());
  27. mVerticalLine->setLayer(QLatin1String("tracer"));
  28. mVerticalLine->setPen(QPen(mLineColor, 0, Qt::DashLine));
  29. mVerticalLine->setClipAxisRect(mAxisRect);
  30. mVerticalLine->setClipToAxisRect(true);
  31. mVerticalLine->point1->setAxisRect(mAxisRect);
  32. mVerticalLine->point1->setAxes(mAxisRect->axis(QCPAxis::atBottom), mAxisRect->axis(QCPAxis::atRight));
  33. mVerticalLine->point2->setAxisRect(mAxisRect);
  34. mVerticalLine->point2->setAxes(mAxisRect->axis(QCPAxis::atBottom), mAxisRect->axis(QCPAxis::atRight));
  35. mVerticalLine->point1->setCoords(0, 0);
  36. mVerticalLine->point2->setCoords(0, 0);
  37. mVerticalLine->setVisible(mVisible);
  38. for(auto graph : mAxisRect->graphs()) {
  39. auto tracer = new QCPItemTracer(mAxisRect->parentPlot());
  40. tracer->position->setAxisRect(mAxisRect);
  41. tracer->position->setAxes(mAxisRect->axis(QCPAxis::atBottom), mAxisRect->axis(QCPAxis::atRight));
  42. tracer->setLayer(QLatin1String("tracer"));
  43. tracer->position->setType(QCPItemPosition::ptPlotCoords);
  44. tracer->setClipAxisRect(mAxisRect);
  45. tracer->setClipToAxisRect(true);
  46. tracer->setStyle(QCPItemTracer::tsSquare);
  47. tracer->setPen(graph->pen());
  48. tracer->setBrush(Qt::NoBrush);
  49. tracer->setGraph(graph);
  50. tracer->setSize(7);
  51. tracer->setVisible(mVisible);
  52. tracer->setInterpolating(false);
  53. mTracers.append(tracer);
  54. auto arrow = new QCPItemLine(mAxisRect->parentPlot());
  55. arrow->setLayer(QLatin1String("tracer"));
  56. arrow->end->setParentAnchor(tracer->position);
  57. arrow->start->setParentAnchor(arrow->end);
  58. arrow->start->setCoords(15, 0);
  59. arrow->start->setAxisRect(mAxisRect);
  60. arrow->start->setAxes(mAxisRect->axis(QCPAxis::atBottom), mAxisRect->axis(QCPAxis::atRight));
  61. arrow->end->setAxisRect(mAxisRect);
  62. arrow->end->setAxes(mAxisRect->axis(QCPAxis::atBottom), mAxisRect->axis(QCPAxis::atRight));
  63. arrow->setClipAxisRect(mAxisRect);
  64. arrow->setClipToAxisRect(true);
  65. arrow->setPen(graph->pen());
  66. arrow->setVisible(mVisible);
  67. mArrows.append(arrow);
  68. auto text = new QCPItemText(mAxisRect->parentPlot());
  69. text->setLayer(QLatin1String("tracer"));
  70. text->setClipAxisRect(mAxisRect);
  71. text->setClipToAxisRect(true);
  72. text->setPadding(QMargins(8, 0, 8, 0));
  73. text->setPen(graph->pen());
  74. text->setColor(graph->pen().color());
  75. text->setBrush(mTextBrush);
  76. text->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);
  77. text->position->setParentAnchor(arrow->start);
  78. text->position->setAxisRect(mAxisRect);
  79. text->position->setAxes(mAxisRect->axis(QCPAxis::atBottom), mAxisRect->axis(QCPAxis::atRight));
  80. text->setVisible(mVisible);
  81. mTexts.append(text);
  82. }
  83. mTimeText = new QCPItemText(mAxisRect->parentPlot());
  84. mTimeText->setLayer(QLatin1String("tracer"));
  85. mTimeText->setClipAxisRect(mAxisRect);
  86. mTimeText->setClipToAxisRect(false);
  87. mTimeText->setPadding(QMargins(8, 0, 8, 0));
  88. mTimeText->setPen(QPen(mLineColor, 0));
  89. mTimeText->setColor(mTextColor);
  90. mTimeText->setBrush(mTextBrush);
  91. mTimeText->setPositionAlignment(Qt::AlignHCenter);
  92. mTimeText->position->setAxisRect(mAxisRect);
  93. mTimeText->position->setAxes(mAxisRect->axis(QCPAxis::atBottom), mAxisRect->axis(QCPAxis::atRight));
  94. mTimeText->position->setTypeX(QCPItemPosition::ptPlotCoords);
  95. mTimeText->position->setTypeY(QCPItemPosition::ptAxisRectRatio);
  96. mTimeText->setVisible(mVisible);
  97. mIsInitialized = true;
  98. } else {
  99. qDebug() << Q_FUNC_INFO << "Already initialized or axisRect null";
  100. }
  101. }
  102. void PlotTimeTracer::setVisible(bool visible)
  103. {
  104. if(!mIsInitialized)
  105. return;
  106. if(visible != mVisible) {
  107. mVisible = visible;
  108. if(mVerticalLine)
  109. mVerticalLine->setVisible(mVisible);
  110. for(auto tracer : mTracers)
  111. tracer->setVisible(mVisible);
  112. for(auto arrow : mArrows)
  113. arrow->setVisible(mVisible);
  114. for(auto text : mTexts)
  115. text->setVisible(mVisible);
  116. if(mTimeText)
  117. mTimeText->setVisible(mVisible);
  118. }
  119. }
  120. bool PlotTimeTracer::visible() const
  121. {
  122. return mVisible;
  123. }
  124. QCPAxis::AxisType PlotTimeTracer::yAxisType() const
  125. {
  126. return mYAxisType;
  127. }
  128. void PlotTimeTracer::setYAxisType(const QCPAxis::AxisType &yAxisType)
  129. {
  130. mYAxisType = yAxisType;
  131. }
  132. QPointF PlotTimeTracer::mousePos() const
  133. {
  134. return mMousePos;
  135. }
  136. void PlotTimeTracer::setMousePos(const QPointF &mousePos)
  137. {
  138. mMousePos = mousePos;
  139. updateItems();
  140. }
  141. void PlotTimeTracer::updateItems()
  142. {
  143. if(mIsInitialized) {
  144. double keyValue = mAxisRect->axis(QCPAxis::atBottom)->pixelToCoord(mMousePos.x());
  145. mVerticalLine->point1->setCoords(keyValue, mAxisRect->axis(mYAxisType)->range().lower);
  146. mVerticalLine->point2->setCoords(keyValue, mAxisRect->axis(mYAxisType)->range().upper);
  147. if(mAxisRect->rect().right() - mMousePos.x() < 200) {
  148. for(int i=0; i<mTracers.count();i++) {
  149. mTracers.at(i)->setGraphKey(keyValue);
  150. mTracers.at(i)->updatePosition();
  151. mArrows.at(i)->start->setCoords(-15, 0);
  152. mTexts.at(i)->setPositionAlignment(Qt::AlignRight|Qt::AlignVCenter);
  153. mTexts.at(i)->setText(QString::number(mTracers.at(i)->position->value()));
  154. }
  155. } else {
  156. for(int i=0; i<mTracers.count();i++) {
  157. mTracers.at(i)->setGraphKey(keyValue);
  158. mTracers.at(i)->updatePosition();
  159. mArrows.at(i)->start->setCoords(15, 0);
  160. mTexts.at(i)->setPositionAlignment(Qt::AlignLeft|Qt::AlignVCenter);
  161. mTexts.at(i)->setText(QString::number(mTracers.at(i)->position->value()));
  162. }
  163. }
  164. mTimeText->position->setCoords(keyValue, 0);
  165. mTimeText->setText(QDateTime::fromMSecsSinceEpoch(qint64(keyValue*1000)).toUTC().toString("HH:mm:ss.zzz"));
  166. } else {
  167. qDebug() << Q_FUNC_INFO << "Not initialized";
  168. }
  169. }
  170. QColor PlotTimeTracer::lineColor() const
  171. {
  172. return mLineColor;
  173. }
  174. void PlotTimeTracer::setLineColor(const QColor &lineColor)
  175. {
  176. mLineColor = lineColor;
  177. }
  178. QColor PlotTimeTracer::textColor() const
  179. {
  180. return mTextColor;
  181. }
  182. void PlotTimeTracer::setTextColor(const QColor &textColor)
  183. {
  184. mTextColor = textColor;
  185. }
  186. QBrush PlotTimeTracer::textBrush() const
  187. {
  188. return mTextBrush;
  189. }
  190. void PlotTimeTracer::setTextBrush(const QBrush &textBrush)
  191. {
  192. mTextBrush = textBrush;
  193. }