src/PrinterDialog.cpp

changeset 52
ff7b3a41c9b5
child 53
d36879f13d32
equal deleted inserted replaced
51:355100088e1f 52:ff7b3a41c9b5
1 /**
2 * Printer.cpp is part of bmsapp.
3 *
4 * bmsapp is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * bmsapp is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include "PrinterDialog.h"
18 #include "config.h"
19 #include "bmsapp.h"
20
21 #include <QPrintPreviewDialog>
22
23
24 PrinterDialog::PrinterDialog(int job, int rec, QWidget* parent) : QDialog(parent)
25 {
26 qDebug() << "PrinterDialog start job" << job << "rec" << rec;
27
28 p_job = job;
29 p_rec = rec;
30
31 QPrinter printer(QPrinter::ScreenResolution);
32 QPrintPreviewDialog preview(&printer, this);
33 connect(&preview, &QPrintPreviewDialog::paintRequested,
34 this, &PrinterDialog::printDocument);
35 preview.exec();
36
37 qDebug() << "return";
38 }
39
40
41 PrinterDialog::~PrinterDialog()
42 {
43 qDebug() << "PrinterDialog done";
44 }
45
46
47 void PrinterDialog::printDocument(QPrinter *printer)
48 {
49 qDebug() << "PrinterDialog printDocument()";
50
51 QRect rectangle;
52 QRect boundingRect;
53 QPainter painter;
54 QString w;
55
56 const QStringList f_types({tr("Grain"), tr("Sugar"), tr("Extract"), tr("Dry extract"), tr("Adjunct")});
57 const QStringList h_form({tr("Pellet"), tr("Plug"), tr("Leaf"), tr("Leaf Wet"), tr("Cryo")});
58 const QStringList m_types({tr("Spice"), tr("Herb"), tr("Flavor"), tr("Fining"), tr("Water agent"), tr("Yeast nutrient"), tr("Other")});
59
60 painter.begin(printer);
61 bool firstPage = true;
62 qreal y = 0;
63
64 if (p_job == PR_SUPPLIES) {
65
66 double tot_fermentables = 0, tot_hops = 0, tot_yeasts = 0, tot_miscs = 0;
67
68 printHeader(&painter, tr("Inventaris"));
69 y = 140;
70
71 /* Fermentables supplies header */
72 painter.setFont(QFont("Helvetica", 9, QFont::Bold));
73 painter.setPen(Qt::black);
74 painter.fillRect( 0, y, 735, 20, QColor(255, 150, 100, 255));
75 painter.drawText( 0, y+4, 90, 20, Qt::AlignLeft, tr("Type"));
76 painter.drawText( 90, y+4, 100, 20, Qt::AlignLeft, tr("Supplier"));
77 painter.drawText(190, y+4, 270, 20, Qt::AlignLeft, tr("Fermentable"));
78 painter.drawText(460, y+4, 115, 20, Qt::AlignRight, tr("Stock"));
79 painter.drawText(575, y+4, 80, 20, Qt::AlignRight, tr("Price/Kg"));
80 painter.drawText(655, y+4, 80, 20, Qt::AlignRight, tr("Value"));
81 y += 20;
82 painter.setFont(QFont("Helvetica", 9, QFont::Normal));
83
84 QSqlQuery query("SELECT type,name,supplier,inventory,cost FROM inventory_fermentables WHERE inventory > 0 ORDER BY type,supplier,name");
85 query.first();
86 for (int i = 0 ; i < query.size() ; i++ ) {
87 if (i % 2) {
88 painter.fillRect( 0, y, 735, 20, QColor(210, 245, 255, 255));
89 } else {
90 painter.fillRect( 0, y, 735, 20, QColor(255, 255, 210, 255));
91 }
92 painter.drawText( 0, y+4, 90, 20, Qt::AlignLeft, f_types[query.value(0).toInt()]);
93 painter.drawText( 90, y+4, 100, 20, Qt::AlignLeft, query.value(2).toString());
94 painter.drawText(190, y+4, 270, 20, Qt::AlignLeft, query.value(1).toString());
95 w = QString("%1 kg").arg(query.value(3).toDouble(), 10, 'f', 3);
96 painter.drawText(460, y+4, 115, 20, Qt::AlignRight, w);
97 w = QString("%1 €").arg(query.value(4).toDouble(), 8, 'f', 2);
98 painter.drawText(575, y+4, 80, 20, Qt::AlignRight, w);
99 w = QString("%1 €").arg(query.value(3).toDouble() * query.value(4).toDouble(), 8, 'f', 2);
100 tot_fermentables += (query.value(3).toDouble() * query.value(4).toDouble());
101 painter.drawText(655, y+4, 80, 20, Qt::AlignRight, w);
102 query.next();
103 y += 20;
104 }
105 painter.fillRect( 0, y, 735, 20, QColor(255, 150, 100, 255));
106 painter.drawText( 0, y+4, 100, 20, Qt::AlignLeft, tr("Total"));
107 w = QString("%1 €").arg(tot_fermentables, 8, 'f', 2);
108 painter.drawText(655, y+4, 80, 20, Qt::AlignRight, w);
109 y += 20;
110 qDebug() << " * " << y;
111
112 query.exec("SELECT name,form,origin,inventory,cost FROM inventory_hops WHERE inventory > 0 ORDER BY origin,name");
113 query.first();
114 qDebug() << "hops" << query.size();
115 y += 60; // TODO: proper pagebreak check
116 /* Hops supplies header */
117 painter.setFont(QFont("Helvetica", 9, QFont::Bold));
118 painter.setPen(Qt::black);
119 painter.fillRect( 0, y, 735, 20, QColor(255, 150, 100, 255));
120 painter.drawText( 0, y+4, 120, 20, Qt::AlignLeft, tr("Country"));
121 painter.drawText(120, y+4, 260, 20, Qt::AlignLeft, tr("Hop name"));
122 painter.drawText(380, y+4, 80, 20, Qt::AlignLeft, tr("Form"));
123 painter.drawText(460, y+4, 115, 20, Qt::AlignRight, tr("Stock"));
124 painter.drawText(575, y+4, 80, 20, Qt::AlignRight, tr("Price/Kg"));
125 painter.drawText(655, y+4, 80, 20, Qt::AlignRight, tr("Value"));
126 y += 20;
127 painter.setFont(QFont("Helvetica", 9, QFont::Normal));
128 for (int i = 0; i < query.size(); i++) {
129 if (i % 2) {
130 painter.fillRect( 0, y, 735, 20, QColor(210, 245, 255, 255));
131 } else {
132 painter.fillRect( 0, y, 735, 20, QColor(255, 255, 210, 255));
133 }
134 painter.drawText( 0, y+4, 120, 20, Qt::AlignLeft, query.value(2).toString());
135 painter.drawText(120, y+4, 260, 20, Qt::AlignLeft, query.value(0).toString());
136 painter.drawText(380, y+4, 80, 20, Qt::AlignLeft, h_form[query.value(1).toInt()]);
137 if (query.value(3).toDouble() < 0.6)
138 w = QString("%1 gr").arg(query.value(3).toDouble() * 1000.0, 10, 'f', 1);
139 else
140 w = QString("%1 kg").arg(query.value(3).toDouble(), 10, 'f', 3);
141 painter.drawText(460, y+4, 115, 20, Qt::AlignRight, w);
142 w = QString("%1 €").arg(query.value(4).toDouble(), 8, 'f', 2);
143 painter.drawText(575, y+4, 80, 20, Qt::AlignRight, w);
144 w = QString("%1 €").arg(query.value(3).toDouble() * query.value(4).toDouble(), 8, 'f', 2);
145 tot_hops += (query.value(3).toDouble() * query.value(4).toDouble());
146 painter.drawText(655, y+4, 80, 20, Qt::AlignRight, w);
147 query.next();
148 y += 20;
149 }
150 painter.fillRect( 0, y, 735, 20, QColor(255, 150, 100, 255));
151 painter.drawText( 0, y+4, 100, 20, Qt::AlignLeft, tr("Total"));
152 w = QString("%1 €").arg(tot_hops, 8, 'f', 2);
153 painter.drawText(655, y+4, 80, 20, Qt::AlignRight, w);
154 y += 20;
155 qDebug() << " * " << y;
156
157 query.exec("SELECT name,laboratory,product_id,form,inventory,cost FROM inventory_yeasts WHERE inventory > 0 ORDER BY laboratory,product_id");
158 query.first();
159 qDebug() << "yeasts" << query.size();
160
161 query.exec("SELECT name,type,amount_is_weight,inventory,cost FROM inventory_miscs WHERE inventory > 0 ORDER BY type,name");
162 query.first();
163 qDebug() << "miscs" << query.size();
164 }
165
166
167 painter.end();
168 }
169
170
171
172 void PrinterDialog::printHeader(QPainter *painter, QString title)
173 {
174 QSqlQuery query("SELECT brewery_logo, brewery_name FROM profile_setup");
175 query.first();
176 QByteArray logoByteArray = query.value(0).toByteArray();
177 QPixmap outPixmap = QPixmap();
178 outPixmap.loadFromData(logoByteArray);
179 painter->drawPixmap(0, 0, 120, 120, outPixmap);
180
181 painter->setFont(QFont("Helvetica", 18, QFont::Bold));
182 painter->drawText( 150, 0, 500, 40, Qt::AlignLeft, title + " " + query.value(1).toString());
183 }
184
185

mercurial