/// /// Copyright (c) 2015-2018 Sensus Slovensko a.s. /// using System; using System.IO; using System.Windows.Forms; using log4net; using Config.Entities; using TBF.BenchControl.Generic; using TBF.BenchControl.Configs; namespace TBF.BenchControl.Output.Printers.Label { 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); } } 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; templateTextBox.Text = config.Template; orientationComboBox.Text = config.PageOrientation.ToString(); paperWidthTextBox.Text = config.PaperWidth.ToString(); paperHeightTextBox.Text = config.PaperHeight.ToString(); 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(); nrOfCopiesTextBox.Text = config.NrOfCopies.ToString(); goodOnlyCheckBox.Checked = config.GoodOnly; supressPrintingCheckBox.Checked = config.SupressPrinting; } public void Unlock() { nameTextBox.Enabled = true; templateTextBox.Enabled = true; orientationComboBox.Enabled = true; paperWidthTextBox.Enabled = true; paperHeightTextBox.Enabled = true; itemsToPrintButton.Enabled = true; fontFamilyComboBox.Enabled = true; fontSizeTextBox.Enabled = true; boldCheckBox.Enabled = true; italicCheckBox.Enabled = true; cultureComboBox.Enabled = true; nrOfCopiesTextBox.Enabled = true; goodOnlyCheckBox.Enabled = true; supressPrintingCheckBox.Enabled = true; } public CfgUpdateFlags VerifyCfg(ref string message) { CfgUpdateFlags flags = CfgUpdateFlags.None; if (!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(paperWidthTextBox.Text, out dummy) || dummy <= 0) { message += Environment.NewLine + "Invalid paper width"; flags |= CfgUpdateFlags.Error; } if (!int.TryParse(paperHeightTextBox.Text, out dummy) || dummy <= 0) { message += Environment.NewLine + "Invalid paper 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; } 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); } flags |= UpdateDifferent(ref config.Template, templateTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); if (config.PageOrientation != GetOrientation(orientationComboBox.Text)) { config.PageOrientation = GetOrientation(orientationComboBox.Text); flags |= (CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); } flags |= UpdateDifferent(ref config.PaperWidth, paperWidthTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); flags |= UpdateDifferent(ref config.PaperHeight, paperHeightTextBox.Text, CfgUpdateFlags.InvokeCfgChange | CfgUpdateFlags.AnyChange); flags |= UpdateDifferent(ref config.ItemsToPrint, itemsToPrint, 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; } } 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.SupressPrinting, supressPrintingCheckBox.Checked, 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 CmdResponseHandler; public void StartResponseHandler() { } public void StopResponseHandler() { } #endregion Configuration Change Handling } }