Sat, 19 Mar 2022 19:22:58 +0100
First step in loading the Inventory Suppliers database on the mainscreen. But, it is loaded at program start en not removed when done. Weird programming to get this working.
24 | 1 | /** |
2 | * InventoryHops.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 "InventoryHops.h" | |
18 | #include "EditHop.h" | |
19 | #include "../ui/ui_InventoryHops.h" | |
20 | #include "config.h" | |
21 | #include "bmsapp.h" | |
22 | ||
23 | ||
24 | InventoryHops::InventoryHops(QWidget *parent) : QDialog(parent), ui(new Ui::InventoryHops) | |
25 | { | |
26 | qDebug() << "InventoryHops start"; | |
27 | ||
28 | ui->setupUi(this); | |
29 | emit refreshTable(); | |
30 | ||
31 | setWindowTitle( QString("BMSapp - %1 - Inventory Hops").arg(VERSIONSTRING) ); | |
32 | } | |
33 | ||
34 | ||
35 | void InventoryHops::refreshTable() | |
36 | { | |
37 | QString w; | |
38 | ||
39 | qDebug() << "InventoryHops reload"; | |
40 | ||
41 | QSqlQuery query("SELECT * FROM inventory_hops ORDER BY origin,name"); | |
42 | const QStringList labels({tr("Origin"), tr("Name"), tr("Type"), tr("Form"), tr("Alpha"), tr("Beta"), tr("Cohumulone"), tr("HSI"), tr("Harvest"), tr("Stock"), tr("Edit")}); | |
43 | const QStringList types({tr("Bittering"), tr("Aroma"), tr("Both")}); | |
44 | const QStringList form({tr("Pellet"), tr("Plug"), tr("Leaf"), tr("Leaf Wet"), tr("Cryo")}); | |
45 | ||
46 | /* origin supplier name type graintype color yield inventory Edit */ | |
47 | ui->tableHops->setColumnCount(11); | |
48 | ui->tableHops->setColumnWidth(0, 130); /* Origin */ | |
49 | ui->tableHops->setColumnWidth(1, 250); /* Name */ | |
50 | ui->tableHops->setColumnWidth(2, 80); /* Type */ | |
51 | ui->tableHops->setColumnWidth(3, 80); /* Form */ | |
52 | ui->tableHops->setColumnWidth(4, 80); /* Alpha */ | |
53 | ui->tableHops->setColumnWidth(5, 80); /* Beta */ | |
54 | ui->tableHops->setColumnWidth(6, 80); /* cohumulone */ | |
55 | ui->tableHops->setColumnWidth(7, 80); /* HSI */ | |
56 | ui->tableHops->setColumnWidth(8, 80); /* Harvest date */ | |
57 | ui->tableHops->setColumnWidth(9, 80); /* Stock */ | |
58 | ui->tableHops->setColumnWidth(10, 80); /* Edit button */ | |
59 | ui->tableHops->setRowCount(query.size()); | |
60 | ui->tableHops->setHorizontalHeaderLabels(labels); | |
61 | ui->tableHops->verticalHeader()->hide(); | |
62 | ui->tableHops->setFixedSize(1100 + 24, 640); /* Even if this is too large, it works */ | |
63 | ||
64 | QTableWidgetItem *rightitem = new QTableWidgetItem(); | |
65 | rightitem->setTextAlignment(Qt::AlignRight); | |
66 | ||
67 | query.first(); | |
68 | for (int ridx = 0 ; ridx < query.size() ; ridx++ ) { | |
69 | ui->tableHops->setItem(ridx, 0, new QTableWidgetItem(query.value(12).toString())); /* Origin */ | |
70 | ui->tableHops->setItem(ridx, 1, new QTableWidgetItem(query.value(1).toString())); /* Name */ | |
71 | ui->tableHops->setItem(ridx, 2, new QTableWidgetItem(types[query.value(9).toInt()])); /* Type */ | |
72 | ui->tableHops->setItem(ridx, 3, new QTableWidgetItem(form[query.value(10).toInt()])); /* Form */ | |
73 | w = QString("%1 %").arg(query.value(2).toDouble(), 2, 'f', 1, '0' ); /* Alpha% */ | |
74 | QTableWidgetItem *item = new QTableWidgetItem(w); | |
75 | item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
76 | ui->tableHops->setItem(ridx, 4, item); | |
77 | w = QString("%1 %").arg(query.value(3).toDouble(), 2, 'f', 1, '0' ); /* Beta% */ | |
78 | item = new QTableWidgetItem(w); | |
79 | item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
80 | ui->tableHops->setItem(ridx, 5, item); | |
81 | ||
82 | w = QString("%1 %").arg(query.value(6).toDouble(), 2, 'f', 1, '0' ); /* Cohumulone */ | |
83 | item = new QTableWidgetItem(w); | |
84 | item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
85 | ui->tableHops->setItem(ridx, 6, item); | |
86 | ||
87 | w = QString("%1").arg(query.value(8).toDouble(), 2, 'f', 1, '0' ); /* HSI */ | |
88 | item = new QTableWidgetItem(w); | |
89 | item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
90 | ui->tableHops->setItem(ridx, 7, item); | |
91 | ||
92 | if (query.value(15).toDouble() > 0) | |
93 | ui->tableHops->setItem(ridx, 8, new QTableWidgetItem(query.value(17).toString())); /* Harvest */ | |
94 | else | |
95 | ui->tableHops->setItem(ridx, 8, new QTableWidgetItem(QString(""))); | |
96 | ||
97 | w = QString(""); | |
98 | if (query.value(15).toDouble() > 0) { | |
99 | if (query.value(15).toDouble() < 1.000) { | |
100 | w = QString("%1 gr").arg(query.value(15).toDouble() * 1000.0, 2, 'f', 1, '0' ); | |
101 | } else { | |
102 | w = QString("%1 kg").arg(query.value(15).toDouble(), 4, 'f', 3, '0' ); | |
103 | } | |
104 | } | |
105 | item = new QTableWidgetItem(w); | |
106 | item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); | |
107 | ui->tableHops->setItem(ridx, 9, item); | |
108 | ||
109 | /* Add the Edit button */ | |
110 | QWidget* pWidget = new QWidget(); | |
111 | QPushButton* btn_edit = new QPushButton(); | |
112 | btn_edit->setObjectName(QString("%1").arg(query.value(0).toString())); /* Send record with the button */ | |
113 | btn_edit->setText(tr("Edit")); | |
114 | connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editButton_clicked())); | |
115 | QHBoxLayout* pLayout = new QHBoxLayout(pWidget); | |
116 | pLayout->addWidget(btn_edit); | |
117 | pLayout->setContentsMargins(5, 0, 5, 0); | |
118 | pWidget->setLayout(pLayout); | |
119 | ui->tableHops->setCellWidget(ridx, 10, pWidget); | |
120 | query.next(); | |
121 | } | |
122 | ||
123 | setWindowTitle( QString("BMSapp - %1 - Inventory Hops").arg(VERSIONSTRING) ); | |
124 | } | |
125 | ||
126 | ||
127 | InventoryHops::~InventoryHops() | |
128 | { | |
129 | qDebug() << "InventoryHops done"; | |
130 | delete ui; | |
131 | } | |
132 | ||
133 | ||
134 | void InventoryHops::edit(int recno) | |
135 | { | |
136 | qDebug() << "InventoryHops edit:" << recno; | |
137 | ||
138 | EditHop dialog(recno, this); | |
139 | /* Signal from editor if a refresh is needed */ | |
140 | connect(&dialog, SIGNAL(entry_changed()), this, SLOT(refreshTable())); | |
141 | dialog.setModal(true); | |
142 | dialog.exec(); | |
143 | } | |
144 | ||
145 | ||
146 | void InventoryHops::on_editButton_clicked() | |
147 | { | |
148 | QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender()); | |
149 | int recno = pb->objectName().toInt(); | |
150 | qDebug() << Q_FUNC_INFO << recno; | |
151 | edit(recno); | |
152 | } | |
153 | ||
154 | ||
155 | void InventoryHops::on_insertButton_clicked() | |
156 | { | |
157 | qDebug() << Q_FUNC_INFO; | |
158 | edit(-1); | |
159 | } | |
160 | ||
161 | ||
44
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
162 | void InventoryHops::on_exportButton_clicked() |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
163 | { |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
164 | qDebug() << Q_FUNC_INFO; |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
165 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
166 | QSqlQuery query("SELECT * FROM inventory_hops ORDER BY origin,name"); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
167 | const QStringList types({ "Bittering", "Aroma", "Both" }); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
168 | const QStringList forms({ "Pellet", "Plug", "Leaf", "Leaf", "Pellet" }); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
169 | /* "Leaf Wet", "Cryo" */ |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
170 | /* We use more hp forms then beerxml knows about, so we send known names */ |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
171 | /* instead of what we internally use. */ |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
172 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
173 | QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::homePath() + "/hops.xml", tr("Files (*.xml)")); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
174 | if (fileName == 0) { |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
175 | QMessageBox::warning(this, tr("Save File"), tr("No XML file selected.")); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
176 | return; |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
177 | } |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
178 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
179 | QFile file(fileName); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
180 | file.open(QIODevice::WriteOnly); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
181 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
182 | QXmlStreamWriter *xmlWriter = new QXmlStreamWriter(&file); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
183 | xmlWriter->writeStartDocument(); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
184 | xmlWriter->setAutoFormatting(true); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
185 | xmlWriter->setAutoFormattingIndent(1); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
186 | xmlWriter->writeStartElement("HOPS"); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
187 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
188 | query.first(); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
189 | for (int i = 0 ; i < query.size() ; i++ ) { |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
190 | xmlWriter->writeStartElement("HOP"); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
191 | xmlWriter->writeTextElement("VERSION", "1"); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
192 | xmlWriter->writeTextElement("NAME", query.value(1).toString()); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
193 | if (query.value(11).toString().length()) |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
194 | xmlWriter->writeTextElement("NOTES", query.value(11).toString()); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
195 | xmlWriter->writeTextElement("ALWAYS_ON_STOCK", query.value(14).toInt() ? "TRUE":"FALSE"); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
196 | xmlWriter->writeTextElement("ALPHA", QString::number(query.value(2).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
197 | xmlWriter->writeTextElement("BETA", QString::number(query.value(3).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
198 | xmlWriter->writeTextElement("HUMULENE", QString::number(query.value(4).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
199 | xmlWriter->writeTextElement("CAROPHYLLENE", QString::number(query.value(5).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
200 | xmlWriter->writeTextElement("COHUMULONE", QString::number(query.value(6).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
201 | xmlWriter->writeTextElement("MYRCENE", QString::number(query.value(7).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
202 | xmlWriter->writeTextElement("TOTAL_OIL", QString::number(query.value(19).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
203 | xmlWriter->writeTextElement("HSI", QString::number(query.value(8).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
204 | xmlWriter->writeTextElement("TYPE", types[query.value(9).toInt()]); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
205 | xmlWriter->writeTextElement("FORM", forms[query.value(10).toInt()]); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
206 | xmlWriter->writeTextElement("ORIGIN", query.value(12).toString()); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
207 | if (query.value(13).toString().length()) |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
208 | xmlWriter->writeTextElement("SUBSTITUTES", query.value(13).toString()); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
209 | if (query.value(16).toDouble() > 0) |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
210 | xmlWriter->writeTextElement("COST", QString::number(query.value(16).toDouble(), 'f', 4)); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
211 | xmlWriter->writeEndElement(); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
212 | query.next(); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
213 | } |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
214 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
215 | xmlWriter->writeEndElement(); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
216 | xmlWriter->writeEndDocument(); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
217 | QMessageBox::information(this, tr("Save File"), tr("XML export ready")); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
218 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
219 | file.close(); |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
220 | } |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
221 | |
5a9a159c2d34
Added yeasts and hops XML exports.
Michiel Broek <mbroek@mbse.eu>
parents:
24
diff
changeset
|
222 | |
24 | 223 | void InventoryHops::on_quitButton_clicked() |
224 | { | |
225 | emit firstWindow(); | |
226 | } | |
227 |