summaryrefslogtreecommitdiffstats
path: root/testapp/plotwidget.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'testapp/plotwidget.cpp')
-rw-r--r--testapp/plotwidget.cpp161
1 files changed, 161 insertions, 0 deletions
diff --git a/testapp/plotwidget.cpp b/testapp/plotwidget.cpp
new file mode 100644
index 0000000..0564df2
--- /dev/null
+++ b/testapp/plotwidget.cpp
@@ -0,0 +1,161 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtGui module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#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(this);
+ QString legend;
+ QTextStream ts(&legend);
+ ts << "<table border=\"0\">";
+ ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::red).light().name() << "\" /><td>releaseVelocity X</td></tr>";
+ ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::red).dark().name() << "\" /><td>releaseVelocity Y</td></tr>";
+ ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::green).light().name() << "\" /><td>Content Position X</td></tr>";
+ ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::green).dark().name() << "\" /><td>Content Position Y</td></tr>";
+ ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::blue).light().name() << "\" /><td>Overshoot Position X</td></tr>";
+ ts << "<tr><td width=\"30\" bgcolor=\"" << QColor(Qt::blue).dark().name() << "\" /><td>Overshoot Position Y</td></tr>";
+ ts << "</table>";
+ m_legend->setText(legend);
+}
+
+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);
+
+ const PlotItem *last = &(*it++);
+
+ 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
+}