HamBook – Blame information for rev 12

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