forked from seandepagnier/weather_routing_pi
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathConfigurationDialog.cpp
More file actions
765 lines (652 loc) · 29.6 KB
/
Copy pathConfigurationDialog.cpp
File metadata and controls
765 lines (652 loc) · 29.6 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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/***************************************************************************
*
* Project: OpenCPN Weather Routing plugin
* Author: Sean D'Epagnier
*
***************************************************************************
* Copyright (C) 2015 by Sean D'Epagnier *
* *
* 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 3 of the License, or *
* (at your option) 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, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************
*
*/
#include <wx/wx.h>
#include <stdlib.h>
#include <math.h>
#include "Boat.h"
#include "RouteMapOverlay.h"
#include "ConfigurationDialog.h"
#include "BoatDialog.h"
#include "weather_routing_pi.h"
#include "WeatherRouting.h"
#include <algorithm>
ConfigurationDialog::ConfigurationDialog(WeatherRouting& weatherrouting)
#ifndef __WXOSX__
: ConfigurationDialogBase(&weatherrouting),
#else
: ConfigurationDialogBase(&weatherrouting, wxID_ANY,
_("Weather Routing Configuration"),
wxDefaultPosition, wxDefaultSize,
wxDEFAULT_DIALOG_STYLE | wxSTAY_ON_TOP),
#endif
m_WeatherRouting(weatherrouting),
m_bBlockUpdate(false) {
wxFileConfig* pConf = GetOCPNConfigObject();
pConf->SetPath(_T( "/PlugIns/WeatherRouting" ));
#ifdef __OCPN__ANDROID__
wxSize sz = ::wxGetDisplaySize();
SetSize(0, 0, sz.x, sz.y - 40);
#else
wxPoint p = GetPosition();
pConf->Read(_T ( "ConfigurationX" ), &p.x, p.x);
pConf->Read(_T ( "ConfigurationY" ), &p.y, p.y);
SetPosition(p);
#endif
}
ConfigurationDialog::~ConfigurationDialog() {
wxFileConfig* pConf = GetOCPNConfigObject();
pConf->SetPath(_T( "/PlugIns/WeatherRouting" ));
wxPoint p = GetPosition();
pConf->Write(_T ( "ConfigurationX" ), p.x);
pConf->Write(_T ( "ConfigurationY" ), p.y);
}
void ConfigurationDialog::EditBoat() {
m_WeatherRouting.m_BoatDialog.LoadPolar(m_tBoat->GetValue());
m_WeatherRouting.m_BoatDialog.Show();
}
void ConfigurationDialog::OnGribTime(wxCommandEvent& event) {
SetStartDateTime(m_GribTimelineTime);
Update();
}
void ConfigurationDialog::OnCurrentTime(wxCommandEvent& event) {
SetStartDateTime(wxDateTime::Now()); // Local time converted to UTC
Update();
}
void ConfigurationDialog::OnUseCurrentTime(wxCommandEvent& event) {
m_dpStartDate->Enable(!m_cbUseCurrentTime->IsChecked());
m_tpTime->Enable(!m_cbUseCurrentTime->IsChecked());
m_bGribTime->Enable(!m_cbUseCurrentTime->IsChecked());
m_bCurrentTime->Enable(!m_cbUseCurrentTime->IsChecked());
Update();
}
void ConfigurationDialog::OnStartFromBoat(wxCommandEvent& event) {
m_cStart->Enable(!m_rbStartFromBoat->GetValue());
Update();
}
void ConfigurationDialog::OnStartFromPosition(wxCommandEvent& event) {
AddPositions(true);
m_cStart->Enable(m_rbStartPositionSelection->GetValue());
Update();
}
void ConfigurationDialog::OnStartFromWaypoint(wxCommandEvent& event) {
AddWaypoints(true);
m_cStart->Enable(m_rbStartWaypointSelection->GetValue());
Update();
}
void ConfigurationDialog::OnEndAtPosition(wxCommandEvent& event) {
AddPositions(false);
m_cStart->Enable(m_rbStartPositionSelection->GetValue());
Update();
}
void ConfigurationDialog::OnEndAtWaypoint(wxCommandEvent& event) {
AddWaypoints(false);
m_cStart->Enable(m_rbStartWaypointSelection->GetValue());
Update();
}
void ConfigurationDialog::OnAvoidCyclones(wxCommandEvent& event) { Update(); }
void ConfigurationDialog::OnUseMotor(wxCommandEvent& event) {
// Enable/disable motor controls based on checkbox state
bool useMotor = m_cbUseMotor->IsChecked();
m_sMotorSpeedThreshold->Enable(useMotor);
m_sMotorSpeed->Enable(useMotor);
Update();
}
void ConfigurationDialog::OnUseOptimalAngles(wxCommandEvent& event) {
bool use_optimal_angles = m_cbUseOptimalAngles->IsChecked();
if (use_optimal_angles) {
m_sFromDegree->SetValue(0.0);
m_sToDegree->SetValue(180.0);
m_edited_controls.push_back(m_sFromDegree);
m_edited_controls.push_back(m_sToDegree);
}
m_sFromDegree->Enable(!use_optimal_angles);
m_sToDegree->Enable(!use_optimal_angles);
Update();
}
void ConfigurationDialog::OnBoatFilename(wxCommandEvent& event) {
wxFileDialog openDialog(
this, _("Select Boat File"), wxFileName(m_tBoat->GetValue()).GetPath(),
wxT(""), wxT("xml (*.xml)|*.XML;*.xml|All files (*.*)|*.*"), wxFD_OPEN);
if (openDialog.ShowModal() == wxID_OK) SetBoatFilename(openDialog.GetPath());
}
// --- Macros for filling controls from configurations --------------------------------
// Control helper used by many SET_* macros
#define SET_CONTROL_VALUE(VALUE, CONTROL, SETTER, TYPE, NULLVALUE) \
do { \
bool allsame = true; \
std::list<RouteMapConfiguration>::iterator it = configurations.begin(); \
TYPE value = VALUE; \
for (it++; it != configurations.end(); ++it) { \
if (value != VALUE) { \
allsame = false; \
break; \
} \
} \
CONTROL->SETTER(allsame ? value : NULLVALUE); \
wxSize s(CONTROL->GetSize()); \
if (allsame) \
CONTROL->SetForegroundColour(wxColour(0, 0, 0)); \
else \
CONTROL->SetForegroundColour(wxColour(180, 180, 180)); \
CONTROL->Fit(); \
CONTROL->SetSize(s); \
} while (0)
#define SET_CONTROL(FIELD, CONTROL, SETTER, TYPE, NULLVALUE) \
SET_CONTROL_VALUE((*it).FIELD, CONTROL, SETTER, TYPE, NULLVALUE)
#define SET_CHOICE_VALUE(FIELD, VALUE) \
do { \
bool allsame = true; \
std::list<RouteMapConfiguration>::iterator it = configurations.begin(); \
wxString value = VALUE; \
for (it++; it != configurations.end(); ++it) { \
if (value != VALUE) { \
allsame = false; \
break; \
} \
} \
if (allsame) \
m_c##FIELD->SetValue(value); \
else { \
if (m_c##FIELD->GetString(m_c##FIELD->GetCount() - 1) != wxEmptyString) \
m_c##FIELD->Append(wxEmptyString); \
m_c##FIELD->SetValue(wxEmptyString); \
} \
} while (0)
#define SET_CHOICE(FIELD) SET_CHOICE_VALUE(FIELD, (*it).FIELD)
#define SET_SPIN_VALUE(FIELD, VALUE) \
SET_CONTROL_VALUE(VALUE, m_s##FIELD, SetValue, int, value)
#define SET_SPIN(FIELD) SET_SPIN_VALUE(FIELD, (*it).FIELD)
#define SET_SPIN_DOUBLE_VALUE(FIELD, VALUE) \
SET_CONTROL_VALUE(VALUE, m_s##FIELD, SetValue, double, value)
#define SET_SPIN_DOUBLE(FIELD) SET_SPIN_DOUBLE_VALUE(FIELD, (*it).FIELD)
// Safer checkbox macros: use internal wr_cfg loop and safe fallback for 2-state
#define SET_CHECKBOX_FIELD(FIELD, VALUE_EXPR) \
do { \
bool wr_alltrue = true, wr_allfalse = true; \
for (const auto& wr_cfg : configurations) { \
if (VALUE_EXPR) \
wr_allfalse = false; \
else \
wr_alltrue = false; \
} \
wxCheckBox* cb = m_cb##FIELD; \
if ( cb && cb->Is3State() ) { \
cb->Set3StateValue(wr_alltrue ? wxCHK_CHECKED \
: wr_allfalse ? wxCHK_UNCHECKED \
: wxCHK_UNDETERMINED); \
} else if ( cb ) { \
/* 2-state fallback: if unanimous keep value, otherwise choose false */\
if (wr_alltrue) cb->SetValue(true); \
else cb->SetValue(false); \
} \
} while (0)
#define SET_CHECKBOX(FIELD) SET_CHECKBOX_FIELD(FIELD, wr_cfg.FIELD)
#define GET_CHECKBOX(FIELD) \
do { \
wxCheckBox* cb = m_cb##FIELD; \
if (cb && cb->Is3State()) { \
wxCheckBoxState wr_st = cb->Get3StateValue(); \
if (wr_st == wxCHK_UNCHECKED) \
configuration.FIELD = false; \
else if (wr_st == wxCHK_CHECKED) \
configuration.FIELD = true; \
/* UNDETERMINED: leave configuration.FIELD unchanged */ \
} else if (cb) { \
/* 2-state checkbox: use boolean value */ \
configuration.FIELD = cb->GetValue(); \
} \
} while (0)
#ifdef __OCPN__ANDROID__
#define NO_EDITED_CONTROLS 1
#else
#define NO_EDITED_CONTROLS 0
#endif
// ---------------------------------------------------------------------------------
void ConfigurationDialog::SetConfigurations(
std::list<RouteMapConfiguration> configurations) {
m_bBlockUpdate = true;
m_edited_controls.clear();
//-----------
// Keep original behaviour for multi-selection fields
SET_CHOICE(Start);
// If there are no configurations, nothing to populate - bail out safely.
if (configurations.empty()) {
m_bBlockUpdate = false;
return;
}
// Create iterator used by the existing macros that expect (*it)
std::list<RouteMapConfiguration>::iterator it = configurations.begin();
const bool ult =
m_WeatherRouting.m_SettingsDialog.m_cbUseLocalTime->IsChecked();
// Populate date/time from the first configuration safely
if (it != configurations.end()) {
// Date part: only set if valid (avoid MSW assert), otherwise set "none"
// only when the control supports wxDP_ALLOWNONE
wxDateTime dateVal = it->StartTime.GetDateOnly(); // No conversion here
wxSize s(m_dpStartDate->GetSize());
if (dateVal.IsValid()) {
// SetValue() assumes dateVal is UTC and converts to local time for GUI
if (!ult) dateVal = dateVal.ToUTC(); // Undo the FromUTC() in SetValue()
m_dpStartDate->SetValue(dateVal);
m_dpStartDate->SetForegroundColour(wxColour(0, 0, 0));
} else if (m_dpStartDate->GetWindowStyle() & wxDP_ALLOWNONE) {
m_dpStartDate->SetValue(wxDefaultDateTime);
m_dpStartDate->SetForegroundColour(wxColour(180, 180, 180));
} // else leave control unchanged
m_dpStartDate->Fit();
m_dpStartDate->SetSize(s);
// Time part: only set if valid (time control usually requires a valid
// value)
wxDateTime timeVal = it->StartTime;
wxSize s2(m_tpTime->GetSize());
if (timeVal.IsValid()) {
// SetValue() assumes timeVal is UTC and converts to local time for GUI
if (!ult) timeVal = timeVal.ToUTC(); // Undo the FromUTC() in SetValue()
m_tpTime->SetValue(timeVal);
m_tpTime->SetForegroundColour(wxColour(0, 0, 0));
} // else leave control unchanged
m_tpTime->Fit();
m_tpTime->SetSize(s2);
}
// -------------
SET_CHECKBOX(UseCurrentTime);
bool timeButtonsEnabled = m_tpTime->IsEnabled() &&
m_dpStartDate->IsEnabled() &&
!m_cbUseCurrentTime->IsChecked();
m_dpStartDate->Enable(timeButtonsEnabled);
m_tpTime->Enable(timeButtonsEnabled);
m_bCurrentTime->Enable(timeButtonsEnabled);
m_bGribTime->Enable(timeButtonsEnabled);
SET_SPIN_VALUE(TimeStepHours, (int)((*it).DeltaTime / 3600));
SET_SPIN_VALUE(TimeStepMinutes, ((int)(*it).DeltaTime / 60) % 60);
SET_CONTROL(boatFileName, m_tBoat, SetValue, wxString, _T(""));
long l = m_tBoat->GetValue().Length();
m_tBoat->SetSelection(l, l);
SET_CHOICE(End);
// if there's a Route GUID it's an OpenCPN route, in that case disable start
// and end.
bool oRoute = false;
bool allStartFromBoat = true;
bool allStartFromPosition = true;
for (auto it : configurations) {
if (!it.RouteGUID.IsEmpty()) {
oRoute = true;
break;
}
if (it.StartType != RouteMapConfiguration::START_FROM_BOAT)
allStartFromBoat = false;
if (it.StartType != RouteMapConfiguration::START_FROM_POSITION)
allStartFromPosition = false;
}
m_rbStartFromBoat->Enable(!oRoute);
m_rbStartPositionSelection->Enable(!oRoute);
m_rbStartFromBoat->SetValue(allStartFromBoat);
m_rbStartPositionSelection->SetValue(allStartFromPosition);
if (!oRoute && !m_rbStartFromBoat->GetValue()) {
bool isPosition = false;
for (const auto& p : RouteMap::Positions) {
if (p.Name == it->Start) {
isPosition = true;
break;
}
}
isPosition ? AddPositions(true) : AddWaypoints(true);
m_rbStartPositionSelection->SetValue(isPosition);
m_rbStartWaypointSelection->SetValue(!isPosition);
m_cStart->SetValue(it->Start);
m_cStart->Enable(true);
} else
m_cStart->Enable(false);
if (!oRoute) {
bool isPosition = false;
std::cout << "INIT Searching for " << it->End << std::endl;
for (const auto& p : RouteMap::Positions) {
if (p.Name == it->End) {
isPosition = true;
break;
}
}
isPosition ? AddPositions(false) : AddWaypoints(false);
m_rbEndPositionSelection->SetValue(isPosition);
m_rbEndWaypointSelection->SetValue(!isPosition);
m_cEnd->SetValue(it->End);
m_cEnd->Enable(true);
} else
m_cEnd->Enable(false);
m_bGribTime->Enable(!m_cbUseCurrentTime->IsChecked());
m_bCurrentTime->Enable(!m_cbUseCurrentTime->IsChecked());
m_dpStartDate->Enable(!m_cbUseCurrentTime->IsChecked());
m_tpTime->Enable(!m_cbUseCurrentTime->IsChecked());
SET_SPIN(FromDegree);
SET_SPIN(ToDegree);
SET_CHECKBOX(UseOptimalAngles);
// Enable/disable degree controls based on checkbox state
m_sFromDegree->Enable(!m_cbUseOptimalAngles->IsChecked());
m_sToDegree->Enable(!m_cbUseOptimalAngles->IsChecked());
SET_SPIN_DOUBLE(ByDegrees);
// Motor settings
SET_CHECKBOX(UseMotor);
SET_SPIN_DOUBLE_VALUE(MotorSpeedThreshold, (*it).MotorSpeedThreshold);
SET_SPIN_DOUBLE_VALUE(MotorSpeed, (*it).MotorSpeed);
// Enable/disable motor controls based on checkbox state
bool motorEnabled = m_cbUseMotor->IsChecked();
m_sMotorSpeedThreshold->Enable(motorEnabled);
m_sMotorSpeed->Enable(motorEnabled);
SET_CHOICE_VALUE(Integrator,
((*it).Integrator == RouteMapConfiguration::RUNGE_KUTTA
? _T("Runge Kutta")
: _T("Newton")));
SET_SPIN(MaxDivertedCourse);
SET_SPIN(MaxCourseAngle);
SET_SPIN(MaxSearchAngle);
SET_SPIN(MaxTrueWindKnots);
SET_SPIN(MaxApparentWindKnots);
SET_SPIN_DOUBLE(MaxSwellMeters);
SET_SPIN(MaxLatitude);
SET_SPIN(TackingTime);
SET_SPIN(JibingTime);
SET_SPIN(SailPlanChangeTime);
SET_SPIN(WindVSCurrent);
SET_CHECKBOX(AvoidCycloneTracks);
SET_SPIN(CycloneMonths);
SET_SPIN(CycloneDays);
SET_SPIN_DOUBLE(SafetyMarginLand);
SET_CHECKBOX(DetectLand);
SET_CHECKBOX(DetectBoundary);
SET_CHECKBOX(Currents);
SET_CHECKBOX(OptimizeTacking);
SET_CHECKBOX(InvertedRegions);
SET_CHECKBOX(Anchoring);
SET_CHECKBOX(UseGrib);
SET_CONTROL(ClimatologyType, m_cClimatologyType, SetSelection, int, -1);
SET_CHECKBOX(AllowDataDeficient);
SET_SPIN_VALUE(WindStrength, (int)((*it).WindStrength * 100));
SET_SPIN_VALUE(UpwindEfficiency, (int)((*it).UpwindEfficiency * 100));
SET_SPIN_VALUE(DownwindEfficiency, (int)((*it).DownwindEfficiency * 100));
SET_SPIN_VALUE(NightCumulativeEfficiency,
(int)((*it).NightCumulativeEfficiency * 100));
m_bBlockUpdate = false;
}
void ConfigurationDialog::AddSource(wxString name) {
m_cStart->Append(name);
m_cEnd->Append(name);
}
void ConfigurationDialog::RemoveSource(wxString name) {
int i = m_cStart->FindString(name, true);
if (i >= 0) {
m_cStart->Delete(i);
m_cEnd->Delete(i);
}
}
void ConfigurationDialog::ClearSources() {
m_cStart->Clear();
m_cEnd->Clear();
}
void ConfigurationDialog::AddWaypoints(const bool atStart) {
wxComboBox* combobox = atStart ? m_cStart : m_cEnd;
combobox->Clear();
wxArrayString waypoint_guids = GetWaypointGUIDArray();
for (const auto& guid : waypoint_guids)
combobox->Append(GetWaypoint_Plugin(guid)->m_MarkName);
}
void ConfigurationDialog::AddPositions(const bool atStart) {
wxComboBox* combobox = atStart ? m_cStart : m_cEnd;
combobox->Clear();
for (const auto& p : RouteMap::Positions) combobox->Append(p.Name);
}
void ConfigurationDialog::SetBoatFilename(wxString path) {
m_tBoat->SetValue(path);
long l = m_tBoat->GetValue().Length();
m_tBoat->SetSelection(l, l);
Update();
}
void ConfigurationDialog::OnResetAdvanced(wxCommandEvent& event) {
m_bBlockUpdate = true;
// constraints
m_sMaxLatitude->SetValue(90);
m_sWindVSCurrent->SetValue(0);
m_sMaxCourseAngle->SetValue(180);
m_sMaxSearchAngle->SetValue(120);
m_cbAvoidCycloneTracks->SetValue(false);
// XXX missing 2
// Options
m_cbInvertedRegions->SetValue(false);
m_cbAnchoring->SetValue(false);
m_cIntegrator->SetSelection(0);
m_sWindStrength->SetValue(100);
m_sUpwindEfficiency->SetValue(100);
m_sDownwindEfficiency->SetValue(100);
m_sNightCumulativeEfficiency->SetValue(100);
m_sTackingTime->SetValue(0);
m_sJibingTime->SetValue(0);
m_sSailPlanChangeTime->SetValue(0);
m_sSafetyMarginLand->SetValue(0.);
m_sFromDegree->SetValue(0);
m_sToDegree->SetValue(180);
m_cbUseOptimalAngles->SetValue(false);
m_sFromDegree->Enable(true);
m_sToDegree->Enable(true);
m_sByDegrees->SetValue(5.0);
// Motor settings
m_cbUseMotor->SetValue(false);
m_sMotorSpeedThreshold->SetValue(2.0);
m_sMotorSpeed->SetValue(5.0);
m_sMotorSpeedThreshold->Enable(false);
m_sMotorSpeed->Enable(false);
m_bBlockUpdate = false;
Update();
}
void ConfigurationDialog::SetStartDateTime(wxDateTime datetime) {
// datetime is passed as UTC
if (datetime.IsValid()) {
// SetValue() for both pickers assumes that datetime is UTC and converts
// to local time for display in the GUI
if (!m_WeatherRouting.m_SettingsDialog.m_cbUseLocalTime->IsChecked())
datetime = datetime.ToUTC(); // Undo the FromUTC() in SetValue()
m_dpStartDate->SetValue(datetime);
m_tpTime->SetValue(datetime);
m_edited_controls.push_back(m_tpTime);
m_edited_controls.push_back(m_dpStartDate);
} else {
wxMessageDialog mdlg(this, _("Invalid Date Time."),
wxString(_("Weather Routing"), wxOK | wxICON_WARNING));
mdlg.ShowModal();
}
}
#define GET_SPIN(FIELD) \
if (NO_EDITED_CONTROLS || \
std::find(m_edited_controls.begin(), m_edited_controls.end(), \
(wxObject*)m_s##FIELD) != m_edited_controls.end()) { \
configuration.FIELD = m_s##FIELD->GetValue(); \
m_s##FIELD->SetForegroundColour(wxColour(0, 0, 0)); \
}
#define GET_CHOICE(FIELD) \
if (NO_EDITED_CONTROLS || \
std::find(m_edited_controls.begin(), m_edited_controls.end(), \
(wxObject*)m_c##FIELD) != m_edited_controls.end()) \
if (m_c##FIELD->GetValue() != wxEmptyString) { \
configuration.FIELD = m_c##FIELD->GetValue(); \
if (m_c##FIELD->GetString(m_c##FIELD->GetCount() - 1) == wxEmptyString) \
m_c##FIELD->Delete(m_c##FIELD->GetCount() - 1); \
}
void ConfigurationDialog::Update() {
if (m_bBlockUpdate) return;
// Ensure the combobox is properly enabled/disabled based on radio button
// selection
m_cStart->Enable(!m_rbStartFromBoat->GetValue());
bool refresh = false;
RouteMapConfiguration configuration;
std::list<RouteMapOverlay*> currentroutemaps =
m_WeatherRouting.CurrentRouteMaps();
for (std::list<RouteMapOverlay*>::iterator it = currentroutemaps.begin();
it != currentroutemaps.end(); it++) {
configuration = (*it)->GetConfiguration();
// Set the start type based on the radio button selection
if (m_rbStartFromBoat->GetValue()) {
configuration.StartType = RouteMapConfiguration::START_FROM_BOAT;
} else {
configuration.StartType = RouteMapConfiguration::START_FROM_POSITION;
}
// Only get start position choice if not using boat position
if (configuration.StartType == RouteMapConfiguration::START_FROM_POSITION)
GET_CHOICE(Start);
GET_CHOICE(End);
GET_CHECKBOX(UseCurrentTime);
if (NO_EDITED_CONTROLS ||
std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_dpStartDate) != m_edited_controls.end() ||
std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_tpTime) != m_edited_controls.end()) {
if (!m_dpStartDate->GetDateCtrlValue().IsValid()) continue;
/*
* GetDateCtrlValue() and GetTimeCtrlValue() assume that the entered time
* is local time and convert to UTC for internal storage
* GetHour() etc. convert internal UTC value to local time
* wxDateTime() constructor assumes input in local time and converts to
* UTC for internal storage
* Therefore we must undo one ToUTC() conversion if the GUI option has
* been set to use UTC
* Note: There will be one hour error if the switch to/from DST happens
* in between these calls
* Note: GetTimeCtrlValue() sets the date part to Jan 1st of the current
* year, therefore calling ToUTC() on it is not DST-safe
*/
wxDateTime ctrl_date = m_dpStartDate->GetDateCtrlValue();
wxDateTime ctrl_time = m_tpTime->GetTimeCtrlValue();
wxDateTime time(ctrl_date.GetDay(), ctrl_date.GetMonth(),
ctrl_date.GetYear(), ctrl_time.GetHour(),
ctrl_time.GetMinute(), ctrl_time.GetSecond());
if (!time.IsValid())
time = wxDateTime::Now(); // Local time converted to UTC
else if (!m_WeatherRouting.m_SettingsDialog.m_cbUseLocalTime->IsChecked())
time = time.FromUTC(); // Undo extra conversion
configuration.StartTime = time;
if (std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_dpStartDate) != m_edited_controls.end())
m_dpStartDate->SetForegroundColour(wxColour(0, 0, 0));
else if (std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_tpTime) != m_edited_controls.end())
m_tpTime->SetForegroundColour(wxColour(0, 0, 0));
}
if (!m_tBoat->GetValue().empty()) {
configuration.boatFileName = m_tBoat->GetValue();
m_tBoat->SetForegroundColour(wxColour(0, 0, 0));
}
if (NO_EDITED_CONTROLS ||
std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_sTimeStepHours) != m_edited_controls.end() ||
std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_sTimeStepMinutes) != m_edited_controls.end()) {
configuration.DeltaTime = 60 * (60 * m_sTimeStepHours->GetValue() +
m_sTimeStepMinutes->GetValue());
m_sTimeStepHours->SetForegroundColour(wxColour(0, 0, 0));
m_sTimeStepMinutes->SetForegroundColour(wxColour(0, 0, 0));
}
if (m_cIntegrator->GetValue() == _T("Newton"))
configuration.Integrator = RouteMapConfiguration::NEWTON;
else if (m_cIntegrator->GetValue() == _T("Runge Kutta"))
configuration.Integrator = RouteMapConfiguration::RUNGE_KUTTA;
GET_SPIN(MaxDivertedCourse);
GET_SPIN(MaxCourseAngle);
GET_SPIN(MaxSearchAngle);
GET_SPIN(MaxTrueWindKnots);
GET_SPIN(MaxApparentWindKnots);
GET_SPIN(MaxSwellMeters);
GET_SPIN(MaxLatitude);
GET_SPIN(TackingTime);
GET_SPIN(JibingTime);
GET_SPIN(SailPlanChangeTime);
GET_SPIN(WindVSCurrent);
if (m_sWindStrength->IsEnabled())
configuration.WindStrength = m_sWindStrength->GetValue() / 100.0;
if (m_sUpwindEfficiency->IsEnabled())
configuration.UpwindEfficiency = m_sUpwindEfficiency->GetValue() / 100.0;
if (m_sDownwindEfficiency->IsEnabled())
configuration.DownwindEfficiency =
m_sDownwindEfficiency->GetValue() / 100.0;
if (m_sNightCumulativeEfficiency->IsEnabled())
configuration.NightCumulativeEfficiency =
m_sNightCumulativeEfficiency->GetValue() / 100.0;
GET_CHECKBOX(AvoidCycloneTracks);
GET_SPIN(CycloneMonths);
GET_SPIN(CycloneDays);
GET_SPIN(SafetyMarginLand);
GET_CHECKBOX(DetectLand);
GET_CHECKBOX(DetectBoundary);
GET_CHECKBOX(Currents);
GET_CHECKBOX(OptimizeTacking);
GET_CHECKBOX(InvertedRegions);
GET_CHECKBOX(Anchoring);
GET_CHECKBOX(UseGrib);
if (m_cClimatologyType->GetSelection() != -1)
configuration.ClimatologyType =
(RouteMapConfiguration::ClimatologyDataType)
m_cClimatologyType->GetSelection();
GET_CHECKBOX(AllowDataDeficient);
if (m_sWindStrength->IsEnabled())
configuration.WindStrength = m_sWindStrength->GetValue() / 100.0;
GET_SPIN(FromDegree);
GET_SPIN(ToDegree);
GET_CHECKBOX(UseOptimalAngles);
GET_SPIN(ByDegrees);
// Motor settings
GET_CHECKBOX(UseMotor);
if (NO_EDITED_CONTROLS ||
std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_sMotorSpeedThreshold) !=
m_edited_controls.end()) {
configuration.MotorSpeedThreshold = m_sMotorSpeedThreshold->GetValue();
m_sMotorSpeedThreshold->SetForegroundColour(wxColour(0, 0, 0));
}
if (NO_EDITED_CONTROLS ||
std::find(m_edited_controls.begin(), m_edited_controls.end(),
(wxObject*)m_sMotorSpeed) != m_edited_controls.end()) {
configuration.MotorSpeed = m_sMotorSpeed->GetValue();
m_sMotorSpeed->SetForegroundColour(wxColour(0, 0, 0));
}
(*it)->SetConfiguration(configuration);
/* if the start position changed, we must reset the route */
RouteMapConfiguration newc = (*it)->GetConfiguration();
if (newc.StartLat != configuration.StartLat ||
newc.StartLon != configuration.StartLon) {
(*it)->Reset();
refresh = true;
} else if (newc.EndLat != configuration.EndLat ||
newc.EndLon != configuration.EndLon)
refresh = true; // update drawing
}
double by = m_sByDegrees->GetValue();
if (m_sToDegree->GetValue() - m_sFromDegree->GetValue() < 2 * by) {
wxMessageDialog mdlg(
this, _("Warning: less than 4 different degree steps specified\n"),
wxString(_("Weather Routing"), wxOK | wxICON_WARNING));
mdlg.ShowModal();
}
m_WeatherRouting.UpdateCurrentConfigurations();
if (refresh) m_WeatherRouting.GetParent()->Refresh();
// Schedule auto-save to persist any configuration changes
m_WeatherRouting.ScheduleAutoSave();
}