HamBook – Rev 59
?pathlinks?
using System;
using System.IO.Ports;
using System.Linq;
using System.Threading;
using System.Windows.Forms;
using Configuration;
using HamBook.Utilities;
using NAudio.Wave;
namespace HamBook
{
public partial class SettingsForm : Form
{
public bool SaveOnClose { get; private set; }
private readonly Configuration.Configuration _configuration;
private readonly CancellationToken _cancellationToken;
public SettingsForm()
{
InitializeComponent();
}
public SettingsForm(Configuration.Configuration configuration, CancellationToken cancellationToken) : this()
{
_configuration = configuration;
_cancellationToken = cancellationToken;
}
private void SettingsForm_Load(object sender, EventArgs e)
{
Utilities.WindowState.FormTracker.Track(this);
var ports = SerialPort.GetPortNames();
if (ports != null && ports.Length != 0)
{
foreach (var port in ports)
{
comboBox1.Items.Add(port);
}
comboBox1.Text = ports[0];
}
// Create data bindings.
checkBox1.DataBindings.Add(nameof(checkBox1.Checked), _configuration, nameof(_configuration.LaunchOnBoot),
true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
comboBox1.DataBindings.Add(nameof(comboBox1.Text), _configuration, nameof(_configuration.Port), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
comboBox2.DataBindings.Add(nameof(comboBox2.Text), _configuration, nameof(_configuration.Speed), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
comboBox3.DataBindings.Add(nameof(comboBox3.Text), _configuration, nameof(_configuration.DataBits), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
comboBox4.DataBindings.Add(nameof(comboBox4.Text), _configuration, nameof(_configuration.Parity), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
comboBox5.DataBindings.Add(nameof(comboBox5.Text), _configuration, nameof(_configuration.StopBits), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
comboBox7.DataBindings.Add(nameof(comboBox7.Text), _configuration, nameof(_configuration.Radio), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
comboBox8.DataBindings.Add(nameof(comboBox8.Text), _configuration, nameof(_configuration.Handshake), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
var bandBindingSource = new BindingSource();
bandBindingSource.DataSource = _configuration.Definitions.Bands;
comboBox6.DataSource = bandBindingSource;
comboBox6.DisplayMember = "Meters";
textBox1.DataBindings.Add(nameof(textBox1.Text), bandBindingSource, nameof(Band.Min), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
textBox2.DataBindings.Add(nameof(textBox1.Text), bandBindingSource, nameof(Band.Max), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown6.DataBindings.Add(nameof(numericUpDown6.Value), _configuration.SerialPortTimeout,
nameof(_configuration.SerialPortTimeout.Read), true, DataSourceUpdateMode.OnPropertyChanged,
string.Empty);
numericUpDown7.DataBindings.Add(nameof(numericUpDown7.Value), _configuration.SerialPortTimeout,
nameof(_configuration.SerialPortTimeout.Write), true, DataSourceUpdateMode.OnPropertyChanged,
string.Empty);
comboBox9.DataBindings.Add(nameof(comboBox9.Text), _configuration.Audio,
nameof(_configuration.Audio.InputDeviceFriendlyName), true, DataSourceUpdateMode.OnPropertyChanged,
string.Empty);
comboBox10.DataBindings.Add(nameof(comboBox10.Text), _configuration.Audio,
nameof(_configuration.Audio.OutputDeviceFriendlyName), true, DataSourceUpdateMode.OnPropertyChanged,
string.Empty);
foreach (var deviceNumber in Enumerable.Range(0, WaveIn.DeviceCount))
{
var capabilities = WaveIn.GetCapabilities(deviceNumber);
comboBox9.Items.Add(capabilities.ProductName);
}
foreach (var deviceNumber in Enumerable.Range(0, WaveOut.DeviceCount))
{
var capabilities = WaveOut.GetCapabilities(deviceNumber);
comboBox10.Items.Add(capabilities.ProductName);
}
foreach (var notificationState in _configuration.Notifications.State)
{
switch (notificationState.Type)
{
case NotificationType.None:
continue;
}
var index = dataGridView1.Rows.Add();
var nameTextBox = (DataGridViewTextBoxCell)dataGridView1.Rows[index].Cells["DescriptionColumn"];
nameTextBox.Value = Miscellaneous.GetDescriptionFromEnumValue(notificationState.Type);
var enableCheckBox = (DataGridViewCheckBoxCell)dataGridView1.Rows[index].Cells["EnableColumn"];
enableCheckBox.Value = notificationState.Enabled;
var lingerTextBox = (DataGridViewTextBoxCell)dataGridView1.Rows[index].Cells["LingerColumn"];
lingerTextBox.Value = notificationState.LingerTime;
}
numericUpDown1.DataBindings.Add(nameof(numericUpDown1.Value), _configuration.Visualizations.Spectrogram,
nameof(_configuration.Visualizations.Spectrogram.SampleRate), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown2.DataBindings.Add(nameof(numericUpDown2.Value), _configuration.Visualizations.Spectrogram,
nameof(_configuration.Visualizations.Spectrogram.FftSamples), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown3.DataBindings.Add(nameof(numericUpDown3.Value), _configuration.Visualizations.Spectrogram,
nameof(_configuration.Visualizations.Spectrogram.AudioBufferTimespan), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown4.DataBindings.Add(nameof(numericUpDown4.Value), _configuration.Visualizations.Spectrogram,
nameof(_configuration.Visualizations.Spectrogram.SpectrumIntensity), true,
DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown5.DataBindings.Add(nameof(numericUpDown5.Value), _configuration.Navigation,
nameof(_configuration.Navigation.FrequencyStep), true, DataSourceUpdateMode.OnPropertyChanged,
string.Empty);
checkBox2.DataBindings.Add(nameof(checkBox2.Checked), _configuration.Navigation,
nameof(_configuration.Navigation.MouseScrollSound), true, DataSourceUpdateMode.OnPropertyChanged,
string.Empty);
}
private void button1_Click(object sender, EventArgs e)
{
SaveOnClose = true;
Close();
}
private void button2_Click(object sender, EventArgs e)
{
SaveOnClose = false;
Close();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBox = (TextBox)sender;
if (string.IsNullOrEmpty(textBox.Text) || !int.TryParse(textBox.Text, out var frequency)) return;
var band = (Band)comboBox6.SelectedValue;
band.Min = frequency;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
var textBox = (TextBox)sender;
if (string.IsNullOrEmpty(textBox.Text) || !int.TryParse(textBox.Text, out var frequency)) return;
var band = (Band)comboBox6.SelectedValue;
band.Max = frequency;
}
private void DataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
var dataGridView = (DataGridView)sender;
if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
var dataGridView = (DataGridView)sender;
dataGridView.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var dataGridView = (DataGridView)sender;
if (dataGridView.CurrentCell is DataGridViewCheckBoxCell)
if (dataGridView.CurrentCell.IsInEditMode)
if (dataGridView.IsCurrentCellDirty)
dataGridView.EndEdit();
}
private void DataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
var dataGridView = (DataGridView)sender;
if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
switch (dataGridView.Columns[e.ColumnIndex].Name)
{
case "EnableColumn":
ProcessEnable(dataGridView.Rows[e.RowIndex]);
break;
case "LingerColumn":
ProcessLinger(dataGridView.Rows[e.RowIndex]);
break;
}
}
private void DataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
{
var dataGridView = (DataGridView)sender;
if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
!(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
return;
dataGridView.EndEdit();
}
private void DataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
var dataGridView = (DataGridView)sender;
if (e.RowIndex == -1 || e.ColumnIndex == -1 ||
!(dataGridView.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn))
return;
dataGridView.EndEdit();
}
private void DataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e)
{
// Do not allow errors.
}
private void ProcessEnable(DataGridViewRow dataGridViewRow)
{
var enableCheckBoxCell =
(DataGridViewCheckBoxCell)dataGridViewRow.Cells["EnableColumn"];
foreach (var notificationState in _configuration.Notifications.State)
switch (notificationState.Type)
{
case NotificationType.SignalScanDetect:
notificationState.Enabled = (bool)enableCheckBoxCell.Value;
break;
}
}
private void ProcessLinger(DataGridViewRow dataGridViewRow)
{
var lingerCheckBoxCell =
(DataGridViewTextBoxCell)dataGridViewRow.Cells["LingerColumn"];
foreach (var notificationState in _configuration.Notifications.State)
switch (notificationState.Type)
{
case NotificationType.SignalScanDetect:
if (int.TryParse($"{lingerCheckBoxCell.Value}", out var value))
{
notificationState.LingerTime = value;
}
break;
}
}
}
}
Generated by GNU Enscript 1.6.5.90.