tbf/TBF/UI/Bench/Components/ComponentParametersDlg.cs

199 lines
6.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Common;
using NHibernate;
///
/// Copyright (c) 2013-2020 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using TBF.Resources;
using TBF.Rig;
using TBF.Rig.Generic;
using TBF.UI.Shared;
namespace TBF.UI.Bench.Components
{
public partial class ComponentParametersDlg : Form
{
public IComponentCfgCtrl ComponentCfgCtrl;
const int ComponentCfgCtrlHeight = 350;
const int ComponentCfgCtrlMoreHeight = 250;
public Form SharedButtonsParentForm
{
get { return sharedButtons.ParentForm; }//...MF
}
public IComponentCfg Config
{
get
{
if (ComponentCfgCtrl == null) return null;
return ComponentCfgCtrl.Config;
}
}
public CfgUpdateFlags Flags;
public bool UnlockAfterStart;
/// <summary>
/// List of components (=component configuration instances)
/// </summary>
public IList<Config.Entities.Component> CmpntEntities;
public ISession Session;
int cmpntEntityId;
ComponentParametersDlg()
{
InitializeComponent();
}
/// <summary>
/// Create a window to edit component configuration
/// </summary>
/// <param name="cmpntEntityId">Config.Entities.Component.Id or 0 when this is a new/copied/immported component</param>
public ComponentParametersDlg(Form parentForm, int cmpntEntityId = 0)
: this()
{
this.cmpntEntityId = cmpntEntityId;
/// SharedDlgButtons configuration
sharedButtons.ParentForm = parentForm;
sharedButtons.RequiredGroupMembership = new GID[] { GID.Metrologists };
sharedButtons.MoreButtonTop = ComponentCfgCtrlHeight - 45;
sharedButtons.OptionalButtons = SharedButtons.Buttons.None;
sharedButtons.Unlocked += Unlock;
sharedButtons.OKClicked += okButton_Click;
sharedButtons.CancelClicked += cancelButton_Click;
sharedButtons.MoreClicked += moreButton_Click;
Flags = CfgUpdateFlags.None;
}
private void ComponentCfgForm_Load(object sender, EventArgs e)
{
Text = Strings.Properties;
if (ComponentCfgCtrl != null)
{
var ctrl = (UserControl)ComponentCfgCtrl;
ctrl.Dock = DockStyle.Fill;
splitContainer.Panel1.Controls.Add(ctrl);
// autoresize dialog based on this control
ResizeToFitControl(ctrl);
if (ComponentCfgCtrl.ShowMore)
sharedButtons.OptionalButtons = SharedButtons.Buttons.More;
if (UnlockAfterStart)
{
sharedButtons.UnlockButtons();
ComponentCfgCtrl.Unlock();
}
}
}
/// <summary>
/// Handler of the Unlock button click
/// </summary>
private void Unlock(object sender, EventArgs e)
{
ComponentCfgCtrl.Unlock();
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
if (ComponentCfgCtrl == null) return CfgUpdateFlags.None; /// Nothing to verify -> OK
return ComponentCfgCtrl.VerifyCfg(ref message);
}
private void okButton_Click(object sender, EventArgs e)
{
string message = string.Empty;
CfgUpdateFlags flags = VerifyCfg(ref message);
if ((flags & CfgUpdateFlags.Error) == CfgUpdateFlags.Error)
{
MessageBox.Show(Strings.Please_change_the_following_settings + message,
Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
if (ComponentCfgCtrl != null)
{
flags = ComponentCfgCtrl.UpdateCfg();
/// Prevent component name duplicity
if (CmpntEntities != null)
{
foreach (var cmpnt in CmpntEntities)
{
if (ComponentCfgCtrl.Config.Name == cmpnt.Name && cmpntEntityId != cmpnt.Id)
{
MessageBox.Show("Component with the same name already exists");
return;
}
}
}
if ((flags & CfgUpdateFlags.RestartRqrd) == CfgUpdateFlags.RestartRqrd)
{
MessageBox.Show(Strings.Program_restart_is_required_to_apply_some_settings,
Strings.Warning, MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
ComponentCfgCtrl.Closing();
}
Flags = flags;
DialogResult = DialogResult.OK;
Close();
return;
}
private void cancelButton_Click(object sender, EventArgs e)
{
if (ComponentCfgCtrl != null) ComponentCfgCtrl.Closing();
DialogResult = DialogResult.Cancel;
Close();
return;
}
private void moreButton_Click(object sender, EventArgs e)
{
ComponentCfgCtrl.Unlock();
if (sharedButtons.MoreActive)
{
Height = Height + ComponentCfgCtrlMoreHeight;
}
else
{
Height = Height - ComponentCfgCtrlMoreHeight;
}
return;
}
private void ResizeToFitControl(UserControl cfgCtrl)
{
if (cfgCtrl == null) return;
// Ask the control for its preferred size
Size desired = cfgCtrl.PreferredSize;
// Extra space for right panel + form borders
int extraWidth = splitContainer.Panel2.Width + 40;
int extraHeight = 80;
this.Width = desired.Width + extraWidth;
this.Height = desired.Height + extraHeight;
// Do not exceed screen working area
var screen = Screen.FromControl(this).WorkingArea;
if (this.Width > screen.Width) this.Width = screen.Width - 20;
if (this.Height > screen.Height) this.Height = screen.Height - 20;
}
}
}