#include #include "plotwidget.h" #include "qkineticscroller.h" PlotWidget::PlotWidget(QKineticScroller *scroller) : QWidget(), m_scroller(scroller) { setWindowTitle(QLatin1String("Plot")); m_scroller->registerDebugHook(debugHook, this); m_clear = new QPushButton(QLatin1String("Clear"), this); connect(m_clear, SIGNAL(clicked()), this, SLOT(reset())); m_legend = new QLabel(QLatin1String("Legend"), this); QString tooltip; QTextStream ts(&tooltip); ts << ""; ts << ""; ts << ""; ts << ""; ts << ""; ts << ""; ts << ""; ts << "
releaseVelocity X
releaseVelocity Y
Content Position X
Content Position Y
Overshoot Position X
Overshoot Position Y
"; m_legend->setToolTip(tooltip); } void PlotWidget::debugHook(void *user, const QPointF &releaseVelocity, const QPointF &contentPosition, const QPointF &overshootPosition) { PlotItem pi = { releaseVelocity, contentPosition, overshootPosition }; static_cast(user)->addPlotItem(pi); } static inline void doMaxMin(const QPointF &v, qreal &minmaxv) { minmaxv = qMax(minmaxv, qMax(qAbs(v.x()), qAbs(v.y()))); } void PlotWidget::addPlotItem(const PlotItem &pi) { m_plotitems.append(pi); minMaxVelocity = minMaxPosition = 0; while (m_plotitems.size() > 500) m_plotitems.removeFirst(); foreach (const PlotItem &pi, m_plotitems) { doMaxMin(pi.releaseVelocity, minMaxVelocity); doMaxMin(pi.contentPosition, minMaxPosition); doMaxMin(pi.overshootPosition, minMaxPosition); } update(); } void PlotWidget::reset() { m_plotitems.clear(); minMaxVelocity = minMaxPosition = 0; update(); } void PlotWidget::resizeEvent(QResizeEvent *) { QSize cs = m_clear->sizeHint(); QSize ls = m_legend->sizeHint(); m_clear->setGeometry(4, 4, cs.width(), cs.height()); m_legend->setGeometry(4, height() - ls.height() - 4, ls.width(), ls.height()); } void PlotWidget::paintEvent(QPaintEvent *) { #define SCALE(v, mm) ((qreal(1) - (v / mm)) * qreal(0.5) * height()) QColor rvColor = Qt::red; QColor cpColor = Qt::green; QColor opColor = Qt::blue; QPainter p(this); p.setRenderHints(QPainter::Antialiasing); p.fillRect(rect(), Qt::white); if (m_plotitems.isEmpty()) return; int x = 2; int offset = m_plotitems.size() - width() / 2; QList::const_iterator it = m_plotitems.constBegin(); if (offset > 0) it += (offset - 1); // PlotItem &last = const_cast(*it++); // PlotItem dummy; const PlotItem *last = &(*it++); // last = *it++; // qWarning() << "lastOp[0].y = " << last.overshootPosition.y() << m_plotitems.at(1).overshootPosition.y(); while (it != m_plotitems.constEnd()) { p.setPen(rvColor.light()); p.drawLine(qreal(x - 2), SCALE(last->releaseVelocity.x(), minMaxVelocity), qreal(x), SCALE(it->releaseVelocity.x(), minMaxVelocity)); p.setPen(rvColor.dark()); p.drawLine(qreal(x - 2), SCALE(last->releaseVelocity.y(), minMaxVelocity), qreal(x), SCALE(it->releaseVelocity.y(), minMaxVelocity)); p.setPen(cpColor.light()); p.drawLine(qreal(x - 2), SCALE(last->contentPosition.x(), minMaxPosition), qreal(x), SCALE(it->contentPosition.x(), minMaxPosition)); p.setPen(cpColor.dark()); p.drawLine(qreal(x - 2), SCALE(last->contentPosition.y(), minMaxPosition), qreal(x), SCALE(it->contentPosition.y(), minMaxPosition)); p.setPen(opColor.light()); p.drawLine(qreal(x - 2), SCALE(last->overshootPosition.x(), minMaxPosition), qreal(x), SCALE(it->overshootPosition.x(), minMaxPosition)); p.setPen(opColor.dark()); p.drawLine(qreal(x - 2), SCALE(last->overshootPosition.y(), minMaxPosition), qreal(x), SCALE(it->overshootPosition.y(), minMaxPosition)); last = &(*it++); x += 2; } #undef SCALE }