2010-12-18 18:23:09 -04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using ArducopterConfigurator.PresentationModels;
|
|
|
|
|
using ArducopterConfigurator.views;
|
|
|
|
|
using ArducopterConfigurator.Views;
|
|
|
|
|
|
|
|
|
|
namespace ArducopterConfigurator
|
|
|
|
|
{
|
|
|
|
|
public partial class mainForm : Form, IView<MainVm>
|
|
|
|
|
{
|
|
|
|
|
private MainVm _vm;
|
|
|
|
|
|
|
|
|
|
public mainForm(MainVm vm)
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
SetDataContext(vm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// IPresentationModel to IView map
|
|
|
|
|
private readonly Dictionary<Type, Type> _viewMap = new Dictionary<Type, Type>
|
|
|
|
|
{
|
2011-02-16 18:19:32 -04:00
|
|
|
|
{typeof (SensorsVm), typeof (FlightDataView)},
|
2010-12-18 18:23:09 -04:00
|
|
|
|
{typeof (TransmitterChannelsVm), typeof (TransmitterChannelsView)},
|
|
|
|
|
{typeof (MotorCommandsVm), typeof (MotorCommandsView)},
|
|
|
|
|
{typeof (AcroModeConfigVm), typeof (AcroConfigView)},
|
|
|
|
|
{typeof (StableModeConfigVm), typeof (StableConfigView)},
|
|
|
|
|
{typeof (PositionHoldConfigVm), typeof (PositionHoldConfigView)},
|
|
|
|
|
{typeof (AltitudeHoldConfigVm), typeof (AltitudeHoldConfigView)},
|
|
|
|
|
{typeof (SerialMonitorVm), typeof (SerialMonitorView)},
|
2011-02-13 05:25:54 -04:00
|
|
|
|
{typeof (FlightControlPidsVm), typeof (FlightControlPidsView)},
|
|
|
|
|
{typeof (PositionAltitudePidsVm), typeof (PositionAltitudePidsView)},
|
2010-12-18 18:23:09 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private Control CreateAndBindView(IPresentationModel model)
|
|
|
|
|
{
|
|
|
|
|
if (_viewMap.ContainsKey(model.GetType()))
|
|
|
|
|
{
|
|
|
|
|
var viewtype = _viewMap[model.GetType()];
|
|
|
|
|
var view = Activator.CreateInstance(viewtype);
|
|
|
|
|
|
|
|
|
|
var methodInfo = this.GetType().GetMethod("BindView");
|
|
|
|
|
var closedBindViewMethod = methodInfo.MakeGenericMethod(model.GetType());
|
|
|
|
|
return closedBindViewMethod.Invoke(this, new[] { model, view }) as Control;
|
|
|
|
|
}
|
2011-02-13 05:25:54 -04:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentOutOfRangeException("model","Cannot find entry in view map for type: " + model.GetType());
|
|
|
|
|
}
|
2010-12-18 18:23:09 -04:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// called via reflection above to close the T
|
|
|
|
|
public Control BindView<T>(T model, IView<T> view) where T : IPresentationModel
|
|
|
|
|
{
|
|
|
|
|
view.SetDataContext(model);
|
|
|
|
|
return view.Control;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void BindButtons(MainVm vm)
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in this.Controls)
|
|
|
|
|
if (c is Button)
|
|
|
|
|
(c as Button).Click += btn_Click;
|
|
|
|
|
|
|
|
|
|
vm.PropertyChanged += CheckCommandStates;
|
|
|
|
|
CheckCommandStates(this,new PropertyChangedEventArgs(string.Empty));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CheckCommandStates(object sender, PropertyChangedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
foreach (var c in this.Controls)
|
|
|
|
|
{
|
|
|
|
|
var btn = c as Button;
|
|
|
|
|
if (btn == null) continue;
|
|
|
|
|
var cmd =btn.Tag as ICommand;
|
|
|
|
|
if (cmd!=null)
|
|
|
|
|
btn.Enabled = cmd.CanExecute(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected void btn_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var cmd = (sender as Button).Tag as ICommand;
|
|
|
|
|
|
|
|
|
|
if (cmd != null)
|
|
|
|
|
if (cmd.CanExecute(null))
|
|
|
|
|
cmd.Execute(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#region Implementation of IView
|
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
|
public void SetDataContext(MainVm vm)
|
2010-12-18 18:23:09 -04:00
|
|
|
|
{
|
2011-02-13 05:25:54 -04:00
|
|
|
|
_vm = vm;
|
|
|
|
|
mainVmBindingSource.DataSource = vm;
|
2011-01-27 11:58:32 -04:00
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
|
availablePortsBindingSource.DataSource = vm.AvailablePorts;
|
2011-01-27 11:58:32 -04:00
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
|
|
|
|
|
|
foreach (var monitorVm in _vm.MonitorVms)
|
|
|
|
|
{
|
|
|
|
|
var tp = new TabPage(monitorVm.Name) {Tag = monitorVm};
|
|
|
|
|
var control = CreateAndBindView(monitorVm);
|
|
|
|
|
if (control != null)
|
|
|
|
|
{
|
|
|
|
|
tp.Controls.Add(control);
|
|
|
|
|
control.Size = tp.ClientRectangle.Size;
|
|
|
|
|
}
|
|
|
|
|
tabCtrlMonitorVms.TabPages.Add(tp);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-13 05:25:54 -04:00
|
|
|
|
var tabVm = tabCtrlMonitorVms.SelectedTab.Tag as IPresentationModel;
|
2010-12-18 18:23:09 -04:00
|
|
|
|
_vm.Select(tabVm);
|
|
|
|
|
|
|
|
|
|
UpdateConnectionStatusLabel();
|
|
|
|
|
_vm.PropertyChanged += ((sender, e) => UpdateConnectionStatusLabel());
|
|
|
|
|
|
2011-01-25 17:47:18 -04:00
|
|
|
|
// hack for INPC subscribe bug in Mono
|
|
|
|
|
if (Program.IsMonoRuntime)
|
2011-02-13 05:25:54 -04:00
|
|
|
|
vm.PropertyChanged += ((sender, e) => mainVmBindingSource.ResetBindings(false));
|
2011-01-25 17:47:18 -04:00
|
|
|
|
|
2010-12-18 18:23:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Control Control
|
|
|
|
|
{
|
|
|
|
|
get { return this; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
private void tabCtrlConfigs_Selected(object sender, TabControlEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
var control = e.TabPage.Controls[0];
|
|
|
|
|
control.Size = e.TabPage.ClientRectangle.Size;
|
2011-02-13 05:25:54 -04:00
|
|
|
|
var tabVm = e.TabPage.Tag as IPresentationModel;
|
2010-12-18 18:23:09 -04:00
|
|
|
|
_vm.Select(tabVm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void mainForm_SizeChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ResizeChildControls();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void MainFormLoaded(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
ResizeChildControls();
|
|
|
|
|
BindButtons(_vm);
|
2011-02-23 17:43:06 -04:00
|
|
|
|
|
|
|
|
|
if (System.Deployment.Application.ApplicationDeployment.IsNetworkDeployed)
|
|
|
|
|
{
|
|
|
|
|
var ad = System.Deployment.Application.ApplicationDeployment.CurrentDeployment;
|
|
|
|
|
Text = "Arducopter Configurator " + ad.CurrentVersion;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Text = "Arducopter Configurator (No Version)";
|
|
|
|
|
}
|
2010-12-18 18:23:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void ResizeChildControls()
|
|
|
|
|
{
|
|
|
|
|
foreach (TabPage tabPage in tabCtrlMonitorVms.TabPages)
|
|
|
|
|
{
|
|
|
|
|
var control = tabPage.Controls[0];
|
|
|
|
|
control.Size = tabPage.ClientRectangle.Size;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateConnectionStatusLabel()
|
|
|
|
|
{
|
|
|
|
|
switch (_vm.ConnectionState)
|
|
|
|
|
{
|
|
|
|
|
case MainVm.SessionStates.Disconnected:
|
|
|
|
|
lblConnectionStatus.Text="Not Connected";
|
|
|
|
|
break;
|
|
|
|
|
case MainVm.SessionStates.Connecting:
|
|
|
|
|
lblConnectionStatus.Text="Connecting...";
|
|
|
|
|
break;
|
|
|
|
|
case MainVm.SessionStates.Connected:
|
|
|
|
|
lblConnectionStatus.Text="Connected - version " + _vm.ApmVersion;
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new ArgumentOutOfRangeException();
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-02-24 13:13:02 -04:00
|
|
|
|
|
|
|
|
|
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
System.Diagnostics.Process.Start("http://diydrones.com/profile/AndrewRadford");
|
|
|
|
|
}
|
2011-02-26 11:44:39 -04:00
|
|
|
|
|
|
|
|
|
private void cmboComPorts_DropDown(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
_vm.RefreshPortListCommand.Execute(null);
|
|
|
|
|
}
|
2010-12-18 18:23:09 -04:00
|
|
|
|
}
|
|
|
|
|
}
|