add wraparound support to C2 physics
[openc2e.git] / qtgui / GraphWidget.cpp
blob991d49ec675fdc148869ea0cf0dc7976f766df12
1 /*
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License along
13 with this program; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 #include "GraphWidget.h"
19 GraphWidget::GraphWidget(QWidget *parent) : QWidget(parent) {
20 minvertical = -1.0f;
21 maxvertical = 1.0f;
22 sizehorizontal = 100;
25 GraphWidget::~GraphWidget() {
28 void GraphWidget::resetGraph() {
29 datasets.clear();
30 wipeGraph();
33 void GraphWidget::wipeGraph() {
34 datapoints.clear();
36 update();
39 void GraphWidget::setVerticalScale(float min, float max) {
40 minvertical = min;
41 maxvertical = max;
43 update();
46 void GraphWidget::setHorizontalSize(unsigned int size) {
47 sizehorizontal = size;
49 update();
52 void GraphWidget::addDataSet(unsigned int dataset) {
53 datasets[dataset].colour = QColor(255, 255, 255);
54 datapoints[dataset].reserve(sizehorizontal);
57 void GraphWidget::removeDataSet(unsigned int dataset) {
58 std::map<unsigned int, DataSetDetails>::iterator si = datasets.find(dataset);
59 if (si != datasets.end()) datasets.erase(si);
61 std::map<unsigned int, std::vector<float> >::iterator di = datapoints.find(dataset);
62 if (di != datapoints.end()) datapoints.erase(di);
64 update();
67 void GraphWidget::setDataSetColour(unsigned int dataset, QColor colour) {
68 datasets[dataset].colour = colour;
70 update();
73 void GraphWidget::setDataSetName(unsigned int dataset, QString name) {
74 datasets[dataset].name = name;
77 void GraphWidget::addDataPoint(unsigned int dataset, float data) {
78 datapoints[dataset].push_back(data);
79 if (datapoints[dataset].size() > sizehorizontal)
80 datapoints[dataset].erase(datapoints[dataset].begin());
82 update();
85 void GraphWidget::paintEvent(QPaintEvent *) {
86 QPainter painter(this);
88 float xmultiplier = (float)width() / sizehorizontal;
89 float ymultiplier = (float)height() / (maxvertical - minvertical);
91 for (std::map<unsigned int, std::vector<float> >::iterator i = datapoints.begin(); i != datapoints.end(); i++) {
92 std::vector<float> &points = i->second;
93 if (points.size() == 0) continue;
95 painter.setPen(datasets[i->first].colour);
97 unsigned int pointposition = 0;
99 // drawing starts on the *right* side, so as to keep everything in sync
100 if (points.size() < sizehorizontal)
101 pointposition = sizehorizontal - points.size();
103 float posx, posy, oldposx = 0.0f, oldposy = 0.0f;
104 for (unsigned int j = 0; j < points.size(); j++) {
105 posx = pointposition * xmultiplier;
106 posy = height() - ((points[j] - minvertical) * ymultiplier);
108 // TODO: draw point
110 if (j > 0) {
111 painter.drawLine(oldposx, oldposy, posx, posy);
114 oldposx = posx;
115 oldposy = posy;
116 pointposition++;