ProductLookupDock.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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.Core;
  14. using InABox.Dxf;
  15. using InABox.WPF;
  16. using Microsoft.Win32;
  17. namespace PRSDesktop
  18. {
  19. /// <summary>
  20. /// Interaction logic for ProductLookup.xaml
  21. /// </summary>
  22. public partial class ProductLookupDock : UserControl, IDockPanel
  23. {
  24. private ProductHoldingControl _holdings;
  25. public ProductLookupDock()
  26. {
  27. InitializeComponent();
  28. }
  29. public void Setup()
  30. {
  31. if (!Security.CanView<StockLocation>())
  32. return;
  33. if (_holdings == null)
  34. {
  35. _holdings = new ProductHoldingControl();
  36. HoldingsTab.Content = _holdings;
  37. HoldingsTab.Visibility = Visibility.Visible;
  38. }
  39. _holdings.Refresh(true, false);
  40. }
  41. public void Refresh()
  42. {
  43. }
  44. private void Search_TextChanged(object sender, TextChangedEventArgs e)
  45. {
  46. }
  47. private void Search_KeyUp(object sender, KeyEventArgs e)
  48. {
  49. if (e.Key == Key.Enter)
  50. {
  51. var search = Search.Text;
  52. if (!string.IsNullOrEmpty(search))
  53. {
  54. using (new WaitCursor())
  55. {
  56. var products = new Client<Product>().Query(
  57. new Filter<Product>(x => x.Code).Contains(Search.Text).Or(x => x.Name)
  58. .Contains(search), //.And(x => x.Image.ID).IsNotEqualTo(Guid.Empty),
  59. new Columns<Product>(x => x.ID, x => x.Code, x => x.Name, x => x.Image.ID, x => x.Image.FileName),
  60. new SortOrder<Product>(x => x.Code)
  61. );
  62. var items = new List<PDI>();
  63. foreach (var row in products.Rows)
  64. {
  65. var item = new PDI
  66. {
  67. ID = row.Get<Product, Guid>(x => x.ID),
  68. Code = row.Get<Product, string>(x => x.Code),
  69. Name = row.Get<Product, string>(x => x.Name),
  70. ImageID = row.Get<Product, Guid>(x => x.Image.ID),
  71. ImageName = row.Get<Product, string>(x => x.Image.FileName)
  72. };
  73. items.Add(item);
  74. }
  75. Items.ItemsSource = items;
  76. if (items.Any())
  77. {
  78. Items.Focus();
  79. Items.SelectedIndex = 0;
  80. var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
  81. if (listBoxItem != null)
  82. listBoxItem.Focus();
  83. }
  84. ;
  85. }
  86. }
  87. else
  88. {
  89. Items.ItemsSource = null;
  90. Image.Source = null;
  91. }
  92. }
  93. else if (e.Key == Key.Down)
  94. {
  95. if (Items.ItemsSource != null)
  96. {
  97. var items = Items.ItemsSource as List<PDI>;
  98. if (items.Any())
  99. {
  100. Items.Focus();
  101. //Items.SelectedValue = items.First();
  102. Items.SelectedIndex = 0;
  103. var listBoxItem = (ListBoxItem)Items.ItemContainerGenerator.ContainerFromItem(Items.SelectedItem);
  104. listBoxItem.Focus();
  105. }
  106. }
  107. }
  108. }
  109. private void Items_SelectionChanged(object sender, SelectionChangedEventArgs e)
  110. {
  111. Image.Source = null;
  112. var productid = Guid.Empty;
  113. if (e.AddedItems.Count > 0)
  114. {
  115. var item = e.AddedItems[0] as PDI;
  116. productid = item.ID;
  117. if (!item.ImageID.Equals(Guid.Empty))
  118. new Client<Document>().Query(
  119. new Filter<Document>(x => x.ID).IsEqualTo(item.ImageID),
  120. new Columns<Document>(x => x.ID, x => x.Data),
  121. null,
  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 = (PDI)Items.SelectedValue;
  136. if (sel.ImageID == o)
  137. Image.Source = bmp.AsBitmapImage(false);
  138. }
  139. }), id
  140. );
  141. }
  142. }
  143. );
  144. else
  145. Image.Source = null;
  146. }
  147. else
  148. {
  149. Image.Source = null;
  150. }
  151. if (Security.CanView<StockLocation>())
  152. {
  153. _holdings.ParentID = productid;
  154. _holdings.Refresh(false, true);
  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 PDI;
  169. var anyproducts = pdi != null;
  170. var validimage = pdi != null && pdi.ImageID != 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 pdi = Items.SelectedItem as PDI;
  180. return pdi != null ? pdi.Code : "";
  181. }
  182. private void UpdateProductImages(Guid id, string filename, Bitmap bitmap)
  183. {
  184. var pdi = Items.SelectedItem as PDI;
  185. if (pdi == 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. new Columns<Document>(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. var product = new Product { ID = pdi.ID };
  215. product.Image.ID = CoreUtils.FullGuid;
  216. product.CommitChanges();
  217. product.Image.ID = docid;
  218. product.Image.FileName = Path.GetFileName(filename);
  219. new Client<Product>().Save(product, "Cleared Product Image", (p, err) => { });
  220. pdi.ImageID = docid;
  221. pdi.ImageName = Path.GetFileName(filename);
  222. Image.Source = bitmap?.AsBitmapImage();
  223. }
  224. private Bitmap ConvertDXFFile(string filename)
  225. {
  226. Bitmap result = null;
  227. using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
  228. {
  229. //String newfile = Path.ChangeExtension(Path.GetFileName(filename), "png");
  230. try
  231. {
  232. result = DxfUtils.ProcessImage(stream, Path.GetFileNameWithoutExtension(filename));
  233. }
  234. catch (Exception e)
  235. {
  236. MessageBox.Show(e.Message);
  237. result = null;
  238. }
  239. }
  240. return result;
  241. }
  242. private void LoadImageFromFile_Click(object sender, RoutedEventArgs e)
  243. {
  244. var pdi = Items.SelectedItem as PDI;
  245. if (pdi == null)
  246. return;
  247. var dlg = new OpenFileDialog();
  248. dlg.Filter = "Image Files (*.png;*.jpg;*.jpeg;*.bmp)|*.png;*.jpg;*.jpeg;*.bmp|DXF Files (*.dxf)|*.dxf";
  249. if (dlg.ShowDialog() == true)
  250. {
  251. var filename = dlg.FileName.ToLower();
  252. Progress.Show("Updating Images: " + Path.GetFileName(filename));
  253. Bitmap bmp = null;
  254. if (Path.GetExtension(filename).ToLower().Equals(".dxf"))
  255. bmp = ConvertDXFFile(filename);
  256. else
  257. bmp = System.Drawing.Image.FromFile(filename) as Bitmap;
  258. UpdateProductImages(Guid.Empty, filename, bmp);
  259. Progress.Close();
  260. MessageBox.Show(string.Format("Imported [{0}] into [{1}]", Path.GetFileName(dlg.FileName), SelectedProducts()));
  261. }
  262. }
  263. private void SaveImageToFile_Click(object sender, RoutedEventArgs e)
  264. {
  265. var pdi = Items.SelectedItem as PDI;
  266. if (pdi == null)
  267. return;
  268. var bmp = (Image.Source as BitmapImage).AsBitmap();
  269. var dlg = new SaveFileDialog();
  270. dlg.Filter = "Image Files (*.png)|*.png";
  271. dlg.FileName = pdi.ImageName;
  272. if (dlg.ShowDialog() == true)
  273. bmp.Save(dlg.FileName);
  274. }
  275. private void CopyImageToClipboard_Click(object sender, RoutedEventArgs e)
  276. {
  277. var pdi = Items.SelectedItem as PDI;
  278. if (pdi == null)
  279. return;
  280. var bmp = (Image.Source as BitmapImage).AsBitmap();
  281. var data = new Tuple<Guid, string, Bitmap>(
  282. pdi.ImageID,
  283. pdi.ImageName,
  284. bmp
  285. );
  286. Clipboard.SetData("ProductImage", data);
  287. }
  288. private void PasteImageFromClipboard_Click(object sender, RoutedEventArgs e)
  289. {
  290. var pdi = Items.SelectedItem as PDI;
  291. if (pdi == null)
  292. return;
  293. if (MessageBox.Show("Are you sure you wish to update the image for the selected product?", "Update Image", MessageBoxButton.YesNo,
  294. MessageBoxImage.Question) != MessageBoxResult.Yes)
  295. return;
  296. Progress.Show("Updating Images");
  297. var id = Guid.Empty;
  298. var filename = "";
  299. Bitmap bitmap = null;
  300. if (Clipboard.ContainsData("ProductImage"))
  301. {
  302. var data = Clipboard.GetData("ProductImage") as Tuple<Guid, string, Bitmap>;
  303. id = data.Item1;
  304. filename = data.Item2;
  305. bitmap = data.Item3;
  306. }
  307. else if (Clipboard.ContainsImage())
  308. {
  309. var data = Clipboard.GetImage();
  310. bitmap = data.AsBitmap2();
  311. filename = string.Format("clip{0:yyyyMMddhhmmss}.png", DateTime.Now);
  312. if (Clipboard.ContainsFileDropList())
  313. {
  314. var list = Clipboard.GetFileDropList();
  315. if (list.Count > 0)
  316. filename = Path.ChangeExtension(Path.GetFileName(list[0]), ".png");
  317. }
  318. //filename = String.Format("clip{0:yyyyMMddhhmmss}.png",DateTime.Now);
  319. //bitmap.Save(filename);
  320. id = Guid.Empty;
  321. }
  322. if (bitmap == null)
  323. {
  324. MessageBox.Show("Unable to paste data from clipboard");
  325. return;
  326. }
  327. Progress.Show("");
  328. UpdateProductImages(id, filename, bitmap);
  329. Progress.Close();
  330. MessageBox.Show(string.Format("Pasted [{0}] into [{1}]", Path.GetFileName(filename), SelectedProducts()));
  331. }
  332. private void ClearImage_Click(object sender, RoutedEventArgs e)
  333. {
  334. if (MessageBox.Show("Are you sure you wish to clear the image for the selected product?", "Clear Image", MessageBoxButton.YesNo,
  335. MessageBoxImage.Question) != MessageBoxResult.Yes)
  336. return;
  337. Progress.Show("Clearing Image");
  338. UpdateProductImages(Guid.Empty, "", null);
  339. Progress.Close();
  340. MessageBox.Show(string.Format("Cleared image from [{0}]", string.Join(", ", SelectedProducts())));
  341. }
  342. private class PDI
  343. {
  344. public string Code { get; set; }
  345. public string Name { get; set; }
  346. public Guid ImageID { get; set; }
  347. public string ImageName { get; set; }
  348. public Guid ID { get; set; }
  349. }
  350. }
  351. }