/*************************************************************************** export.cpp ------------------- Export methods for the Recipe class ------------------- Copyright 2005-2008 David Johnson Please see the header file for copyright and license information. ***************************************************************************/ #include #include #include #include #include #include #include #include #include "beerxmlreader.h" #include "data.h" #include "resource.h" #include "textprinter.h" #include "recipe.h" using namespace Resource; static QString escape(const QString &s); static QString underline(const QString &line); ////////////////////////////////////////////////////////////////////////////// // escape() // -------- // Escape special xml/html characters QString escape(const QString &s) { QString x = s; x.replace(QLatin1Char('&'), QLatin1String("&")); x.replace(QLatin1Char('<'), QLatin1String("<")); x.replace(QLatin1Char('>'), QLatin1String(">")); x.replace(QLatin1Char('\''), QLatin1String("'")); x.replace(QLatin1Char('"'), QLatin1String(""")); return x; } QString underline(const QString &line) { QString text; text = line + '\n' + text.fill('-', line.length()) + '\n'; return text; } ////////////////////////////////////////////////////////////////////////////// // exportHTML() // ------------ // Export recipe as html bool Recipe::exportHtml(const QString &filename) { if (!filename.isEmpty()) { QFile datafile(filename); if (!datafile.open(QFile::WriteOnly | QFile::Text)) { // error opening file qWarning() << "Error: Cannot open file" << filename; QMessageBox::warning(0, TITLE, QObject::tr("Cannot write file %1:\n%2") .arg(filename) .arg(datafile.errorString())); datafile.close(); return false; } QTextStream data(&datafile); QApplication::setOverrideCursor(Qt::WaitCursor); data << recipeHTML(); QApplication::restoreOverrideCursor(); datafile.close(); return true; } return false; } ////////////////////////////////////////////////////////////////////////////// // recipeHTML() // ------------ // Get the html of the recipe for printing and exporting const QString Recipe::recipeHTML() { const QString header = "%1\n"; const QString table = "\n"; const QString th = "\n"; // heading QString html = "\n"; html += "\n\n"; html += "\n"; html += "" + escape(title_) + "\n"; html += "\n"; html += "\n"; html += "\n\n"; html += "\n"; html += table.arg("header").arg("5 bgcolor=\"#CCCCCC\" width=\"100%\""); html += "\n"; html += "
%1
\n" + header.arg(escape(title_)) + "
\n

\n\n"; // recipe table html += table.arg("recipe").arg(0); html += "\n\n"; html += th.arg(tr("Recipe")); html += "\n"; html += "" + escape(title_) + "\n"; html += "\n"; html += th.arg(tr("Style")); html += "\n"; html += "" + escape(style_.name()) + "\n"; html += "\n\n"; html += th.arg(tr("Brewer")); html += "\n"; html += "" + escape(brewer_) + "\n"; html += "\n"; html += th.arg(tr("Batch")); html += "\n"; html += "" + size_.toString(2) + "\n"; html += "\n\n" + th.arg(tr(method().toUtf8())); html += "\n\n\n

\n\n"; // characteristics table html += header.arg(tr("Recipe Characteristics")); html += table.arg("characteristics").arg(0); html += "\n\n"; html += th.arg(tr("Recipe Gravity")); html += "\n"; html += "" + QString::number(og_, 'f', 3) + tr(" OG\n"); html += "\n"; html += th.arg(tr("Estimated FG")); html += "\n"; html += "" + QString::number(FGEstimate(), 'f', 3) + tr(" FG\n"); html += "\n\n"; html += th.arg(tr("Recipe Bitterness")); html += "\n"; html += "" + QString::number(ibu_, 'f', 0) + tr(" IBU\n"); html += "\n"; html += th.arg(tr("Alcohol by Volume")); html += "\n"; html += "" + QString::number(ABV() * 100.0, 'f', 1) + tr("%\n"); html += "\n\n"; html += th.arg(tr("Recipe Color")); html += "\n"; html += "" + QString::number(srm_, 'f', 0) + DEGREE + tr(" SRM\n"); html += "\n"; html += th.arg(tr("Alcohol by Weight")); html += "\n"; html += "" + QString::number(ABW() * 100.0, 'f', 1) + tr("%\n"); html += "\n\n\n

