Warning, file /office/calligra/libs/odf/KoUnit.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* This file is part of the KDE project 0002 Copyright (C) 1998, 1999 Reginald Stadlbauer <reggie@kde.org> 0003 Copyright (C) 1998, 1999 Torben Weis <weis@kde.org> 0004 Copyright (C) 2004, Nicolas GOUTTE <goutte@kde.org> 0005 Copyright (C) 2010 Thomas Zander <zander@kde.org> 0006 Copyright 2012 Friedrich W. H. Kossebau <kossebau@kde.org> 0007 0008 This library is free software; you can redistribute it and/or 0009 modify it under the terms of the GNU Library General Public 0010 License as published by the Free Software Foundation; either 0011 version 2 of the License, or (at your option) any later version. 0012 0013 This library is distributed in the hope that it will be useful, 0014 but WITHOUT ANY WARRANTY; without even the implied warranty of 0015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0016 Library General Public License for more details. 0017 0018 You should have received a copy of the GNU Library General Public License 0019 along with this library; see the file COPYING.LIB. If not, write to 0020 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0021 * Boston, MA 02110-1301, USA. 0022 */ 0023 0024 #ifndef KOUNIT_H 0025 #define KOUNIT_H 0026 0027 // Calligra 0028 #include "koodf_export.h" 0029 // Qt 0030 #include <QString> 0031 #include <QDebug> 0032 #include <QMetaType> 0033 // std 0034 #include <math.h> // for floor 0035 0036 class QStringList; 0037 0038 // 1 inch ^= 72 pt 0039 // 1 inch ^= 25.399956 mm (-pedantic ;p) 0040 // 1 pt = 1/12 pi 0041 // 1 pt ^= 0.0077880997 cc 0042 // 1 cc = 12 dd 0043 // Note: I don't use division but multiplication with the inverse value 0044 // because it's faster ;p (Werner) 0045 #define POINT_TO_MM(px) qreal((px)*0.352777167) 0046 #define MM_TO_POINT(mm) qreal((mm)*2.83465058) 0047 #define POINT_TO_CM(px) qreal((px)*0.0352777167) 0048 #define CM_TO_POINT(cm) qreal((cm)*28.3465058) 0049 #define POINT_TO_DM(px) qreal((px)*0.00352777167) 0050 #define DM_TO_POINT(dm) qreal((dm)*283.465058) 0051 #define POINT_TO_INCH(px) qreal((px)*0.01388888888889) 0052 #define INCH_TO_POINT(inch) qreal((inch)*72.0) 0053 #define MM_TO_INCH(mm) qreal((mm)*0.039370147) 0054 #define INCH_TO_MM(inch) qreal((inch)*25.399956) 0055 #define POINT_TO_PI(px) qreal((px)*0.083333333) 0056 #define POINT_TO_CC(px) qreal((px)*0.077880997) 0057 #define PI_TO_POINT(pi) qreal((pi)*12) 0058 #define CC_TO_POINT(cc) qreal((cc)*12.840103) 0059 /** 0060 * %Calligra stores everything in pt (using "qreal") internally. 0061 * When displaying a value to the user, the value is converted to the user's unit 0062 * of choice, and rounded to a reasonable precision to avoid 0.999999 0063 * 0064 * For implementing the selection of a unit type in the UI use the *ForUi() methods. 0065 * They ensure the same order of the unit types in all places, with the order not 0066 * bound to the order in the enum (so ABI-compatible extension is possible) and 0067 * with the order and scope of listed types controlled by the @c ListOptions parameter. 0068 */ 0069 class KOODF_EXPORT KoUnit 0070 { 0071 public: 0072 /** Length units supported by Calligra. */ 0073 enum Type { 0074 Millimeter = 0, 0075 Point, ///< Postscript point, 1/72th of an Inco 0076 Inch, 0077 Centimeter, 0078 Decimeter, 0079 Pica, 0080 Cicero, 0081 Pixel, 0082 TypeCount ///< @internal 0083 }; 0084 0085 /// Used to control the scope of the unit types listed in the UI 0086 enum ListOption { 0087 ListAll = 0, 0088 HidePixel = 1, 0089 HideMask = HidePixel 0090 }; 0091 Q_DECLARE_FLAGS(ListOptions, ListOption) 0092 0093 /** Returns a KoUnit instance with the type at the @p index of the UI list with the given @p listOptions. */ 0094 static KoUnit fromListForUi(int index, ListOptions listOptions = ListAll, qreal factor = 1.0); 0095 0096 /// Convert a unit symbol string into a KoUnit 0097 /// @param symbol symbol to convert 0098 /// @param ok if set, it will be true if the unit was known, false if unknown 0099 static KoUnit fromSymbol(const QString &symbol, bool *ok = 0); 0100 0101 /** Construction requires initialization. The factor is for variable factor units like pixel */ 0102 explicit KoUnit(Type unit = Point, qreal factor = 1.0) { 0103 m_type = unit; 0104 m_pixelConversion = factor; 0105 } 0106 0107 KoUnit& operator=(Type unit) { 0108 m_type = unit; m_pixelConversion = 1.0; return *this; 0109 } 0110 0111 bool operator!=(const KoUnit &other) const { 0112 return !operator==(other); 0113 } 0114 0115 bool operator==(const KoUnit &other) const { 0116 return m_type == other.m_type && 0117 (m_type != Pixel || 0118 qFuzzyCompare(m_pixelConversion, other.m_pixelConversion)); 0119 } 0120 0121 KoUnit::Type type() const { 0122 return m_type; 0123 } 0124 0125 void setFactor(qreal factor) { 0126 m_pixelConversion = factor; 0127 } 0128 /** 0129 * Prepare ptValue to be displayed in pt 0130 * This method will round to 0.001 precision 0131 */ 0132 static qreal toPoint(qreal ptValue) { 0133 // No conversion, only rounding (to 0.001 precision) 0134 return floor(ptValue * 1000.0) / 1000.0; 0135 } 0136 0137 /** 0138 * Prepare ptValue to be displayed in mm 0139 * This method will round to 0.0001 precision, use POINT_TO_MM() for lossless conversion. 0140 */ 0141 static qreal toMillimeter(qreal ptValue) { 0142 // "mm" values are rounded to 0.0001 millimeters 0143 return floor(POINT_TO_MM(ptValue) * 10000.0) / 10000.0; 0144 } 0145 0146 /** 0147 * Prepare ptValue to be displayed in cm 0148 * This method will round to 0.0001 precision, use POINT_TO_CM() for lossless conversion. 0149 */ 0150 static qreal toCentimeter(qreal ptValue) { 0151 return floor(POINT_TO_CM(ptValue) * 10000.0) / 10000.0; 0152 } 0153 0154 /** 0155 * Prepare ptValue to be displayed in dm 0156 * This method will round to 0.0001 precision, use POINT_TO_DM() for lossless conversion. 0157 */ 0158 static qreal toDecimeter(qreal ptValue) { 0159 return floor(POINT_TO_DM(ptValue) * 10000.0) / 10000.0; 0160 } 0161 0162 /** 0163 * Prepare ptValue to be displayed in inch 0164 * This method will round to 0.00001 precision, use POINT_TO_INCH() for lossless conversion. 0165 */ 0166 static qreal toInch(qreal ptValue) { 0167 // "in" values are rounded to 0.00001 inches 0168 return floor(POINT_TO_INCH(ptValue) * 100000.0) / 100000.0; 0169 } 0170 0171 /** 0172 * Prepare ptValue to be displayed in pica 0173 * This method will round to 0.00001 precision, use POINT_TO_PI() for lossless conversion. 0174 */ 0175 static qreal toPica(qreal ptValue) { 0176 // "pi" values are rounded to 0.00001 inches 0177 return floor(POINT_TO_PI(ptValue) * 100000.0) / 100000.0; 0178 } 0179 0180 /** 0181 * Prepare ptValue to be displayed in cicero 0182 * This method will round to 0.00001 precision, use POINT_TO_CC() for lossless conversion. 0183 */ 0184 static qreal toCicero(qreal ptValue) { 0185 // "cc" values are rounded to 0.00001 inches 0186 return floor(POINT_TO_CC(ptValue) * 100000.0) / 100000.0; 0187 } 0188 0189 /** 0190 * convert the given value directly from one unit to another 0191 */ 0192 static qreal convertFromUnitToUnit(const qreal value, const KoUnit &fromUnit, const KoUnit &toUnit, qreal factor = 1.0); 0193 0194 0195 /** 0196 * This method is the one to use to display a value in a dialog 0197 * \return the value @p ptValue converted to unit and rounded, ready to be displayed 0198 */ 0199 qreal toUserValue(qreal ptValue) const; 0200 0201 /** 0202 * Convert the value @p ptValue to a given unit @p unit 0203 * Unlike KoUnit::ptToUnit the return value remains unrounded, so that it can be used in complex calculation 0204 * \return the converted value 0205 */ 0206 static qreal ptToUnit(const qreal ptValue, const KoUnit &unit); 0207 0208 /// This method is the one to use to display a value in a dialog 0209 /// @return the value @p ptValue converted the unit and rounded, ready to be displayed 0210 QString toUserStringValue(qreal ptValue) const; 0211 0212 /// This method is the one to use to read a value from a dialog 0213 /// @return the value converted to points for internal use 0214 qreal fromUserValue(qreal value) const; 0215 0216 /// This method is the one to use to read a value from a dialog 0217 /// @param value value entered by the user 0218 /// @param ok if set, the pointed bool is set to true if the value could be 0219 /// converted to a qreal, and to false otherwise. 0220 /// @return the value converted to points for internal use 0221 qreal fromUserValue(const QString &value, bool *ok = 0) const; 0222 0223 /// Get the description string of the given unit 0224 static QString unitDescription(KoUnit::Type type); 0225 0226 /// Get the symbol string of the unit 0227 QString symbol() const; 0228 0229 /// Returns the list of unit types for the UI, controlled with the given @p listOptions. 0230 static QStringList listOfUnitNameForUi(ListOptions listOptions = ListAll); 0231 0232 /// Get the index of this unit in the list of unit types for the UI, 0233 /// if it is controlled with the given @p listOptions. 0234 int indexInListForUi(ListOptions listOptions = ListAll) const; 0235 0236 /// parse common %Calligra and Odf values, like "10cm", "5mm" to pt 0237 static qreal parseValue(const QString &value, qreal defaultVal = 0.0); 0238 0239 /// parse an angle to its value in degrees 0240 static qreal parseAngle(const QString &value, qreal defaultVal = 0.0); 0241 0242 QString toString() const { 0243 return symbol(); 0244 } 0245 0246 /** 0247 * Get an approximate scale of a unit vector that was converted by 0248 * the transformation. 0249 * 0250 * Please note that exact values are guaranteed only for 0251 * combinations of Translate, Rotation and Uniform Scale 0252 * matrices. For combinations having shears and perspective the 0253 * value will be average for the point near CS origin. 0254 */ 0255 static qreal approxTransformScale(const QTransform &t); 0256 0257 /** 0258 * Adjust the unit by pixel transformation applied to the 0259 * describing object. It multiplies the pixel coefficient by the 0260 * average scale of the matrix. 0261 */ 0262 void adjustByPixelTransform(const QTransform &t); 0263 0264 private: 0265 Type m_type; 0266 qreal m_pixelConversion; 0267 }; 0268 0269 #ifndef QT_NO_DEBUG_STREAM 0270 KOODF_EXPORT QDebug operator<<(QDebug, const KoUnit &); 0271 #endif 0272 0273 Q_DECLARE_METATYPE(KoUnit) 0274 Q_DECLARE_OPERATORS_FOR_FLAGS(KoUnit::ListOptions) 0275 0276 #endif