src/PrinterDialog.cpp

Thu, 14 Apr 2022 21:54:37 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 14 Apr 2022 21:54:37 +0200
changeset 133
08635b028dcf
parent 90
2396457a8167
child 155
f671c599f65b
permissions
-rw-r--r--

Load waters during recipe startup. Started calcWater() function. Load profile_setup record global. The print function uses the globals now too instead of loading from the database.

/**
 * 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 "global.h"

#include <QPrintPreviewDialog>
#include <QDebug>
#include <QtSql>


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();
}


PrinterDialog::~PrinterDialog() {}


void PrinterDialog::printDocument(QPrinter *printer)
{
    qDebug() << "PrinterDialog printDocument()";

    QRect rectangle;
    QRect boundingRect;
    QPainter painter;
    QString w;
    QSqlQuery query;

    const QColor c_header(255, 150, 100, 255);
    const QColor c_line1(210, 245, 255, 255);
    const QColor c_line2(255, 255, 210, 255);
    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")});
    const QStringList y_unit({tr("pkg"), tr("gr"), tr("ml"), tr("ml"), tr("ml"), tr("ml"), tr("gr")});
    const QStringList y_types({tr("Lager"), tr("Ale"), tr("Wheat"), tr("Wine"), tr("Champagne"), tr("Brett"), tr("Kveik"), tr("Hybrid")});
    const QStringList y_form({tr("Liquid"), tr("Dry"), tr("Slant"), tr("Culture"), tr("Frozen"), tr("Bottle"), tr("Dried")});

    painter.begin(printer);
    qreal y = 0;

    if (p_job == PR_SUPPLIES) {
	/*
	 * Print supplies in stock
	 */
	double	tot_fermentables = 0, tot_hops = 0, tot_yeasts = 0, tot_miscs = 0;

	printHeader(&painter);
	y = 120;
	/* Fermentables supplies header */
	painter.setFont(QFont("Helvetica", 9, QFont::Bold));
	painter.setPen(Qt::black);
	painter.fillRect(  0, y,   735, 20, c_header);
	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));
	query.exec("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 ((y + 20) > painter.device()->height()) {
                printer->newPage();
		printHeader(&painter);
                y = 120;
            }
	    painter.fillRect(  0, y,   735, 20, (i % 2) ? c_line1:c_line2);
	    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, c_header);
	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;

	/* Hops supplies */
	query.exec("SELECT name,form,origin,inventory,cost FROM inventory_hops WHERE inventory > 0 ORDER BY origin,name");
        query.first();
	if ((y + 80 + (query.size() * 20)) > painter.device()->height()) {	/* Rows + header + footer + blank */
	    printer->newPage();
	    printHeader(&painter);
	    y = 120;
	} else {
	    y += 40;
	}
        painter.setFont(QFont("Helvetica", 9, QFont::Bold));
        painter.setPen(Qt::black);
        painter.fillRect(  0, y,   735, 20, c_header);
        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 ((y + 20) > painter.device()->height()) {
		printer->newPage();
		printHeader(&painter);
		y = 120;
	    }
	    painter.fillRect(  0, y,   735, 20, (i % 2) ? c_line1:c_line2);
            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, c_header);
        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;

	/* Yeasts supplies */
	query.exec("SELECT name,laboratory,product_id,form,inventory,cost FROM inventory_yeasts WHERE inventory > 0 ORDER BY laboratory,product_id");
        query.first();
	if ((y + 80 + (query.size() * 20)) > painter.device()->height()) {      /* Rows + header + footer + blank */
            printer->newPage();
            printHeader(&painter);
            y = 120;
        } else {
            y += 40;
        }
        painter.setFont(QFont("Helvetica", 9, QFont::Bold));
        painter.setPen(Qt::black);
        painter.fillRect(  0, y,   735, 20, c_header);
        painter.drawText(  0, y+4, 120, 20, Qt::AlignLeft, tr("Laboratory"));
        painter.drawText(120, y+4, 120, 20, Qt::AlignLeft, tr("Product"));
        painter.drawText(240, y+4, 220, 20, Qt::AlignLeft, tr("Yeast"));
        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 ((y + 20) > painter.device()->height()) {
                printer->newPage();
		printHeader(&painter);
                y = 120;
            }
            painter.fillRect(  0, y,   735, 20, (i % 2) ? c_line1:c_line2);
            painter.drawText(  0, y+4, 120, 20, Qt::AlignLeft, query.value(1).toString());
	    painter.drawText(120, y+4, 120, 20, Qt::AlignLeft, query.value(2).toString());
	    painter.drawText(240, y+4, 220, 20, Qt::AlignLeft, query.value(0).toString());
	    if (query.value(3).toInt() == 0)
		w = QString("%1 %2").arg(query.value(4).toDouble(), 10, 'f', 1).arg(y_unit[query.value(3).toInt()]);
	    else
		w = QString("%1 %2").arg(query.value(4).toDouble() * 1000.0, 10, 'f', 1).arg(y_unit[query.value(3).toInt()]);
            painter.drawText(460, y+4, 115, 20, Qt::AlignRight, w);
            w = QString("%1 €").arg(query.value(5).toDouble(), 8, 'f', 2);
            painter.drawText(575, y+4,  80, 20, Qt::AlignRight, w);
            w = QString("%1 €").arg(query.value(4).toDouble() * query.value(5).toDouble(), 8, 'f', 2);
            tot_yeasts += (query.value(4).toDouble() * query.value(5).toDouble());
            painter.drawText(655, y+4,  80, 20, Qt::AlignRight, w);
            query.next();
            y += 20;
	}
	painter.fillRect(  0, y,   735, 20, c_header);
        painter.drawText(  0, y+4, 100, 20, Qt::AlignLeft, tr("Total"));
        w = QString("%1 €").arg(tot_yeasts, 8, 'f', 2);
        painter.drawText(655, y+4,  80, 20, Qt::AlignRight, w);
        y += 20;

	/* Miscs supplies */
	query.exec("SELECT name,type,amount_is_weight,inventory,cost FROM inventory_miscs WHERE inventory > 0 ORDER BY type,name");
        query.first();
	if ((y + 80 + (query.size() * 20)) > painter.device()->height()) {      /* Rows + header + footer + blank */
            printer->newPage();
            printHeader(&painter);
            y = 120;
        } else {
            y += 40;
        }
        painter.setFont(QFont("Helvetica", 9, QFont::Bold));
        painter.setPen(Qt::black);
        painter.fillRect(  0, y,   735, 20, c_header);
        painter.drawText(  0, y+4, 120, 20, Qt::AlignLeft, tr("Type"));
        painter.drawText(120, y+4, 340, 20, Qt::AlignLeft, tr("Ingredient"));
        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 ((y + 20) > painter.device()->height()) {
                printer->newPage();
                printHeader(&painter);
                y = 120;
            }
            painter.fillRect(  0, y,   735, 20, (i % 2) ? c_line1:c_line2);
            painter.drawText(  0, y+4, 120, 20, Qt::AlignLeft, m_types[query.value(1).toInt()]);
            painter.drawText(120, y+4, 340, 20, Qt::AlignLeft, query.value(0).toString());
            if (query.value(2).toInt())
                w = QString("%1 gr").arg(query.value(3).toDouble() * 1000.0, 10, 'f', 1);
            else
                w = QString("%1 ml").arg(query.value(3).toDouble() * 1000.0, 10, 'f', 1);
            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_miscs += (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, c_header);
        painter.drawText(  0, y+4, 100, 20, Qt::AlignLeft, tr("Total"));
        w = QString("%1 €").arg(tot_miscs, 8, 'f', 2);
        painter.drawText(655, y+4,  80, 20, Qt::AlignRight, w);
        y += 20;

    } else if (p_job == PR_YEASTBANK) {
	/*
	 * Print yeast in the private yeast bank.
	 */
	printHeader(&painter);
        y = 120;

	painter.setFont(QFont("Helvetica", 9, QFont::Bold));
        painter.setPen(Qt::black);
        painter.fillRect(  0, y,   735, 20, c_header);
        painter.drawText(  0, y+4, 200, 20, Qt::AlignLeft, tr("Yeast"));
        painter.drawText(200, y+4, 230, 20, Qt::AlignLeft, tr("Description"));
	painter.drawText(430, y+4,  80, 20, Qt::AlignCenter, tr("Type"));
	painter.drawText(510, y+4,  80, 20, Qt::AlignCenter, tr("Form"));
        painter.drawText(590, y+4,  70, 20, Qt::AlignRight, tr("Stock"));
        painter.drawText(665, y+4,  70, 20, Qt::AlignLeft, tr("Date"));
        y += 20;
        painter.setFont(QFont("Helvetica", 9, QFont::Normal));
	query.prepare("SELECT name,type,form,inventory,production_date,short_desc "
		       "FROM inventory_yeasts WHERE inventory > 0 AND laboratory = :my_lab ORDER BY product_id");
	query.bindValue(":my_lab", my_yeastlab);
	query.exec();
        query.first();
	for (int i = 0; i < query.size(); i++) {
	    painter.fillRect(  0, y,   735, 20, (i % 2) ? c_line1:c_line2);
            painter.drawText(  0, y+4, 200, 20, Qt::AlignLeft, query.value(0).toString());
	    painter.drawText(200, y+4, 230, 20, Qt::AlignLeft, query.value(5).toString());
	    painter.drawText(430, y+4,  80, 20, Qt::AlignCenter, y_types[query.value(1).toInt()]);
	    painter.drawText(510, y+4,  80, 20, Qt::AlignCenter, y_form[query.value(2).toInt()]);

	    if (query.value(2).toInt() == 0)
                w = QString("%1 %2").arg(query.value(3).toDouble(), 10, 'f', 1).arg(y_unit[query.value(2).toInt()]);
            else
                w = QString("%1 %2").arg(query.value(3).toDouble() * 1000.0, 10, 'f', 1).arg(y_unit[query.value(2).toInt()]);
	    painter.drawText(590, y+4,  70, 20, Qt::AlignRight, w);
	    painter.drawText(665, y+4,  70, 20, Qt::AlignLeft, query.value(4).toString());
	    query.next();
	    y += 20;
	}
    }


    painter.end();
}



