From cbae7243b84060604c783ec83c9f6bfaf406bbce Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sun, 3 Nov 2013 01:23:09 -0400 Subject: [PATCH] Added initial implementation of XmlPaperParser. --- CMakeLists.txt | 2 +- libglabels/CMakeLists.txt | 1 + libglabels/Db.cpp | 34 +++++------ libglabels/Db.h | 2 +- libglabels/FrameCd.cpp | 10 +-- libglabels/FrameEllipse.cpp | 8 +-- libglabels/FrameRect.cpp | 8 +-- libglabels/FrameRound.cpp | 10 +-- libglabels/XmlPaperParser.cpp | 112 ++++++++++++++++++++++++++++++++++ libglabels/XmlPaperParser.h | 46 ++++++++++++++ libglabels/XmlUtil.h | 59 ++++++++++++++++++ 11 files changed, 254 insertions(+), 38 deletions(-) create mode 100644 libglabels/XmlPaperParser.cpp create mode 100644 libglabels/XmlPaperParser.h create mode 100644 libglabels/XmlUtil.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e3ef6b..3acf762 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,7 @@ set (Package_Version "${Major_Version}.${Minor_Version}.${Bugfix_Version}") set (Unique_Package_Name ${Package_Name}-${API_Version}) -find_package(Qt4 REQUIRED) +find_package(Qt4 4.8.4 REQUIRED QtCore QtGui QtXml) add_definitions (-g) diff --git a/libglabels/CMakeLists.txt b/libglabels/CMakeLists.txt index b04f35d..0f83d16 100644 --- a/libglabels/CMakeLists.txt +++ b/libglabels/CMakeLists.txt @@ -18,6 +18,7 @@ set (libglabels_sources StrUtil.cpp Template.cpp Db.cpp + XmlPaperParser.cpp ) set (libglabels_qobject_headers diff --git a/libglabels/Db.cpp b/libglabels/Db.cpp index 916efd1..47c83e6 100644 --- a/libglabels/Db.cpp +++ b/libglabels/Db.cpp @@ -52,7 +52,7 @@ namespace libglabels } else { - qDebug( "Duplicate paper ID: \"%s\".", paper->id().toStdString().c_str() ); + qDebug( "Duplicate paper ID: \"%s\".", qPrintable(paper->id()) ); } } @@ -74,7 +74,7 @@ namespace libglabels } } - qDebug( "Unknown paper name: \"%s\".", name.toStdString().c_str() ); + qDebug( "Unknown paper name: \"%s\".", qPrintable(name) ); return NULL; } @@ -96,7 +96,7 @@ namespace libglabels } } - qDebug( "Unknown paper ID: \"%s\".", id.toStdString().c_str() ); + qDebug( "Unknown paper ID: \"%s\".", qPrintable(id) ); return NULL; } @@ -112,7 +112,7 @@ namespace libglabels } } - qDebug( "Unknown paper name: \"%s\".", name.toStdString().c_str() ); + qDebug( "Unknown paper name: \"%s\".", qPrintable(name) ); return mEmpty; } @@ -128,7 +128,7 @@ namespace libglabels } } - qDebug( "Unknown paper id: \"%s\".", id.toStdString().c_str() ); + qDebug( "Unknown paper id: \"%s\".", qPrintable(id) ); return mEmpty; } @@ -164,7 +164,7 @@ namespace libglabels } else { - qDebug( "Duplicate category ID: \"%s\".", category->id().toStdString().c_str() ); + qDebug( "Duplicate category ID: \"%s\".", qPrintable(category->id()) ); } } @@ -186,7 +186,7 @@ namespace libglabels } } - qDebug( "Unknown category name: \"%s\".", name.toStdString().c_str() ); + qDebug( "Unknown category name: \"%s\".", qPrintable(name) ); return NULL; } @@ -208,7 +208,7 @@ namespace libglabels } } - qDebug( "Unknown category ID: \"%s\".", id.toStdString().c_str() ); + qDebug( "Unknown category ID: \"%s\".", qPrintable(id) ); return NULL; } @@ -224,7 +224,7 @@ namespace libglabels } } - qDebug( "Unknown category name: \"%s\".", name.toStdString().c_str() ); + qDebug( "Unknown category name: \"%s\".", qPrintable(name) ); return mEmpty; } @@ -240,7 +240,7 @@ namespace libglabels } } - qDebug( "Unknown category id: \"%s\".", id.toStdString().c_str() ); + qDebug( "Unknown category id: \"%s\".", qPrintable(id) ); return mEmpty; } @@ -269,7 +269,7 @@ namespace libglabels } else { - qDebug( "Duplicate vendor name: \"%s\".", vendor->name().toStdString().c_str() ); + qDebug( "Duplicate vendor name: \"%s\".", qPrintable(vendor->name()) ); } } @@ -291,7 +291,7 @@ namespace libglabels } } - qDebug( "Unknown vendor name: \"%s\".", name.toStdString().c_str() ); + qDebug( "Unknown vendor name: \"%s\".", qPrintable(name) ); return NULL; } @@ -307,7 +307,7 @@ namespace libglabels } } - qDebug( "Unknown vendor name: \"%s\".", name.toStdString().c_str() ); + qDebug( "Unknown vendor name: \"%s\".", qPrintable(name) ); return mEmpty; } @@ -335,7 +335,7 @@ namespace libglabels } else { - qDebug( "Duplicate template name: \"%s\".", tmplate->name().toStdString().c_str() ); + qDebug( "Duplicate template name: \"%s\".", qPrintable(tmplate->name()) ); } } @@ -357,7 +357,7 @@ namespace libglabels } } - qDebug( "Unknown template name: \"%s\".", name.toStdString().c_str() ); + qDebug( "Unknown template name: \"%s\".", qPrintable(name) ); return NULL; } @@ -379,9 +379,7 @@ namespace libglabels } } - qDebug( "Unknown template brand, part: \"%s\", \"%s\".", - brand.toStdString().c_str(), - part.toStdString().c_str() ); + qDebug( "Unknown template brand, part: \"%s\", \"%s\".", qPrintable(brand), qPrintable(part) ); return NULL; } diff --git a/libglabels/Db.h b/libglabels/Db.h index b0b6628..19c7109 100644 --- a/libglabels/Db.h +++ b/libglabels/Db.h @@ -48,7 +48,7 @@ namespace libglabels public: - static Db *instance() { return new Db; } + static Db *instance() { static Db *db = new Db(); return db; } static void registerPaper( Paper *paper ); static const Paper *lookupPaperFromName( const QString &name ); diff --git a/libglabels/FrameCd.cpp b/libglabels/FrameCd.cpp index 863db6d..14eea44 100644 --- a/libglabels/FrameCd.cpp +++ b/libglabels/FrameCd.cpp @@ -36,16 +36,16 @@ namespace libglabels QString dStr = StrUtil::formatFraction( 2 * mR1 * units->unitsPerPoint() ); mSizeDescription = QString().sprintf( "%s %s %s", - dStr.toStdString().c_str(), - units->name().toStdString().c_str(), - tr("diameter").toStdString().c_str() ); + qPrintable(dStr), + qPrintable(units->name()), + qPrintable(tr("diameter")) ); } else { mSizeDescription = QString().sprintf( "%.5g %s %s", 2 * mR1 * units->unitsPerPoint(), - units->name().toStdString().c_str(), - tr("diameter").toStdString().c_str() ); + qPrintable(units->name()), + qPrintable(tr("diameter")) ); } return mSizeDescription; diff --git a/libglabels/FrameEllipse.cpp b/libglabels/FrameEllipse.cpp index 9a96320..f3f6e0c 100644 --- a/libglabels/FrameEllipse.cpp +++ b/libglabels/FrameEllipse.cpp @@ -37,16 +37,16 @@ namespace libglabels QString hStr = StrUtil::formatFraction( mH * units->unitsPerPoint() ); mSizeDescription = QString().sprintf( "%s x %s %s", - wStr.toStdString().c_str(), - hStr.toStdString().c_str(), - units->name().toStdString().c_str() ); + qPrintable(wStr), + qPrintable(hStr), + qPrintable(units->name()) ); } else { mSizeDescription = QString().sprintf( "%.5g x %.5g %s", mW * units->unitsPerPoint(), mH * units->unitsPerPoint(), - units->name().toStdString().c_str() ); + qPrintable(units->name()) ); } } diff --git a/libglabels/FrameRect.cpp b/libglabels/FrameRect.cpp index 46b770d..ea41979 100644 --- a/libglabels/FrameRect.cpp +++ b/libglabels/FrameRect.cpp @@ -37,16 +37,16 @@ namespace libglabels QString hStr = StrUtil::formatFraction( mH * units->unitsPerPoint() ); mSizeDescription = QString().sprintf( "%s x %s %s", - wStr.toStdString().c_str(), - hStr.toStdString().c_str(), - units->name().toStdString().c_str() ); + qPrintable(wStr), + qPrintable(hStr), + qPrintable(units->name()) ); } else { mSizeDescription = QString().sprintf( "%.5g x %.5g %s", mW * units->unitsPerPoint(), mH * units->unitsPerPoint(), - units->name().toStdString().c_str() ); + qPrintable(units->name()) ); } return mSizeDescription; diff --git a/libglabels/FrameRound.cpp b/libglabels/FrameRound.cpp index eebf118..2a62012 100644 --- a/libglabels/FrameRound.cpp +++ b/libglabels/FrameRound.cpp @@ -36,16 +36,16 @@ namespace libglabels QString dStr = StrUtil::formatFraction( 2 * mR * units->unitsPerPoint() ); mSizeDescription = QString().sprintf( "%s %s %s", - dStr.toStdString().c_str(), - units->name().toStdString().c_str(), - tr("diameter").toStdString().c_str() ); + qPrintable(dStr), + qPrintable(units->name()), + qPrintable(tr("diameter")) ); } else { mSizeDescription = QString().sprintf( "%.5g %s %s", 2 * mR * units->unitsPerPoint(), - units->name().toStdString().c_str(), - tr("diameter").toStdString().c_str() ); + qPrintable(units->name()), + qPrintable(tr("diameter")) ); } return mSizeDescription; diff --git a/libglabels/XmlPaperParser.cpp b/libglabels/XmlPaperParser.cpp new file mode 100644 index 0000000..c0b6e0e --- /dev/null +++ b/libglabels/XmlPaperParser.cpp @@ -0,0 +1,112 @@ +/* XmlPaperParser.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 "XmlPaperParser.h" + +#include +#include +#include +#include + +#include "Paper.h" +#include "XmlUtil.h" +#include "Db.h" + + +namespace libglabels +{ + + bool XmlPaperParser::readFile( const QString &fileName ) + { + QFile file( fileName ); + + if ( !file.open( QFile::ReadOnly | QFile::Text) ) + { + std::cerr << "Error: Cannot read file " << qPrintable(fileName) + << ": " << qPrintable(file.errorString()) + << std::endl; + return false; + } + + + QDomDocument doc; + QString errorString; + int errorLine; + int errorColumn; + + if ( !doc.setContent( &file, false, &errorString, &errorLine, &errorColumn ) ) + { + std::cerr << "Error: Parse error at line " << errorLine + << "column " << errorColumn + << ": " << qPrintable(errorString) + << std::endl; + return false; + } + + QDomElement root = doc.documentElement(); + if ( root.tagName() != "Glabels-paper-sizes" ) + { + std::cerr << "Error: Not a Glabels-paper-sizes file" << std::endl; + return false; + } + + parseRootNode( root ); + return true; + } + + + void XmlPaperParser::parseRootNode( const QDomElement &node ) + { + QDomNode child = node; + while ( !child.isNull() ) + { + if ( child.toElement().tagName() == "Paper-size" ) + { + parsePaperSizeNode( child.toElement() ); + } + else if ( !child.isComment() ) + { + std::cerr << "Warning: bad element: " << qPrintable(child.toElement().tagName()) + << ", Ignored" + << std::endl; + } + } + } + + + void XmlPaperParser::parsePaperSizeNode( const QDomElement &node ) + { + QString id = XmlUtil::getAttrString( node, "id", "" ); + QString name = XmlUtil::getAttrStringI18n( node, "name", "" ); + + double width = XmlUtil::getAttrLength( node, "width", 0 ); + double height = XmlUtil::getAttrLength( node, "height", 0 ); + + QString pwgSize = XmlUtil::getAttrString( node, "pwg_size", "" ); + + Paper *paper = new Paper( id, name, width, height, pwgSize ); + if ( paper != NULL ) + { + Db::registerPaper( paper ); + } + } + +} + diff --git a/libglabels/XmlPaperParser.h b/libglabels/XmlPaperParser.h new file mode 100644 index 0000000..92c549e --- /dev/null +++ b/libglabels/XmlPaperParser.h @@ -0,0 +1,46 @@ +/* XmlPaperParser.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_XmlPaperParser_h +#define libglabels_XmlPaperParser_h + + +#include +#include + + +namespace libglabels +{ + + class XmlPaperParser + { + public: + XmlPaperParser() {} + + bool readFile( const QString &fileName ); + + private: + void parseRootNode( const QDomElement &node ); + void parsePaperSizeNode( const QDomElement &node ); + }; + +} + +#endif // libglabels_XmlPaperParser_h diff --git a/libglabels/XmlUtil.h b/libglabels/XmlUtil.h new file mode 100644 index 0000000..c436c5b --- /dev/null +++ b/libglabels/XmlUtil.h @@ -0,0 +1,59 @@ +/* XmlUtil.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_XmlUtil_h +#define libglabels_XmlUtil_h + +#include +#include + +#include "Units.h" + + +namespace libglabels +{ + + class XmlUtil + { + private: + XmlUtil() + { + mDefaultUnits = Units::point(); + } + + static void init() + { + static XmlUtil *xmlUtil = new XmlUtil(); + } + + public: + + static QString getAttrString( const QDomElement &node, const QString &name, const QString &default_val ); + static QString getAttrStringI18n( const QDomElement &node, const QString &name, const QString &default_val ); + static double getAttrLength( const QDomElement &node, const QString &name, double default_val ); + + private: + static Units *mDefaultUnits; + + }; + +} + +#endif // libglabels_XmlUtil_h