Power System Platform  2026w11a-beta
Loading...
Searching...
No Matches
MathExpressionForm Class Reference
Inheritance diagram for MathExpressionForm:
Collaboration diagram for MathExpressionForm:

Public Member Functions

 MathExpressionForm (wxWindow *parent, MathExpression *mathExpr)
 

Protected Member Functions

bool ValidateData ()
 
void SetSintaxHighlights ()
 
bool CheckMathExpression ()
 
std::string GetVariablesToParse ()
 
virtual void OnCancelButtonClick (wxCommandEvent &event)
 
virtual void OnCheckButtonClick (wxCommandEvent &event)
 
virtual void OnOKButtonClick (wxCommandEvent &event)
 
virtual void OnTextEnter (wxCommandEvent &event)
 
virtual void OnTextUpdate (wxCommandEvent &event)
 
virtual void OnLeftClickDown (wxMouseEvent &event)
 

Protected Attributes

std::vector< wxString > m_translatedErrorMsg
 
wxWindow * m_parent
 
MathExpressionm_mathExpr
 

Detailed Description

Definition at line 26 of file MathExpressionForm.h.

Constructor & Destructor Documentation

◆ MathExpressionForm()

MathExpressionForm::MathExpressionForm ( wxWindow *  parent,
MathExpression mathExpr 
)

Definition at line 23 of file MathExpressionForm.cpp.

23 : MathExpressionFormBase(parent)
24{
25 m_parent = parent;
26 m_mathExpr = mathExpr;
27
28 // Variables string.
29 wxString variables = "";
30 for(unsigned int i = 0; i < m_mathExpr->GetVariables().size(); ++i) {
31 variables += m_mathExpr->GetVariables()[i] + " ";
32 }
33 variables.RemoveLast();
34
35 // Variables text ctrl.
36 wxFont variablesFont = m_textCtrlVariables->GetFont();
37 variablesFont.SetWeight(wxFONTWEIGHT_BOLD);
38 m_textCtrlVariables->SetFont(variablesFont);
39 m_textCtrlVariables->SetForegroundColour(wxColour(160, 0, 0));
40 m_textCtrlVariables->SetValue(variables);
41
42 m_stcMathExpr->SetText(m_mathExpr->GetMathExpression());
43
44 m_staticTextCheckStatus->SetLabel(_("No checks performed."));
45 SetSintaxHighlights();
46 m_stcMathExpr->SetTabWidth(3);
47
48 // Error indicator
49 m_stcMathExpr->IndicatorSetUnder(ERROR_INDICATOR, true);
50 m_stcMathExpr->IndicatorSetStyle(ERROR_INDICATOR, wxSTC_INDIC_ROUNDBOX);
51 m_stcMathExpr->IndicatorSetAlpha(ERROR_INDICATOR, 200);
52 m_stcMathExpr->IndicatorSetUnder(ERROR_INDICATOR, true);
53 m_stcMathExpr->IndicatorSetForeground(ERROR_INDICATOR, wxColor(255, 85, 0));
54 m_stcMathExpr->SetIndicatorCurrent(ERROR_INDICATOR);
55
56 // Parser error messages to translation.
57 m_translatedErrorMsg.clear();
58 m_translatedErrorMsg.push_back(_("Syntax error"));
59 m_translatedErrorMsg.push_back(_("Mismatched parenthesis"));
60 m_translatedErrorMsg.push_back(_("Missing ')'"));
61 m_translatedErrorMsg.push_back(_("Empty parentheses"));
62 m_translatedErrorMsg.push_back(_("Syntax error: Operator expected"));
63 m_translatedErrorMsg.push_back(_("Not enough memory"));
64 m_translatedErrorMsg.push_back(_("An unexpected error occurred"));
65 m_translatedErrorMsg.push_back(_("Syntax error in input variables"));
66 m_translatedErrorMsg.push_back(_("Illegal number of parameters to function"));
67 m_translatedErrorMsg.push_back(_("Syntax error: Premature end of string"));
68 m_translatedErrorMsg.push_back(_("Syntax error: Expecting ( after function"));
69 m_translatedErrorMsg.push_back(_("Syntax error: Unknown identifier"));
70 m_translatedErrorMsg.push_back(_("No function has been parsed yet"));
71}

