EnviornmentWidget.cxx 31.5 KB
Newer Older
1 2 3 4 5
/* Distributed under the Apache License, Version 2.0.
See accompanying NOTICE file for details.*/
#include "EnviornmentWidget.h"
#include "ui_Enviornment.h"
#include <QLayout>
Aaron Bray's avatar
Aaron Bray committed
6 7
#include <QGroupBox.h>
#include <QFileDialog>
8
#include <qmessagebox.h>
Aaron Bray's avatar
Aaron Bray committed
9 10 11

#include "EnviornmentWidget.h"
#include "AddPopup.h"
12 13 14

#include "cdm/CommonDataModel.h"
#include "PulsePhysiologyEngine.h"
Aaron Bray's avatar
Aaron Bray committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

#include "substance/SESubstance.h"
#include "substance/SESubstanceManager.h"
#include "substance/SESubstanceFraction.h"
#include "substance/SESubstanceConcentration.h"

#include "system/environment/SEEnvironment.h"
#include "system/environment/SEEnvironmentalConditions.h"
#include "system/environment/SEActiveConditioning.h"
#include "system/environment/SEAppliedTemperature.h"
#include "system/environment/actions/SEThermalApplication.h"
#include "system/environment/actions/SEChangeEnvironmentConditions.h"

#include "controls/ScalarWidget.h"
#include "controls/ScalarQuantityWidget.h"

31
#include "cdm/utils/FileUtils.h"
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "cdm/CommonDataModel.h"
#include "cdm/PhysiologyEngine.h"
#include "cdm/utils/FileUtils.h"
#include "cdm/engine/SEPatientConfiguration.h"
#include "cdm/patient/SEPatient.h"
#include "cdm/properties/SEScalar0To1.h"
#include "cdm/properties/SEScalarArea.h"
#include "cdm/properties/SEScalarFrequency.h"
#include "cdm/properties/SEScalarLength.h"
#include "cdm/properties/SEScalarMass.h"
#include "cdm/properties/SEScalarMassPerVolume.h"
#include "cdm/properties/SEScalarPower.h"
#include "cdm/properties/SEScalarPressure.h"
#include "cdm/properties/SEScalarTime.h"
#include "cdm/properties/SEScalarVolume.h"
Aaron Bray's avatar
Aaron Bray committed
47
#include "cdm/properties/SEScalarHeatResistanceArea.h"
48 49 50 51
#include "cdm/properties/SEScalarLengthPerTime.h"
#include "cdm/properties/SEScalarTemperature.h"
#include "cdm/properties/SEScalarPower.h"

52 53 54 55 56
class QEnviornmentWidget::Controls : public Ui::EnviornmentWidget
{
public:
  Controls(QTextEdit& log) : LogBox(log) {}
  QTextEdit&                         LogBox;
57
  std::string type = "Enviornment";
Aaron Bray's avatar
Aaron Bray committed
58 59 60
  // Environmental Conditions
  QScalarQuantityWidget<MassPerVolumeUnit>*      AirDensity;
  QScalarQuantityWidget<LengthPerTimeUnit>*      AirVelocity;
61
  QScalarQuantityWidget<TemperatureUnit>*        AmbientTemperature;
Aaron Bray's avatar
Aaron Bray committed
62 63 64
  QScalarQuantityWidget<PressureUnit>*           AtmosphericPressure;
  QScalarQuantityWidget<HeatResistanceAreaUnit>* ClothingResistance;
  QScalarWidget*                                 Emissivity;
65
  QScalarQuantityWidget<TemperatureUnit>*        MeanRadiantTemperature;
Aaron Bray's avatar
Aaron Bray committed
66
  QScalarWidget*                                 RelativeHumidity;
67
  QScalarQuantityWidget<TemperatureUnit>*        RespirationAmbientTemperature;
AbhijithTam's avatar
AbhijithTam committed
68

Aaron Bray's avatar
Aaron Bray committed
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
  // Eventually, These should be dynamic based on a list of substances
  SESubstance*                                   O2;
  SESubstance*                                   CO2;
  SESubstance*                                   N2;
  SESubstance*                                   CO;
  QScalarWidget*                                 AmbientO2;
  QScalarWidget*                                 AmbientCO2;
  QScalarWidget*                                 AmbientN2;
  QScalarWidget*                                 AmbientCO;
  SEScalar0To1                                   invalidFraction;
  // And we should add the ability to add new particulate definitions
  SESubstance*                                   Smoke;
  QScalarQuantityWidget<MassPerVolumeUnit>*      AmbientSmoke;
  SEScalarMassPerVolume                          invalidConcentration;

  QScalarQuantityWidget<PowerUnit>*              CoolingPower;
  QScalarWidget*                                 CoolingFraction;
  QScalarQuantityWidget<AreaUnit>*               CoolingArea;

  QScalarQuantityWidget<PowerUnit>*              HeatingPower;
  QScalarWidget*                                 HeatingFraction;
  QScalarQuantityWidget<AreaUnit>*               HeatingArea;

  QScalarWidget*                                 AppliedFraction;
  QScalarQuantityWidget<TemperatureUnit>*        AppliedTemperature;
  QScalarQuantityWidget<AreaUnit>*               AppliedArea;

