Skip to content

Commit 8ce16a0

Browse files
committed
Consolidate stimulus verification logic
- Add a check when closing the Headstage64 dialog - Move existing logic to base class - Ensure that the dialogs can be closed and the contents discarded correctly, allowing for one device to save parameters and another device to discard parameters where appropriate
1 parent a37e982 commit 8ce16a0

7 files changed

+52
-113
lines changed

OpenEphys.Onix1.Design/GenericStimulusSequenceDialog.cs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,35 @@ void ButtonOk_Click(object sender, EventArgs e)
8484
}
8585
}
8686

87-
internal virtual bool CanCloseForm(out DialogResult result)
87+
internal bool CanCloseForm(out DialogResult result, string stimulusName = "Stimulus")
8888
{
89-
result = DialogResult.OK;
90-
return true;
89+
bool canClose = true;
90+
91+
if (!IsSequenceValid())
92+
{
93+
DialogResult resultContinue = MessageBox.Show($"Warning: {stimulusName} sequence is not valid. " +
94+
$"If you continue, the current settings for {stimulusName} will be discarded. " +
95+
"Press OK to discard all changes for this device, or press Cancel to continue editing the sequence.",
96+
$"Invalid {stimulusName} Sequence",
97+
MessageBoxButtons.OKCancel);
98+
99+
if (resultContinue == DialogResult.OK)
100+
{
101+
result = DialogResult.Cancel;
102+
}
103+
else
104+
{
105+
result = DialogResult.OK;
106+
canClose = false;
107+
}
108+
}
109+
else
110+
{
111+
result = DialogResult.OK;
112+
}
113+
114+
DialogResult = result;
115+
return canClose;
91116
}
92117

93118
internal void OnSelect(object sender, EventArgs e)
@@ -381,7 +406,7 @@ void CenterAxesOnCursor(ZedGraphControl zedGraphControl)
381406

382407
internal virtual bool IsSequenceValid()
383408
{
384-
return true;
409+
throw new NotImplementedException();
385410
}
386411

387412
internal virtual void SetStatusValidity()

OpenEphys.Onix1.Design/Headstage64Dialog.Designer.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

OpenEphys.Onix1.Design/Headstage64Dialog.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Windows.Forms;
1+
using System;
2+
using System.Windows.Forms;
23

34
namespace OpenEphys.Onix1.Design
45
{
@@ -38,5 +39,19 @@ public Headstage64Dialog(ConfigureHeadstage64 configureNode)
3839

3940
menuStrip1.Visible = false;
4041
}
42+
43+
void OnClickOk(object sender, EventArgs e)
44+
{
45+
if (ElectricalStimulatorSequenceDialog.CanCloseForm(out DialogResult electricalResult, "Electrical Stimulator")
46+
&& OpticalStimulatorSequenceDialog.CanCloseForm(out DialogResult opticalResult, "Optical Stimulator"))
47+
{
48+
if (electricalResult == DialogResult.OK || opticalResult == DialogResult.OK)
49+
DialogResult = DialogResult.OK;
50+
else
51+
DialogResult = DialogResult.Cancel;
52+
53+
Close();
54+
}
55+
}
4156
}
4257
}

OpenEphys.Onix1.Design/Headstage64Editor.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ public override bool EditComponent(ITypeDescriptorContext context, object compon
2525
DesignHelper.CopyProperties((ConfigureRhd2164)editorDialog.Rhd2164Dialog.Device, configureNode.Rhd2164, DesignHelper.PropertiesToIgnore);
2626
DesignHelper.CopyProperties((ConfigureBno055)editorDialog.Bno055Dialog.Device, configureNode.Bno055, DesignHelper.PropertiesToIgnore);
2727
DesignHelper.CopyProperties((ConfigureTS4231V1)editorDialog.TS4231V1Dialog.Device, configureNode.TS4231, DesignHelper.PropertiesToIgnore);
28-
configureNode.ElectricalStimulator = editorDialog.ElectricalStimulatorSequenceDialog.ElectricalStimulator;
29-
configureNode.OpticalStimulator = editorDialog.OpticalStimulatorSequenceDialog.OpticalStimulator;
28+
29+
if (editorDialog.ElectricalStimulatorSequenceDialog.DialogResult == DialogResult.OK)
30+
configureNode.ElectricalStimulator = editorDialog.ElectricalStimulatorSequenceDialog.ElectricalStimulator;
31+
32+
if (editorDialog.OpticalStimulatorSequenceDialog.DialogResult == DialogResult.OK)
33+
configureNode.OpticalStimulator = editorDialog.OpticalStimulatorSequenceDialog.OpticalStimulator;
3034

3135
return true;
3236
}

