src/CalibrateiSpindel.cpp

changeset 505
7ae4d022cf8f
parent 504
ea7cf64c9a6c
child 506
ea07f6c97a69
equal deleted inserted replaced
504:ea7cf64c9a6c 505:7ae4d022cf8f
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 #include "CalibrateiSpindel.h" 17 #include "CalibrateiSpindel.h"
18 #include "../ui/ui_CalibrateiSpindel.h"
18 #include "global.h" 19 #include "global.h"
19 #include "Utils.h" 20 #include "Utils.h"
20 #include "MainWindow.h" 21 #include "MainWindow.h"
21 22
22 23
27 28
28 #ifdef DEBUG_MONITOR 29 #ifdef DEBUG_MONITOR
29 qDebug() << "CalibrateiSpindel record:" << id; 30 qDebug() << "CalibrateiSpindel record:" << id;
30 #endif 31 #endif
31 32
32 gridLayout = new QGridLayout(this); 33 ui->setupUi(this);
33 gridLayout->setObjectName(QString::fromUtf8("gridLayout")); 34 this->recno = id;
34 tableCalData = new QTableWidget(this); 35 setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
35 tableCalData->setObjectName(QString::fromUtf8("tableCalData")); 36 WindowTitle();
36 tableCalData->setEnabled(true);
37 QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
38 sizePolicy.setHorizontalStretch(0);
39 sizePolicy.setVerticalStretch(0);
40 tableCalData->setSizePolicy(sizePolicy);
41 tableCalData->setMinimumSize(QSize(1024, 0));
42 gridLayout->addWidget(tableCalData, 0, 0, 1, 1);
43
44 // ui->setupUi(this);
45 // this->recno = id;
46 // setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::CustomizeWindowHint);
47 // setWindowTitle(tr("BMSapp - Calibrate iSpindel"));
48
49 // connect(parent, SIGNAL(updateiSpindel(QString)), this, SLOT(refreshiSpindel(QString)));
50 // emit refreshTable();
51 }
52
53
54 void CalibrateiSpindel::refreshTable()
55 {
56 QSqlQuery query;
57 37
58 query.prepare("SELECT node,alias,calibrate FROM mon_ispindels WHERE record = :recno"); 38 query.prepare("SELECT node,alias,calibrate FROM mon_ispindels WHERE record = :recno");
59 query.bindValue(":recno", this->recno); 39 query.bindValue(":recno", this->recno);
60 query.exec(); 40 query.exec();
61 if (query.next()) { 41 if (query.next()) {
62 42
63 _node = query.value("node").toString(); 43 _node = query.value("node").toString();
64 _alias = query.value("alias").toString(); 44 _alias = query.value("alias").toString();
65 _data_old = query.value("calibrate").toString(); 45 ui->nameEdit->setText(_node+"/"+_alias);
66 _data_new = query.value("calibrate").toString(); 46
47 QJsonParseError parseError;
48 const auto& json = query.value("calibrate").toString();
49
50 if (!json.trimmed().isEmpty()) {
51 const auto& formattedJson = QString("%1").arg(json);
52 QJsonDocument jsonResponse = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
53 if (parseError.error != QJsonParseError::NoError) {
54 qWarning() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
55 } else {
56
57 QJsonObject jsonObj = jsonResponse.object();
58 // qDebug() << "polyData: " << jsonObj["polyData"].toArray();
59 QJsonArray polyData = jsonObj.value("polyData").toArray();
60 // qDebug() << polyData;
61 for (int i = 0; i < polyData.size(); i++) {
62 Old[i] = New[i] = polyData.at(i).toDouble();
63 qDebug() << i << New[i];
64 }
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');
66 ui->oldEdit->setText(_data_old);
67
68 qDebug() << "calData: " << jsonObj["calData"].toArray();
69 QJsonArray calData = jsonObj.value("calData").toArray();
70 qDebug() << calData;
71 totaldata = 0;
72 for (int i = 0; i < calData.size(); i++) {
73 QJsonObject calObj = calData.at(i).toObject();
74 oCal[i].plato = nCal[i].plato = calObj["plato"].toDouble();
75 oCal[i].angle = nCal[i].angle = calObj["angle"].toDouble();
76 oCal[i].sg = nCal[i].sg = Utils::plato_to_sg(oCal[i].plato);
77 totaldata++;
78 }
79
80 }
81 }
67 82
68 } 83 }
69 84
85 connect(ui->dataTable, SIGNAL(cellChanged(int, int)), this, SLOT(cell_Changed(int, int)));
86 // connect(parent, SIGNAL(updateiSpindel(QString)), this, SLOT(refreshiSpindel(QString)));
87 emit refreshTable();
88 }
89
90
91 void CalibrateiSpindel::refreshTable()
92 {
93 QString w;
94 QWidget* pWidget;
95 QHBoxLayout* pLayout;
96 double d;
97
98 qDebug() << "refreshTable" << totaldata;
99
100 /*
101 * 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.
103 */
104 this->ignoreChanges = true;
105
106 const QStringList labels({tr("SG"), tr("°Plato"), tr("Angle"), tr("Del")});
107 ui->dataTable->setColumnCount(4);
108 ui->dataTable->setColumnWidth(0, 100); /* SG */
109 ui->dataTable->setColumnWidth(1, 100); /* °Plato */
110 ui->dataTable->setColumnWidth(2, 100); /* Tilt angle */
111 ui->dataTable->setColumnWidth(3, 55); /* Del button */
112 ui->dataTable->setHorizontalHeaderLabels(labels);
113 ui->dataTable->verticalHeader()->hide();
114 ui->dataTable->setRowCount(totaldata);
115
116 for (int i = 0; i < totaldata; i++) {
117 qDebug() << i << nCal[i].sg << nCal[i].plato << nCal[i].angle;
118
119 w = QString("%1").arg(nCal[i].sg, 1, 'f', 4, '0');
120 QTableWidgetItem *item = new QTableWidgetItem(w);
121 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
122 ui->dataTable->setItem(i, 0, item);
123
124 w = QString("%1").arg(nCal[i].plato, 1, 'f', 3, '0');
125 item = new QTableWidgetItem(w);
126 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
127 ui->dataTable->setItem(i, 1, item);
128
129 w = QString("%1").arg(nCal[i].angle, 1, 'f', 5, '0');
130 item = new QTableWidgetItem(w);
131 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
132 ui->dataTable->setItem(i, 2, item);
133
134 /* Add the Delete row button */
135 pWidget = new QWidget();
136 QPushButton* btn_del = new QPushButton();
137 btn_del->setObjectName(QString("%1").arg(i)); /* Send row with the button */
138 btn_del->setText(tr("Del"));
139 connect(btn_del, SIGNAL(clicked()), this, SLOT(on_deleteRow_clicked()));
140 pLayout = new QHBoxLayout(pWidget);
141 pLayout->addWidget(btn_del);
142 pLayout->setContentsMargins(5, 0, 5, 0);
143 pWidget->setLayout(pLayout);
144 ui->dataTable->setCellWidget(i, 3, pWidget);
145 }
146
147 this->ignoreChanges = false;
70 } 148 }
71 149
72 150
73 CalibrateiSpindel::~CalibrateiSpindel() 151 CalibrateiSpindel::~CalibrateiSpindel()
74 { 152 {
75 // delete ui; 153 delete ui;
76 } 154 }
77 155
78 156
79 void CalibrateiSpindel::on_quitButton_clicked() 157 void CalibrateiSpindel::on_quitButton_clicked()
80 { 158 {
81 this->close(); 159 this->close();
82 this->setResult(1); 160 this->setResult(1);
83 } 161 }
84 162
85 163
164 void CalibrateiSpindel::on_saveButton_clicked()
165 {
166 }
167
168
169 void CalibrateiSpindel::on_deleteRow_clicked()
170 {
171 QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
172 int row = pb->objectName().toInt();
173 qDebug() << "Delete row" << row;
174 }
175
176
177 void CalibrateiSpindel::on_addButton_clicked()
178 {
179 qDebug() << "Add row" << totaldata;
180 }
181
182
183 void CalibrateiSpindel::cell_Changed(int nRow, int nCol)
184 {
185 QString w;
186
187 if (this->ignoreChanges)
188 return;
189
190 qDebug() << "Cell at row " + QString::number(nRow) + " column " + QString::number(nCol) + " was changed.";
191
192 }
193
194
195 /*
196 * Window header, mark any change with '**'
197 */
198 void CalibrateiSpindel::WindowTitle()
199 {
200 QString txt;
201
202 txt = QString(tr("BMSapp - Calibrate iSpindel %1").arg(this->recno));
203
204 if (this->textIsChanged) {
205 txt.append((QString(" **")));
206 }
207 setWindowTitle(txt);
208 }
209

mercurial