Blame view

GUI/SW2/SRC/src/utility/LblUnits.cpp 6.71 KB
886c558b   Steve Greedy   SACAMOS Public Re...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
//**************************************************************************************************
//                                          LblUnits.cpp                                           *
//                                         --------------                                          *
// Started     : 2014-03-01                                                                        *
// Last Update : 2015-01-18                                                                        *
// Copyright   : (C) 2014-2016 MSWaters                                                            *
//**************************************************************************************************

//**************************************************************************************************
//                                                                                                 *
//      This program is free software; you can redistribute it and/or modify it under the          *
//      terms of the GNU General Public License as published by the Free Software Foundation;      *
//      either version 3 of the License, or (at your option) any later version.                    *
//                                                                                                 *
//**************************************************************************************************

#include "LblUnits.hpp"

//**************************************************************************************************
// Constructor.

LblUnits::LblUnits( void )
{
  bClear( );
}

//**************************************************************************************************
// Destructor.

LblUnits::~LblUnits( )
{
}

//**************************************************************************************************
// Create an instance of this object.
//
// Argument List :
//   poWin  - The parent window
//   oWinID - The window identifier
//   iWidth - The width of the wxLabel (wxStaticText) control in pixels
//
// Return Values :
//   true  - Success
//   false - Failure

bool  LblUnits::bCreate( wxWindow * poWin, wxWindowID oWinID, int iWidth )
{
  // Check if the object has already been created
  if( bIsCreated( ) )                           return( true );

  // Create the object
  if( ! wxLabel::Create( poWin, oWinID, wxT(""), wxDefaultPosition, wxSize( iWidth, GUI_CTRL_HT-2 ),
                         wxST_NO_AUTORESIZE ) ) return( false );

  return( true );
}

//**************************************************************************************************
// Clear object attributes.
//
// Return Values :
//   Success - true
//   Failure - false

bool  LblUnits::bClear( void )
{
  m_eUnitsType = eUNITS_NONE;
  m_iUnitsExp  = 0;

  if( bIsCreated( ) ) SetLabel( wxT("") );

  return( true );
}

//**************************************************************************************************
// Set the units type attribute.
//
// Argument List :
//   eUType - the enumerated units type
//
// Return Values :
//   true  - Success
//   false - Failure

bool  LblUnits::bSetUnitsType( eTypeUnits eUType )
{
  wxString  osUnits;
  wxChar    ocPrefix;

  m_eUnitsType = eUType;

  // Set the units label string if possible
  if( UnitsBase::bGetUnitsType( eUType, osUnits ) )
  {
    // Prepend units prefix if non-zero
    if( m_iUnitsExp != 0 )
      if( CnvtType::bUnitExpToPfx( m_iUnitsExp, &ocPrefix ) )
        osUnits.Prepend( ocPrefix );
  }
  else osUnits = wxT("");

  // Set the units label
  SetLabel( osUnits );

  return( true );
}

//**************************************************************************************************
// Set the units from a string.
//
// Argument List :
//   rosUnits - the units string
//
// Return Values :
//   true  - Success
//   false - Failure

bool  LblUnits::bSetUnits( const wxString & rosUnits )
{
  eTypeUnits  eUType;
  int         iExp;

  if( ! bIsCreated( ) ) return( false );

  if( ! rosUnits.IsEmpty( ) )
  {
    // Set the units label
    SetLabel( rosUnits );

    // Attempt to determine the enumerated units type
    if( bGetUnitsType( rosUnits, &eUType ) )
      m_eUnitsType = eUType;

    // Attempt to determine the units exponent
    if( ! rosUnits.Lower( ).StartsWith( wxT("metre") ) )
      if( CnvtType::bUnitPfxToExp( rosUnits[ 0 ], &iExp ) )
        m_iUnitsExp = iExp;
  }
  else bClear( );

  return( true );
}

//**************************************************************************************************
// Set the units as an exponent.
//
// Argument List :
//   iExp - the units exponent
//
// Return Values :
//   true  - Success
//   false - Failure

bool  LblUnits::bSetUnits( int iExp )
{
  if( iExp<-24 || iExp>24 ) return( false );
  if( iExp%3 != 0 )         return( false );

  m_iUnitsExp = iExp;

  bSetUnitsType( m_eUnitsType );

  return( true );
}

//**************************************************************************************************
// Get the units.
//
// Return Values :
//   Success - The selected units
//   Failure - An empty string

const wxString & LblUnits::rosGetUnits( void )
{
  static  wxString  osUnits;

  osUnits.Empty( );

  if( ! bIsCreated( ) ) return( osUnits );

  osUnits = GetLabel( );

  return( osUnits );
}

//**************************************************************************************************
// Print the object attributes.
//
// Argument List :
//   rosPrefix - A prefix to every line displayed (usually just spaces)

void  LblUnits::Print( const wxString & rosPrefix )
{
  std::cout << rosPrefix.mb_str( ) << "m_eUnitsType : ";
  switch( m_eUnitsType )
  {
    case eUNITS_CAP  : std::cout << "eUNITS_CAP";  break;
    case eUNITS_IND  : std::cout << "eUNITS_IND";  break;
    case eUNITS_RES  : std::cout << "eUNITS_RES";  break;
    case eUNITS_COND : std::cout << "eUNITS_COND"; break;
    case eUNITS_VOLT : std::cout << "eUNITS_VOLT"; break;
    case eUNITS_CURR : std::cout << "eUNITS_CURR"; break;
    case eUNITS_TIME : std::cout << "eUNITS_TIME"; break;
    case eUNITS_FREQ : std::cout << "eUNITS_FREQ"; break;
    case eUNITS_CHRG : std::cout << "eUNITS_CHRG"; break;
    case eUNITS_PHAD : std::cout << "eUNITS_PHAD"; break;
    case eUNITS_PHAR : std::cout << "eUNITS_PHAR"; break;
    case eUNITS_TMPC : std::cout << "eUNITS_TMPC"; break;
    case eUNITS_TMPF : std::cout << "eUNITS_TMPF"; break;
    case eUNITS_PCT  : std::cout << "eUNITS_PCT";  break;
    case eUNITS_EXP  : std::cout << "eUNITS_EXP";  break;

    case eUNITS_NONE : std::cout << "eUNITS_NONE"; break;

    default          : std::cout << "Invalid";
  }
  std::cout << '\n';

  std::cout << rosPrefix.mb_str( ) << "m_iUnitsExp  : " << m_iUnitsExp << '\n';
}

//**************************************************************************************************