|
| 1 | +#include "graph.h" |
| 2 | +#include "applicationsettings.h" |
| 3 | +#include <QDir> |
| 4 | +#include <QDomDocument> |
| 5 | +#include <QDomElement> |
| 6 | +#include <QPainter> |
| 7 | + |
| 8 | +Graph::Graph(QWidget *parent) : |
| 9 | + QWidget(parent) |
| 10 | +{ |
| 11 | +} |
| 12 | + |
| 13 | +void Graph::trend(const QString &projectName) |
| 14 | +{ |
| 15 | + values.clear(); |
| 16 | + datetimes.clear(); |
| 17 | + |
| 18 | + ApplicationSettings settings; |
| 19 | + QDir dir(settings.resultsFolder); |
| 20 | + dir.setSorting(QDir::Name); |
| 21 | + dir.setNameFilters(QStringList() << "*.xml"); |
| 22 | + dir.setFilter(QDir::Files | QDir::NoDotAndDotDot); |
| 23 | + foreach(const QFileInfo fileinfo, dir.entryInfoList()) { |
| 24 | + const QString filename = fileinfo.canonicalFilePath(); |
| 25 | + QFile file(filename); |
| 26 | + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) |
| 27 | + continue; |
| 28 | + |
| 29 | + QDomDocument doc; |
| 30 | + if (!doc.setContent(&file)) |
| 31 | + continue; |
| 32 | + |
| 33 | + const QDomElement rootElement = doc.documentElement(); |
| 34 | + if (rootElement.tagName() != "results") |
| 35 | + continue; |
| 36 | + |
| 37 | + const QDomElement metaElement = rootElement.firstChildElement("meta"); |
| 38 | + if (metaElement.isNull()) |
| 39 | + continue; |
| 40 | + |
| 41 | + const QDomElement projectElement = metaElement.firstChildElement("project"); |
| 42 | + if (projectElement.text() != projectName) |
| 43 | + continue; |
| 44 | + |
| 45 | + int results = 0; |
| 46 | + for (QDomElement child = rootElement.firstChildElement("results"); !child.isNull(); child = child.nextSiblingElement()) |
| 47 | + ++results; |
| 48 | + values.append(results); |
| 49 | + datetimes.append(fileinfo.baseName().mid(0,10)); |
| 50 | + } |
| 51 | + |
| 52 | + if (isVisible()) |
| 53 | + update(); |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +static void drawLineGraph(QPainter *painter, int left, int top, int width, int height, const int values[], int numberOfValues) |
| 58 | +{ |
| 59 | + int maxvalue = 0; |
| 60 | + for (int i = 0; i < numberOfValues; i++) { |
| 61 | + if (values[i] > maxvalue) |
| 62 | + maxvalue = values[i]; |
| 63 | + } |
| 64 | + |
| 65 | + painter->setPen(Qt::gray); |
| 66 | + painter->setBrush(Qt::white); |
| 67 | + painter->drawRect(left,top,width,height); |
| 68 | + |
| 69 | + const int bottom = top + height; |
| 70 | + const int w = width - 2; |
| 71 | + const int h = height - 2; |
| 72 | + |
| 73 | + painter->setPen(Qt::red); |
| 74 | + for (int i = 1; i < numberOfValues; i++) { |
| 75 | + painter->drawLine(left + 1 + (i-1) * w / (numberOfValues - 1), |
| 76 | + bottom - 1 - (h * values[i-1]) / maxvalue, |
| 77 | + left + 1 + i * w / (numberOfValues - 1), |
| 78 | + bottom - 1 - (h * values[i]) / maxvalue); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | + |
| 83 | +void Graph::paintEvent(QPaintEvent * /*event*/) |
| 84 | +{ |
| 85 | + QPainter painter(this); |
| 86 | + painter.fillRect(rect(),Qt::white); |
| 87 | + drawLineGraph(&painter,50,50,width()-100,height()-100,&values[0],values.size()); |
| 88 | +} |
| 89 | + |
0 commit comments