  SEThermalApplication*                          ThermalApplication;
  SEChangeEnvironmentConditions*                 EnvironmentalConditions;
  bool                                           sendEnvironment = false;
  bool                                           sendAction = false;
100 101 102 103
  bool                       CoolingFracLast = false;
  bool                       HeatingFracLast = false;
  bool                       AppliedFracLast = false;
  bool                       applied = false;
104 105
};

Aaron Bray's avatar
Aaron Bray committed
106
QEnviornmentWidget::QEnviornmentWidget(QPulse& qp, QWidget *parent, Qt::WindowFlags flags) : QDockWidget(parent,flags)
107
{
Aaron Bray's avatar
Aaron Bray committed
108
  m_Controls = new Controls(qp.GetLogBox());
109
  m_Controls->setupUi(this);
Aaron Bray's avatar
Aaron Bray committed
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

  ////////////////////////////////////
  // Setup Environmental Conditions //
  ////////////////////////////////////

  m_Controls->AirDensity = new QScalarQuantityWidget<MassPerVolumeUnit>("Air Density", 0.0, 10.0, 1, MassPerVolumeUnit::g_Per_L, ScalarOptionWidget::Check, this);
  m_Controls->AirDensity->enableConverter(true);
  m_Controls->AirDensity->enableInput(true);
  m_Controls->AirDensity->addUnit(MassPerVolumeUnit::g_Per_m3);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->AirDensity);
  m_Controls->AirVelocity = new QScalarQuantityWidget<LengthPerTimeUnit>("Air Velocity", 0.0, 30.0, 1, LengthPerTimeUnit::ft_Per_s, ScalarOptionWidget::Check, this);
  m_Controls->AirVelocity->enableConverter(true);
  m_Controls->AirVelocity->enableInput(true);
  m_Controls->AirVelocity->addUnit(LengthPerTimeUnit::m_Per_s);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->AirVelocity);
  m_Controls->AmbientTemperature = new QScalarQuantityWidget<TemperatureUnit>("Ambient Temperature", 0.0, 150.0, 1, TemperatureUnit::F, ScalarOptionWidget::Check, this);
  m_Controls->AmbientTemperature->enableConverter(true);
  m_Controls->AmbientTemperature->enableInput(true);
  m_Controls->AmbientTemperature->addUnit(TemperatureUnit::C);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->AmbientTemperature);
  m_Controls->AtmosphericPressure = new QScalarQuantityWidget<PressureUnit>("Atmospheric Pressure", 0.0, 100.0, 1, PressureUnit::psi, ScalarOptionWidget::Check, this);
  m_Controls->AtmosphericPressure->enableConverter(true);
  m_Controls->AtmosphericPressure->enableInput(true);
  m_Controls->AtmosphericPressure->addUnit(PressureUnit::mmHg);
  m_Controls->AtmosphericPressure->addUnit(PressureUnit::Pa);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->AtmosphericPressure);
  m_Controls->ClothingResistance = new QScalarQuantityWidget<HeatResistanceAreaUnit>("Clothing Resistance", 0, 65, 1, HeatResistanceAreaUnit::clo, ScalarOptionWidget::Check, this);
  m_Controls->ClothingResistance->enableConverter(true);
  m_Controls->ClothingResistance->enableInput(true);
  m_Controls->ClothingResistance->addUnit(HeatResistanceAreaUnit::clo);
  m_Controls->ClothingResistance->addUnit(HeatResistanceAreaUnit::rsi);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->ClothingResistance);
  m_Controls->Emissivity = new QScalarWidget("Emissivity", 0.0, 1.0, .1, ScalarOptionWidget::Check, this);
  m_Controls->Emissivity->enableInput(true);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->Emissivity);
  m_Controls->MeanRadiantTemperature = new QScalarQuantityWidget<TemperatureUnit>("Mean Radiant Temperature", 0.0, 150.0, 1, TemperatureUnit::F, ScalarOptionWidget::Check, this);
  m_Controls->MeanRadiantTemperature->enableConverter(true);
  m_Controls->MeanRadiantTemperature->enableInput(true);
  m_Controls->MeanRadiantTemperature->addUnit(TemperatureUnit::C);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->MeanRadiantTemperature);
  m_Controls->RelativeHumidity = new QScalarWidget("Relative Humidity", 0.0, 1.0, 0.1, ScalarOptionWidget::Check, this);
  m_Controls->RelativeHumidity->enableInput(true);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->RelativeHumidity);
  m_Controls->RespirationAmbientTemperature = new QScalarQuantityWidget<TemperatureUnit>("Respiration Ambient Temperature", 0.0, 150.0, 1, TemperatureUnit::F, ScalarOptionWidget::Check, this);
  m_Controls->RespirationAmbientTemperature->enableConverter(true);
  m_Controls->RespirationAmbientTemperature->enableInput(true);
  m_Controls->RespirationAmbientTemperature->addUnit(TemperatureUnit::C);
  m_Controls->RespirationAmbientTemperature->addUnit(TemperatureUnit::F);
  m_Controls->RespirationAmbientTemperature->addUnit(TemperatureUnit::K);
  m_Controls->ConditionsProperties->layout()->addWidget(m_Controls->RespirationAmbientTemperature);

  ////////////////////////////////////////////////
  // Ambient Environment Gasses and Particulate //
  ////////////////////////////////////////////////
  m_Controls->O2 = qp.GetEngine().GetSubstanceManager().GetSubstance("Oxygen");
  m_Controls->AmbientO2 = new QScalarWidget("Oxygen", 0.0, 1.0, 0.1, ScalarOptionWidget::Check, this);
  m_Controls->AmbientO2->setDecimals(5);
  m_Controls->AmbientO2->enableInput(true);
  m_Controls->AmbientGasses->layout()->addWidget(m_Controls->AmbientO2);
  m_Controls->CO2 = qp.GetEngine().GetSubstanceManager().GetSubstance("CarbonDioxide");
  m_Controls->AmbientCO2 = new QScalarWidget("Carbon Dioxide", 0.0, 1.0, 0.1, ScalarOptionWidget::Check, this);
  m_Controls->AmbientCO2->setDecimals(5);
  m_Controls->AmbientCO2->enableInput(true);
  m_Controls->AmbientGasses->layout()->addWidget(m_Controls->AmbientCO2);
  m_Controls->N2 = qp.GetEngine().GetSubstanceManager().GetSubstance("Nitrogen");
  m_Controls->AmbientN2 = new QScalarWidget("Nitrogen", 0.0, 1.0, 0.1, ScalarOptionWidget::Check, this);
  m_Controls->AmbientN2->setDecimals(5);
  m_Controls->AmbientN2->enableInput(true);
  m_Controls->AmbientGasses->layout()->addWidget(m_Controls->AmbientN2);
  m_Controls->CO = qp.GetEngine().GetSubstanceManager().GetSubstance("CarbonMonoxide");
  m_Controls->AmbientCO = new QScalarWidget("Carbon Monoxide", 0.0, 1.0, 0.1, ScalarOptionWidget::Check, this);
  m_Controls->AmbientCO->setDecimals(5);
  m_Controls->AmbientCO->enableInput(true);
  m_Controls->AmbientGasses->layout()->addWidget(m_Controls->AmbientCO);

  m_Controls->Smoke = qp.GetEngine().GetSubstanceManager().GetSubstance("ForestFireParticulate");
  m_Controls->AmbientSmoke = new QScalarQuantityWidget<MassPerVolumeUnit>("Smoke", 0.0, 1.0, 0.1, MassPerVolumeUnit::mg_Per_m3, ScalarOptionWidget::Check, this);
  m_Controls->AmbientSmoke->enableInput(true);
  m_Controls->AmbientSmoke->enableConverter(true);
  m_Controls->AmbientSmoke->addUnit(MassPerVolumeUnit::g_Per_mL);
  m_Controls->AmbientSmoke->addUnit(MassPerVolumeUnit::g_Per_L);
  m_Controls->AmbientAerosols->layout()->addWidget(m_Controls->AmbientSmoke);

  ///////////////////////////////
  // Setup Environment Actions //
  ///////////////////////////////

  m_Controls->ActiveCoolingCheck->setChecked(false);
  m_Controls->ActiveCoolingCheck->setEnabled(false);
  m_Controls->CoolingPower = new QScalarQuantityWidget<PowerUnit>("Power", 0.0, 150.0, 1, PowerUnit::J_Per_s, ScalarOptionWidget::None, this);
  m_Controls->CoolingPower->setDisabled(true);
