-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEquationsDialog.cpp
More file actions
218 lines (192 loc) · 8.99 KB
/
EquationsDialog.cpp
File metadata and controls
218 lines (192 loc) · 8.99 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "wx_pch.h"
#include "EquationsDialog.h"
#include "SimXMain.h"
#include "globals.h"
#ifndef WX_PRECOMP
//(*InternalHeadersPCH(EquationsDialog)
#include <wx/string.h>
#include <wx/intl.h>
//*)
#endif
//(*InternalHeaders(EquationsDialog)
//*)
//(*IdInit(EquationsDialog)
const long EquationsDialog::ID_CHOICE1 = wxNewId();
const long EquationsDialog::ID_STATICTEXT1 = wxNewId();
const long EquationsDialog::ID_TEXTCTRL1 = wxNewId();
const long EquationsDialog::ID_PANEL3 = wxNewId();
const long EquationsDialog::ID_BUTTON1 = wxNewId();
const long EquationsDialog::ID_CHECKLISTBOX1 = wxNewId();
const long EquationsDialog::ID_PANEL4 = wxNewId();
const long EquationsDialog::ID_BUTTON2 = wxNewId();
const long EquationsDialog::ID_BUTTON4 = wxNewId();
const long EquationsDialog::ID_PANEL1 = wxNewId();
const long EquationsDialog::ID_PANEL2 = wxNewId();
//*)
BEGIN_EVENT_TABLE(EquationsDialog,wxDialog)
//(*EventTable(EquationsDialog)
//*)
END_EVENT_TABLE()
EquationsDialog::EquationsDialog(wxWindow* parent,wxWindowID id,const wxPoint& pos,const wxSize& size)
{
//(*Initialize(EquationsDialog)
wxFlexGridSizer* FlexGridSizer1;
wxBoxSizer* BoxSizer3;
wxBoxSizer* BoxSizer2;
wxBoxSizer* BoxSizer4;
wxBoxSizer* BoxSizer1;
wxStdDialogButtonSizer* StdDialogButtonSizer1;
Create(parent, wxID_ANY, _("Equations"), wxDefaultPosition, wxDefaultSize, wxDEFAULT_DIALOG_STYLE, _T("wxID_ANY"));
BoxSizer1 = new wxBoxSizer(wxVERTICAL);
Panel1 = new wxPanel(this, ID_PANEL1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL1"));
FlexGridSizer1 = new wxFlexGridSizer(0, 2, 0, 0);
FlexGridSizer1->AddGrowableCol(0);
Panel3 = new wxPanel(Panel1, ID_PANEL3, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL, _T("ID_PANEL3"));
BoxSizer3 = new wxBoxSizer(wxHORIZONTAL);
Choice1 = new wxChoice(Panel3, ID_CHOICE1, wxDefaultPosition, wxSize(50,-1), 0, 0, 0, wxDefaultValidator, _T("ID_CHOICE1"));
Choice1->SetFocus();
BoxSizer3->Add(Choice1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
StaticText1 = new wxStaticText(Panel3, ID_STATICTEXT1, _("="), wxDefaultPosition, wxDefaultSize, 0, _T("ID_STATICTEXT1"));
BoxSizer3->Add(StaticText1, 0, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
TextCtrl1 = new wxTextCtrl(Panel3, ID_TEXTCTRL1, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_TEXTCTRL1"));
BoxSizer3->Add(TextCtrl1, 3, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel3->SetSizer(BoxSizer3);
BoxSizer3->Fit(Panel3);
BoxSizer3->SetSizeHints(Panel3);
FlexGridSizer1->Add(Panel3, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
btnAddEquation = new wxButton(Panel1, ID_BUTTON1, _("Add"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON1"));
btnAddEquation->Disable();
FlexGridSizer1->Add(btnAddEquation, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel4 = new wxPanel(Panel1, ID_PANEL4, wxDefaultPosition, wxSize(269,112), wxTAB_TRAVERSAL, _T("ID_PANEL4"));
CheckListBox1 = new wxCheckListBox(Panel4, ID_CHECKLISTBOX1, wxDefaultPosition, wxSize(288,240), 0, 0, 0, wxDefaultValidator, _T("ID_CHECKLISTBOX1"));
FlexGridSizer1->Add(Panel4, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 10);
BoxSizer2 = new wxBoxSizer(wxVERTICAL);
btnEditEquation = new wxButton(Panel1, ID_BUTTON2, _("Edit"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON2"));
btnEditEquation->Disable();
BoxSizer2->Add(btnEditEquation, 0, wxTOP|wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
btnRemoveEquation = new wxButton(Panel1, ID_BUTTON4, _("Remove"), wxDefaultPosition, wxDefaultSize, 0, wxDefaultValidator, _T("ID_BUTTON4"));
btnRemoveEquation->Disable();
BoxSizer2->Add(btnRemoveEquation, 0, wxBOTTOM|wxLEFT|wxRIGHT|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
FlexGridSizer1->Add(BoxSizer2, 1, wxALL|wxEXPAND|wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL, 5);
Panel1->SetSizer(FlexGridSizer1);
FlexGridSizer1->Fit(Panel1);
FlexGridSizer1->SetSizeHints(Panel1);
BoxSizer1->Add(Panel1, 1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
Panel2 = new wxPanel(this, ID_PANEL2, wxDefaultPosition, wxDefaultSize, wxDOUBLE_BORDER|wxTAB_TRAVERSAL, _T("ID_PANEL2"));
BoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
BoxSizer4->Add(-1,-1,1, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
StdDialogButtonSizer1 = new wxStdDialogButtonSizer();
StdDialogButtonSizer1->AddButton(new wxButton(Panel2, wxID_OK, wxEmptyString));
StdDialogButtonSizer1->AddButton(new wxButton(Panel2, wxID_HELP, wxEmptyString));
StdDialogButtonSizer1->Realize();
BoxSizer4->Add(StdDialogButtonSizer1, 1, wxALL|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 5);
Panel2->SetSizer(BoxSizer4);
BoxSizer4->Fit(Panel2);
BoxSizer4->SetSizeHints(Panel2);
BoxSizer1->Add(Panel2, 0, wxALL|wxEXPAND|wxALIGN_CENTER_HORIZONTAL|wxALIGN_CENTER_VERTICAL, 0);
SetSizer(BoxSizer1);
BoxSizer1->Fit(this);
BoxSizer1->SetSizeHints(this);
Connect(ID_CHOICE1,wxEVT_COMMAND_CHOICE_SELECTED,(wxObjectEventFunction)&EquationsDialog::OnChoice1Select);
Connect(ID_TEXTCTRL1,wxEVT_COMMAND_TEXT_UPDATED,(wxObjectEventFunction)&EquationsDialog::OnTextCtrl1Text);
Connect(ID_BUTTON1,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&EquationsDialog::OnbtnAddEquationClick);
Connect(ID_BUTTON2,wxEVT_COMMAND_BUTTON_CLICKED,(wxObjectEventFunction)&EquationsDialog::OnbtnEditEquationClick);
//*)
populate();
}
EquationsDialog::~EquationsDialog()
{
//(*Destroy(EquationsDialog)
//*)
}
/// @todo (david#1#) When an equation is edited it *appears* to be applied twice when its
/// graph is displayed. This suggests the original version of the equation hasn't
/// been cleared.
void EquationsDialog::populate()
{
Choice1->Clear();
CheckListBox1->Clear();
if (Expression::haveFreeVariables()) {
// Create a dropdown for the lhs, containing currently undefined variables
vector<string> &freeVars = Expression::getFreeVariables();
for (auto it : freeVars) {
Choice1->Append(s2ws(it));
}
// Set first item as default selection
Choice1->Enable(true);
Choice1->SetSelection(0);
Choice1->SetFocus();
} else {
Choice1->Enable(false);
}
unordered_map<string, Expression*> &definitions = Expression::getDefinitions();
for (auto it : definitions) {
// Specials are now listed separately so don't need to be filtered out.
CheckListBox1->Append(s2ws(it.first) + _(" = ") + s2ws(it.second->getRHS()));
}
btnAddEquation->Enable(!TextCtrl1->GetValue().IsEmpty() && Choice1->GetSelection() != wxNOT_FOUND);
btnEditEquation->Enable(!CheckListBox1->IsEmpty());
}
Expression *EquationsDialog::createEquation(wxString &wsVar, wxString &wsExp)
{
string var = ws2s(wsVar);
string exp = ws2s(wsExp);
string filtered;
char last, c;
for (unsigned int i = 0; i < exp.size(); i++)
{
c = exp[i];
if (last != ' ' || c != ' ' || i == 0) {
last = c;
filtered += c;
}
}
cout << "EquationsDialog::createEquation(" << var << "," << filtered << ")" << endl;
return new Expression(var, filtered);
}
void EquationsDialog::OnbtnAddEquationClick(wxCommandEvent& event)
{
// Create an expression with the LHS (from Choice1) as its name and RHS
// as its value. This should automatically add all the required names to
// the dictionary (via makeToken) so we can then re-populate Choice1.
/// @todo (david#5#) Build a mechanism for extracting equations from the definitions
/// vector and use it to repopulate the equations list (CheckListBox1).
/// Expression::getRHS() will help.
wxString lhs, rhs;
wxASSERT(!Choice1.IsEmpty());
int sel = Choice1->GetSelection();
lhs = Choice1->GetString(sel);
rhs = TextCtrl1->GetValue();
if (rhs.IsEmpty()) {
wxMessageBox(_("Please enter a value for this variable"), _("Error"), wxICON_ERROR);
} else {
createEquation(lhs, rhs.Trim().Trim(false));
populate();
}
}
void EquationsDialog::OnChoice1Select(wxCommandEvent& event)
{
btnAddEquation->Enable(!TextCtrl1->GetValue().IsEmpty());
}
void EquationsDialog::OnTextCtrl1Text(wxCommandEvent& event)
{
btnAddEquation->Enable(!TextCtrl1->GetValue().IsEmpty() && Choice1->GetSelection() != wxNOT_FOUND);
}
void EquationsDialog::OnbtnEditEquationClick(wxCommandEvent& event)
{
int sel = CheckListBox1->GetSelection();
// This parses out the variable name and predicate assuming is was created
// using the procedure in populate(). If populate is changed this must be
// updated to match
wxString str = CheckListBox1->GetString(sel);
CheckListBox1->Delete(sel);
int pos = str.rfind(_(" = "));
wxString var = str.Mid(0, pos);
wxString rhs = str.Mid(pos + 3);
// Find the definition for this equation
Expression::find(ws2s(var));
Choice1->Clear();
Choice1->Append(var);
Choice1->SetSelection(0);
TextCtrl1->SetValue(rhs);
}