src/ChartiSpindel.cpp

changeset 494
49ac23d25f61
parent 438
e06b04ef1579
child 498
c6f957fa7442
--- a/src/ChartiSpindel.cpp	Sat Feb 11 15:48:02 2023 +0100
+++ b/src/ChartiSpindel.cpp	Sun Feb 12 13:58:36 2023 +0100
@@ -23,6 +23,9 @@
 {
     QSqlQuery query;
     double timestamp;
+    double sg_min = 1.2, sg_max = 0.8, sg;
+    double temp_min = 100, temp_max = 0, temp;
+    double batt_min = 4.5, batt_max = 3.0, batt;
 
     qDebug() << "ChartiSpindel:" << code << name;
 
@@ -53,9 +56,28 @@
     query.exec();
     while (query.next()) {
         timestamp = query.value("datetime").toDateTime().toSecsSinceEpoch() * 1000;
-        temperature->append(timestamp, query.value("temperature").toDouble());
-        density->append(timestamp, query.value("sg").toDouble());
-        battery ->append(timestamp, round(query.value("battery").toDouble() * 500) / 500);
+
+	sg = query.value("sg").toDouble();
+	if ((ceil(sg * 100) / 100) > sg_max)
+	    sg_max = ceil(sg * 100) / 100;
+	if ((floor(sg * 100) / 100) < sg_min)
+	    sg_min = floor(sg * 100) / 100;
+
+	temp = query.value("temperature").toDouble();
+	if (ceil(temp) > temp_max)
+	    temp_max = ceil(temp);
+	if (floor(temp) < temp_min)
+	    temp_min = floor(temp);
+
+	batt = round(query.value("battery").toDouble() * 500) / 500;
+	if ((ceil(batt * 10) / 10) > batt_max)
+	    batt_max = ceil(batt * 10) / 10;
+	if ((floor(batt * 10) / 10) < batt_min)
+	    batt_min = floor(batt * 10) / 10;
+
+        temperature->append(timestamp, temp);
+        density->append(timestamp, sg);
+        battery ->append(timestamp, batt);
     }
 
     temperature->setName(tr("Temperature"));
@@ -84,6 +106,7 @@
     density->attachAxis(axisX);
 
     QValueAxis *axisYT = new QValueAxis;
+    axisYT->setRange(temp_min, temp_max);
     axisYT->setTickCount(10);
     axisYT->setLabelFormat("%.1f");
     axisYT->setTitleText(tr("Temperature °C"));
@@ -92,6 +115,7 @@
     temperature->attachAxis(axisYT);
 
     QValueAxis *axisYD = new QValueAxis;
+    axisYD->setRange(sg_min, sg_max);
     axisYD->setTickCount(10);
     axisYD->setLabelFormat("%.3f");
     axisYD->setTitleText("SG");
@@ -100,6 +124,7 @@
     density->attachAxis(axisYD);
 
     QValueAxis *axisYB = new QValueAxis;
+    axisYB->setRange(batt_min, batt_max);
     axisYB->setTickCount(10);
     axisYB->setLabelFormat("%.2f");
     axisYB->setTitleText(tr("Battery volt"));
@@ -131,12 +156,30 @@
 
 void ChartiSpindel::savePNG()
 {
-    QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), QDir::homePath() + "/ispindel.png", tr("Image (*.png)"));
+    QSettings settings(QSettings::IniFormat, QSettings::UserScope, "mbse", "bmsapp");
+    QString dirName;
+
+    /*
+     * First check if the directory stored in the settings file exists.
+     * It might be on a removable media that was last used ...
+     * If so, fallback to the user's home directory.
+     */
+    dirName = settings.value("paths/download").toString();
+    if (! QDir(dirName).exists()) {
+        dirName = QDir::homePath();
+    }
+
+    QString path = QFileDialog::getSaveFileName(this, tr("Save Image"), dirName + "/ispindel.png", tr("Image (*.png)"));
     if (path.isEmpty()) {
 	QMessageBox::warning(this, tr("Save File"), tr("No image file selected."));
 	return;
     }
 
+    /*
+     * Update to current selected path
+     */
+    settings.setValue("paths/download", QFileInfo(path).absolutePath());
+
     QImage img((chartView->size()), QImage::Format_ARGB32);
     QPainter painter;
     painter.begin(&img);
@@ -161,8 +204,16 @@
 	    t_tooltip->setText(QString(tr("%1\nTemperature %2°C")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 2, 'f', 1));
 	else if (series == density)
 	    t_tooltip->setText(QString(tr("%1\nDensity %2 SG")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 5, 'f', 4));
-	else if (series == battery)
-	    t_tooltip->setText(QString(tr("%1\nBattery %2 volt")).arg(timeis.toString("dd-MM-yyyy hh:mm")).arg(point.y(), 3, 'f', 2));
+	else if (series == battery) {
+	    double batt = point.y() - 3.064;	/* 0% */
+	    if (batt < 0)
+		batt = 0;
+	    batt = round(batt / 1.17875 * 1000.0) / 10;	/* 100% range */
+	    if (batt > 100)
+		batt = 100;
+	    t_tooltip->setText(QString(tr("%1\nBattery %2 volt\nCapacity %3%")).arg(timeis.toString("dd-MM-yyyy hh:mm"))
+			    .arg(point.y(), 3, 'f', 2).arg(batt, 2, 'f', 1, '0'));
+	}
 	t_tooltip->setAnchor(point);
 	t_tooltip->setZValue(11);
 	t_tooltip->updateGeometry();

mercurial