123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using System.Windows.Media.Imaging;
- using Comal.Classes;
- using InABox.Clients;
- using InABox.Configuration;
- using InABox.Core;
- using InABox.Dxf;
- using InABox.Wpf;
- using InABox.WPF;
- using Microsoft.Win32;
- namespace PRSDesktop;
- public class ProductDockFilterButton : FilterButton<Product>
- {
- public ProductDockFilterButton() : base(
- new GlobalConfiguration<CoreFilterDefinitions>("ProductDock.Product"),
- new UserConfiguration<CoreFilterDefinitions>("ProductDock.Product"))
- {
- }
- }
- /// <summary>
- /// Interaction logic for ProductLookup.xaml
- /// </summary>
- public partial class ProductLookupDock : UserControl, IDockPanel
- {
- private ProductHoldingControl? _holdings;
- private Product[] Products = Array.Empty<Product>();
- public ProductLookupDock()
- {
- InitializeComponent();
- }
-
- public void Setup()
- {
- if (!Security.CanView<StockLocation>())
- return;
- if (_holdings == null)
- {
- _holdings = new ProductHoldingControl();
- HoldingsTab.Content = _holdings;
- HoldingsTab.Visibility = Visibility.Visible;
- }
- _holdings.Refresh(true, false);
- }
- public void Refresh()
- {
- }
- private Filter<Product>? GetSearchFilter()
- {
- var search = Search.Text;
- if (!string.IsNullOrEmpty(search))
- {
- return new Filter<Product>(x => x.Code).Contains(Search.Text).Or(x => x.Name).Contains(search);
- }
- else
- {
- return null;
- }
- }
- private void Reload()
- {
- var filters = new Filters<Product>();
- filters.Add(GetSearchFilter());
- filters.Add(FilterButton.GetFilter());
- var filter = filters.Combine() ?? new Filter<Product>().None();
- using (new WaitCursor())
- {
- Products = Client.Query(
- filter,
- Columns.Required<Product>().Add(x => x.ID, x => x.Code, x => x.Name, x => x.Image.ID, x => x.Image.FileName),
- new SortOrder<Product>(x => x.Code))
- .ToObjects<Product>().ToArray();
- Items.ItemsSource = Products;
- if (Products.Any())
- {
- Items.Focus();
- Items.SelectedIndex = 0;
- var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
- listBoxItem?.Focus();
- }
- }
- }
- private void Search_TextChanged(object sender, TextChangedEventArgs e)
- {
- }
- private void Search_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- Reload();
- }
- else if (e.Key == Key.Down)
- {
- if (Products.Any())
- {
- Items.Focus();
- //Items.SelectedValue = items.First();
- Items.SelectedIndex = 0;
- var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
- listBoxItem.Focus();
- }
- }
- }
- private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- Image.Source = null;
- if (e.AddedItems.Count > 0)
- {
- var item = e.AddedItems[0] as Product;
- if (!item.Image.ID.Equals(Guid.Empty))
- new Client<Document>().Query(
- new Filter<Document>(x => x.ID).IsEqualTo(item.Image.ID),
- Columns.None<Document>().Add(x => x.ID, x => x.Data),
- null,
- CoreRange.All,
- (docs, error) =>
- {
- var row = docs.Rows.FirstOrDefault();
- if (row != null)
- {
- var id = row.Get<Document, Guid>(x => x.ID);
-
- var data = row.Get<Document, byte[]>(x => x.Data);
- if (data?.Any() == true)
- {
- var ms = new MemoryStream(data);
- var bmp = new Bitmap(ms);
- Dispatcher.BeginInvoke(
- new Action<Guid>(o =>
- {
- if (Items.SelectedValue != null)
- {
- var sel = (Product)Items.SelectedValue;
- if (sel.Image.ID == o)
- Image.Source = bmp.AsBitmapImage(false);
- }
- }), id
- );
- }
- }
- }
- );
- else
- Image.Source = null;
- if (Security.CanView<StockLocation>())
- {
- _holdings.Product = item;
- _holdings.Refresh(false, true);
- }
- }
- else
- {
- Image.Source = null;
- }
- }
- private void Items_KeyUp(object sender, KeyEventArgs e)
- {
- }
- private void Items_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Up)
- if (Items.SelectedIndex == 0)
- Search.Focus();
- }
- private void ImageMenu_Opened(object sender, RoutedEventArgs e)
- {
- var pdi = Items.SelectedItem as Product;
- var anyproducts = pdi != null;
- var validimage = pdi != null && pdi.Image.ID != Guid.Empty;
- LoadImageFromFile.IsEnabled = anyproducts;
- SaveImageToFile.IsEnabled = validimage;
- CopyImageToClipboard.IsEnabled = validimage;
- PasteImageFromClipboard.IsEnabled = anyproducts && (Clipboard.ContainsData("ProductImage") || Clipboard.ContainsImage());
- ClearImage.IsEnabled = anyproducts;
- }
- private string SelectedProducts()
- {
- var product = Items.SelectedItem as Product;
- return product != null ? product.Code : "";
- }
- private void UpdateProductImages(Guid id, string filename, Bitmap? bitmap)
- {
- var product = Items.SelectedItem as Product;
- if (product == null)
- return;
- var docid = id;
- if (bitmap != null && docid == Guid.Empty)
- {
- byte[] data = null;
- using (var ms = new MemoryStream())
- {
- bitmap.Save(ms, ImageFormat.Png);
- data = ms.GetBuffer();
- }
- var crc = CoreUtils.CalculateCRC(data);
- var doc = new Client<Document>().Query(
- new Filter<Document>(x => x.FileName).IsEqualTo(Path.GetFileName(filename)).And(x => x.CRC).IsEqualTo(crc),
- Columns.None<Document>().Add(x => x.ID)
- ).Rows.FirstOrDefault()?.ToObject<Document>();
- if (doc == null)
- {
- doc = new Document
- {
- FileName = Path.GetFileName(filename),
- CRC = crc,
- TimeStamp = DateTime.Now,
- Data = data
- };
- new Client<Document>().Save(doc, "");
- }
- docid = doc.ID;
- }
- product.Image.ID = CoreUtils.FullGuid;
- product.CommitChanges();
- product.Image.ID = docid;
- product.Image.FileName = Path.GetFileName(filename);
- new Client<Product>().Save(product, "Cleared Product Image", (p, err) => { });
- Image.Source = bitmap?.AsBitmapImage();
- }
- private Bitmap? ConvertDXFFile(string filename)
- {
- Bitmap? result = null;
- using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
- {
- //String newfile = Path.ChangeExtension(Path.GetFileName(filename), "png");
- try
- {
- result = DxfUtils.ProcessImage(stream, Path.GetFileNameWithoutExtension(filename));
- }
- catch (Exception e)
- {
- MessageWindow.ShowError("Could not load DXF file", e);
- result = null;
- }
- }
- return result;
- }
- private void LoadImageFromFile_Click(object sender, RoutedEventArgs e)
- {
- var product = Items.SelectedItem as Product;
- if (product == null)
- return;
- var dlg = new OpenFileDialog();
- dlg.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|DXF Files (*.dxf)|*.dxf";
- if (dlg.ShowDialog() == true)
- {
- var filename = dlg.FileName.ToLower();
- Progress.Show("Updating Images: " + Path.GetFileName(filename));
- Bitmap? bmp = null;
- if (Path.GetExtension(filename).ToLower().Equals(".dxf"))
- bmp = ConvertDXFFile(filename);
- else
- bmp = System.Drawing.Image.FromFile(filename) as Bitmap;
- UpdateProductImages(Guid.Empty, filename, bmp);
- Progress.Close();
- MessageBox.Show(string.Format("Imported [{0}] into [{1}]", Path.GetFileName(dlg.FileName), SelectedProducts()));
- }
- }
- private void SaveImageToFile_Click(object sender, RoutedEventArgs e)
- {
- var product = Items.SelectedItem as Product;
- if (product == null)
- return;
- var bmp = (Image.Source as BitmapImage).AsBitmap();
- var dlg = new SaveFileDialog();
- dlg.Filter = "Image Files (*.png)|*.png";
- dlg.FileName = product.Image.FileName;
- if (dlg.ShowDialog() == true)
- bmp.Save(dlg.FileName);
- }
- private void CopyImageToClipboard_Click(object sender, RoutedEventArgs e)
- {
- var product = Items.SelectedItem as Product;
- if (product == null)
- return;
- var bmp = (Image.Source as BitmapImage).AsBitmap();
- var data = new Tuple<Guid, string, Bitmap>(
- product.Image.ID,
- product.Image.FileName,
- bmp
- );
- Clipboard.SetData("ProductImage", data);
- }
- private void PasteImageFromClipboard_Click(object sender, RoutedEventArgs e)
- {
- if (Items.SelectedItem is not Product product)
- return;
- if (MessageBox.Show("Are you sure you wish to update the image for the selected product?", "Update Image", MessageBoxButton.YesNo,
- MessageBoxImage.Question) != MessageBoxResult.Yes)
- return;
- Progress.Show("Updating Images");
- var id = Guid.Empty;
- var filename = "";
- Bitmap? bitmap = null;
- if (Clipboard.ContainsData("ProductImage"))
- {
- if(Clipboard.GetData("ProductImage") is Tuple<Guid, string, Bitmap> data)
- {
- id = data.Item1;
- filename = data.Item2;
- bitmap = data.Item3;
- }
- }
- else if (Clipboard.ContainsImage())
- {
- var data = Clipboard.GetImage();
- bitmap = data.AsBitmap2();
- filename = string.Format("clip{0:yyyyMMddhhmmss}.png", DateTime.Now);
- if (Clipboard.ContainsFileDropList())
- {
- var list = Clipboard.GetFileDropList();
- if (list.Count > 0)
- filename = Path.ChangeExtension(Path.GetFileName(list[0]), ".png");
- }
- //filename = String.Format("clip{0:yyyyMMddhhmmss}.png",DateTime.Now);
- //bitmap.Save(filename);
- id = Guid.Empty;
- }
- if (bitmap == null)
- {
- MessageBox.Show("Unable to paste data from clipboard");
- return;
- }
- Progress.Show("");
- UpdateProductImages(id, filename, bitmap);
- Progress.Close();
- MessageBox.Show(string.Format("Pasted [{0}] into [{1}]", Path.GetFileName(filename), SelectedProducts()));
- }
- private void ClearImage_Click(object sender, RoutedEventArgs e)
- {
- if (MessageWindow.ShowYesNo("Are you sure you wish to clear the image for the selected product?", "Clear Image"))
- return;
- Progress.Show("Clearing Image");
- UpdateProductImages(Guid.Empty, "", null);
- Progress.Close();
- MessageBox.Show(string.Format("Cleared image from [{0}]", string.Join(", ", SelectedProducts())));
- }
- private void Grid_MouseMove(object sender, MouseEventArgs e)
- {
- if(sender is not Grid grid || grid.Tag is not Product product || e.LeftButton != MouseButtonState.Pressed)
- {
- return;
- }
- DragDrop.DoDragDrop(grid, product, DragDropEffects.Copy);
- }
- private void FilterButton_OnFilterRefresh()
- {
- Reload();
- }
- }
|