123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Configuration;
- using InABox.Core;
- using PRSDesktop.Utils;
- using Syncfusion.Windows.ComponentModel;
- using Syncfusion.Windows.Controls.Grid;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for FactoryTemplateItemsPanel.xaml
- /// </summary>
- public partial class FactoryTemplateItemsPanel : UserControl
- {
- private readonly List<FactoryTemplateItem> _items = new();
- private Type _type = typeof(FactoryTemplateItem);
- private FactorySetup settings;
- public FactoryTemplateItemsPanel()
- {
- InitializeComponent();
- Grid.Model.RowCount = 0;
- Grid.Model.ColumnCount = 6;
- Grid.Model.ColumnWidths[0] = 20;
- Grid.Model.ColumnWidths[1] = 20;
- Grid.Model.ColumnWidths[2] = Grid.ActualWidth - 160;
- Grid.Model.ColumnWidths[3] = 50;
- Grid.Model.ColumnWidths[4] = 50;
- Grid.Model.ColumnWidths[5] = 20;
- Grid.Model.HeaderColumns = 0;
- }
- public IList<T> GetItems<T>() where T : FactoryTemplateItem
- {
- var result = new List<T>();
- foreach (var item in _items)
- result.Add((T)item);
- return result;
- }
- public void SetItems<T>(IList<T> items) where T : FactoryTemplateItem
- {
- settings = new GlobalConfiguration<FactorySetup>().Load();
- _items.Clear();
- _items.AddRange(items);
- _type = typeof(T);
- Grid.Model.RowCount = 0;
- Grid.Model.RowCount = items.Count + 2;
- }
- private FactoryTemplateItem CreateItem()
- {
- return (FactoryTemplateItem)Activator.CreateInstance(_type);
- }
- private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
- {
- Grid.Model.ColumnWidths[2] = e.NewSize.Width - 160;
- }
- private void Grid_QueryCellInfo(object sender, GridQueryCellInfoEventArgs e)
- {
- try
- {
- var row = e.Cell.RowIndex - 1;
- var col = e.Cell.ColumnIndex;
- switch (col)
- {
- case 0:
- if (row != 0 && row < _items.Count)
- GridUtils.SetButtonImage(e.Style, PRSDesktop.Resources.uparrow, Grid.Model, col);
- else
- GridUtils.SetButtonImage(e.Style, null, Grid.Model, col);
- break;
- case 1:
- if (row < _items.Count - 1)
- GridUtils.SetButtonImage(e.Style, PRSDesktop.Resources.downarrow, Grid.Model, col);
- else
- GridUtils.SetButtonImage(e.Style, null, Grid.Model, col);
- break;
- case 2:
- if (row == -1)
- {
- e.Style.CellValue = "Item Type";
- }
- else
- {
- e.Style.CellType = "ComboBox";
- var items = new List<FactoryTemplateItem>();
- items.AddRange(settings.TemplateItems);
- if (row == _items.Count)
- items.Add(new FactoryTemplateItem { Code = "", Description = "Add new..", Qty = 1 });
- e.Style.ItemsSource = items.Select(x => new { x.Description }).ToList();
- e.Style.DropDownStyle = GridDropDownStyle.AutoComplete;
- e.Style.DisplayMember = "Description";
- e.Style.ValueMember = "Description";
- e.Style.CellValue = row < _items.Count ? _items[row].Description : "Add new..";
- }
- e.Style.VerticalAlignment = VerticalAlignment.Center;
- break;
- case 3:
- e.Style.CellValue = row == -1 ? "Code" : row < _items.Count ? _items[row].Code : "";
- e.Style.HorizontalAlignment = HorizontalAlignment.Center;
- e.Style.VerticalAlignment = VerticalAlignment.Center;
- break;
- case 4:
- e.Style.CellValue = row == -1 ? "Qty" : row < _items.Count ? _items[row].Qty.ToString() : "";
- e.Style.HorizontalAlignment = HorizontalAlignment.Center;
- e.Style.VerticalAlignment = VerticalAlignment.Center;
- break;
- case 5:
- if (row < _items.Count)
- GridUtils.SetButtonImage(e.Style, PRSDesktop.Resources.delete, Grid.Model, col);
- else
- GridUtils.SetButtonImage(e.Style, null, Grid.Model, col);
- break;
- }
- }
- catch (Exception err)
- {
- Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", err.Message, err.StackTrace));
- }
- }
- private void Grid_CommitCellInfo(object sender, GridCommitCellInfoEventArgs e)
- {
- FactoryTemplateItem item;
- if (e.Style.RowIndex > _items.Count)
- {
- item = CreateItem();
- _items.Add(item);
- }
- else
- {
- item = _items[e.Style.RowIndex - 1];
- }
- switch (e.Cell.ColumnIndex)
- {
- case 2:
- item.Description = Convert.ToString(e.Style.CellValue);
- var lookup = settings.TemplateItems.Where(x => x.Description.Equals(item.Description)).FirstOrDefault();
- item.Code = lookup.Code;
- item.Qty = lookup.Qty;
- break;
- case 3:
- item.Code = Convert.ToString(e.Style.CellValue);
- break;
- case 4:
- item.Qty = Convert.ToInt32(e.Style.CellValue.ToString());
- break;
- }
- Grid.Model.RowCount = 0;
- Grid.Model.RowCount = _items.Count + 2;
- }
- private void Grid_CellClick(object sender, GridCellClickEventArgs e)
- {
- var row = e.RowIndex - 1;
- if (row > -1 && row < _items.Count)
- {
- var item = _items[row];
- switch (e.ColumnIndex)
- {
- case 0:
- if (row > 0)
- {
- _items.Remove(item);
- _items.Insert(row - 1, item);
- e.Handled = true;
- }
- break;
- case 1:
- if (e.RowIndex < settings.Sections.Count - 1)
- {
- _items.Remove(item);
- _items.Insert(row + 1, item);
- e.Handled = true;
- }
- break;
- case 5:
- _items.Remove(item);
- e.Handled = true;
- break;
- }
- if (e.Handled)
- {
- Grid.Model.RowCount = 0;
- Grid.Model.RowCount = _items.Count + 2;
- }
- }
- }
- private void Grid_CurrentCellValidated(object sender, SyncfusionRoutedEventArgs args)
- {
- }
- }
- }
|