Initial implementation of XmlUtil. Completed XmlPaperParser.

This commit is contained in:
Jim Evins
2013-11-03 11:10:08 -05:00
parent cbae7243b8
commit 109574a409
11 changed files with 342 additions and 73 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
cmake_minimum_required (VERSION 2.8)
project (glabels-qt)
project (glabels_qt)
set (Package_Name "glabels-qt")
+3
View File
@@ -34,14 +34,17 @@ include (${QT_USE_FILE})
include_directories (
${glabels_qt_SOURCE_DIR}
)
link_directories (
${glabels_qt_SOURCE_DIR}/libglabels
)
add_executable (glabels-qt ${glabels_sources} ${glabels_moc_sources} ${glabels_qrc_sources})
target_link_libraries (glabels-qt
libglabels
${QT_LIBRARIES}
)
+12
View File
@@ -22,9 +22,14 @@
#include <QApplication>
#include "MainWindow.h"
#include "libglabels/Db.h"
////// TEMPORARY TESTING ////////
#include "libglabels/XmlPaperParser.h"
/////////////////////////////////
using namespace gLabels;
using namespace libglabels;
int main( int argc, char **argv )
@@ -35,6 +40,13 @@ int main( int argc, char **argv )
QCoreApplication::setOrganizationDomain( "glabels.org" );
QCoreApplication::setApplicationName( "glabels-qt" );
Db::init();
////// TEMPORARY TESTING ////////
XmlPaperParser parser;
parser.readFile( "/usr/local/share/libglabels-3.0/templates/paper-sizes.xml" );
Db::printKnownPapers();
/////////////////////////////////
MainWindow mainWin;
mainWin.show();
+1
View File
@@ -19,6 +19,7 @@ set (libglabels_sources
Template.cpp
Db.cpp
XmlPaperParser.cpp
XmlUtil.cpp
)
set (libglabels_qobject_headers
+47 -44
View File
@@ -26,15 +26,17 @@
namespace libglabels
{
std::list<Paper *> mPapers;
std::list<QString> mPaperIds;
std::list<QString> mPaperNames;
std::list<Category *> mCategories;
std::list<QString> mCategoryIds;
std::list<QString> mCategoryNames;
std::list<Vendor *> mVendors;
std::list<QString> mVendorNames;
std::list<Template *> mTemplates;
std::list<Paper*> Db::mPapers;
std::list<QString> Db::mPaperIds;
std::list<QString> Db::mPaperNames;
std::list<Category*> Db::mCategories;
std::list<QString> Db::mCategoryIds;
std::list<QString> Db::mCategoryNames;
std::list<Vendor*> Db::mVendors;
std::list<QString> Db::mVendorNames;
std::list<Template*> Db::mTemplates;
QString Db::mEmpty = "";
Db::Db()
{
@@ -44,7 +46,7 @@ namespace libglabels
void Db::registerPaper( Paper *paper )
{
if ( lookupPaperFromId( paper->id() ) == NULL )
if ( !isPaperIdKnown( paper->id() ) )
{
mPapers.push_back( paper );
mPaperIds.push_back( paper->id() );
@@ -135,10 +137,10 @@ namespace libglabels
bool Db::isPaperIdKnown( const QString &id )
{
if ( !id.isNull() && !id.isEmpty() )
std::list<Paper*>::const_iterator it;
for ( it = mPapers.begin(); it != mPapers.end(); it++ )
{
const Paper *paper = lookupPaperFromId( id );
if ( paper != NULL )
if ( (*it)->id() == id )
{
return true;
}
@@ -156,7 +158,7 @@ namespace libglabels
void Db::registerCategory( Category *category )
{
if ( lookupCategoryFromId( category->id() ) == NULL )
if ( !isCategoryIdKnown( category->id() ) )
{
mCategories.push_back( category );
mCategoryIds.push_back( category->id() );
@@ -247,10 +249,10 @@ namespace libglabels
bool Db::isCategoryIdKnown( const QString &id )
{
if ( !id.isNull() && !id.isEmpty() )
std::list<Category*>::const_iterator it;
for ( it = mCategories.begin(); it != mCategories.end(); it++ )
{
const Category *category = lookupCategoryFromId( id );
if ( category != NULL )
if ( (*it)->id() == id )
{
return true;
}
@@ -262,7 +264,7 @@ namespace libglabels
void Db::registerVendor( Vendor *vendor )
{
if ( lookupVendorFromName( vendor->name() ) == NULL )
if ( !isVendorNameKnown( vendor->name() ) )
{
mVendors.push_back( vendor );
mVendorNames.push_back( vendor->name() );
@@ -314,10 +316,10 @@ namespace libglabels
bool Db::isVendorNameKnown( const QString &name )
{
if ( !name.isNull() && !name.isEmpty() )
std::list<Vendor*>::const_iterator it;
for ( it = mVendors.begin(); it != mVendors.end(); it++ )
{
const Vendor *vendor = lookupVendorFromName( name );
if ( vendor != NULL )
if ( (*it)->name() == name )
{
return true;
}
@@ -329,7 +331,7 @@ namespace libglabels
void Db::registerTemplate( Template *tmplate )
{
if ( lookupTemplateFromName( tmplate->name() ) == NULL )
if ( !isTemplateKnown( tmplate->brand(), tmplate->part() ) )
{
mTemplates.push_back( tmplate );
}
@@ -386,10 +388,10 @@ namespace libglabels
bool Db::isTemplateKnown( const QString &brand, const QString &part )
{
if ( !brand.isNull() && !brand.isEmpty() && !part.isNull() && part.isEmpty() )
std::list<Template*>::const_iterator it;
for ( it = mTemplates.begin(); it != mTemplates.end(); it++ )
{
const Template *tmplate = lookupTemplateFromBrandPart( brand, part );
if ( tmplate != NULL )
if ( ((*it)->brand() == brand) && ((*it)->part() == part) )
{
return true;
}
@@ -426,12 +428,13 @@ namespace libglabels
{
Paper *paper = *it;
std::cout << "paper " <<
"id='" << paper->id().toStdString() << "', " <<
"name='" << paper->name().toStdString() << "', " <<
"width=" << paper->width() << "pts, " <<
"height=" << paper->height() << "pts" <<
std::endl;
std::cout << "paper "
<< "id='" << qPrintable(paper->id()) << "', "
<< "name='" << qPrintable(paper->name()) << "', "
<< "width=" << paper->width() << "pts, "
<< "height=" << paper->height() << "pts, "
<< "pwg_size=" << qPrintable(paper->pwgSize())
<< std::endl;
}
std::cout << std::endl;
@@ -447,10 +450,10 @@ namespace libglabels
{
Category *category = *it;
std::cout << "category " <<
"id='" << category->id().toStdString() << "', " <<
"name='" << category->name().toStdString() << "', " <<
std::endl;
std::cout << "category "
<< "id='" << category->id().toStdString() << "', "
<< "name='" << category->name().toStdString() << "', "
<< std::endl;
}
std::cout << std::endl;
@@ -466,10 +469,10 @@ namespace libglabels
{
Vendor *vendor = *it;
std::cout << "vendor " <<
"name='" << vendor->name().toStdString() << "', " <<
"url='" << vendor->url().toStdString() << "'" <<
std::endl;
std::cout << "vendor "
<< "name='" << vendor->name().toStdString() << "', "
<< "url='" << vendor->url().toStdString() << "'"
<< std::endl;
}
std::cout << std::endl;
@@ -485,11 +488,11 @@ namespace libglabels
{
Template *tmplate = *it;
std::cout << "template " <<
"brand='" << tmplate->brand().toStdString() << "', " <<
"part='" << tmplate->part().toStdString() << "', " <<
"description='" << tmplate->description().toStdString() << "'" <<
std::endl;
std::cout << "template "
<< "brand='" << tmplate->brand().toStdString() << "', "
<< "part='" << tmplate->part().toStdString() << "', "
<< "description='" << tmplate->description().toStdString() << "'"
<< std::endl;
}
std::cout << std::endl;
+4 -8
View File
@@ -22,7 +22,7 @@
#define libglabels_Db_h
#include <QObject>
#include <QCoreApplication>
#include <QString>
#include "Paper.h"
@@ -34,20 +34,16 @@
namespace libglabels
{
class Db : public QObject
class Db
{
Q_OBJECT
signals:
static void changed();
Q_DECLARE_TR_FUNCTIONS(Db)
private:
Db();
public:
static void init() { instance(); }
static Db *instance() { static Db *db = new Db(); return db; }
static void registerPaper( Paper *paper );
+6 -1
View File
@@ -36,7 +36,7 @@ namespace
namespace libglabels
{
Units *Units::from_id( const QString &id )
Units *Units::fromId( const QString &id )
{
if ( id == "pt" )
{
@@ -137,4 +137,9 @@ namespace libglabels
}
bool Units::isIdValid( QString id )
{
return ( (id == "pt") || (id == "in") || (id == "mm") || (id == "cm") || (id == "pc") || (id == "") );
}
}
+3 -1
View File
@@ -53,7 +53,7 @@ namespace libglabels
inline double unitsPerPoint() const { return mUnitsPerPoint; }
static Units *from_id( const QString &id );
static Units *fromId( const QString &id );
static Units *point();
@@ -65,6 +65,8 @@ namespace libglabels
static Units *pica();
static bool isIdValid( QString id );
private:
QString mId;
+4 -5
View File
@@ -74,8 +74,7 @@ namespace libglabels
void XmlPaperParser::parseRootNode( const QDomElement &node )
{
QDomNode child = node;
while ( !child.isNull() )
for ( QDomNode child = node.firstChild(); !child.isNull(); child = child.nextSibling() )
{
if ( child.toElement().tagName() == "Paper-size" )
{
@@ -93,13 +92,13 @@ namespace libglabels
void XmlPaperParser::parsePaperSizeNode( const QDomElement &node )
{
QString id = XmlUtil::getAttrString( node, "id", "" );
QString name = XmlUtil::getAttrStringI18n( node, "name", "" );
QString id = XmlUtil::getAttr( node, "id", "" );
QString name = XmlUtil::getAttrI18n( node, "name", "" );
double width = XmlUtil::getAttrLength( node, "width", 0 );
double height = XmlUtil::getAttrLength( node, "height", 0 );
QString pwgSize = XmlUtil::getAttrString( node, "pwg_size", "" );
QString pwgSize = XmlUtil::getAttr( node, "pwg_size", "" );
Paper *paper = new Paper( id, name, width, height, pwgSize );
if ( paper != NULL )
+229
View File
@@ -0,0 +1,229 @@
/* XmlUtil.cpp
*
* Copyright (C) 2013 Jim Evins <evins@snaught.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "XmlUtil.h"
#include <QTextStream>
#include <iostream>
namespace libglabels
{
Units *XmlUtil::mDefaultUnits;
QString XmlUtil::getAttr( const QDomElement &node, const QString &name, const char *default_value )
{
return node.attribute( name, QString(default_value) );
}
QString XmlUtil::getAttr( const QDomElement &node, const QString &name, const QString &default_value )
{
return node.attribute( name, default_value );
}
double XmlUtil::getAttr( const QDomElement &node, const QString &name, double default_value )
{
QString valueString = node.attribute( name, "" );
if ( valueString != "" )
{
bool ok;
double value = valueString.toDouble( &ok );
if ( !ok )
{
std::cerr << "Error: bad double value in attribute "
<< qPrintable( node.tagName() ) << ":" << qPrintable( name )
<< " : '" << qPrintable( valueString ) << "'"
<< std:: endl;
return default_value;
}
return value;
}
return default_value;
}
bool XmlUtil::getAttr( const QDomElement &node, const QString &name, bool default_value )
{
QString valueString = node.attribute( name, "" );
if ( valueString != "" )
{
int intValue = valueString.toInt();
if ( (valueString == "True") ||
(valueString == "TRUE") ||
(valueString == "true") ||
(intValue == 1) )
{
return true;
}
if ( (valueString == "False") ||
(valueString == "FALSE") ||
(valueString == "false") ||
(intValue == 0) )
{
return false;
}
std::cerr << "Error: bad boolean value in attribute "
<< qPrintable( node.tagName() ) << ":" << qPrintable( name )
<< " : '" << qPrintable( valueString ) << "'"
<< std:: endl;
return default_value;
}
return default_value;
}
int XmlUtil::getAttr( const QDomElement &node, const QString &name, int default_value )
{
QString valueString = node.attribute( name, "" );
if ( valueString != "" )
{
bool ok;
int value = valueString.toInt( &ok );
if ( !ok )
{
std::cerr << "Error: bad integer value in attribute "
<< qPrintable( node.tagName() ) << ":" << qPrintable( name )
<< " : '" << qPrintable( valueString ) << "'"
<< std:: endl;
return default_value;
}
return value;
}
return default_value;
}
uint32_t XmlUtil::getAttr( const QDomElement &node, const QString &name, uint32_t default_value )
{
QString valueString = node.attribute( name, "" );
if ( valueString != "" )
{
// TODO: Does base-0 do what we want? I.e. use base determined by format e.g. "0xff"
bool ok;
uint32_t value = valueString.toInt( &ok, 0 );
if ( !ok )
{
std::cerr << "Error: bad unsigned integer value in attribute "
<< qPrintable( node.tagName() ) << ":" << qPrintable( name )
<< " : '" << qPrintable( valueString ) << "'"
<< std:: endl;
return default_value;
}
return value;
}
return default_value;
}
QString XmlUtil::getAttrI18n( const QDomElement &node, const QString &name, const QString &default_value )
{
// TODO: are translations done in a compatable way, so that we can use "_name" attributes?
return node.attribute( QString("_").append(name), default_value );
}
double XmlUtil::getAttrLength( const QDomElement &node, const QString &name, double default_value )
{
QString valueString = node.attribute( name, "" );
if ( valueString != "" )
{
double value;
QString unitsString;
QTextStream valueStream( &valueString, QIODevice::ReadOnly );
valueStream >> value >> unitsString;
if ( !Units::isIdValid( unitsString ) )
{
std::cerr << "Error: bad length value in attribute "
<< qPrintable( node.tagName() ) << ":" << qPrintable( name )
<< " : '" << qPrintable( valueString ) << "'"
<< std:: endl;
return default_value;
}
Units *units = Units::fromId( unitsString );
return value * units->pointsPerUnit();
}
return default_value;
}
void XmlUtil::setAttr( const QDomElement &node, const QString &name, const char *value )
{
// TODO
}
void XmlUtil::setAttr( const QDomElement &node, const QString &name, const QString &value )
{
// TODO
}
void XmlUtil::setAttr( const QDomElement &node, const QString &name, double value )
{
// TODO
}
void XmlUtil::setAttr( const QDomElement &node, const QString &name, bool value )
{
// TODO
}
void XmlUtil::setAttr( const QDomElement &node, const QString &name, int value )
{
// TODO
}
void XmlUtil::setAttr( const QDomElement &node, const QString &name, uint32_t value )
{
// TODO
}
void XmlUtil::setAttrLength( const QDomElement &node, const QString &name, double value )
{
// TODO
}
}
+23 -4
View File
@@ -23,6 +23,7 @@
#include <QString>
#include <QDomElement>
#include <stdint.h>
#include "Units.h"
@@ -38,16 +39,34 @@ namespace libglabels
mDefaultUnits = Units::point();
}
public:
static void init()
{
static XmlUtil *xmlUtil = new XmlUtil();
}
public:
static const Units *defaultUnits() { return mDefaultUnits; }
static void setDefaultUnits( Units *defaultUnits ) { mDefaultUnits = defaultUnits; }
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 );
static QString getAttr( const QDomElement &node, const QString &name, const char *default_value );
static QString getAttr( const QDomElement &node, const QString &name, const QString &default_value );
static double getAttr( const QDomElement &node, const QString &name, double default_value );
static bool getAttr( const QDomElement &node, const QString &name, bool default_value );
static int getAttr( const QDomElement &node, const QString &name, int default_value );
static uint32_t getAttr( const QDomElement &node, const QString &name, uint32_t default_value );
static QString getAttrI18n( const QDomElement &node, const QString &name, const QString &default_value );
static double getAttrLength( const QDomElement &node, const QString &name, double default_value );
static void setAttr( const QDomElement &node, const QString &name, const char *value );
static void setAttr( const QDomElement &node, const QString &name, const QString &value );
static void setAttr( const QDomElement &node, const QString &name, double value );
static void setAttr( const QDomElement &node, const QString &name, bool value );
static void setAttr( const QDomElement &node, const QString &name, int value );
static void setAttr( const QDomElement &node, const QString &name, uint32_t value );
static void setAttrLength( const QDomElement &node, const QString &name, double value );
private:
static Units *mDefaultUnits;