Files
tbf/TBF/Rig/Output/Printers/GroupPrinting/Single/PrinterCfgCtrl.cs
T

292 lines
8.7 KiB
C#

///
/// Copyright (c) 2015-2023 Sensus Slovensko a.s.
///
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Printing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using log4net;
using Common;
using Config.Entities;
using FluentNHibernate.Conventions;
using log4net.Repository.Hierarchy;
using NHibernate;
using TBF.Resources;
using TBF.Rig.Generic;
using TBF.Rig.Configs;
namespace TBF.Rig.Output.Printers.GroupPrinting.Single
{
public partial class PrinterCfgCtrl : ConfigCtrlUtils, IComponentCfgCtrl
{
static readonly ILog log = LogManager.GetLogger(typeof(PrinterCfgCtrl));
public bool ShowMore
{
get { return false; }
}
IList<Component> cmpntEntities;
private ISession session;
PrinterCfg config;
public IComponentCfg Config
{
get { return config as IComponentCfg; }
set
{
config = value as PrinterCfg;
Redraw();
}
}
List<string> itemsToPrint;
public PrinterCfgCtrl()
{
InitializeComponent();
Localize();
}
private void PrinterCfgCtrl_Load(object sender, EventArgs e)
{
printerComboBox.Items.Add(Strings.default_printer);
foreach (var p in PrinterSettings.InstalledPrinters) printerComboBox.Items.Add(p);
EnableEdit(false);
InintComponents();
Redraw();
}
void Localize()
{
}
public void Closing()
{
// if (printerComboBox.Enabled && !printerComboBox.Text.IsEmpty())
// {
// GroupPrinterUtils.UpdateParametersInEntitys(session, cmpntEntities, printerComboBox.Text);
// }
}
void InintComponents()
{
cmpntEntities = cmpntEntities == null ? new List<Component>() : cmpntEntities;
itemsToPrint = config.ItemsToPrint?.ToList();
session = GroupPrinterUtils.GetCorrespondedEntities(in cmpntEntities, in itemsToPrint);
RedrawAll();
}
void Redraw()
{
if (config == null) return; /// Control was not loaded, settings were not changed
classNameLabel.Text = config.Factory.ClassName;
nameTextBox.Text = config.Name;
printerComboBox.Text =
string.IsNullOrEmpty(config.PrinterName) ? Strings.default_printer : config.PrinterName;
}
public void Unlock()
{
EnableEdit(true);
}
private void EnableEdit(bool bEnable)
{
nameTextBox.Enabled = bEnable;
printerComboBox.Enabled = bEnable;
selectPrinterButton.Enabled = bEnable;
btListAdd.Enabled = bEnable;
btListRemove.Enabled = bEnable;
btMoveUp.Enabled = bEnable;
btMoveDown.Enabled = bEnable;
listViewEx.Enabled = bEnable;
}
public CfgUpdateFlags VerifyCfg(ref string message)
{
CfgUpdateFlags flags = CfgUpdateFlags.None;
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);
}
string newPrinter = (printerComboBox.Text == Strings.default_printer) ? string.Empty : printerComboBox.Text;
if (config.PrinterName != newPrinter)
{
config.PrinterName = newPrinter;
flags |= (CfgUpdateFlags.RestartRqrd | CfgUpdateFlags.AnyChange);
}
if ((flags & CfgUpdateFlags.InvokeCfgChange) != 0)
{
Printer.OnCfgChange(this, new CfgChangeArgs(CfgChangeCmd.CfgChange, config));
}
List<string> itemsComponent = new List<string>();
foreach (Component entity in cmpntEntities)
{
itemsComponent.Add(PrinterDef.fromComponent(entity).ToString());
}
config.ItemsToPrint = itemsComponent.ToArray();
return flags;
}
#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
private void selectPrinterButton_Click(object sender, EventArgs e)
{
PrintDialog dlg = new PrintDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
printerComboBox.Text = dlg.PrinterSettings.PrinterName;
}
}
private void btListAdd_Click(object sender, EventArgs e)
{
PrinterTestBenchComponentSelectorDlg dlg = new PrinterTestBenchComponentSelectorDlg();
if (dlg.ShowDialog() == DialogResult.OK)
{
if (cmpntEntities == null)
{
cmpntEntities = new List<Component>();
}
else
{
if (cmpntEntities.Any(com => com.ItemNr == dlg.SelectedComponent.ItemNr))
{
MessageBox.Show("Item is in list included already!", "Warning", MessageBoxButtons.OK,
MessageBoxIcon.Warning);
return;
}
}
cmpntEntities.Add(dlg.SelectedComponent);
RedrawAll();
}
}
void RedrawAll()
{
listViewEx.Items.Clear();
foreach (var cmpnt in cmpntEntities) DrawOne(cmpnt);
}
void DrawOne(Component cmpnt)
{
ListViewItem lvi = new ListViewItem(cmpnt.ItemNr.ToString());
lvi.Tag = cmpnt;
lvi.SubItems.Add(cmpnt.Name);
listViewEx.Items.Add(lvi);
}
////////////////////////
private void btListRemove_Click(object sender, EventArgs e)
{
if (listViewEx.SelectedItems.Count > 0)
{
int selectedIndex = listViewEx.SelectedIndices[0];
Component cmpntEntity = cmpntEntities[selectedIndex];
if (MessageBox.Show($"Do you want remove {cmpntEntity.Name} from list?", "Remove Action",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.Yes)
{
cmpntEntities.Remove(cmpntEntity);
listViewEx.Items.RemoveAt(selectedIndex);
}
}
}
private void btMoveUp_Click(object sender, EventArgs e)
{
if (listViewEx.SelectedItems.Count > 0)
{
int selectedIndex = listViewEx.SelectedIndices[0];
if (selectedIndex > 0)
{
int newItemIndex = (selectedIndex - 1);
Component cmpntEntity = cmpntEntities[selectedIndex];
cmpntEntities.Insert(newItemIndex, cmpntEntity);
cmpntEntities.RemoveAt(selectedIndex + 1);
RedrawAll();
listViewEx.Items[newItemIndex].Selected = true;
}
}
}
private void btMoveDown_Click(object sender, EventArgs e)
{
if (listViewEx.SelectedItems.Count > 0)
{
int selectedIndex = listViewEx.SelectedIndices[0];
if (selectedIndex < listViewEx.Items.Count - 1)
{
Component cmpntEntity = cmpntEntities[selectedIndex];
int newItemIndex = selectedIndex + 2;
cmpntEntities.Insert(newItemIndex, cmpntEntity);
cmpntEntities.RemoveAt(selectedIndex);
RedrawAll();
listViewEx.Items[newItemIndex - 1].Selected = true;
}
}
}
}
}