-
Notifications
You must be signed in to change notification settings - Fork 0
/
themewidget.cpp
297 lines (253 loc) · 9.95 KB
/
themewidget.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "themewidget.h"
#include "ui_themewidget.h"
#include <QtCharts/QChartView>
#include <QtCharts/QPieSeries>
#include <QtCharts/QPieSlice>
#include <QtCharts/QAbstractBarSeries>
#include <QtCharts/QPercentBarSeries>
#include <QtCharts/QStackedBarSeries>
#include <QtCharts/QBarSeries>
#include <QtCharts/QBarSet>
#include <QtCharts/QLineSeries>
#include <QtCharts/QSplineSeries>
#include <QtCharts/QScatterSeries>
#include <QtCharts/QAreaSeries>
#include <QtCharts/QLegend>
#include <QtWidgets/QGridLayout>
#include <QtWidgets/QFormLayout>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QSpinBox>
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QGroupBox>
#include <QtWidgets/QLabel>
#include <QtCore/QRandomGenerator>
#include <QtCharts/QBarCategoryAxis>
#include <QtWidgets/QApplication>
#include <QtCharts/QValueAxis>
#include <QTimer>
#include <QtCharts>
#include <QtNetwork>
ThemeWidget::ThemeWidget(QWidget *parent) : QWidget(parent), m_ui(new Ui_ThemeWidgetForm)
{
// Timer, sodass alle paar sekunden / minuten das Wetter aktuallisiert wird (processGetWeather)
weatherTimer = new QTimer(this);
connect(weatherTimer, SIGNAL(timeout()), this, SLOT(processGetWeather()));
weatherTimer->start(0);
quoteTimer = new QTimer(this);
connect(quoteTimer, SIGNAL(timeout()), this, SLOT(processGetQuote()));
quoteTimer->start(0);
clockTimer = new QTimer(this);
connect(clockTimer, SIGNAL(timeout()), this, SLOT(processSetClock()));
clockTimer->start(0);
sensorTimer = new QTimer(this);
connect(sensorTimer, SIGNAL(timeout()), this, SLOT(processGetSensor()));
sensorTimer->start(0);
m_ui->setupUi(this);
// Set the colors from the light theme as default ones
QPalette pal = qApp->palette();
//pal.setColor(QPalette::Shadow, QRgb(0x0000FF));
qApp->setPalette(pal);
}
void ThemeWidget::initChart(){
QChartView *chartView;
chartView = new QChartView(createLineChart());
m_ui->gridLayout->addWidget(chartView, 1, 2);
m_charts << chartView;
}
ThemeWidget::~ThemeWidget()
{
delete m_ui;
}
void ThemeWidget::TestFunction(){
}
// LineWidget benutzt daten von "dataTable", hier wird "diese ge"dataTable gefüllt
DataTable ThemeWidget::initData()
{
DataTable dataTable;
DataList dataList;
qreal yValue(0);
m_valueMin=1000;
for (int j(0); j < weatherForecast.dataCount; j++) {
yValue = weatherForecast.data.at(j).temperature;
if(yValue > m_valueMax){
m_valueMax = yValue;
}
if(yValue < m_valueMin){
m_valueMin = yValue;
}
QPointF value(j*3,yValue);
QString label = "Slice " + QString::number(j);
//QString label = weatherForecast.data.at(j).time;
dataList << Data(value, label);
}
dataTable << dataList;
return dataTable;
}
QChart *ThemeWidget::createLineChart() const
{
QChart *chart = new QChart();
chart->setTitle("Line chart");
QString name("Temperature ");
int nameIndex = 0;
for (const DataList &list : m_dataTable) {
QLineSeries *series = new QLineSeries(chart);
for (const Data &data : list)
series->append(data.first);
series->setName(name);
nameIndex++;
chart->addSeries(series);
}
// TODO set Range mit int m_valueMax und m_valueCount;
chart->createDefaultAxes();
//chart->axisX()->setR
chart->axisX()->setRange(QVariant(0), QVariant(40*3));
chart->axisY()->setRange(m_valueMin - 3, m_valueMax + 3);
// Add space to label to add space between labels and axis
static_cast<QValueAxis *>(chart->axisY())->setLabelFormat("%.1f ");
return chart;
}
void ThemeWidget::updateUI()
{
QChart::ChartTheme theme = QChart::ChartThemeDark;
const auto charts = m_charts;
for (QChartView *chartView : charts) {
chartView->chart()->setTheme(theme);
}
QPalette pal = window()->palette();
pal.setColor(QPalette::Window, QRgb(0x121218));
pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
window()->setPalette(pal);
/*
if (!m_charts.isEmpty() && m_charts.at(0)->chart()->theme() != theme) {
for (QChartView *chartView : charts) {
chartView->chart()->setTheme(theme);
}
QPalette pal = window()->palette();
pal.setColor(QPalette::Window, QRgb(0x121218));
pal.setColor(QPalette::WindowText, QRgb(0xd6d6d6));
window()->setPalette(pal);
}
*/
// Set antialiasing
for (QChartView *chart : charts)
chart->setRenderHint(QPainter::Antialiasing, true);
// Set animation options
for (QChartView *chartView : charts)
chartView->chart()->setAnimationOptions(QChart::AllAnimations);
// Update legend alignment
if (!Qt::AlignBottom) {
for (QChartView *chartView : charts)
chartView->chart()->legend()->hide();
} else {
for (QChartView *chartView : charts) {
chartView->chart()->legend()->setAlignment(Qt::AlignBottom);
chartView->chart()->legend()->show();
}
}
}
// Sensoren auslesen
void ThemeWidget::processGetWeather(){
if (sensorTimer->interval() == 0){
}
sensorTimer->setInterval(6000);
}
// Uhr updaten
void ThemeWidget::processSetClock(){
clockTimer->setInterval(1000);
int hour = QTime::currentTime().hour();
QString hourString;
if(hour<10){
hourString = "0";
}
hourString += QString::number(hour);
int minute = QTime::currentTime().minute();
QString minuteString;
if(minute<10){
minuteString = "0";
}
minuteString += QString::number(minute);
int second = QTime::currentTime().second();
QString secondString;
if(second<10){
secondString = "0";
}
secondString += QString::number(second);
m_ui->clockLabel->setText(hourString +":"+minuteString+":"+secondString);
}
// Wetter-json downloaden
void ThemeWidget::processGetWeather(){
weatherTimer->setInterval(6000);
weatherManager = new QNetworkAccessManager(this);
connect(weatherManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(weatherReplyFinished(QNetworkReply*)));
QUrl url("http://api.openweathermap.org/data/2.5/forecast?id=2950159&APPID=85f8e39c1f41443aed236ccc25d0a34a&units=metric");
QNetworkRequest request(url);
weatherManager->get(request);
}
// json downloaden (benutz ich später)
void ThemeWidget::processGetQuote(){
quoteTimer->setInterval(86400000- QTime::currentTime().msecsSinceStartOfDay());
quoteManager = new QNetworkAccessManager(this);
connect(quoteManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(quoteReplyFinished(QNetworkReply*)));
QUrl url("http://taeglicheszit.at/zitat-api.php?format=json");
QNetworkRequest request(url);
quoteManager->get(request);
}
// json reply parsen (benutz ich später
void ThemeWidget::quoteReplyFinished(QNetworkReply *reply){
if(reply->error())
m_ui->quoteLabel->setText("ERROR!!");
else
{
QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object();
m_ui->quoteLabel->setText(jsonObject["zitat"].toString());
//m_ui->quoteLabel->setText("Dies ist ein sehr sehrsehr sehrsehr sehrsehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehr sehrsehr sehr langes Zitat.");
reply->deleteLater();
}
}
// json reply parsen
void ThemeWidget::weatherReplyFinished(QNetworkReply *reply){
if(reply->error()){
QLabel *outData = new QLabel("Error reading Weather");
outData->setStyleSheet("QLabel { color : white; }");
weatherLabelVector.clear();
weatherLabelVector.push_back(outData);
//weatherGrid->addWidget(weatherLabelVector.last(),0,0,1,1);
weatherManager->clearAccessCache();
}else{
QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object();
QJsonObject jasonObjectCity = jsonObject["city"].toObject();
weatherForecast.cityName = jasonObjectCity["name"].toString();
weatherForecast.dataCount = jsonObject["cnt"].toInt();
QJsonArray jsonArray = jsonObject["list"].toArray();
int counter = 0;
foreach (const QJsonValue & value, jsonArray){
QJsonObject jasonObjectList = value.toObject();
weatherdatapoint tmp;
tmp.timeUnixUtc = jasonObjectList["dt"].toInt();
tmp.time = jasonObjectList["dt_txt"].toString();
QJsonObject jasonObjectListMain = jasonObjectList["main"].toObject();
tmp.temperature = jasonObjectListMain["temp"].toDouble();
tmp.humidity = jasonObjectListMain["humidity"].toDouble();
QJsonArray QJsonArrayListWeather = jasonObjectList["weather"].toArray();
QJsonObject jasonObjectListWeather = QJsonArrayListWeather[0].toObject();
tmp.description = jasonObjectListWeather["description"].toString();
QJsonObject jasonObjectListClouds = jasonObjectList["clouds"].toObject();
tmp.clouds = jasonObjectListClouds["all"].toInt();
QJsonObject jasonObjectListWind = jasonObjectList["wind"].toObject();
tmp.windSpeed = jasonObjectListWind["speed"].toDouble();
tmp.windDirection = jasonObjectListWind["deg"].toInt();
weatherForecast.data.push_back(tmp);
QLabel *outData = new QLabel("data");
outData->setStyleSheet("QLabel { color : white; }");
outData->setText(tmp.time + " Description:"+tmp.description+" Temperature: "+QString::number(tmp.temperature)+"°C Humidity: "+QString::number(tmp.humidity)+"% clouds:"+QString::number(tmp.clouds)+"% Windspeed:"+QString::number(tmp.windSpeed)+"m/s Windirection"+QString::number(tmp.windDirection)+"°");
weatherLabelVector.push_back(outData);
//weatherGrid->addWidget(weatherLabelVector.last(),counter,0,1,1);
counter++;
}
weatherManager->clearAccessCache();
reply->deleteLater();
}
m_dataTable = initData();
initChart();
updateUI();
}