#include "dialogimpl.h"//DialogImpl::DialogImpl( QWidget * parent, Qt::WFlags f) : QDialog(parent, f){ setupUi(this); connect (btn1, SIGNAL(clicked()), this, SLOT(click()));}//void DialogImpl:: click(){ QPrinter printer; printer.setOutputFormat(QPrinter::PdfFormat); printer.setPageSize(QPrinter:: A4); printer.setOrientation(QPrinter:: Landscape); printer.setOutputFileName("sample.pdf"); QPainter paint(&printer); //paint.setRenderHint(QPainter::Antialiasing, true); setTemplate(paint, "ttn.xml");}//----------------------------------------------void DialogImpl :: setTemplate(QPainter &painter, QString filename){ QDomDocument domDoc; QFile file(filename); if (file.open(QIODevice::ReadOnly)) { if(domDoc.setContent(&file) ) { //lib.qMessageBox("setTemplate","fileSetContent"); QDomElement domElement= domDoc.documentElement(); traversNode (domElement, painter); } file.close(); }}//---------------------------------------------void DialogImpl :: drawTextNode(QPainter &paint, QRect rect, QString text, QFont font){ QRectF r(rect); //------------------------------------------------ paint.setFont(font); QTextOption opt; opt.setWrapMode(QTextOption :: WordWrap); paint.drawText(r, text, opt);}//-----------------------------------------------------------------------void DialogImpl :: traversNode(const QDomNode& node, QPainter &painter){ QDomNode domNode = node.firstChild(); while(!domNode.isNull()) { if(domNode.isElement()) { QDomElement domElement = domNode.toElement(); if(!domElement.isNull()) { if(domElement.tagName() == "textblock") { int x = domElement.attribute("x", "").toInt(); int y = domElement.attribute("y", "").toInt(); int w = domElement.attribute("w", "").toInt(); int h = domElement.attribute("h", "").toInt(); int fs = domElement.attribute("fontsize", "").toInt(); QString fontfml = domElement.attribute("fontfamily", ""); QString fontstl = domElement.attribute("fontstyle", ""); QString text = domElement.text(); QRect rect(x, y, w, h); QFont font(fontfml, fs, QFont::Normal); if (fontstl == "bold") { lib.qMessageBox("", ""); font.setBold(true);} //font.setWeight(150); drawTextNode(painter, rect, text, font); } if(domElement.tagName() == "textcell") { int x = domElement.attribute("x", "").toInt(); int y = domElement.attribute("y", "").toInt(); int w = domElement.attribute("w", "").toInt(); int h = domElement.attribute("h", "").toInt(); int fs = domElement.attribute("fontsize", "").toInt(); QString fontfml = domElement.attribute("fontfamily", ""); QString fontstl = domElement.attribute("fontstyle", ""); QString text = domElement.text(); QRect rect(x, y - 12, w, h + 12); QFont font(fontfml, fs, QFont::Normal); if (fontstl == "bold") font.setBold(true); drawTextCell(painter, rect, text, font); } if(domElement.tagName() == "rect") { int x = domElement.attribute("x", "").toInt(); int y = domElement.attribute("y", "").toInt(); int w = domElement.attribute("w", "").toInt(); int h = domElement.attribute("h", "").toInt(); int lw = domElement.attribute("linewidth", "").toInt(); QRect rect(x, y, w, h); drawRectNode(painter, rect, lw); } if(domElement.tagName() == "line") { int x = domElement.attribute("x", "").toInt(); int y = domElement.attribute("y", "").toInt(); int l = domElement.attribute("l", "").toInt(); drawLineNode(painter, x, y, l); } } } traversNode (domNode, painter); domNode = domNode.nextSibling(); }}//-------------------------------------------------------------------------void DialogImpl :: drawRectNode(QPainter &paint, QRect rect, int width){ //------------------------------------------------ QPen pen; pen.setStyle(Qt:: SolidLine); pen.setColor(QColor(0,0,0)); pen.setWidth(width); paint.setPen(pen); paint.setBrush(Qt::NoBrush); paint.drawRect(rect);}//-----------------------------------------------------void DialogImpl :: drawTextCell(QPainter &paint, QRect rect, QString text, QFont font){ paint.setFont(font); paint.setBrush(Qt::NoBrush); paint.drawText(rect, Qt::AlignCenter, text); }//--------------------------------------------------------void DialogImpl :: drawLineNode(QPainter &paint, int x, int y, int length){ QPen pen; pen.setStyle(Qt:: SolidLine); pen.setColor(QColor(0, 0, 0)); pen.setWidth(1); paint.setPen(pen); paint.drawLine(x, y, length + x, y);}//--------------------------------------------------------