After several days experimenting, trials and a lot of errors, a working and nice looking printer support has been added to the application.

Sun, 13 Mar 2022 22:56:22 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 13 Mar 2022 22:56:22 +0100
changeset 52
ff7b3a41c9b5
parent 51
355100088e1f
child 53
d36879f13d32

After several days experimenting, trials and a lot of errors, a working and nice looking printer support has been added to the application.

CMakeLists.txt file | annotate | diff | comparison | revisions
src/MainWindow.cpp file | annotate | diff | comparison | revisions
src/MainWindow.h file | annotate | diff | comparison | revisions
src/PrinterDialog.cpp file | annotate | diff | comparison | revisions
src/PrinterDialog.h file | annotate | diff | comparison | revisions
src/bmsapp.h file | annotate | diff | comparison | revisions
ui/MainWindow.ui file | annotate | diff | comparison | revisions
--- a/CMakeLists.txt	Tue Mar 08 16:15:31 2022 +0100
+++ b/CMakeLists.txt	Sun Mar 13 22:56:22 2022 +0100
@@ -71,7 +71,7 @@
 # ===== Find Qt5 =====
 
 # Minimum versio 5.13 for debug messages.
-find_package(Qt5 5.13 REQUIRED COMPONENTS Core Widgets Network Sql LinguistTools)
+find_package(Qt5 5.13 REQUIRED COMPONENTS Core Widgets Network Sql LinguistTools PrintSupport)
 
 # PrintSupport Xml LinguistTools
 
@@ -121,6 +121,7 @@
     ${SRCDIR}/EditProfileMash.cpp
     ${SRCDIR}/Setup.cpp
     ${SRCDIR}/Utils.cpp
+    ${SRCDIR}/PrinterDialog.cpp
     ${SRCDIR}/MainWindow.cpp
     ${SRCDIR}/database/database.cpp
     ${SRCDIR}/nulldateedit.cpp
@@ -149,6 +150,7 @@
     ${SRCDIR}/EditProfileMash.h
     ${SRCDIR}/Setup.h
     ${SRCDIR}/Utils.h
+    ${SRCDIR}/PrinterDialog.h
     ${SRCDIR}/MainWindow.h
     ${SRCDIR}/database/database.h
     ${SRCDIR}/nulldateedit.h
@@ -206,7 +208,7 @@
 endif()
 
 add_executable(${bmsapp_EXECUTABLE} ${SOURCE_FILES} ${QM_FILES})
-target_link_libraries(${bmsapp_EXECUTABLE} Qt5::Core Qt5::Widgets Qt5::Network Qt5::Sql)
+target_link_libraries(${bmsapp_EXECUTABLE} Qt5::Core Qt5::Widgets Qt5::Network Qt5::Sql Qt5::PrintSupport)
 
 # `make translations'
 add_custom_target(translations DEPENDS ${QM_FILES})
--- a/src/MainWindow.cpp	Tue Mar 08 16:15:31 2022 +0100
+++ b/src/MainWindow.cpp	Sun Mar 13 22:56:22 2022 +0100
@@ -26,6 +26,7 @@
 #include "ProfileWaters.h"
 #include "ProfileMashs.h"
 #include "Setup.h"
+#include "PrinterDialog.h"
 #include "../ui/ui_MainWindow.h"
 #include "config.h"
 
@@ -185,6 +186,20 @@
 }
 
 
