ProductLookupDock.xaml.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Input;
  10. using System.Windows.Media.Imaging;
  11. using Comal.Classes;
  12. using InABox.Clients;
  13. using InABox.Configuration;
  14. using InABox.Core;
  15. using InABox.Dxf;
  16. using InABox.Wpf;
  17. using InABox.WPF;
  18. using Microsoft.Win32;
  19. namespace PRSDesktop;
  20. public class ProductDockFilterButton : FilterButton<Product>
  21. {
  22. public ProductDockFilterButton() : base(
  23. new GlobalConfiguration<CoreFilterDefinitions>("ProductDock.Product"),
  24. new UserConfiguration<CoreFilterDefinitions>("ProductDock.Product"))
  25. {
  26. }
  27. }
  28. /// <summary>
  29. /// Interaction logic for ProductLookup.xaml
  30. /// </summary>
  31. public partial class ProductLookupDock : UserControl, IDockPanel
  32. {
  33. private ProductHoldingControl? _holdings;
  34. private Product[] Products = Array.Empty<Product>();
  35. public ProductLookupDock()
  36. {
  37. InitializeComponent();
  38. }
  39. public void Setup()
  40. {
  41. if (!Security.CanView<StockLocation>())
  42. return;
  43. if (_holdings == null)
  44. {
  45. _holdings = new ProductHoldingControl();
  46. HoldingsTab.Content = _holdings;
  47. HoldingsTab.Visibility = Visibility.Visible;
  48. }
  49. _holdings.Refresh(true, false);
  50. }
  51. public void Refresh()
  52. {
  53. }
  54. private Filter<Product>? GetSearchFilter()
  55. {
  56. var search = Search.Text;
  57. if (!string.IsNullOrEmpty(search))
  58. {
  59. return new Filter<Product>(x => x.Code).Contains(Search.Text).Or(x => x.Name).Contains(search);
  60. }
  61. else
  62. {
  63. return null;
  64. }
  65. }
  66. private void Reload()
  67. {
  68. var filters = new Filters<Product>();
  69. filters.Add(GetSearchFilter());
  70. filters.Add(FilterButton.GetFilter());
  71. var filter = filters.Combine() ?? new Filter<Product>().None();
  72. using (new WaitCursor())
  73. {
  74. Products = Client.Query(
  75. filter,
  76. Columns.Required<Product>().Add(x => x.ID, x => x.Code, x => x.Name, x => x.Image.ID, x => x.Image.FileName),
  77. new SortOrder<Product>(x => x.Code))
  78. .ToObjects<Product>().ToArray();
  79. Items.ItemsSource = Products;
  80. if (Products.Any())
  81. {
  82. Items.Focus();
  83. Items.SelectedIndex = 0;
  84. var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
  85. listBoxItem?.Focus();
  86. }
  87. }
  88. }
  89. private void Search_TextChanged(object sender, TextChangedEventArgs e)
  90. {
  91. }
  92. private void Search_KeyUp(object sender, KeyEventArgs e)
  93. {
  94. if (e.Key == Key.Enter)
  95. {
  96. Reload();
  97. }
  98. else if (e.Key == Key.Down)
  99. {
  100. if (Products.Any())
  101. {
  102. Items.Focus();
  103. //Items.SelectedValue = items.First();
  104. Items.SelectedIndex = 0;
  105. var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
  106. listBoxItem.Focus();
  107. }
  108. }
  109. }
  110. private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. Image.Source = null;
  113. if (e.AddedItems.Count > 0)
  114. {
  115. var item = e.AddedItems[0] as Product;
  116. if (!item.Image.ID.Equals(Guid.Empty))
  117. new Client<Document>().Query(
  118. new Filter<Document>(x => x.ID).IsEqualTo(item.Image.ID),
  119. Columns.None<Document>().Add(x => x.ID, x => x.Data),
  120. null,
  121. CoreRange.All,
  122. (docs, error) =>
  123. {
  124. var row = docs.Rows.FirstOrDefault();
  125. if (row != null)
  126. {
  127. var id = row.Get<Document, Guid>(x => x.ID);
  128. var ms = new MemoryStream(row.Get<Document, byte[]>(x => x.Data));
  129. var bmp = new Bitmap(ms);
  130. Dispatcher.BeginInvoke(
  131. new Action<Guid>(o =>
  132. {
  133. if (Items.SelectedValue != null)
  134. {
  135. var sel = (Product)Items.SelectedValue;
  136. if (sel.Image.ID == o)
  137. Image.Source = bmp.AsBitmapImage(false);
  138. }
  139. }), id
  140. );
  141. }
  142. }
  143. );
  144. else
  145. Image.Source = null;
  146. if (Security.CanView<StockLocation>())
  147. {
  148. _holdings.Product = item;
  149. _holdings.Refresh(false, true);
  150. }
  151. }
  152. else
  153. {
  154. Image.Source = null;
  155. }
  156. }
  157. private void Items_KeyUp(object sender, KeyEventArgs e)
  158. {
  159. }
  160. private void Items_KeyDown(object sender, KeyEventArgs e)
  161. {
  162. if (e.Key == Key.Up)
  163. if (Items.SelectedIndex == 0)
  164. Search.Focus();
  165. }
  166. private void ImageMenu_Opened(object sender, RoutedEventArgs e)
  167. {
  168. var pdi = Items.SelectedItem as Product;
  169. var anyproducts = pdi != null;
  170. var validimage = pdi != null && pdi.Image.ID != Guid.Empty;
  171. LoadImageFromFile.IsEnabled = anyproducts;
  172. SaveImageToFile.IsEnabled = validimage;
  173. CopyImageToClipboard.IsEnabled = validimage;
  174. PasteImageFromClipboard.IsEnabled = anyproducts && (Clipboard.ContainsData("ProductImage") || Clipboard.ContainsImage());
  175. ClearImage.IsEnabled = anyproducts;
  176. }
  177. private string SelectedProducts()
  178. {
  179. var product = Items.SelectedItem as Product;
  180. return product != null ? product.Code : "";
  181. }
  182. private void UpdateProductImages(Guid id, string filename, Bitmap? bitmap)
  183. {
  184. var product = Items.SelectedItem as Product;
  185. if (product == null)
  186. return;
  187. var docid = id;
  188. if (bitmap != null && docid == Guid.Empty)
  189. {
  190. byte[] data = null;
  191. using (var ms = new MemoryStream())
  192. {
  193. bitmap.Save(ms, ImageFormat.Png);
  194. data = ms.GetBuffer();
  195. }
  196. var crc = CoreUtils.CalculateCRC(data);
  197. var doc = new Client<Document>().Query(
  198. new Filter<Document>(x => x.FileName).IsEqualTo(Path.GetFileName(filename)).And(x => x.CRC).IsEqualTo(crc),
  199. Columns.None<Document>().Add(x => x.ID)
  200. ).Rows.FirstOrDefault()?.ToObject<Document>();
  201. if (doc == null)
  202. {
  203. doc = new Document
  204. {
  205. FileName = Path.GetFileName(filename),
  206. CRC = crc,
  207. TimeStamp = DateTime.Now,
  208. Data = data
  209. };
  210. new Client<Document>().Save(doc, "");
  211. }
  212. docid = doc.ID;
  213. }
  214. product.Image.ID = CoreUtils.FullGuid;
  215. product.CommitChanges();
  216. product.Image.ID = docid;
  217. product.Image.FileName = Path.GetFileName(filename);
  218. new Client<Product>().Save(product, "Cleared Product Image", (p, err) => { });
  219. Image.Source = bitmap?.AsBitmapImage();
  220. }
  221. private Bitmap? ConvertDXFFile(string filename)
  222. {
  223. Bitmap? result = null;
  224. using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
  225. {
  226. //String newfile = Path.ChangeExtension(Path.GetFileName(filename), "png");
  227. try
  228. {
  229. result = DxfUtils.ProcessImage(stream, Path.GetFileNameWithoutExtension(filename));
  230. }
  231. catch (Exception e)
  232. {
  233. MessageWindow.ShowError("Could not load DXF file", e);
  234. result = null;
  235. }
  236. }
  237. return result;
  238. }
  239. private void LoadImageFromFile_Click(object sender, RoutedEventArgs e)
  240. {
  241. var product = Items.SelectedItem as Product;
  242. if (product == null)
  243. return;
  244. var dlg = new OpenFileDialog();
  245. dlg.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|DXF Files (*.dxf)|*.dxf";
  246. if (dlg.ShowDialog() == true)
  247. {
  248. var filename = dlg.FileName.ToLower();
  249. Progress.Show("Updating Images: " + Path.GetFileName(filename));
  250. Bitmap? bmp = null;
  251. if (Path.GetExtension(filename).ToLower().Equals(".dxf"))
  252. bmp = ConvertDXFFile(filename);
  253. else
  254. bmp = System.Drawing.Image.FromFile(filename) as Bitmap;
  255. UpdateProductImages(Guid.Empty, filename, bmp);
  256. Progress.Close();
  257. MessageBox.Show(string.Format("Imported [{0}] into [{1}]", Path.GetFileName(dlg.FileName), SelectedProducts()));
  258. }
  259. }
  260. private void SaveImageToFile_Click(object sender, RoutedEventArgs e)
  261. {
  262. var product = Items.SelectedItem as Product;
  263. if (product == null)
  264. return;
  265. var bmp = (Image.Source as BitmapImage).AsBitmap();
  266. var dlg = new SaveFileDialog();
  267. dlg.Filter = "Image Files (*.png)|*.png";
  268. dlg.FileName = product.Image.FileName;
  269. if (dlg.ShowDialog() == true)
  270. bmp.Save(dlg.FileName);
  271. }
  272. private void CopyImageToClipboard_Click(object sender, RoutedEventArgs e)
  273. {
  274. var product = Items.SelectedItem as Product;
  275. if (product == null)
  276. return;
  277. var bmp = (Image.Source as BitmapImage).AsBitmap();
  278. var data = new Tuple<Guid, string, Bitmap>(
  279. product.Image.ID,
  280. product.Image.FileName,
  281. bmp
  282. );
  283. Clipboard.SetData("ProductImage", data);
  284. }
  285. private void PasteImageFromClipboard_Click(object sender, RoutedEventArgs e)
  286. {
  287. if (Items.SelectedItem is not Product product)
  288. return;
  289. if (MessageBox.Show("Are you sure you wish to update the image for the selected product?", "Update Image", MessageBoxButton.YesNo,
  290. MessageBoxImage.Question) != MessageBoxResult.Yes)
  291. return;
  292. Progress.Show("Updating Images");
  293. var id = Guid.Empty;
  294. var filename = "";
  295. Bitmap? bitmap = null;
  296. if (Clipboard.ContainsData("ProductImage"))
  297. {
  298. if(Clipboard.GetData("ProductImage") is Tuple<Guid, string, Bitmap> data)
  299. {
  300. id = data.Item1;
  301. filename = data.Item2;
  302. bitmap = data.Item3;
  303. }
  304. }
  305. else if (Clipboard.ContainsImage())
  306. {
  307. var data = Clipboard.GetImage();
  308. bitmap = data.AsBitmap2();
  309. filename = string.Format("clip{0:yyyyMMddhhmmss}.png", DateTime.Now);
  310. if (Clipboard.ContainsFileDropList())
  311. {
  312. var list = Clipboard.GetFileDropList();
  313. if (list.Count > 0)
  314. filename = Path.ChangeExtension(Path.GetFileName(list[0]), ".png");
  315. }
  316. //filename = String.Format("clip{0:yyyyMMddhhmmss}.png",DateTime.Now);
  317. //bitmap.Save(filename);
  318. id = Guid.Empty;
  319. }
  320. if (bitmap == null)
  321. {
  322. MessageBox.Show("Unable to paste data from clipboard");
  323. return;
  324. }
  325. Progress.Show("");
  326. UpdateProductImages(id, filename, bitmap);
  327. Progress.Close();
  328. MessageBox.Show(string.Format("Pasted [{0}] into [{1}]", Path.GetFileName(filename), SelectedProducts()));
  329. }
  330. private void ClearImage_Click(object sender, RoutedEventArgs e)
  331. {
  332. if (MessageWindow.ShowYesNo("Are you sure you wish to clear the image for the selected product?", "Clear Image"))
  333. return;
  334. Progress.Show("Clearing Image");
  335. UpdateProductImages(Guid.Empty, "", null);
  336. Progress.Close();
  337. MessageBox.Show(string.Format("Cleared image from [{0}]", string.Join(", ", SelectedProducts())));
  338. }
  339. private void Grid_MouseMove(object sender, MouseEventArgs e)
  340. {
  341. if(sender is not Grid grid || grid.Tag is not Product product || e.LeftButton != MouseButtonState.Pressed)
  342. {
  343. return;
  344. }
  345. DragDrop.DoDragDrop(grid, product, DragDropEffects.Copy);
  346. }
  347. private void FilterButton_OnFilterRefresh()
  348. {
  349. Reload();
  350. }
  351. }