\n\n"; // ingredients table html += header.arg(tr("Ingredients")); html += table.arg("ingredients").arg(0); html += "\n"; // grains html += "\n" + th.arg(tr("Quantity")); html += "\n"; html += th.arg(tr("Grain")); html += "\n"; html += th.arg(tr("Type")); html += "\n"; html += th.arg(tr("Use")); html += "\n\n"; foreach (Grain grain, grains_) { html += "\n" + grain.weight().toString(2) + "\n"; html += "\n"; html += "" + escape(grain.name()) + "\n"; html += "\n"; html += "" + grain.type() + "\n"; html += "\n"; html += "" + grain.use() + "\n\n\n"; } // hops html += "\n" + th.arg(tr("Quantity")); html += "\n"; html += th.arg(tr("Hop")); html += "\n"; html += th.arg(tr("Type")); html += "\n"; html += th.arg(tr("Time")); html += "\n\n"; foreach (Hop hop, hops_) { html += "\n" + hop.weight().toString(2) + "\n"; html += "\n"; html += "" + escape(hop.name()) + "\n"; html += "\n"; html += "" + hop.type() + "\n"; html += "\n"; html += "" + QString::number(hop.time()) + tr(" minutes\n\n\n"); } // misc ingredients html += "\n" + th.arg(tr("Quantity")); html += "\n"; html += th.arg(tr("Misc")); html += "\n"; html += th.arg(tr("Notes")); html += "\n\n"; foreach (Misc misc, miscs_) { html += "\n" + misc.quantity().toString(2) + "\n"; html += "\n"; html += "" + escape(misc.name()) + "\n"; html += "\n"; html += "" + misc.type() + "\n"; html += "\n"; html += "" + escape(misc.notes()) + "\n\n\n"; } html += "\n\n

\n\n"; // notes // TODO: using replace() might be dangerous if we ever use richtext in notes html += header.arg(tr("Recipe Notes")) + "\n"; html += "

" + escape(recipenotes_).replace('\n', "
\n") + "\n

\n

\n"; html += header.arg(tr("Batch Notes")) + "\n"; html += "

" + escape(batchnotes_).replace('\n', "
\n") + "\n

