21 |
21 |
22 ChartiSpindel::ChartiSpindel(QString code, QString name, QWidget *parent) : QDialog(parent) |
22 ChartiSpindel::ChartiSpindel(QString code, QString name, QWidget *parent) : QDialog(parent) |
23 { |
23 { |
24 QSqlQuery query; |
24 QSqlQuery query; |
25 double timestamp; |
25 double timestamp; |
|
26 double sg_min = 1.2, sg_max = 0.8, sg; |
|
27 double temp_min = 100, temp_max = 0, temp; |
|
28 double batt_min = 4.5, batt_max = 3.0, batt; |
26 |
29 |
27 qDebug() << "ChartiSpindel:" << code << name; |
30 qDebug() << "ChartiSpindel:" << code << name; |
28 |
31 |
29 QDialog* dialog = new QDialog(parent); |
32 QDialog* dialog = new QDialog(parent); |
30 dialog->setWindowTitle(tr("BMSapp - iSpindel ") + "\"" + name + "\""); |
33 dialog->setWindowTitle(tr("BMSapp - iSpindel ") + "\"" + name + "\""); |
51 query.prepare("SELECT * FROM log_ispindel WHERE code=:code ORDER BY datetime"); |
54 query.prepare("SELECT * FROM log_ispindel WHERE code=:code ORDER BY datetime"); |
52 query.bindValue(":code", code); |
55 query.bindValue(":code", code); |
53 query.exec(); |
56 query.exec(); |
54 while (query.next()) { |
57 while (query.next()) { |
55 timestamp = query.value("datetime").toDateTime().toSecsSinceEpoch() * 1000; |
58 timestamp = query.value("datetime").toDateTime().toSecsSinceEpoch() * 1000; |
56 temperature->append(timestamp, query.value("temperature").toDouble()); |
59 |
57 density->append(timestamp, query.value("sg").toDouble()); |
60 sg = query.value("sg").toDouble(); |
58 battery ->append(timestamp, round(query.value("battery").toDouble() * 500) / 500); |
61 if ((ceil(sg * 100) / 100) > sg_max) |
|
62 sg_max = ceil(sg * 100) / 100; |
|
63 if ((floor(sg * 100) / 100) < sg_min) |
|
64 sg_min = floor(sg * 100) / 100; |
|
65 |
|
66 temp = query.value("temperature").toDouble(); |
|
67 if (ceil(temp) > temp_max) |
|
68 temp_max = ceil(temp); |
|
69 if (floor(temp) < temp_min) |
|
70 temp_min = floor(temp); |
|
71 |
|
72 batt = round(query.value("battery").toDouble() * 500) / 500; |
|
73 if ((ceil(batt * 10) / 10) > batt_max) |
|
74 batt_max = ceil(batt * 10) / 10; |
|
75 if ((floor(batt * 10) / 10) < batt_min) |
|
76 batt_min = floor(batt * 10) / 10; |
|
77 |
|
78 temperature->append(timestamp, temp); |
|
79 density->append(timestamp, sg); |
|
80 battery ->append(timestamp, batt); |
59 } |
81 } |
60 |
82 |
61 temperature->setName(tr("Temperature")); |
83 temperature->setName(tr("Temperature")); |
62 temperature->setColor(QColorConstants::Svg::red); |
84 temperature->setColor(QColorConstants::Svg::red); |
63 density->setName(tr("SG")); |
85 density->setName(tr("SG")); |
82 battery->attachAxis(axisX); |
104 battery->attachAxis(axisX); |
83 temperature->attachAxis(axisX); |
105 temperature->attachAxis(axisX); |
84 density->attachAxis(axisX); |
106 density->attachAxis(axisX); |
85 |
107 |
86 QValueAxis *axisYT = new QValueAxis; |
108 QValueAxis *axisYT = new QValueAxis; |
|
109 axisYT->setRange(temp_min, temp_max); |
87 axisYT->setTickCount(10); |
110 axisYT->setTickCount(10); |
88 axisYT->setLabelFormat("%.1f"); |
111 axisYT->setLabelFormat("%.1f"); |
89 axisYT->setTitleText(tr("Temperature °C")); |
112 axisYT->setTitleText(tr("Temperature °C")); |
90 axisYT->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); |
113 axisYT->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); |
91 chart->addAxis(axisYT, Qt::AlignRight); |
114 chart->addAxis(axisYT, Qt::AlignRight); |
92 temperature->attachAxis(axisYT); |
115 temperature->attachAxis(axisYT); |
93 |
116 |
94 QValueAxis *axisYD = new QValueAxis; |
117 QValueAxis *axisYD = new QValueAxis; |
|
118 axisYD->setRange(sg_min, sg_max); |
95 axisYD->setTickCount(10); |
119 axisYD->setTickCount(10); |
96 axisYD->setLabelFormat("%.3f"); |
120 axisYD->setLabelFormat("%.3f"); |
97 axisYD->setTitleText("SG"); |
121 axisYD->setTitleText("SG"); |
98 axisYD->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); |
122 axisYD->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); |
99 chart->addAxis(axisYD, Qt::AlignLeft); |
123 chart->addAxis(axisYD, Qt::AlignLeft); |
100 density->attachAxis(axisYD); |
124 density->attachAxis(axisYD); |
101 |
125 |
102 QValueAxis *axisYB = new QValueAxis; |
126 QValueAxis *axisYB = new QValueAxis; |
|
127 axisYB->setRange(batt_min, batt_max); |
103 axisYB->setTickCount(10); |
128 axisYB->setTickCount(10); |
104 axisYB->setLabelFormat("%.2f"); |
129 axisYB->setLabelFormat("%.2f"); |
105 axisYB->setTitleText(tr("Battery volt")); |
130 axisYB->setTitleText(tr("Battery volt")); |
106 axisYB->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); |
131 axisYB->setLabelsFont(QFont("Helvetica", 8, QFont::Normal)); |
107 chart->addAxis(axisYB, Qt::AlignRight); |
132 chart->addAxis(axisYB, Qt::AlignRight); |
129 ChartiSpindel::~ChartiSpindel() {} |
154 ChartiSpindel::~ChartiSpindel() {} |
130 |
155 |
131 |
156 |
132 void ChartiSpindel::savePNG() |
157 void ChartiSpindel::savePNG() |
133 { |
158 { |
134 QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), QDir::homePath() + "/ispindel.png", tr("Image (*.png)")); |
159 QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp"); |
|
160 QString dirName; |
|
161 |
|
162 /* |
|
163 * First check if the directory stored in the settings file exists. |
|
164 * It might be on a removable media that was last used ... |
|
165 * If so, fallback to the user's home directory. |
|
166 */ |
|
167 dirName = settings.value("paths/download").toString(); |
|
168 if (! QDir(dirName).exists()) { |
|
169 dirName = QDir::homePath(); |
|
170 } |
|
171 |
|
172 QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), dirName + "/ispindel.png", tr("Image (*.png)")); |
135 if (path.isEmpty()) { |
173 if (path.isEmpty()) { |
136 QMessageBox::warning(this, tr("Save File"), tr("No image file selected.")); |
174 QMessageBox::warning(this, tr("Save File"), tr("No image file selected.")); |
137 return; |
175 return; |
138 } |
176 } |
|
177 |
|
178 /* |
|
179 * Update to current selected path |
|
180 */ |
|
181 settings.setValue("paths/download", QFileInfo(path).absolutePath()); |
139 |
182 |
140 QImage img((chartView->size()), QImage::Format_ARGB32); |
183 QImage img((chartView->size()), QImage::Format_ARGB32); |
141 QPainter painter; |
184 QPainter painter; |
142 painter.begin(&img); |
185 painter.begin(&img); |
143 chartView->render(&painter); |
186 chartView->render(&painter); |
159 t_tooltip->setSeries(series); |
202 t_tooltip->setSeries(series); |
160 if (series == temperature) |
203 if (series == temperature) |
161 t_tooltip->setText(QString(tr("%1\nTemperature %2°C")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 2, 'f', 1)); |
204 t_tooltip->setText(QString(tr("%1\nTemperature %2°C")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 2, 'f', 1)); |
162 else if (series == density) |
205 else if (series == density) |
163 t_tooltip->setText(QString(tr("%1\nDensity %2 SG")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 5, 'f', 4)); |
206 t_tooltip->setText(QString(tr("%1\nDensity %2 SG")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 5, 'f', 4)); |
164 else if (series == battery) |
207 else if (series == battery) { |
165 t_tooltip->setText(QString(tr("%1\nBattery %2 volt")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 3, 'f', 2)); |
208 double batt = point.y() - 3.064; /* 0% */ |
|
209 if (batt < 0) |
|
210 batt = 0; |
|
211 batt = round(batt / 1.17875 * 1000.0) / 10; /* 100% range */ |
|
212 if (batt > 100) |
|
213 batt = 100; |
|
214 t_tooltip->setText(QString(tr("%1\nBattery %2 volt\nCapacity %3%")).arg(timeis.toString("dd-MM-yyyy hh:mm")) |
|
215 .arg(point.y(), 3, 'f', 2).arg(batt, 2, 'f', 1, '0')); |
|
216 } |
166 t_tooltip->setAnchor(point); |
217 t_tooltip->setAnchor(point); |
167 t_tooltip->setZValue(11); |
218 t_tooltip->setZValue(11); |
168 t_tooltip->updateGeometry(); |
219 t_tooltip->updateGeometry(); |
169 t_tooltip->show(); |
220 t_tooltip->show(); |
170 } else { |
221 } else { |