void PrinterDialog::printHeader(QPainter *painter)
{
    QPixmap outPixmap = QPixmap();
    outPixmap.loadFromData(my_logoByteArray);
    int w = outPixmap.width();
    int h = outPixmap.height();
    /* Make sure to keep the logo aspect ratio */
    if (w == h) {
	painter->drawPixmap(0, 0, 100, 100, outPixmap);
    } else if (w > h) {
	painter->drawPixmap(0, 0, 100, (h * 100) / w, outPixmap);
    } else {
	painter->drawPixmap(0, 0, (w * 100) / h, 100, outPixmap);
    }

    /* The fat header line */
    painter->setFont(QFont("Helvetica",18, QFont::Bold));
    if (p_job == PR_SUPPLIES) {
	painter->drawText(120, 0,  500, 40, Qt::AlignLeft, tr("Inventory") + " " + my_brewery_name);
    } else if (p_job == PR_YEASTBANK) {
	painter->drawText(120, 0,  500, 40, Qt::AlignLeft, tr("Yeastbank") + " " + my_brewery_name);
    } else {
        painter->drawText(120, 0,  500, 40, Qt::AlignLeft, "?? " + my_brewery_name);
    }
    /* The first normal header line */
    painter->setFont(QFont("Helvetica",10, QFont::Normal));
    painter->drawText(120,35,  80, 20, Qt::AlignLeft, tr("Date and time"));
    painter->drawText(200,35, 400, 20, Qt::AlignLeft, ": " + QDateTime::currentDateTime().toString("dd-MM-yyyy hh:mm"));
    /* The report itself may print more lines from y = 55. */
}

mercurial