Skip to content

Commit 00aae81

Browse files
committed
GUI2: add graph class
1 parent 1aacd27 commit 00aae81

7 files changed

Lines changed: 141 additions & 4 deletions

File tree

‎gui2/graph.cpp‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+

‎gui2/graph.h‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef GRAPH_H
2+
#define GRAPH_H
3+
4+
#include <QPaintEvent>
5+
#include <QString>
6+
#include <QVector>
7+
#include <QWidget>
8+
9+
class Graph : public QWidget {
10+
Q_OBJECT
11+
public:
12+
explicit Graph(QWidget *parent = 0);
13+
void trend(const QString &projectName);
14+
15+
protected:
16+
void paintEvent(QPaintEvent *event);
17+
18+
private:
19+
QVector<int> values;
20+
QVector<QString> datetimes;
21+
22+
};
23+
24+
#endif // GRAPH_H

‎gui2/gui2.pro‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ SOURCES += main.cpp\
2020
projectwidget.cpp \
2121
resultsform.cpp \
2222
projectlist.cpp \
23-
codebrowser.cpp
23+
codebrowser.cpp \
24+
graph.cpp
2425

2526
HEADERS += mainwindow.h \
2627
settingsdialog.h \
@@ -30,7 +31,8 @@ HEADERS += mainwindow.h \
3031
projectwidget.h \
3132
resultsform.h \
3233
projectlist.h \
33-
codebrowser.h
34+
codebrowser.h \
35+
graph.h
3436

3537
FORMS += mainwindow.ui \
3638
settingsdialog.ui \

‎gui2/mainwindow.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "mainwindow.h"
22
#include "applicationsettings.h"
33
#include "configureprojects.h"
4+
#include "graph.h"
45
#include "resultsform.h"
56
#include "settingsdialog.h"
67
#include "ui_mainwindow.h"
@@ -16,6 +17,7 @@ MainWindow::MainWindow(QWidget *parent) :
1617

1718
connect(ui->projectwidget, SIGNAL(scan()), this, SLOT(scan()));
1819
connect(ui->projectwidget, SIGNAL(log()), this, SLOT(log()));
20+
connect(ui->projectwidget, SIGNAL(trend()), this, SLOT(trend()));
1921

2022
// TODO: right now we don't show anything on the status bar
2123
this->statusBar()->hide();
@@ -36,6 +38,10 @@ MainWindow::MainWindow(QWidget *parent) :
3638
resultsForm = new ResultsForm(this);
3739
resultsForm->hide();
3840
layout->addWidget(resultsForm);
41+
42+
graph = new Graph(this);
43+
graph->hide();
44+
layout->addWidget(graph);
3945
}
4046

4147
MainWindow::~MainWindow()
@@ -78,6 +84,7 @@ void MainWindow::scan()
7884
{
7985
const ProjectList::Project *project = projectList.getproject(ui->projectwidget->getProjectName());
8086
if (project) {
87+
graph->hide();
8188
resultsForm->show();
8289
resultsForm->scan(*project);
8390
}
@@ -87,7 +94,15 @@ void MainWindow::log()
8794
{
8895
const ProjectList::Project *project = projectList.getproject(ui->projectwidget->getProjectName());
8996
if (project) {
97+
graph->hide();
9098
resultsForm->show();
9199
resultsForm->showResults(*project);
92100
}
93101
}
102+
103+
void MainWindow::trend()
104+
{
105+
graph->trend(ui->projectwidget->getProjectName());
106+
resultsForm->hide();
107+
graph->show();
108+
}

‎gui2/mainwindow.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace Ui {
99
class MainWindow;
1010
}
1111

12+
class Graph;
1213
class ResultsForm;
1314

1415
class MainWindow : public QMainWindow {
@@ -26,11 +27,13 @@ private slots:
2627
// Project widget..
2728
void scan();
2829
void log();
30+
void trend();
2931

3032
private:
3133
Ui::MainWindow *ui;
32-
ProjectList projectList;
33-
ResultsForm *resultsForm;
34+
ProjectList projectList;
35+
ResultsForm *resultsForm;
36+
Graph *graph;
3437
};
3538

3639
#endif // MAINWINDOW_H

‎gui2/projectwidget.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ void ProjectWidget::mousePressEvent(QMouseEvent *event)
144144
{
145145
int button = (width() - event->pos().x()) / (distance + buttonSize);
146146

147+
if (button == 0)
148+
emit(trend());
149+
147150
if (button == 2)
148151
emit(log());
149152

‎gui2/projectwidget.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class ProjectWidget : public QWidget {
1616
signals:
1717
void scan();
1818
void log();
19+
void trend();
1920

2021
private:
2122
void paintEvent(QPaintEvent *event);

0 commit comments

Comments
 (0)