Qt Virtual Chart Table (QVCT)
CUnitSpeed.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/CUnitSpeed.hpp"
27 
28 
29 //------------------------------------------------------------------------------
30 // CONSTANTS / STATIC
31 //------------------------------------------------------------------------------
32 
35 
36 const QMap<CUnitSpeed::EUnit,QString> &CUnitSpeed::symbols()
37 {
39 }
40 
42 {
43  return oUnitSpeedSymbols.qMapSymbols.value( _eUnit, "?" );
44 }
45 
46 CUnitSpeed::EUnit CUnitSpeed::fromSymbol( const QString& _rqString )
47 {
48  return oUnitSpeedSymbols.qMapSymbols.key( _rqString, UNDEFINED );
49 }
50 
51 const QMap<CUnitSpeed::EUnit,QString> &CUnitSpeed::codes()
52 {
54 }
55 
57 {
58  return oUnitSpeedCodes.qMapCodes.value( _eUnit, "undef" );
59 }
60 
61 CUnitSpeed::EUnit CUnitSpeed::fromCode( const QString& _rqString )
62 {
63  return oUnitSpeedCodes.qMapCodes.key( _rqString, UNDEFINED );
64 }
65 
66 double CUnitSpeed::toValue( double _fdValue, CUnitSpeed::EUnit _eUnit )
67 {
68  switch( _eUnit )
69  {
70  case M_S: return _fdValue;
71  case KM_H: return _fdValue * 3.6;
72  case MI_H: return _fdValue * 2.23693629205;
73  case KT: return _fdValue * 1.94384449244;
74  default: return 0;
75  }
76 }
77 
78 QString CUnitSpeed::toString( double _fdValue, CUnitSpeed::EUnit _eUnit, int _iPrecision )
79 {
80  switch( _eUnit )
81  {
82  case M_S: return QString::number( _fdValue, 'f', _iPrecision )+oUnitSpeedSymbols.qMapSymbols.value( _eUnit );
83  case KM_H: return QString::number( _fdValue * 3.6, 'f', _iPrecision )+oUnitSpeedSymbols.qMapSymbols.value( _eUnit );
84  case MI_H: return QString::number( _fdValue * 2.23693629205, 'f', _iPrecision )+oUnitSpeedSymbols.qMapSymbols.value( _eUnit );
85  case KT: return QString::number( _fdValue * 1.94384449244, 'f', _iPrecision )+oUnitSpeedSymbols.qMapSymbols.value( _eUnit );
86  default: return "?";
87  }
88 }
89 
90 QString CUnitSpeed::toString( double _fdValue )
91 {
92  CSettings* __poSettings = QVCTRuntime::useSettings();
93  return toString( _fdValue, __poSettings->getUnitSpeed(), __poSettings->getPrecisionSpeed() );
94 }
95 
96 double CUnitSpeed::fromString( const QString& _rqString, EUnit _eUnit, bool* _pbOK )
97 {
98  static const QRegExp __qRegExp( "^\\s*(-?\\d+(\\.\\d+)?)\\s*([^\\d\\s]*)?\\s*$" );
99 
100  if( _pbOK ) *_pbOK = false;
101  if( !__qRegExp.exactMatch( _rqString ) ) return 0.0;
102  bool __bOK;
103  double __fdValue = __qRegExp.cap(1).toDouble( &__bOK );
104  if( !__bOK ) return 0.0;
105  QString __qsUnit = __qRegExp.cap(3);
106  switch( __qsUnit.isEmpty() ? _eUnit : oUnitSpeedSymbols.qMapSymbols.key( __qsUnit, UNDEFINED ) )
107  {
108  case M_S: break;
109  case KM_H: __fdValue = __fdValue / 3.6; break;
110  case MI_H: __fdValue = __fdValue / 2.23693629205; break;
111  case KT: __fdValue = __fdValue / 1.94384449244; break;
112  default: return 0.0;
113  }
114  if( _pbOK ) *_pbOK = true;
115  return __fdValue;
116 }
117 
118 double CUnitSpeed::fromString( const QString& _rqString, bool* _pbOK )
119 {
120  CSettings* __poSettings = QVCTRuntime::useSettings();
121  return fromString( _rqString, __poSettings->getUnitSpeed(), _pbOK );
122 }
[UI] Container for the application's settings
Definition: CSettings.hpp:51
int getPrecisionSpeed()
[Precision] Returns the speed decimal precision
Definition: CSettings.hpp:370
CUnitSpeed::EUnit getUnitSpeed()
[Unit] Returns the speed format/unit
Definition: CSettings.hpp:353
Container class for supported machine-friendly format/unit codes.
Definition: CUnitSpeed.hpp:124
QMap< CUnitSpeed::EUnit, QString > qMapCodes
Definition: CUnitSpeed.hpp:138
Container class for supported human-readable format/unit symbols.
Definition: CUnitSpeed.hpp:104
QMap< CUnitSpeed::EUnit, QString > qMapSymbols
Definition: CUnitSpeed.hpp:118
static const QMap< EUnit, QString > & symbols()
Returns the list of supported human-readable format/unit symbols.
Definition: CUnitSpeed.cpp:36
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...
Definition: CUnitSpeed.cpp:78
static double fromString(const QString &_rqString, EUnit _eUnit, bool *_pbOK=0)
Returns the numeric value corresponding (parsed) from the string.
Definition: CUnitSpeed.cpp:96
static QString toCode(EUnit _eUnit)
Returns the machine-friendly code corresponding to the given format/unit ID.
Definition: CUnitSpeed.cpp:56
static double toValue(double _fdValue, EUnit _eUnit)
Returns the converted value, using the specified format/unit.
Definition: CUnitSpeed.cpp:66
static QString toSymbol(EUnit _eUnit)
Returns the human-readable symbol corresponding to the given format/unit ID.
Definition: CUnitSpeed.cpp:41
static const CUnitSpeedCodes oUnitSpeedCodes
Container for supported machine-friendly format/unit codes.
Definition: CUnitSpeed.hpp:65
static EUnit fromSymbol(const QString &_rqsSymbol)
Returns the format/unit ID corresponding to the given human-readable symbol.
Definition: CUnitSpeed.cpp:46
static EUnit fromCode(const QString &_rqsCode)
Returns the format/unit ID corresponding to the given machine-friendly code.
Definition: CUnitSpeed.cpp:61
static const CUnitSpeedSymbols oUnitSpeedSymbols
Container for supported human-readable format/unit symbols.
Definition: CUnitSpeed.hpp:63
EUnit
Format/unit ID.
Definition: CUnitSpeed.hpp:53
@ UNDEFINED
undefined format/unit
Definition: CUnitSpeed.hpp:58
@ KM_H
kilometers per hour [km/h]
Definition: CUnitSpeed.hpp:55
@ MI_H
statute miles per hour [mi/h]
Definition: CUnitSpeed.hpp:56
@ KT
nautical miles per hour (knots) [kt]
Definition: CUnitSpeed.hpp:57
@ M_S
meters per second [m/s]
Definition: CUnitSpeed.hpp:54
static const QMap< EUnit, QString > & codes()
Returns the list of supported machine-friendly format/unit codes.
Definition: CUnitSpeed.cpp:51
static CSettings * useSettings()