qabstractplot.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "qabstractplot.h"
  2. QAbstractPlot::QAbstractPlot(QQuickItem *parent)
  3. : QQuickPaintedItem(parent)
  4. , m_pCustomPlot(new QCustomPlot())
  5. {
  6. setFlag(QQuickItem::ItemHasContents, true);
  7. setAcceptedMouseButtons(Qt::AllButtons);
  8. connect(this, &QQuickPaintedItem::widthChanged, this, &QAbstractPlot::onPlotSizeChanged);
  9. connect(this, &QQuickPaintedItem::heightChanged, this, &QAbstractPlot::onPlotSizeChanged);
  10. connect(m_pCustomPlot, &QCustomPlot::afterReplot,
  11. this, &QAbstractPlot::onPlotUpdate, Qt::UniqueConnection);
  12. }
  13. QAbstractPlot::~QAbstractPlot()
  14. {
  15. delete m_pCustomPlot;
  16. }
  17. QCustomPlot *QAbstractPlot::customPlot()
  18. {
  19. return m_pCustomPlot;
  20. }
  21. void QAbstractPlot::setOpenGL(bool enabled)
  22. {
  23. if(m_pCustomPlot)
  24. {
  25. m_pCustomPlot->setOpenGl(true, 4);
  26. }
  27. }
  28. void QAbstractPlot::paint(QPainter *painter)
  29. {
  30. if (!painter || !painter->isActive())
  31. return;
  32. // m_pCustomPlot->setGeometry(0, 0, this->width(), this->height());
  33. // painter->drawPixmap(0, 0, this->width(), this->height(), m_pCustomPlot->toPixmap());
  34. QPixmap pixmap(boundingRect().size().toSize());
  35. QCPPainter qcpPainter(&pixmap);
  36. m_pCustomPlot->toPainter(&qcpPainter);
  37. painter->drawPixmap(QPoint(), pixmap);
  38. }
  39. void QAbstractPlot::postMouseEvents(QMouseEvent *event)
  40. {
  41. if(m_pCustomPlot)
  42. {
  43. QMouseEvent* newEvent = new QMouseEvent(event->type(), event->localPos(),
  44. event->button(), event->buttons(),
  45. event->modifiers());
  46. QCoreApplication::postEvent(m_pCustomPlot, newEvent);
  47. }
  48. }
  49. void QAbstractPlot::postWheelEvents(QWheelEvent *event)
  50. {
  51. if(m_pCustomPlot)
  52. {
  53. // QWheelEvent* newEvent = new QWheelEvent(event->pos(),
  54. // event->delta(),
  55. // event->buttons(),
  56. // event->modifiers(),
  57. // event->orientation());
  58. QWheelEvent* newEvent = new QWheelEvent(event->position(), event->globalPosition(),
  59. event->pixelDelta(), event->angleDelta(),
  60. event->buttons(), event->modifiers(),
  61. event->phase(), event->inverted());
  62. QCoreApplication::postEvent(m_pCustomPlot, newEvent);
  63. }
  64. }
  65. void QAbstractPlot::mousePressEvent(QMouseEvent *event)
  66. {
  67. this->postMouseEvents(event);
  68. }
  69. void QAbstractPlot::mouseReleaseEvent(QMouseEvent *event)
  70. {
  71. this->postMouseEvents(event);
  72. }
  73. void QAbstractPlot::mouseMoveEvent(QMouseEvent *event)
  74. {
  75. this->postMouseEvents(event);
  76. }
  77. void QAbstractPlot::mouseDoubleClickEvent(QMouseEvent *event)
  78. {
  79. this->postMouseEvents(event);
  80. }
  81. void QAbstractPlot::wheelEvent(QWheelEvent *event)
  82. {
  83. this->postWheelEvents(event);
  84. }
  85. void QAbstractPlot::onPlotSizeChanged()
  86. {
  87. m_pCustomPlot->setGeometry(0, 0, (int)width(), (int)height());
  88. #if 0
  89. QCPMarginGroup *marginGroup = new QCPMarginGroup(m_pCustomPlot);
  90. m_pCustomPlot->axisRect()->setMarginGroup(QCP::msBottom|QCP::msTop, marginGroup);
  91. m_pCustomPlot->axisRect()->setAutoMargins(QCP::msNone);
  92. m_pCustomPlot->axisRect()->setMargins(QMargins(0, 0, 0, 0));
  93. m_pCustomPlot->plotLayout()->setMargins(QMargins(0, 0, 0, 0));
  94. #endif
  95. m_pCustomPlot->setViewport(QRect(0, 0, (int)width(), (int)height()));
  96. m_pCustomPlot->axisRect()->setOuterRect(QRect(0, 0, (int)width(), (int)height()));
  97. m_pCustomPlot->axisRect()->setMinimumMargins (QMargins(0, 0, 0, 0));
  98. m_pCustomPlot->axisRect()->setMargins(QMargins(0, 0, 0, 0));
  99. }
  100. void QAbstractPlot::onPlotUpdate()
  101. {
  102. this->update();
  103. }