+void MainWindow::on_actionSupplies_list_triggered()
+{
+    qDebug() << Q_FUNC_INFO;
+    QDialog *printer = new PrinterDialog(PR_SUPPLIES, -1, this);
+}
+
+
+void MainWindow::on_actionYeast_bank_triggered()
+{
+    qDebug() << Q_FUNC_INFO;
+    QDialog *printer = new PrinterDialog(PR_YEASTBANK, -1, this);
+}
+
+
 void MainWindow::fromProfileWaters()
 {
     qDebug() << Q_FUNC_INFO;
--- a/src/MainWindow.h	Tue Mar 08 16:15:31 2022 +0100
+++ b/src/MainWindow.h	Sun Mar 13 22:56:22 2022 +0100
@@ -38,6 +38,8 @@
     void on_actionMiscs_triggered();
     void on_actionWaters_triggered();
     void on_actionEquipments_triggered();
+    void on_actionSupplies_list_triggered();
+    void on_actionYeast_bank_triggered();
     void on_actionWater_profiles_triggered();
     void on_actionMash_profiles_triggered();
     void on_actionSetup_triggered();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/PrinterDialog.cpp	Sun Mar 13 22:56:22 2022 +0100
@@ -0,0 +1,185 @@
+/**
+ * Printer.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 "PrinterDialog.h"
+#include "config.h"
+#include "bmsapp.h"
+
+#include <QPrintPreviewDialog>
+
+
+PrinterDialog::PrinterDialog(int job, int rec, QWidget* parent) : QDialog(parent)
+{
+    qDebug() << "PrinterDialog start job" << job << "rec" << rec;
+
+    p_job = job;
+    p_rec = rec;
+
+    QPrinter printer(QPrinter::ScreenResolution);
+    QPrintPreviewDialog preview(&printer, this);
+    connect(&preview, &QPrintPreviewDialog::paintRequested,
+            this, &PrinterDialog::printDocument);
+    preview.exec();
+
+    qDebug() << "return";
+}
+
+
+PrinterDialog::~PrinterDialog()
+{
+    qDebug() << "PrinterDialog done";
+}
+
+
+void PrinterDialog::printDocument(QPrinter *printer)
+{
+    qDebug() << "PrinterDialog printDocument()";
+
+    QRect rectangle;
+    QRect boundingRect;
+    QPainter painter;
+    QString w;
+
+    const QStringList f_types({tr("Grain"), tr("Sugar"), tr("Extract"), tr("Dry extract"), tr("Adjunct")});
+    const QStringList h_form({tr("Pellet"), tr("Plug"), tr("Leaf"), tr("Leaf Wet"), tr("Cryo")});
+    const QStringList m_types({tr("Spice"), tr("Herb"), tr("Flavor"), tr("Fining"), tr("Water agent"), tr("Yeast nutrient"), tr("Other")});
+
+    painter.begin(printer);
+    bool firstPage = true;
+    qreal y = 0;
+
+    if (p_job == PR_SUPPLIES) {
+
+	double	tot_fermentables = 0, tot_hops = 0, tot_yeasts = 0, tot_miscs = 0;
+
+	printHeader(&painter, tr("Inventaris"));
+	y = 140;
+
+	/* Fermentables supplies header */
+	painter.setFont(QFont("Helvetica", 9, QFont::Bold));
+	painter.setPen(Qt::black);
+	painter.fillRect(  0, y,   735, 20, QColor(255, 150, 100, 255));
+	painter.drawText(  0, y+4,  90, 20, Qt::AlignLeft, tr("Type"));
+	painter.drawText( 90, y+4, 100, 20, Qt::AlignLeft, tr("Supplier"));
+	painter.drawText(190, y+4, 270, 20, Qt::AlignLeft, tr("Fermentable"));
+	painter.drawText(460, y+4, 115, 20, Qt::AlignRight, tr("Stock"));
+	painter.drawText(575, y+4,  80, 20, Qt::AlignRight, tr("Price/Kg"));
+	painter.drawText(655, y+4,  80, 20, Qt::AlignRight, tr("Value"));
+	y += 20;
+	painter.setFont(QFont("Helvetica", 9, QFont::Normal));
+
+	QSqlQuery query("SELECT type,name,supplier,inventory,cost FROM inventory_fermentables WHERE inventory > 0 ORDER BY type,supplier,name");
+	query.first();
+	for (int i = 0 ; i < query.size() ; i++ ) {
+	    if (i % 2) {
+		painter.fillRect(  0, y,   735, 20, QColor(210, 245, 255, 255));
+	    } else {
+		painter.fillRect(  0, y,   735, 20, QColor(255, 255, 210, 255));
+	    }
+	    painter.drawText(  0, y+4,  90, 20, Qt::AlignLeft, f_types[query.value(0).toInt()]);
+	    painter.drawText( 90, y+4, 100, 20, Qt::AlignLeft, query.value(2).toString());
+	    painter.drawText(190, y+4, 270, 20, Qt::AlignLeft, query.value(1).toString());
+	    w = QString("%1 kg").arg(query.value(3).toDouble(), 10, 'f', 3);
+	    painter.drawText(460, y+4, 115, 20, Qt::AlignRight, w);
+	    w = QString("%1 €").arg(query.value(4).toDouble(), 8, 'f', 2);
+	    painter.drawText(575, y+4,  80, 20, Qt::AlignRight, w);
+	    w = QString("%1 €").arg(query.value(3).toDouble() * query.value(4).toDouble(), 8, 'f', 2);
+	    tot_fermentables += (query.value(3).toDouble() * query.value(4).toDouble());
+	    painter.drawText(655, y+4,  80, 20, Qt::AlignRight, w);
+	    query.next();
+	    y += 20;
+	}
+	painter.fillRect(  0, y,   735, 20, QColor(255, 150, 100, 255));
+	painter.drawText(  0, y+4, 100, 20, Qt::AlignLeft, tr("Total"));
+	w = QString("%1 €").arg(tot_fermentables, 8, 'f', 2);
+	painter.drawText(655, y+4,  80, 20, Qt::AlignRight, w);
+	y += 20;
+	qDebug() << " * " << y;
+
+	query.exec("SELECT name,form,origin,inventory,cost FROM inventory_hops WHERE inventory > 0 ORDER BY origin,name");
+        query.first();
+	qDebug() << "hops" << query.size();
+	y += 60;	// TODO: proper pagebreak check
+	/* Hops supplies header */
+        painter.setFont(QFont("Helvetica", 9, QFont::Bold));
+        painter.setPen(Qt::black);
+        painter.fillRect(  0, y,   735, 20, QColor(255, 150, 100, 255));
+        painter.drawText(  0, y+4, 120, 20, Qt::AlignLeft, tr("Country"));
+	painter.drawText(120, y+4, 260, 20, Qt::AlignLeft, tr("Hop name"));
+	painter.drawText(380, y+4,  80, 20, Qt::AlignLeft, tr("Form"));
+	painter.drawText(460, y+4, 115, 20, Qt::AlignRight, tr("Stock"));
+        painter.drawText(575, y+4,  80, 20, Qt::AlignRight, tr("Price/Kg"));
+        painter.drawText(655, y+4,  80, 20, Qt::AlignRight, tr("Value"));
+	y += 20;
+        painter.setFont(QFont("Helvetica", 9, QFont::Normal));
+	for (int i = 0; i < query.size(); i++) {
+	    if (i % 2) {
+                painter.fillRect(  0, y,   735, 20, QColor(210, 245, 255, 255));
+            } else {
+                painter.fillRect(  0, y,   735, 20, QColor(255, 255, 210, 255));
+            }
+            painter.drawText(  0, y+4, 120, 20, Qt::AlignLeft, query.value(2).toString());
+	    painter.drawText(120, y+4, 260, 20, Qt::AlignLeft, query.value(0).toString());
+	    painter.drawText(380, y+4,  80, 20, Qt::AlignLeft, h_form[query.value(1).toInt()]);
+	    if (query.value(3).toDouble() < 0.6)
+		w = QString("%1 gr").arg(query.value(3).toDouble() * 1000.0, 10, 'f', 1);
+	    else
+	        w = QString("%1 kg").arg(query.value(3).toDouble(), 10, 'f', 3);
+            painter.drawText(460, y+4, 115, 20, Qt::AlignRight, w);
+            w = QString("%1 €").arg(query.value(4).toDouble(), 8, 'f', 2);
+            painter.drawText(575, y+4,  80, 20, Qt::AlignRight, w);
+            w = QString("%1 €").arg(query.value(3).toDouble() * query.value(4).toDouble(), 8, 'f', 2);
+            tot_hops += (query.value(3).toDouble() * query.value(4).toDouble());
+            painter.drawText(655, y+4,  80, 20, Qt::AlignRight, w);
+            query.next();
+            y += 20;
+	}
+	painter.fillRect(  0, y,   735, 20, QColor(255, 150, 100, 255));
+        painter.drawText(  0, y+4, 100, 20, Qt::AlignLeft, tr("Total"));
+        w = QString("%1 €").arg(tot_hops, 8, 'f', 2);
+        painter.drawText(655, y+4,  80, 20, Qt::AlignRight, w);
+	y += 20;
+        qDebug() << " * " << y;
+
+	query.exec("SELECT name,laboratory,product_id,form,inventory,cost FROM inventory_yeasts WHERE inventory > 0 ORDER BY laboratory,product_id");
+        query.first();
+        qDebug() << "yeasts" << query.size();
+
+	query.exec("SELECT name,type,amount_is_weight,inventory,cost FROM inventory_miscs WHERE inventory > 0 ORDER BY type,name");
+        query.first();
+        qDebug() << "miscs" << query.size();
+    }
+
+
+    painter.end();
+}
+
+
+
+void PrinterDialog::printHeader(QPainter *painter, QString title)
+{
+    QSqlQuery query("SELECT brewery_logo, brewery_name FROM profile_setup");
+    query.first();
+    QByteArray logoByteArray = query.value(0).toByteArray();
+    QPixmap outPixmap = QPixmap();
+    outPixmap.loadFromData(logoByteArray);
+    painter->drawPixmap(0, 0, 120, 120, outPixmap);
+
+    painter->setFont(QFont("Helvetica", 18, QFont::Bold));
+    painter->drawText(  150, 0,  500, 40, Qt::AlignLeft, title + " " + query.value(1).toString());
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/PrinterDialog.h	Sun Mar 13 22:56:22 2022 +0100
@@ -0,0 +1,42 @@
+#ifndef	_PRINTERDIALOG_H
+#define	_PRINTERDIALOG_H
+
+#include <QDialog>
+#include <QPrinter>
+#include <QPainter>
+
+
+enum JobType { PR_SUPPLIES, PR_YEASTBANK };
+
+namespace Ui {
+class PrinterDialog;
+}
+
+
+class PrinterDialog : public QDialog
+{
+	Q_OBJECT
+
+public:
+    /**
+     * @brief Construct a new Printer Dialog
+     * @param parent
+     */
+    explicit PrinterDialog(int job, int rec, QWidget *parent = 0);
+
+    /**
+     * @brief Destroy the Printer Dialog
+     */
+    ~PrinterDialog();
+
+public slots:
+    void printDocument(QPrinter *printer);
+
+private:
+    int p_job;
+    int p_rec;
+
+    void printHeader(QPainter *painter, QString title);
+};
+
+#endif
--- a/src/bmsapp.h	Tue Mar 08 16:15:31 2022 +0100
+++ b/src/bmsapp.h	Sun Mar 13 22:56:22 2022 +0100
@@ -29,12 +29,15 @@
 #include <QFileDialog>
 #include <QXmlStreamReader>
 #include <QXmlStreamWriter>
-
 #include <QJsonArray>
 #include <QJsonDocument>
 #include <QJsonObject>
+#include <QMap>
+#include <QPrinter>
+#include <QWidget>
 
 #include "Utils.h"
+#include "PrinterDialog.h"
 #include "database/database.h"
 
 
--- a/ui/MainWindow.ui	Tue Mar 08 16:15:31 2022 +0100
+++ b/ui/MainWindow.ui	Sun Mar 13 22:56:22 2022 +0100
@@ -266,7 +266,7 @@
   </action>
   <action name="actionSupplies_list">
    <property name="enabled">
-    <bool>false</bool>
+    <bool>true</bool>
    </property>
    <property name="icon">
     <iconset>
@@ -278,7 +278,7 @@
   </action>
   <action name="actionYeast_bank">
    <property name="enabled">
-    <bool>false</bool>
+    <bool>true</bool>
    </property>
    <property name="icon">
     <iconset>

mercurial