summaryrefslogtreecommitdiffstats
path: root/Project/SumForm.cpp
blob: 028b00460d4e0bdfd96a1ca50047e5028f006f91 (plain)
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
/*
 *  Copyright (C) 2017  Thales Lima Oliveira <thales@ufu.br>
 *
 *  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 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "SumForm.h"
#include "Sum.h"

SumForm::SumForm(wxWindow* parent, Sum* sum) : SumFormBase(parent)
{
    SetSize(GetBestSize());

    m_parent = parent;
    m_sum = sum;

    wxString signalStr = "";
    auto signalList = m_sum->GetSignalList();
    for(auto it = signalList.begin(), itEnd = signalList.end(); it != itEnd; ++it) {
        Sum::Signal signal = *it;
        switch(signal) {
            case Sum::SIGNAL_POSITIVE: {
                signalStr += "+";
            } break;
            case Sum::SIGNAL_NEGATIVE: {
                signalStr += "-";
            } break;
        }
        if(it != itEnd - 1) signalStr += " ";
    }
    m_textCtrlSigns->SetValue(signalStr);
}

SumForm::~SumForm() {}
void SumForm::OnOKClick(wxCommandEvent& event)
{
    if(ValidateData()) EndModal(wxID_OK);
}

bool SumForm::ValidateData()
{
    wxString signalStr = "";
    for(int i = 0; i < (int)m_textCtrlSigns->GetValue().length(); ++i) {
        if(m_textCtrlSigns->GetValue()[i] != ' ') signalStr += m_textCtrlSigns->GetValue()[i];
    }
    if(signalStr.size() < 2) {
        wxMessageDialog msg(this, _("You must assign at least two signals."), _("Error"),
                            wxOK | wxCENTRE | wxICON_ERROR);
        msg.ShowModal();
        return false;
    }

    std::vector<Sum::Signal> signalList;
    for(int i = 0; i < (int)signalStr.length(); ++i) {
        switch(signalStr[i].GetValue()) {
            case '+': {
                signalList.push_back(Sum::SIGNAL_POSITIVE);
            } break;
            case '-': {
                signalList.push_back(Sum::SIGNAL_NEGATIVE);
            } break;
            default: {
                wxMessageDialog msg(this, _("Value entered incorrectly in the field \"Signs\"."), _("Error"),
                                    wxOK | wxCENTRE | wxICON_ERROR);
                msg.ShowModal();
                return false;
            }
        }
    }

    int diff = (int)signalList.size() - (int)m_sum->GetSignalList().size();

    if(diff < 0) {
        diff = std::abs(diff);
        for(int i = 0; i < diff; ++i) {
            m_sum->RemoveInNode();
        }
    } else if(diff > 0) {
        for(int i = 0; i < diff; ++i) {
            m_sum->AddInNode();
        }
    }
    m_sum->SetSignalList(signalList);
    m_sum->UpdatePoints();
    return true;
}