From 54bfcca5a90e82b2e6c816acd09a870233fd8952 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sun, 30 Aug 2015 20:13:27 -0400 Subject: [PATCH] Added XmlTemplateCreator class. --- libglabels/CMakeLists.txt | 1 + libglabels/XmlTemplateCreator.cpp | 332 ++++++++++++++++++++++++++++++ libglabels/XmlTemplateCreator.h | 68 ++++++ 3 files changed, 401 insertions(+) create mode 100644 libglabels/XmlTemplateCreator.cpp create mode 100644 libglabels/XmlTemplateCreator.h diff --git a/libglabels/CMakeLists.txt b/libglabels/CMakeLists.txt index 14caee6..c1d7a84 100644 --- a/libglabels/CMakeLists.txt +++ b/libglabels/CMakeLists.txt @@ -24,6 +24,7 @@ set (libglabels_sources XmlPaperParser.cpp XmlVendorParser.cpp XmlTemplateParser.cpp + XmlTemplateCreator.cpp XmlUtil.cpp MiniPreviewPixmap.cpp ) diff --git a/libglabels/XmlTemplateCreator.cpp b/libglabels/XmlTemplateCreator.cpp new file mode 100644 index 0000000..0dfcff4 --- /dev/null +++ b/libglabels/XmlTemplateCreator.cpp @@ -0,0 +1,332 @@ +/* XmlTemplateCreator.cpp + * + * Copyright (C) 2013 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#include "XmlTemplateCreator.h" + +#include +#include +#include +#include + +#include "Template.h" +#include "XmlUtil.h" +#include "Db.h" + + +namespace libglabels +{ + + bool XmlTemplateCreator::writeTemplates( const QList tmplates, const QString &fileName ) + { + QDomDocument doc( "Glabels-templates" ); + QDomElement root = doc.createElement( "Glabels-templates" ); + doc.appendChild( root ); + + foreach ( const Template* tmplate, tmplates ) + { + createTemplateNode( root, tmplate ); + } + + QFile file( fileName ); + + if ( !file.open( QFile::WriteOnly | QFile::Text) ) + { + qWarning() << "Error: Cannot open file " << fileName + << ": " << file.errorString(); + return false; + } + + + if ( file.write( doc.toByteArray() ) < 0 ) + { + qWarning() << "Error: Cannot write file " << fileName + << ": " << file.errorString(); + return false; + } + + return true; + } + + + bool XmlTemplateCreator::writeTemplate( const Template* tmplate, const QString& fileName ) + { + QList tmplates; + + tmplates.append(tmplate); + + return writeTemplates( tmplates, fileName ); + } + + + void XmlTemplateCreator::createTemplateNode( QDomElement &parent, const Template* tmplate ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Template" ); + parent.appendChild( node ); + + XmlUtil::setStringAttr( node, "brand", tmplate->brand() ); + XmlUtil::setStringAttr( node, "part", tmplate->part() ); + + XmlUtil::setStringAttr( node, "size", tmplate->paperId() ); + if ( tmplate->isSizeOther() ) + { + XmlUtil::setLengthAttr( node, "width", tmplate->pageWidth() ); + XmlUtil::setLengthAttr( node, "height", tmplate->pageWidth() ); + } + + + XmlUtil::setStringAttr( node, "description", tmplate->description() ); + + createMetaNode( node, "product_url", tmplate->productUrl() ); +#if TODO + foreach ( QString categoryId, tmplate->categoryIds() ) + { + createMetaNode( node, "category", categoryId ); + } +#endif + + foreach ( Frame* frame, tmplate->frames() ) + { + createLabelNode( node, frame ); + } + } + + + void XmlTemplateCreator::createMetaNode( QDomElement &parent, const QString& attr, const QString& value ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Meta" ); + parent.appendChild( node ); + + XmlUtil::setStringAttr( node, attr, value ); + } + + + void XmlTemplateCreator::createLabelNode( QDomElement &parent, const Frame* frame ) + { + if ( const FrameRect* frameRect = dynamic_cast(frame) ) + { + createLabelRectangleNode( parent, frameRect ); + } + else if ( const FrameEllipse* frameEllipse = dynamic_cast(frame) ) + { + createLabelEllipseNode( parent, frameEllipse ); + } + else if ( const FrameRound* frameRound = dynamic_cast(frame) ) + { + createLabelRoundNode( parent, frameRound ); + } + else if ( const FrameCd* frameCd = dynamic_cast(frame) ) + { + createLabelCdNode( parent, frameCd ); + } + else + { + Q_ASSERT_X( false, "XmlTemplateCreator::createLabelNode", "Invalid frame type." ); + } + } + + + void XmlTemplateCreator::createLabelRectangleNode( QDomElement &parent, const FrameRect* frame ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Label-rectangle" ); + parent.appendChild( node ); + + XmlUtil::setStringAttr( node, "id", frame->id() ); + XmlUtil::setLengthAttr( node, "width", frame->w() ); + XmlUtil::setLengthAttr( node, "height", frame->h() ); + XmlUtil::setLengthAttr( node, "round", frame->r() ); + XmlUtil::setLengthAttr( node, "x_waste", frame->xWaste() ); + XmlUtil::setLengthAttr( node, "y_waste", frame->yWaste() ); + + createLabelNodeCommon( node, frame ); + } + + + void XmlTemplateCreator::createLabelEllipseNode( QDomElement &parent, const FrameEllipse* frame ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Label-ellipse" ); + parent.appendChild( node ); + + XmlUtil::setStringAttr( node, "id", frame->id() ); + XmlUtil::setLengthAttr( node, "width", frame->w() ); + XmlUtil::setLengthAttr( node, "height", frame->h() ); + XmlUtil::setLengthAttr( node, "waste", frame->waste() ); + + createLabelNodeCommon( node, frame ); + } + + + void XmlTemplateCreator::createLabelRoundNode( QDomElement &parent, const FrameRound* frame ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Label-round" ); + parent.appendChild( node ); + + XmlUtil::setStringAttr( node, "id", frame->id() ); + XmlUtil::setLengthAttr( node, "radius", frame->r() ); + XmlUtil::setLengthAttr( node, "waste", frame->waste() ); + + createLabelNodeCommon( node, frame ); + } + + + void XmlTemplateCreator::createLabelCdNode( QDomElement &parent, const FrameCd* frame ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Label-cd" ); + parent.appendChild( node ); + + XmlUtil::setStringAttr( node, "id", frame->id() ); + XmlUtil::setLengthAttr( node, "radius", frame->r1() ); + XmlUtil::setLengthAttr( node, "hole", frame->r2() ); + XmlUtil::setLengthAttr( node, "waste", frame->waste() ); + if ( frame->w() ) + { + XmlUtil::setLengthAttr( node, "width", frame->w() ); + } + if ( frame->h() ) + { + XmlUtil::setLengthAttr( node, "height", frame->h() ); + } + + createLabelNodeCommon( node, frame ); + } + + + void XmlTemplateCreator::createLabelNodeCommon( QDomElement &node, const Frame *frame ) + { + foreach ( Markup* markup, frame->markups() ) + { + if ( MarkupMargin* markupMargin = dynamic_cast(markup) ) + { + createMarkupMarginNode( node, markupMargin ); + } + else if ( MarkupLine* markupLine = dynamic_cast(markup) ) + { + createMarkupLineNode( node, markupLine ); + } + else if ( MarkupCircle* markupCircle = dynamic_cast(markup) ) + { + createMarkupCircleNode( node, markupCircle ); + } + else if ( MarkupRect* markupRect = dynamic_cast(markup) ) + { + createMarkupRectNode( node, markupRect ); + } + else if ( MarkupEllipse* markupEllipse = dynamic_cast(markup) ) + { + createMarkupEllipseNode( node, markupEllipse ); + } + else + { + Q_ASSERT_X( false, "XmlTemplateCreator::createLabelNodeCommon", "Invalid markup type." ); + } + } + + foreach ( Layout* layout, frame->layouts() ) + { + createLayoutNode( node, layout ); + } + } + + + void XmlTemplateCreator::createLayoutNode( QDomElement& parent, const Layout* layout ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Layout" ); + parent.appendChild( node ); + + XmlUtil::setIntAttr( node, "nx", layout->nx() ); + XmlUtil::setIntAttr( node, "ny", layout->ny() ); + + XmlUtil::setLengthAttr( node, "x0", layout->x0() ); + XmlUtil::setLengthAttr( node, "y0", layout->y0() ); + + XmlUtil::setLengthAttr( node, "dx", layout->dx() ); + XmlUtil::setLengthAttr( node, "dy", layout->dy() ); + } + + + void XmlTemplateCreator::createMarkupMarginNode( QDomElement& parent, const MarkupMargin* markup ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Markup-margin" ); + parent.appendChild( node ); + + XmlUtil::setLengthAttr( node, "size", markup->size() ); + } + + + void XmlTemplateCreator::createMarkupLineNode( QDomElement& parent, const MarkupLine* markup ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Markup-line" ); + parent.appendChild( node ); + + XmlUtil::setLengthAttr( node, "x1", markup->x1() ); + XmlUtil::setLengthAttr( node, "y1", markup->y1() ); + XmlUtil::setLengthAttr( node, "x2", markup->x2() ); + XmlUtil::setLengthAttr( node, "y2", markup->y2() ); + } + + + void XmlTemplateCreator::createMarkupCircleNode( QDomElement& parent, const MarkupCircle* markup ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Markup-circle" ); + parent.appendChild( node ); + + XmlUtil::setLengthAttr( node, "x0", markup->x0() ); + XmlUtil::setLengthAttr( node, "y0", markup->y0() ); + XmlUtil::setLengthAttr( node, "radius", markup->r() ); + } + + + void XmlTemplateCreator::createMarkupRectNode( QDomElement& parent, const MarkupRect* markup ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Markup-rect" ); + parent.appendChild( node ); + + XmlUtil::setLengthAttr( node, "x1", markup->x1() ); + XmlUtil::setLengthAttr( node, "y1", markup->y1() ); + XmlUtil::setLengthAttr( node, "w", markup->w() ); + XmlUtil::setLengthAttr( node, "h", markup->h() ); + XmlUtil::setLengthAttr( node, "r", markup->r() ); + } + + + void XmlTemplateCreator::createMarkupEllipseNode( QDomElement& parent, const MarkupEllipse* markup ) + { + QDomDocument doc = parent.ownerDocument(); + QDomElement node = doc.createElement( "Markup-ellipse" ); + parent.appendChild( node ); + + XmlUtil::setLengthAttr( node, "x1", markup->x1() ); + XmlUtil::setLengthAttr( node, "y1", markup->y1() ); + XmlUtil::setLengthAttr( node, "w", markup->w() ); + XmlUtil::setLengthAttr( node, "h", markup->h() ); + } + +} + diff --git a/libglabels/XmlTemplateCreator.h b/libglabels/XmlTemplateCreator.h new file mode 100644 index 0000000..3607a82 --- /dev/null +++ b/libglabels/XmlTemplateCreator.h @@ -0,0 +1,68 @@ +/* XmlTemplateCreator.h + * + * Copyright (C) 2013 Jim Evins + * + * This file is part of gLabels-qt. + * + * gLabels-qt is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * gLabels-qt is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with gLabels-qt. If not, see . + */ + +#ifndef libglabels_XmlTemplateCreator_h +#define libglabels_XmlTemplateCreator_h + + +#include +#include + +#include "Template.h" +#include "FrameRect.h" +#include "FrameCd.h" +#include "FrameRound.h" +#include "FrameEllipse.h" +#include "Layout.h" +#include "Markup.h" + + +namespace libglabels +{ + + class XmlTemplateCreator + { + public: + XmlTemplateCreator() {} + + bool writeTemplates( const QList tmplates, const QString& fileName ); + bool writeTemplate( const Template* tmplate, const QString& fileName ); + void createTemplateNode( QDomElement& parent, const Template* tmplate ); + + private: + void createMetaNode( QDomElement& parent, const QString& attr, const QString& value ); + void createLabelNode( QDomElement& parent, const Frame* frame ); + void createLabelRectangleNode( QDomElement& parent, const FrameRect* frame ); + void createLabelEllipseNode( QDomElement& parent, const FrameEllipse* frame ); + void createLabelRoundNode( QDomElement& parent, const FrameRound* frame ); + void createLabelCdNode( QDomElement& parent, const FrameCd* frame ); + void createLabelNodeCommon( QDomElement& node, const Frame* frame ); + void createLayoutNode( QDomElement& parent, const Layout* layout ); + void createMarkupMarginNode( QDomElement& parent, const MarkupMargin* markupMargin ); + void createMarkupLineNode( QDomElement& parent, const MarkupLine* markupLine ); + void createMarkupCircleNode( QDomElement& parent, const MarkupCircle* markupCircle ); + void createMarkupRectNode( QDomElement& parent, const MarkupRect* markupRect ); + void createMarkupEllipseNode( QDomElement& parent, const MarkupEllipse* markupEllipse ); + + }; + +} + +#endif // libglabels_XmlTemplateCreator_h