src/CalibrateiSpindel.cpp

changeset 506
ea07f6c97a69
parent 505
7ae4d022cf8f
child 507
fa07b6c6238a
equal deleted inserted replaced
505:7ae4d022cf8f 506:ea07f6c97a69
16 */ 16 */
17 #include "CalibrateiSpindel.h" 17 #include "CalibrateiSpindel.h"
18 #include "../ui/ui_CalibrateiSpindel.h" 18 #include "../ui/ui_CalibrateiSpindel.h"
19 #include "global.h" 19 #include "global.h"
20 #include "Utils.h" 20 #include "Utils.h"
21 #include "polyfit.h"
21 #include "MainWindow.h" 22 #include "MainWindow.h"
22 23
23 24
24 25
25 CalibrateiSpindel::CalibrateiSpindel(int id, QWidget *parent) : QDialog(parent), ui(new Ui::CalibrateiSpindel) 26 CalibrateiSpindel::CalibrateiSpindel(int id, QWidget *parent) : QDialog(parent), ui(new Ui::CalibrateiSpindel)
53 if (parseError.error != QJsonParseError::NoError) { 54 if (parseError.error != QJsonParseError::NoError) {
54 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; 55 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
55 } else { 56 } else {
56 57
57 QJsonObject jsonObj = jsonResponse.object(); 58 QJsonObject jsonObj = jsonResponse.object();
58 // qDebug() << "polyData: " << jsonObj["polyData"].toArray();
59 QJsonArray polyData = jsonObj.value("polyData").toArray(); 59 QJsonArray polyData = jsonObj.value("polyData").toArray();
60 // qDebug() << polyData;
61 for (int i = 0; i < polyData.size(); i++) { 60 for (int i = 0; i < polyData.size(); i++) {
62 Old[i] = New[i] = polyData.at(i).toDouble(); 61 Old[i] = New[i] = polyData.at(i).toDouble();
63 qDebug() << i << New[i]; 62 qDebug() << i << New[i];
64 } 63 }
65 _data_old = QString("(%1 * x^3) + (%2 * x^2) + (%3 * x) + %4").arg(Old[0], 0, 'f', 9, '0').arg(Old[1], 0, 'f', 9, '0').arg(Old[2], 0, 'f', 9, '0').arg(Old[3], 0, 'f', 9, '0'); 64 _data_old = QString("(%1 * x^3) + (%2 * x^2) + (%3 * x) + %4").arg(Old[0], 0, 'f', 9, '0').arg(Old[1], 0, 'f', 9, '0').arg(Old[2], 0, 'f', 9, '0').arg(Old[3], 0, 'f', 9, '0');
66 ui->oldEdit->setText(_data_old); 65 ui->oldEdit->setText(_data_old);
67 66
68 qDebug() << "calData: " << jsonObj["calData"].toArray(); 67 qDebug() << "calData: " << jsonObj["calData"].toArray();
69 QJsonArray calData = jsonObj.value("calData").toArray(); 68 QJsonArray calData = jsonObj.value("calData").toArray();
70 qDebug() << calData; 69 qDebug() << calData;
71 totaldata = 0; 70 oldtotal = 0;
72 for (int i = 0; i < calData.size(); i++) { 71 for (int i = 0; i < calData.size(); i++) {
73 QJsonObject calObj = calData.at(i).toObject(); 72 QJsonObject calObj = calData.at(i).toObject();
74 oCal[i].plato = nCal[i].plato = calObj["plato"].toDouble(); 73 oCal[i].plato = nCal[i].plato = calObj["plato"].toDouble();
75 oCal[i].angle = nCal[i].angle = calObj["angle"].toDouble(); 74 oCal[i].angle = nCal[i].angle = calObj["angle"].toDouble();
76 oCal[i].sg = nCal[i].sg = Utils::plato_to_sg(oCal[i].plato); 75 oCal[i].sg = nCal[i].sg = Utils::plato_to_sg(oCal[i].plato);
77 totaldata++; 76 oldtotal++;
78 } 77 }
79 78 newtotal = oldtotal;
80 } 79 }
81 } 80 }
82 81
83 } 82 }
84 83
91 void CalibrateiSpindel::refreshTable() 90 void CalibrateiSpindel::refreshTable()
92 { 91 {
93 QString w; 92 QString w;
94 QWidget* pWidget; 93 QWidget* pWidget;
95 QHBoxLayout* pLayout; 94 QHBoxLayout* pLayout;
96 double d; 95 double d, x[12], y[12];
97 96
98 qDebug() << "refreshTable" << totaldata; 97 qDebug() << "refreshTable" << oldtotal << newtotal;
99 98
100 /* 99 /*
101 * During filling the table turn off the cellChanged signal because every cell that is filled 100 * During filling the table turn off the cellChanged signal because every cell that is filled
102 * triggers the cellChanged signal. The QTableWidget has no better signal to use. 101 * triggers the cellChanged signal. The QTableWidget has no better signal to use.
103 */ 102 */
109 ui->dataTable->setColumnWidth(1, 100); /* °Plato */ 108 ui->dataTable->setColumnWidth(1, 100); /* °Plato */
110 ui->dataTable->setColumnWidth(2, 100); /* Tilt angle */ 109 ui->dataTable->setColumnWidth(2, 100); /* Tilt angle */
111 ui->dataTable->setColumnWidth(3, 55); /* Del button */ 110 ui->dataTable->setColumnWidth(3, 55); /* Del button */
112 ui->dataTable->setHorizontalHeaderLabels(labels); 111 ui->dataTable->setHorizontalHeaderLabels(labels);
113 ui->dataTable->verticalHeader()->hide(); 112 ui->dataTable->verticalHeader()->hide();
114 ui->dataTable->setRowCount(totaldata); 113 ui->dataTable->setRowCount(newtotal);
115 114
116 for (int i = 0; i < totaldata; i++) { 115 for (int i = 0; i < 12; i++) {
116 x[i] = y[i] = 0;
117 }
118
119 for (int i = 0; i < newtotal; i++) {
117 qDebug() << i << nCal[i].sg << nCal[i].plato << nCal[i].angle; 120 qDebug() << i << nCal[i].sg << nCal[i].plato << nCal[i].angle;
121
122 y[i] = nCal[i].plato;
123 x[i] = nCal[i].angle;
118 124
119 w = QString("%1").arg(nCal[i].sg, 1, 'f', 4, '0'); 125 w = QString("%1").arg(nCal[i].sg, 1, 'f', 4, '0');
120 QTableWidgetItem *item = new QTableWidgetItem(w); 126 QTableWidgetItem *item = new QTableWidgetItem(w);
121 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter); 127 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
122 ui->dataTable->setItem(i, 0, item); 128 ui->dataTable->setItem(i, 0, item);
141 pLayout->addWidget(btn_del); 147 pLayout->addWidget(btn_del);
142 pLayout->setContentsMargins(5, 0, 5, 0); 148 pLayout->setContentsMargins(5, 0, 5, 0);
143 pWidget->setLayout(pLayout); 149 pWidget->setLayout(pLayout);
144 ui->dataTable->setCellWidget(i, 3, pWidget); 150 ui->dataTable->setCellWidget(i, 3, pWidget);
145 } 151 }
152 int rc = Polyfit::polyfit(newtotal, x, y, 4, New);
153 qDebug() << "poly:" << rc << New[0] << New[1] << New[2] << New[3];
154
155 _data_new = QString("(%1 * x^3) + (%2 * x^2) + (%3 * x) + %4").arg(New[0], 0, 'f', 9, '0').arg(New[1], 0, 'f', 9, '0').arg(New[2], 0, 'f', 9, '0').arg(New[3], 0, 'f', 9, '0');
156 ui->newEdit->setText(_data_new);
157
158 /*
159 * Check the new formula against the old formula.
160 */
161 this->textIsChanged = (_data_old.compare(_data_new) == 0) ? false:true;
162 qDebug() << "changed" << this->textIsChanged << _data_old.compare(_data_new);
163 CalibrateiSpindel::WindowTitle();
164
165 new_plot = new QLineSeries();
166 old_plot = new QLineSeries();
167
168 for (int i = 0; i < oldtotal; i++) {
169 old_plot->append(oCal[i].angle, oCal[i].plato);
170 }
171 for (int i = 0; i < newtotal; i++) {
172 new_plot->append(nCal[i].angle, nCal[i].plato);
173 }
174
175 old_plot->setName(tr("Old"));
176 new_plot->setName(tr("New"));
177
178 chart = new QChart();
179 chart->setTitle(tr("Calibration plot"));
180 chart->addSeries(old_plot);
181 chart->addSeries(new_plot);
182
183 QValueAxis *axisX = new QValueAxis;
184 axisX->setRange(10, 80);
185 axisX->setTickCount(8);
186 axisX->setLabelFormat("%.0f");
187 axisX->setTitleText(tr("Angle"));
188 axisX->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
189 chart->addAxis(axisX, Qt::AlignBottom);
190 old_plot->attachAxis(axisX);
191 new_plot->attachAxis(axisX);
192
193 QValueAxis *axisY = new QValueAxis;
194 axisY->setRange(0, 20);
195 axisY->setTickCount(11);
196 axisY->setLabelFormat("%.1f");
197 axisY->setTitleText("Plato");
198 axisY->setLabelsFont(QFont("Helvetica", 8, QFont::Normal));
199 chart->addAxis(axisY, Qt::AlignLeft);
200 old_plot->attachAxis(axisY);
201 new_plot->attachAxis(axisY);
202
203 ui->chartView->setRenderHint(QPainter::Antialiasing);
204 ui->chartView->setChart(chart);
146 205
147 this->ignoreChanges = false; 206 this->ignoreChanges = false;
148 } 207 }
149 208
150 209
168 227
169 void CalibrateiSpindel::on_deleteRow_clicked() 228 void CalibrateiSpindel::on_deleteRow_clicked()
170 { 229 {
171 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); 230 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
172 int row = pb->objectName().toInt(); 231 int row = pb->objectName().toInt();
173 qDebug() << "Delete row" << row; 232 qDebug() << "Delete row" << row << newtotal;
233
234 if (newtotal < 4) {
235 QMessageBox::warning(this, tr("iSpindel calibrate"), tr("You cannot delete too many rows."));
236 return;
237 }
238
239 if (row == (newtotal - 1)) {
240 qDebug() << "Delete last row";
241 newtotal--;
242 } else {
243 newtotal--;
244 for (int i = row; i < newtotal; i++) {
245 nCal[i].sg = nCal[i+1].sg;
246 nCal[i].plato = nCal[i+1].plato;
247 nCal[i].angle = nCal[i+1].angle;
248 qDebug() << i << " < " << i+1;
249 }
250 }
251
252 emit refreshTable();
174 } 253 }
175 254
176 255
177 void CalibrateiSpindel::on_addButton_clicked() 256 void CalibrateiSpindel::on_addButton_clicked()
178 { 257 {
179 qDebug() << "Add row" << totaldata; 258 qDebug() << "Add row" << newtotal;
180 } 259 }
181 260
182 261
183 void CalibrateiSpindel::cell_Changed(int nRow, int nCol) 262 void CalibrateiSpindel::cell_Changed(int nRow, int nCol)
184 { 263 {

mercurial