-
Notifications
You must be signed in to change notification settings - Fork 2
/
exchangeui.cpp
151 lines (129 loc) · 4.94 KB
/
exchangeui.cpp
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
/*
* Copyright (C) 2019 Alessandro Arrabito
*/
/*
* This file is part of QtExchangeDemo.
*
* QtExchangeDemo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QtExchangeDemo is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QtExchangeDemo. If not, see <http://www.gnu.org/licenses/>.
*/
#include "exchangeui.h"
#include "exchangemessages.h"
#include <QtCharts>
#include "config.h"
const int minWidth = 800;
const int minHeight = 600;
const int graph_time_window_secs = 60;
ExchangeUI::ExchangeUI(QSharedPointer<ExchangeClient> _client)
: QMainWindow(nullptr), client(_client)
{
QChart *chart = new QChart();
QLineSeries *series = new QLineSeries();
chart->legend()->hide();
chart->addSeries(series);
chart->setTitle("Exchange Ethereum - Euro");
QChartView *chartView = new QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
chartView->setUpdatesEnabled(true);
QValueAxis *axisX = new QValueAxis();
QValueAxis *axisY = new QValueAxis();
chart->addAxis(axisX,Qt::AlignBottom);
chart->addAxis(axisY,Qt::AlignLeft);
series->attachAxis(axisX);
series->attachAxis(axisY);
axisX->setTickCount(5);
axisX->setRange(0, 0);
axisY->setRange(0, 0);
connect(client.get(), &ExchangeClient::data_fetched, this, [series, axisX, axisY](double val){
// handle chart updates
time_t xval = time(nullptr);
series->append(xval, val);
//update vertical size of graph
static double min = val;
static double max = val;
min = std::min(val,min);
max = std::max(val,max);
if(max > axisY->max())
axisY->setMax(max+10);
if(min > axisY->min())
axisY->setMin(min-10);
// initialize time range
static QDateTime start = QDateTime::currentDateTime();
static QDateTime end = QDateTime::currentDateTime().addSecs(graph_time_window_secs);
if(axisX->max() < end.toTime_t() )
{
axisX->setRange(start.toTime_t(), end.toTime_t());
}
// update time range
if(xval > end.toTime_t())
{
start = start.addSecs(graph_time_window_secs/2);
end = end.addSecs(graph_time_window_secs/2);
axisX->setRange(start.toTime_t(), end.toTime_t());
}
});
// Setup of UI objects
QVBoxLayout *layout = new QVBoxLayout;
// refresh setting widget
QHBoxLayout *layout_refresh = new QHBoxLayout;
QLabel *label_refresh = new QLabel("refresh time ms.");
QLineEdit *edit_refresh = new QLineEdit(QString::number(default_refresh_interval));
layout_refresh->addWidget(label_refresh);
layout_refresh->addWidget(edit_refresh);
// url setting widget
QHBoxLayout *layout_url = new QHBoxLayout;
QLabel *label_url = new QLabel("exchange provider");
QLineEdit *edit_url = new QLineEdit(default_provider_url);
layout_url->addWidget(label_url);
layout_url->addWidget(edit_url);
// field setting widget
QHBoxLayout *layout_field = new QHBoxLayout;
QLabel *label_field = new QLabel("exchange field");
QLineEdit *edit_field = new QLineEdit(default_provider_field);
layout_field->addWidget(label_field);
layout_field->addWidget(edit_field);
// setup layout
layout->addWidget(chartView);
layout->addItem(layout_refresh);
layout->addItem(layout_url);
layout->addItem(layout_field);
// button update parameters
QPushButton *update_params = new QPushButton("Update Parameters");
connect(update_params, &QPushButton::clicked, this, [this, edit_url, edit_field, edit_refresh](){
client->setProviderUrl(edit_url->text());
client->setProviderField(edit_field->text());
client->setRefreshInterval(edit_refresh->text().toInt());
client->setParameter();
});
layout->addWidget(update_params);
// button activate fetch of exchange data
QPushButton *activate_fetch = new QPushButton("Activate Fetch");
static bool fetch_is_active = false;
connect(activate_fetch, &QPushButton::clicked, this, [this, update_params]()
{
fetch_is_active = !fetch_is_active;
update_params->setEnabled(!fetch_is_active);
client->setActivateStatus(fetch_is_active);
});
layout->addWidget(activate_fetch);
layout->addWidget(update_params);
//setCentralWidget(layout);
QWidget *window = new QWidget;
window->setLayout(layout);
setCentralWidget(window);
setMinimumWidth(minWidth);
setMinimumHeight(minHeight);
}
ExchangeUI::~ExchangeUI()
{
}