tbf/TestBenchFramework/BenchControl/Output/Printers/Zapiska/PrinterCfgCtrl.cs
2017-09-26 14:45:49 +02:00

580 lines
20 KiB
C#

///
/// Copyright (c) 2017 Sensus Metering Systems
///
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using log4net;
using Config.Entities;
using TBF.BenchControl.Generic;
using TBF.Resources;
namespace TBF.BenchControl.Output.Printers.Zapiska
{
public partial class PrinterCfgCtrl : UserControl, IComponentCfgCtrl
{
static readonly ILog log = LogManager.GetLogger(typeof(PrinterCfgCtrl));
public bool ShowMore { get { return false; } }
PrinterCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as PrinterCfg;
Redraw();
}
}
string header;
string[] commonItems1;
string[] commonItems2;
string[] commonItems3;
string[] testItems;
string[] belowItems1;
string[] belowItems2;
string[] belowItems3;
string footer;
public PrinterCfgCtrl()
{
InitializeComponent();
Localize();
orientationComboBox.Items.Add(PageOrientation.Portrait.ToString());
orientationComboBox.Items.Add(PageOrientation.Landscape.ToString());
foreach (var ff in System.Drawing.FontFamily.Families)
{
titleFontFamilyComboBox.Items.Add(ff.Name);
headerFontFamilyComboBox.Items.Add(ff.Name);
bodyFontFamilyComboBox.Items.Add(ff.Name);
}
}
private void PrinterCfgCtrl_Load(object sender, EventArgs e)
{
for (Culture s = 0; s < Culture.Count; s++) cultureComboBox.Items.Add(s.ToString());
header = config.Header;
commonItems1 = config.CommonItems1;
commonItems2 = config.CommonItems2;
commonItems3 = config.CommonItems3;
testItems = config.SelectedItems;
belowItems1 = config.BelowItems1;
belowItems2 = config.BelowItems2;
belowItems3 = config.BelowItems3;
footer = config.Footer;
Redraw();
}
void Localize()
{
nameLabel.Text = Strings.Name;
cultureLabel.Text = "Culture";
headerButton.Text = Strings.Header;
commonItems1Button.Text = "Common 1";
commonItems2Button.Text = "Common 2";
commonItems3Button.Text = "Common 3";
testItemsButton.Text = "Test items";
footerButton.Text = Strings.Footer;
}
public void Closing()
{
}
PageOrientation GetOrientation(string str)
{
if (str.Equals(PageOrientation.Landscape.ToString())) return PageOrientation.Landscape;
if (str.Equals(PageOrientation.Portrait.ToString())) return PageOrientation.Portrait;
return (PageOrientation)(-1);
}
void Redraw()
{
if (config == null) return; /// Control was not loaded, settings were not changed
classNameLabel.Text = config.Factory.ClassName;
nameTextBox.Text = config.Name;
orientationComboBox.Text = config.PageOrientation.ToString();
topMarginTextBox.Text = config.TopMargin.ToString();
bottomMarginTextBox.Text = config.BottomMargin.ToString();
leftMarginTextBox.Text = config.LeftMargin.ToString();
titleFontFamilyComboBox.Text = config.TitFntFml;
titleFontSizeTextBox.Text = config.TitFntSz.ToString();
titleBoldCheckBox.Checked = ((config.TitFntSty & (int)System.Drawing.FontStyle.Bold) != 0);
titleItalicCheckBox.Checked = ((config.TitFntSty & (int)System.Drawing.FontStyle.Italic) != 0);
headerFontFamilyComboBox.Text = config.HdrFntFml;
headerFontSizeTextBox.Text = config.HdrFntSz.ToString();
headerBoldCheckBox.Checked = ((config.HdrFntSty & (int)System.Drawing.FontStyle.Bold) != 0);
headerItalicCheckBox.Checked = ((config.HdrFntSty & (int)System.Drawing.FontStyle.Italic) != 0);
bodyFontFamilyComboBox.Text = config.BodyFntFml;
bodyFontSizeTextBox.Text = config.BodyFntSz.ToString();
bodyBoldCheckBox.Checked = ((config.BodyFntSty & (int)System.Drawing.FontStyle.Bold) != 0);
bodyItalicCheckBox.Checked = ((config.BodyFntSty & (int)System.Drawing.FontStyle.Italic) != 0);
colW0TextBox.Text = config.ColW0Pct.ToString();
colW1TextBox.Text = config.ColW1Pct.ToString();
colW2TextBox.Text = config.ColW2Pct.ToString();
belowW0TextBox.Text = config.BelowW0Pct.ToString();
belowW1TextBox.Text = config.BelowW1Pct.ToString();
belowW2TextBox.Text = config.BelowW2Pct.ToString();
supressPrintingCheckBox.Checked = config.SupressPrinting;
goodOnlyCheckBox.Checked = config.GoodOnly;
cultureComboBox.Text = config.Culture.ToString();
}
public void Unlock()
{
nameTextBox.Enabled = true;
orientationComboBox.Enabled = true;
topMarginTextBox.Enabled = true;
bottomMarginTextBox.Enabled = true;
leftMarginTextBox.Enabled = true;
titleFontFamilyComboBox.Enabled = true;
titleFontSizeTextBox.Enabled = true;
titleBoldCheckBox.Enabled = true;
titleItalicCheckBox.Enabled = true;
headerFontFamilyComboBox.Enabled = true;
headerFontSizeTextBox.Enabled = true;
headerBoldCheckBox.Enabled = true;
headerItalicCheckBox.Enabled = true;
bodyFontFamilyComboBox.Enabled = true;
bodyFontSizeTextBox.Enabled = true;
bodyBoldCheckBox.Enabled = true;
bodyItalicCheckBox.Enabled = true;
supressPrintingCheckBox.Enabled = true;
goodOnlyCheckBox.Enabled = true;
cultureComboBox.Enabled = true;
headerButton.Enabled = true;
colW0TextBox.Enabled = true;
colW1TextBox.Enabled = true;
colW2TextBox.Enabled = true;
commonItems1Button.Enabled = true;
commonItems2Button.Enabled = true;
commonItems3Button.Enabled = true;
testItemsButton.Enabled = true;
belowW0TextBox.Enabled = true;
belowW1TextBox.Enabled = true;
belowW2TextBox.Enabled = true;
belowItems1Button.Enabled = true;
belowItems2Button.Enabled = true;
belowItems3Button.Enabled = true;
footerButton.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
if ((int)GetOrientation(orientationComboBox.Text) < 0)
{
message += Environment.NewLine + "Invalid 'Page orientation'";
flags |= CfgUpdateFlags.Error;
}
int dummy, dummy2, dummy3;
if (!int.TryParse(topMarginTextBox.Text, out dummy) || dummy < 0 || dummy > 500)
{
message += Environment.NewLine + "'Top margin' should be between 0 and 500";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(bottomMarginTextBox.Text, out dummy) || dummy < 0 || dummy > 200)
{
message += Environment.NewLine + "'Bottom margin' should be between 0 and 200";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(leftMarginTextBox.Text, out dummy) || dummy < 0 || dummy > 200)
{
message += Environment.NewLine + "Left margin' should be between 0 and 200";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(titleFontSizeTextBox.Text, out dummy) || dummy < 6 || dummy > 48)
{
message += Environment.NewLine + "Title font size should be between 6 and 48";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(headerFontSizeTextBox.Text, out dummy) || dummy < 6 || dummy > 48)
{
message += Environment.NewLine + "Header font size should be between 6 and 48";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(bodyFontSizeTextBox.Text, out dummy) || dummy < 6 || dummy > 48)
{
message += Environment.NewLine + "Body font size should be between 6 and 48";
flags |= CfgUpdateFlags.Error;
}
if (!cultureComboBox.Items.Contains(cultureComboBox.Text))
{
flags |= CfgUpdateFlags.Error;
message += Environment.NewLine + "Invalid culture";
}
if (!int.TryParse(colW0TextBox.Text, out dummy) || dummy < 0 || dummy > 100)
{
message += Environment.NewLine + "Column width 0 should be between 0 and 100";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(colW1TextBox.Text, out dummy2) || dummy2 < 0 || dummy2 > 100)
{
message += Environment.NewLine + "Column width 1 should be between 0 and 100";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(colW2TextBox.Text, out dummy3) || dummy3 < 0 || dummy3 > 100)
{
message += Environment.NewLine + "Column width 2 should be between 0 and 100";
flags |= CfgUpdateFlags.Error;
}
if (dummy + dummy2 + dummy3 > 100)
{
message += Environment.NewLine + "Sum of column widths should be <= 100";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(belowW0TextBox.Text, out dummy) || dummy < 0 || dummy > 100)
{
message += Environment.NewLine + "Column width 0 should be between 0 and 100";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(belowW1TextBox.Text, out dummy2) || dummy2 < 0 || dummy2 > 100)
{
message += Environment.NewLine + "Column width 1 should be between 0 and 100";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(belowW2TextBox.Text, out dummy3) || dummy3 < 0 || dummy3 > 100)
{
message += Environment.NewLine + "Column width 2 should be between 0 and 100";
flags |= CfgUpdateFlags.Error;
}
if (dummy + dummy2 + dummy3 > 100)
{
message += Environment.NewLine + "Sum of column widths should be <= 100";
flags |= CfgUpdateFlags.Error;
}
return flags;
}
public CfgUpdateFlags UpdateCfg()
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
if (config == null) return CfgUpdateFlags.Error; /// Control was not loaded, settings were not changed
if (config.Name != nameTextBox.Text)
{
config.Name = nameTextBox.Text;
flags |= (CfgUpdateFlags.RestartRqrd | CfgUpdateFlags.AnyChange);
}
if (config.PageOrientation != GetOrientation(orientationComboBox.Text))
{
config.PageOrientation = GetOrientation(orientationComboBox.Text);
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
int tmp = int.Parse(topMarginTextBox.Text);
if (config.TopMargin != tmp)
{
config.TopMargin = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(bottomMarginTextBox.Text);
if (config.BottomMargin != tmp)
{
config.BottomMargin = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(leftMarginTextBox.Text);
if (config.LeftMargin != tmp)
{
config.LeftMargin = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
/// Title font properties
if (config.TitFntFml != titleFontFamilyComboBox.Text)
{
config.TitFntFml = titleFontFamilyComboBox.Text;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(titleFontSizeTextBox.Text);
if (config.TitFntSz != tmp)
{
config.TitFntSz = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = (titleBoldCheckBox.Checked ? (int)System.Drawing.FontStyle.Bold : 0)
+ (titleItalicCheckBox.Checked ? (int)System.Drawing.FontStyle.Italic : 0);
if (config.TitFntSty != tmp)
{
config.TitFntSty = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
/// Header font properties
if (config.HdrFntFml != headerFontFamilyComboBox.Text)
{
config.HdrFntFml = headerFontFamilyComboBox.Text;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(headerFontSizeTextBox.Text);
if (config.HdrFntSz != tmp)
{
config.HdrFntSz = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = (headerBoldCheckBox.Checked ? (int)System.Drawing.FontStyle.Bold : 0)
+ (headerItalicCheckBox.Checked ? (int)System.Drawing.FontStyle.Italic : 0);
if (config.HdrFntSty != tmp)
{
config.HdrFntSty = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
/// Body font properties
if (config.BodyFntFml != bodyFontFamilyComboBox.Text)
{
config.BodyFntFml = bodyFontFamilyComboBox.Text;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(bodyFontSizeTextBox.Text);
if (config.BodyFntSz != tmp)
{
config.BodyFntSz = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = (bodyBoldCheckBox.Checked ? (int)System.Drawing.FontStyle.Bold : 0)
+ (bodyItalicCheckBox.Checked ? (int)System.Drawing.FontStyle.Italic : 0);
if (config.BodyFntSty != tmp)
{
config.BodyFntSty = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.SupressPrinting != supressPrintingCheckBox.Checked)
{
config.SupressPrinting = supressPrintingCheckBox.Checked;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.GoodOnly != goodOnlyCheckBox.Checked)
{
config.GoodOnly = goodOnlyCheckBox.Checked;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(colW0TextBox.Text);
if (config.ColW0Pct != tmp)
{
config.ColW0Pct = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(colW1TextBox.Text);
if (config.ColW1Pct != tmp)
{
config.ColW1Pct = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(colW2TextBox.Text);
if (config.ColW2Pct != tmp)
{
config.ColW2Pct = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(belowW0TextBox.Text);
if (config.BelowW0Pct != tmp)
{
config.BelowW0Pct = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(belowW1TextBox.Text);
if (config.BelowW1Pct != tmp)
{
config.BelowW1Pct = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
tmp = int.Parse(belowW2TextBox.Text);
if (config.BelowW2Pct != tmp)
{
config.BelowW2Pct = tmp;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
}
if (config.Header != header) { config.Header = header; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.CommonItems1 != commonItems1) { config.CommonItems1 = commonItems1; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.CommonItems2 != commonItems2) { config.CommonItems2 = commonItems2; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.CommonItems3 != commonItems3) { config.CommonItems3 = commonItems3; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.SelectedItems != testItems) { config.SelectedItems = testItems; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.BelowItems1 != belowItems1) { config.BelowItems1 = belowItems1; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.BelowItems2 != belowItems2) { config.BelowItems2 = belowItems2; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.BelowItems3 != belowItems3) { config.BelowItems3 = belowItems3; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if (config.Footer != footer) { config.Footer = footer; flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); }
if ((flags & CfgUpdateFlags.InvokeCfgChange) != 0)
{
Printer.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config));
}
return flags;
}
private void headerButton_Click(object sender, EventArgs e)
{
HeaderFooterDlg dlg = new HeaderFooterDlg(true, string.IsNullOrEmpty(header) ? string.Empty : header.Replace("~", Environment.NewLine));
if (dlg.ShowDialog() == DialogResult.OK)
{
header = dlg.EditedText.Replace(Environment.NewLine, "~");
}
}
private void commonItems1Button_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(commonItems1),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
commonItems1 = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void commonItems2Button_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(commonItems2),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
commonItems2 = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void commonItems3Button_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(commonItems3),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
commonItems3 = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void testItemsButton_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg(true)
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(testItems),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
testItems = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void belowItems1Button_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(belowItems1),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
belowItems1 = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void belowItems2Button_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(belowItems2),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
belowItems2 = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void belowItems3Button_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
MetersKind = config.MetersKind,
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(belowItems3),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
belowItems3 = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
private void footerButton_Click(object sender, EventArgs e)
{
HeaderFooterDlg dlg = new HeaderFooterDlg(false, string.IsNullOrEmpty(footer) ? string.Empty : footer.Replace("~", Environment.NewLine));
if (dlg.ShowDialog() == DialogResult.OK)
{
footer = dlg.EditedText.Replace(Environment.NewLine, "~");
}
}
#region Configuration Change Handling
public static void OnCmdResponse(object sender, CmdResponseArgs args)
{
if (CmdResponseHandler == null) return;
try { CmdResponseHandler(sender, args); }
catch (Exception e) { log.Error("CmdResponseHandler(...) failed", e); }
}
public static event EventHandler<CmdResponseArgs> CmdResponseHandler;
public void StartResponseHandler() { }
public void StopResponseHandler() { }
#endregion Configuration Change Handling
}
}