<commit_before>// This is free and unencumbered software released into the public domain.
// For more information, please refer to <http://unlicense.org/>

#include "weather_load_case_manager_dialog.h"

#include "wx/spinbutt.h"
#include "wx/xrc/xmlres.h"

#include "span_analyzer_app.h"
#include "span_analyzer_doc.h"
#include "weather_load_case_editor_dialog.h"
#include "weather_load_case_unit_converter.h"

BEGIN_EVENT_TABLE(WeatherLoadCaseManagerDialog, wxDialog)
  EVT_BUTTON(XRCID("button_groups_add"), WeatherLoadCaseManagerDialog::OnButtonGroupAdd)
  EVT_BUTTON(XRCID("button_groups_delete"), WeatherLoadCaseManagerDialog::OnButtonGroupDelete)
  EVT_BUTTON(XRCID("button_weathercases_add"), WeatherLoadCaseManagerDialog::OnButtonWeathercaseAdd)
  EVT_BUTTON(XRCID("button_weathercases_delete"), WeatherLoadCaseManagerDialog::OnButtonWeathercaseDelete)
  EVT_BUTTON(wxID_CANCEL, WeatherLoadCaseManagerDialog::OnCancel)
  EVT_BUTTON(wxID_OK, WeatherLoadCaseManagerDialog::OnOk)
  EVT_CLOSE(WeatherLoadCaseManagerDialog::OnClose)
  EVT_LISTBOX(XRCID("listbox_groups"), WeatherLoadCaseManagerDialog::OnListBoxGroupSelect)
  EVT_LISTBOX_DCLICK(XRCID("listbox_groups"), WeatherLoadCaseManagerDialog::OnListBoxGroupDoubleClick)
  EVT_LISTBOX_DCLICK(XRCID("listbox_weathercases"), WeatherLoadCaseManagerDialog::OnListBoxWeatherCaseDoubleClick)
  EVT_SPIN_DOWN(XRCID("spinbutton_groups"), WeatherLoadCaseManagerDialog::OnSpinButtonGroupDown)
  EVT_SPIN_DOWN(XRCID("spinbutton_weathercases"), WeatherLoadCaseManagerDialog::OnSpinButtonWeathercaseDown)
  EVT_SPIN_UP(XRCID("spinbutton_groups"), WeatherLoadCaseManagerDialog::OnSpinButtonGroupUp)
  EVT_SPIN_UP(XRCID("spinbutton_weathercases"), WeatherLoadCaseManagerDialog::OnSpinButtonWeathercaseUp)
END_EVENT_TABLE()

WeatherLoadCaseManagerDialog::WeatherLoadCaseManagerDialog(
    wxWindow* parent,
    const units::UnitSystem& units,
    std::list<WeatherLoadCaseGroup>* groups_weathercase) {
  // loads dialog from virtual xrc file system
  wxXmlResource::Get()->LoadDialog(this, parent,
                                   "weather_load_case_manager_dialog");

  // gets listbox references
  listbox_groups_ = XRCCTRL(*this, "listbox_groups", wxListBox);
  listbox_weathercases_ = XRCCTRL(*this, "listbox_weathercases", wxListBox);

  // saves references to data
  groups_ = groups_weathercase;
  units_ = &units;

  // makes copies so user can modify
  groups_modified_ = *groups_;

  // fills the listboxes with the set descriptions
  wxListBox* listbox = nullptr;
  listbox = XRCCTRL(*this, "listbox_groups", wxListBox);
  for (auto iter = groups_modified_.cbegin();
       iter != groups_modified_.cend(); iter++) {
    const WeatherLoadCaseGroup& group = *iter;
    listbox->Append(group.name);
  }

  // fits the dialog around the sizers
  this->Fit();
}

WeatherLoadCaseManagerDialog::~WeatherLoadCaseManagerDialog() {
}

