~scottydelicious666/brewtarget/brewtarget

« back to all changes in this revision

Viewing changes to src/BrewDayWidget.cpp

  • Committer: Mik Firestone
  • Date: 2010-11-12 05:28:03 UTC
  • Revision ID: git-v1:93ad4d2fc81166e80813645c6201476281b5306e
Adding print functionality to the brewday instructions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 */
18
18
 
19
19
#include "instruction.h"
 
20
#include "brewtarget.h"
20
21
#include "BrewDayWidget.h"
21
22
#include <QListWidgetItem>
 
23
#include <QPrinter>
 
24
#include <QPrintDialog>
 
25
#include <QDate>
 
26
#include <QVector>
22
27
#include "InstructionWidget.h"
23
28
#include "TimerWidget.h"
24
29
 
39
44
   connect( pushButton_remove, SIGNAL(clicked()), this, SLOT(removeSelectedInstruction()) );
40
45
   connect( pushButton_up, SIGNAL(clicked()), this, SLOT(pushInstructionUp()) );
41
46
   connect( pushButton_down, SIGNAL(clicked()), this, SLOT(pushInstructionDown()) );
 
47
   connect( pushButton_print, SIGNAL(clicked()), this, SLOT(pushInstructionPrint()) );
 
48
 
 
49
   /* Instantiate the Webview and then connect its signal */
 
50
   doc = new QWebView();
 
51
   connect( doc, SIGNAL(loadFinished(bool)), this, SLOT(loadComplete(bool)) );
 
52
 
 
53
   printer = new QPrinter;
 
54
   printer->setPageSize(QPrinter::Letter);
42
55
}
43
56
 
44
57
QSize BrewDayWidget::sizeHint() const
83
96
   listWidget->setCurrentRow(row+1);
84
97
}
85
98
 
 
99
QString BrewDayWidget::getCSS() 
 
100
{
 
101
   QFile cssInput(":/css/brewday.css");
 
102
   QString css;
 
103
 
 
104
   if (cssInput.open(QFile::ReadOnly)) {
 
105
      QTextStream inStream(&cssInput);
 
106
      while ( ! inStream.atEnd() )
 
107
      {
 
108
         css += inStream.readLine();
 
109
      }
 
110
   }
 
111
   return css;
 
112
}
 
113
 
 
114
QString BrewDayWidget::buildTitleTable()
 
115
{
 
116
   QString header;
 
117
   QString body;
 
118
 
 
119
   // Do the style sheet first
 
120
   header = "<html><head><style type=\"text/css\">";
 
121
   header += getCSS();
 
122
   header += "</style></head>";
 
123
 
 
124
   body   = "<body>";
 
125
   body += tr("<h1>%1</h1>").arg(recObs->getName().c_str());
 
126
   body += tr("<img src=\"%1\" />").arg("qrc:/images/title.svg");
 
127
 
 
128
   // Build the top table
 
129
   // Build the first row: Style and Date
 
130
   body += "<table id=\"title\">";
 
131
   body += tr("<tr><td class=\"left\">Style</td>");
 
132
   body += tr("<td class=\"value\">%1</td>")
 
133
           .arg(recObs->getStyle()->getName().c_str());
 
134
   body += tr("<td class=\"right\">Date</td>");
 
135
   body += tr("<td class=\"value\">%1</td></tr>")
 
136
           .arg(QDate::currentDate().toString());
 
137
 
 
138
   body += tr("<tr><td class=\"left\">Boil Volume</td><td class=\"value\">%1</td><td class=\"right\">Preboil Gravity</td><td class=\"value\">%2</td></tr>")
 
139
           .arg(Brewtarget::displayAmount(recObs->getBatchSize_l(), Units::liters))
 
140
           .arg(Brewtarget::displayOG(recObs->getBoilGrav()));
 
141
 
 
142
   body += tr("<tr><td class=\"left\">Final Volume</td><td class=\"value\">%1</td><td class=\"right\">Final Gravity</td><td class=\"value\">%2</td></tr>")
 
143
           .arg(Brewtarget::displayAmount(recObs->getBoilSize_l(),Units::liters))
 
144
           .arg(Brewtarget::displayOG(recObs->getOg()));
 
145
 
 
146
   body += tr("<tr><td class=\"left\">Boil Time</td><td class=\"value\">%1</td><td class=\"right\">IBU</td><td class=\"value\">%2</td></tr>")
 
147
           .arg(Brewtarget::displayAmount(recObs->getBoilTime_min(),Units::minutes))
 
148
           .arg(recObs->getIBU(),0,'f',1);
 
149
 
 
150
   body += tr("<tr><td class=\"left\">Predicted Efficiency</td><td class=\"value\">%1</td></tr>")
 
151
           .arg(Brewtarget::displayAmount(recObs->getEfficiency_pct(),0));
 
152
 
 
153
   body += "</table>";
 
154
 
 
155
   return header + body;
 
156
 
 
157
}
 
