From 262932a4c71ac421bf7fa8e19fab5d1784bf4cf4 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sat, 2 Nov 2013 17:10:49 -0400 Subject: [PATCH] Added initial implementation of Template. --- libglabels/CMakeLists.txt | 1 + libglabels/Frame.cpp | 27 +++++++ libglabels/Frame.h | 9 ++- libglabels/FrameCd.cpp | 32 ++++---- libglabels/FrameCd.h | 9 ++- libglabels/FrameEllipse.cpp | 28 +++---- libglabels/FrameEllipse.h | 9 ++- libglabels/FrameRect.cpp | 9 ++- libglabels/FrameRect.h | 10 ++- libglabels/FrameRound.cpp | 26 +++--- libglabels/FrameRound.h | 9 ++- libglabels/Layout.cpp | 14 ++-- libglabels/Layout.h | 13 ++- libglabels/Markup.h | 12 +++ libglabels/Template.cpp | 154 ++++++++++++++++++++++++++++++++++++ libglabels/Template.h | 114 ++++++++++++++++++++++++++ 16 files changed, 416 insertions(+), 60 deletions(-) create mode 100644 libglabels/Template.cpp create mode 100644 libglabels/Template.h diff --git a/libglabels/CMakeLists.txt b/libglabels/CMakeLists.txt index 6cafba6..662875b 100644 --- a/libglabels/CMakeLists.txt +++ b/libglabels/CMakeLists.txt @@ -16,6 +16,7 @@ set (libglabels_sources FrameRound.cpp FrameEllipse.cpp StrUtil.cpp + Template.cpp ) set (libglabels_qobject_headers diff --git a/libglabels/Frame.cpp b/libglabels/Frame.cpp index 28241bb..316df43 100644 --- a/libglabels/Frame.cpp +++ b/libglabels/Frame.cpp @@ -24,6 +24,33 @@ namespace libglabels { + Frame::Frame( const Frame &other ) + { + mId = other.mId; + mNLabels = 0; + + { + std::list::const_iterator it; + + for ( it = other.mLayouts.begin(); it != other.mLayouts.end(); it++ ) + { + Layout *layout = (*it)->dup(); + addLayout( layout ); + } + } + + { + std::list::const_iterator it; + + for ( it = other.mMarkups.begin(); it != other.mMarkups.end(); it++ ) + { + Markup *markup = (*it)->dup(); + addMarkup( markup ); + } + } + } + + std::vector Frame::getOrigins() const { std::vector origins( nLabels() ); diff --git a/libglabels/Frame.h b/libglabels/Frame.h index d8274f7..8fc5e0c 100644 --- a/libglabels/Frame.h +++ b/libglabels/Frame.h @@ -45,10 +45,16 @@ namespace libglabels { } + Frame( const Frame &other ); + public: + virtual Frame *dup() const = 0; + inline const QString &id() const { return mId; } inline int nLabels() const { return mNLabels; } inline const QString &layoutDescription() { return mLayoutDescription; } + inline const std::list &layouts() { return mLayouts; } + inline const std::list &markups() { return mMarkups; } std::vector getOrigins() const; @@ -59,7 +65,8 @@ namespace libglabels virtual double h() const = 0; virtual const QString &sizeDescription( Units *units ) = 0; - virtual bool isSimilar( Frame *b ) const = 0; + virtual bool isSimilarTo( Frame *other ) const = 0; + private: QString mId; diff --git a/libglabels/FrameCd.cpp b/libglabels/FrameCd.cpp index 007208f..863db6d 100644 --- a/libglabels/FrameCd.cpp +++ b/libglabels/FrameCd.cpp @@ -29,22 +29,6 @@ namespace libglabels { - bool FrameCd::isSimilar( Frame *b ) const - { - if ( FrameCd *bCd = dynamic_cast(b) ) - { - if ( (fabs( mW - bCd->mW ) <= Constants::EPSILON) && - (fabs( mH - bCd->mH ) <= Constants::EPSILON) && - (fabs( mR1 - bCd->mR1 ) <= Constants::EPSILON) && - (fabs( mR2 - bCd->mR2 ) <= Constants::EPSILON) ) - { - return true; - } - } - return false; - } - - const QString &FrameCd::sizeDescription( Units *units ) { if ( units->id() == "in" ) @@ -67,5 +51,21 @@ namespace libglabels return mSizeDescription; } + + bool FrameCd::isSimilarTo( Frame *other ) const + { + if ( FrameCd *otherCd = dynamic_cast(other) ) + { + if ( (fabs( mW - otherCd->mW ) <= Constants::EPSILON) && + (fabs( mH - otherCd->mH ) <= Constants::EPSILON) && + (fabs( mR1 - otherCd->mR1 ) <= Constants::EPSILON) && + (fabs( mR2 - otherCd->mR2 ) <= Constants::EPSILON) ) + { + return true; + } + } + return false; + } + } diff --git a/libglabels/FrameCd.h b/libglabels/FrameCd.h index e980459..5e80006 100644 --- a/libglabels/FrameCd.h +++ b/libglabels/FrameCd.h @@ -40,6 +40,13 @@ namespace libglabels { } + FrameCd( const FrameCd &other ) + : mR1(other.mR1), mR2(other.mR2), mW(other.mW), mH(other.mH), mWaste(other.mWaste), Frame(other) + { + } + + Frame *dup() const { return new FrameCd( *this ); } + inline double r1() const { return mR1; } inline double r2() const { return mR2; } inline double waste() const { return mWaste; } @@ -48,7 +55,7 @@ namespace libglabels double h() const { return (mH == 0) ? 2*mR1 : mH; } const QString &sizeDescription( Units *units ); - bool isSimilar( Frame *b ) const; + bool isSimilarTo( Frame *other ) const; private: diff --git a/libglabels/FrameEllipse.cpp b/libglabels/FrameEllipse.cpp index 93c9981..9a96320 100644 --- a/libglabels/FrameEllipse.cpp +++ b/libglabels/FrameEllipse.cpp @@ -29,20 +29,6 @@ namespace libglabels { - bool FrameEllipse::isSimilar( Frame *b ) const - { - if ( FrameEllipse *bEllipse = dynamic_cast(b) ) - { - if ( (fabs( mW - bEllipse->mW ) <= Constants::EPSILON) && - (fabs( mH - bEllipse->mH ) <= Constants::EPSILON) ) - { - return true; - } - } - return false; - } - - const QString &FrameEllipse::sizeDescription( Units *units ) { if ( units->id() == "in" ) @@ -64,5 +50,19 @@ namespace libglabels } } + + bool FrameEllipse::isSimilarTo( Frame *other ) const + { + if ( FrameEllipse *otherEllipse = dynamic_cast(other) ) + { + if ( (fabs( mW - otherEllipse->mW ) <= Constants::EPSILON) && + (fabs( mH - otherEllipse->mH ) <= Constants::EPSILON) ) + { + return true; + } + } + return false; + } + } diff --git a/libglabels/FrameEllipse.h b/libglabels/FrameEllipse.h index 4743a67..b6d4448 100644 --- a/libglabels/FrameEllipse.h +++ b/libglabels/FrameEllipse.h @@ -38,13 +38,20 @@ namespace libglabels { } + FrameEllipse( const FrameEllipse &other ) + : mW(other.mW), mH(other.mH), mWaste(other.mWaste), Frame(other) + { + } + + Frame *dup() const { return new FrameEllipse( *this ); } + inline double waste() const { return mWaste; } double w() const { return mW; } double h() const { return mH; } const QString &sizeDescription( Units *units ); - bool isSimilar( Frame *b ) const; + bool isSimilarTo( Frame *other ) const; private: diff --git a/libglabels/FrameRect.cpp b/libglabels/FrameRect.cpp index 8b19023..46b770d 100644 --- a/libglabels/FrameRect.cpp +++ b/libglabels/FrameRect.cpp @@ -53,12 +53,12 @@ namespace libglabels } - bool FrameRect::isSimilar( Frame *b ) const + bool FrameRect::isSimilarTo( Frame *other ) const { - if ( FrameRect *bRect = dynamic_cast(b) ) + if ( FrameRect *otherRect = dynamic_cast(other) ) { - if ( (fabs( mW - bRect->mW ) <= Constants::EPSILON) && - (fabs( mH - bRect->mH ) <= Constants::EPSILON) ) + if ( (fabs( mW - otherRect->mW ) <= Constants::EPSILON) && + (fabs( mH - otherRect->mH ) <= Constants::EPSILON) ) { return true; } @@ -66,5 +66,6 @@ namespace libglabels return false; } + } diff --git a/libglabels/FrameRect.h b/libglabels/FrameRect.h index f69183a..1f11b5d 100644 --- a/libglabels/FrameRect.h +++ b/libglabels/FrameRect.h @@ -40,6 +40,14 @@ namespace libglabels { } + FrameRect( const FrameRect &other ) + : mW(other.mW), mH(other.mH), mR(other.mR), mXWaste(other.mXWaste), mYWaste(other.mYWaste), + Frame(other) + { + } + + Frame *dup() const { return new FrameRect( *this ); } + inline double r() const { return mR; } inline double xWaste() const { return mXWaste; } inline double yWaste() const { return mYWaste; } @@ -48,7 +56,7 @@ namespace libglabels double h() const { return mH; } const QString &sizeDescription( Units *units ); - bool isSimilar( Frame *b ) const; + bool isSimilarTo( Frame *other ) const; private: diff --git a/libglabels/FrameRound.cpp b/libglabels/FrameRound.cpp index a737a3c..eebf118 100644 --- a/libglabels/FrameRound.cpp +++ b/libglabels/FrameRound.cpp @@ -29,19 +29,6 @@ namespace libglabels { - bool FrameRound::isSimilar( Frame *b ) const - { - if ( FrameRound *bRound = dynamic_cast(b) ) - { - if ( fabs( mR - bRound->mR ) <= Constants::EPSILON ) - { - return true; - } - } - return false; - } - - const QString &FrameRound::sizeDescription( Units *units ) { if ( units->id() == "in" ) @@ -64,5 +51,18 @@ namespace libglabels return mSizeDescription; } + + bool FrameRound::isSimilarTo( Frame *other ) const + { + if ( FrameRound *otherRound = dynamic_cast(other) ) + { + if ( fabs( mR - otherRound->mR ) <= Constants::EPSILON ) + { + return true; + } + } + return false; + } + } diff --git a/libglabels/FrameRound.h b/libglabels/FrameRound.h index 0b35f4f..1371917 100644 --- a/libglabels/FrameRound.h +++ b/libglabels/FrameRound.h @@ -37,6 +37,13 @@ namespace libglabels { } + FrameRound( const FrameRound &other ) + : mR(other.mR), mWaste(other.mWaste), Frame(other) + { + } + + Frame *dup() const { return new FrameRound( *this ); } + inline double r() const { return mR; } inline double waste() const { return mWaste; } @@ -44,7 +51,7 @@ namespace libglabels double h() const { return 2*mR; } const QString &sizeDescription( Units *units ); - bool isSimilar( Frame *b ) const; + bool isSimilarTo( Frame *other ) const; private: diff --git a/libglabels/Layout.cpp b/libglabels/Layout.cpp index bbae805..caff342 100644 --- a/libglabels/Layout.cpp +++ b/libglabels/Layout.cpp @@ -28,14 +28,14 @@ namespace libglabels { - bool Layout::is_similar_to( const Layout &b ) + bool Layout::isSimilarTo( const Layout *other ) { - return ( (mNx == b.mNx) && - (mNy == b.mNy) && - (fabs(mX0 - b.mX0) < Constants::EPSILON) && - (fabs(mY0 - b.mY0) < Constants::EPSILON) && - (fabs(mDx - b.mDx) < Constants::EPSILON) && - (fabs(mDy - b.mDy) < Constants::EPSILON) ); + return ( (mNx == other->mNx) && + (mNy == other->mNy) && + (fabs(mX0 - other->mX0) < Constants::EPSILON) && + (fabs(mY0 - other->mY0) < Constants::EPSILON) && + (fabs(mDx - other->mDx) < Constants::EPSILON) && + (fabs(mDy - other->mDy) < Constants::EPSILON) ); } } diff --git a/libglabels/Layout.h b/libglabels/Layout.h index d54cc19..6532f86 100644 --- a/libglabels/Layout.h +++ b/libglabels/Layout.h @@ -33,6 +33,11 @@ namespace libglabels { } + Layout( const Layout &other ) + : mNx(other.mNx), mNy(other.mNy), mX0(other.mX0), mY0(other.mY0), mDx(other.mDx), mDy(other.mDy) + { + } + inline int nx() const { return mNx; } inline int ny() const { return mNy; } @@ -42,7 +47,13 @@ namespace libglabels inline double dx() const { return mDx; } inline double dy() const { return mDy; } - bool is_similar_to( const Layout &b ); + bool isSimilarTo( const Layout *other ); + + inline Layout *dup() const + { + Layout *other = new Layout( *this ); + return other; + } private: diff --git a/libglabels/Markup.h b/libglabels/Markup.h index 07bdf0b..fc7278b 100644 --- a/libglabels/Markup.h +++ b/libglabels/Markup.h @@ -27,6 +27,8 @@ namespace libglabels class Markup { + public: + virtual Markup *dup() const = 0; }; @@ -39,6 +41,8 @@ namespace libglabels inline double size() const { return mSize; } + Markup *dup() const { return new MarkupMargin( mSize ); } + private: double mSize; }; @@ -56,6 +60,8 @@ namespace libglabels inline double x2() const { return mX2; } inline double y2() const { return mY2; } + Markup *dup() const { return new MarkupLine( mX1, mY1, mX2, mY2 ); } + private: double mX1; double mY1; @@ -78,6 +84,8 @@ namespace libglabels inline double h() const { return mH; } inline double r() const { return mR; } + Markup *dup() const { return new MarkupRect( mX1, mY1, mW, mH, mR ); } + private: double mX1; double mY1; @@ -100,6 +108,8 @@ namespace libglabels inline double w() const { return mW; } inline double h() const { return mH; } + Markup *dup() const { return new MarkupEllipse( mX1, mY1, mW, mH ); } + private: double mX1; double mY1; @@ -120,6 +130,8 @@ namespace libglabels inline double y0() const { return mY0; } inline double r() const { return mR; } + Markup *dup() const { return new MarkupCircle( mX0, mY0, mR ); } + private: double mX0; double mY0; diff --git a/libglabels/Template.cpp b/libglabels/Template.cpp new file mode 100644 index 0000000..c0cd6f6 --- /dev/null +++ b/libglabels/Template.cpp @@ -0,0 +1,154 @@ +/* Template.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 "Template.h" + + +namespace libglabels +{ + + Template::Template( const Template &other ) + { + mBrand = other.mBrand; + mPart = other.mPart; + mDescription = other.mDescription; + mPaperId = other.mPaperId; + mPageWidth = other.mPageWidth; + mPageHeight = other.mPageHeight; + mEquivPart = other.mEquivPart; + mName = other.mName; + mProductUrl = other.mProductUrl; + + { + std::list::const_iterator it; + for ( it = other.mFrames.begin(); it != other.mFrames.end(); it++ ) + { + Frame *frame = (*it)->dup(); + + addFrame( frame ); + } + } + + { + std::list::const_iterator it; + for ( it = other.mCategoryIds.begin(); it != other.mCategoryIds.end(); it++ ) + { + addCategory( *it ); + } + } + } + + + // Generic full page template + Template *Template::full_page( const QString &paperId ) + { + // TODO + return NULL; + } + + + // From equivalent part number + Template *Template::from_equiv( const QString &brand, + const QString &part, + const QString &equiv_part ) + { + // TODO + return NULL; + } + + + void Template::addCategory( const QString &categoryId ) + { + mCategoryIds.push_back( categoryId ); + } + + + void Template::addFrame( Frame *frame ) + { + mFrames.push_back( frame ); + } + + + bool Template::operator==( const Template &other ) const + { + return (mBrand == other.mBrand) && (mPart == other.mPart); + } + + + bool Template::hasCategory( const QString &categoryId ) const + { + std::list::const_iterator it; + + for ( it = mCategoryIds.begin(); it != mCategoryIds.end(); it++ ) + { + if ( categoryId == *it ) + { + return true; + } + } + + return false; + } + + + bool Template::isSimilarTo( const Template &other ) const + { + // Does page size match? + if ( (mPaperId != other.mPaperId) || + (mPageWidth != other.mPageWidth ) || + (mPageHeight != other.mPageHeight ) ) + { + return false; + } + + // Are frames similar + Frame *frame1 = *(mFrames.begin()); + Frame *frame2 = *(other.mFrames.begin()); + if ( !frame1->isSimilarTo( frame2 ) ) + { + return false; + } + + // Are they layed out similarly? + std::list::const_iterator it1; + std::list::const_iterator it2; + for ( it1 = frame1->layouts().begin(); it1 != frame1->layouts().end(); it1++ ) + { + bool matchFound = false; + for ( it2 = frame2->layouts().begin(); it2 != frame2->layouts().end(); it2++ ) + { + if ( (*it1)->isSimilarTo(*it2) ) + { + matchFound = true; + break; + } + } + if ( !matchFound ) + { + return false; + } + } + + return true; + } + + +} + diff --git a/libglabels/Template.h b/libglabels/Template.h new file mode 100644 index 0000000..9e57bff --- /dev/null +++ b/libglabels/Template.h @@ -0,0 +1,114 @@ +/* Template.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_Template_h +#define libglabels_Template_h + +#include +#include + +#include +#include + +#include "Units.h" +#include "Point.h" +#include "Frame.h" + + +namespace libglabels +{ + + class Template + { + Q_DECLARE_TR_FUNCTIONS(Template) + + public: + Template( const QString &brand, + const QString &part, + const QString &description, + const QString &paperId, + double pageWidth = 0, + double pageHeight = 0 ) + : mBrand(brand), + mPart(part), + mDescription(description), + mPaperId(paperId), + mPageWidth(pageWidth), + mPageHeight(pageHeight) + { + } + + Template( const Template &other ); + + inline Template *dup() const { return new Template( *this ); } + + + // Generic full page template + static Template *full_page( const QString &paperId ); + + // From equivalent part number + static Template *from_equiv( const QString &brand, + const QString &part, + const QString &equiv_part ); + + + inline const QString &brand() const { return mBrand; } + inline const QString &part() const { return mPart; } + inline const QString &description() const { return mDescription; } + + inline const QString &paperId() const { return mPaperId; } + inline double pageWidth() const { return mPageWidth; } + inline double pageHeight() const { return mPageHeight; } + + inline const QString &equivPart() const { return mEquivPart; } + inline void setEquivPart( const QString &value ) { mEquivPart = value; } + + inline const QString &productUrl() const { return mProductUrl; } + inline void setProductUrl( const QString &value ) { mProductUrl = value; } + + void addCategory( const QString &categoryId ); + void addFrame( Frame *frame ); + + bool operator==( const Template &other ) const; + + bool hasCategory( const QString &categoryId ) const; + bool isSimilarTo( const Template &other ) const; + + private: + QString mBrand; + QString mPart; + QString mDescription; + + QString mPaperId; + double mPageWidth; + double mPageHeight; + + QString mEquivPart; + QString mName; + + QString mProductUrl; + std::list mCategoryIds; + + std::list mFrames; + }; + +} + +#endif // libglabels_Template_h