◆ ~MathExpressionForm()

MathExpressionForm::~MathExpressionForm ( )
virtual

Definition at line 73 of file MathExpressionForm.cpp.

73{}

Member Function Documentation

◆ CheckMathExpression()

bool MathExpressionForm::CheckMathExpression ( )
protected

Definition at line 158 of file MathExpressionForm.cpp.

159{
160 bool success = true;
161 m_stcMathExpr->IndicatorClearRange(0, m_stcMathExpr->GetValue().length());
162 int currentLang = wxLocale::GetSystemLanguage();
163 wxLocale newLocale(wxLANGUAGE_ENGLISH_US);
164 MathExprParser parser = m_mathExpr->GetParser();
165 int parserRes = parser.Parse(static_cast<std::string>(m_stcMathExpr->GetValue()), GetVariablesToParse());
166 if(parserRes != -1) {
167 m_staticTextCheckStatus->SetLabel(m_translatedErrorMsg[parser.GetParseErrorType()]);
168 m_staticTextCheckStatus->SetForegroundColour(wxColor(255, 0, 0));
169 m_stcMathExpr->IndicatorFillRange(parserRes, 1);
170 success = false;
171 } else {
172 m_staticTextCheckStatus->SetLabel(_("The math expression is correct."));
173 m_staticTextCheckStatus->SetForegroundColour(wxColor(0, 128, 0));
174 }
175 wxLocale oldLocale(currentLang);
176 Layout();
177 return success;
178}

◆ GetVariablesToParse()

std::string MathExpressionForm::GetVariablesToParse ( )
protected

Definition at line 143 of file MathExpressionForm.cpp.

144{
145 wxString variables = wxT("time,step,switch,") + m_textCtrlVariables->GetValue();
146 for(unsigned int i = 0; i < variables.length(); ++i) {
147 if(variables[i] == ' ') variables[i] = ',';
148 }
149 return static_cast<std::string>(variables);
150}

◆ OnCancelButtonClick()

virtual void MathExpressionForm::OnCancelButtonClick ( wxCommandEvent &  event)
inlineprotectedvirtual

Definition at line 36 of file MathExpressionForm.h.

36{ EndModal(wxID_CANCEL); }

◆ OnCheckButtonClick()

void MathExpressionForm::OnCheckButtonClick ( wxCommandEvent &  event)
protectedvirtual

Definition at line 75 of file MathExpressionForm.cpp.

75{ CheckMathExpression(); }

◆ OnLeftClickDown()

void MathExpressionForm::OnLeftClickDown ( wxMouseEvent &  event)
protectedvirtual

Definition at line 152 of file MathExpressionForm.cpp.

153{
154 m_stcMathExpr->IndicatorClearRange(0, m_stcMathExpr->GetValue().length());
155 event.Skip();
156}

◆ OnOKButtonClick()

void MathExpressionForm::OnOKButtonClick ( wxCommandEvent &  event)
protectedvirtual

Definition at line 77 of file MathExpressionForm.cpp.

78{
79 if(ValidateData()) EndModal(wxID_OK);
80}

◆ OnTextEnter()

virtual void MathExpressionForm::OnTextEnter ( wxCommandEvent &  event)
inlineprotectedvirtual

Definition at line 39 of file MathExpressionForm.h.

39{}

◆ OnTextUpdate()

void MathExpressionForm::OnTextUpdate ( wxCommandEvent &  event)
protectedvirtual

Definition at line 141 of file MathExpressionForm.cpp.

141{ SetSintaxHighlights(); }

◆ SetSintaxHighlights()

