Qt Virtual Chart Table (QVCT)
CUnitElevation.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/CUnitElevation.hpp"
27 
28 
29 //------------------------------------------------------------------------------
30 // CONSTANTS / STATIC
31 //------------------------------------------------------------------------------
32 
34 
35 const QMap<CUnitElevation::EUnit,QString> &CUnitElevation::symbols()
36 {
38 }
39 
41 {
42  return oUnitElevationSymbols.qMapSymbols.value( _eUnit, "?" );
43 }
44 
46 {
47  return oUnitElevationSymbols.qMapSymbols.key( _rqString, UNDEFINED );
48 }
49 
50 const QMap<CUnitElevation::EUnit,QString> &CUnitElevation::codes()
51 {
53 }
54 
56 {
57  return oUnitElevationSymbols.qMapSymbols.value( _eUnit, "undef" );
58 }
59 
61 {
62  return oUnitElevationSymbols.qMapSymbols.key( _rqString, UNDEFINED );
63 }
64 
65 double CUnitElevation::toValue( double _fdValue, CUnitElevation::EUnit _eUnit )
66 {
67  switch( _eUnit )
68  {
69  case M: return _fdValue;
70  case FT: return _fdValue / 0.3048;
71  default: return 0;
72  }
73 }
74 
75 QString CUnitElevation::toString( double _fdValue, CUnitElevation::EUnit _eUnit, int _iPrecision )
76 {
77  switch( _eUnit )
78  {
79  case M: return QString::number( _fdValue, 'f', _iPrecision )+oUnitElevationSymbols.qMapSymbols.value( _eUnit );
80  case FT: return QString::number( _fdValue / 0.3048, 'f', _iPrecision )+oUnitElevationSymbols.qMapSymbols.value( _eUnit );
81  default: return "?";
82  }
83 }
84 
85 QString CUnitElevation::toString( double _fdValue )
86 {
87  CSettings* __poSettings = QVCTRuntime::useSettings();
88  return toString( _fdValue, __poSettings->getUnitElevation(), __poSettings->getPrecisionElevation() );
89 }
90 
91 double CUnitElevation::fromString( const QString& _rqString, EUnit _eUnit, bool* _pbOK )
92 {
93  static const QRegExp __qRegExp( "^\\s*(-?\\d+(\\.\\d+)?)\\s*([^\\d\\s]*)?\\s*$" );
94 
95  if( _pbOK ) *_pbOK = false;
96  if( !__qRegExp.exactMatch( _rqString ) ) return 0.0;
97  bool __bOK;
98  double __fdValue = __qRegExp.cap(1).toDouble( &__bOK );
99  if( !__bOK ) return 0.0;
100  QString __qsUnit = __qRegExp.cap(3);
101  switch( __qsUnit.isEmpty() ? _eUnit : oUnitElevationSymbols.qMapSymbols.key( __qsUnit, UNDEFINED ) )
102  {
103  case M: break;
104  case FT: __fdValue = __fdValue * 0.3048; break;
105  default: return 0.0;
106  }
107  if( _pbOK ) *_pbOK = true;
108  return __fdValue;
109 }
110 
111 double CUnitElevation::fromString( const QString& _rqString, bool* _pbOK )
112 {
113  CSettings* __poSettings = QVCTRuntime::useSettings();
114  return fromString( _rqString, __poSettings->getUnitElevation(), _pbOK );
115 }
[UI] Container for the application's settings
Definition: CSettings.hpp:51
CUnitElevation::EUnit getUnitElevation()
[Unit] Returns the elevation format/unit
Definition: CSettings.hpp:355
int getPrecisionElevation()
[Precision] Returns the elevation decimal precision
Definition: CSettings.hpp:372
Container class for supported human-readable format/unit symbols.
QMap< CUnitElevation::EUnit, QString > qMapSymbols
static double toValue(double _fdValue, EUnit _eUnit)
Returns the converted value, using the specified format/unit.
static const QMap< EUnit, QString > & codes()
Returns the list of supported machine-friendly format/unit codes.
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 QString toCode(EUnit _eUnit)
Returns the machine-friendly code corresponding to the given format/unit ID.
EUnit
Format/unit ID.
@ UNDEFINED
undefined format/unit
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 double fromString(const QString &_rqString, EUnit _eUnit=UNDEFINED, bool *_pbOK=0)
Returns the numeric value corresponding (parsed) from the string.
static EUnit fromCode(const QString &_rqsCode)
Returns the format/unit ID corresponding to the given machine-friendly code.
static const CUnitElevationSymbols oUnitElevationSymbols
Container for supported human-readable format/unit symbols.
static CSettings * useSettings()