201 202 203 204
  m_Controls->CoolingPower->addUnit(PowerUnit::kcal_Per_hr);
  m_Controls->CoolingPower->addUnit(PowerUnit::BTU_Per_hr);
  m_Controls->CoolingPower->addUnit(PowerUnit::W);
  m_Controls->ActiveCoolingWidget->layout()->addWidget(m_Controls->CoolingPower);
Aaron Bray's avatar
Aaron Bray committed
205 206
  m_Controls->CoolingFraction = new QScalarWidget("Surface Area Fraction", 0.0, 1.0, 0.1, ScalarOptionWidget::Radio, this);
  m_Controls->CoolingFraction->setDisabled(true);
207
  m_Controls->ActiveCoolingWidget->layout()->addWidget(m_Controls->CoolingFraction);
Aaron Bray's avatar
Aaron Bray committed
208 209
  m_Controls->CoolingArea = new QScalarQuantityWidget<AreaUnit>("Surface Area", 0.0, 150.0, 1, AreaUnit::m2, ScalarOptionWidget::Radio, this);
  m_Controls->CoolingArea->setDisabled(true);
210 211
  m_Controls->CoolingArea->addUnit(AreaUnit::cm2);
  m_Controls->ActiveCoolingWidget->layout()->addWidget(m_Controls->CoolingArea);
Aaron Bray's avatar
Aaron Bray committed
212 213 214 215 216 217 218 219 220 221 222
  // Connect Signals/Slots
  connect(m_Controls->ActiveCoolingCheck, SIGNAL(clicked()), this, SLOT(EnableCooling()));
  connect(m_Controls->CoolingFraction->getRadioButton(), SIGNAL(clicked()), this, SLOT(EnableCoolingSurfaceAreaFraction()));
  connect(m_Controls->CoolingArea->getRadioButton(), SIGNAL(clicked()), this, SLOT(EnableCoolingSurfaceArea()));
  m_Controls->CoolingPower->enableConverter(true);
  m_Controls->CoolingArea->enableConverter(true);

  m_Controls->ActiveHeatingCheck->setChecked(false);
  m_Controls->ActiveHeatingCheck->setEnabled(false);
  m_Controls->HeatingPower = new QScalarQuantityWidget<PowerUnit>("Power", 0.0, 150.0, 1, PowerUnit::J_Per_s, ScalarOptionWidget::None, this);
  m_Controls->HeatingPower->setDisabled(true);
