tbf/TBF/BenchControl/Output/Printers/MultiLabel/PrinterCfgCtrl.cs
2020-03-06 08:21:24 +01:00

311 lines
12 KiB
C#

///
/// Copyright (c) 2020 Sensus Slovensko a.s.
///
using System;
using System.IO;
using System.Windows.Forms;
using log4net;
using Config.Entities;
using Results.Output.Printers;
using TBF.BenchControl.Generic;
using TBF.BenchControl.Configs;
namespace TBF.BenchControl.Output.Printers.MultiLabel
{
public partial class PrinterCfgCtrl : ConfigCtrlUtils, 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[] itemsToPrint;
public PrinterCfgCtrl()
{
InitializeComponent();
Localize();
orientationComboBox.Items.Add(PageOrientation.Portrait.ToString());
orientationComboBox.Items.Add(PageOrientation.Landscape.ToString());
foreach (var ff in System.Drawing.FontFamily.Families)
{
fontFamilyComboBox.Items.Add(ff.Name);
}
for (BarcodeType bt = BarcodeType.None; bt < BarcodeType.Count; bt++)
{
barcodeTypeComboBox.Items.Add(bt.ToString());
}
}
private void PrinterCfgCtrl_Load(object sender, EventArgs e)
{
for (Culture s = 0; s < Culture.Count; s++) cultureComboBox.Items.Add(s.ToString());
itemsToPrint = config.ItemsToPrint;
Redraw();
}
void Localize()
{
}
public void Closing()
{
}
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();
labelColumnsCountTextBox.Text = config.LabelColumnsCount.ToString();
leftTopLabelLeftTextBox.Text = config.LeftTopLabelLeft.ToString();
leftTopLabelTopTextBox.Text = config.LeftTopLabelTop.ToString();
nrOfCopiesTextBox.Text = config.NrOfCopies.ToString();
goodOnlyCheckBox.Checked = config.GoodOnly;
labelWidthTextBox.Text = config.LabelWidth.ToString();
labelHeightTextBox.Text = config.LabelHeight.ToString();
templateTextBox.Text = config.Template;
fontFamilyComboBox.Text = config.FontFamily;
fontSizeTextBox.Text = config.FontSize.ToString();
boldCheckBox.Checked = ((config.FontStyle & (int)System.Drawing.FontStyle.Bold) != 0);
italicCheckBox.Checked = ((config.FontStyle & (int)System.Drawing.FontStyle.Italic) != 0);
cultureComboBox.Text = config.Culture.ToString();
barcodeTypeComboBox.Text = config.BarcodeType.ToString();
barcodeLeftTextBox.Text = config.BarcodeLeft.ToString();
barcodeTopTextBox.Text = config.BarcodeTop.ToString();
barcodeWidthTextBox.Text = config.BarcodeWidth.ToString();
barcodeHeightTextBox.Text = config.BarcodeHeight.ToString();
}
public void Unlock()
{
nameTextBox.Enabled = true;
orientationComboBox.Enabled = true;
labelColumnsCountTextBox.Enabled = true;
leftTopLabelLeftTextBox.Enabled = true;
leftTopLabelTopTextBox.Enabled = true;
nrOfCopiesTextBox.Enabled = true;
goodOnlyCheckBox.Enabled = true;
itemsToPrintButton.Enabled = true;
labelWidthTextBox.Enabled = true;
labelHeightTextBox.Enabled = true;
templateTextBox.Enabled = true;
fontFamilyComboBox.Enabled = true;
fontSizeTextBox.Enabled = true;
boldCheckBox.Enabled = true;
italicCheckBox.Enabled = true;
cultureComboBox.Enabled = true;
barcodeTypeComboBox.Enabled = true;
barcodeLeftTextBox.Enabled = true;
barcodeTopTextBox.Enabled = true;
barcodeWidthTextBox.Enabled = true;
barcodeHeightTextBox.Enabled = true;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
if (!string.IsNullOrEmpty(templateTextBox.Text) && !File.Exists(Path.Combine("C:\\TBF\\Templates", templateTextBox.Text)))
{
message += Environment.NewLine + "Template file missing";
flags |= CfgUpdateFlags.Error;
}
if ((int)GetOrientation(orientationComboBox.Text) < 0)
{
message += Environment.NewLine + "Invalid page orientation";
flags |= CfgUpdateFlags.Error;
}
int dummy;
if (!int.TryParse(labelColumnsCountTextBox.Text, out dummy) || dummy < 1)
{
message += Environment.NewLine + "Invalid label columns count";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(leftTopLabelLeftTextBox.Text, out dummy) || dummy < 0)
{
message += Environment.NewLine + "Invalid left-top label left";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(leftTopLabelTopTextBox.Text, out dummy) || dummy < 0)
{
message += Environment.NewLine + "Invalid left-top label top";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(labelWidthTextBox.Text, out dummy) || dummy <= 0)
{
message += Environment.NewLine + "Invalid label width";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(labelHeightTextBox.Text, out dummy) || dummy <= 0)
{
message += Environment.NewLine + "Invalid label height";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(fontSizeTextBox.Text, out dummy) || dummy < 6 || dummy > 48)
{
message += Environment.NewLine + "Font size' should be between 6 and 48";
flags |= CfgUpdateFlags.Error;
}
if (!cultureComboBox.Items.Contains(cultureComboBox.Text))
{
message += Environment.NewLine + "Invalid culture";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(nrOfCopiesTextBox.Text, out dummy) || dummy < 0 || dummy > 9)
{
message += Environment.NewLine + "'Number of copies' should be between 0 and 9";
flags |= CfgUpdateFlags.Error;
}
if (!barcodeTypeComboBox.Items.Contains(barcodeTypeComboBox.Text))
{
message += Environment.NewLine + "Invalid barcode type";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(barcodeLeftTextBox.Text, out dummy) || dummy < 0 || dummy > 2000)
{
message += Environment.NewLine + "Barcode 'Left' should be between 0 and 2000";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(barcodeTopTextBox.Text, out dummy) || dummy < 0 || dummy > 2000)
{
message += Environment.NewLine + "Barcode 'Top' should be between 0 and 2000";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(barcodeWidthTextBox.Text, out dummy) || dummy < 0 || dummy > 200)
{
message += Environment.NewLine + "Barcode 'Width' should be between 0 and 200";
flags |= CfgUpdateFlags.Error;
}
if (!int.TryParse(barcodeHeightTextBox.Text, out dummy) || dummy < 0 || dummy > 200)
{
message += Environment.NewLine + "Barcode 'Height' should be between 0 and 200";
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);
}
flags |= UpdateDifferent(ref config.LabelColumnsCount, labelColumnsCountTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.LeftTopLabelLeft, leftTopLabelLeftTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.LeftTopLabelTop, leftTopLabelTopTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.NrOfCopies, nrOfCopiesTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.GoodOnly, goodOnlyCheckBox.Checked, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.ItemsToPrint, itemsToPrint, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.LabelWidth, labelWidthTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.LabelHeight, labelHeightTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.Template, templateTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
/// Body font properties
flags |= UpdateDifferent(ref config.FontFamily, fontFamilyComboBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.FontSize, int.Parse(fontSizeTextBox.Text), CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
int tmp = (boldCheckBox.Checked ? (int)System.Drawing.FontStyle.Bold : 0)
+ (italicCheckBox.Checked ? (int)System.Drawing.FontStyle.Italic : 0);
flags |= UpdateDifferent(ref config.FontStyle, tmp, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
for (Culture i = 0; i < Culture.Count; i++)
{
if (i.ToString().Equals(cultureComboBox.Text) && (config.Culture != i))
{
config.Culture = i;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
break;
}
}
for (BarcodeType bt = BarcodeType.None; bt < BarcodeType.Count; bt++)
{
if (bt.ToString().Equals(barcodeTypeComboBox.Text))
{
config.BarcodeType = bt;
flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
break;
}
}
flags |= UpdateDifferent(ref config.BarcodeLeft, barcodeLeftTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.BarcodeTop, barcodeTopTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.BarcodeWidth, barcodeWidthTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
flags |= UpdateDifferent(ref config.BarcodeHeight, barcodeHeightTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange);
if ((flags & CfgUpdateFlags.InvokeCfgChange) != 0)
{
Printer.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config));
}
return flags;
}
private void itemsToPrintButton_Click(object sender, EventArgs e)
{
Results.Forms.ResultsConfigDlg dlg = new Results.Forms.ResultsConfigDlg()
{
SelectedItems = Results.WMeterRsltItemSpec.FromStrArray(itemsToPrint),
};
if (dlg.ShowDialog() == DialogResult.OK)
{
itemsToPrint = Results.WMeterRsltItemSpec.ToStrArray(dlg.SelectedItems);
}
}
#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
}
}