src/CalibrateiSpindel.cpp

changeset 510
080524ab2fe8
parent 509
af4a8649245e
child 511
c6fa5be47634
--- a/src/CalibrateiSpindel.cpp	Sun Oct 15 13:30:24 2023 +0200
+++ b/src/CalibrateiSpindel.cpp	Sun Oct 15 17:37:08 2023 +0200
@@ -57,6 +57,7 @@
 	_node = query.value("node").toString();
 	_alias = query.value("alias").toString();
 	ui->nameEdit->setText(_node+"/"+_alias);
+	qDebug() << query.value("calibrate").toString();
 
 	QJsonParseError parseError;
         const auto& json = query.value("calibrate").toString();
@@ -83,9 +84,12 @@
 		oldtotal = 0;
 		for (int i = 0; i < calData.size(); i++) {
 		    QJsonObject calObj = calData.at(i).toObject();
-		    oCal[i].plato = nCal[i].plato = calObj["plato"].toDouble();
-		    oCal[i].angle = nCal[i].angle = calObj["angle"].toDouble();
-		    oCal[i].sg = nCal[i].sg = Utils::plato_to_sg(oCal[i].plato);
+		    Calibrate c;
+		    c.plato = calObj["plato"].toDouble();
+		    c.angle = calObj["angle"].toDouble();
+		    c.sg = Utils::plato_to_sg(c.plato);
+		    nCal.append(c);
+		    oCal.append(c);
 		    oldtotal++;
 		}
 		newtotal = oldtotal;
@@ -129,6 +133,10 @@
 	x[i] = y[i] = 0;
     }
 
+    std::sort(nCal.begin() , nCal.end(), [=]( const Calibrate& test1 , const Calibrate& test2 )->bool {
+	return test2.angle < test1.angle;
+    });
+
     for (int i = 0; i < newtotal; i++) {
 	qDebug() << i << nCal[i].sg << nCal[i].plato << nCal[i].angle;
 
@@ -174,6 +182,7 @@
     this->textIsChanged = (_data_old.compare(_data_new) == 0) ? false:true;
     qDebug() << "changed" << this->textIsChanged << _data_old.compare(_data_new);
     CalibrateiSpindel::WindowTitle();
+    ui->saveButton->setEnabled(this->textIsChanged);
 
     new_plot = new QLineSeries();
     old_plot = new QLineSeries();
@@ -228,6 +237,20 @@
 
 void CalibrateiSpindel::on_quitButton_clicked()
 {
+    if (this->textIsChanged) {
+        int rc = QMessageBox::warning(this, tr("iSpindel calibrate changed"), tr("The calibration data has been modified. Save changes?"),
+                                QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
+        switch (rc) {
+            case QMessageBox::Save:
+                        on_saveButton_clicked();
+                        break;  /* Saved and then Quit */
+            case QMessageBox::Discard:
+                        break;  /* Quit without Save */
+            case QMessageBox::Cancel:
+                        return; /* Return to the editor page */
+        }
+    }
+
     this->close();
     this->setResult(1);
 }
@@ -235,6 +258,42 @@
 
 void CalibrateiSpindel::on_saveButton_clicked()
 {
+    QSqlQuery query;
+
+    if (this->textIsChanged) {
+	/*
+	 * Build json data
+	 */
+	QJsonObject calObj;
+	QJsonArray coeff, poly;
+
+	for (int i = 0; i < 4; i++) {
+	    coeff.append(New[i]);
+	}
+	calObj.insert("polyData", coeff);
+	for (int i = 0; i < newtotal; i++) {
+	    QJsonObject obj;
+	    obj.insert("plato", nCal[i].plato);
+	    obj.insert("angle", nCal[i].angle);
+	    poly.append(obj);
+	}
+	calObj.insert("calData", poly);
+
+	query.prepare("UPDATE mon_ispindels SET calibrate=:calibrate WHERE record = :recno");
+	query.bindValue(":calibrate", QJsonDocument(calObj).toJson(QJsonDocument::Compact) );
+	query.bindValue(":recno", this->recno);
+	query.exec();
+	if (query.lastError().isValid()) {
+            qWarning() << "CalibrateiSpindel" << query.lastError();
+            QMessageBox::warning(this, tr("Database error"),
+                        tr("MySQL error: %1\n%2\n%3")
+                        .arg(query.lastError().nativeErrorCode())
+                        .arg(query.lastError().driverText())
+                        .arg(query.lastError().databaseText()));
+        } else {
+            qDebug() << "CalibrateiSpindel Saved";
+        }
+    }
 }
 
 

mercurial