Qt Virtual Chart Table (QVCT)
CUnitTimeDelta.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 // C/C++
20 #include <cmath>
21 
22 // QT
23 #include <QMap>
24 #include <QRegExp>
25 #include <QString>
26 
27 // QVCT
28 #include "QVCTRuntime.hpp"
29 #include "units/CUnitTimeDelta.hpp"
30 
31 
32 //------------------------------------------------------------------------------
33 // CONSTANTS / STATIC
34 //------------------------------------------------------------------------------
35 
38 
39 const QMap<CUnitTimeDelta::EUnit,QString> &CUnitTimeDelta::symbols()
40 {
42 }
43 
45 {
46  return oUnitTimeDeltaSymbols.qMapSymbols.value( _eUnit, "?" );
47 }
48 
50 {
51  return oUnitTimeDeltaSymbols.qMapSymbols.key( _rqString, UNDEFINED );
52 }
53 
54 const QMap<CUnitTimeDelta::EUnit,QString> &CUnitTimeDelta::codes()
55 {
57 }
58 
60 {
61  return oUnitTimeDeltaCodes.qMapCodes.value( _eUnit, "undef" );
62 }
63 
65 {
66  return oUnitTimeDeltaCodes.qMapCodes.key( _rqString, UNDEFINED );
67 }
68 
69 QString CUnitTimeDelta::toString( double _fdValue, CUnitTimeDelta::EUnit _eUnit, int _iPrecision )
70 {
71  switch( _eUnit )
72  {
73 
74  case HMS:
75  {
76  double __fdSec = fabs( _fdValue );
77  double __fdMin;
78  modf( __fdSec / 60.0, &__fdMin );
79  __fdSec -= __fdMin * 60.0;
80  double __fdHour;
81  modf( __fdMin / 60.0, &__fdHour );
82  __fdMin -= __fdHour * 60.0;
83  QString __qsSec = QString::number( __fdSec, 'f', _iPrecision ).prepend( "0" ).right( 2+( _iPrecision>0 ? _iPrecision+1 : 0 ) ); // WARNING: rounding-up may occur!
84  if( __qsSec[0] == '6' ) { __fdMin += 1; __qsSec[0] = '0'; } // fix rounding-up
85  QString __qsMin = QString::number( __fdMin, 'f', 0 ).prepend( "0" ).right( 2 );
86  if( __qsMin[0] == '6' ) { __fdHour += 1; __qsMin[0] = '0'; } // fix rounding-up
87  return QString::number( _fdValue < 0 ? -__fdHour : __fdHour, 'f', 0 )+":"+__qsMin+":"+__qsSec;
88  }
89 
90  case HM:
91  {
92  double __fdMin = fabs( _fdValue ) / 60.0;
93  double __fdHour;
94  modf( __fdMin / 60.0, &__fdHour );
95  __fdMin -= __fdHour * 60.0;
96  QString __qsMin = QString::number( __fdMin, 'f', _iPrecision ).prepend( "0" ).right( 2+( _iPrecision>0 ? _iPrecision+1 : 0 ) ); // WARNING: rounding-up may occur!
97  if( __qsMin[0] == '6' ) { __fdHour += 1; __qsMin[0] = '0'; } // fix rounding-up
98  return QString::number( _fdValue < 0 ? -__fdHour : __fdHour, 'f', 0 )+":"+__qsMin;
99  }
100 
101  case M:
102  return QString::number( _fdValue / 60.0, 'f', _iPrecision )+"'";
103 
104  case S:
105  return QString::number( _fdValue, 'f', _iPrecision )+"\"";
106 
107  default: return "?";
108  }
109 }
110 
111 QString CUnitTimeDelta::toString( double _fdValue )
112 {
113  CSettings* __poSettings = QVCTRuntime::useSettings();
114  return toString( _fdValue, __poSettings->getUnitTimeDelta(), __poSettings->getPrecisionTimeDelta() );
115 }
116 
117 double CUnitTimeDelta::fromString( const QString& _rqString, bool* _pbOK )
118 {
119  static const QRegExp __qRegExpHMS( "^\\s*(-?)(0?\\d|1\\d|2[0-3]):(0?\\d|[1-5]\\d):(0?\\d|[1-5]\\d(\\.\\d+)?)\\s*$" );
120  static const QRegExp __qRegExpHM( "^\\s*(-?)(0?\\d|1\\d|2[0-3]):((0?\\d|[1-5]\\d)(\\.\\d+)?)\\s*$" );
121  static const QRegExp __qRegExpM( "^\\s*(-?)(\\d+(\\.\\d+)?)\\s*'\\s*$" );
122  static const QRegExp __qRegExpS( "^\\s*(-?)(\\d+(\\.\\d+)?)\\s*\"\\s*$" );
123 
124  if( _pbOK ) *_pbOK = false;
125  bool __bOK;
126  double __fdValue;
127  QString __qsSign;
128  if( __qRegExpHMS.exactMatch( _rqString ) )
129  {
130  __qsSign = __qRegExpHMS.cap(1);
131  __fdValue = __qRegExpHMS.cap(2).toDouble( &__bOK ) * 3600.0;
132  if( !__bOK ) return 0.0;
133  __fdValue += __qRegExpHMS.cap(3).toDouble( &__bOK ) * 60.0;
134  if( !__bOK ) return 0.0;
135  __fdValue += __qRegExpHMS.cap(4).toDouble( &__bOK );
136  if( !__bOK ) return 0.0;
137  }
138  else if( __qRegExpHM.exactMatch( _rqString ) )
139  {
140  __qsSign = __qRegExpHM.cap(1);
141  __fdValue = __qRegExpHM.cap(2).toDouble( &__bOK ) * 3600.0;
142  if( !__bOK ) return 0.0;
143  __fdValue += __qRegExpHM.cap(3).toDouble( &__bOK ) * 60.0;
144  if( !__bOK ) return 0.0;
145  }
146  else if( __qRegExpM.exactMatch( _rqString ) )
147  {
148  __qsSign = __qRegExpM.cap(1);
149  __fdValue = __qRegExpM.cap(2).toDouble( &__bOK ) * 60.0;
150  if( !__bOK ) return 0.0;
151  }
152  else if( __qRegExpS.exactMatch( _rqString ) )
153  {
154  __qsSign = __qRegExpS.cap(1);
155  __fdValue = __qRegExpS.cap(2).toDouble( &__bOK );
156  if( !__bOK ) return 0.0;
157  }
158  else return 0.0;
159  if( !__qsSign.isEmpty() ) __fdValue = -__fdValue;
160  if( _pbOK ) *_pbOK = true;
161  return __fdValue;
162 }
[UI] Container for the application's settings
Definition: CSettings.hpp:51
int getPrecisionTimeDelta()
[Precision] Returns the time difference decimal precision
Definition: CSettings.hpp:362
CUnitTimeDelta::EUnit getUnitTimeDelta()
[Unit] Returns the time difference format/unit
Definition: CSettings.hpp:343
Container class for supported machine-friendly format/unit codes.
QMap< CUnitTimeDelta::EUnit, QString > qMapCodes
Container class for supported human-readable format/unit symbols.
QMap< CUnitTimeDelta::EUnit, QString > qMapSymbols
static const QMap< EUnit, QString > & symbols()
Returns the list of supported human-readable format/unit symbols.
static const CUnitTimeDeltaSymbols oUnitTimeDeltaSymbols
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 EUnit fromCode(const QString &_rqsCode)
Returns the format/unit ID corresponding to the given machine-friendly code.
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 double fromString(const QString &_rqString, bool *_pbOK=0)
Returns the numeric value corresponding (parsed) from the string.
static QString toCode(EUnit _eUnit)
Returns the machine-friendly code corresponding to the given format/unit ID.
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 fromSymbol(const QString &_rqsSymbol)
Returns the format/unit ID corresponding to the given human-readable symbol.
static const CUnitTimeDeltaCodes oUnitTimeDeltaCodes
Container for supported machine-friendly format/unit codes.
static CSettings * useSettings()