forked from danmar/cppcheck
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.cpp
More file actions
160 lines (129 loc) · 4.73 KB
/
graph.cpp
File metadata and controls
160 lines (129 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "graph.h"
#include "applicationsettings.h"
#include <QDir>
#include <QDomDocument>
#include <QDomElement>
#include <QPainter>
Graph::Graph(QWidget *parent) :
QWidget(parent)
{
}
static QString getstr(const QDomElement element, const QString &tagName)
{
const QDomElement child = element.firstChildElement(tagName);
return child.isNull() ? QString() : child.text();
}
void Graph::diff(const QString &projectName)
{
values.clear();
datetimes.clear();
ApplicationSettings settings;
QDir dir(settings.resultsFolder);
dir.setSorting(QDir::Name);
dir.setNameFilters(QStringList() << "*.xml");
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
QSet<QString> previousResults;
bool first = true;
foreach(const QFileInfo fileinfo, dir.entryInfoList()) {
const QString filename = fileinfo.canonicalFilePath();
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QDomDocument doc;
if (!doc.setContent(&file))
continue;
const QDomElement rootElement = doc.documentElement();
if (rootElement.tagName() != "results")
continue;
const QDomElement metaElement = rootElement.firstChildElement("meta");
if (metaElement.isNull())
continue;
const QDomElement projectElement = metaElement.firstChildElement("project");
if (projectElement.text() != projectName)
continue;
int added=0;
QSet<QString> results;
for (QDomElement child = rootElement.firstChildElement("results"); !child.isNull(); child = child.nextSiblingElement()) {
const QString str = getstr(child,"id") + "," + getstr(child,"line") + "," + getstr(child,"file");
results.insert(str);
if (!first) {
if (!previousResults.contains(str))
added++;
}
}
previousResults = results;
values.append(added);
datetimes.append(fileinfo.baseName().mid(0,10));
first = false;
}
if (isVisible())
update();
}
void Graph::trend(const QString &projectName)
{
values.clear();
datetimes.clear();
ApplicationSettings settings;
QDir dir(settings.resultsFolder);
dir.setSorting(QDir::Name);
dir.setNameFilters(QStringList() << "*.xml");
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
foreach(const QFileInfo fileinfo, dir.entryInfoList()) {
const QString filename = fileinfo.canonicalFilePath();
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
continue;
QDomDocument doc;
if (!doc.setContent(&file))
continue;
const QDomElement rootElement = doc.documentElement();
if (rootElement.tagName() != "results")
continue;
const QDomElement metaElement = rootElement.firstChildElement("meta");
if (metaElement.isNull())
continue;
const QDomElement projectElement = metaElement.firstChildElement("project");
if (projectElement.text() != projectName)
continue;
int results = 0;
for (QDomElement child = rootElement.firstChildElement("results"); !child.isNull(); child = child.nextSiblingElement())
++results;
values.append(results);
datetimes.append(fileinfo.baseName().mid(0,10));
}
if (isVisible())
update();
}
static void drawLineGraph(QPainter *painter, int left, int top, int width, int height, const QVector<int> &values)
{
const int numberOfValues = values.size();
int maxvalue = 0;
for (int i = 0; i < numberOfValues; i++) {
if (values[i] > maxvalue)
maxvalue = values[i];
}
painter->setPen(Qt::gray);
painter->setBrush(Qt::white);
painter->drawRect(left,top,width,height);
if (maxvalue == 0)
return;
const int bottom = top + height;
const int w = width - 2;
const int h = height - 2;
painter->setPen(Qt::red);
for (int i = 1; i < numberOfValues; i++) {
painter->drawLine(left + 1 + (i-1) * w / (numberOfValues - 1),
bottom - 1 - (h * values[i-1]) / maxvalue,
left + 1 + i * w / (numberOfValues - 1),
bottom - 1 - (h * values[i]) / maxvalue);
}
}
void Graph::paintEvent(QPaintEvent * /*event*/)
{
QPainter painter(this);
painter.fillRect(rect(),Qt::white);
drawLineGraph(&painter,50,50,width()-100,height()-100,values);
painter.setPen(Qt::black);
painter.drawText(QRect(50,height()-40,100,20), Qt::AlignLeft, datetimes.front());
painter.drawText(QRect(width()-150,height()-40,100,20), Qt::AlignRight, datetimes.back());
}