diff options
author | Robert Griebl <rgriebl@trolltech.com> | 2010-04-14 18:43:03 +0200 |
---|---|---|
committer | Robert Griebl <rgriebl@trolltech.com> | 2010-04-14 18:43:03 +0200 |
commit | e36ef61f1a848508e13bf20ca3a17f7fe7b34b23 (patch) | |
tree | 70b5aabe8ac48462d4ad770af8ce3a878d58a4a8 | |
parent | 048d94bf93f5199054a39a5b7920960d533a7c4b (diff) |
forgot to add files
-rw-r--r-- | plotwidget.cpp | 125 | ||||
-rw-r--r-- | plotwidget.h | 39 |
2 files changed, 164 insertions, 0 deletions
diff --git a/plotwidget.cpp b/plotwidget.cpp new file mode 100644 index 0000000..90231ec --- /dev/null +++ b/plotwidget.cpp @@ -0,0 +1,125 @@ +#include <QtGui> + +#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 << "<table border=\"0\">"; + ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::red).dark().name() << "\" /><td>releaseVelocity X</td></tr>"; + ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::red).light().name() << "\" /><td>releaseVelocity Y</td></tr>"; + ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::green).dark().name() << "\" /><td>Content Position X</td></tr>"; + ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::green).light().name() << "\" /><td>Content Position Y</td></tr>"; + ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::blue).dark().name() << "\" /><td>Overshoot Position X</td></tr>"; + ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::blue).light().name() << "\" /><td>Overshoot Position Y</td></tr>"; + ts << "</table>"; + 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<PlotWidget *>(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<PlotItem>::const_iterator it = m_plotitems.constBegin(); + if (offset > 0) + it += (offset - 1); + +// PlotItem &last = const_cast<PlotItem &>(*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 +} diff --git a/plotwidget.h b/plotwidget.h new file mode 100644 index 0000000..8031549 --- /dev/null +++ b/plotwidget.h @@ -0,0 +1,39 @@ +#ifndef PLOTWIDGET_H +#define PLOTWIDGET_H + +#include <QtGui> + +class QKineticScroller; + +class PlotWidget : public QWidget +{ + Q_OBJECT + +public: + PlotWidget(QKineticScroller *scroller); + +public slots: + void reset(); + +protected: + void resizeEvent(QResizeEvent *); + void paintEvent(QPaintEvent *); + +private: + static void debugHook(void *user, const QPointF &releaseVelocity, const QPointF &contentPosition, const QPointF &overshootPosition); + + struct PlotItem { + QPointF releaseVelocity; + QPointF contentPosition; + QPointF overshootPosition; + }; + + void addPlotItem(const PlotItem &pi); + + QKineticScroller *m_scroller; + QList<PlotItem> m_plotitems; + qreal minMaxVelocity, minMaxPosition; + QWidget *m_clear, *m_legend; +}; + +#endif |