\n"; html += "\n\n"; return html; } ////////////////////////////////////////////////////////////////////////////// // exportText() // ------------ // Export recipe as text bool Recipe::exportText(const QString &filename) { if (!filename.isEmpty()) { QFile datafile(filename); if (!datafile.open(QFile::WriteOnly | QFile::Text)) { // error opening file qWarning() << "Error: Cannot open file" << filename; QMessageBox::warning(0, TITLE, QObject::tr("Cannot write file %1:\n%2") .arg(filename) .arg(datafile.errorString())); datafile.close(); return false; } QTextStream data(&datafile); QApplication::setOverrideCursor(Qt::WaitCursor); data << recipeText(); QApplication::restoreOverrideCursor(); datafile.close(); return true; } return false; } ////////////////////////////////////////////////////////////////////////////// // recipeText() // ------------ // Get the ascii text of the recipe for exporting const QString Recipe::recipeText() { // title stuff QString text = underline(title()); text += tr("Brewer: ") + brewer_ + '\n'; text += tr("Style: ") + style_.name() + '\n'; text += tr("Batch: ") + size_.toString(2); text += tr(method().toUtf8()); text += "\n\n"; // style stuff text += underline(tr("Characteristics")); text += tr("Recipe Gravity: ") + QString::number(og_, 'f', 3) + tr(" OG\n"); text += tr("Recipe Bitterness: ") + QString::number(ibu_, 'f', 0) + tr(" IBU\n"); text += tr("Recipe Color: ") + QString::number(srm_, 'f', 0) + DEGREE + tr(" SRM\n"); text += tr("Estimated FG: ") + QString::number(FGEstimate(), 'f', 3) + '\n'; text += tr("Alcohol by Volume: ") + QString::number(ABV() * 100.0, 'f', 1) + tr("%\n"); text += tr("Alcohol by Weight: ") + QString::number(ABW() * 100.0, 'f', 1) + tr("%\n\n"); // ingredients text += underline(tr("Ingredients")); // grains // using a map will sort the grains QMultiMap gmap; foreach (Grain grain, grains_) gmap.insert(grain.name(), grain); foreach (Grain grain, gmap.values()) { text += grain.name().leftJustified(30, ' '); text += grain.weight().toString(2) + ", "; text += grain.type() + ", "; text += grain.use() + '\n'; } text += '\n'; // hops // using a map will sort the hops QMultiMap hmap; foreach (Hop hop, hops_) hmap.insert(hop.name(), hop); foreach (Hop hop, hmap.values()) { text += hop.name().leftJustified(30, ' '); text += hop.weight().toString(2) + ", "; text += hop.type() + ", "; text += QString::number(hop.time()) + tr(" minutes\n"); } text += '\n'; // misc ingredients // using a map will sort the ingredients QMultiMap mmap; foreach (Misc misc, miscs_) mmap.insert(misc.name(), misc); foreach (Misc misc, mmap.values()) { text += misc.name().leftJustified(30, ' '); text += misc.quantity().toString(2) + ", "; text += misc.type() + ", "; text += misc.notes() + '\n'; } text += '\n'; // notes text += underline(tr("Notes")); // TODO: wrap long notes text += tr("Recipe Notes:\n") + recipenotes_ + "\n\n"; text += tr("Batch Notes:\n") + batchnotes_ + "\n\n"; return text; } ////////////////////////////////////////////////////////////////////////////// // exportBeerXML() // --------------- // Export recipe to BeerXML format bool Recipe::exportBeerXML(const QString &filename) { // open file QFile datafile(filename); if (!datafile.open(QFile::WriteOnly | QFile::Text)) { // error opening file qWarning() << "Error: Cannot open file" << filename; QMessageBox::warning(0, TITLE, QObject:: tr("Cannot write file %1:\n%2") .arg(filename) .arg(datafile.errorString())); datafile.close(); return false; } QApplication::setOverrideCursor(Qt::WaitCursor); // write out xml BeerXmlWriter writer(&datafile); writer.writeRecipe(this); datafile.close(); // recipe is saved, so set flags accordingly setModified(false); QApplication::restoreOverrideCursor(); return true; } ////////////////////////////////////////////////////////////////////////////// // importBeerXml() // --------------- // Import recipe from BeerXML format. Assumes beerXmlFormat() has been called bool Recipe::importBeerXml(const QString &filename) { // open file QFile datafile(filename); if (!datafile.open(QFile::ReadOnly | QFile::Text)) { // error opening file qWarning() << "Error: Cannot open" << filename; QMessageBox::warning(0, TITLE, QObject::tr("Cannot read file %1:\n%2") .arg(filename) .arg(datafile.errorString())); datafile.close(); return false; } QApplication::setOverrideCursor(Qt::WaitCursor); title_.clear(); brewer_.clear(); size_ = Data::instance()->defaultSize(); style_ = Data::instance()->defaultStyle(); grains_.clear(); hops_.clear(); miscs_.clear(); recipenotes_.clear(); batchnotes_.clear(); // parse file BeerXmlReader reader(&datafile); bool status = reader.readRecipe(this); datafile.close(); if (!status) { qWarning() << "Error: Problem reading file" << filename; qWarning() << reader.errorString(); QMessageBox::warning(0, TITLE, tr("Error reading file %1").arg(filename)); QApplication::restoreOverrideCursor(); return false; } // calculate the numbers recalc(); // just loaded recipes are not modified setModified(false); emit recipeChanged(); QApplication::restoreOverrideCursor(); return true; } ////////////////////////////////////////////////////////////////////////////// // exportPdf() // ------------ // Export recipe as pdf bool Recipe::exportPdf(TextPrinter *textprinter, const QString &filename) { if (textprinter && (!filename.isEmpty())) { QTextDocument document; document.setHtml(recipeHTML()); textprinter->exportPdf(&document, QString(), filename); return true; } return false; }