HamBook – Rev 14
?pathlinks?
using Configuration;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace HamBook
{
public partial class SettingsForm : Form
{
public bool SaveOnClose { get; private set; }
private Configuration.Configuration _configuration;
public SettingsForm()
{
InitializeComponent();
}
public SettingsForm(Configuration.Configuration configuration, CancellationToken cancellationToken) : this()
{
_configuration = configuration;
}
private void SettingsForm_Load(object sender, EventArgs e)
{
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);
textBox3.DataBindings.Add(nameof(textBox3.Text), _configuration.SerialPortTimeout, nameof(SerialPortTimeout.Read), true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
textBox4.DataBindings.Add(nameof(textBox4.Text), _configuration.SerialPortTimeout, nameof(SerialPortTimeout.Write), true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
textBox5.DataBindings.Add(nameof(textBox5.Text), _configuration, nameof(_configuration.ScanDetectPause), 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 = Utilities.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.Visualisations.Spectrogram, nameof(_configuration.Visualisations.Spectrogram.SampleRate), true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown2.DataBindings.Add(nameof(numericUpDown2.Value), _configuration.Visualisations.Spectrogram, nameof(_configuration.Visualisations.Spectrogram.FFTSamples), true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown3.DataBindings.Add(nameof(numericUpDown3.Value), _configuration.Visualisations.Spectrogram, nameof(_configuration.Visualisations.Spectrogram.AudioBufferTimespan), true, DataSourceUpdateMode.OnPropertyChanged, string.Empty);
numericUpDown4.DataBindings.Add(nameof(numericUpDown4.Value), _configuration.Visualisations.Spectrogram, nameof(_configuration.Visualisations.Spectrogram.SpectrumIntensity), 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;
}
}
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 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.
}
}
}