Added import from xml, equipments and beerstyles.

Sat, 11 Jun 2022 12:59:49 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 11 Jun 2022 12:59:49 +0200
changeset 275
f472f9773782
parent 274
33b5a9a42370
child 276
7316a419334d

Added import from xml, equipments and beerstyles.

CMakeLists.txt file | annotate | diff | comparison | revisions
src/ImportXML.cpp file | annotate | diff | comparison | revisions
src/ImportXML.h file | annotate | diff | comparison | revisions
src/InventoryEquipments.cpp file | annotate | diff | comparison | revisions
src/InventoryEquipments.h file | annotate | diff | comparison | revisions
src/MainWindow.cpp file | annotate | diff | comparison | revisions
src/MainWindow.h file | annotate | diff | comparison | revisions
ui/ImportXML.ui file | annotate | diff | comparison | revisions
ui/MainWindow.ui file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Fri Jun 10 10:53:59 2022 +0200
+++ b/CMakeLists.txt	Sat Jun 11 12:59:49 2022 +0200
@@ -166,6 +166,7 @@
     ${SRCDIR}/ProdOnDate.cpp
     ${SRCDIR}/ProdOnTree.cpp
     ${SRCDIR}/EditProduct.cpp
+    ${SRCDIR}/ImportXML.cpp
     ${SRCDIR}/Setup.cpp
     ${SRCDIR}/Utils.cpp
     ${SRCDIR}/PrinterDialog.cpp
@@ -210,6 +211,7 @@
     ${SRCDIR}/ProdOnDate.h
     ${SRCDIR}/ProdOnTree.h
     ${SRCDIR}/EditProduct.h
+    ${SRCDIR}/ImportXML.h
     ${SRCDIR}/Setup.h
     ${SRCDIR}/Utils.h
     ${SRCDIR}/PrinterDialog.h
@@ -237,6 +239,7 @@
     ${UIDIR}/EditProfileFerment.ui
     ${UIDIR}/EditRecipe.ui
     ${UIDIR}/EditProduct.ui