OpenEphys.Onix1.Design/Headstage64ElectricalStimulatorSequenceDialog.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -252,41 +252,6 @@ static bool AnyCurrentIsSet(ConfigureHeadstage64ElectricalStimulator sequence)
252252
return true;
253253
}
254254

255-
internal override bool CanCloseForm(out DialogResult result)
256-
{
257-
if (ElectricalStimulator != null)
258-
{
259-
if (!IsSequenceValid(ElectricalStimulator, out string reason))
260-
{
261-
DialogResult resultContinue = MessageBox.Show($"Warning: Stimulus sequence is not valid ({reason}). " +
262-
"If you continue, the current settings will be discarded. " +
263-
"Press OK to discard changes, or press Cancel to continue editing the sequence.", "Invalid Sequence",
264-
MessageBoxButtons.OKCancel);
265-
266-
if (resultContinue == DialogResult.OK)
267-
{
268-
result = DialogResult.Cancel;
269-
return true;
270-
}
271-
else
272-
{
273-
result = DialogResult.OK;
274-
return false;
275-
}
276-
}
277-
else
278-
{
279-
result = DialogResult.OK;
280-
return true;
281-
}
282-
}
283-
else
284-
{
285-
result = DialogResult.Cancel;
286-
return true;
287-
}
288-
}
289-
290255
internal override double GetPeakToPeakAmplitudeInMicroAmps()
291256
{
292257
var peakToPeak = Math.Max(Math.Max(ElectricalStimulator.PhaseOneCurrent, ElectricalStimulator.PhaseTwoCurrent), ElectricalStimulator.InterPhaseCurrent)

OpenEphys.Onix1.Design/Headstage64OpticalStimulatorSequenceDialog.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -305,40 +305,5 @@ internal override void SetStatusValidity()
305305
toolStripStatusIsValid.Text = "Warning: " + reason;
306306
}
307307
}
308-
309-
internal override bool CanCloseForm(out DialogResult result)
310-
{
311-
if (OpticalStimulator != null)
312-
{
313-
if (!IsSequenceValid(OpticalStimulator, out string reason))
314-
{
315-
DialogResult resultContinue = MessageBox.Show($"Warning: Stimulus sequence is not valid ({reason}). " +
316-
"If you continue, the current settings will be discarded. " +
317-
"Press OK to discard changes, or press Cancel to continue editing the sequence.", "Invalid Sequence",
318-
MessageBoxButtons.OKCancel);
319-
320-
if (resultContinue == DialogResult.OK)
321-
{
322-
result = DialogResult.Cancel;
323-
return true;
324-
}
325-
else
326-
{
327-
result = DialogResult.OK;
328-
return false;
329-
}
330-
}
331-
else
332-
{
333-
result = DialogResult.OK;
334-
return true;
335-
}
336-
}
337-
else
338-
{
339-
result = DialogResult.Cancel;
340-
return true;
341-
}
342-
}
343308
}
344309
}

OpenEphys.Onix1.Design/Rhs2116StimulusSequenceDialog.cs

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -115,41 +115,6 @@ public Rhs2116StimulusSequenceDialog(ConfigureRhs2116Trigger rhs2116Trigger)
115115
DrawStimulusWaveform();
116116
}
117117

118-
internal override bool CanCloseForm(out DialogResult result)
119-
{
120-
if (Sequence != null)
121-
{
122-
if (!Sequence.Valid)
123-
{
124-
DialogResult resultContinue = MessageBox.Show("Warning: Stimulus sequence is not valid. " +
125-
"If you continue, the current settings will be discarded. " +
126-
"Press OK to discard changes, or press Cancel to continue editing the sequence.", "Invalid Sequence",
127-
MessageBoxButtons.OKCancel);
128-
129-
if (resultContinue == DialogResult.OK)
130-
{
131-
result = DialogResult.Cancel;
132-
return true;
133-
}
134-
else
135-
{
136-
result = DialogResult.OK;
137-
return false;
138-
}
139-
}
140-
else
141-
{
142-
result = DialogResult.OK;
143-
return true;
144-
}
145-
}
146-
else
147-
{
148-
result = DialogResult.Cancel;
149-
return true;
150-
}
151-
}
152-
153118
internal void OnZoom(object sender, EventArgs e)
154119
{
155120
ChannelDialog.UpdateFontSize();

0 commit comments

Comments
 (0)