Qt Virtual Chart Table (QVCT)
CUnitBearing.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/CUnitBearing.hpp"
27 
28 
29 //------------------------------------------------------------------------------
30 // CONSTANTS / STATIC
31 //------------------------------------------------------------------------------
32 
35 
36 const QMap<CUnitBearing::EUnit,QString> &CUnitBearing::symbols()
37 {
39 }
40 
42 {
43  return oUnitBearingSymbols.qMapSymbols.value( _eUnit, "?" );
44 }
45 
46 CUnitBearing::EUnit CUnitBearing::fromSymbol( const QString& _rqString )
47 {
48  return oUnitBearingSymbols.qMapSymbols.key( _rqString, UNDEFINED );
49 }
50 
51 const QMap<CUnitBearing::EUnit,QString> &CUnitBearing::codes()
52 {
54 }
55 
57 {
58  return oUnitBearingCodes.qMapCodes.value( _eUnit, "undef" );
59 }
60 
61 CUnitBearing::EUnit CUnitBearing::fromCode( const QString& _rqString )
62 {
63  return oUnitBearingCodes.qMapCodes.key( _rqString, UNDEFINED );
64 }
65 
66 double CUnitBearing::toValue( double _fdValue, CUnitBearing::EUnit _eUnit )
67 {
68  switch( _eUnit )
69  {
70  case DEG: return _fdValue;
71  case RAD: return _fdValue * QVCT::PI / 180.0;
72  case GRAD: return _fdValue / 0.9;
73  default: return 0;
74  }
75 }
76 
77 QString CUnitBearing::toString( double _fdValue, CUnitBearing::EUnit _eUnit, int _iPrecision )
78 {
79  switch( _eUnit )
80  {
81  case DEG: return QString::number( _fdValue, 'f', _iPrecision )+oUnitBearingSymbols.qMapSymbols.value( _eUnit );
82  case RAD: return QString::number( _fdValue * QVCT::PI / 180.0, 'f', _iPrecision )+oUnitBearingSymbols.qMapSymbols.value( _eUnit );
83  case GRAD: return QString::number( _fdValue / 0.9, 'f', _iPrecision )+oUnitBearingSymbols.qMapSymbols.value( _eUnit );
84  default: return "?";
85  }
86 }
87 
88 QString CUnitBearing::toString( double _fdValue )
89 {
90  CSettings* __poSettings = QVCTRuntime::useSettings();
91  return toString( _fdValue, __poSettings->getUnitBearing(), __poSettings->getPrecisionBearing() );
92 }
93 
94 double CUnitBearing::fromString( const QString& _rqString, EUnit _eUnit, bool* _pbOK )
95 {
96  static const QRegExp __qRegExp( "^\\s*(-?\\d+(\\.\\d+)?)\\s*([^\\d\\s]*)?\\s*$" );
97 
98  if( _pbOK ) *_pbOK = false;
99  if( !__qRegExp.exactMatch( _rqString ) ) return 0.0;
100  bool __bOK;
101  double __fdValue = __qRegExp.cap(1).toDouble( &__bOK );
102  if( !__bOK ) return 0.0;
103  QString __qsUnit = __qRegExp.cap(3);
104  switch( __qsUnit.isEmpty() ? _eUnit : oUnitBearingSymbols.qMapSymbols.key( __qsUnit, UNDEFINED ) )
105  {
106  case DEG: break;
107  case RAD: __fdValue = __fdValue * 180.0 / QVCT::PI; break;
108  case GRAD: __fdValue = __fdValue * 0.9; break;
109  default: return 0.0;
110  }
111  if( _pbOK ) *_pbOK = true;
112  return __fdValue;
113 }
114 
115 double CUnitBearing::fromString( const QString& _rqString, bool* _pbOK )
116 {
117  CSettings* __poSettings = QVCTRuntime::useSettings();
118  return fromString( _rqString, __poSettings->getUnitBearing(), _pbOK );
119 }
[UI] Container for the application's settings
Definition: CSettings.hpp:51
CUnitBearing::EUnit getUnitBearing()
[Unit] Returns the bearing format/unit
Definition: CSettings.hpp:349
int getPrecisionBearing()
[Precision] Returns the bearing decimal precision
Definition: CSettings.hpp:366
Container class for supported machine-friendly format/unit codes.
QMap< CUnitBearing::EUnit, QString > qMapCodes
Container class for supported human-readable format/unit symbols.
QMap< CUnitBearing::EUnit, QString > qMapSymbols
static QString toCode(EUnit _eUnit)
Returns the machine-friendly code corresponding to the given format/unit ID.
EUnit
Format/unit ID.
@ RAD
radians
@ UNDEFINED
undefined format/unit
@ DEG
degrees
static const QMap< EUnit, QString > & symbols()
Returns the list of supported human-readable format/unit symbols.
static double toValue(double _fdValue, EUnit _eUnit)
Returns the converted value, using the specified format/unit.
static const CUnitBearingSymbols oUnitBearingSymbols
Container for supported human-readable format/unit symbols.
static const CUnitBearingCodes oUnitBearingCodes
Container for 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 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 EUnit fromCode(const QString &_rqsCode)
Returns the format/unit ID corresponding to the given machine-friendly code.
static EUnit fromSymbol(const QString &_rqsSymbol)
Returns the format/unit ID corresponding to the given human-readable symbol.
static QString toSymbol(EUnit _eUnit)
Returns the human-readable symbol corresponding to the given format/unit ID.
static const QMap< EUnit, QString > & codes()
Returns the list of supported machine-friendly format/unit codes.
static CSettings * useSettings()
static constexpr double PI
Definition: QVCT.hpp:45