Sun, 13 Mar 2022 22:56:22 +0100
After several days experimenting, trials and a lot of errors, a working and nice looking printer support has been added to the application.
/** * 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()); }