• Skip to content
  • Skip to link menu
KDE 3.5 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

kviewshell

units.cpp

Go to the documentation of this file.
00001 // units.cpp
00002 //
00003 // Part of KVIEWSHELL - A framework for multipage text/gfx viewers
00004 //
00005 // (C) 2003 Stefan Kebekus
00006 // Distributed under the GPL
00007 
00008 // Add header files alphabetically
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   // Metric units
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   // Imperial units
00037   {25.4, "in"},
00038   {25.4, "inch"},
00039 
00040   // Typographical units
00041   {2540.0/7227.0, "pt"},  // TeX points. 7227points = 254cm
00042   {25.4/72.0, "bp"},      // big points, 1/72 inch as used in Postscript
00043   {25.4/6.0, "pc"},       // pica = 1/6 inch
00044   {25.4/6.0, "pica"},
00045   {25.4*0.0148, "dd"},    // didot points = 0.0148 inches
00046   {25.4*0.0148, "didot"},
00047   {25.4*0.178, "cc"},     // cicero points = 0.178 inches
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   //  kdDebug() << "convertToMM( " << distance << " )" << endl;
00059 
00060   float MMperUnit = 0.0;
00061   int unitPos = 0; // position of the unit in the string
00062 
00063   // Check for various known units, and store the beginning position
00064   // of the unit in 'unitPos', so that distance[0..unitPos] will hold
00065   // the value. Store the number of mm per unit in 'MMperUnit'.
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   // If no unit has been found -> error message, set *ok to false and
00073   // return 0.0.
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 }

kviewshell

Skip menu "kviewshell"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • kviewshell
Generated for API Reference by doxygen 1.5.9
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal