ChoUnits.cpp 15.1 KB
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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
//**************************************************************************************************
//                                          ChoUnits.cpp                                           *
//                                         --------------                                          *
// Started     : 2004-03-27                                                                        *
// Last Update : 2015-01-24                                                                        *
// Copyright   : (C) 2004-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 "ChoUnits.hpp"

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

ChoUnits::ChoUnits( void ) : UnitsBase( ), wxChoice( )
{
  bClear( );
}

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

ChoUnits::~ChoUnits( )
{
}

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

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

  // Create the object
#if wxCHECK_VERSION( 3,0,0 )
  if( ! wxChoice::Create( poWin, oWinID, wxDefaultPosition, wxSize( iWidth, -1) ) ) return( false );
#else
  if( ! wxChoice::Create( poWin, oWinID, wxDefaultPosition, wxSize( iWidth, GUI_CTRL_HT ) ) )
                                                                                    return( false );
#endif
  // Set the units
  if( ! bSetUnitsType( m_eUnitsType ) )                                             return( false );
  if( ! bSetUnits( m_osDefUnits ) )                                                 return( false );

  return( true );
}

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

bool  ChoUnits::bClear( void )
{
  m_eUnitsType = eUNITS_NONE;
  m_osDefUnits = wxT("Units");

  if( bIsCreated( ) ) Clear( );

  return( true );
}

//**************************************************************************************************
// Set the type of units to be displayed.
//
// Argument List :
//   eUType - The type of units to be displayed
//
// Return Values :
//   Success - true
//   Failure - false

bool  ChoUnits::bSetUnitsType( eTypeUnits eUType )
{
  int  i1;

  if( ! bIsCreated( ) )                     return( false );
  if( eUType==m_eUnitsType && !IsEmpty( ) ) return( true );

  // Load the new choice items into the control
  switch( eUType )
  {
    case eUNITS_CAP :   // Capacitance
      Clear( ); // Delete existing choice items from the list
      Append( wxT("F")      , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mF")     , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uF")     , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("nF")     , new wxStringClientData(  wxT("-9") ) );
      Append( wxT("pF")     , new wxStringClientData( wxT("-12") ) );
      m_osDefUnits = wxT("uF");
      break;

    case eUNITS_IND :   // Inductance
      Clear( );
      Append( wxT("H")      , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mH")     , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uH")     , new wxStringClientData(  wxT("-6") ) );
      m_osDefUnits = wxT("mH");
      break;

    case eUNITS_RES :   // Resistance
      Clear( );
      Append( wxT("GOhm")   , new wxStringClientData(   wxT("9") ) );
      Append( wxT("MOhm")   , new wxStringClientData(   wxT("6") ) );
      Append( wxT("kOhm")   , new wxStringClientData(   wxT("3") ) );
      Append( wxT("Ohm")    , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mOhm")   , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uOhm")   , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("nOhm")   , new wxStringClientData(  wxT("-9") ) );
      m_osDefUnits = wxT("kOhm");
      break;

    case eUNITS_COND :  // Conductance
      Clear( );
      Append( wxT("kS")     , new wxStringClientData(   wxT("3") ) );
      Append( wxT("S")      , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mS")     , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uS")     , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("nS")     , new wxStringClientData(  wxT("-9") ) );
      Append( wxT("pS")     , new wxStringClientData( wxT("-12") ) );
      Append( wxT("fS")     , new wxStringClientData( wxT("-15") ) );
      m_osDefUnits = wxT("mS");
      break;

    case eUNITS_VOLT :  // Voltage
      Clear( );
      Append( wxT("MV")     , new wxStringClientData(   wxT("6") ) );
      Append( wxT("kV")     , new wxStringClientData(   wxT("3") ) );
      Append( wxT("V")      , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mV")     , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uV")     , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("nV")     , new wxStringClientData(  wxT("-9") ) );
      Append( wxT("pV")     , new wxStringClientData( wxT("-12") ) );
      Append( wxT("fV")     , new wxStringClientData( wxT("-15") ) );
      m_osDefUnits = wxT("mV");
      break;

    case eUNITS_CURR :  // Current
      Clear( );
      Append( wxT("kA")     , new wxStringClientData(   wxT("3") ) );
      Append( wxT("A")      , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mA")     , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uA")     , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("nA")     , new wxStringClientData(  wxT("-9") ) );
      Append( wxT("pA")     , new wxStringClientData( wxT("-12") ) );
      m_osDefUnits = wxT("mA");
      break;

    case eUNITS_TIME :  // Time
      Clear( );
      Append( wxT("Sec")    , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mSec")   , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uSec")   , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("nSec")   , new wxStringClientData(  wxT("-9") ) );
      Append( wxT("pSec")   , new wxStringClientData( wxT("-12") ) );
      m_osDefUnits = wxT("mSec");
      break;

    case eUNITS_FREQ :  // Frequency
      Clear( );
      Append( wxT("THz")    , new wxStringClientData(  wxT("12") ) );
      Append( wxT("GHz")    , new wxStringClientData(   wxT("9") ) );
      Append( wxT("MHz")    , new wxStringClientData(   wxT("6") ) );
      Append( wxT("kHz")    , new wxStringClientData(   wxT("3") ) );
      Append( wxT("Hz")     , new wxStringClientData(   wxT("0") ) );
      m_osDefUnits = wxT("kHz");
      break;

    case eUNITS_CHRG :  // Charge
      Clear( );
      Append( wxT("C")      , new wxStringClientData(   wxT("0") ) );
      Append( wxT("mC")     , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("uC")     , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("nC")     , new wxStringClientData(  wxT("-9") ) );
      Append( wxT("pC")     , new wxStringClientData( wxT("-12") ) );
      Append( wxT("fC")     , new wxStringClientData( wxT("-15") ) );
      m_osDefUnits = wxT("uC");
      break;

    case eUNITS_PHAD :  // Phase / angle
    case eUNITS_PHAR :
      Clear( );
      Append( wxT("Degree") , new wxStringClientData(   wxT("0") ) );
      Append( wxT("Radian") , new wxStringClientData(   wxT("0") ) );
      m_osDefUnits = wxT("Degree");
      break;

    case eUNITS_TMPC :  // Temperature
    case eUNITS_TMPF :
      Clear( );
      Append( wxT("Deg.C")  , new wxStringClientData(   wxT("0") ) );
      Append( wxT("Deg.F")  , new wxStringClientData(   wxT("0") ) );
      m_osDefUnits = wxT("Deg.C");
      break;

    case eUNITS_EXP :  // Dimensionless, append an exponent
      Clear( );
