Added miscs XML export

Sat, 05 Mar 2022 21:49:36 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 05 Mar 2022 21:49:36 +0100
changeset 45
4024b389f689
parent 44
5a9a159c2d34
child 46
404b79f6a681

Added miscs XML export

src/InventoryMiscs.cpp file | annotate | diff | comparison | revisions
src/InventoryMiscs.h file | annotate | diff | comparison | revisions
--- a/src/InventoryMiscs.cpp	Sat Mar 05 21:15:59 2022 +0100
+++ b/src/InventoryMiscs.cpp	Sat Mar 05 21:49:36 2022 +0100
@@ -147,6 +147,64 @@
 }
 
 
+void InventoryMiscs::on_exportButton_clicked()
+{
+    qDebug() << Q_FUNC_INFO;
+
+    QSqlQuery query("SELECT * FROM inventory_miscs ORDER BY name");
+    const QStringList types({ "Spice", "Herb", "Flavor", "Fining", "Water agent", "Other",          "Other" });
+    /*                        'Spice', 'Herb', 'Flavor', 'Fining', 'Water agent', 'Yeast nutrient', 'Other'  */
+    /*  We use more misc types then beerxml knows about, so we send known names */
+    /*  instead of what we internally use.                                      */
+    const QStringList uses({ "Secondary", "Mash", "Boil", "Primary", "Secondary", "Bottling" });
+    /*                         'Starter', 'Mash', 'Boil', 'Primary', 'Secondary', 'Bottling'  */
+    /* Stupid beerxml leaves us no other choice then to map a starter ingredient to secondary */
+
+    QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), QDir::homePath() + "/miscs.xml", tr("Files (*.xml)"));
+    if (fileName == 0) {
+        QMessageBox::warning(this, tr("Save File"), tr("No XML file selected."));
+        return;
+    }
+
+    QFile file(fileName);
+    file.open(QIODevice::WriteOnly);
+
+    QXmlStreamWriter *xmlWriter = new QXmlStreamWriter(&file);
+    xmlWriter->writeStartDocument();
+    xmlWriter->setAutoFormatting(true);
+    xmlWriter->setAutoFormattingIndent(1);
+    xmlWriter->writeStartElement("MISCS");
+
+    query.first();
+    for (int i = 0 ; i < query.size() ; i++ ) {
+        xmlWriter->writeStartElement("MISC");
+        xmlWriter->writeTextElement("VERSION", "1");
+        xmlWriter->writeTextElement("NAME", query.value(1).toString());
+	xmlWriter->writeTextElement("TYPE", types[query.value(2).toInt()]);
+	xmlWriter->writeTextElement("AMOUNT_IS_WEIGHT", query.value(5).toInt() ? "TRUE":"FALSE");
+	xmlWriter->writeTextElement("USE", uses[query.value(3).toInt()]);
+	if (query.value(4).toDouble() > 0)
+	    xmlWriter->writeTextElement("TIME", QString::number(query.value(4).toDouble(), 'f', 3));
+	if (query.value(10).toDouble() > 0)
+	    xmlWriter->writeTextElement("COST", QString::number(query.value(10).toDouble(), 'f', 5));
+	xmlWriter->writeTextElement("ALWAYS_ON_STOCK", query.value(8).toInt() ? "TRUE":"FALSE");
+	if (query.value(7).toString().length())
+	    xmlWriter->writeTextElement("NOTES", query.value(7).toString());
+	if (query.value(6).toString().length())
+	    xmlWriter->writeTextElement("USE_FOR", query.value(6).toString()); 
+
+	xmlWriter->writeEndElement();
+        query.next();
+    }
+
+    xmlWriter->writeEndElement();
+    xmlWriter->writeEndDocument();
+    QMessageBox::information(this, tr("Save File"), tr("XML export ready"));
+
+    file.close();
+}
+
+
 void InventoryMiscs::on_quitButton_clicked()
 {
     emit firstWindow();
--- a/src/InventoryMiscs.h	Sat Mar 05 21:15:59 2022 +0100
+++ b/src/InventoryMiscs.h	Sat Mar 05 21:49:36 2022 +0100
@@ -22,6 +22,7 @@
     void on_quitButton_clicked();
     void on_insertButton_clicked();
     void on_editButton_clicked();
+    void on_exportButton_clicked();
     void refreshTable(void);
 
 private:

mercurial