From 336d892983365ad7e5fc14549d4c0493334a6153 Mon Sep 17 00:00:00 2001 From: Jim Evins Date: Sun, 27 Oct 2013 23:16:36 -0400 Subject: [PATCH] Some of the low-hanging fruit in libglabels. --- CMakeLists.txt | 9 ++- libglabels/CMakeLists.txt | 34 +++++++++ libglabels/Category.cpp | 21 +++++ libglabels/Category.h | 49 ++++++++++++ libglabels/Layout.cpp | 41 ++++++++++ libglabels/Layout.h | 59 ++++++++++++++ libglabels/Paper.cpp | 21 +++++ libglabels/Paper.h | 65 ++++++++++++++++ libglabels/Point.cpp | 54 +++++++++++++ libglabels/Point.h | 48 ++++++++++++ libglabels/Units.cpp | 140 ++++++++++++++++++++++++++++++++++ libglabels/Units.h | 79 +++++++++++++++++++ libglabels/Vendor.cpp | 21 +++++ libglabels/Vendor.h | 49 ++++++++++++ libglabels/privateConstants.h | 40 ++++++++++ 15 files changed, 726 insertions(+), 4 deletions(-) create mode 100644 libglabels/CMakeLists.txt create mode 100644 libglabels/Category.cpp create mode 100644 libglabels/Category.h create mode 100644 libglabels/Layout.cpp create mode 100644 libglabels/Layout.h create mode 100644 libglabels/Paper.cpp create mode 100644 libglabels/Paper.h create mode 100644 libglabels/Point.cpp create mode 100644 libglabels/Point.h create mode 100644 libglabels/Units.cpp create mode 100644 libglabels/Units.h create mode 100644 libglabels/Vendor.cpp create mode 100644 libglabels/Vendor.h create mode 100644 libglabels/privateConstants.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0b5f88f..7e3ef6b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,11 @@ cmake_minimum_required (VERSION 2.8) -project (qtLabels) +project (glabels-qt) -set (Package_Name "qtLabels") -set (Short_Name "qtLabels") -set (Package_URL "https://github.com/jimevins/qtLabels") +set (Package_Name "glabels-qt") +set (Short_Name "glabels-qt") +set (Package_URL "https://github.com/jimevins/glabels-qt") set (Major_Version "0") set (Minor_Version "0") @@ -26,4 +26,5 @@ add_definitions (-g) # Subdirectories # add_subdirectory (app) +add_subdirectory (libglabels) diff --git a/libglabels/CMakeLists.txt b/libglabels/CMakeLists.txt new file mode 100644 index 0000000..e6bd0f1 --- /dev/null +++ b/libglabels/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required (VERSION 2.8) + +project (libglabels CXX) + +set (libglabels_sources + Category.cpp + Paper.cpp + Vendor.cpp + Units.cpp + Point.cpp + Layout.cpp +) + +set (libglabels_qobject_headers +) + +set (libglabels_resource_files +) + +qt4_wrap_cpp (libglabels_moc_sources ${libglabels_qobject_headers}) +qt4_add_resources(libglabels_qrc_sources ${libglabels_resource_files}) + +include (${QT_USE_FILE}) + + +include_directories ( +) + +link_directories ( +) + +add_library (libglabels ${libglabels_sources} ${libglabels_moc_sources} ${libglabels_qrc_sources}) + +install (TARGETS libglabels ARCHIVE DESTINATION lib) diff --git a/libglabels/Category.cpp b/libglabels/Category.cpp new file mode 100644 index 0000000..d598836 --- /dev/null +++ b/libglabels/Category.cpp @@ -0,0 +1,21 @@ +/* Category.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 "Category.h" diff --git a/libglabels/Category.h b/libglabels/Category.h new file mode 100644 index 0000000..a648918 --- /dev/null +++ b/libglabels/Category.h @@ -0,0 +1,49 @@ +/* Category.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_Category_h +#define libglabels_Category_h + + +#include + + +namespace libglabels +{ + + class Category + { + public: + Category( const QString &id, const QString &value ) : mId(id), mValue(value) + { + } + + inline QString id() const { return mId; } + + inline QString value() const { return mValue; } + + private: + QString mId; + QString mValue; + }; + +} + +#endif // libglabels_Category_h diff --git a/libglabels/Layout.cpp b/libglabels/Layout.cpp new file mode 100644 index 0000000..bbae805 --- /dev/null +++ b/libglabels/Layout.cpp @@ -0,0 +1,41 @@ +/* Layout.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 "Layout.h" + +#include + +#include "privateConstants.h" + + +namespace libglabels +{ + + bool Layout::is_similar_to( const Layout &b ) + { + 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) ); + } + +} diff --git a/libglabels/Layout.h b/libglabels/Layout.h new file mode 100644 index 0000000..d54cc19 --- /dev/null +++ b/libglabels/Layout.h @@ -0,0 +1,59 @@ +/* Layout.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_Layout_h +#define libglabels_Layout_h + + +namespace libglabels +{ + + class Layout + { + public: + Layout( int nx, int ny, double x0, double y0, double dx, double dy ) + : mNx(nx), mNy(ny), mX0(x0), mY0(y0), mDx(dx), mDy(dy) + { + } + + inline int nx() const { return mNx; } + inline int ny() const { return mNy; } + + inline double x0() const { return mX0; } + inline double y0() const { return mY0; } + + inline double dx() const { return mDx; } + inline double dy() const { return mDy; } + + bool is_similar_to( const Layout &b ); + + + private: + int mNx; + int mNy; + double mX0; + double mY0; + double mDx; + double mDy; + }; + +} + +#endif // libglabels_Layout_h diff --git a/libglabels/Paper.cpp b/libglabels/Paper.cpp new file mode 100644 index 0000000..938f368 --- /dev/null +++ b/libglabels/Paper.cpp @@ -0,0 +1,21 @@ +/* Paper.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 "Paper.h" diff --git a/libglabels/Paper.h b/libglabels/Paper.h new file mode 100644 index 0000000..720fe49 --- /dev/null +++ b/libglabels/Paper.h @@ -0,0 +1,65 @@ +/* Paper.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_Paper_h +#define libglabels_Paper_h + + +#include + + +namespace libglabels +{ + + class Paper + { + public: + Paper( const QString &id, + const QString &name, + double width, + double height, + const QString &pwgSize ) : mId(id), mName(name), mWidth(width), mHeight(height), mPwgSize(pwgSize) + { + } + + inline QString id() const { return mId; } + + inline QString name() const { return mName; } + + /* Width (in points) */ + inline double width() const { return mWidth; } + + /* Height (in points) */ + inline double height() const { return mHeight; } + + /* PWG 5101.1-2002 size name */ + inline QString pwgSize() const { return mPwgSize; } + + private: + QString mId; + QString mName; + double mWidth; + double mHeight; + QString mPwgSize; + }; + +} + +#endif // libglabels_Paper_h diff --git a/libglabels/Point.cpp b/libglabels/Point.cpp new file mode 100644 index 0000000..c8c3f5d --- /dev/null +++ b/libglabels/Point.cpp @@ -0,0 +1,54 @@ +/* Point.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 "Point.h" + + +namespace libglabels +{ + + int Point::compare_to( const Point &b ) + { + if ( mY < b.mY ) + { + return -1; + } + else if ( mY > b.mY ) + { + return 1; + } + else + { + if ( mX < b.mX ) + { + return -1; + } + else if ( mX > b.mX ) + { + return 1; + } + else + { + return 0; /* hopefully 2 label frames won't have the same origin. */ + } + } + } + +} diff --git a/libglabels/Point.h b/libglabels/Point.h new file mode 100644 index 0000000..eb8a3ee --- /dev/null +++ b/libglabels/Point.h @@ -0,0 +1,48 @@ +/* Point.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_Point_h +#define libglabels_Point_h + + +namespace libglabels +{ + + class Point + { + public: + Point( double x, double y ) : mX(x), mY(y) + { + } + + inline double x() const { return mX; } + inline double y() const { return mY; } + + int compare_to( const Point &b ); + + + private: + double mX; + double mY; + }; + +} + +#endif // libglabels_Point_h diff --git a/libglabels/Units.cpp b/libglabels/Units.cpp new file mode 100644 index 0000000..89efd27 --- /dev/null +++ b/libglabels/Units.cpp @@ -0,0 +1,140 @@ +/* Units.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 "Units.h" + +#include + + +namespace +{ + const double POINTS_PER_POINT = 1.0; /* internal units are points. */ + const double POINTS_PER_INCH = 72.0; + const double POINTS_PER_MM = 2.83464566929; + const double POINTS_PER_CM = (10.0*POINTS_PER_MM); + const double POINTS_PER_PICA = (1.0/12.0); +} + + +namespace libglabels +{ + + Units *Units::from_id( const QString &id ) + { + if ( id == "pt" ) + { + return point(); + } + else if ( id == "in" ) + { + return inch(); + } + else if ( id == "mm" ) + { + return mm(); + } + else if ( id == "cm" ) + { + return cm(); + } + else if ( id == "pc" ) + { + return pica(); + } + else if ( id == "" ) + { + /* Missing or empty units id defaults to points. */ + return point(); + } + else + { + qWarning() << "Unknown Units ID \"" << id << "\", defaults to \"pt\""; + return point(); + } + + } + + + Units *Units::point() + { + static Units *instance = NULL; + + if ( instance == NULL ) + { + instance = new Units( "pt", tr("points"), POINTS_PER_POINT ); + } + + return instance; + } + + + Units *Units::inch() + { + static Units *instance = NULL; + + if ( instance == NULL ) + { + instance = new Units( "in", tr("inches"), POINTS_PER_INCH ); + } + + return instance; + } + + + Units *Units::mm() + { + static Units *instance = NULL; + + if ( instance == NULL ) + { + instance = new Units( "mm", tr("mm"), POINTS_PER_MM ); + } + + return instance; + } + + + Units *Units::cm() + { + static Units *instance = NULL; + + if ( instance == NULL ) + { + instance = new Units( "cm", tr("cm"), POINTS_PER_CM ); + } + + return instance; + } + + + Units *Units::pica() + { + static Units *instance = NULL; + + if ( instance == NULL ) + { + instance = new Units( "pc", tr("picas"), POINTS_PER_PICA ); + } + + return instance; + } + + +} diff --git a/libglabels/Units.h b/libglabels/Units.h new file mode 100644 index 0000000..232b54f --- /dev/null +++ b/libglabels/Units.h @@ -0,0 +1,79 @@ +/* Units.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_Units_h +#define libglabels_Units_h + + +#include +#include + + +namespace libglabels +{ + + + class Units + { + Q_DECLARE_TR_FUNCTIONS(Units) + + + private: + Units( const QString &id, const QString &name, double pointsPerUnit ) + : mId(id), mName(name), mPointsPerUnit(pointsPerUnit) + { + mUnitsPerPoint = 1.0 / mPointsPerUnit; + } + + + public: + inline QString id() const { return mId; } + + inline QString name() const { return mName; } + + inline double pointsPerUnit() const { return mPointsPerUnit; } + + inline double unitsPerPoint() const { return mUnitsPerPoint; } + + + static Units *from_id( const QString &id ); + + static Units *point(); + + static Units *inch(); + + static Units *mm(); + + static Units *cm(); + + static Units *pica(); + + + private: + QString mId; + QString mName; + double mPointsPerUnit; + double mUnitsPerPoint; + + }; + +} + +#endif // libglabels_Units_h diff --git a/libglabels/Vendor.cpp b/libglabels/Vendor.cpp new file mode 100644 index 0000000..4b5c0b3 --- /dev/null +++ b/libglabels/Vendor.cpp @@ -0,0 +1,21 @@ +/* Vendor.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 "Vendor.h" diff --git a/libglabels/Vendor.h b/libglabels/Vendor.h new file mode 100644 index 0000000..1bc8f49 --- /dev/null +++ b/libglabels/Vendor.h @@ -0,0 +1,49 @@ +/* Vendor.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_Vendor_h +#define libglabels_Vendor_h + + +#include + + +namespace libglabels +{ + + class Vendor + { + public: + Vendor( const QString &name, const QString &url ) : mName(name), mUrl(url) + { + } + + inline QString name() const { return mName; } + + inline QString url() const { return mUrl; } + + private: + QString mName; + QString mUrl; + }; + +} + +#endif // libglabels_Vendor_h diff --git a/libglabels/privateConstants.h b/libglabels/privateConstants.h new file mode 100644 index 0000000..c94247f --- /dev/null +++ b/libglabels/privateConstants.h @@ -0,0 +1,40 @@ +/* privateConstants.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_privateConstants_h +#define libglabels_privateConstants_h + + +#include + + +namespace libglabels +{ + + namespace Constants + { + + const double EPSILON = 0.5; /* Allowed error when comparing dimensions. (0.5pts ~= .007in ~= .2mm) */ + + } + +} + +#endif // libglabels_privateConstants_h