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

UNNAMED_READER/corelibrary

length.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2003-2006 by Stefan Kebekus <kebekus@kde.org>           *
00003  *   Copyright (C) 2006 by Wilfried Huss                                   *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
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   // Metric units
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   // Imperial units
00053   {25.4f, "in"},
00054   {25.4f, "inch"},
00055 
00056   // Typographical units
00057   {float(2540.0/7227.0), "pt"},  // TeX points. 7227points = 254cm
00058   {float(25.4/72.0), "bp"},      // big points, 1/72 inch as used in Postscript
00059   {float(25.4/6.0), "pc"},       // pica = 1/6 inch
00060   {float(25.4/6.0), "pica"},
00061   {float(25.4*0.0148), "dd"},    // didot points = 0.0148 inches
00062   {float(25.4*0.0148), "didot"},
00063   {float(25.4*0.178), "cc"},     // cicero points = 0.178 inches
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; // position of the unit in the string
00074 
00075   // Check for various known units, and store the beginning position
00076   // of the unit in 'unitPos', so that distance[0..unitPos] will hold
00077   // the value. Store the number of mm per unit in 'MMperUnit'.
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   // If no unit has been found -> error message, set *ok to false and
00085   // return 0.0.
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 } // namespace UNNAMED_READER
00098 

UNNAMED_READER/corelibrary

Skip menu "UNNAMED_READER/corelibrary"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  •   corelibrary
Generated for API Reference by doxygen 1.5.4
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