bool WeatherLoadCaseManagerDialog::IsReferencedByDocument(
    const std::string& name) const {
  // gets document
  const SpanAnalyzerDoc* doc =
      (SpanAnalyzerDoc*)wxGetApp().manager_doc()->GetCurrentDocument();
  if (doc == nullptr) {
    return false;
  }

  // scans all spans to see if weathercase is referenced
  for (auto iter = doc->spans().cbegin(); iter != doc->spans().cend(); iter++) {
    const Span& span = *iter;

    if (span.linecable.constraint.case_weather->description == name) {
      return true;
    }

    if (span.linecable.weathercase_stretch_creep->description == name) {
      return true;
    }

    if (span.linecable.weathercase_stretch_load->description == name) {
      return true;
    }
  }

  return false;
}

bool WeatherLoadCaseManagerDialog::IsUniqueName(const std::string& name,
                                                const int& index_ignore) const {
  if (index_group_activated_ < 0) {
    return false;
  }

  const WeatherLoadCaseGroup& group =
      *(std::next(groups_modified_.cbegin(), index_group_activated_));

  // scans all weathercases to see if the name is a duplicate
  for (auto iter = group.weathercases.cbegin();
       iter != group.weathercases.cend(); iter++) {
    const WeatherLoadCase& weathercase = *iter;

    const int index = std::distance(group.weathercases.cbegin(), iter);
    if (index == index_ignore) {
      continue;
    } else {
      if (weathercase.description == name) {
        return false;
      }
    }
  }

  return true;
}

void WeatherLoadCaseManagerDialog::OnButtonGroupAdd(
    wxCommandEvent& event) {
  // gets weathercase group name from user
  std::string name = wxGetTextFromUser("Enter a weathercase group name",
                                       "Weathercase Group Name");
  if (name.empty() == true) {
    return;
  }

  // adds to the group list
  WeatherLoadCaseGroup group;
  groups_modified_.push_back(group);

  // updates the listbox
  listbox_groups_->Append(group.name);
}

