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 //#include <KoGlobal.h> 0029 #include "KgvUnit.h" 0030 #include "kgraphviewerlib_debug.h" 0031 0032 #include <QDebug> 0033 #include <QLocale> 0034 #include <klocalizedstring.h> 0035 #include <qregularexpression.h> 0036 0037 QStringList KgvUnit::listOfUnitName() 0038 { 0039 QStringList lst; 0040 for (uint i = 0; i <= KgvUnit::U_LASTUNIT; ++i) { 0041 KgvUnit::Unit unit = static_cast<KgvUnit::Unit>(i); 0042 lst.append(KgvUnit::unitDescription(unit)); 0043 } 0044 return lst; 0045 } 0046 0047 QString KgvUnit::unitDescription(Unit _unit) 0048 { 0049 switch (_unit) { 0050 case KgvUnit::U_MM: 0051 return i18n("Millimeters (mm)"); 0052 case KgvUnit::U_CM: 0053 return i18n("Centimeters (cm)"); 0054 case KgvUnit::U_DM: 0055 return i18n("Decimeters (dm)"); 0056 case KgvUnit::U_INCH: 0057 return i18n("Inches (in)"); 0058 case KgvUnit::U_PI: 0059 return i18n("Pica (pi)"); 0060 case KgvUnit::U_DD: 0061 return i18n("Didot (dd)"); 0062 case KgvUnit::U_CC: 0063 return i18n("Cicero (cc)"); 0064 case KgvUnit::U_PT: 0065 return i18n("Points (pt)"); 0066 default: 0067 return i18n("Error."); 0068 } 0069 } 0070 0071 double KgvUnit::toUserValue(double ptValue, Unit unit) 0072 { 0073 switch (unit) { 0074 case U_MM: 0075 return toMM(ptValue); 0076 case U_CM: 0077 return toCM(ptValue); 0078 case U_DM: 0079 return toDM(ptValue); 0080 case U_INCH: 0081 return toInch(ptValue); 0082 case U_PI: 0083 return toPI(ptValue); 0084 case U_DD: 0085 return toDD(ptValue); 0086 case U_CC: 0087 return toCC(ptValue); 0088 case U_PT: 0089 default: 0090 return toPoint(ptValue); 0091 } 0092 } 0093 0094 double KgvUnit::ptToUnit(const double ptValue, const Unit unit) 0095 { 0096 switch (unit) { 0097 case U_MM: 0098 return POINT_TO_MM(ptValue); 0099 case U_CM: 0100 return POINT_TO_CM(ptValue); 0101 case U_DM: 0102 return POINT_TO_DM(ptValue); 0103 case U_INCH: 0104 return POINT_TO_INCH(ptValue); 0105 case U_PI: 0106 return POINT_TO_PI(ptValue); 0107 case U_DD: 0108 return POINT_TO_DD(ptValue); 0109 case U_CC: 0110 return POINT_TO_CC(ptValue); 0111 case U_PT: 0112 default: 0113 return ptValue; 0114 } 0115 } 0116 0117 QString KgvUnit::toUserStringValue(double ptValue, Unit unit) 0118 { 0119 return QLocale().toString(toUserValue(ptValue, unit)); 0120 } 0121 0122 double KgvUnit::fromUserValue(double value, Unit unit) 0123 { 0124 switch (unit) { 0125 case U_MM: 0126 return MM_TO_POINT(value); 0127 case U_CM: 0128 return CM_TO_POINT(value); 0129 case U_DM: 0130 return DM_TO_POINT(value); 0131 case U_INCH: 0132 return INCH_TO_POINT(value); 0133 case U_PI: 0134 return PI_TO_POINT(value); 0135 case U_DD: 0136 return DD_TO_POINT(value); 0137 case U_CC: 0138 return CC_TO_POINT(value); 0139 case U_PT: 0140 default: 0141 return value; 0142 } 0143 } 0144 0145 double KgvUnit::fromUserValue(const QString &value, Unit unit, bool *ok) 0146 { 0147 return fromUserValue(QLocale().toDouble(value, ok), unit); 0148 } 0149 0150 double KgvUnit::parseValue(const QString &sval, double defaultVal) 0151 { 0152 QString value = sval; 0153 value = value.simplified(); 0154 value.remove(' '); 0155 0156 if (value.isEmpty()) 0157 return defaultVal; 0158 0159 int index = value.indexOf(QRegularExpression("[a-z]+$")); 0160 if (index == -1) 0161 return value.toDouble(); 0162 0163 QString unit = value.mid(index); 0164 value.truncate(index); 0165 double val = value.toDouble(); 0166 0167 if (unit == "pt") 0168 return val; 0169 0170 bool ok; 0171 Unit u = KgvUnit::unit(unit, &ok); 0172 if (ok) 0173 return fromUserValue(val, u); 0174 0175 if (unit == "m") 0176 return fromUserValue(val * 10.0, U_DM); 0177 else if (unit == "km") 0178 return fromUserValue(val * 10000.0, U_DM); 0179 qCWarning(KGRAPHVIEWERLIB_LOG) << "Unit" << unit << "is not supported, please report."; 0180 0181 // TODO : add support for mi/ft ? 0182 return defaultVal; 0183 } 0184 0185 KgvUnit::Unit KgvUnit::unit(const QString &_unitName, bool *ok) 0186 { 0187 if (ok) 0188 *ok = true; 0189 if (_unitName == QString::fromLatin1("mm")) 0190 return U_MM; 0191 if (_unitName == QString::fromLatin1("cm")) 0192 return U_CM; 0193 if (_unitName == QString::fromLatin1("dm")) 0194 return U_DM; 0195 if (_unitName == QString::fromLatin1("in") || _unitName == QString::fromLatin1("inch") /*compat*/) 0196 return U_INCH; 0197 if (_unitName == QString::fromLatin1("pi")) 0198 return U_PI; 0199 if (_unitName == QString::fromLatin1("dd")) 0200 return U_DD; 0201 if (_unitName == QString::fromLatin1("cc")) 0202 return U_CC; 0203 if (_unitName == QString::fromLatin1("pt")) 0204 return U_PT; 0205 if (ok) 0206 *ok = false; 0207 return U_PT; 0208 } 0209 0210 QString KgvUnit::unitName(Unit _unit) 0211 { 0212 if (_unit == U_MM) 0213 return QString::fromLatin1("mm"); 0214 if (_unit == U_CM) 0215 return QString::fromLatin1("cm"); 0216 if (_unit == U_DM) 0217 return QString::fromLatin1("dm"); 0218 if (_unit == U_INCH) 0219 return QString::fromLatin1("in"); 0220 if (_unit == U_PI) 0221 return QString::fromLatin1("pi"); 0222 if (_unit == U_DD) 0223 return QString::fromLatin1("dd"); 0224 if (_unit == U_CC) 0225 return QString::fromLatin1("cc"); 0226 return QString::fromLatin1("pt"); 0227 }