Qt Virtual Chart Table (QVCT)
CUnitDistance.cpp
Go to the documentation of this file.
1 // INDENTING (emacs/vi): -*- mode:c++; tab-width:2; c-basic-offset:2; intent-tabs-mode:nil; -*- ex: set tabstop=2 expandtab:
2 
3 /*
4  * Qt Virtual Chart Table (QVCT)
5  * Copyright (C) 2012 Cedric Dufour <http://cedric.dufour.name>
6  * Author: Cedric Dufour <http://cedric.dufour.name>
7  *
8  * The Qt Virtual Chart Table (QVCT) is free software:
9  * you can redistribute it and/or modify it under the terms of the GNU General
10  * Public License as published by the Free Software Foundation, Version 3.
11  *
12  * The Qt Virtual Chart Table (QVCT) is distributed in the hope
13  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
14  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * See the GNU General Public License for more details.
17  */
18 
19 // QT
20 #include <QMap>
21 #include <QRegExp>
22 #include <QString>
23 
24 // QVCT
25 #include "QVCTRuntime.hpp"
26 #include "units/CUnitDistance.hpp"
27 
28 
29 //------------------------------------------------------------------------------
30 // CONSTANTS / STATIC
31 //------------------------------------------------------------------------------
32 
34 
35 const QMap<CUnitDistance::EUnit,QString> &CUnitDistance::symbols()
36 {
38 }
39 
41 {
42  return oUnitDistanceSymbols.qMapSymbols.value( _eUnit, "?" );
43 }
44 
46 {
47  return oUnitDistanceSymbols.qMapSymbols.key( _rqString, UNDEFINED );
48 }
49 
50 const QMap<CUnitDistance::EUnit,QString> &CUnitDistance::codes()
51 {
53 }
54 
56 {
57  return oUnitDistanceSymbols.qMapSymbols.value( _eUnit, "undef" );
58 }
59 
60 CUnitDistance::EUnit CUnitDistance::fromCode( const QString& _rqString )
61 {
62  return oUnitDistanceSymbols.qMapSymbols.key( _rqString, UNDEFINED );
63 }
64 
65 double CUnitDistance::toValue( double _fdValue, CUnitDistance::EUnit _eUnit )
66 {
67  switch( _eUnit )
68  {
69  case M: return _fdValue;
70  case KM: return _fdValue / 1000.0;
71  case FT: return _fdValue / 0.3048;
72  case MI: return _fdValue / 1609.344;
73  case NM: return _fdValue / 1852.0;
74  default: return 0;
75  }
76 }
77 
78 QString CUnitDistance::toString( double _fdValue, CUnitDistance::EUnit _eUnit, int _iPrecision )
79 {
80  switch( _eUnit )
81  {
82  case M: return QString::number( _fdValue, 'f', _iPrecision )+oUnitDistanceSymbols.qMapSymbols.value( _eUnit );
83  case KM: return QString::number( _fdValue / 1000.0, 'f', _iPrecision )+oUnitDistanceSymbols.qMapSymbols.value( _eUnit );
84  case FT: return QString::number( _fdValue / 0.3048, 'f', _iPrecision )+oUnitDistanceSymbols.qMapSymbols.value( _eUnit );
85  case MI: return QString::number( _fdValue / 1609.344, 'f', _iPrecision )+oUnitDistanceSymbols.qMapSymbols.value( _eUnit );
86  case NM: return QString::number( _fdValue / 1852.0, 'f', _iPrecision )+oUnitDistanceSymbols.qMapSymbols.value( _eUnit );
87  default: return "?";
88  }
89 }
90 
91 QString CUnitDistance::toString( double _fdValue )
92 {
93  CSettings* __poSettings = QVCTRuntime::useSettings();
94  return toString( _fdValue, __poSettings->getUnitDistance(), __poSettings->getPrecisionDistance() );
95 }
96 
97 double CUnitDistance::fromString( const QString& _rqString, EUnit _eUnit, bool* _pbOK )
98 {
99  static const QRegExp __qRegExp( "^\\s*(-?\\d+(\\.\\d+)?)\\s*([^\\d\\s]*)?\\s*$" );
100 
101  if( _pbOK ) *_pbOK = false;
102  if( !__qRegExp.exactMatch( _rqString ) ) return 0.0;
103  bool __bOK;
104  double __fdValue = __qRegExp.cap(1).toDouble( &__bOK );
105  if( !__bOK ) return 0.0;
106  QString __qsUnit = __qRegExp.cap(3);
107  switch( __qsUnit.isEmpty() ? _eUnit : oUnitDistanceSymbols.qMapSymbols.key( __qsUnit, UNDEFINED ) )
108  {
109  case M: break;
110  case KM: __fdValue = __fdValue * 1000.0; break;
111  case FT: __fdValue = __fdValue * 0.3048; break;
112  case MI: __fdValue = __fdValue * 1609.344; break;
113  case NM: __fdValue = __fdValue * 1852.0; break;
114  default: return 0.0;
115  }
116  if( _pbOK ) *_pbOK = true;
117  return __fdValue;
118 }
119 
120 double CUnitDistance::fromString( const QString& _rqString, bool* _pbOK )
121 {
122  CSettings* __poSettings = QVCTRuntime::useSettings();
123  return fromString( _rqString, __poSettings->getUnitDistance(), _pbOK );
124 }
[UI] Container for the application's settings
Definition: CSettings.hpp:51
int getPrecisionDistance()
[Precision] Returns the distance decimal precision
Definition: CSettings.hpp:368
CUnitDistance::EUnit getUnitDistance()
[Unit] Returns the distance format/unit
Definition: CSettings.hpp:351
Container class for supported human-readable format/unit symbols.
QMap< CUnitDistance::EUnit, QString > qMapSymbols
static QString toCode(EUnit _eUnit)
Returns the machine-friendly code corresponding to the given format/unit ID.
static EUnit fromCode(const QString &_rqsCode)
Returns the format/unit ID corresponding to the given machine-friendly code.
EUnit
Format/unit ID.
@ MI
statute miles [mi]
@ KM
kilometers [km]
@ FT
feet [ft]
@ NM
nautical miles [nm]
@ UNDEFINED
undefined format/unit
@ M
meters [m]
static QString toSymbol(EUnit _eUnit)
Returns the human-readable symbol corresponding to the given format/unit ID.
static const QMap< EUnit, QString > & symbols()
Returns the list of supported human-readable format/unit symbols.
static EUnit fromSymbol(const QString &_rqsSymbol)
Returns the format/unit ID corresponding to the given human-readable symbol.
static const CUnitDistanceSymbols oUnitDistanceSymbols
Container for supported human-readable format/unit symbols.
static const QMap< EUnit, QString > & codes()
Returns the list of supported machine-friendly format/unit codes.
static double fromString(const QString &_rqString, EUnit _eUnit=UNDEFINED, bool *_pbOK=0)
Returns the numeric value corresponding (parsed) from the string.
static double toValue(double _fdValue, EUnit _eUnit)
Returns the converted value, using the specified format/unit.
static QString toString(double _fdValue, EUnit _eUnit, int _iPrecision=0)
Returns the formatted represention of the given value, using the specified format/unit and decimal pr...
static CSettings * useSettings()