-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGUIMyFrame1.cpp
More file actions
395 lines (316 loc) · 9.03 KB
/
GUIMyFrame1.cpp
File metadata and controls
395 lines (316 loc) · 9.03 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "GUIMyFrame1.h"
GUIMyFrame1::GUIMyFrame1(wxWindow* parent)
:
MyFrame1(parent)
{
panelWidth = panelNaWykres->GetSize().GetWidth();
panelHeight = panelNaWykres->GetSize().GetHeight();
mouseX = panelWidth / 2;
mouseY = panelHeight / 2;
}
void GUIMyFrame1::showInfo(wxMouseEvent& event)
{
wxMessageDialog* infoDialog = new wxMessageDialog(this, "-> use only x and y variables\n-> use \"sqrt()\" as aquare root\n->use \"^n\" to raise to the n-th power\n-> use \".\" to create floating-point numbers\n-> you can use constants \"pi\" and \"e\"\n-> sin(), cos() and tan() are available to be used too",
"How to plot?", wxOK | wxICON_INFORMATION);
infoDialog->ShowModal();
infoDialog->Destroy();
}
bool GUIMyFrame1::checkFunction()
{
double x, y;
te_variable vars[] = { {"x", &x}, {"y", &y} };
const char* c = function.c_str();
int err;
te_expr* expr = te_compile(c, vars, 2, &err);
if (!expr) {
textCtrlFunkcja->SetValue("");
return false;
}
if (!checkNumbers()) {
return false;
}
int sample = 50;
double move = std::max(((xMax - xMin) / sample), (yMax - yMin) / sample);
double movex = std::min((xMax - xMin), move);
double movey = std::min((yMax - yMin), move);
zValuesVec.clear();
vector < double > tempVec;
for (double xi = xMin; xi <= (xMax); xi += movex) {
for (double yi = yMin; yi <= (yMax); yi += movey) {
x = xi;
y = yi;
tempVec.push_back(te_eval(expr));
}
zValuesVec.push_back(tempVec);
tempVec.clear();
}
return true;
}
bool GUIMyFrame1::checkNumbers()
{
try {
xMin = stod(textCtrlXMin->GetValue().ToStdString());
textCtrlXMin->SetValue(to_string(xMin));
}
catch (const std::invalid_argument&) {
textCtrlXMin->SetValue("");
return false;
}
catch (const std::out_of_range&) {
textCtrlXMin->SetValue("");
return false;
}
try {
xMax = stod(textCtrlXMax->GetValue().ToStdString());
textCtrlXMax->SetValue(to_string(xMax));
}
catch (const std::invalid_argument&) {
textCtrlXMax->SetValue("");
return false;
}
catch (const std::out_of_range&) {
textCtrlXMax->SetValue("");
return false;
}
try {
yMin = stod(textCtrlYMin->GetValue().ToStdString());
textCtrlYMin->SetValue(to_string(yMin));
}
catch (const std::invalid_argument&) {
textCtrlYMin->SetValue("");
return false;
}
catch (const std::out_of_range&) {
textCtrlYMin->SetValue("");
return false;
}
try {
yMax = stod(textCtrlYMax->GetValue().ToStdString());
textCtrlYMax->SetValue(to_string(yMax));
}
catch (const std::invalid_argument&) {
textCtrlYMax->SetValue("");
return false;
}
catch (const std::out_of_range&) {
textCtrlYMax->SetValue("");
return false;
}
try {
zMin = stod(textCtrlZMin->GetValue().ToStdString());
textCtrlZMin->SetValue(to_string(zMin));
}
catch (const std::invalid_argument&) {
textCtrlZMin->SetValue("");
return false;
}
catch (const std::out_of_range&) {
textCtrlZMin->SetValue("");
return false;
}
try {
zMax = stod(textCtrlZMax->GetValue().ToStdString());
textCtrlZMax->SetValue(to_string(zMax));
}
catch (const std::invalid_argument&) {
textCtrlZMax->SetValue("");
return false;
}
catch (const std::out_of_range&) {
textCtrlZMax->SetValue("");
return false;
}
if (xMax <= xMin) {
textCtrlXMax->SetValue("");
textCtrlXMin->SetValue("");
return false;
}
if (yMax <= yMin) {
textCtrlYMax->SetValue("");
textCtrlYMin->SetValue("");
return false;
}
if (zMax <= zMin) {
textCtrlZMax->SetValue("");
textCtrlZMin->SetValue("");
return false;
}
return true;
}
void GUIMyFrame1::perspectiveClick(wxMouseEvent& event)
{
inPerspective = true;
inMap = false;
mouseRotateX = 0;
mouseRotateY = 0;
if (radioRzut->GetValue()) {
radioMapa->SetValue(false);
//generujemy wykres!
if (generateClicked) repaint();
}
else {
radioRzut->SetValue(true);
}
}
void GUIMyFrame1::outlineClick(wxMouseEvent& event)
{
inPerspective = false;
inMap = true;
mouseRotateX = 0;
mouseRotateY = 0;
if (radioMapa->GetValue()) {
radioRzut->SetValue(false);
//generujemy wykres!
if (generateClicked) repaint();
}
else {
radioMapa->SetValue(true);
}
}
void GUIMyFrame1::printClick(wxMouseEvent& event)
{
wxClientDC dc(panelNaWykres);
wxSize size = panelNaWykres->GetSize();
wxBitmap bitmap(size);
wxMemoryDC memDC;
memDC.SelectObject(bitmap);
memDC.Blit(0, 0, size.GetWidth(), size.GetHeight(), &dc, 0, 0);
memDC.SelectObject(wxNullBitmap);
wxImage printPicture = bitmap.ConvertToImage();
printPicture.AddHandler(new wxJPEGHandler);
printPicture.AddHandler(new wxPNGHandler);
wxPrinter printer;
ImagePrintout printout("Image Printout", printPicture);
if (!printer.Print(this, &printout, true)) {
if (wxPrinter::GetLastError() == wxPRINTER_ERROR) {
wxMessageBox("There was a problem with printing.", "Error", wxOK);
}
else {
wxMessageBox("The printing was cancelled.", "Info", wxOK);
}
}
}
void GUIMyFrame1::saveClick(wxMouseEvent& event)
{
wxFileDialog saveFileDialog(this, "Choose a file", "", "", "PNG files (*.png)|*.png", wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if (saveFileDialog.ShowModal() == wxID_CANCEL) return;
wxClientDC dc(panelNaWykres);
wxSize size = panelNaWykres->GetSize();
wxBitmap bitmap(size);
wxMemoryDC memDC;
memDC.SelectObject(bitmap);
memDC.Blit(0, 0, size.GetWidth(), size.GetHeight(), &dc, 0, 0);
memDC.SelectObject(wxNullBitmap);
wxImage savePicture = bitmap.ConvertToImage();
savePicture.AddHandler(new wxJPEGHandler);
savePicture.AddHandler(new wxPNGHandler);
savePicture.SaveFile(saveFileDialog.GetPath());
}
void GUIMyFrame1::generateClick(wxMouseEvent& event)
{
function = textCtrlFunkcja->GetValue();
mouseRotateX = 0;
mouseRotateY = 0;
scrollCounter = 0;
if (!checkFunction()) return;
repaint();
generateClicked = true;
}
void GUIMyFrame1::panelRepaint(wxSizeEvent& event)
{
if ((panelWidth != panelNaWykres->GetSize().GetWidth() || panelHeight != panelNaWykres->GetSize().GetHeight()) && generateClicked) {
panelWidth = panelNaWykres->GetSize().GetWidth();
panelHeight = panelNaWykres->GetSize().GetHeight();
repaint();
return;
}
panelWidth = panelNaWykres->GetSize().GetWidth();
panelHeight = panelNaWykres->GetSize().GetHeight();
}
void GUIMyFrame1::repaint()
{
//gerneruj wykres
if (isPerspective()) {
//generuj rzut perspektywiczny
objPer.getAxis(xMin, xMax, yMin, yMax, zMin, zMax);
objPer.RecountFunctionIntoData(zValuesVec);
objPer.Repaint(panelNaWykres, panelWidth, panelHeight, mouseRotateX, mouseRotateY, scrollCounter);
}
else {
//generuj mape konturowa
objMap.getRanges(xMin, xMax, yMin, yMax, zMin, zMax);
objMap.prepareData(zValuesVec, panelWidth, panelHeight, function);
objMap.repaint(panelNaWykres, panelWidth, panelHeight);
}
}
void GUIMyFrame1::onMouseEnter(wxMouseEvent& event)
{
movingAllowed = true;
}
void GUIMyFrame1::onMouseMove(wxMouseEvent& event)
{
if (!movingAllowed || isMap() || !leftPressed) return;
if (mouseX == -10000 && mouseY == -10000) {
mouseX = event.GetPosition().x;
mouseY = event.GetPosition().y;
return;
}
int newMouseX = event.GetPosition().x;
int newMouseY = event.GetPosition().y;
double rotateX = (double)(newMouseX - mouseX) / (double)panelWidth * 180;
double rotateY = (double)(newMouseY - mouseY) / (double)panelHeight * 180;
//tutaj przekazujemy do perspectivic i rysujemy
if (abs(rotateX - lastRotateX) > M_PI / 100 || abs(rotateY - lastRotateY) > M_PI / 100) {
lastRotateX = rotateX;
lastRotateY = rotateY;
objPer.Repaint(panelNaWykres, panelWidth, panelHeight, mouseRotateX + rotateX, mouseRotateY + rotateY, scrollCounter);
}
}
void GUIMyFrame1::onMouseLeave(wxMouseEvent& event)
{
movingAllowed = false;
int newMouseX = event.GetPosition().x;
int newMouseY = event.GetPosition().y;
double rotateX = (double)(newMouseX - mouseX) / (double)panelWidth * 180;
double rotateY = (double)(newMouseY - mouseY) / (double)panelHeight * 180;
leftPressed = false;
mouseX = -10000;
mouseY = -10000;
lastRotateX = 0;
lastRotateY = 0;
mouseRotateX += rotateX;
mouseRotateY += rotateY;
mouseRotateX -= (double)((int)mouseRotateX / 360) * 360;
mouseRotateY -= (double)((int)mouseRotateY / 360) * 360;
}
void GUIMyFrame1::onLeftMouseDown(wxMouseEvent& event)
{
leftPressed = true;
}
void GUIMyFrame1::onLeftMouseUp(wxMouseEvent& event)
{
leftPressed = false;
int newMouseX = event.GetPosition().x;
int newMouseY = event.GetPosition().y;
double rotateX = (double)(newMouseX - mouseX) / (double)panelWidth * 180;
double rotateY = (double)(newMouseY - mouseY) / (double)panelHeight * 180;
mouseX = -10000;
mouseY = -10000;
lastRotateX = 0;
lastRotateY = 0;
mouseRotateX += rotateX;
mouseRotateY += rotateY;
mouseRotateX -= (double)((int)mouseRotateX / 360) * 360;
mouseRotateY -= (double)((int)mouseRotateY / 360) * 360;
}
void GUIMyFrame1::onMouseScroll(wxMouseEvent& event)
{
if ( isMap() || !movingAllowed ) return;
int rotation = event.GetWheelRotation();
int delta = event.GetWheelDelta();
int linesPerAction = event.GetLinesPerAction();
double scrollAmount = (double)rotation / delta;
scrollCounter+= scrollAmount;
scrollCounter = std::min(std::max(scrollCounter, -50), 11);
repaint();
}