HamBook – Blame information for rev 58
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
54 | office | 1 | using System; |
1 | office | 2 | using System.IO.Ports; |
3 | using System.Linq; |
||
4 | using System.Threading; |
||
5 | using System.Windows.Forms; |
||
54 | office | 6 | using Configuration; |
7 | using HamBook.Utilities; |
||
8 | using NAudio.Wave; |
||
1 | office | 9 | |
10 | namespace HamBook |
||
11 | { |
||
12 | public partial class SettingsForm : Form |
||
13 | { |
||
54 | office | 14 | private readonly Configuration.Configuration _configuration; |
58 | office | 15 | private readonly CancellationToken _cancellationToken; |
1 | office | 16 | |
17 | public SettingsForm() |
||
18 | { |
||
19 | InitializeComponent(); |
||
53 | office | 20 | Utilities.WindowState.FormTracker.Track(this); |
1 | office | 21 | } |
54 | office | 22 | |
1 | office | 23 | public SettingsForm(Configuration.Configuration configuration, CancellationToken cancellationToken) : this() |
24 | { |
||
25 | _configuration = configuration; |
||
58 | office | 26 | _cancellationToken = cancellationToken; |
1 | office | 27 | } |
28 | |||
54 | office | 29 | public bool SaveOnClose { get; private set; } |
30 | |||
1 | office | 31 | private void SettingsForm_Load(object sender, EventArgs e) |
32 | { |
||
33 | var ports = SerialPort.GetPortNames(); |
||
34 | |||
35 | if (ports != null && ports.Length != 0) |
||
36 | { |
||
58 | office | 37 | foreach (var port in ports) |
38 | { |
||
39 | comboBox1.Items.Add(port); |
||
40 | } |
||
1 | office | 41 | |
42 | comboBox1.Text = ports[0]; |
||
43 | } |
||
44 | |||
45 | // Create data bindings. |
||
54 | office | 46 | checkBox1.DataBindings.Add(nameof(checkBox1.Checked), _configuration, nameof(_configuration.LaunchOnBoot), |
47 | true, DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
8 | office | 48 | |
54 | office | 49 | comboBox1.DataBindings.Add(nameof(comboBox1.Text), _configuration, nameof(_configuration.Port), true, |
50 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
51 | comboBox2.DataBindings.Add(nameof(comboBox2.Text), _configuration, nameof(_configuration.Speed), true, |
||
52 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
53 | comboBox3.DataBindings.Add(nameof(comboBox3.Text), _configuration, nameof(_configuration.DataBits), true, |
||
54 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
55 | comboBox4.DataBindings.Add(nameof(comboBox4.Text), _configuration, nameof(_configuration.Parity), true, |
||
56 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
57 | comboBox5.DataBindings.Add(nameof(comboBox5.Text), _configuration, nameof(_configuration.StopBits), true, |
||
58 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
1 | office | 59 | |
54 | office | 60 | comboBox7.DataBindings.Add(nameof(comboBox7.Text), _configuration, nameof(_configuration.Radio), true, |
61 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
62 | comboBox8.DataBindings.Add(nameof(comboBox8.Text), _configuration, nameof(_configuration.Handshake), true, |
||
63 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
5 | office | 64 | |
65 | var bandBindingSource = new BindingSource(); |
||
66 | bandBindingSource.DataSource = _configuration.Definitions.Bands; |
||
67 | comboBox6.DataSource = bandBindingSource; |
||
12 | office | 68 | comboBox6.DisplayMember = "Meters"; |
5 | office | 69 | |
54 | office | 70 | textBox1.DataBindings.Add(nameof(textBox1.Text), bandBindingSource, nameof(Band.Min), true, |
71 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
72 | textBox2.DataBindings.Add(nameof(textBox1.Text), bandBindingSource, nameof(Band.Max), true, |
||
73 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
||
7 | office | 74 | |
54 | office | 75 | numericUpDown6.DataBindings.Add(nameof(numericUpDown6.Value), _configuration.SerialPortTimeout, |
76 | nameof(_configuration.SerialPortTimeout.Read), true, DataSourceUpdateMode.OnPropertyChanged, |
||
77 | string.Empty); |
||
78 | numericUpDown7.DataBindings.Add(nameof(numericUpDown7.Value), _configuration.SerialPortTimeout, |
||
79 | nameof(_configuration.SerialPortTimeout.Write), true, DataSourceUpdateMode.OnPropertyChanged, |
||
80 | string.Empty); |
||
10 | office | 81 | |
54 | office | 82 | comboBox9.DataBindings.Add(nameof(comboBox9.Text), _configuration.Audio, |
83 | nameof(_configuration.Audio.InputDeviceFriendlyName), true, DataSourceUpdateMode.OnPropertyChanged, |
||
84 | string.Empty); |
||
85 | comboBox10.DataBindings.Add(nameof(comboBox10.Text), _configuration.Audio, |
||
86 | nameof(_configuration.Audio.OutputDeviceFriendlyName), true, DataSourceUpdateMode.OnPropertyChanged, |
||
87 | string.Empty); |
||
10 | office | 88 | |
89 | foreach (var deviceNumber in Enumerable.Range(0, WaveIn.DeviceCount)) |
||
90 | { |
||
91 | var capabilities = WaveIn.GetCapabilities(deviceNumber); |
||
92 | |||
93 | comboBox9.Items.Add(capabilities.ProductName); |
||
94 | } |
||
95 | |||
96 | foreach (var deviceNumber in Enumerable.Range(0, WaveOut.DeviceCount)) |
||
97 | { |
||
98 | var capabilities = WaveOut.GetCapabilities(deviceNumber); |
||
99 | comboBox10.Items.Add(capabilities.ProductName); |
||
100 | } |
||
54 | office | 101 | |
102 | foreach (var notificationState in _configuration.Notifications.State) |
||
12 | office | 103 | { |
54 | office | 104 | switch (notificationState.Type) |
13 | office | 105 | { |
106 | case NotificationType.None: |
||
107 | continue; |
||
108 | } |
||
109 | |||
12 | office | 110 | var index = dataGridView1.Rows.Add(); |
111 | |||
13 | office | 112 | var nameTextBox = (DataGridViewTextBoxCell)dataGridView1.Rows[index].Cells["DescriptionColumn"]; |
54 | office | 113 | nameTextBox.Value = Miscellaneous.GetDescriptionFromEnumValue(notificationState.Type); |
12 | office | 114 | |
115 | var enableCheckBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[index].Cells["EnableColumn"]; |
||
54 | office | 116 | enableCheckBox.Value = notificationState.Enabled; |
12 | office | 117 | |
118 | var lingerTextBox = (DataGridViewTextBoxCell)dataGridView1.Rows[index].Cells["LingerColumn"]; |
||
119 | lingerTextBox.Value = notificationState.LingerTime; |
||
120 | } |
||
14 | office | 121 | |
56 | office | 122 | numericUpDown1.DataBindings.Add(nameof(numericUpDown1.Value), _configuration.Visualizations.Spectrogram, |
123 | nameof(_configuration.Visualizations.Spectrogram.SampleRate), true, |
||
54 | office | 124 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
56 | office | 125 | numericUpDown2.DataBindings.Add(nameof(numericUpDown2.Value), _configuration.Visualizations.Spectrogram, |
126 | nameof(_configuration.Visualizations.Spectrogram.FftSamples), true, |
||
54 | office | 127 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
56 | office | 128 | numericUpDown3.DataBindings.Add(nameof(numericUpDown3.Value), _configuration.Visualizations.Spectrogram, |
129 | nameof(_configuration.Visualizations.Spectrogram.AudioBufferTimespan), true, |
||
54 | office | 130 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
56 | office | 131 | numericUpDown4.DataBindings.Add(nameof(numericUpDown4.Value), _configuration.Visualizations.Spectrogram, |
132 | nameof(_configuration.Visualizations.Spectrogram.SpectrumIntensity), true, |
||
54 | office | 133 | DataSourceUpdateMode.OnPropertyChanged, string.Empty); |
21 | office | 134 | |
54 | office | 135 | numericUpDown5.DataBindings.Add(nameof(numericUpDown5.Value), _configuration.Navigation, |
136 | nameof(_configuration.Navigation.FrequencyStep), true, DataSourceUpdateMode.OnPropertyChanged, |
||
137 | string.Empty); |
||
32 | office | 138 | |
54 | office | 139 | checkBox2.DataBindings.Add(nameof(checkBox2.Checked), _configuration.Navigation, |
140 | nameof(_configuration.Navigation.MouseScrollSound), true, DataSourceUpdateMode.OnPropertyChanged, |
||
141 | string.Empty); |
||
1 | office | 142 | } |
143 | |||
144 | private void button1_Click(object sender, EventArgs e) |
||
145 | { |
||
146 | SaveOnClose = true; |
||
147 | Close(); |
||
148 | } |
||
149 | |||
150 | private void button2_Click(object sender, EventArgs e) |
||
151 | { |
||
152 | SaveOnClose = false; |
||
153 | Close(); |
||
154 | } |
||
5 | office | 155 | |
156 | private void textBox1_TextChanged(object sender, EventArgs e) |
||
157 | { |
||
158 | var textBox = (TextBox)sender; |
||
54 | office | 159 | if (string.IsNullOrEmpty(textBox.Text) || !int.TryParse(textBox.Text, out var frequency)) return; |
5 | office | 160 | |
161 | var band = (Band)comboBox6.SelectedValue; |
||
162 | band.Min = frequency; |
||
163 | } |
||
164 | |||
165 | private void textBox2_TextChanged(object sender, EventArgs e) |
||
166 | { |
||
167 | var textBox = (TextBox)sender; |
||
54 | office | 168 | if (string.IsNullOrEmpty(textBox.Text) || !int.TryParse(textBox.Text, out var frequency)) return; |
5 | office | 169 | |
170 | var band = (Band)comboBox6.SelectedValue; |
||
171 | band.Max = frequency; |
||
172 | } |
||
12 | office | 173 | |
174 | private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) |
||
175 | { |
||
176 | var dataGridView = (DataGridView)sender; |
||
177 | |||
178 | if (dataGridView.CurrentCell is DataGridViewCheckBoxCell) |
||
179 | dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); |
||
180 | } |
||
181 | |||
182 | private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e) |
||
183 | { |
||
184 | var dataGridView = (DataGridView)sender; |
||
185 | |||
186 | dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit); |
||
187 | } |
||
188 | |||
189 | private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) |
||
190 | { |
||
191 | var dataGridView = (DataGridView)sender; |
||
192 | |||
193 | if (dataGridView.CurrentCell is DataGridViewCheckBoxCell) |
||
194 | if (dataGridView.CurrentCell.IsInEditMode) |
||
195 | if (dataGridView.IsCurrentCellDirty) |
||
196 | dataGridView.EndEdit(); |
||
197 | } |
||
198 | |||
199 | private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) |
||
200 | { |
||
201 | var dataGridView = (DataGridView)sender; |
||
202 | |||
54 | office | 203 | if (e.RowIndex == -1 || e.ColumnIndex == -1) return; |
12 | office | 204 | |
205 | switch (dataGridView.Columns[e.ColumnIndex].Name) |
||
206 | { |
||
207 | case "EnableColumn": |
||
208 | ProcessEnable(dataGridView.Rows[e.RowIndex]); |
||
209 | break; |
||
58 | office | 210 | case "LingerColumn": |
211 | ProcessLinger(dataGridView.Rows[e.RowIndex]); |
||
212 | break; |
||
12 | office | 213 | } |
214 | } |
||
215 | |||
216 | private void DataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e) |
||
217 | { |
||
218 | var dataGridView = (DataGridView)sender; |
||
219 | |||
220 | if (e.RowIndex == -1 || e.ColumnIndex == -1 || |
||
221 | !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)) |
||
222 | return; |
||
223 | |||
224 | dataGridView.EndEdit(); |
||
225 | } |
||
226 | |||
227 | private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e) |
||
228 | { |
||
229 | var dataGridView = (DataGridView)sender; |
||
230 | |||
231 | if (e.RowIndex == -1 || e.ColumnIndex == -1 || |
||
232 | !(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn)) |
||
233 | return; |
||
234 | |||
235 | dataGridView.EndEdit(); |
||
236 | } |
||
237 | |||
238 | private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) |
||
239 | { |
||
240 | // Do not allow errors. |
||
241 | } |
||
242 | |||
15 | office | 243 | private void ProcessEnable(DataGridViewRow dataGridViewRow) |
244 | { |
||
245 | var enableCheckBoxCell = |
||
246 | (DataGridViewCheckBoxCell)dataGridViewRow.Cells["EnableColumn"]; |
||
247 | |||
248 | foreach (var notificationState in _configuration.Notifications.State) |
||
249 | switch (notificationState.Type) |
||
250 | { |
||
251 | case NotificationType.SignalScanDetect: |
||
252 | notificationState.Enabled = (bool)enableCheckBoxCell.Value; |
||
253 | break; |
||
254 | } |
||
255 | } |
||
58 | office | 256 | |
257 | private void ProcessLinger(DataGridViewRow dataGridViewRow) |
||
258 | { |
||
259 | var lingerCheckBoxCell = |
||
260 | (DataGridViewTextBoxCell)dataGridViewRow.Cells["LingerColumn"]; |
||
261 | |||
262 | foreach (var notificationState in _configuration.Notifications.State) |
||
263 | switch (notificationState.Type) |
||
264 | { |
||
265 | case NotificationType.SignalScanDetect: |
||
266 | if (int.TryParse($"{lingerCheckBoxCell.Value}", out var value)) |
||
267 | { |
||
268 | notificationState.LingerTime = value; |
||
269 | } |
||
270 | |||
271 | break; |
||
272 | } |
||
273 | } |
||
1 | office | 274 | } |
54 | office | 275 | } |