kviewshell
units.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include <config.h>
00011
00012 #include <kdebug.h>
00013 #include <kglobal.h>
00014 #include <klocale.h>
00015 #include <math.h>
00016 #include <qstringlist.h>
00017
00018 #include "units.h"
00019
00020 class unitOfDistance
00021 {
00022 public:
00023 float mmPerUnit;
00024 const char* name;
00025 };
00026
00027 unitOfDistance distanceUnitTable[] = {
00028
00029 {1.0, "mm"},
00030 {1.0, "millimeter"},
00031 {10.0, "cm"},
00032 {10.0, "centimeter"},
00033 {100.0*10.0, "m"},
00034 {100.0*10.0, "meter"},
00035
00036
00037 {25.4, "in"},
00038 {25.4, "inch"},
00039
00040
00041 {2540.0/7227.0, "pt"},
00042 {25.4/72.0, "bp"},
00043 {25.4/6.0, "pc"},
00044 {25.4/6.0, "pica"},
00045 {25.4*0.0148, "dd"},
00046 {25.4*0.0148, "didot"},
00047 {25.4*0.178, "cc"},
00048 {25.4*0.178, "cicero"},
00049
00050 {0.0, 0},
00051 };
00052
00053
00054
00055
00056 float distance::convertToMM(const QString &distance, bool *ok)
00057 {
00058
00059
00060 float MMperUnit = 0.0;
00061 int unitPos = 0;
00062
00063
00064
00065
00066 for(int i=0; MMperUnit==0.0 && distanceUnitTable[i].name != 0; i++) {
00067 unitPos = distance.findRev(distanceUnitTable[i].name);
00068 if (unitPos != -1)
00069 MMperUnit = distanceUnitTable[i].mmPerUnit;
00070 }
00071
00072
00073
00074 if (MMperUnit == 0.0) {
00075 kdError() << "distance::convertToMM: no known unit found in the string '" << distance << "'." << endl;
00076 if (ok)
00077 *ok = false;
00078 return 0.0;
00079 }
00080
00081 QString val = distance.left(unitPos).simplifyWhiteSpace();
00082 return MMperUnit*val.toFloat(ok);
00083 }