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