You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

XxwTracer.cpp 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "XxwTracer.h"
  2. XxwTracer::XxwTracer(QCustomPlot* _plot, TracerType _type, QObject* parent)
  3. : QObject(parent), m_plot(_plot), m_type(_type)
  4. {
  5. m_visible = true;
  6. m_tracer = Q_NULLPTR; // 跟踪的点
  7. m_label = Q_NULLPTR; // 显示的数值
  8. m_arrow = Q_NULLPTR; // 箭头
  9. if (m_plot) {
  10. QColor clrDefault(Qt::red); // 跟踪点颜色
  11. QBrush brushDefault(Qt::NoBrush);
  12. QPen penDefault(clrDefault);
  13. // penDefault.setBrush(brushDefault);
  14. penDefault.setWidthF(1);
  15. m_tracer = new QCPItemTracer(m_plot);
  16. m_tracer->setStyle(QCPItemTracer::tsCircle);
  17. m_tracer->setPen(penDefault);
  18. m_tracer->setBrush(brushDefault);
  19. // 设置跟踪锚定点
  20. m_label = new QCPItemText(m_plot);
  21. m_label->setLayer("overlay");
  22. m_label->setClipToAxisRect(false);
  23. m_label->setPadding(QMargins(5, 5, 5, 5));
  24. m_label->setBrush(brushDefault);
  25. m_label->setPen(penDefault);
  26. m_label->position->setParentAnchor(m_tracer->position);
  27. // m_label->setFont(QFont("宋体", 8));
  28. m_label->setFont(QFont("Arial", 8));
  29. m_label->setColor(clrDefault);
  30. m_label->setText("");
  31. m_arrow = new QCPItemLine(m_plot);
  32. QPen arrowPen(clrDefault, 1);
  33. m_arrow->setPen(penDefault);
  34. m_arrow->setLayer("overlay");
  35. m_arrow->setClipToAxisRect(false);
  36. m_arrow->setHead(QCPLineEnding::esSpikeArrow); // 设置头部为箭头形状
  37. switch (m_type) {
  38. case XAxisTracer: {
  39. m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
  40. m_tracer->position->setTypeY(QCPItemPosition::ptAxisRectRatio);
  41. m_tracer->setSize(7);
  42. m_label->setPositionAlignment(Qt::AlignTop | Qt::AlignHCenter);
  43. m_arrow->end->setParentAnchor(m_tracer->position);
  44. m_arrow->start->setParentAnchor(m_arrow->end);
  45. m_arrow->start->setCoords(0, 20); // 偏移量
  46. break;
  47. }
  48. case YAxisTracer: {
  49. m_tracer->position->setTypeX(QCPItemPosition::ptAxisRectRatio);
  50. m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);
  51. m_tracer->setSize(7);
  52. m_label->setPositionAlignment(Qt::AlignRight | Qt::AlignHCenter);
  53. m_arrow->end->setParentAnchor(m_tracer->position);
  54. m_arrow->start->setParentAnchor(m_label->position);
  55. m_arrow->start->setCoords(-20, 0); // 偏移量
  56. break;
  57. }
  58. case DataTracer: {
  59. m_tracer->position->setTypeX(QCPItemPosition::ptPlotCoords);
  60. m_tracer->position->setTypeY(QCPItemPosition::ptPlotCoords);
  61. m_tracer->setSize(5);
  62. m_label->setPositionAlignment(Qt::AlignLeft | Qt::AlignVCenter);
  63. m_arrow->end->setParentAnchor(m_tracer->position);
  64. m_arrow->start->setParentAnchor(m_arrow->end);
  65. m_arrow->start->setCoords(20, 0);
  66. break;
  67. }
  68. default:
  69. break;
  70. }
  71. setVisible(false);
  72. }
  73. }
  74. XxwTracer::~XxwTracer()
  75. {
  76. if (m_plot) {
  77. if (m_tracer)
  78. m_plot->removeItem(m_tracer);
  79. if (m_label)
  80. m_plot->removeItem(m_label);
  81. if (m_arrow)
  82. m_plot->removeItem(m_arrow);
  83. }
  84. }
  85. void XxwTracer::setPen(const QPen& pen)
  86. {
  87. if (m_tracer)
  88. m_tracer->setPen(pen);
  89. if (m_arrow)
  90. m_arrow->setPen(pen);
  91. }
  92. void XxwTracer::setBrush(const QBrush& brush)
  93. {
  94. if (m_tracer)
  95. m_tracer->setBrush(brush);
  96. }
  97. void XxwTracer::setLabelPen(const QPen& pen)
  98. {
  99. if (m_label) {
  100. m_label->setPen(pen);
  101. m_label->setBrush(Qt::NoBrush);
  102. m_label->setColor(pen.color());
  103. }
  104. }
  105. void XxwTracer::setText(const QString& text)
  106. {
  107. if (m_label)
  108. m_label->setText(text);
  109. }
  110. void XxwTracer::setVisible(bool vis)
  111. {
  112. m_visible = vis;
  113. if (m_tracer)
  114. m_tracer->setVisible(m_visible);
  115. if (m_label)
  116. m_label->setVisible(m_visible);
  117. if (m_arrow)
  118. m_arrow->setVisible(m_visible);
  119. }
  120. void XxwTracer::updatePosition(double xValue, double yValue)
  121. {
  122. if (!m_visible) {
  123. setVisible(true);
  124. m_visible = true;
  125. }
  126. if (yValue > m_plot->yAxis->range().upper)
  127. yValue = m_plot->yAxis->range().upper;
  128. switch (m_type) {
  129. case XAxisTracer: {
  130. m_tracer->position->setCoords(xValue, 1);
  131. m_label->position->setCoords(0, 15);
  132. m_arrow->start->setCoords(0, 15);
  133. m_arrow->end->setCoords(0, 0);
  134. setText(QString::number(xValue));
  135. break;
  136. }
  137. case YAxisTracer: {
  138. m_tracer->position->setCoords(0, yValue);
  139. m_label->position->setCoords(-20, 0);
  140. // m_arrow->start->setCoords(20, 0);
  141. // m_arrow->end->setCoords(0, 0);
  142. setText(QString::number(yValue));
  143. break;
  144. }
  145. case DataTracer: {
  146. m_tracer->position->setCoords(xValue, yValue);
  147. m_label->position->setCoords(20, 20);
  148. // setText(QString("x:%1,y:%2").arg(xValue).arg(yValue));
  149. QString xVal = QString::number(xValue, 'f', 6);
  150. QString yVal = QString::number(yValue, 'f', 6);
  151. QString Val = "x:" + xVal + ",y:" + yVal;
  152. setText(Val);
  153. break;
  154. }
  155. default:
  156. break;
  157. }
  158. }
  159. XxwTraceLine::XxwTraceLine(QCustomPlot* _plot, LineType _type, QObject* parent)
  160. : QObject(parent), m_type(_type), m_plot(_plot)
  161. {
  162. m_lineV = Q_NULLPTR;
  163. m_lineH = Q_NULLPTR;
  164. initLine();
  165. }
  166. XxwTraceLine::~XxwTraceLine()
  167. {
  168. if (m_plot) {
  169. if (m_lineV)
  170. m_plot->removeItem(m_lineV);
  171. if (m_lineH)
  172. m_plot->removeItem(m_lineH);
  173. }
  174. }
  175. void XxwTraceLine::initLine()
  176. {
  177. if (m_plot) {
  178. QPen linesPen(Qt::red, 1, Qt::DashLine); // 设置游标颜色和宽度
  179. if (VerticalLine == m_type || Both == m_type) {
  180. m_lineV = new QCPItemStraightLine(m_plot); // 垂直线
  181. m_lineV->setLayer("overlay");
  182. m_lineV->setPen(linesPen);
  183. m_lineV->setClipToAxisRect(true);
  184. m_lineV->point1->setCoords(0, 0);
  185. m_lineV->point2->setCoords(0, 0);
  186. }
  187. if (HorizonLine == m_type || Both == m_type) {
  188. m_lineH = new QCPItemStraightLine(m_plot); // 水平线
  189. m_lineH->setLayer("overlay");
  190. m_lineH->setPen(linesPen);
  191. m_lineH->setClipToAxisRect(true);
  192. m_lineH->point1->setCoords(0, 0);
  193. m_lineH->point2->setCoords(0, 0);
  194. }
  195. }
  196. }
  197. void XxwTraceLine::updatePosition(double xValue, double yValue)
  198. {
  199. if (VerticalLine == m_type || Both == m_type) {
  200. if (m_lineV) {
  201. m_lineV->point1->setCoords(xValue, m_plot->yAxis->range().lower);
  202. m_lineV->point2->setCoords(xValue, m_plot->yAxis->range().upper);
  203. }
  204. }
  205. if (HorizonLine == m_type || Both == m_type) {
  206. if (m_lineH) {
  207. m_lineH->point1->setCoords(m_plot->xAxis->range().lower, yValue);
  208. m_lineH->point2->setCoords(m_plot->xAxis->range().upper, yValue);
  209. }
  210. }
  211. }