158
 
 
159
bool BrewDayWidget::loadComplete(bool ok) 
 
160
{
 
161
   doc->print(printer);
 
162
   return ok;
 
163
}
 
164
 
 
165
void BrewDayWidget::pushInstructionPrint()
 
166
{
 
167
   QString pDoc;
 
168
   int i, j, size;
 
169
 
 
170
 
 
171
   QPrintDialog *dialog = new QPrintDialog(printer, this);
 
172
   dialog->setWindowTitle(tr("Print Document"));
 
173
   if (dialog->exec() != QDialog::Accepted)
 
174
      return;
 
175
 
 
176
   if( recObs == 0 )
 
177
      return;
 
178
 
 
179
   // Start building the document to be printed.  I think.
 
180
   size = recObs->getNumInstructions();
 
181
   pDoc = buildTitleTable();
 
182
 
 
183
   // Start the instructions header
 
184
   pDoc += "<h2>Instructions</h2>";
 
185
   pDoc += "<table id=\"steps\">";
 
186
   pDoc += tr("<tr><th class=\"check\">Completed</th><th class=\"time\">Time</th><th class=\"step\">Step</th></tr>");
 
187
   for( i = 0; i < size; ++i )
 
188
   {
 
189
      QString stepTime, tmp;
 
190
      QVector<QString> reagents;
 
191
 
 
192
      if (recObs->getInstruction(i)->getInterval())
 
193
         stepTime = Brewtarget::displayAmount(recObs->getInstruction(i)->getInterval(), Units::minutes, 0);
 
194
      else
 
195
         stepTime = "--";
 
196
 
 
197
      tmp = "";
 
198
      reagents = recObs->getInstruction(i)->getReagents();
 
199
      if ( reagents.size() > 1 ) {
 
200
         tmp = tr("<ul>");
 
201
         for ( j = 0; j < reagents.size(); j++ ) 
 
202
         {
 
203
            tmp += tr("<li>%1</li>")
 
204
                   .arg(reagents[j]);
 
205
         }
 
206
         tmp += tr("</ul>");
 
207
      }
 
208
      else {
 
209
         tmp = reagents[0];
 
210
      }
 
211
 
 
212
      QString altTag = i % 2 ? "alt" : "norm";
 
213
 
 
214
      pDoc += tr("<tr class=\"%1\"><td class=\"check\"></td><td class=\"time\">%2</td><td align=\"step\">%3 : %4</td></tr>")
 
215
               .arg(altTag)
 
216
               .arg(stepTime)
 
217
               .arg(recObs->getInstruction(i)->getName())
 
218
               .arg(tmp);
 
219
   }
 
220
   pDoc += "</table></body></html>";
 
221
 
 
222
   doc->setHtml(pDoc);
 
223
}
 
224
 
86
225
void BrewDayWidget::setRecipe(Recipe* rec)
87
226
{
88
227
   recObs = rec;