//    Append( wxT("x 1E21") , new wxStringClientData(  wxT("21") ) );
//    Append( wxT("x 1E18") , new wxStringClientData(  wxT("18") ) );
      Append( wxT("x 1E15") , new wxStringClientData(  wxT("15") ) );
      Append( wxT("x 1E12") , new wxStringClientData(  wxT("12") ) );
      Append( wxT("x 1E9")  , new wxStringClientData(   wxT("9") ) );
      Append( wxT("x 1E6")  , new wxStringClientData(   wxT("6") ) );
      Append( wxT("x 1E3")  , new wxStringClientData(   wxT("3") ) );
      Append( wxT("x 1")    , new wxStringClientData(   wxT("0") ) );
      Append( wxT("x 1E-3") , new wxStringClientData(  wxT("-3") ) );
      Append( wxT("x 1E-6") , new wxStringClientData(  wxT("-6") ) );
      Append( wxT("x 1E-9") , new wxStringClientData(  wxT("-9") ) );
      Append( wxT("x 1E-12"), new wxStringClientData( wxT("-12") ) );
      Append( wxT("x 1E-15"), new wxStringClientData( wxT("-15") ) );
//    Append( wxT("x 1E-18"), new wxStringClientData( wxT("-18") ) );
//    Append( wxT("x 1E-21"), new wxStringClientData( wxT("-21") ) );
      m_osDefUnits = wxT("x 1");
      break;

    case eUNITS_NONE :  // No units specified
      Clear( );
      Append( wxT("Units")  , new wxStringClientData(   wxT("0") ) );
      m_osDefUnits = wxT("Units");
      break;

    default :
      return( false );
  }

  // Set the new units type
  m_eUnitsType = eUType;

  // Select the default units
  i1 = FindString( m_osDefUnits, true );  // Do case sensitive string search
  if( i1 != wxNOT_FOUND ) SetSelection( i1 );

  return( true );
}

//**************************************************************************************************
// Set the units to be displayed.
//
// Note : If a units type is specified in the function argument it must correspond with the type
//        currently set in this class instance.
//
// Argument List:
//   rosUnits - The units to be displayed as a string
//
// Return Values:
//   Success - true
//   Failure - false

bool  ChoUnits::bSetUnits( const wxString & rosUnits )
{
  eTypeUnits  eUType;
  int         i1;

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

  // Are a different units type specified in the function argument?
  if( UnitsBase::bGetUnitsType( rosUnits, &eUType ) )
    if( eUType != eGetUnitsType( ) ) return( false );

  // Check that the specified units are supported
  i1 = FindString( rosUnits );
  if( i1 == wxNOT_FOUND )            return( false );

  // Set the units displayed in the control
  SetSelection( i1 );

  return( true );
}

//**************************************************************************************************
// Set the units to be displayed.
//
// Argument List :
//   iExp - The exponent associated with the units to be displayed
//
// Return Values :
//   Success - true
//   Failure - false

bool  ChoUnits::bSetUnits( int iExp )
{
  wxString  os1;
  long      li1;
  uint      ui1;

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

  for( ui1=0; ui1<GetCount( ); ui1++ )
  {
    li1 = 0;
    os1 = ( (wxStringClientData *) GetClientObject( ui1 ) )->GetData( );
    os1.ToLong( &li1 );
    if( (int) li1 == iExp )
    {
      SetSelection( ui1 );
      return( true );
    }
  }

  return( false );
}

//**************************************************************************************************
// Set the default units.
//
// Argument List :
//   rosUnits - The default units to be used
//
// Return Values :
//   Success - true
//   Failure - false

bool  ChoUnits::bSetDefUnits( const wxString & rosUnits )
{
  // Don't proceed unless the display object has been created
  if( ! bIsCreated( ) )                       return( false );

  // Attempt to find the specified units
  if( FindString( rosUnits ) == wxNOT_FOUND ) return( false );

  // Set the default units
  m_osDefUnits = rosUnits;

  return( true );
}

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

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

  osUnits.Empty( );

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

  osUnits = GetStringSelection( );

  return( osUnits );
}

//**************************************************************************************************
// Get the currently selected units as an exponent.
//
// Eg. if the units are mV return -3 or if the units are MOhm return 6.
//
// Return Values :
//   Success - The units exponent
//   Failure - 0

int  ChoUnits::iGetUnits( void )
{
  wxString  os1;
  long      i1;

  if( ! bIsCreated( ) )                   return( 0 );

  i1 = GetSelection( );
  if( i1 == wxNOT_FOUND )                 return( 0 );

  os1 = ( (wxStringClientData *) GetClientObject( i1 ) )->GetData( );
  if( ! CnvtType::bStrToInt( os1, &i1 ) ) return( 0 );

  return( i1 );
}

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

void  ChoUnits::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_osDefUnits : " << m_osDefUnits.mb_str( ) << '\n';
}

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