223 224 225 226
  m_Controls->HeatingPower->addUnit(PowerUnit::kcal_Per_hr);
  m_Controls->HeatingPower->addUnit(PowerUnit::BTU_Per_hr);
  m_Controls->HeatingPower->addUnit(PowerUnit::W);
  m_Controls->ActiveHeatingWidget->layout()->addWidget(m_Controls->HeatingPower);
Aaron Bray's avatar
Aaron Bray committed
227 228
  m_Controls->HeatingFraction = new QScalarWidget("Surface Area Fraction", 0.0, 1.0, 0.1, ScalarOptionWidget::Radio, this);
  m_Controls->HeatingFraction->setDisabled(true);
229
  m_Controls->ActiveHeatingWidget->layout()->addWidget(m_Controls->HeatingFraction);
Aaron Bray's avatar
Aaron Bray committed
230 231
  m_Controls->HeatingArea = new QScalarQuantityWidget<AreaUnit>("Surface Area", 0.0, 150.0, 1, AreaUnit::m2, ScalarOptionWidget::Radio, this);
  m_Controls->HeatingArea->setDisabled(true);
232 233
  m_Controls->HeatingArea->addUnit(AreaUnit::cm2);
  m_Controls->ActiveHeatingWidget->layout()->addWidget(m_Controls->HeatingArea);
Aaron Bray's avatar
Aaron Bray committed
234 235 236 237 238 239 240 241 242 243 244
  // Connect Signals/Slots
  connect(m_Controls->ActiveHeatingCheck, SIGNAL(clicked()), this, SLOT(EnableHeating()));
  connect(m_Controls->HeatingFraction->getRadioButton(), SIGNAL(clicked()), this, SLOT(EnableHeatingSurfaceAreaFraction()));
  connect(m_Controls->HeatingArea->getRadioButton(), SIGNAL(clicked()), this, SLOT(EnableHeatingSurfaceArea()));
  m_Controls->HeatingPower->enableConverter(true);
  m_Controls->HeatingArea->enableConverter(true);

  m_Controls->AppliedTempCheck->setChecked(false);
  m_Controls->AppliedTempCheck->setEnabled(false);
  m_Controls->AppliedTemperature = new QScalarQuantityWidget<TemperatureUnit>("Temperature", 0.0, 150.0, 1, TemperatureUnit::F, ScalarOptionWidget::None, this);
  m_Controls->AppliedTemperature->setDisabled(true);
245 246
  m_Controls->AppliedTemperature->addUnit(TemperatureUnit::C);
  m_Controls->AppliedTempWidget->layout()->addWidget(m_Controls->AppliedTemperature);
Aaron Bray's avatar
Aaron Bray committed
247 248 249 250 251
  m_Controls->AppliedFraction = new QScalarWidget("Surface Area Fraction", 0.0, 1.0, 0.1, ScalarOptionWidget::Radio, this);
  m_Controls->AppliedFraction->setDisabled(true);
  m_Controls->AppliedTempWidget->layout()->addWidget(m_Controls->AppliedFraction);
  m_Controls->AppliedArea = new QScalarQuantityWidget<AreaUnit>("Surface Area", 0.0, 150.0, 1, AreaUnit::m2, ScalarOptionWidget::Radio, this);
  m_Controls->AppliedArea->setDisabled(true);
252 253
  m_Controls->AppliedArea->addUnit(AreaUnit::cm2);
  m_Controls->AppliedTempWidget->layout()->addWidget(m_Controls->AppliedArea);
Aaron Bray's avatar
Aaron Bray committed
254 255 256 257
  // Connect Signals/Slots
  connect(m_Controls->AppliedTempCheck, SIGNAL(clicked()), this, SLOT(EnableAppliedTemp()));
  connect(m_Controls->AppliedFraction->getRadioButton(), SIGNAL(clicked()), this, SLOT(EnableAppliedSurfaceAreaFraction()));
  connect(m_Controls->AppliedArea->getRadioButton(), SIGNAL(clicked()), this, SLOT(EnableAppliedTempSurfaceArea()));
AbhijithTam's avatar
AbhijithTam committed
258 259 260
  m_Controls->AppliedTemperature->enableConverter(true);
  m_Controls->AppliedArea->enableConverter(true);

Aaron Bray's avatar
Aaron Bray committed
261 262 263 264 265 266 267
  m_Controls->ThermalApplication = new SEThermalApplication();
  m_Controls->ThermalApplication->GetActiveCooling().GetPower().SetValue(0, PowerUnit::J_Per_s);
  m_Controls->ThermalApplication->GetActiveHeating().GetPower().SetValue(0, PowerUnit::J_Per_s);
  m_Controls->ThermalApplication->GetAppliedTemperature().SetState(eSwitch::Off);
  m_Controls->EnvironmentalConditions = new SEChangeEnvironmentConditions(qp.GetEngine().GetSubstanceManager());

  connect(m_Controls->ApplyAll, SIGNAL(clicked()), this, SLOT(ControlsToEnvironment()));
