Debian Codegraph @

src/export.cpp

raw symbols SCIP
1 /***************************************************************************
2 export.cpp
3 -------------------
4 Export methods for the Recipe class
5 -------------------
6 Copyright 2005-2008 David Johnson
7 Please see the header file for copyright and license information.
8 ***************************************************************************/
9
10 #include <QApplication>
11 #include <QFile>
12 #include <QInputDialog>
13 #include <QLocale>
14 #include <QMessageBox>
15 #include <QPrinter>
16 #include <QTextDocument>
17 #include <QTextStream>
18
19 #include "beerxmlreader.h"
20 #include "data.h"
21 #include "resource.h"
22 #include "textprinter.h"
23
24 #include "recipe.h"
25
26 using namespace Resource;
27
28 static QString escape(const QString &s);
29 static QString underline(const QString &line);
30
31 //////////////////////////////////////////////////////////////////////////////
32 // escape()
33 // --------
34 // Escape special xml/html characters
35
36 QString escape(const QString &s)
37 {
38 QString x = s;
39 x.replace(QLatin1Char('&'), QLatin1String("&amp;"));
40 x.replace(QLatin1Char('<'), QLatin1String("&lt;"));
41 x.replace(QLatin1Char('>'), QLatin1String("&gt;"));
42 x.replace(QLatin1Char('\''), QLatin1String("&apos;"));
43 x.replace(QLatin1Char('"'), QLatin1String("&quot;"));
44 return x;
45 }
46
47 QString underline(const QString &line)
48 {
49 QString text;
50 text = line + '\n' + text.fill('-', line.length()) + '\n';
51 return text;
52 }
53
54 //////////////////////////////////////////////////////////////////////////////
55 // exportHTML()
56 // ------------
57 // Export recipe as html
58
59 bool Recipe::exportHtml(const QString &filename)
60 {
61 if (!filename.isEmpty()) {
62 QFile datafile(filename);
63 if (!datafile.open(QFile::WriteOnly | QFile::Text)) {
64 // error opening file
65 qWarning() << "Error: Cannot open file" << filename;
66 QMessageBox::warning(0, TITLE,
67 QObject::tr("Cannot write file %1:\n%2")
68 .arg(filename)
69 .arg(datafile.errorString()));
70 datafile.close();
71 return false;
72 }
73
74 QTextStream data(&datafile);
75 QApplication::setOverrideCursor(Qt::WaitCursor);
76
77 data << recipeHTML();
78
79 QApplication::restoreOverrideCursor();
80 datafile.close();
81 return true;
82 }
83 return false;
84 }
85
86 //////////////////////////////////////////////////////////////////////////////
87 // recipeHTML()
88 // ------------
89 // Get the html of the recipe for printing and exporting
90
91 const QString Recipe::recipeHTML()
92 {
93 const QString header = "<big><strong>%1</strong></big>\n";
94 const QString table = "<table summary=\"%1\" border=0 cellpadding=0 cellspacing=%2>\n";
95 const QString th = "<td><strong>%1</strong></td>\n";
96
97 // heading
98 QString html = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
99 html += "<html>\n<head>\n";
100 html += "<meta name=\"generator\" content=\"" + TITLE + " " + VERSION + " \">\n";
101 html += "<title>" + escape(title_) + "</title>\n";
102 html += "<meta name=\"author\" content=\"" + escape(brewer_) + "\">\n";
103 html += "<style type='text/css'>\n";
104 html += "body { font-family: sans-serif; }\n";
105 html += "</style>\n";
106 html += "</head>\n\n";
107
108 html += "<body>\n";
109
110 html += table.arg("header").arg("5 bgcolor=\"#CCCCCC\" width=\"100%\"");
111 html += "<tr><td>\n" + header.arg(escape(title_)) + "</td></tr>\n";
112 html += "</table>\n<p>\n\n";
113
114 // recipe table
115 html += table.arg("recipe").arg(0);
116 html += "<tbody>\n<tr>\n";
117 html += th.arg(tr("Recipe"));
118 html += "<td width=\"15\"></td>\n";
119 html += "<td>" + escape(title_) + "</td>\n";
120 html += "<td width=\"25\"></td>\n";
121
122 html += th.arg(tr("Style"));
123 html += "<td width=\"15\"></td>\n";
124 html += "<td>" + escape(style_.name()) + "</td>\n";
125 html += "</tr>\n<tr>\n";
126
127 html += th.arg(tr("Brewer"));
128 html += "<td></td>\n";
129 html += "<td>" + escape(brewer_) + "</td>\n";
130 html += "<td></td>\n";
131
132 html += th.arg(tr("Batch"));
133 html += "<td></td>\n";
134 html += "<td>" + size_.toString(2) + "</td>\n";
135
136 html += "</tr>\n<tr>\n" + th.arg(tr(method().toUtf8()));
137 html += "</tr>\n</tbody>\n</table>\n<p>\n\n";
138
139 // characteristics table
140 html += header.arg(tr("Recipe Characteristics"));
141 html += table.arg("characteristics").arg(0);
142 html += "<tbody>\n<tr>\n";
143
144 html += th.arg(tr("Recipe Gravity"));
145 html += "<td width=\"15\"></td>\n";
146 html += "<td>" + QString::number(og_, 'f', 3) + tr(" OG</td>\n");
147 html += "<td width=\"25\"></td>\n";
148
149 html += th.arg(tr("Estimated FG"));
150 html += "<td width=\"15\"></td>\n";
151 html += "<td>" + QString::number(FGEstimate(), 'f', 3) + tr(" FG</td>\n");
152 html += "</tr>\n<tr>\n";
153
154 html += th.arg(tr("Recipe Bitterness"));
155 html += "<td></td>\n";
156 html += "<td>" + QString::number(ibu_, 'f', 0) + tr(" IBU</td>\n");
157 html += "<td></td>\n";
158
159 html += th.arg(tr("Alcohol by Volume"));
160 html += "<td></td>\n";
161 html += "<td>" + QString::number(ABV() * 100.0, 'f', 1) + tr("%</td>\n");
162 html += "</tr>\n<tr>\n";
163
164 html += th.arg(tr("Recipe Color"));
165 html += "<td></td>\n";
166 html += "<td>" + QString::number(srm_, 'f', 0) + DEGREE + tr(" SRM</td>\n");
167 html += "<td></td>\n";
168
169 html += th.arg(tr("Alcohol by Weight"));
170 html += "<td></td>\n";
171 html += "<td>" + QString::number(ABW() * 100.0, 'f', 1) + tr("%</td>\n");
172 html += "</tr>\n</tbody>\n</table>\n<p>\n\n";
173
174 // ingredients table
175 html += header.arg(tr("Ingredients"));
176 html += table.arg("ingredients").arg(0);
177 html += "<tbody>\n";
178
179 // grains
180 html += "<tr>\n" + th.arg(tr("Quantity"));
181 html += "<td width=\"15\"></td>\n";
182 html += th.arg(tr("Grain"));
183 html += "<td width=\"15\"></td>\n";
184 html += th.arg(tr("Type"));
185 html += "<td width=\"15\"></td>\n";
186 html += th.arg(tr("Use"));
187 html += "</tr>\n\n";
188
189 foreach (Grain grain, grains_) {
190 html += "<tr>\n<td>" + grain.weight().toString(2) + "</td>\n";
191 html += "<td></td>\n";
192 html += "<td>" + escape(grain.name()) + "</td>\n";
193 html += "<td></td>\n";
194 html += "<td>" + grain.type() + "</td>\n";
195 html += "<td></td>\n";
196 html += "<td>" + grain.use() + "</td>\n</tr>\n\n";
197 }
198
199 // hops
200 html += "<tr>\n" + th.arg(tr("Quantity"));
201 html += "<td width=\"15\"></td>\n";
202 html += th.arg(tr("Hop"));
203 html += "<td width=\"15\"></td>\n";
204 html += th.arg(tr("Type"));
205 html += "<td width=\"15\"></td>\n";
206 html += th.arg(tr("Time"));
207 html += "</tr>\n\n";
208
209 foreach (Hop hop, hops_) {
210 html += "<tr>\n<td>" + hop.weight().toString(2) + "</td>\n";
211 html += "<td></td>\n";
212 html += "<td>" + escape(hop.name()) + "</td>\n";
213 html += "<td></td>\n";
214 html += "<td>" + hop.type() + "</td>\n";
215 html += "<td></td>\n";
216 html += "<td colspan=2>" + QString::number(hop.time()) + tr(" minutes</td>\n</tr>\n\n");
217 }
218
219 // misc ingredients
220 html += "<tr>\n" + th.arg(tr("Quantity"));
221 html += "<td width=\"15\"></td>\n";
222 html += th.arg(tr("Misc"));
223 html += "<td width=\"15\"></td>\n";
224 html += th.arg(tr("Notes"));
225 html += "</tr>\n\n";
226
227 foreach (Misc misc, miscs_) {
228 html += "<tr>\n<td>" + misc.quantity().toString(2) + "</td>\n";
229 html += "<td></td>\n";
230 html += "<td>" + escape(misc.name()) + "</td>\n";
231 html += "<td></td>\n";
232 html += "<td>" + misc.type() + "</td>\n";
233 html += "<td></td>\n";
234 html += "<td colspan=3>" + escape(misc.notes()) + "</td>\n</tr>\n\n";
235 }
236
237 html += "</tbody>\n</table>\n<p>\n\n";
238
239 // notes
240 // TODO: using replace() might be dangerous if we ever use richtext in notes
241 html += header.arg(tr("Recipe Notes")) + "\n";
242 html += "<p>" + escape(recipenotes_).replace('\n', "<br>\n") + "\n</p>\n<p>\n";
243
244 html += header.arg(tr("Batch Notes")) + "\n";
245 html += "<p>" + escape(batchnotes_).replace('\n', "<br>\n") + "\n</p>\n";
246
247 html += "</body>\n</html>\n";
248
249 return html;
250 }
251
252 //////////////////////////////////////////////////////////////////////////////
253 // exportText()
254 // ------------
255 // Export recipe as text
256
257 bool Recipe::exportText(const QString &filename)
258 {
259 if (!filename.isEmpty()) {
260 QFile datafile(filename);
261 if (!datafile.open(QFile::WriteOnly | QFile::Text)) {
262 // error opening file
263 qWarning() << "Error: Cannot open file" << filename;
264 QMessageBox::warning(0, TITLE,
265 QObject::tr("Cannot write file %1:\n%2")
266 .arg(filename)
267 .arg(datafile.errorString()));
268 datafile.close();
269 return false;
270 }
271
272 QTextStream data(&datafile);
273 QApplication::setOverrideCursor(Qt::WaitCursor);
274
275 data << recipeText();
276
277 QApplication::restoreOverrideCursor();
278 datafile.close();
279 return true;
280 }
281 return false;
282 }
283
284 //////////////////////////////////////////////////////////////////////////////
285 // recipeText()
286 // ------------
287 // Get the ascii text of the recipe for exporting
288
289 const QString Recipe::recipeText()
290 {
291 // title stuff
292 QString text = underline(title());
293 text += tr("Brewer: ") + brewer_ + '\n';
294 text += tr("Style: ") + style_.name() + '\n';
295 text += tr("Batch: ") + size_.toString(2);
296 text += tr(method().toUtf8());
297 text += "\n\n";
298
299 // style stuff
300 text += underline(tr("Characteristics"));
301 text += tr("Recipe Gravity: ") +
302 QString::number(og_, 'f', 3) + tr(" OG\n");
303 text += tr("Recipe Bitterness: ") +
304 QString::number(ibu_, 'f', 0) + tr(" IBU\n");
305 text += tr("Recipe Color: ") +
306 QString::number(srm_, 'f', 0) + DEGREE + tr(" SRM\n");
307 text += tr("Estimated FG: ") +
308 QString::number(FGEstimate(), 'f', 3) + '\n';
309 text += tr("Alcohol by Volume: ") +
310 QString::number(ABV() * 100.0, 'f', 1) + tr("%\n");
311 text += tr("Alcohol by Weight: ") +
312 QString::number(ABW() * 100.0, 'f', 1) + tr("%\n\n");
313
314 // ingredients
315 text += underline(tr("Ingredients"));
316
317 // grains
318 // using a map will sort the grains
319 QMultiMap<QString, Grain> gmap;
320 foreach (Grain grain, grains_) gmap.insert(grain.name(), grain);
321
322 foreach (Grain grain, gmap.values()) {
323 text += grain.name().leftJustified(30, ' ');
324 text += grain.weight().toString(2) + ", ";
325 text += grain.type() + ", ";
326 text += grain.use() + '\n';
327 }
328 text += '\n';
329
330 // hops
331 // using a map will sort the hops
332 QMultiMap<QString, Hop> hmap;
333 foreach (Hop hop, hops_) hmap.insert(hop.name(), hop);
334
335 foreach (Hop hop, hmap.values()) {
336 text += hop.name().leftJustified(30, ' ');
337 text += hop.weight().toString(2) + ", ";
338 text += hop.type() + ", ";
339 text += QString::number(hop.time()) + tr(" minutes\n");
340 }
341 text += '\n';
342
343 // misc ingredients
344 // using a map will sort the ingredients
345 QMultiMap<QString, Misc> mmap;
346 foreach (Misc misc, miscs_) mmap.insert(misc.name(), misc);
347
348 foreach (Misc misc, mmap.values()) {
349 text += misc.name().leftJustified(30, ' ');
350 text += misc.quantity().toString(2) + ", ";
351 text += misc.type() + ", ";
352 text += misc.notes() + '\n';
353 }
354 text += '\n';
355
356 // notes
357 text += underline(tr("Notes"));
358
359 // TODO: wrap long notes
360 text += tr("Recipe Notes:\n") + recipenotes_ + "\n\n";
361 text += tr("Batch Notes:\n") + batchnotes_ + "\n\n";
362
363 return text;
364 }
365
366 //////////////////////////////////////////////////////////////////////////////
367 // exportBeerXML()
368 // ---------------
369 // Export recipe to BeerXML format
370
371 bool Recipe::exportBeerXML(const QString &filename)
372 {
373 // open file
374 QFile datafile(filename);
375 if (!datafile.open(QFile::WriteOnly | QFile::Text)) {
376 // error opening file
377 qWarning() << "Error: Cannot open file" << filename;
378 QMessageBox::warning(0, TITLE,
379 QObject:: tr("Cannot write file %1:\n%2")
380 .arg(filename)
381 .arg(datafile.errorString()));
382 datafile.close();
383 return false;
384 }
385
386 QApplication::setOverrideCursor(Qt::WaitCursor);
387
388 // write out xml
389 BeerXmlWriter writer(&datafile);
390 writer.writeRecipe(this);
391 datafile.close();
392
393 // recipe is saved, so set flags accordingly
394 setModified(false);
395 QApplication::restoreOverrideCursor();
396 return true;
397 }
398
399 //////////////////////////////////////////////////////////////////////////////
400 // importBeerXml()
401 // ---------------
402 // Import recipe from BeerXML format. Assumes beerXmlFormat() has been called
403
404 bool Recipe::importBeerXml(const QString &filename)
405 {
406 // open file
407 QFile datafile(filename);
408 if (!datafile.open(QFile::ReadOnly | QFile::Text)) {
409 // error opening file
410 qWarning() << "Error: Cannot open" << filename;
411 QMessageBox::warning(0, TITLE,
412 QObject::tr("Cannot read file %1:\n%2")
413 .arg(filename)
414 .arg(datafile.errorString()));
415 datafile.close();
416 return false;
417 }
418
419 QApplication::setOverrideCursor(Qt::WaitCursor);
420 title_.clear();
421 brewer_.clear();
422 size_ = Data::instance()->defaultSize();
423 style_ = Data::instance()->defaultStyle();
424 grains_.clear();
425 hops_.clear();
426 miscs_.clear();
427 recipenotes_.clear();
428 batchnotes_.clear();
429
430 // parse file
431 BeerXmlReader reader(&datafile);
432 bool status = reader.readRecipe(this);
433 datafile.close();
434
435 if (!status) {
436 qWarning() << "Error: Problem reading file" << filename;
437 qWarning() << reader.errorString();
438 QMessageBox::warning(0, TITLE,
439 tr("Error reading file %1").arg(filename));
440 QApplication::restoreOverrideCursor();
441 return false;
442 }
443
444
445 // calculate the numbers
446 recalc();
447
448 // just loaded recipes are not modified
449 setModified(false);
450 emit recipeChanged();
451
452 QApplication::restoreOverrideCursor();
453 return true;
454 }
455
456 //////////////////////////////////////////////////////////////////////////////
457 // exportPdf()
458 // ------------
459 // Export recipe as pdf
460
461 bool Recipe::exportPdf(TextPrinter *textprinter, const QString &filename)
462 {
463 if (textprinter && (!filename.isEmpty())) {
464 QTextDocument document;
465 document.setHtml(recipeHTML());
466 textprinter->exportPdf(&document, QString(), filename);
467 return true;
468 }
469 return false;
470 }