src/EditProfileMash.cpp

changeset 49
29cf6e350063
child 50
571a13a4860b
equal deleted inserted replaced
48:ddd1171ecda5 49:29cf6e350063
1 /**
2 * EditProfileMash.cpp is part of bmsapp.
3 *
4 * bmsapp is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * bmsapp is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
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/>.
16 */
17 #include "EditProfileMash.h"
18 #include "../ui/ui_EditProfileMash.h"
19 #include "bmsapp.h"
20
21
22 EditProfileMash::EditProfileMash(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditProfileMash)
23 {
24 QSqlQuery query;
25
26 qDebug() << "EditProfileMash record:" << id;
27 ui->setupUi(this);
28 this->recno = id;
29
30 if (id >= 0) {
31 query.prepare("SELECT * FROM profile_mash WHERE record = :recno");
32 query.bindValue(":recno", id);
33 query.exec();
34 query.next();
35
36 ui->nameEdit->setText(query.value(1).toString());
37 ui->notesEdit->setPlainText(query.value(2).toString());
38 QJsonParseError parseError;
39 const auto& json = query.value(3).toString();
40
41 if (!json.trimmed().isEmpty()) {
42 const auto& formattedJson = QString("%1").arg(json);
43 this->steps = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError);
44
45 if (parseError.error != QJsonParseError::NoError)
46 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ;
47 }
48
49 } else {
50 /* Set some defaults */
51 const auto& formattedJson = QString("[]");
52 this->steps = QJsonDocument::fromJson(formattedJson.toUtf8());
53 }
54
55 connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditProfileMash::is_changed);
56 connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
57 connect(ui->stepsTable, SIGNAL(cellPressed(int, int)), this, SLOT(cell_Changed(int, int)));
58
59 ui->saveButton->setEnabled(false);
60 ui->deleteButton->setEnabled((id >= 0) ? true:false);
61
62 emit refreshTable();
63
64 WindowTitle();
65 }
66
67
68 void EditProfileMash::refreshTable()
69 {
70 QString w;
71 QWidget* pWidget;
72 QHBoxLayout* pLayout;
73 double d;
74 int total;
75
76 qDebug() << "Steps reload";
77
78 const QStringList labels({tr("Step name"), tr("Type"), tr("Start °C"), tr("End °C"), tr("Rest time"), tr("Ramp time"), tr("Button")});
79 ui->stepsTable->setColumnCount(7);
80 ui->stepsTable->setColumnWidth(0, 250); /* Step name */
81 ui->stepsTable->setColumnWidth(1, 150); /* Step type */
82 ui->stepsTable->setColumnWidth(2, 75); /* Start temp */
83 ui->stepsTable->setColumnWidth(3, 75); /* End temp */
84 ui->stepsTable->setColumnWidth(4, 75); /* Step time */
85 ui->stepsTable->setColumnWidth(5, 75); /* Ramp time */
86 ui->stepsTable->setColumnWidth(6, 80); /* Button */
87 ui->stepsTable->setHorizontalHeaderLabels(labels);
88 ui->stepsTable->verticalHeader()->hide();
89
90 qDebug() << " ** " << this->steps << this->steps.isArray() << this->steps.array().size() ;
91
92 total = 0;
93 ui->stepsTable->setRowCount(this->steps.array().size());
94
95 if (this->steps.isArray()) {
96 for (int i = 0; i < this->steps.array().size(); i++) {
97 QJsonObject obj = this->steps.array().at(i).toObject();
98 qDebug() << i << obj;
99
100 ui->stepsTable->setItem(i, 0, new QTableWidgetItem(obj["step_name"].toString()));
101
102 QComboBox* myComboBox = new QComboBox();
103 myComboBox->addItem(tr("Infusion"));
104 myComboBox->addItem(tr("Temperature"));
105 myComboBox->addItem(tr("Decoction"));
106 ui->stepsTable->setCellWidget(i, 1, myComboBox);
107 if (obj["step_type"].isString())
108 d = QString(obj["step_type"].toString()).toDouble();
109 else
110 d = obj["step_type"].toDouble();
111 myComboBox->setCurrentIndex((int)d);
112
113 if (obj["step_temp"].isString())
114 d = QString(obj["step_temp"].toString()).toDouble();
115 else
116 d = obj["step_temp"].toDouble();
117 w = QString("%1").arg(d, 2, 'f', 1, '0');
118 QTableWidgetItem *item = new QTableWidgetItem(w);
119 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
120 ui->stepsTable->setItem(i, 2, item);
121
122 if (obj["end_temp"].isString())
123 d = QString(obj["end_temp"].toString()).toDouble();
124 else
125 d = obj["end_temp"].toDouble();
126 w = QString("%1").arg(d, 2, 'f', 1, '0');
127 item = new QTableWidgetItem(w);
128 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
129 ui->stepsTable->setItem(i, 3, item);
130
131 if (obj["step_time"].isString())
132 d = QString(obj["step_time"].toString()).toDouble();
133 else
134 d = obj["step_time"].toDouble();
135 w = QString("%1").arg(d, 1, 'f', 0, '0');
136 item = new QTableWidgetItem(w);
137 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
138 ui->stepsTable->setItem(i, 4, item);
139 total += (int)d;
140
141 if (obj["ramp_time"].isString())
142 d = QString(obj["ramp_time"].toString()).toDouble();
143 else
144 d = obj["ramp_time"].toDouble();
145 w = QString("%1").arg(d, 1, 'f', 0, '0');
146 item = new QTableWidgetItem(w);
147 item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
148 ui->stepsTable->setItem(i, 5, item);
149 if (i > 0)
150 total += (int)d;
151
152 /* Add the Delete row button */
153 pWidget = new QWidget();
154 QPushButton* btn_edit = new QPushButton();
155 btn_edit->setObjectName(QString("%1").arg(i)); /* Send row with the button */
156 btn_edit->setText(tr("Delete"));
157 connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_deleteRow_clicked()));
158 pLayout = new QHBoxLayout(pWidget);
159 pLayout->addWidget(btn_edit);
160 pLayout->setContentsMargins(5, 0, 5, 0);
161 pWidget->setLayout(pLayout);
162 ui->stepsTable->setCellWidget(i, 6, pWidget);
163 }
164 }
165
166 /* Show the calculated total mash time. */
167 ui->totalEdit->setText(QString("%1:%2").arg(total / 60).arg(total % 60, 2, 'f', 0, '0'));
168 }
169
170
171 EditProfileMash::~EditProfileMash()
172 {
173 qDebug() << "EditProfileMash done";
174 delete ui;
175 emit entry_changed();
176 }
177
178
179 /*
180 * Window header, mark any change with '**'
181 */
182 void EditProfileMash::WindowTitle()
183 {
184 QString txt;
185
186 if (this->recno < 0) {
187 txt = QString(tr("BMSapp - Add new mash profile"));
188 } else {
189 txt = QString(tr("BMSapp - Edit mash profile %1").arg(this->recno));
190 }
191
192 if (this->textIsChanged) {
193 txt.append((QString(" **")));
194 }
195 setWindowTitle(txt);
196 }
197
198
199 void EditProfileMash::on_saveButton_clicked()
200 {
201 QSqlQuery query;
202
203 /* If there are errors in the form, show a message and do "return;" */
204 if (ui->nameEdit->text().length() < 2) {
205 QMessageBox::warning(this, tr("Edit Mash"), tr("Name empty or too short."));
206 return;
207 }
208
209 if (this->textIsChanged) {
210 if (this->recno == -1) {
211 query.prepare("INSERT INTO profile_mash SET name=:name, notes=:notes, "
212 "uuid = :uuid");
213 } else {
214 query.prepare("UPDATE profile_mash SET name=:name, notes=:notes "
215 "WHERE record = :recno");
216 }
217 query.bindValue(":name", ui->nameEdit->text());
218 query.bindValue(":notes", ui->notesEdit->toPlainText());
219
220 if (this->recno == -1) {
221 query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
222 } else {
223 query.bindValue(":recno", this->recno);
224 }
225 query.exec();
226 if (query.lastError().isValid()) {
227 qDebug() << "EditProfileMash" << query.lastError();
228 QMessageBox::warning(this, tr("Database error"),
229 tr("MySQL error: %1\n%2\n%3")
230 .arg(query.lastError().nativeErrorCode())
231 .arg(query.lastError().driverText())
232 .arg(query.lastError().databaseText()));
233 } else {
234 qDebug() << "EditProfileMash Saved";
235 }
236 }
237
238 ui->saveButton->setEnabled(false);
239 this->textIsChanged = false;
240 WindowTitle();
241 }
242
243
244 void EditProfileMash::on_deleteButton_clicked()
245 {
246 QSqlQuery query;
247
248 query.prepare("DELETE FROM profile_water WHERE record = :recno");
249 query.bindValue(":recno", this->recno);
250 query.exec();
251 if (query.lastError().isValid()) {
252 qDebug() << "EditProfileMash" << query.lastError();
253 QMessageBox::warning(this, tr("Database error"),
254 tr("MySQL error: %1\n%2\n%3")
255 .arg(query.lastError().nativeErrorCode())
256 .arg(query.lastError().driverText())
257 .arg(query.lastError().databaseText()));
258 } else {
259 qDebug() << "EditProfileMash Deleted" << this->recno;
260 }
261
262 this->close();
263 this->setResult(1);
264 }
265
266
267 void EditProfileMash::is_changed()
268 {
269 ui->saveButton->setEnabled(true);
270 ui->deleteButton->setEnabled((this->recno >= 0) ? true:false);
271 this->textIsChanged = true;
272 WindowTitle();
273 }
274
275
276 void EditProfileMash::cell_Changed(int nRow, int nCol)
277 {
278 qDebug() << "Cell at row " + QString::number(nRow) + " column " + QString::number(nCol) + " was double clicked.";
279
280 //ui->stepsTable->sortItems(2, Qt::AscendingOrder); // Sort on temp
281 }
282
283
284 void EditProfileMash::on_addButton_clicked()
285 {
286 qDebug() << "Add cell";
287 }
288
289
290 void EditProfileMash::on_deleteRow_clicked()
291 {
292 qDebug() << "Delete row";
293 }
294
295
296 void EditProfileMash::on_quitButton_clicked()
297 {
298 if (this->textIsChanged) {
299 int rc = QMessageBox::warning(this, tr("Mash changed"), tr("The water has been modified\n Save changes?"),
300 QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
301 switch (rc) {
302 case QMessageBox::Save:
303 on_saveButton_clicked();
304 break; /* Saved and then Quit */
305 case QMessageBox::Discard:
306 break; /* Quit without Save */
307 case QMessageBox::Cancel:
308 return; /* Return to the editor page */
309 }
310 }
311
312 this->close();
313 this->setResult(1);
314 }

mercurial