+    ${UIDIR}/ImportXML.ui
     ${UIDIR}/MainWindow.ui
   )
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ImportXML.cpp	Sat Jun 11 12:59:49 2022 +0200
@@ -0,0 +1,582 @@
+/**
+ * ImportXML.cpp is part of bmsapp.
+ *
+ * bmsapp is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * bmsapp is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "ImportXML.h"
+#include "../ui/ui_ImportXML.h"
+#include "MainWindow.h"
+
+
+ImportXML::ImportXML(QWidget *parent) : QDialog(parent), ui(new Ui::ImportXML)
+{
+    qDebug() << "ImportXML start";
+    ui->setupUi(this);
+    WindowTitle();
+    ui->progressBar->setValue(0);
+    connect(ui->quitButton, SIGNAL(clicked()), parent, SLOT(fromImportXML()));
+}
+
+
+ImportXML::~ImportXML()
+{
+    qDebug() << "ImportXML done";
+    delete ui;
+}
+
+
+void ImportXML::WindowTitle()
+{
+    QString txt = QString(tr("BMSapp - Import XML"));
+    setWindowTitle(txt);
+}
+
+
+void ImportXML::on_openButton_clicked()
+{
+    QSqlQuery query;
+    QString   sql, log;
+    int	eq_total = 0, eq_errors = 0, f_total = 0, f_errors = 0, h_total = 0, h_errors = 0;
+    int m_total = 0, m_errors = 0, y_total = 0, y_errors = 0, rec_total = 0, rec_errors = 0;
+    int st_total = 0, st_errors = 0, mash_total = 0, mash_errors = 0, w_total = 0, w_errors = 0;
+    int total = 0, errors = 0;
+
+    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath() + "/*.xml", tr("Files (*.xml)"));
+    if (fileName == 0) {
+        QMessageBox::warning(this, tr("Open File"), tr("No XML file selected."));
+        return;
+    }
+    ui->fileEdit->setText(fileName);
+
+    QFile file(fileName);
+    qint64 fsize = file.size();
+
+    log = "Import XML file `" + fileName + "`\n\n"; 
+    qInfo() << "Import XML" << fileName << "length" << fsize << "bytes";
+
+    file.open(QIODevice::ReadOnly);
+    QXmlStreamReader *xml = new QXmlStreamReader(&file);
+
+    while (xml->readNext()) {
+
+	if (xml->atEnd())
+	    break;
+
+	if (xml->tokenType() == QXmlStreamReader::StartDocument) {
+//	    qDebug() << xml->readElementText();
+	    // Just skip
+	} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "EQUIPMENTS")) {
+	    /*
+	     * Equipments
+	     */
+	    while (xml->readNext()) {
+		if (xml->atEnd())
+            	    break;
+		if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "EQUIPMENTS")) {
+		    qDebug() << "0 /EQUIPMENTS";
+		    break;
+		}
+		if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "EQUIPMENT")) {
+		    /*
+		     * Equipment
+		     */
+		    eq_total++;
+		    total++;
+		    sql = "INSERT INTO inventory_equipments SET uuid='";
+		    sql.append(QUuid::createUuid().toString().mid(1, 36));
+		    sql.append("', top_up_water=0");
+
+		    while (xml->readNext()) {
+		    	if (xml->atEnd())
+                            break;
+		    	if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "EQUIPMENT")) {
+			    query.exec(sql);
+			    if (query.lastError().isValid()) {
+//				qWarning() << sql << query.lastError();
+				eq_errors++;
+				errors++;
+			    }
+			    ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+			    break;
+		    	}
+			if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+			    // Ignore.
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "NAME")) {
+			    sql.append(QString(", name='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "NOTES")) {
+			    sql.append(QString(", notes='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "BOIL_SIZE")) {
+			    sql.append(QString(", boil_size=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "BATCH_SIZE")) {
+			    sql.append(QString(", batch_size=%1").arg(xml->readElementText().toDouble(), 3, 'f', 2, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "TUN_VOLUME")) {
+			    sql.append(QString(", tun_volume=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "TUN_WEIGHT")) {
+			    sql.append(QString(", tun_weight=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "TUN_SPECIFIC_HEAT")) {
+			    sql.append(QString(", tun_specific_heat=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "TUN_HEIGHT")) {
+			    sql.append(QString(", tun_height=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "TRUB_CHILLER_LOSS")) {
+			    sql.append(QString(", trub_chiller_loss=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "EVAP_RATE")) {
+			    sql.append(QString(", evap_rate=%1").arg(xml->readElementText().toDouble(), 3, 'f', 2, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "BOIL_TIME")) {
+			    sql.append(QString(", boil_time=%1").arg(xml->readElementText().toDouble(), 1, 'f', 0, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "CALC_BOIL_VOLUME")) {
+			    sql.append(QString(", calc_boil_volume=%1").arg((xml->readElementText() == "TRUE") ? "true":"false"));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "TOP_UP_KETTLE")) {
+			    sql.append(QString(", top_up_kettle=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "HOP_UTILIZATION")) {
+			    sql.append(QString(", hop_utilization=%1").arg(xml->readElementText().toDouble(), 1, 'f', 0, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "LAUTER_VOLUME")) {
+			    sql.append(QString(", lauter_volume=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "LAUTER_HEIGHT")) {
+			    sql.append(QString(", lauter_height=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "LAUTER_DEADSPACE")) {
+			    sql.append(QString(", lauter_deadspace=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "KETTLE_VOLUME")) {
+			    sql.append(QString(", kettle_volume=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "KETTLE_HEIGHT")) {
+			    sql.append(QString(", kettle_height=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "MASH_VOLUME")) {
+			    sql.append(QString(", mash_volume=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "EFFICIENCY")) {
+			    sql.append(QString(", efficiency=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else {
+			    if (xml->tokenType() == QXmlStreamReader::StartElement)
+			    	qDebug() << "2  " << xml->tokenType() << xml->name();
+			}
+		    }
+		}
+	    }
+	} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "STYLES")) {
+	    /*
+             * Styles
+             */
+            while (xml->readNext()) {
+	        if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "STYLES")) {
+                    qDebug() << "0 /STYLES";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "STYLE")) {
+                    /*
+                     * Style
+                     */
+                    st_total++;
+                    total++;
+                    sql = "INSERT INTO profile_styles SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+		    sql.append("'");
+
+		    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "STYLE")) {
+                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+                                qWarning() << sql << query.lastError();
+                                st_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && ((xml->name() == "VERSION")) ||
+			    (xml->name() == "DISPLAY_OG_MIN") || (xml->name() == "DISPLAY_OG_MAX") || (xml->name() == "DISPLAY_FG_MIN") ||
+			    (xml->name() == "DISPLAY_FG_MAX") || (xml->name() == "DISPLAY_IBU_MIN") || (xml->name() == "DISPLAY_IBU_MAX") ||
+			    (xml->name() == "DISPLAY_COLOR_MIN") || (xml->name() == "DISPLAY_COLOR_MAX") || (xml->name() == "DISPLAY_CARB_MIN") ||
+			    (xml->name() == "DISPLAY_CARB_MAX") || (xml->name() == "DISPLAY_ABV_MIN") || (xml->name() == "DISPLAY_ABV_MAX") ||
+			    (xml->name() == "OG_RANGE") || (xml->name() == "FG_RANGE") || (xml->name() == "IBU_RANGE") ||
+			    (xml->name() == "CARB_RANGE") || (xml->name() == "COLOR_RANGE") || (xml->name() == "ABV_RANGE")) {
+                            // Ignore.
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "NAME")) {
+                            sql.append(QString(", name='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "CATEGORY")) {
+                            sql.append(QString(", category='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "CATEGORY_NUMBER")) {
+                            sql.append(QString(", category_number=%1").arg(xml->readElementText().toInt()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "STYLE_LETTER")) {
+                            sql.append(QString(", style_letter='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "STYLE_GUIDE")) {
+                            sql.append(QString(", style_guide='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "TYPE")) {
+                            sql.append(QString(", type=%1").arg(xml->readElementText().toInt()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "NOTES")) {
+                            sql.append(QString(", notes='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "OG_MIN")) {
+                            sql.append(QString(", og_min=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "OG_MAX")) {
+                            sql.append(QString(", og_max=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "FG_MIN")) {
+                            sql.append(QString(", fg_min=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "FG_MAX")) {
+                            sql.append(QString(", fg_max=%1").arg(xml->readElementText().toDouble(), 4, 'f', 3, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "IBU_MIN")) {
+                            sql.append(QString(", ibu_min=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+                        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "IBU_MAX")) {
+                            sql.append(QString(", ibu_max=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "COLOR_MIN")) {
+                            sql.append(QString(", color_min=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+                        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "COLOR_MAX")) {
+                            sql.append(QString(", color_max=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "CARB_MIN")) {
+                            sql.append(QString(", carb_min=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+                        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "CARB_MAX")) {
+                            sql.append(QString(", carb_max=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "ABV_MIN")) {
+                            sql.append(QString(", abv_min=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+                        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "ABV_MAX")) {
+                            sql.append(QString(", abv_max=%1").arg(xml->readElementText().toDouble(), 2, 'f', 1, '0'));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "PROFILE")) {
+                            sql.append(QString(", profile='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "INGREDIENTS")) {
+                            sql.append(QString(", ingredients='%1'").arg(xml->readElementText()));
+			} else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "EXAMPLES")) {
+                            sql.append(QString(", examples='%1'").arg(xml->readElementText()));
+			} else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "FERMENTABLES")) {
+            /*
+             * Fermentables
+             */
+            while (xml->readNext()) {
+                if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "FERMENTABLES")) {
+                    qDebug() << "0 /FERMENTABLES";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "FERMENTABLE")) {
+                    /*
+                     * Fermentable
+                     */
+                    f_total++;
+                    total++;
+                    sql = "INSERT INTO inventory_fermentables SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+
+                    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "FERMENTABLE")) {
+//                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+//                              qWarning() << sql << query.lastError();
+                                f_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+                            // Ignore.
+                        } else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "HOPS")) {
+            /*
+             * Hops
+             */
+            while (xml->readNext()) {
+                if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "HOPS")) {
+                    qDebug() << "0 /HOPS";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "HOP")) {
+                    /*
+                     * Hop
+                     */
+                    h_total++;
+                    total++;
+                    sql = "INSERT INTO inventory_hops SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+
+                    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "HOP")) {
+//                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+//                              qWarning() << sql << query.lastError();
+                                h_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+                            // Ignore.
+                        } else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "MISCS")) {
+            /*
+             * Miscs
+             */
+            while (xml->readNext()) {
+                if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "MISCS")) {
+                    qDebug() << "0 /MISCS";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "MISC")) {
+                    /*
+                     * Misc
+                     */
+                    m_total++;
+                    total++;
+                    sql = "INSERT INTO inventory_miscs SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+
+                    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "MISC")) {
+//                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+//                              qWarning() << sql << query.lastError();
+                                m_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+                            // Ignore.
+                        } else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "YEASTS")) {
+            /*
+             * Yeasts
+             */
+            while (xml->readNext()) {
+                if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "YEASTS")) {
+                    qDebug() << "0 /YEASTS";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "YEAST")) {
+                    /*
+                     * Yeast
+                     */
+                    y_total++;
+                    total++;
+                    sql = "INSERT INTO inventory_yeasts SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+
+                    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "YEAST")) {
+//                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+//                              qWarning() << sql << query.lastError();
+                                y_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+                            // Ignore.
+                        } else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "WATERS")) {
+            /*
+             * Waters
+             */
+            while (xml->readNext()) {
+                if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "WATERS")) {
+                    qDebug() << "0 /WATERS";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "WATER")) {
+                    /*
+                     * Water
+                     */
+                    w_total++;
+                    total++;
+                    sql = "INSERT INTO inventory_waters SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+
+                    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "WATER")) {
+//                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+//                              qWarning() << sql << query.lastError();
+                                w_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+                            // Ignore.
+                        } else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "RECIPES")) {
+            /*
+             * Recipes
+             */
+            while (xml->readNext()) {
+                if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "RECIPES")) {
+                    qDebug() << "0 /RECIPES";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "RECIPE")) {
+                    /*
+                     * Recipe
+                     */
+                    rec_total++;
+                    total++;
+                    sql = "INSERT INTO recipes SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+
+                    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "RECIPE")) {
+//                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+//                              qWarning() << sql << query.lastError();
+                                rec_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+                            // Ignore.
+                        } else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+        } else if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "MASHS")) {
+            /*
+             * Mashes
+             */
+            while (xml->readNext()) {
+                if (xml->atEnd())
+                    break;
+                if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "MASHS")) {
+                    qDebug() << "0 /MASHS";
+                    break;
+                }
+                if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "MASH")) {
+                    /*
+                     * Mash
+                     */
+                    mash_total++;
+                    total++;
+                    sql = "INSERT INTO inventory_mashs SET uuid='";
+                    sql.append(QUuid::createUuid().toString().mid(1, 36));
+
+                    while (xml->readNext()) {
+                        if (xml->atEnd())
+                            break;
+                        if ((xml->tokenType() == QXmlStreamReader::EndElement) && (xml->name() == "MASH")) {
+//                            query.exec(sql);
+                            if (query.lastError().isValid()) {
+//                              qWarning() << sql << query.lastError();
+                                mash_errors++;
+                                errors++;
+                            }
+                            ui->progressBar->setValue(round(100 * xml->characterOffset() / fsize));
+                            break;
+                        }
+                        if ((xml->tokenType() == QXmlStreamReader::StartElement) && (xml->name() == "VERSION")) {
+                            // Ignore.
+                        } else {
+                            if (xml->tokenType() == QXmlStreamReader::StartElement)
+                                qDebug() << "2  " << xml->tokenType() << xml->name();
+                        }
+                    }
+                }
+            }
+	} else {
+	    qDebug() << "Unknown level 0" << xml->name();
+	}
+	if (xml->atEnd())
+	    break;
+    }
+
+    log.append(QString("             total  errors\n"));
+    log.append(QString("             -----  ------\n"));
+    log.append(QString("styles         %1     %2\n").arg(st_total, 3).arg(st_errors, 3));
+    log.append(QString("equipments     %1     %2\n").arg(eq_total, 3).arg(eq_errors, 3));
+    log.append(QString("fermentables   %1     %2\n").arg(f_total, 3).arg(f_errors, 3));
+    log.append(QString("hops           %1     %2\n").arg(h_total, 3).arg(h_errors, 3));
+    log.append(QString("miscs          %1     %2\n").arg(m_total, 3).arg(m_errors, 3));
+    log.append(QString("yeasts         %1     %2\n").arg(y_total, 3).arg(y_errors, 3));
+    log.append(QString("waters         %1     %2\n").arg(w_total, 3).arg(w_errors, 3));
+    log.append(QString("mashes         %1     %2\n").arg(mash_total, 3).arg(mash_errors, 3));
+    log.append(QString("recipes        %1     %2\n").arg(rec_total, 3).arg(rec_errors, 3));
+    log.append(QString("             -----  ------\n"));
+    log.append(QString("total          %1     %2\n").arg(total, 3).arg(errors, 3));
+    ui->logEdit->setPlainText(log);
+
+    ui->progressBar->setValue(100);
+    if (xml->hasError()) {
+	qWarning() << "error" << xml->error();
+    } else {
+	qDebug() << "all good" << eq_total << eq_errors;
+    }
+    file.close();
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ImportXML.h	Sat Jun 11 12:59:49 2022 +0200
@@ -0,0 +1,28 @@
+#ifndef _IMPORTXML_H
+#define _IMPORTXML_H
+
+#include <QDialog>
+
+
+namespace Ui {
+class ImportXML;
+}
+
+class ImportXML : public QDialog
+{
+    Q_OBJECT
+
+public:
+    explicit ImportXML(QWidget *parent = 0);
+    ~ImportXML();
+
+private slots:
+    void on_openButton_clicked();
+
+private:
+    Ui::ImportXML *ui;
+
+    void WindowTitle();
+};
+
+#endif
--- a/src/InventoryEquipments.cpp	Fri Jun 10 10:53:59 2022 +0200
+++ b/src/InventoryEquipments.cpp	Sat Jun 11 12:59:49 2022 +0200
@@ -61,16 +61,7 @@
     QIcon icon1;
     icon1.addFile(QString::fromUtf8(":/icons/silk/database_save.png"), QSize(), QIcon::Normal, QIcon::Off);
     exportButton->setIcon(icon1);
-    horizontalLayout->addWidget(exportButton, 0, Qt::AlignRight);
-
-    importButton = new QPushButton(groupBox);
-    importButton->setObjectName(QString::fromUtf8("importButton"));
-    importButton->setMinimumSize(QSize(80, 24));
-    importButton->setText(tr("Import"));
-    QIcon icon2;
-    icon2.addFile(QString::fromUtf8(":/icons/silk/database_add.png"), QSize(), QIcon::Normal, QIcon::Off);
-    importButton->setIcon(icon2);
-    horizontalLayout->addWidget(importButton, 0, Qt::AlignRight);
+    horizontalLayout->addWidget(exportButton, 0, Qt::AlignCenter);
 
     insertButton = new QPushButton(groupBox);
     insertButton->setObjectName(QString::fromUtf8("insertButton"));
@@ -85,7 +76,6 @@
     connect(quitButton, SIGNAL(clicked()), parent, SLOT(fromInventoryEquipments()));
     connect(insertButton, SIGNAL(clicked()), this, SLOT(on_insertButton_clicked()));
     connect(exportButton, SIGNAL(clicked()), this, SLOT(on_exportButton_clicked()));
-//    connect(importButton, SIGNAL(clicked()), this, SLOT(on_importButton_clicked()));
     connect(this, SIGNAL(setStatus(QString)), parent, SLOT(statusMsg(QString)));
     emit refreshTable();
 }
--- a/src/InventoryEquipments.h	Fri Jun 10 10:53:59 2022 +0200
+++ b/src/InventoryEquipments.h	Sat Jun 11 12:59:49 2022 +0200
@@ -37,7 +37,6 @@
     QHBoxLayout *horizontalLayout;
     QPushButton *quitButton;
     QPushButton *exportButton;
-    QPushButton *importButton;
     QPushButton *insertButton;
 
     void edit(int recno);
--- a/src/MainWindow.cpp	Fri Jun 10 10:53:59 2022 +0200
+++ b/src/MainWindow.cpp	Sat Jun 11 12:59:49 2022 +0200
@@ -33,6 +33,7 @@
 #include "ProfileMashs.h"
 #include "ProfileStyles.h"
 #include "ProfileFerments.h"
+#include "ImportXML.h"
 #include "Setup.h"
 #include "PrinterDialog.h"
 #include "../ui/ui_MainWindow.h"
@@ -295,6 +296,27 @@
 }
 
 
+void MainWindow::fromImportXML()
+{
+    ui->mainStack->setCurrentIndex(-1);
+    ui->mainStack->removeWidget(ImportXMLWindow);
+    delete ImportXMLWindow;
+    setWindowTitle( QString("BMSapp - %1").arg(VERSIONSTRING) );
+    ui->menuBar->setVisible(true);
+}
+
+
+void MainWindow::on_actionImport_XML_triggered()
+{
+    ImportXMLWindow = new ImportXML(this);
+    int index = ui->mainStack->count();
+    ui->mainStack->addWidget(ImportXMLWindow);
+    ui->mainStack->setCurrentIndex(index);
+    setWindowTitle( QString("BMSapp - %1 - Import XML").arg(VERSIONSTRING));
+    ui->menuBar->setVisible(false);
+}
+
+
 void MainWindow::fromRecipesTree()
 {
     ui->mainStack->setCurrentIndex(-1);
--- a/src/MainWindow.h	Fri Jun 10 10:53:59 2022 +0200
+++ b/src/MainWindow.h	Sat Jun 11 12:59:49 2022 +0200
@@ -18,6 +18,7 @@
 #include "ProfileMashs.h"
 #include "ProfileStyles.h"
 #include "ProfileFerments.h"
+#include "ImportXML.h"
 #include "Setup.h"
 
 #include <QMainWindow>
@@ -120,9 +121,11 @@
     ProfileMashs *ProfileMashsWindow;
     ProfileStyles *ProfileStylesWindow;
     ProfileFerments *ProfileFermentsWindow;
+    ImportXML *ImportXMLWindow;
     Setup *SetupWindow;
 
 private slots:
+    void on_actionImport_XML_triggered();
     void on_actionExit_triggered();
     void on_actionProd_inprod_triggered();
     void on_actionOn_Name_triggered();
@@ -147,6 +150,7 @@
     void on_actionAbout_triggered();
 
 public slots:
+    void fromImportXML();
     void fromRecipesTree();
     void fromInventorySuppliers();
     void fromInventoryFermentables();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ui/ImportXML.ui	Sat Jun 11 12:59:49 2022 +0200
@@ -0,0 +1,145 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>ImportXML</class>
+ <widget class="QDialog" name="ImportXML">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>1280</width>
+    <height>640</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Dialog</string>
+  </property>
+  <layout class="QGridLayout" name="gridLayout">
+   <item row="0" column="0">
+    <widget class="QWidget" name="topWidget" native="true">
+     <widget class="QLabel" name="fileLabel">
+      <property name="geometry">
+       <rect>
+        <x>200</x>
+        <y>20</y>
+        <width>91</width>
+        <height>20</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>XML file:</string>
+      </property>
+      <property name="alignment">
+       <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+      </property>
+     </widget>
+     <widget class="QLineEdit" name="fileEdit">
+      <property name="geometry">
+       <rect>
+        <x>310</x>
+        <y>20</y>
+        <width>661</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="maxLength">
+       <number>128</number>
+      </property>
+     </widget>
+     <widget class="QPushButton" name="quitButton">
+      <property name="geometry">
+       <rect>
+        <x>600</x>
+        <y>540</y>
+        <width>80</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="sizePolicy">
+       <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+        <horstretch>0</horstretch>
+        <verstretch>0</verstretch>
+       </sizepolicy>
+      </property>
+      <property name="text">
+       <string>Quit</string>
+      </property>
+      <property name="icon">
+       <iconset>
+        <normaloff>:icons/silk/door_out.png</normaloff>:icons/silk/door_out.png</iconset>
+      </property>
+     </widget>
+     <widget class="QPushButton" name="openButton">
+      <property name="enabled">
+       <bool>true</bool>
+      </property>
+      <property name="geometry">
+       <rect>
+        <x>1000</x>
+        <y>20</y>
+        <width>80</width>
+        <height>23</height>
+       </rect>
+      </property>
+      <property name="text">
+       <string>Open</string>
+      </property>
+      <property name="icon">
+       <iconset resource="../../../../../../home/mbroek/MyProjects/bmsapp/resources/icons.qrc">
+        <normaloff>:/icons/silk/application_form_add.png</normaloff>:/icons/silk/application_form_add.png</iconset>
+      </property>
+     </widget>
+     <widget class="QGroupBox" name="groupBox">
+      <property name="geometry">
+       <rect>
+        <x>200</x>
+        <y>80</y>
+        <width>881</width>
+        <height>431</height>
+       </rect>
+      </property>
+      <property name="title">
+       <string>Results</string>
+      </property>
+      <widget class="QPlainTextEdit" name="logEdit">
+       <property name="geometry">
+        <rect>
+         <x>30</x>
+         <y>90</y>
+         <width>821</width>
+         <height>311</height>
+        </rect>
+       </property>
+       <property name="font">
+        <font>
+         <family>Courier 10 Pitch</family>
+        </font>
+       </property>
+      </widget>
+      <widget class="QProgressBar" name="progressBar">
+       <property name="geometry">
+        <rect>
+         <x>30</x>
+         <y>40</y>
+         <width>821</width>
+         <height>23</height>
+        </rect>
+       </property>
+       <property name="value">
+        <number>24</number>
+       </property>
+      </widget>
+     </widget>
+    </widget>
+   </item>
+  </layout>
+ </widget>
+ <tabstops>
+  <tabstop>fileEdit</tabstop>
+  <tabstop>quitButton</tabstop>
+  <tabstop>openButton</tabstop>
+ </tabstops>
+ <resources>
+  <include location="../../../../../../home/mbroek/MyProjects/bmsapp/resources/icons.qrc"/>
+ </resources>
+ <connections/>
+</ui>
--- a/ui/MainWindow.ui	Fri Jun 10 10:53:59 2022 +0200
+++ b/ui/MainWindow.ui	Sat Jun 11 12:59:49 2022 +0200
@@ -47,6 +47,7 @@
     <property name="title">
      <string>File</string>
     </property>
+    <addaction name="actionImport_XML"/>
     <addaction name="actionExit"/>
    </widget>
    <widget class="QMenu" name="menuHelp">
@@ -86,7 +87,7 @@
     <property name="title">
      <string>Monitor</string>
     </property>
-    <addaction name="actionSystens"/>
+    <addaction name="actionSystems"/>
     <addaction name="separator"/>
     <addaction name="actionFermenters"/>
     <addaction name="actionCO2_Meters"/>
@@ -150,7 +151,7 @@
     <string>About</string>
    </property>
   </action>
-  <action name="actionSystens">
+  <action name="actionSystems">
    <property name="icon">
     <iconset>
      <normaloff>:icons/silk/server.png</normaloff>:icons/silk/server.png</iconset>
@@ -420,6 +421,15 @@
     <string>On Beerstyle</string>
    </property>
   </action>
+  <action name="actionImport_XML">
+   <property name="icon">
+    <iconset resource="../resources/icons.qrc">
+     <normaloff>:/icons/silk/application_get.png</normaloff>:/icons/silk/application_get.png</iconset>
+   </property>
+   <property name="text">
+    <string>Import XML</string>
+   </property>
+  </action>
  </widget>
  <layoutdefault spacing="6" margin="11"/>
  <resources>

mercurial