268 269
  connect(m_Controls->LoadEnvironment, SIGNAL(clicked()), this, SLOT(LoadEnvironmentFile()));
  connect(m_Controls->SaveEnviornment, SIGNAL(clicked()), this, SLOT(SaveEnvironmentFile()));
Aaron Bray's avatar
Aaron Bray committed
270
  Reset();
271 272 273 274
}

QEnviornmentWidget::~QEnviornmentWidget()
{
Aaron Bray's avatar
Aaron Bray committed
275
  // TODO Clean up all the widgets
276
  m_Controls->Enviornment->close();
277 278 279 280 281
  delete m_Controls;
}

void QEnviornmentWidget::Reset()
{
Aaron Bray's avatar
Aaron Bray committed
282 283 284
  LoadEnvironmentFile("./environments/Standard.json");
  m_Controls->sendEnvironment = false;
  m_Controls->sendAction = false;
285

286 287 288 289 290 291 292 293 294 295 296 297
  m_Controls->CoolingArea->reset();
  m_Controls->CoolingFraction->reset();
  m_Controls->CoolingPower->reset();
  m_Controls->HeatingFraction->reset();
  m_Controls->HeatingArea->reset();
  m_Controls->HeatingPower->reset();
  m_Controls->AppliedFraction->reset();
  m_Controls->AppliedTemperature->reset();
  m_Controls->AppliedArea->reset();
  m_Controls->AppliedFracLast = false;
  m_Controls->CoolingFracLast = false;
  m_Controls->HeatingFracLast = false;
298 299

  EnableControls(true, false);
Aaron Bray's avatar
Aaron Bray committed
300
}
AbhijithTam's avatar
AbhijithTam committed
301

302
void QEnviornmentWidget::EnableControls(bool conditions, bool actions)
Aaron Bray's avatar
Aaron Bray committed
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
  m_Controls->AirDensity->enableInput(conditions);
  m_Controls->AirVelocity->enableInput(conditions);
  m_Controls->AmbientTemperature->enableInput(conditions);
  m_Controls->AtmosphericPressure->enableInput(conditions);
  m_Controls->ClothingResistance->enableInput(conditions);
  m_Controls->Emissivity->enableInput(conditions);
  m_Controls->MeanRadiantTemperature->enableInput(conditions);
  m_Controls->RelativeHumidity->enableInput(conditions);
  m_Controls->RespirationAmbientTemperature->enableInput(conditions);

  m_Controls->AmbientO2->enableInput(conditions);
  m_Controls->AmbientCO2->enableInput(conditions);
  m_Controls->AmbientN2->enableInput(conditions);
  m_Controls->AmbientCO->enableInput(conditions);

  m_Controls->AmbientSmoke->enableInput(conditions);

  m_Controls->LoadEnvironment->setEnabled(conditions);
  m_Controls->SaveEnviornment->setEnabled(conditions);

  m_Controls->ActiveCoolingCheck->setChecked(false);
  m_Controls->ActiveCoolingCheck->setEnabled(actions);

  m_Controls->ActiveHeatingCheck->setChecked(false);
  m_Controls->ActiveHeatingCheck->setEnabled(actions);

  m_Controls->AppliedTempCheck->setEnabled(actions);
  m_Controls->AppliedTempCheck->setChecked(false);
  EnableCooling();
  EnableHeating();
  EnableAppliedTemp();
  m_Controls->ApplyAll->setEnabled(actions);
AbhijithTam's avatar
AbhijithTam committed
336 337
}

338
void QEnviornmentWidget::LoadEnvironmentFile()
Aaron Bray's avatar
Aaron Bray committed
339 340 341 342 343
{
  QString fileName = QFileDialog::getOpenFileName(this,
    "Open Environment", "./environments", "Environment Files (*.json)");
  if (fileName.isEmpty())
    return;
AbhijithTam's avatar
AbhijithTam committed
344

Aaron Bray's avatar
Aaron Bray committed
345 346
  std::string s = fileName.toStdString();
  LoadEnvironmentFile(s);
AbhijithTam's avatar
AbhijithTam committed
347
}
348 349 350 351 352 353 354 355 356 357 358 359
void QEnviornmentWidget::LoadEnvironmentFile(const std::string& fileName)
{
  if(m_Controls->EnvironmentalConditions->GetConditions().SerializeFromFile(fileName, SerializationFormat::JSON))
    EnvironmentToControls();
  else 
  {
    QMessageBox messageBox;
    std::string message = "Unable to load Enviornment File";
    messageBox.critical(0, "Error", QString::fromUtf8(message.c_str()));
    messageBox.setFixedSize(500, 200);
  }
}
AbhijithTam's avatar
AbhijithTam committed
360

