File indexing completed on 2024-05-19 15:27:51

0001 /* This file is part of KGraphViewer.
0002    Copyright (C) 2005-2007 Gael de Chalendar <kleag@free.fr>
0003 
0004    KGraphViewer is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU General Public
0006    License as published by the Free Software Foundation, version 2.
0007 
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0011    General Public License for more details.
0012 
0013    You should have received a copy of the GNU General Public License
0014    along with this program; if not, write to the Free Software
0015    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0016    02110-1301, USA
0017 */
0018 
0019 /* This file was part of the KDE project
0020    Copyright (C) 2005 Jarosław Staniek <staniek@kde.org>
0021 
0022    This program is free software; you can redistribute it and/or
0023    modify it under the terms of the GNU Library General Public
0024    License as published by the Free Software Foundation; either
0025    version 2 of the License, or (at your option) any later version.
0026  */
0027 
0028 #ifndef KGVUNIT_H
0029 #define KGVUNIT_H
0030 
0031 #include <math.h> // for floor
0032 #include <qstring.h>
0033 #include <qstringlist.h>
0034 
0035 // 1 inch ^= 72 pt
0036 // 1 inch ^= 25.399956 mm (-pedantic ;p)
0037 // 1 pt = 1/12 pi
0038 // 1 pt ^= 0.0077880997 cc
0039 // 1 cc = 12 dd
0040 // Note: I don't use division but multiplication with the inverse value
0041 // because it's faster ;p (Werner)
0042 #define POINT_TO_MM(px) ((px)*0.352777167)
0043 #define MM_TO_POINT(mm) ((mm)*2.83465058)
0044 #define POINT_TO_CM(px) ((px)*0.0352777167)
0045 #define CM_TO_POINT(cm) ((cm)*28.3465058)
0046 #define POINT_TO_DM(px) ((px)*0.00352777167)
0047 #define DM_TO_POINT(dm) ((dm)*283.465058)
0048 #define POINT_TO_INCH(px) ((px)*0.01388888888889)
0049 #define INCH_TO_POINT(inch) ((inch)*72.0)
0050 #define MM_TO_INCH(mm) ((mm)*0.039370147)
0051 #define INCH_TO_MM(inch) ((inch)*25.399956)
0052 #define POINT_TO_PI(px) ((px)*0.083333333)
0053 #define POINT_TO_DD(px) ((px)*0.006490083)
0054 #define POINT_TO_CC(px) ((px)*0.077880997)
0055 #define PI_TO_POINT(pi) ((pi)*12)
0056 #define DD_TO_POINT(dd) ((dd)*154.08124)
0057 #define CC_TO_POINT(cc) ((cc)*12.840103)
0058 /**
0059  * %KOffice stores everything in pt (using "double") internally.
0060  * When displaying a value to the user, the value is converted to the user's unit
0061  * of choice, and rounded to a reasonable precision to avoid 0.999999
0062  */
0063 class KgvUnit
0064 {
0065 public:
0066     /** Length units supported by KOffice. */
0067     enum Unit {
0068         U_MM = 0,
0069         U_PT = 1,
0070         U_INCH = 2,
0071         U_CM = 3,
0072         U_DM = 4,
0073         U_PI = 5,         // pica
0074         U_DD = 6,         // didot
0075         U_CC = 7,         // cicero
0076         U_LASTUNIT = U_CC // update when adding a new unit
0077         // when adding a new unit, make sure to implement support for it in koRuler too
0078     };
0079 
0080     /// Prepare ptValue to be displayed in pt
0081     static double toPoint(double ptValue)
0082     {
0083         // No conversion, only rounding (to 0.001 precision)
0084         return floor(ptValue * 1000.0) / 1000.0;
0085     }
0086 
0087     /// Prepare ptValue to be displayed in mm
0088     static double toMM(double ptValue)
0089     {
0090         // "mm" values are rounded to 0.0001 millimeters
0091         return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0;
0092     }
0093 
0094     /// Prepare ptValue to be displayed in cm
0095     static double toCM(double ptValue)
0096     {
0097         return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0;
0098     }
0099 
0100     /// Prepare ptValue to be displayed in dm
0101     static double toDM(double ptValue)
0102     {
0103         return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0;
0104     }
0105 
0106     /// Prepare ptValue to be displayed in inch
0107     static double toInch(double ptValue)
0108     {
0109         // "in" values are rounded to 0.00001 inches
0110         return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0;
0111     }
0112 
0113     /// Prepare ptValue to be displayed in pica
0114     static double toPI(double ptValue)
0115     {
0116         // "pi" values are rounded to 0.00001 inches
0117         return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0;
0118     }
0119 
0120     /// Prepare ptValue to be displayed in didot
0121     static double toDD(double ptValue)
0122     {
0123         // "dd" values are rounded to 0.00001 inches
0124         return floor(POINT_TO_DD(ptValue) * 100000.0) / 100000.0;
0125     }
0126 
0127     /// Prepare ptValue to be displayed in cicero
0128     static double toCC(double ptValue)
0129     {
0130         // "cc" values are rounded to 0.00001 inches
0131         return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0;
0132     }
0133 
0134     /**
0135      * This method is the one to use to display a value in a dialog
0136      * \return the value @p ptValue converted to @p unit and rounded, ready to be displayed
0137      * Old name: ptToUnit
0138      */
0139     static double toUserValue(double ptValue, Unit unit);
0140 
0141     /**
0142      * Convert the value @p ptValue to a given unit @p unit
0143      * Unlike KoUnit::ptToUnit the return value remains unrounded, so that it can be used in complex calculation
0144      * \return the converted value
0145      * Old name: ptToUnitUnrounded
0146      */
0147     static double ptToUnit(const double ptValue, const Unit unit);
0148 
0149     /// This method is the one to use to display a value in a dialog
0150     /// @return the value @p ptValue converted to @p unit and rounded, ready to be displayed
0151     /// Old name: userValue
0152     static QString toUserStringValue(double ptValue, Unit unit);
0153 
0154     /// This method is the one to use to read a value from a dialog
0155     /// @return the value in @p unit, converted to points for internal use
0156     /// Old name: ptFromUnit
0157     static double fromUserValue(double value, Unit unit);
0158 
0159     /// This method is the one to use to read a value from a dialog
0160     /// @param value value entered by the user
0161     /// @param unit unit type selected by the user
0162     /// @param ok if set, the pointed bool is set to true if the value could be
0163     /// converted to a double, and to false otherwise.
0164     /// @return the value in @p unit, converted to points for internal use
0165     static double fromUserValue(const QString &value, Unit unit, bool *ok = nullptr);
0166 
0167     /// Convert a unit name into a Unit enum
0168     /// @param _unitName name to convert
0169     /// @param ok if set, it will be true if the unit was known, false if unknown
0170     static Unit unit(const QString &_unitName, bool *ok = nullptr);
0171     /// Get the name of a unit
0172     static QString unitName(Unit _unit);
0173     /// Get the full (translated) description of a unit
0174     static QString unitDescription(Unit _unit);
0175     static QStringList listOfUnitName();
0176 
0177     /// parse common %KOffice and OO values, like "10cm", "5mm" to pt
0178     static double parseValue(const QString &value, double defaultVal = 0.0);
0179     // Note: the above method doesn't take a const ref, since it modifies the arg.
0180 };
0181 
0182 #endif // KGVUNIT_H