123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using Comal.Classes;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.DynamicGrid;
- using NPOI.HSSF.Util;
- using Syncfusion.Windows.Tools.Controls;
- namespace PRSDesktop
- {
- public class ProductGroupSelectorData
- {
- // FullGuid => All Staff
- public Guid[] Groups { get; set; }
-
- public ProductGroupSelectorData(Guid[] groups)
- {
- Groups = groups;
- }
- public ProductGroupSelectorData() : this(new[] { CoreUtils.FullGuid })
- {
- }
- }
- public class ProductGroupSelectorSettings
- {
- public ProductGroupSelectorSettings()
- {
- }
- }
-
- public class ProductGroupSelectorSettingsChangedArgs : EventArgs
- {
- public ProductGroupSelectorSettings Settings { get; private set; }
- public ProductGroupSelectorSettingsChangedArgs(ProductGroupSelectorSettings settings)
- {
- Settings = settings;
- }
- }
-
- public delegate void ProductGroupSelectorSettingsChanged(object sender, ProductGroupSelectorSettingsChangedArgs args);
-
- public class ProductGroupSelectorSelectionChangedArgs : EventArgs
- {
- public ProductGroupSelectorData Selection { get; private set; }
- public ProductGroupSelectorSelectionChangedArgs(ProductGroupSelectorData selection)
- {
- Selection = selection;
- }
- }
- public delegate void ProductGroupSelectorSelectionChanged(object sender, ProductGroupSelectorSelectionChangedArgs args);
- public partial class ProductGroupSelector : UserControl
- {
- private enum Suppress
- {
- This
- }
- private class ListEntry
- {
- public Guid Key { get; set; }
- public String Value { get; set; }
- }
- public ProductGroupSelectorSettings Settings { get; set; }
- public event ProductGroupSelectorSettingsChanged SettingsChanged;
- public event ProductGroupSelectorSelectionChanged SelectionChanged;
-
- private void UpdateSettings()
- {
- bool changed = false;
- if (changed && (!EventSuppressor.IsSet(Suppress.This)))
- SettingsChanged?.Invoke(this, new ProductGroupSelectorSettingsChangedArgs(Settings));
- }
-
- private CoreTable _productgroups;
-
- public ProductGroupSelector()
- {
- Settings = new ProductGroupSelectorSettings();
- using (new EventSuppressor(Suppress.This))
- InitializeComponent();
- }
-
- public void Setup()
- {
- using (new EventSuppressor(Suppress.This))
- {
- MultiQuery query = new MultiQuery();
-
- query.Add<ProductGroup>(
- LookupFactory.DefineFilter<ProductGroup>(),
- Columns.None<ProductGroup>().Add(x => x.ID)
- .Add(x => x.Code)
- .Add(x=>x.Description),
- new SortOrder<ProductGroup>(x => x.Code)
- );
-
- query.Query();
- _productgroups = query.Get<ProductGroup>();
- SelectedGroups.ItemsSource = _productgroups.Rows.Select(
- r => new ListEntry()
- {
- Key = r.Get<ProductGroup, Guid>(c => c.ID),
- Value = String.Format("{0}: {1}", r.Get<ProductGroup, String>(c => c.Code), r.Get<ProductGroup, String>(c => c.Description))
- }
- ).ToArray();
-
- }
- }
-
- public T[] GetGroupData<T>(Func<CoreRow, T> transform)
- {
- List<T> result = new List<T>();
- var groups = GetSelectedGroups();
- var rows = _productgroups.Rows.Where(r => groups.Contains(CoreUtils.FullGuid) || groups.Contains(r.Get<ProductGroup, Guid>(c => c.ID)));
- foreach (var row in rows)
- result.Add(transform.Invoke(row));
- return result.ToArray();
- }
- private Guid[] GetSelectedGroups()
- {
- var jobs = (SelectedGroups.SelectedItems.Count == SelectedGroups.Items.Count)
- ? new Guid[] { CoreUtils.FullGuid }
- : SelectedGroups.SelectedItems
- .OfType<ListEntry>()
- .Select(x => x.Key)
- .ToArray();
- return jobs;
- }
- public ProductGroupSelectorData Selection
- {
- get => GetSelectionData();
- set => SelectData(value);
- }
-
- private ProductGroupSelectorData GetSelectionData()
- {
- return new ProductGroupSelectorData(
- GetSelectedGroups()
- );
- }
-
- private void SelectData(ProductGroupSelectorData selection)
- {
- using (new EventSuppressor(Suppress.This))
- {
- var sels = SelectedGroups.Items
- .OfType<ListEntry>()
- .Where(x => selection.Groups.Contains(CoreUtils.FullGuid) || selection.Groups.Contains(x.Key));
- SelectedGroups.SelectedItems.Clear();
- foreach (var sel in sels)
- SelectedGroups.SelectedItems.Add(sel);
- }
- }
- private void SelectedGroups_OnItemChecked(object? sender, ItemCheckedEventArgs e)
- {
- if (EventSuppressor.IsSet(Suppress.This))
- return;
- UpdateSettings();
- SelectionChanged?.Invoke(this, new ProductGroupSelectorSelectionChangedArgs(GetSelectionData()));
- }
- private void SelectAll_OnClick(object sender, RoutedEventArgs e)
- {
- using (new EventSuppressor(Suppress.This))
- {
- var selected = SelectedGroups.SelectedItems
- .OfType<ListEntry>()
- .Select(x=>x.Key)
- .ToArray();
-
- var items = SelectedGroups.Items
- .OfType<ListEntry>()
- .ToArray();
- if (items.Length == selected.Length)
- {
- SelectedGroups.SelectedItems.Clear();
- SelectAll.Content = "All";
- }
- else
- {
- var unselected = items.Where(p => !selected.Contains(p.Key));
- foreach (var item in unselected)
- SelectedGroups.SelectedItems.Add(item);
- SelectAll.Content = "None";
- }
- UpdateSettings();
- SelectionChanged?.Invoke(this, new ProductGroupSelectorSelectionChangedArgs(GetSelectionData()));
- }
- }
- }
- }
|