361 362 363 364 365 366
void QEnviornmentWidget::SaveEnvironmentFile()
{
  QString fileName = QFileDialog::getSaveFileName(this,
    "Save Enviornment", "./environments", "Environment Files (*.json)");
  if (fileName.isEmpty())
    return;
367

368 369 370 371 372 373 374 375 376 377 378 379 380
  std::string s = fileName.toStdString();
  SaveEnviornmentFile(s);
}
void QEnviornmentWidget::SaveEnviornmentFile(const std::string& fileName)
{
  ControlsToEnvironment();
  if (!m_Controls->EnvironmentalConditions->GetConditions().SerializeToFile(fileName, SerializationFormat::JSON) )
  {
    QMessageBox messageBox;
      std::string message = "Unable to save Enviornment File";
      messageBox.critical(0, "Error", QString::fromUtf8(message.c_str()));
      messageBox.setFixedSize(500, 200);
  }
381 382
}

Aaron Bray's avatar
Aaron Bray committed
383 384
void QEnviornmentWidget::ControlsToEnvironment()
{
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 428 429 430
  m_Controls->AirDensity->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetAirDensity());
  m_Controls->AirVelocity->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetAirVelocity());
  m_Controls->AmbientTemperature->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetAmbientTemperature());
  m_Controls->AtmosphericPressure->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetAtmosphericPressure());
  m_Controls->ClothingResistance->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetClothingResistance());
  m_Controls->Emissivity->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetEmissivity());
  m_Controls->MeanRadiantTemperature->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetMeanRadiantTemperature());
  m_Controls->RelativeHumidity->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetRelativeHumidity());
  m_Controls->RespirationAmbientTemperature->getValue(m_Controls->EnvironmentalConditions->GetConditions().GetRespirationAmbientTemperature());
  double fractotal = 0.0;
  for (auto g : m_Controls->EnvironmentalConditions->GetConditions().GetAmbientGases())
  {
    if (&g->GetSubstance() == m_Controls->O2)
    {
      m_Controls->AmbientO2->getValue(g->GetFractionAmount());
      fractotal = fractotal + g->GetFractionAmount().GetValue();
      continue;
    }
    if (&g->GetSubstance() == m_Controls->CO2)
    {
      m_Controls->AmbientCO2->getValue(g->GetFractionAmount());
      fractotal = fractotal + g->GetFractionAmount().GetValue();
      continue;
    }
    if (&g->GetSubstance() == m_Controls->N2)
    {
      m_Controls->AmbientN2->getValue(g->GetFractionAmount());
      fractotal = fractotal + g->GetFractionAmount().GetValue();
      continue;
    }
    if (&g->GetSubstance() == m_Controls->CO)
    {
      m_Controls->AmbientCO->getValue(g->GetFractionAmount());
      fractotal = fractotal + g->GetFractionAmount().GetValue();
      continue;
    }
  }

  m_Controls->AmbientSmoke->setValue(m_Controls->invalidConcentration);
  for (auto g : m_Controls->EnvironmentalConditions->GetConditions().GetAmbientAerosols())
  {
    if (&g->GetSubstance() == m_Controls->Smoke)
    {
      m_Controls->AmbientSmoke->setValue(g->GetConcentration());
    }
  }
Aaron Bray's avatar
Aaron Bray committed
431
  // Ensure that Ambient Gas Fractions sum up to one, if not throw popup error
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
  if (abs(fractotal - (double)1)<.000001)
  {
    m_Controls->sendEnvironment = true;
    // TODO Set our send boolean only if data has changed from the UI to what we have
  }
  else {
    QMessageBox messageBox;
    std::string frac = std::to_string(fractotal);
    std::string message = "Ambient Gas Fractions Do Not Sum to 1.0";
    messageBox.critical(0, "Error", QString::fromUtf8(message.c_str()));
    messageBox.setFixedSize(500, 200);
  }

  if (m_Controls->ActiveCoolingCheck->isChecked())
  {
    if (m_Controls->CoolingArea->getRadioButton()->isChecked())
    {
      m_Controls->CoolingPower->getValue(m_Controls->ThermalApplication->GetActiveCooling().GetPower());
      m_Controls->CoolingArea->getValue(m_Controls->ThermalApplication->GetActiveCooling().GetSurfaceArea());
    }
    else
    {
      m_Controls->CoolingPower->getValue(m_Controls->ThermalApplication->GetActiveCooling().GetPower());
      m_Controls->CoolingFraction->getValue(m_Controls->ThermalApplication->GetActiveCooling().GetSurfaceAreaFraction());
    }
  }

  if (m_Controls->ActiveHeatingCheck->isChecked())
  {
    if (m_Controls->HeatingArea->getRadioButton()->isChecked())
    {
      m_Controls->HeatingPower->getValue(m_Controls->ThermalApplication->GetActiveHeating().GetPower());
      m_Controls->HeatingArea->getValue(m_Controls->ThermalApplication->GetActiveHeating().GetSurfaceArea());
    }
    else
    {
      m_Controls->HeatingPower->getValue(m_Controls->ThermalApplication->GetActiveHeating().GetPower());
      m_Controls->HeatingFraction->getValue(m_Controls->ThermalApplication->GetActiveHeating().GetSurfaceAreaFraction());
    }
  }

  if (m_Controls->AppliedTempCheck->isChecked())
  {
    if (m_Controls->AppliedArea->getRadioButton()->isChecked())
    {
      m_Controls->AppliedTemperature->getValue(m_Controls->ThermalApplication->GetAppliedTemperature().GetTemperature());
      m_Controls->AppliedArea->getValue(m_Controls->ThermalApplication->GetAppliedTemperature().GetSurfaceArea());
    }
    else
    {
      m_Controls->AppliedTemperature->getValue(m_Controls->ThermalApplication->GetAppliedTemperature().GetTemperature());
      m_Controls->AppliedFraction->getValue(m_Controls->ThermalApplication->GetAppliedTemperature().GetSurfaceAreaFraction());
    }
  }

  if (m_Controls->AppliedTempCheck->isChecked() || 
      m_Controls->ActiveCoolingCheck->isChecked() || 
      m_Controls->ActiveHeatingCheck->isChecked()) 
  {
    m_Controls->sendAction = true;
    // TODO Set our send boolean only if data has changed from the UI to what we have
  }
