123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- 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.Core;
- using InABox.Dxf;
- using InABox.WPF;
- using Microsoft.Win32;
- namespace PRSDesktop
- {
- /// <summary>
- /// Interaction logic for ProductLookup.xaml
- /// </summary>
- public partial class ProductLookupDock : UserControl, IDockPanel
- {
- private ProductHoldingControl _holdings;
- 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 void Search_TextChanged(object sender, TextChangedEventArgs e)
- {
- }
- private void Search_KeyUp(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- var search = Search.Text;
- if (!string.IsNullOrEmpty(search))
- {
- using (new WaitCursor())
- {
- var products = new Client<Product>().Query(
- new Filter<Product>(x => x.Code).Contains(Search.Text).Or(x => x.Name)
- .Contains(search), //.And(x => x.Image.ID).IsNotEqualTo(Guid.Empty),
- new Columns<Product>(x => x.ID, x => x.Code, x => x.Name, x => x.Image.ID, x => x.Image.FileName),
- new SortOrder<Product>(x => x.Code)
- );
- var items = new List<PDI>();
- foreach (var row in products.Rows)
- {
- var item = new PDI
- {
- ID = row.Get<Product, Guid>(x => x.ID),
- Code = row.Get<Product, string>(x => x.Code),
- Name = row.Get<Product, string>(x => x.Name),
- ImageID = row.Get<Product, Guid>(x => x.Image.ID),
- ImageName = row.Get<Product, string>(x => x.Image.FileName)
- };
- items.Add(item);
- }
- Items.ItemsSource = items;
- if (items.Any())
- {
- Items.Focus();
- Items.SelectedIndex = 0;
- var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
- if (listBoxItem != null)
- listBoxItem.Focus();
- }
- ;
- }
- }
- else
- {
- Items.ItemsSource = null;
- Image.Source = null;
- }
- }
- else if (e.Key == Key.Down)
- {
- if (Items.ItemsSource != null)
- {
- var items = Items.ItemsSource as List<PDI>;
- if (items.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;
- var productid = Guid.Empty;
- if (e.AddedItems.Count > 0)
- {
- var item = e.AddedItems[0] as PDI;
- productid = item.ID;
- if (!item.ImageID.Equals(Guid.Empty))
- new Client<Document>().Query(
- new Filter<Document>(x => x.ID).IsEqualTo(item.ImageID),
- new Columns<Document>(x => x.ID, x => x.Data),
- null,
- (docs, error) =>
- {
- var row = docs.Rows.FirstOrDefault();
- if (row != null)
- {
- var id = row.Get<Document, Guid>(x => x.ID);
- var ms = new MemoryStream(row.Get<Document, byte[]>(x => x.Data));
- var bmp = new Bitmap(ms);
- Dispatcher.BeginInvoke(
- new Action<Guid>(o =>
- {
- if (Items.SelectedValue != null)
- {
- var sel = (PDI)Items.SelectedValue;
- if (sel.ImageID == o)
- Image.Source = bmp.AsBitmapImage(false);
- }
- }), id
- );
- }
- }
- );
- else
- Image.Source = null;
- }
- else
- {
- Image.Source = null;
- }
- if (Security.CanView<StockLocation>())
- {
- _holdings.ParentID = productid;
- _holdings.Refresh(false, true);
- }
- }
- 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 PDI;
- var anyproducts = pdi != null;
- var validimage = pdi != null && pdi.ImageID != 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 pdi = Items.SelectedItem as PDI;
- return pdi != null ? pdi.Code : "";
- }
- private void UpdateProductImages(Guid id, string filename, Bitmap bitmap)
- {
- var pdi = Items.SelectedItem as PDI;
- if (pdi == 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),
- new Columns<Document>(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;
- }
- var product = new Product { ID = pdi.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) => { });
- pdi.ImageID = docid;
- pdi.ImageName = Path.GetFileName(filename);
- 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)
- {
- MessageBox.Show(e.Message);
- result = null;
- }
- }
- return result;
- }
- private void LoadImageFromFile_Click(object sender, RoutedEventArgs e)
- {
- var pdi = Items.SelectedItem as PDI;
- if (pdi == 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 pdi = Items.SelectedItem as PDI;
- if (pdi == null)
- return;
- var bmp = (Image.Source as BitmapImage).AsBitmap();
- var dlg = new SaveFileDialog();
- dlg.Filter = "Image Files (*.png)|*.png";
- dlg.FileName = pdi.ImageName;
- if (dlg.ShowDialog() == true)
- bmp.Save(dlg.FileName);
- }
- private void CopyImageToClipboard_Click(object sender, RoutedEventArgs e)
- {
- var pdi = Items.SelectedItem as PDI;
- if (pdi == null)
- return;
- var bmp = (Image.Source as BitmapImage).AsBitmap();
- var data = new Tuple<Guid, string, Bitmap>(
- pdi.ImageID,
- pdi.ImageName,
- bmp
- );
- Clipboard.SetData("ProductImage", data);
- }
- private void PasteImageFromClipboard_Click(object sender, RoutedEventArgs e)
- {
- var pdi = Items.SelectedItem as PDI;
- if (pdi == null)
- 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"))
- {
- var data = Clipboard.GetData("ProductImage") as Tuple<Guid, string, Bitmap>;
- 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 (MessageBox.Show("Are you sure you wish to clear the image for the selected product?", "Clear Image", MessageBoxButton.YesNo,
- MessageBoxImage.Question) != MessageBoxResult.Yes)
- return;
- Progress.Show("Clearing Image");
- UpdateProductImages(Guid.Empty, "", null);
- Progress.Close();
- MessageBox.Show(string.Format("Cleared image from [{0}]", string.Join(", ", SelectedProducts())));
- }
- private class PDI
- {
- public string Code { get; set; }
- public string Name { get; set; }
- public Guid ImageID { get; set; }
- public string ImageName { get; set; }
- public Guid ID { get; set; }
- }
- }
- }
|