void WeatherLoadCaseManagerDialog::OnButtonGroupDelete(
    wxCommandEvent& event) {
  if (index_group_activated_ == wxNOT_FOUND) {
    // exits if nothing is selected
    return;
  } else if (index_group_activated_ == 0) {<commit_msg>Bug fix when adding weather case groups.<commit_after>// This is free and unencumbered software released into the public domain.
// For more information, please refer to <http://unlicense.org/>

#include "weather_load_case_manager_dialog.h"

#include "wx/spinbutt.h"
#include "wx/xrc/xmlres.h"

#include "span_analyzer_app.h"
#include "span_analyzer_doc.h"
#include "weather_load_case_editor_dialog.h"
#include "weather_load_case_unit_converter.h"

BEGIN_EVENT_TABLE(WeatherLoadCaseManagerDialog, wxDialog)
  EVT_BUTTON(XRCID("button_groups_add"), WeatherLoadCaseManagerDialog::OnButtonGroupAdd)
  EVT_BUTTON(XRCID("button_groups_delete"), WeatherLoadCaseManagerDialog::OnButtonGroupDelete)
  EVT_BUTTON(XRCID("button_weathercases_add"), WeatherLoadCaseManagerDialog::OnButtonWeathercaseAdd)
  EVT_BUTTON(XRCID("button_weathercases_delete"), WeatherLoadCaseManagerDialog::OnButtonWeathercaseDelete)
  EVT_BUTTON(wxID_CANCEL, WeatherLoadCaseManagerDialog::OnCancel)
  EVT_BUTTON(wxID_OK, WeatherLoadCaseManagerDialog::OnOk)
  EVT_CLOSE(WeatherLoadCaseManagerDialog::OnClose)
  EVT_LISTBOX(XRCID("listbox_groups"), WeatherLoadCaseManagerDialog::OnListBoxGroupSelect)
  EVT_LISTBOX_DCLICK(XRCID("listbox_groups"), WeatherLoadCaseManagerDialog::OnListBoxGroupDoubleClick)
  EVT_LISTBOX_DCLICK(XRCID("listbox_weathercases"), WeatherLoadCaseManagerDialog::OnListBoxWeatherCaseDoubleClick)
  EVT_SPIN_DOWN(XRCID("spinbutton_groups"), WeatherLoadCaseManagerDialog::OnSpinButtonGroupDown)
  EVT_SPIN_DOWN(XRCID("spinbutton_weathercases"), WeatherLoadCaseManagerDialog::OnSpinButtonWeathercaseDown)
  EVT_SPIN_UP(XRCID("spinbutton_groups"), WeatherLoadCaseManagerDialog::OnSpinButtonGroupUp)
  EVT_SPIN_UP(XRCID("spinbutton_weathercases"), WeatherLoadCaseManagerDialog::OnSpinButtonWeathercaseUp)
END_EVENT_TABLE()

WeatherLoadCaseManagerDialog::WeatherLoadCaseManagerDialog(
    wxWindow* parent,
    const units::UnitSystem& units,
    std::list<WeatherLoadCaseGroup>* groups_weathercase) {
  // loads dialog from virtual xrc file system
  wxXmlResource::Get()->LoadDialog(this, parent,
                                   "weather_load_case_manager_dialog");

  // gets listbox references
  listbox_groups_ = XRCCTRL(*this, "listbox_groups", wxListBox);
  listbox_weathercases_ = XRCCTRL(*this, "listbox_weathercases", wxListBox);

  // saves references to data
  groups_ = groups_weathercase;
  units_ = &units;

  // makes copies so user can modify
  groups_modified_ = *groups_;

  // fills the listboxes with the set descriptions
  wxListBox* listbox = nullptr;
  listbox = XRCCTRL(*this, "listbox_groups", wxListBox);
  for (auto iter = groups_modified_.cbegin();
       iter != groups_modified_.cend(); iter++) {
    const WeatherLoadCaseGroup& group = *iter;
    listbox->Append(group.name);
  }

  // fits the dialog around the sizers
  this->Fit();
}

WeatherLoadCaseManagerDialog::~WeatherLoadCaseManagerDialog() {
}

bool WeatherLoadCaseManagerDialog::IsReferencedByDocument(
    const std::string& name) const {
  // gets document
  const SpanAnalyzerDoc* doc =
      (SpanAnalyzerDoc*)wxGetApp().manager_doc()->GetCurrentDocument();
  if (doc == nullptr) {
    return false;
  }

  // scans all spans to see if weathercase is referenced
  for (auto iter = doc->spans().cbegin(); iter != doc->spans().cend(); iter++) {
    const Span& span = *iter;

    if (span.linecable.constraint.case_weather->description == name) {
      return true;
    }

    if (span.linecable.weathercase_stretch_creep->description == name) {
      return true;
    }

    if (span.linecable.weathercase_stretch_load->description == name) {
      return true;
    }
  }

  return false;
}

bool WeatherLoadCaseManagerDialog::IsUniqueName(const std::string& name,
                                                const int& index_ignore) const {
  if (index_group_activated_ < 0) {
    return false;
  }

  const WeatherLoadCaseGroup& group =
      *(std::next(groups_modified_.cbegin(), index_group_activated_));

  // scans all weathercases to see if the name is a duplicate
  for (auto iter = group.weathercases.cbegin();
       iter != group.weathercases.cend(); iter++) {
    const WeatherLoadCase& weathercase = *iter;

    const int index = std::distance(group.weathercases.cbegin(), iter);
    if (index == index_ignore) {
      continue;
    } else {
      if (weathercase.description == name) {
        return false;
      }
    }
  }

  return true;
}

void WeatherLoadCaseManagerDialog::OnButtonGroupAdd(
    wxCommandEvent& event) {
  // gets weathercase group name from user
  std::string name = wxGetTextFromUser("Enter a weathercase group name",
                                       "Weathercase Group Name");
  if (name.empty() == true) {
    return;
  }

  // adds to the group list
  WeatherLoadCaseGroup group;
  group.name = name;
  groups_modified_.push_back(group);

  // updates the listbox
  listbox_groups_->Append(group.name);
}

void WeatherLoadCaseManagerDialog::OnButtonGroupDelete(
    wxCommandEvent& event) {
  if (index_group_activated_ == wxNOT_FOUND) {
    // exits if nothing is selected
    return;
  } else if (index_group_activated_ == 0) {
{'fraction_non_alphanumeric': 0.10414638894296283, 'fraction_numerical': 0.0003893322951138797, 'mean_word_length': 4.676795580110498, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3386029', 'n_tokens_mistral': 3280, 'n_tokens_neox': 3140, 'n_words': 620}