494 495
}

Aaron Bray's avatar
Aaron Bray committed
496 497 498 499 500 501 502 503 504 505 506
void QEnviornmentWidget::EnvironmentToControls()
{
  m_Controls->AirDensity->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetAirDensity());
  m_Controls->AirVelocity->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetAirVelocity());
  m_Controls->AmbientTemperature->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetAmbientTemperature());
  m_Controls->AtmosphericPressure->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetAtmosphericPressure());
  m_Controls->ClothingResistance->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetClothingResistance());
  m_Controls->Emissivity->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetEmissivity());
  m_Controls->MeanRadiantTemperature->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetMeanRadiantTemperature());
  m_Controls->RelativeHumidity->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetRelativeHumidity());
  m_Controls->RespirationAmbientTemperature->setValue(m_Controls->EnvironmentalConditions->GetConditions().GetRespirationAmbientTemperature());
507

Aaron Bray's avatar
Aaron Bray committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
  m_Controls->AmbientO2->setValue(m_Controls->invalidFraction);
  m_Controls->AmbientCO2->setValue(m_Controls->invalidFraction);
  m_Controls->AmbientN2->setValue(m_Controls->invalidFraction);
  m_Controls->AmbientCO->setValue(m_Controls->invalidFraction);
  for (auto g : m_Controls->EnvironmentalConditions->GetConditions().GetAmbientGases())
  {
    if (&g->GetSubstance() == m_Controls->O2)
    {
      m_Controls->AmbientO2->setValue(g->GetFractionAmount());
      continue;
    }
    if (&g->GetSubstance() == m_Controls->CO2)
    {
      m_Controls->AmbientCO2->setValue(g->GetFractionAmount());
      continue;
    }
    if (&g->GetSubstance() == m_Controls->N2)
    {
      m_Controls->AmbientN2->setValue(g->GetFractionAmount());
      continue;
    }
    if (&g->GetSubstance() == m_Controls->CO)
    {
      m_Controls->AmbientCO->setValue(g->GetFractionAmount());
      continue;
    }
  }
535

Aaron Bray's avatar
Aaron Bray committed
536 537 538 539 540 541 542 543 544 545 546
  m_Controls->AmbientSmoke->setValue(m_Controls->invalidConcentration);
  for (auto g : m_Controls->EnvironmentalConditions->GetConditions().GetAmbientAerosols())
  {
    if (&g->GetSubstance() == m_Controls->Smoke)
    {
      m_Controls->AmbientSmoke->setValue(g->GetConcentration());
    }
  }

  // No need to push actions to the UI
}
547 548 549

void QEnviornmentWidget::AtSteadyState(PhysiologyEngine& pulse)
{
Aaron Bray's avatar
Aaron Bray committed
550
  m_Controls->EnvironmentalConditions->GetConditions().Copy(*pulse.GetEnvironment()->GetConditions());
551 552 553
}
void QEnviornmentWidget::AtSteadyStateUpdateUI()
{
Aaron Bray's avatar
Aaron Bray committed
554
  EnvironmentToControls();
555
  EnableControls(true, true);
556 557 558 559 560
}

void QEnviornmentWidget::ProcessPhysiology(PhysiologyEngine& pulse)
{// This is called from a thread, you should NOT update UI here
  // This is where we pull data from pulse, and push any actions to it
AbhijithTam's avatar
AbhijithTam committed
561
  
Aaron Bray's avatar
Aaron Bray committed
562 563 564 565 566 567 568 569 570 571 572
  if (m_Controls->sendEnvironment)
  {
    pulse.ProcessAction(*m_Controls->EnvironmentalConditions);
    m_Controls->sendEnvironment = false;
  }
  if (m_Controls->sendAction)
  {
    pulse.ProcessAction(*m_Controls->ThermalApplication);
    m_Controls->sendAction = false;
  }

573 574 575
}
void QEnviornmentWidget::PhysiologyUpdateUI()
{// This is called from a slot, you can update UI here
Aaron Bray's avatar
Aaron Bray committed
576 577 578 579 580 581 582 583 584 585

  // Nothing is expected to be in the environment
}

