| 1 | #include "PagePrinter.h" |
| 2 | #include "Stationery.h" |
| 3 | #include "PrintRunner.h" |
| 4 | #include "Environment.h" |
| 5 | #include "HerculesStudio.h" |
| 6 | |
| 7 | #include <QHostAddress> |
| 8 | #include <QTcpSocket> |
| 9 | #include <QPainter> |
| 10 | #include <QPdfWriter> |
| 11 | #include <QFile> |
| 12 | #include <QFileInfo> |
| 13 | #include <QDir> |
| 14 | |
| 15 | PagePrinter::PagePrinter(StationeryPtr &stationery, QPainter &painter) : |
| 16 | GenericPagePrinter(stationery, 0.2) |
| 17 | { |
| 18 | setBarsColor(); |
| 19 | mPainter = &painter; |
| 20 | mPen = mPainter; |
| 21 | |
| 22 | mPaperHeight = mStationery->mSize1.toFloat() *25.4; |
| 23 | mPaperWidth = mStationery->mSize2.toFloat() * 25.4; |
| 24 | mLinesPerPage = mPaperHeight * mLinesPerInch; |
| 25 | } |
| 26 | |
| 27 | void PagePrinter::setValues(bool) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | void PagePrinter::setBarsColor() |
| 32 | { |
| 33 | mStationery->getBarsColor(mBarsColor); |
| 34 | } |
| 35 | |
| 36 | bool PagePrinter::print(QByteArray) |
| 37 | { |
| 38 | preparePage(true); |
| 39 | return true; |
| 40 | } |
| 41 | |
| 42 | void PagePrinter::close() |
| 43 | { |
| 44 | |
| 45 | } |
| 46 | |
| 47 | PdfPrinter::PdfPrinter(const QString & dir, StationeryPtr &stationery, DecolationRulesPtr &decolation, int linesPerInch, int fontSize) : |
| 48 | GenericPagePrinter(stationery, 1.0), mPdf(NULL), mDir(dir), mFontSize(fontSize) |
| 49 | { |
| 50 | mDecolation = decolation; |
| 51 | float paperHeight = mStationery->mSize1.toFloat(); |
| 52 | mLinesPerInch = linesPerInch; |
| 53 | mLinesPerPage = paperHeight * mLinesPerInch; |
| 54 | mCurPage = 0; |
| 55 | mCurLine = 0; |
| 56 | mLastPages = decolation->mSeperator.mPages; |
| 57 | mLastPagesCountdown = 0; |
| 58 | |
| 59 | setBarsColor(); |
| 60 | } |
| 61 | |
| 62 | void PdfPrinter::setBarsColor() |
| 63 | { |
| 64 | mStationery->getBarsColor(mBarsColor); |
| 65 | } |
| 66 | |
| 67 | void PdfPrinter::setValues(bool eject) |
| 68 | { |
| 69 | if (mPdf == NULL) return; |
| 70 | mLeftMargin = mPdf->logicalDpiX()/2; // 1/2 inch for margin |
| 71 | mPixelsPerLine = (double)mPdf->logicalDpiY()/(double)mLinesPerInch; |
| 72 | mMargin = mStationery->mTopMargin / 16.0 * mPdf->logicalDpiY(); //16 is inch factor (5/16 is kept as 5) |
| 73 | mBottomMargin = 0.375L * mPdf->logicalDpiY(); |
| 74 | mPaperHeight = mStationery->mSize1.toFloat(); |
| 75 | mPaperWidth = mStationery->mSize2.toFloat(); |
| 76 | if (mStationery->mPaperSize.compare("Custom")==0 && !mStationery->mPortrait) |
| 77 | { |
| 78 | mPaperHeight = mStationery->mSize2.toFloat(); |
| 79 | mPaperWidth = mStationery->mSize1.toFloat(); |
| 80 | } |
| 81 | |
| 82 | mLinesPerPage = mPaperHeight * mLinesPerInch; |
| 83 | |
| 84 | if (eject) mPdf->newPage(); |
| 85 | mPdf->setPageSizeMM(QSize(mPaperWidth*25.4L, mPaperHeight*25.4L)); |
| 86 | mWidthInPixels = mPdf->width(); |
| 87 | mHeightInPixels = mPdf->height(); |
| 88 | mLogicalDpiX = mPdf->logicalDpiX(); |
| 89 | mLogicalDpiY = mPdf->logicalDpiY(); |
| 90 | |
| 91 | if (!mPen) mPen = new QPainter(mPdf); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | bool PdfPrinter::print(QByteArray line) |
| 96 | { |
| 97 | // if reached maximum number of lines or eject found - advance to next page |
| 98 | if ((mCurLine >= mLinesPerPage) || (line[0] == '\f') || mCurPage == 0) |
| 99 | { |
| 100 | mCurPage++; |
| 101 | mCurLine = 0; |
| 102 | if (mLastPagesCountdown > 0) |
| 103 | { |
| 104 | if (--mLastPagesCountdown == 0) |
| 105 | { |
| 106 | mCurPage = mCurLine = 0; |
| 107 | close(); |
| 108 | } |
| 109 | } |
| 110 | if (line[0] == (char)12) |
| 111 | return true; // not a real line |
| 112 | } |
| 113 | |
| 114 | mCurLine++; |
| 115 | |
| 116 | if (mCurPage == 1) |
| 117 | { |
| 118 | if (mCurLine == 1) |
| 119 | { |
| 120 | mAccountingFields.clear(); |
| 121 | |
| 122 | for (int i=0; i<mDecolation->mAccounting.mFields.count(); i++) |
| 123 | { |
| 124 | AccountingField& f = mDecolation->mAccounting.mFields[i]; |
| 125 | mAccountingFields.insert(f.mLine, f); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | auto fi = mAccountingFields.find(mCurLine); |
| 130 | for (; fi != mAccountingFields.end(); fi++) |
| 131 | { |
| 132 | if (mCurLine == fi->mLine) |
| 133 | { |
| 134 | QString fld = line.mid(fi.value().mColumn-1, fi.value().mLength); |
| 135 | mVariables.insert(fi.value().mName, fld); |
| 136 | hOutDebug(3, "FLD:" << fi.value().mName.toStdString() << "=" << fld.toStdString()); |
| 137 | } |
| 138 | } |
| 139 | mFirstPage.push_back(line); |
| 140 | return true; |
| 141 | } |
| 142 | |
| 143 | if (mCurPage == 2 && mCurLine == 1) |
| 144 | { |
| 145 | mCurPage = 1; |
| 146 | mCurLine = 1; |
| 147 | QString decodedFileName = decodeFilename(); //TODO handle file tye (.pdf etc) |
| 148 | QString newFileName = mDir + "/" + decodedFileName; |
| 149 | QFileInfo fileInfo(newFileName); |
| 150 | QDir dir = fileInfo.absoluteDir(); |
| 151 | if (!dir.exists()) |
| 152 | { |
| 153 | if (!dir.mkpath(dir.absolutePath())) |
| 154 | { |
| 155 | mDir = "/tmp/"; |
| 156 | newFileName = mDir + decodedFileName; |
| 157 | fileInfo.setFile(newFileName); |
| 158 | dir = fileInfo.absoluteDir(); |
| 159 | dir.mkpath(dir.absolutePath()); |
| 160 | } |
| 161 | } |
| 162 | mPen = NULL; |
| 163 | mPdf = new QPdfWriter(newFileName); //TODO handle invalid filename (pdf is still created, but is not working) |
| 164 | mVariables.clear(); |
| 165 | |
| 166 | mPdf->setTitle(decodedFileName); |
| 167 | mPdf->setCreator("Hercules Studio"); |
| 168 | while(mFirstPage.count() > 0) |
| 169 | { |
| 170 | QByteArray l = mFirstPage.front(); |
| 171 | mFirstPage.pop_front(); |
| 172 | doPrint(l); |
| 173 | mCurLine++; |
| 174 | } |
| 175 | mCurPage = 2; |
| 176 | mCurLine = 1; |
| 177 | } |
| 178 | return doPrint(line); |
| 179 | } |
| 180 | |
| 181 | bool PdfPrinter::doPrint(QByteArray &line) |
| 182 | { |
| 183 | if (mCurLine == 1) |
| 184 | { |
| 185 | preparePage(mCurPage > 1); |
| 186 | |
| 187 | mPen->setPen(QPen(QColor(0x80, 0x80, 0x80), 1)); |
| 188 | #ifdef Q_OS_DARWIN |
| 189 | mPen->setFont(QFont("Courier",mFontSize)); |
| 190 | #else |
| 191 | mPen->setFont(QFont("Mono",mFontSize)); |
| 192 | #endif |
| 193 | } |
| 194 | if (mPdf != NULL) |
| 195 | mPen->drawText(mPdf->logicalDpiX()/2,mMargin+mCurLine*mPixelsPerLine, line); |
| 196 | |
| 197 | hOutDebug(1, "line " << mCurLine << " page " << mCurPage << " " << line.data()); |
| 198 | |
| 199 | if (mLastPagesCountdown == 0) |
| 200 | { |
| 201 | if (endOfJob(line)) |
| 202 | { |
| 203 | mLastPagesCountdown = mLastPages; |
| 204 | } |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
| 209 | void PdfPrinter::close() |
| 210 | { |
| 211 | if (mPen != NULL && mPen->isActive()) mPen->end(); |
| 212 | delete mPdf; |
| 213 | mPdf = NULL; |
| 214 | } |
| 215 | |
| 216 | bool PdfPrinter::endOfJob(QByteArray &line) |
| 217 | { |
| 218 | auto it = mEndJobTrigger.find(mCurLine); |
| 219 | while(it != mEndJobTrigger.end() && it->line() == mCurLine) |
| 220 | { |
| 221 | if (QString(line.mid(it->column()-1, it->text().length())).compare(it->text(), Qt::CaseInsensitive) == 0) |
| 222 | { |
| 223 | mEndJobTrigger.erase(it); |
| 224 | if (mEndJobTrigger.count() == 0) break; |
| 225 | it = mEndJobTrigger.find(mCurLine); |
| 226 | } |
| 227 | else return false; |
| 228 | } |
| 229 | if (mEndJobTrigger.count() != 0) return false; |
| 230 | return true; |
| 231 | } |
| 232 | |
| 233 | QString PdfPrinter::decodeFilename() |
| 234 | { |
| 235 | QString ret = mDecolation->mFileNameTemplate.toLower(); |
| 236 | for (auto it = mVariables.begin();it != mVariables.end(); it++) |
| 237 | { |
| 238 | QString str = "$" + it.key().toLower() + "$"; |
| 239 | ret.replace(str, it.value().trimmed()); |
| 240 | } |
| 241 | hOutDebug(0,"filename:" << ret.toStdString() << " " << mVariables.count()); |
| 242 | return ret; |
| 243 | } |