Power System Platform  2026w10a-beta
Loading...
Searching...
No Matches
SumForm.cpp
1/*
2 * Copyright (C) 2017 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "SumForm.h"
19#include "../elements/controlElement/Sum.h"
20
21SumForm::SumForm(wxWindow* parent, Sum* sum) : SumFormBase(parent)
22{
23 SetSize(GetBestSize());
24
25 m_parent = parent;
26 m_sum = sum;
27
28 wxString signalStr = "";
29 auto signalList = m_sum->GetSignalList();
30 for(auto it = signalList.begin(), itEnd = signalList.end(); it != itEnd; ++it) {
31 Sum::Signal signal = *it;
32 switch(signal) {
33 case Sum::SIGNAL_POSITIVE: {
34 signalStr += "+";
35 } break;
36 case Sum::SIGNAL_NEGATIVE: {
37 signalStr += "-";
38 } break;
39 }
40 if(it != itEnd - 1) signalStr += " ";
41 }
42 m_textCtrlSigns->SetValue(signalStr);
43}
44
45SumForm::~SumForm() {}
46void SumForm::OnOKClick(wxCommandEvent& event)
47{
48 if(ValidateData()) EndModal(wxID_OK);
49}
50
51bool SumForm::ValidateData()
52{
53 wxString signalStr = "";
54 for(int i = 0; i < (int)m_textCtrlSigns->GetValue().length(); ++i) {
55 if(m_textCtrlSigns->GetValue()[i] != ' ') signalStr += m_textCtrlSigns->GetValue()[i];
56 }
57 if(signalStr.size() < 2) {
58 wxMessageDialog msg(this, _("You must assign at least two signals."), _("Error"),
59 wxOK | wxCENTRE | wxICON_ERROR);
60 msg.ShowModal();
61 return false;
62 }
63
64 std::vector<Sum::Signal> signalList;
65 for(int i = 0; i < (int)signalStr.length(); ++i) {
66 switch(signalStr[i].GetValue()) {
67 case '+': {
68 signalList.push_back(Sum::SIGNAL_POSITIVE);
69 } break;
70 case '-': {
71 signalList.push_back(Sum::SIGNAL_NEGATIVE);
72 } break;
73 default: {
74 wxMessageDialog msg(this, _("Value entered incorrectly in the field \"Signs\"."), _("Error"),
75 wxOK | wxCENTRE | wxICON_ERROR);
76 msg.ShowModal();
77 return false;
78 }
79 }
80 }
81
82 int diff = (int)signalList.size() - (int)m_sum->GetSignalList().size();
83
84 if(diff < 0) {
85 diff = std::abs(diff);
86 for(int i = 0; i < diff; ++i) {
87 m_sum->RemoveInNode();
88 }
89 } else if(diff > 0) {
90 for(int i = 0; i < diff; ++i) {
91 m_sum->AddInNode();
92 }
93 }
94 m_sum->SetSignalList(signalList);
95 m_sum->UpdatePoints();
96 return true;
97}
Sum the all inputs (can choose the input signal).
Definition Sum.h:34