diff --git a/libglabels/CMakeLists.txt b/libglabels/CMakeLists.txt index e6bd0f1..d9958cb 100644 --- a/libglabels/CMakeLists.txt +++ b/libglabels/CMakeLists.txt @@ -9,6 +9,10 @@ set (libglabels_sources Units.cpp Point.cpp Layout.cpp + Markup.cpp + Frame.cpp + FrameRect.cpp + StrUtil.cpp ) set (libglabels_qobject_headers diff --git a/libglabels/Frame.cpp b/libglabels/Frame.cpp new file mode 100644 index 0000000..62a518a --- /dev/null +++ b/libglabels/Frame.cpp @@ -0,0 +1,100 @@ +/* Frame.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 "Frame.h" + + +namespace libglabels +{ + + int Frame::nLabels() const + { + int n = 0; + + std::list::const_iterator it; + for ( it = mLayouts.begin(); it != mLayouts.end(); it++ ) + { + n += (*it)->nx() * (*it)->ny(); + } + + return n; + } + + + QString &Frame::getLayoutDescription() const + { + QString description; + + if ( mLayouts.size() == 1 ) + { + Layout *layout = *mLayouts.begin(); + + /* + * Translators: %1 = number of labels across a page, + * %2 = number of labels down a page, + * %3 = total number of labels on a page (sheet). + */ + description = QString( tr("%1 x %2 (%3 per sheet)") ) + .arg(layout->nx()).arg(layout->ny()).arg(nLabels()); + } + else + { + /* Translators: %1 is the total number of labels on a page (sheet). */ + description = QString( tr("%1 per sheet") ).arg( nLabels() ); + } + + return description; + } + + + std::vector Frame::getOrigins() const + { + std::vector origins( nLabels() ); + + std::list::const_iterator it; + for ( it = mLayouts.begin(); it != mLayouts.end(); it++ ) + { + Layout *lo = *it; + + for ( int iy = 0; iy < lo->ny(); iy++ ) + { + for ( int ix = 0; ix < lo->nx(); ix++ ) + { + origins.push_back( Point( ix*lo->dx() + lo->x0(), iy*lo->dy() + lo->y0() ) ); + } + } + } + + std::sort( origins.begin(), origins.end(), Point::compare ); + } + + + void Frame::addLayout( Layout *layout ) + { + mLayouts.push_back( layout ); + } + + + void Frame::addMarkup( Markup *markup ) + { + mMarkups.push_back( markup ); + } + +} diff --git a/libglabels/Frame.h b/libglabels/Frame.h new file mode 100644 index 0000000..1655fe8 --- /dev/null +++ b/libglabels/Frame.h @@ -0,0 +1,71 @@ +/* Frame.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_Frame_h +#define libglabels_Frame_h + +#include +#include + +#include +#include + +#include "Units.h" +#include "Point.h" +#include "Layout.h" +#include "Markup.h" + + +namespace libglabels +{ + + class Frame + { + Q_DECLARE_TR_FUNCTIONS(Frame) + + protected: + Frame( QString id = "0" ) : mId(id) + { + } + + public: + inline const QString &id() const { return mId; } + + virtual void getSize( double *w, double *h ) const = 0; + virtual bool isSimilar( Frame *b ) const = 0; + virtual QString &getSizeDescription( Units *units ) const = 0; + + int nLabels() const; + QString &getLayoutDescription() const; + std::vector getOrigins() const; + + void addLayout( Layout *layout ); + void addMarkup( Markup *markup ); + + private: + QString mId; + + std::list mLayouts; + std::list mMarkups; + }; + +} + +#endif // libglabels_Frame_h diff --git a/libglabels/FrameRect.cpp b/libglabels/FrameRect.cpp new file mode 100644 index 0000000..023ef6e --- /dev/null +++ b/libglabels/FrameRect.cpp @@ -0,0 +1,75 @@ +/* FrameRect.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 "FrameRect.h" + +#include + +#include "StrUtil.h" +#include "privateConstants.h" + + +namespace libglabels +{ + + void FrameRect::getSize( double *w, double *h ) const + { + *w = mW; + *h = mH; + } + + + bool FrameRect::isSimilar( Frame *b ) const + { + if ( FrameRect *bRect = dynamic_cast(b) ) + { + if ( (fabs( mW - bRect->mW ) <= Constants::EPSILON) && + (fabs( mH - bRect->mH ) <= Constants::EPSILON) ) + { + return true; + } + } + return false; + } + + + QString &FrameRect::getSizeDescription( Units *units ) const + { + if ( units->id() == "in" ) + { + QString wStr = StrUtil::formatFraction( mW * units->unitsPerPoint() ); + QString hStr = StrUtil::formatFraction( mH * units->unitsPerPoint() ); + + return QString().sprintf( "%s x %s %s", + wStr.toStdString().c_str(), + hStr.toStdString().c_str(), + units->name().toStdString().c_str() ); + } + else + { + return QString().sprintf( "%.5g x %.5g %s", + mW * units->unitsPerPoint(), + mH * units->unitsPerPoint(), + units->name().toStdString().c_str() ); + } + } + +} + diff --git a/libglabels/FrameRect.h b/libglabels/FrameRect.h new file mode 100644 index 0000000..cdb34fb --- /dev/null +++ b/libglabels/FrameRect.h @@ -0,0 +1,66 @@ +/* FrameRect.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_FrameRect_h +#define libglabels_FrameRect_h + +#include "Frame.h" + + +namespace libglabels +{ + + class FrameRect : public Frame + { + public: + FrameRect( double w, + double h, + double r, + double xWaste, + double yWaste, + QString id = "0" ) + : mW(w), mH(h), mR(r), mXWaste(xWaste), mYWaste(yWaste), Frame(id) + { + } + + inline double w() const { return mW; } + inline double h() const { return mH; } + inline double r() const { return mR; } + inline double xWaste() const { return mXWaste; } + inline double yWaste() const { return mYWaste; } + + + void getSize( double *w, double *h ) const; + bool isSimilar( Frame *b ) const; + QString &getSizeDescription( Units *units ) const; + + + private: + double mW; + double mH; + double mR; + double mXWaste; + double mYWaste; + + }; + +} + +#endif // libglabels_FrameRect_h diff --git a/libglabels/Markup.cpp b/libglabels/Markup.cpp new file mode 100644 index 0000000..87b36f9 --- /dev/null +++ b/libglabels/Markup.cpp @@ -0,0 +1,22 @@ +/* Markup.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 "Markup.h" + diff --git a/libglabels/Markup.h b/libglabels/Markup.h new file mode 100644 index 0000000..07bdf0b --- /dev/null +++ b/libglabels/Markup.h @@ -0,0 +1,134 @@ +/* Markup.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_Markup_h +#define libglabels_Markup_h + + +namespace libglabels +{ + + class Markup + { + }; + + + class MarkupMargin : public Markup + { + public: + MarkupMargin( double size ) : mSize(size) + { + } + + inline double size() const { return mSize; } + + private: + double mSize; + }; + + + class MarkupLine : public Markup + { + public: + MarkupLine( double x1, double y1, double x2, double y2 ) : mX1(x1), mY1(y1), mX2(x2), mY2(y2) + { + } + + inline double x1() const { return mX1; } + inline double y1() const { return mY1; } + inline double x2() const { return mX2; } + inline double y2() const { return mY2; } + + private: + double mX1; + double mY1; + double mX2; + double mY2; + }; + + + class MarkupRect : public Markup + { + public: + MarkupRect( double x1, double y1, double w, double h, double r ) + : mX1(x1), mY1(y1), mW(w), mH(h), mR(r) + { + } + + inline double x1() const { return mX1; } + inline double y1() const { return mY1; } + inline double w() const { return mW; } + inline double h() const { return mH; } + inline double r() const { return mR; } + + private: + double mX1; + double mY1; + double mW; + double mH; + double mR; + }; + + + class MarkupEllipse : public Markup + { + public: + MarkupEllipse( double x1, double y1, double w, double h ) + : mX1(x1), mY1(y1), mW(w), mH(h) + { + } + + inline double x1() const { return mX1; } + inline double y1() const { return mY1; } + inline double w() const { return mW; } + inline double h() const { return mH; } + + private: + double mX1; + double mY1; + double mW; + double mH; + }; + + + class MarkupCircle : public Markup + { + public: + MarkupCircle( double x0, double y0, double r ) + : mX0(x0), mY0(y0), mR(r) + { + } + + inline double x0() const { return mX0; } + inline double y0() const { return mY0; } + inline double r() const { return mR; } + + private: + double mX0; + double mY0; + double mR; + }; + + + + +} + +#endif // libglabels_Markup_h diff --git a/libglabels/Point.cpp b/libglabels/Point.cpp index c8c3f5d..3c368f1 100644 --- a/libglabels/Point.cpp +++ b/libglabels/Point.cpp @@ -24,23 +24,23 @@ namespace libglabels { - int Point::compare_to( const Point &b ) + int Point::compare( const Point &a, const Point &b ) { - if ( mY < b.mY ) + if ( a.mY < b.mY ) { return -1; } - else if ( mY > b.mY ) + else if ( a.mY > b.mY ) { return 1; } else { - if ( mX < b.mX ) + if ( a.mX < b.mX ) { return -1; } - else if ( mX > b.mX ) + else if ( a.mX > b.mX ) { return 1; } diff --git a/libglabels/Point.h b/libglabels/Point.h index eb8a3ee..6c2e08d 100644 --- a/libglabels/Point.h +++ b/libglabels/Point.h @@ -28,6 +28,10 @@ namespace libglabels class Point { public: + Point() : mX(0), mY(0) + { + } + Point( double x, double y ) : mX(x), mY(y) { } @@ -35,7 +39,7 @@ namespace libglabels inline double x() const { return mX; } inline double y() const { return mY; } - int compare_to( const Point &b ); + static int compare( const Point &a, const Point &b ); private: diff --git a/libglabels/StrUtil.cpp b/libglabels/StrUtil.cpp new file mode 100644 index 0000000..15d6671 --- /dev/null +++ b/libglabels/StrUtil.cpp @@ -0,0 +1,79 @@ +/* StrUtil.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 "StrUtil.h" + +#include + + +namespace +{ + const double FRAC_EPSILON = 0.00005; + const double denom[] = { 1.0, 2.0, 3.0, 4.0, 8.0, 16.0, 32.0, 0.0 }; + const char* denom_string[] = { "1", "₂", "₃", "₄", "₈", "₁₆", "₃₂", NULL }; + const char* num_string[] = { "⁰", "¹", "²", "³", "⁴", "⁵", "⁶", "⁷", "⁸", "⁹", + "¹⁰", "¹¹", "¹²", "¹³", "¹⁴", "¹⁵", "¹⁶", "¹⁷", "¹⁸", "¹⁹", + "²⁰", "²¹", "²²", "²³", "²⁴", "²⁵", "²⁶", "²⁷", "²⁸", "²⁹", + "³⁰", "³¹" }; +} +namespace libglabels +{ + + namespace StrUtil + { + + QString &formatFraction( double x ) + { + int i; + double product, remainder; + + for ( i=0; denom[i] != 0.0; i++ ) + { + product = x * denom[i]; + remainder = fabs(product - ((int)(product+0.5))); + if ( remainder < FRAC_EPSILON ) break; + } + + if ( denom[i] == 0.0 ) + { + /* None of our denominators work. */ + return QString().sprintf( "%.5g", x ); + } + if ( denom[i] == 1.0 ) + { + /* Simple integer. */ + return QString().sprintf( "%.0f", x ); + } + int n = (int)( x * denom[i] + 0.5 ); + int d = (int)denom[i]; + if ( n > d ) + { + return QString().sprintf( "%d%s/%s", (n/d), num_string[n%d], denom_string[i] ); + } + else + { + return QString().sprintf( "%s/%s", num_string[n%d], denom_string[i] ); + } + } + + } + +} + diff --git a/libglabels/StrUtil.h b/libglabels/StrUtil.h new file mode 100644 index 0000000..f9917c1 --- /dev/null +++ b/libglabels/StrUtil.h @@ -0,0 +1,39 @@ +/* StrUtil.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_StrUtil_h +#define libglabels_StrUtil_h + +#include + + +namespace libglabels +{ + + namespace StrUtil + { + + QString &formatFraction( double x ); + + } + +} + +#endif // libglabels_StrUtil_h