From 000c7f69e59046249ec69528a1025b12d51c0985 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Thu, 27 Aug 2015 00:30:54 -0400 Subject: [PATCH] Fleshed out remainder of XmlUtil. Units cleanup. --- libglabels/Frame.h | 2 +- libglabels/FrameCd.cpp | 12 +++---- libglabels/FrameCd.h | 2 +- libglabels/FrameEllipse.cpp | 16 +++++----- libglabels/FrameEllipse.h | 2 +- libglabels/FrameRect.cpp | 16 +++++----- libglabels/FrameRect.h | 2 +- libglabels/FrameRound.cpp | 12 +++---- libglabels/FrameRound.h | 2 +- libglabels/Units.cpp | 63 ++++++++++--------------------------- libglabels/Units.h | 14 +++++---- libglabels/XmlUtil.cpp | 59 +++++++++++++++++----------------- libglabels/XmlUtil.h | 50 ++++++++++++++--------------- 13 files changed, 113 insertions(+), 139 deletions(-) diff --git a/libglabels/Frame.h b/libglabels/Frame.h index ab7093f..5cb3f49 100644 --- a/libglabels/Frame.h +++ b/libglabels/Frame.h @@ -62,7 +62,7 @@ namespace libglabels virtual double w() const = 0; virtual double h() const = 0; - virtual const QString sizeDescription( const Units* units ) const = 0; + virtual const QString sizeDescription( const Units& units ) const = 0; virtual bool isSimilarTo( Frame* other ) const = 0; virtual const QPainterPath& path() const = 0; diff --git a/libglabels/FrameCd.cpp b/libglabels/FrameCd.cpp index a9e5f27..3275ae5 100644 --- a/libglabels/FrameCd.cpp +++ b/libglabels/FrameCd.cpp @@ -80,22 +80,22 @@ namespace libglabels } - const QString FrameCd::sizeDescription( const Units* units ) const + const QString FrameCd::sizeDescription( const Units& units ) const { - if ( units->id() == "in" ) + if ( units.id() == "in" ) { - QString dStr = StrUtil::formatFraction( 2 * mR1 * units->unitsPerPoint() ); + QString dStr = StrUtil::formatFraction( 2 * mR1 * units.unitsPerPoint() ); return QString().sprintf( "%s %s %s", qPrintable(dStr), - qPrintable(units->name()), + qPrintable(units.name()), qPrintable(tr("diameter")) ); } else { return QString().sprintf( "%.5g %s %s", - 2 * mR1 * units->unitsPerPoint(), - qPrintable(units->name()), + 2 * mR1 * units.unitsPerPoint(), + qPrintable(units.name()), qPrintable(tr("diameter")) ); } } diff --git a/libglabels/FrameCd.h b/libglabels/FrameCd.h index 9bbc580..26438a9 100644 --- a/libglabels/FrameCd.h +++ b/libglabels/FrameCd.h @@ -43,7 +43,7 @@ namespace libglabels double w() const; double h() const; - const QString sizeDescription( const Units* units ) const; + const QString sizeDescription( const Units& units ) const; bool isSimilarTo( Frame* other ) const; const QPainterPath& path() const; diff --git a/libglabels/FrameEllipse.cpp b/libglabels/FrameEllipse.cpp index b7a46e7..2c34ec1 100644 --- a/libglabels/FrameEllipse.cpp +++ b/libglabels/FrameEllipse.cpp @@ -59,24 +59,24 @@ namespace libglabels } - const QString FrameEllipse::sizeDescription( const Units* units ) const + const QString FrameEllipse::sizeDescription( const Units& units ) const { - if ( units->id() == "in" ) + if ( units.id() == "in" ) { - QString wStr = StrUtil::formatFraction( mW * units->unitsPerPoint() ); - QString hStr = StrUtil::formatFraction( mH * units->unitsPerPoint() ); + QString wStr = StrUtil::formatFraction( mW * units.unitsPerPoint() ); + QString hStr = StrUtil::formatFraction( mH * units.unitsPerPoint() ); return QString().sprintf( "%s x %s %s", qPrintable(wStr), qPrintable(hStr), - qPrintable(units->name()) ); + qPrintable(units.name()) ); } else { return QString().sprintf( "%.5g x %.5g %s", - mW * units->unitsPerPoint(), - mH * units->unitsPerPoint(), - qPrintable(units->name()) ); + mW * units.unitsPerPoint(), + mH * units.unitsPerPoint(), + qPrintable(units.name()) ); } } diff --git a/libglabels/FrameEllipse.h b/libglabels/FrameEllipse.h index d70b31a..942e3f1 100644 --- a/libglabels/FrameEllipse.h +++ b/libglabels/FrameEllipse.h @@ -42,7 +42,7 @@ namespace libglabels double w() const; double h() const; - const QString sizeDescription( const Units* units ) const; + const QString sizeDescription( const Units& units ) const; bool isSimilarTo( Frame* other ) const; const QPainterPath& path() const; diff --git a/libglabels/FrameRect.cpp b/libglabels/FrameRect.cpp index 8b6c61e..d30ab07 100644 --- a/libglabels/FrameRect.cpp +++ b/libglabels/FrameRect.cpp @@ -61,24 +61,24 @@ namespace libglabels } - const QString FrameRect::sizeDescription( const Units* units ) const + const QString FrameRect::sizeDescription( const Units& units ) const { - if ( units->id() == "in" ) + if ( units.id() == "in" ) { - QString wStr = StrUtil::formatFraction( mW * units->unitsPerPoint() ); - QString hStr = StrUtil::formatFraction( mH * units->unitsPerPoint() ); + QString wStr = StrUtil::formatFraction( mW * units.unitsPerPoint() ); + QString hStr = StrUtil::formatFraction( mH * units.unitsPerPoint() ); return QString().sprintf( "%s x %s %s", qPrintable(wStr), qPrintable(hStr), - qPrintable(units->name()) ); + qPrintable(units.name()) ); } else { return QString().sprintf( "%.5g x %.5g %s", - mW * units->unitsPerPoint(), - mH * units->unitsPerPoint(), - qPrintable(units->name()) ); + mW * units.unitsPerPoint(), + mH * units.unitsPerPoint(), + qPrintable(units.name()) ); } } diff --git a/libglabels/FrameRect.h b/libglabels/FrameRect.h index b9795f0..0eebdb5 100644 --- a/libglabels/FrameRect.h +++ b/libglabels/FrameRect.h @@ -48,7 +48,7 @@ namespace libglabels double w() const; double h() const; - const QString sizeDescription( const Units* units ) const; + const QString sizeDescription( const Units& units ) const; bool isSimilarTo( Frame* other ) const; diff --git a/libglabels/FrameRound.cpp b/libglabels/FrameRound.cpp index 0e59f7e..af1396c 100644 --- a/libglabels/FrameRound.cpp +++ b/libglabels/FrameRound.cpp @@ -60,22 +60,22 @@ namespace libglabels } - const QString FrameRound::sizeDescription( const Units* units ) const + const QString FrameRound::sizeDescription( const Units& units ) const { - if ( units->id() == "in" ) + if ( units.id() == "in" ) { - QString dStr = StrUtil::formatFraction( 2 * mR * units->unitsPerPoint() ); + QString dStr = StrUtil::formatFraction( 2 * mR * units.unitsPerPoint() ); return QString().sprintf( "%s %s %s", qPrintable(dStr), - qPrintable(units->name()), + qPrintable(units.name()), qPrintable(tr("diameter")) ); } else { return QString().sprintf( "%.5g %s %s", - 2 * mR * units->unitsPerPoint(), - qPrintable(units->name()), + 2 * mR * units.unitsPerPoint(), + qPrintable(units.name()), qPrintable(tr("diameter")) ); } } diff --git a/libglabels/FrameRound.h b/libglabels/FrameRound.h index 5d3090a..21234b5 100644 --- a/libglabels/FrameRound.h +++ b/libglabels/FrameRound.h @@ -43,7 +43,7 @@ namespace libglabels double w() const; double h() const; - const QString sizeDescription( const Units* units ) const; + const QString sizeDescription( const Units& units ) const; bool isSimilarTo( Frame* other ) const; const QPainterPath& path() const; diff --git a/libglabels/Units.cpp b/libglabels/Units.cpp index 3d7cf5b..19b4032 100644 --- a/libglabels/Units.cpp +++ b/libglabels/Units.cpp @@ -43,7 +43,13 @@ namespace libglabels } - Units *Units::fromId( const QString &id ) + Units::Units() + : mId("pt"), mName(tr("points")), mPointsPerUnit(POINTS_PER_POINT) + { + } + + + Units Units::fromId( const QString &id ) { if ( id == "pt" ) { @@ -79,68 +85,33 @@ namespace libglabels } - Units *Units::point() + Units Units::point() { - static Units *instance = NULL; - - if ( instance == NULL ) - { - instance = new Units( "pt", tr("points"), POINTS_PER_POINT ); - } - - return instance; + return Units( "pt", tr("points"), POINTS_PER_POINT ); } - Units *Units::inch() + Units Units::inch() { - static Units *instance = NULL; - - if ( instance == NULL ) - { - instance = new Units( "in", tr("inches"), POINTS_PER_INCH ); - } - - return instance; + return Units( "in", tr("inches"), POINTS_PER_INCH ); } - Units *Units::mm() + Units Units::mm() { - static Units *instance = NULL; - - if ( instance == NULL ) - { - instance = new Units( "mm", tr("mm"), POINTS_PER_MM ); - } - - return instance; + return Units( "mm", tr("mm"), POINTS_PER_MM ); } - Units *Units::cm() + Units Units::cm() { - static Units *instance = NULL; - - if ( instance == NULL ) - { - instance = new Units( "cm", tr("cm"), POINTS_PER_CM ); - } - - return instance; + return Units( "cm", tr("cm"), POINTS_PER_CM ); } - Units *Units::pica() + Units Units::pica() { - static Units *instance = NULL; - - if ( instance == NULL ) - { - instance = new Units( "pc", tr("picas"), POINTS_PER_PICA ); - } - - return instance; + return Units( "pc", tr("picas"), POINTS_PER_PICA ); } diff --git a/libglabels/Units.h b/libglabels/Units.h index 9d75ab4..3c141fb 100644 --- a/libglabels/Units.h +++ b/libglabels/Units.h @@ -39,23 +39,25 @@ namespace libglabels Units( const QString &id, const QString &name, double pointsPerUnit ); public: + Units(); + QString id() const; QString name() const; double pointsPerUnit() const; double unitsPerPoint() const; - static Units *fromId( const QString &id ); + static Units fromId( const QString &id ); - static Units *point(); + static Units point(); - static Units *inch(); + static Units inch(); - static Units *mm(); + static Units mm(); - static Units *cm(); + static Units cm(); - static Units *pica(); + static Units pica(); static bool isIdValid( QString id ); diff --git a/libglabels/XmlUtil.cpp b/libglabels/XmlUtil.cpp index f504d0e..bcfe123 100644 --- a/libglabels/XmlUtil.cpp +++ b/libglabels/XmlUtil.cpp @@ -27,7 +27,7 @@ namespace libglabels { - Units* XmlUtil::mDefaultUnits; + Units XmlUtil::mDefaultUnits; XmlUtil::XmlUtil() @@ -42,13 +42,13 @@ namespace libglabels } - const Units* XmlUtil::defaultUnits() + Units XmlUtil::defaultUnits() { return mDefaultUnits; } - void XmlUtil::setDefaultUnits( Units* defaultUnits ) + void XmlUtil::setDefaultUnits( const Units& defaultUnits ) { mDefaultUnits = defaultUnits; } @@ -210,60 +210,61 @@ namespace libglabels return default_value; } - Units* units = Units::fromId( unitsString ); + Units units = Units::fromId( unitsString ); - return value* units->pointsPerUnit(); + return value * units.pointsPerUnit(); } return default_value; } - void XmlUtil::setStringAttr( const QDomElement& node, - const QString& name, - const QString& value ) + void XmlUtil::setStringAttr( QDomElement& node, + const QString& name, + const QString& value ) { - // TODO + node.setAttribute( name, value ); } - void XmlUtil::setDoubleAttr( const QDomElement& node, - const QString& name, - double value ) + void XmlUtil::setDoubleAttr( QDomElement& node, + const QString& name, + double value ) { - // TODO + node.setAttribute( name, QString::number(value) ); } - void XmlUtil::setBoolAttr( const QDomElement& node, - const QString& name, - bool value ) + void XmlUtil::setBoolAttr( QDomElement& node, + const QString& name, + bool value ) { - // TODO + node.setAttribute( name, value ? "true" : "false" ); } - void XmlUtil::setIntAttr( const QDomElement& node, - const QString& name, - int value ) + void XmlUtil::setIntAttr( QDomElement& node, + const QString& name, + int value ) { - // TODO + node.setAttribute( name, QString::number(value) ); } - void XmlUtil::setUIntAttr( const QDomElement& node, - const QString& name, - uint32_t value ) + void XmlUtil::setUIntAttr( QDomElement& node, + const QString& name, + uint32_t value ) { - // TODO + node.setAttribute( name, "0x" + QString::number(value, 16) ); } - void XmlUtil::setLengthAttr( const QDomElement& node, - const QString& name, - double value ) + void XmlUtil::setLengthAttr( QDomElement& node, + const QString& name, + double value ) { - // TODO + value *= mDefaultUnits.unitsPerPoint(); + node.setAttribute( name, QString::number(value) + mDefaultUnits.id() ); } } diff --git a/libglabels/XmlUtil.h b/libglabels/XmlUtil.h index 8ea9b7e..3966667 100644 --- a/libglabels/XmlUtil.h +++ b/libglabels/XmlUtil.h @@ -41,8 +41,8 @@ namespace libglabels static void init(); - static const Units* defaultUnits(); - static void setDefaultUnits( Units* defaultUnits ); + static Units defaultUnits(); + static void setDefaultUnits( const Units& defaultUnits ); static QString getStringAttr( const QDomElement& node, const QString& name, @@ -72,32 +72,32 @@ namespace libglabels const QString& name, double default_value ); - static void setStringAttr( const QDomElement& node, - const QString& name, - const QString& value ); - - static void setDoubleAttr( const QDomElement& node, - const QString& name, - double value ); - - static void setBoolAttr( const QDomElement& node, - const QString& name, - bool value ); - - static void setIntAttr( const QDomElement& node, - const QString& name, - int value ); - - static void setUIntAttr( const QDomElement& node, - const QString& name, - uint32_t value ); - - static void setLengthAttr( const QDomElement& node, + static void setStringAttr( QDomElement& node, const QString& name, - double value ); + const QString& value ); + + static void setDoubleAttr( QDomElement& node, + const QString& name, + double value ); + + static void setBoolAttr( QDomElement& node, + const QString& name, + bool value ); + + static void setIntAttr( QDomElement& node, + const QString& name, + int value ); + + static void setUIntAttr( QDomElement& node, + const QString& name, + uint32_t value ); + + static void setLengthAttr( QDomElement& node, + const QString& name, + double value ); private: - static Units* mDefaultUnits; + static Units mDefaultUnits; };