| 1 | #pragma once |
| 2 | |
| 3 | /* ------------- A NAV/COMM Frequency formatter ---------------------- */ |
| 4 | class FrequencyFormatterBase : public SGReferenced { |
| 5 | public: |
| 6 | virtual ~FrequencyFormatterBase() |
| 7 | { |
| 8 | } |
| 9 | |
| 10 | virtual double getFrequency() const = 0; |
| 11 | |
| 12 | }; |
| 13 | |
| 14 | class FrequencyFormatter : public FrequencyFormatterBase, public SGPropertyChangeListener { |
| 15 | public: |
| 16 | FrequencyFormatter( SGPropertyNode_ptr freqNode, SGPropertyNode_ptr fmtFreqNode, double channelSpacing, double min, double max ) : |
| 17 | _freqNode( freqNode ), |
| 18 | _fmtFreqNode( fmtFreqNode ), |
| 19 | _channelSpacing(channelSpacing), |
| 20 | _min(min), |
| 21 | _max(max) |
| 22 | { |
| 23 | _freqNode->addChangeListener( this, true ); |
| 24 | } |
| 25 | virtual ~FrequencyFormatter() |
| 26 | { |
| 27 | _freqNode->removeChangeListener( this ); |
| 28 | } |
| 29 | |
| 30 | void valueChanged (SGPropertyNode * prop) |
| 31 | { |
| 32 | // format as fixed decimal "nnn.nn" |
| 33 | std::ostringstream buf; |
| 34 | buf << std::fixed |
| 35 | << std::setw(5) |
| 36 | << std::setfill('0') |
| 37 | << std::setprecision(2) |
| 38 | << getFrequency(); |
| 39 | _fmtFreqNode->setStringValue( buf.str() ); |
| 40 | } |
| 41 | |
| 42 | virtual double getFrequency() const |
| 43 | { |
| 44 | double d = SGMiscd::roundToInt(_freqNode->getDoubleValue() / _channelSpacing) * _channelSpacing; |
| 45 | // strip last digit, do not round |
| 46 | double f = ((int)(d*100))/100.0; |
| 47 | if( f < _min ) return _min; |
| 48 | if( f >= _max ) return _max; |
| 49 | return f; |
| 50 | } |
| 51 | |
| 52 | private: |
| 53 | SGPropertyNode_ptr _freqNode; |
| 54 | SGPropertyNode_ptr _fmtFreqNode; |
| 55 | double _channelSpacing; |
| 56 | double _min; |
| 57 | double _max; |
| 58 | }; |