forked from OpenPHDGuiding/phd2
/
advanced_dialog.cpp
532 lines (456 loc) · 18.4 KB
/
advanced_dialog.cpp
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
/*
* advanced_dialog.cpp
* PHD Guiding
*
* Created by Craig Stark.
* Copyright (c) 2006-2010 Craig Stark.
* All rights reserved.
*
* This source code is distributed under the following "BSD" license
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of Craig Stark, Stark Labs nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include "phd.h"
#if defined(__WXOSX__)
# include <wx/choicebk.h>
#endif
// a place to save id of selected panel so we can select the same panel next time the dialog is opened
static int s_selectedPage = -1;
void AdvancedDialog::BuildCtrlSets()
{
m_pGlobalCtrlSet = m_pFrame->GetConfigDlgCtrlSet(m_pFrame, this, m_brainCtrls);
if (pCamera)
m_pCameraCtrlSet = pCamera->GetConfigDlgCtrlSet(m_pCameraSettingsPanel, pCamera, this, m_brainCtrls);
else
m_pCameraCtrlSet = NULL;
m_pGuiderCtrlSet = m_pFrame->pGuider->GetConfigDialogCtrlSet(m_pGuiderSettingsPanel, m_pFrame->pGuider, this, m_brainCtrls);
if (TheAO())
m_pAOCtrlSet = new AOConfigDialogCtrlSet(m_pDevicesSettingsPanel, pMount, this, m_brainCtrls);
else
m_pAOCtrlSet = NULL;
if (pRotator)
m_pRotatorCtrlSet = pRotator->GetConfigDlgCtrlSet(m_pDevicesSettingsPanel, pRotator, this, m_brainCtrls);
else
m_pRotatorCtrlSet = NULL;
// Need a scope ctrl set even if pMount is null - it exports generic controls needed by other panes
m_pScopeCtrlSet = new ScopeConfigDialogCtrlSet(m_pGuiderSettingsPanel, TheScope(), this, m_brainCtrls);
}
void AdvancedDialog::CleanupCtrlSets(void)
{
delete m_pGlobalCtrlSet;
m_pGlobalCtrlSet = NULL;
delete m_pCameraCtrlSet;
m_pCameraCtrlSet = NULL;
delete m_pGuiderCtrlSet;
m_pGuiderCtrlSet = NULL;
delete m_pScopeCtrlSet;
m_pScopeCtrlSet = NULL;
delete m_pAOCtrlSet;
m_pAOCtrlSet = NULL;
delete m_pRotatorCtrlSet;
m_pRotatorCtrlSet = NULL;
}
AdvancedDialog::AdvancedDialog(MyFrame *pFrame) :
wxDialog(pFrame, wxID_ANY, _("Advanced setup"), wxDefaultPosition, wxDefaultSize, wxCAPTION | wxCLOSE_BOX)
{
/*
* The advanced dialog is made up of a number of "on the fly" generated panels that configure different things.
*
* pTopLevelSizer is a top level Box Sizer in wxVERTICAL mode that contains a wxNotebook object
* and an unnamed button sizer with OK and CANCEL buttons.
*
* Each tab of the notebook contains one or more ConfigDialogPane(s) which are basically vertical
* sizers to hold a bunch of UI controls. The UI controls are constructed and managed by ConfigDialogCtrlSet
* objects. These reflect the internal organization of the app and generally bind one-to-one with the major internal
* classes: MyFrame, Guider, Camera, Mount, Scope, AO, Rotator, etc. The controls created by the ConfigDialogCtrlSet
* objects are laid out on the various panes by the ConfigDialogPane instances. So there is a level of indirection here
* such that the controls can generally be placed anywhere, and the ConfigDialogCtrlSet objects don't care. This means the
* overall UI can be optimized for end-users while allowing the underlying controls to reside where they should from an
* internal architecture perspective.
* +------------------------------------+------------------------------------+
* | | Notebook tabs | |
* | + -------------------------------------------------------------------+ |
* | | | |
* | | | |
* | | One or more config dialog panes on each tab of the | |
* | | notebook, possibly nested | |
* | | | |
* | | | |
* | | | |
* | | | |
* | | | |
* + | | |
* | | | |
* + +--------------------------------------------------------------------+ |
* | OK and Cancel Buttons |
* +-------------------------------------------------------------------------+
*
*/
#if defined(__WXOSX__)
m_pNotebook = new wxChoicebook(this, wxID_ANY);
#else
m_pNotebook = new wxNotebook(this, wxID_ANY);
#endif
m_pFrame = pFrame; // We get called before global var is initialized
wxSizerFlags sizer_flags = wxSizerFlags(0).Align(wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL).Border(wxALL,2).Expand();
// Build all the panels first - these are needed to create the various ConfigCtrlSets
// Each panel gets a vertical sizer attached to it
m_pGlobalSettingsPanel = new wxPanel(m_pNotebook);
wxBoxSizer *pGlobalTabSizer = new wxBoxSizer(wxVERTICAL);
m_pGlobalSettingsPanel->SetSizer(pGlobalTabSizer);
m_pNotebook->AddPage(m_pGlobalSettingsPanel, _("Global"), true);
// Camera pane
m_pCameraSettingsPanel = new wxPanel(m_pNotebook);
wxBoxSizer *pCameraTabSizer = new wxBoxSizer(wxVERTICAL);
m_pCameraSettingsPanel->SetSizer(pCameraTabSizer);
m_pNotebook->AddPage(m_pCameraSettingsPanel, _("Camera"), false);
// Guiding pane
m_pGuiderSettingsPanel = new wxPanel(m_pNotebook);
wxBoxSizer *pGuidingTabSizer = new wxBoxSizer(wxVERTICAL);
m_pGuiderSettingsPanel->SetSizer(pGuidingTabSizer);
m_pNotebook->AddPage(m_pGuiderSettingsPanel, _("Guiding"));
// Guiding Algorithms pane
m_pScopeSettingsPanel = new wxPanel(m_pNotebook);
wxBoxSizer *pScopeTabSizer = new wxBoxSizer(wxVERTICAL);
m_pScopeSettingsPanel->SetSizer(pScopeTabSizer);
m_pNotebook->AddPage(m_pScopeSettingsPanel, _("Algorithms"));
// Devices pane - home for AO and rotator - won't be shown if neither device is used
m_pDevicesSettingsPanel = new wxPanel(m_pNotebook);
wxBoxSizer *pDevicesTabSizer = new wxBoxSizer(wxVERTICAL);
m_pDevicesSettingsPanel->SetSizer(pDevicesTabSizer);
m_pNotebook->AddPage(m_pDevicesSettingsPanel, _("Other Devices"));
BuildCtrlSets(); // Populates the m_brainCtrls map with all UI controls
// Pane contruction now pulls controls from the map and places them where they make sense to a user
// Populate global pane
m_pGlobalPane = pFrame->GetConfigDialogPane(m_pGlobalSettingsPanel);
m_pGlobalPane->LayoutControls(m_brainCtrls);
pGlobalTabSizer->Add(m_pGlobalPane, sizer_flags);
// Populate the camera pane
AddCameraPage();
// Populate the guiding pane
m_pGuiderPane = pFrame->pGuider->GetConfigDialogPane(m_pGuiderSettingsPanel);
m_pGuiderPane->LayoutControls(pFrame->pGuider, m_brainCtrls);
pGuidingTabSizer->Add(m_pGuiderPane, sizer_flags);
// Build Mount tab
AddMountPage();
// Build AO tab
AddAoPage();
// Add page for rotator
AddRotatorPage();
// Ok and cancel buttons for the entire dialog box
wxBoxSizer *pTopLevelSizer = new wxBoxSizer(wxVERTICAL);
pTopLevelSizer->Add(m_pNotebook, wxSizerFlags(0).Expand().Border(wxALL, 5));
pTopLevelSizer->Add(CreateButtonSizer(wxOK | wxCANCEL), wxSizerFlags(0).Expand().Border(wxALL, 5));
SetSizerAndFit(pTopLevelSizer);
m_rebuildPanels = false;
}
AdvancedDialog::~AdvancedDialog()
{
CleanupCtrlSets();
}
// Let a client(GearDialog) ask to preload the UI elements - prevents any visible delay when the AdvancedDialog is shown for the first time
void AdvancedDialog::Preload()
{
if (m_rebuildPanels)
RebuildPanels();
}
// Internal debugging function to be sure all controls are hosted on a panel somewhere
void AdvancedDialog::ConfirmLayouts()
{
int orphan_controls = 0;
for (BrainCtrlIdMap::const_iterator it = m_brainCtrls.begin(); it != m_brainCtrls.end(); ++it)
{
BRAIN_CTRL_IDS id = it->first;
const BrainCtrlInfo& info = it->second;
if (!info.isPositioned)
{
Debug.AddLine(wxString::Format("AdvancedDialog internal error: Controlid %d is not positioned", id));
++orphan_controls;
}
}
assert(orphan_controls == 0);
}
// Perform a from-scratch initialization and layout of all the tabs
void AdvancedDialog::RebuildPanels(void)
{
wxSizerFlags sizer_flags = wxSizerFlags(0).Align(wxALIGN_TOP|wxALIGN_CENTER_HORIZONTAL).Border(wxALL,2).Expand();
CleanupCtrlSets();
m_pGlobalPane->Clear(true);
m_pCameraPane->Clear(true);
m_pCameraSettingsPanel->GetSizer()->Clear(true);
m_pGuiderPane->Clear(true);
if (m_pMountPane)
{
m_pMountPane->Clear(true);
m_pScopeSettingsPanel->GetSizer()->Clear(true);
}
if (m_pAOPane)
{
m_pAOPane->Clear(true);
}
if (m_pRotatorPane)
{
m_pRotatorPane->Clear(true);
}
if (m_pRotatorPane != NULL || m_pAOPane != NULL)
m_pDevicesSettingsPanel->GetSizer()->Clear(true);
m_brainCtrls.clear();
BuildCtrlSets();
m_pGlobalPane->LayoutControls(m_brainCtrls);
m_pGlobalPane->Layout();
AddCameraPage();
m_pGuiderPane->LayoutControls(m_pFrame->pGuider, m_brainCtrls); // Guider pane doesn't have specific device dependencies
m_pGuiderPane->Layout();
AddMountPage();
AddAoPage(); // Will handle no AO case
AddRotatorPage(); // Will handle no Rotator case
if (m_pAOPane == NULL && m_pRotatorPane == NULL) // Dump the Other Devices tab if not needed
{
int idx = m_pNotebook->FindPage(m_pDevicesSettingsPanel);
if (idx != wxNOT_FOUND)
m_pNotebook->RemovePage(idx);
}
else
{
int idx = m_pNotebook->FindPage(m_pDevicesSettingsPanel);
if (idx == wxNOT_FOUND)
m_pNotebook->AddPage(m_pDevicesSettingsPanel, _("Other Devices"));
}
GetSizer()->Layout();
GetSizer()->Fit(this);
m_rebuildPanels = false;
ConfirmLayouts(); // maybe should be under compiletime option
}
// Needed by ConfigDialogCtrlSets to know what parent to use when creating a control
wxWindow* AdvancedDialog::GetTabLocation(BRAIN_CTRL_IDS id)
{
if (id < AD_GLOBAL_TAB_BOUNDARY)
return (wxWindow*)m_pGlobalSettingsPanel;
else
if (id < AD_CAMERA_TAB_BOUNDARY)
return (wxWindow*)m_pCameraSettingsPanel;
else
if (id < AD_GUIDER_TAB_BOUNDARY)
return (wxWindow*)m_pGuiderSettingsPanel;
else
if (id < AD_MOUNT_TAB_BOUNDARY)
return (wxWindow*)m_pScopeSettingsPanel;
else
if (id < AD_DEVICES_TAB_BOUNDARY)
return (wxWindow*)m_pDevicesSettingsPanel;
else
{
assert(false); // Fundamental problem
return NULL;
}
}
void AdvancedDialog::AddCameraPage(void)
{
// Even if pCamera is null, the pane hosts other controls
if (pCamera)
m_pCameraPane = pCamera->GetConfigDialogPane(m_pCameraSettingsPanel);
else
m_pCameraPane = new CameraConfigDialogPane(m_pCameraSettingsPanel, pCamera);
m_pCameraPane->LayoutControls(pCamera, m_brainCtrls);
m_pCameraPane->Layout();
m_pCameraSettingsPanel->GetSizer()->Add(m_pCameraPane);
m_pScopeSettingsPanel->Layout();
}
void AdvancedDialog::AddMountPage(void)
{
const long ID_NOMOUNT = 99999;
Mount *mount = pMount;
if (mount)
{
wxWindow *noMsgWindow = m_pScopeSettingsPanel->FindWindow(ID_NOMOUNT);
if (noMsgWindow)
noMsgWindow->Destroy();
m_pMountPane = mount->GetConfigDialogPane(m_pScopeSettingsPanel);
m_pMountPane->LayoutControls(m_pScopeSettingsPanel, m_brainCtrls);
m_pMountPane->Layout();
}
else
{
m_pMountPane = new Mount::MountConfigDialogPane(m_pScopeSettingsPanel, _("Mount"), mount);
wxStaticText *pNoMount = new wxStaticText(m_pScopeSettingsPanel, ID_NOMOUNT, _("No mount specified"));
m_pMountPane->Add(pNoMount);
}
m_pScopeSettingsPanel->GetSizer()->Add(m_pMountPane, wxSizerFlags(0).Border(wxTOP, 10).Expand());
m_pScopeSettingsPanel->Layout();
}
void AdvancedDialog::AddAoPage(void)
{
if (TheAO())
{
m_pAOPane = new AOConfigDialogPane(m_pDevicesSettingsPanel, TheAO());
m_pAOPane->LayoutControls(m_pDevicesSettingsPanel, m_brainCtrls);
m_pAOPane->Layout();
m_pDevicesSettingsPanel->GetSizer()->Add(m_pAOPane, wxSizerFlags(0).Border(wxTOP, 10).Expand());
m_pDevicesSettingsPanel->Layout();
}
else
{
m_pAOPane = NULL;
}
}
void AdvancedDialog::AddRotatorPage(void)
{
if (pRotator)
{
// We have a rotator selected
m_pRotatorPane = new RotatorConfigDialogPane(m_pDevicesSettingsPanel, pRotator);
m_pRotatorPane->LayoutControls(m_pDevicesSettingsPanel, m_brainCtrls);
m_pRotatorPane->Layout();
m_pDevicesSettingsPanel->GetSizer()->Add(m_pRotatorPane, wxSizerFlags(0).Border(wxTOP, 10).Expand());
m_pDevicesSettingsPanel->Layout();
}
else
{
m_pRotatorPane = NULL;
}
}
// All update options for devices are handled by simply forcing a RebuildPanels before the Advanced Dialog is displayed
void AdvancedDialog::UpdateCameraPage(void)
{
m_rebuildPanels = true;
}
void AdvancedDialog::UpdateMountPage(void)
{
m_rebuildPanels = true;
}
void AdvancedDialog::UpdateAoPage(void)
{
m_rebuildPanels = true;
}
void AdvancedDialog::UpdateRotatorPage(void)
{
m_rebuildPanels = true;
}
void AdvancedDialog::LoadValues(void)
{
// Late-binding rebuild of all the panels
if (m_rebuildPanels)
RebuildPanels();
// Load all the current params
m_pGlobalCtrlSet->LoadValues();
if (m_pCameraCtrlSet)
m_pCameraCtrlSet->LoadValues();
if (m_pGuiderCtrlSet)
m_pGuiderCtrlSet->LoadValues();
if (m_pRotatorCtrlSet)
m_pRotatorCtrlSet->LoadValues();
// Mount sub-classes use a hybrid approach involving both CtrlSets and Panes
if (TheAO())
{
m_pAOCtrlSet->LoadValues();
}
if (TheScope())
{
m_pScopeCtrlSet->LoadValues();
m_pMountPane->LoadValues();
}
if (s_selectedPage != -1)
m_pNotebook->ChangeSelection(s_selectedPage);
}
void AdvancedDialog::UnloadValues(void)
{
// Unload all the current params
m_pGlobalCtrlSet->UnloadValues();
if (m_pCameraCtrlSet)
m_pCameraCtrlSet->UnloadValues();
if (m_pGuiderCtrlSet)
m_pGuiderCtrlSet->UnloadValues();
if (m_pRotatorCtrlSet)
m_pRotatorCtrlSet->UnloadValues();
// Mount sub-classes use a hybrid approach involving both CtrlSets and Panes
if (TheAO())
{
m_pAOCtrlSet->UnloadValues();
}
if (TheScope())
{
m_pScopeCtrlSet->UnloadValues();
m_pMountPane->UnloadValues();
}
}
// Any un-do ops need to be handled at the ConfigDialogPane level
void AdvancedDialog::Undo(void)
{
ConfigDialogPane *const panes[] =
{ m_pGlobalPane, m_pGuiderPane, m_pCameraPane, m_pMountPane, m_pAOPane, m_pRotatorPane };
for (unsigned int i = 0; i < WXSIZEOF(panes); i++)
{
ConfigDialogPane *const pane = panes[i];
if (pane)
pane->Undo();
}
}
void AdvancedDialog::EndModal(int retCode)
{
s_selectedPage = m_pNotebook->GetSelection();
wxDialog::EndModal(retCode);
}
// Properties and methods needed by step-size calculator dialog
int AdvancedDialog::GetFocalLength(void)
{
return m_pGlobalCtrlSet->GetFocalLength();
}
void AdvancedDialog::SetFocalLength(int val)
{
m_pGlobalCtrlSet->SetFocalLength(val);
}
double AdvancedDialog::GetPixelSize(void)
{
return m_pCameraCtrlSet ? m_pCameraCtrlSet->GetPixelSize() : 0.0;
}
void AdvancedDialog::SetPixelSize(double val)
{
if (m_pCameraCtrlSet)
m_pCameraCtrlSet->SetPixelSize(val);
}
// Needed to handle reset if the camera binning changes on the fly
void AdvancedDialog::ResetGuidingParams()
{
m_pMountPane->ResetRAGuidingParams();
m_pMountPane->ResetDecGuidingParams();
// No dialog active, so we need to make these changes take effect
if (TheAO())
{
m_pAOCtrlSet->UnloadValues();
}
if (TheScope())
{
m_pScopeCtrlSet->UnloadValues();
m_pMountPane->UnloadValues();
}
}
int AdvancedDialog::GetBinning(void)
{
return m_pCameraCtrlSet ? m_pCameraCtrlSet->GetBinning() : 1;
}
void AdvancedDialog::SetBinning(int binning)
{
if (m_pCameraCtrlSet)
m_pCameraCtrlSet->SetBinning(binning);
}