void QEnviornmentWidget::EnableCooling()
{
  bool b = m_Controls->ActiveCoolingCheck->isChecked();
  if (b)
  {
    m_Controls->CoolingPower->setEnabled(true);
586 587 588
    m_Controls->CoolingArea->setEnabled(true);
    m_Controls->CoolingFraction->setEnabled(true);

Aaron Bray's avatar
Aaron Bray committed
589
    m_Controls->CoolingPower->enableInput(true);
590 591 592 593 594 595 596 597 598 599
    if (m_Controls->CoolingFracLast)
    {
      m_Controls->CoolingArea->enableInput(false);
      m_Controls->CoolingFraction->enableInput(true);
    }
    else
    {
      m_Controls->CoolingArea->enableInput(true);
      m_Controls->CoolingFraction->enableInput(false);
    }
Aaron Bray's avatar
Aaron Bray committed
600 601 602
  }
  else
  {
603
    m_Controls->CoolingFracLast = m_Controls->CoolingFraction->getRadioButton()->isChecked();
Aaron Bray's avatar
Aaron Bray committed
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
    m_Controls->CoolingPower->setDisabled(true);
    m_Controls->CoolingArea->setDisabled(true);
    m_Controls->CoolingFraction->setDisabled(true);
  }
}
void QEnviornmentWidget::EnableCoolingSurfaceArea()
{
  bool b = m_Controls->CoolingArea->getRadioButton()->isChecked();
  m_Controls->CoolingArea->enableInput(b);
  m_Controls->CoolingFraction->enableInput(!b);
}
void QEnviornmentWidget::EnableCoolingSurfaceAreaFraction()
{
  bool b = m_Controls->CoolingFraction->getRadioButton()->isChecked();
  m_Controls->CoolingArea->enableInput(!b);
  m_Controls->CoolingFraction->enableInput(b);
}

void QEnviornmentWidget::EnableHeating()
{
  bool b = m_Controls->ActiveHeatingCheck->isChecked();
  if (b)
  {
    m_Controls->HeatingPower->setEnabled(true);
628 629 630
    m_Controls->HeatingArea->setEnabled(true);
    m_Controls->HeatingFraction->setEnabled(true);

Aaron Bray's avatar
Aaron Bray committed
631
    m_Controls->HeatingPower->enableInput(true);
632 633 634 635 636 637 638 639 640 641
    if (m_Controls->HeatingFracLast)
    {
      m_Controls->HeatingArea->enableInput(false);
      m_Controls->HeatingFraction->enableInput(true);
    }
    else
    {
      m_Controls->HeatingArea->enableInput(true);
      m_Controls->HeatingFraction->enableInput(false);
    }
Aaron Bray's avatar
Aaron Bray committed
642 643 644
  }
  else
  {
645
    m_Controls->HeatingFracLast = m_Controls->HeatingFraction->getRadioButton()->isChecked();
Aaron Bray's avatar
Aaron Bray committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
    m_Controls->HeatingPower->setDisabled(true);
    m_Controls->HeatingArea->setDisabled(true);
    m_Controls->HeatingFraction->setDisabled(true);
  }
}
void QEnviornmentWidget::EnableHeatingSurfaceArea()
{
  bool b = m_Controls->HeatingArea->getRadioButton()->isChecked();
  m_Controls->HeatingArea->enableInput(b);
  m_Controls->HeatingFraction->enableInput(!b);
}
void QEnviornmentWidget::EnableHeatingSurfaceAreaFraction()
{
  bool b = m_Controls->HeatingFraction->getRadioButton()->isChecked();
  m_Controls->HeatingArea->enableInput(!b);
  m_Controls->HeatingFraction->enableInput(b);
}

void QEnviornmentWidget::EnableAppliedTemp()
{
  bool b = m_Controls->AppliedTempCheck->isChecked();
  if (b)
  {
    m_Controls->AppliedTemperature->setEnabled(true);
670 671 672
    m_Controls->AppliedArea->setEnabled(true);
    m_Controls->AppliedFraction->setEnabled(true);

Aaron Bray's avatar
Aaron Bray committed
673
    m_Controls->AppliedTemperature->enableInput(true);
674 675 676 677 678 679 680 681 682 683
    if (m_Controls->AppliedFracLast)
    {
      m_Controls->AppliedArea->enableInput(false);
      m_Controls->AppliedFraction->enableInput(true);
    }
    else
    {
      m_Controls->AppliedArea->enableInput(true);
      m_Controls->AppliedFraction->enableInput(false);
    }
Aaron Bray's avatar
Aaron Bray committed
684 685 686
  }
  else
  {
687
    m_Controls->AppliedFracLast = m_Controls->AppliedFraction->getRadioButton()->isChecked();
Aaron Bray's avatar
Aaron Bray committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
    m_Controls->AppliedTemperature->setDisabled(true);
    m_Controls->AppliedArea->setDisabled(true);
    m_Controls->AppliedFraction->setDisabled(true);
  }
}

void QEnviornmentWidget::EnableAppliedTempSurfaceArea()
{
  bool b = m_Controls->AppliedArea->getRadioButton()->isChecked();
  m_Controls->AppliedArea->enableInput(b);
  m_Controls->AppliedFraction->enableInput(!b);
}

void QEnviornmentWidget::EnableAppliedSurfaceAreaFraction()
{
  bool b = m_Controls->AppliedFraction->getRadioButton()->isChecked();
  m_Controls->AppliedArea->enableInput(!b);
  m_Controls->AppliedFraction->enableInput(b);
706 707 708
}