void MathExpressionForm::SetSintaxHighlights ( )
protected

Definition at line 118 of file MathExpressionForm.cpp.

119{
120 wxString variables = m_textCtrlVariables->GetValue();
121
122 m_stcMathExpr->StyleClearAll();
123 m_stcMathExpr->SetLexer(wxSTC_LEX_DMAP);
124 m_stcMathExpr->StyleSetForeground(wxSTC_DMAP_OPERATOR, wxColour(180, 0, 255));
125 m_stcMathExpr->StyleSetForeground(wxSTC_DMAP_WORD, wxColour(10, 0, 255));
126 m_stcMathExpr->StyleSetForeground(wxSTC_DMAP_WORD2, wxColour(160, 0, 0));
127 // m_stcMathExpr->StyleSetForeground(wxSTC_DMAP_WORD3, wxColour(0, 150, 0));
128 m_stcMathExpr->StyleSetForeground(wxSTC_DMAP_NUMBER, wxColour(0, 150, 0));
129
130 m_stcMathExpr->StyleSetBold(wxSTC_DMAP_WORD, true);
131 m_stcMathExpr->StyleSetBold(wxSTC_DMAP_WORD2, true);
132 m_stcMathExpr->StyleSetBold(wxSTC_DMAP_WORD3, true);
133
134 m_stcMathExpr->SetKeyWords(
135 0, wxT("abs acos acosh arg asin asinh atan atan2 atanh cbrt conj ceil cos cosh cot csc eval exp exp2 floor "
136 "hypot if imag int log log2 log10 max min polar pow real sec sin sinh sqrt tan tanh trunc"));
137 m_stcMathExpr->SetKeyWords(1, wxT("time step switch ") + variables);
138 m_stcMathExpr->SetKeyWords(2, wxT("true false open closed pi e"));
139}

◆ ValidateData()

bool MathExpressionForm::ValidateData ( )
protected

Definition at line 82 of file MathExpressionForm.cpp.

83{
84 if(!CheckMathExpression()) {
85 wxMessageDialog msg(this, _("There is an error on math expression."), _("Error"), wxOK | wxCENTRE | wxICON_ERROR);
86 msg.ShowModal();
87 return false;
88 }
89 wxString rawVariables = m_textCtrlVariables->GetValue();
90 wxString var = "";
91 std::vector<wxString> variables;
92 for(unsigned int i = 0; i < rawVariables.length(); ++i) {
93 if(rawVariables[i] == ' ') {
94 variables.push_back(var);
95 var = "";
96 } else if((i + 1) == static_cast<unsigned int>(rawVariables.length())) {
97 var += rawVariables[i];
98 variables.push_back(var);
99 } else {
100 var += rawVariables[i];
101 }
102 }
103
104 int diff = static_cast<int>(variables.size()) - static_cast<int>(m_mathExpr->GetVariables().size());
105
106 if(diff < 0) {
107 diff = std::abs(diff);
108 for(int i = 0; i < diff; ++i) { m_mathExpr->RemoveInNode(); }
109 } else if(diff > 0) {
110 for(int i = 0; i < diff; ++i) { m_mathExpr->AddInNode(); }
111 }
112 m_mathExpr->SetVariables(variables);
113 m_mathExpr->SetMathExpression(m_stcMathExpr->GetValue());
114 m_mathExpr->UpdatePoints();
115 return true;
116}

Member Data Documentation

◆ m_mathExpr

MathExpression* MathExpressionForm::m_mathExpr
protected

Definition at line 46 of file MathExpressionForm.h.

◆ m_parent

wxWindow* MathExpressionForm::m_parent
protected

Definition at line 45 of file MathExpressionForm.h.

◆ m_translatedErrorMsg

std::vector<wxString> MathExpressionForm::m_translatedErrorMsg
protected

Definition at line 43 of file MathExpressionForm.h.


The documentation for this class was generated from the following files: