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