DynamicDocumentGrid.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using InABox.Core;
  13. using InABox.WPF;
  14. using Microsoft.Win32;
  15. using Microsoft.Xaml.Behaviors.Core;
  16. using Image = System.Windows.Controls.Image;
  17. using InABox.Wpf;
  18. using System.Threading;
  19. namespace InABox.DynamicGrid;
  20. public class DocumentConverter : AbstractConverter<object, object>
  21. {
  22. public override object Convert(object value)
  23. {
  24. return value;
  25. }
  26. }
  27. public class TimeStampToBrushConverter : AbstractConverter<DateTime, System.Windows.Media.Brush?>
  28. {
  29. public System.Windows.Media.Brush? Empty { get; init; }
  30. public System.Windows.Media.Brush? Set { get; init; }
  31. public override System.Windows.Media.Brush? Convert(DateTime value)
  32. {
  33. return value.IsEmpty()
  34. ? Empty
  35. : Set;
  36. }
  37. }
  38. public class DynamicDocumentGrid<TDocument, TEntity, TEntityLink> : DynamicManyToManyGrid<TDocument, TEntity>
  39. where TEntity : Entity, IPersistent, IRemotable, new()
  40. where TDocument : Entity, IEntityDocument<TEntityLink>, IPersistent, IRemotable, new() // Entity, IPersistent, IRemotable, IManyToMany<TEntity, Document>, new()
  41. where TEntityLink : EntityLink<TEntity>, new()
  42. {
  43. public bool ShowSupercededColumn { get; set; }
  44. private bool _simpleTemplate;
  45. public bool SimpleTemplate
  46. {
  47. get => _simpleTemplate;
  48. set
  49. {
  50. _simpleTemplate = value;
  51. RowHeight = value
  52. ? 150
  53. : 100;
  54. }
  55. }
  56. private DynamicTemplateColumn _template;
  57. public DynamicDocumentGrid()
  58. {
  59. MultiSelect = false;
  60. HiddenColumns.Add(x => x.DocumentLink.ID);
  61. HiddenColumns.Add(x => x.Superceded);
  62. HiddenColumns.Add(x => x.DocumentLink.FileName);
  63. HiddenColumns.Add(x => x.Thumbnail);
  64. HiddenColumns.Add(x => x.Notes);
  65. //ActionColumns.Add(new DynamicImageColumn(DocumentImage, ViewDocument) { Position = DynamicActionColumnPosition.Start });
  66. //ActionColumns.Add(new DynamicImageColumn(DiskImage, SaveDocument) { Position = DynamicActionColumnPosition.Start });
  67. _template = new DynamicTemplateColumn(DocumentTemplate)
  68. {
  69. Position = DynamicActionColumnPosition.Start,
  70. Width = 0,
  71. HeaderText = "Attached Documents"
  72. };
  73. ActionColumns.Add(_template);
  74. //supercedecolumn = new DynamicImageColumn(SupercededImage, SupercedeDocument);
  75. //ActionColumns.Add(supercedecolumn);
  76. RowHeight = 100;
  77. }
  78. protected override void DoDoubleClick(object sender, DynamicGridCellClickEventArgs args)
  79. {
  80. var doc = SelectedRows.FirstOrDefault()?.ToObject<TDocument>();
  81. if (doc != null)
  82. {
  83. var editor = new DocumentEditor(new IEntityDocument[] { doc });
  84. //editor.PrintAllowed = Security.IsAllowed<CanPrintFactoryFloorDrawings>();
  85. editor.SaveAllowed = false;
  86. editor.ShowDialog();
  87. }
  88. }
  89. private FrameworkElement DocumentTemplate(CoreRow row)
  90. {
  91. return SimpleTemplate
  92. ? CreateSimpleTemplate()
  93. : CreateDetailedTemplate();
  94. }
  95. private FrameworkElement CreateDetailedTemplate()
  96. {
  97. Grid grid = new Grid()
  98. {
  99. Height = 100,
  100. ContextMenu = CreateContextMenu(),
  101. RowDefinitions =
  102. {
  103. new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) },
  104. new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) },
  105. },
  106. ColumnDefinitions =
  107. {
  108. new ColumnDefinition() { Width = new GridLength(100) },
  109. new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) },
  110. new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Auto) },
  111. }
  112. };
  113. // grid.SetBinding(
  114. // Grid.BackgroundProperty,
  115. // new Binding("Superceded")
  116. // {
  117. // Converter = new TimeStampToBrushConverter()
  118. // {
  119. // Empty = new SolidColorBrush(Colors.LightYellow),
  120. // Set = new SolidColorBrush(Colors.Silver)
  121. // }
  122. // }
  123. // );
  124. Image thumbnail = new Image()
  125. {
  126. Stretch = Stretch.Uniform,
  127. Margin = new Thickness(5, 2, 5, 2),
  128. };
  129. var ttImage = new Image();
  130. ttImage.SetBinding(Image.SourceProperty,
  131. new Binding("Thumbnail") { Converter = new BytesToBitmapImageConverter() });
  132. thumbnail.ToolTip = new ToolTip()
  133. {
  134. Content = ttImage
  135. };
  136. thumbnail.SetBinding(Image.SourceProperty,
  137. new Binding("Thumbnail") { Converter = new BytesToBitmapImageConverter() });
  138. thumbnail.SetValue(Grid.RowProperty, 0);
  139. thumbnail.SetValue(Grid.RowSpanProperty, 2);
  140. thumbnail.SetValue(Grid.ColumnProperty, 0);
  141. grid.Children.Add(thumbnail);
  142. var dock = new DockPanel();
  143. dock.SetValue(Grid.RowProperty, 0);
  144. dock.SetValue(Grid.ColumnProperty, 1);
  145. grid.Children.Add(dock);
  146. var superceded = new Label()
  147. {
  148. FontWeight = FontWeights.Bold,
  149. Content = "*** SUPERCEDED ***",
  150. Margin = new Thickness(0, 0, 5, 0)
  151. };
  152. superceded.SetBinding(Label.VisibilityProperty,
  153. new Binding("Superceded") { Converter = new DateTimeToVisibilityConverter() });
  154. superceded.SetValue(DockPanel.DockProperty, Dock.Left);
  155. dock.Children.Add(superceded);
  156. var filename = new Label()
  157. {
  158. FontWeight = FontWeights.Bold
  159. };
  160. filename.SetBinding(Label.ContentProperty, new Binding("DocumentLink_FileName"));
  161. filename.SetValue(DockPanel.DockProperty, Dock.Left);
  162. dock.Children.Add(filename);
  163. var buttons = new StackPanel()
  164. {
  165. Orientation = Orientation.Horizontal
  166. };
  167. buttons.SetValue(Grid.RowProperty, 0);
  168. buttons.SetValue(Grid.ColumnProperty, 2);
  169. grid.Children.Add(buttons);
  170. var view = new Button()
  171. {
  172. Content = new Image() { Source = Wpf.Resources.multi_image.AsBitmapImage() },
  173. BorderBrush = new SolidColorBrush(Colors.Transparent),
  174. Background = new SolidColorBrush(Colors.Transparent),
  175. Height = 32,
  176. Width = 32,
  177. ToolTip = "View Documents",
  178. Command = new ActionCommand(ViewDocuments)
  179. };
  180. buttons.Children.Add(view);
  181. var copy = new Button()
  182. {
  183. Content = new Image() { Source = Wpf.Resources.copy.AsBitmapImage() },
  184. BorderBrush = new SolidColorBrush(Colors.Transparent),
  185. Background = new SolidColorBrush(Colors.Transparent),
  186. Height = 32,
  187. Width = 32,
  188. ToolTip = "Copy to Clipboard",
  189. Command = new ActionCommand(CopyDocuments)
  190. };
  191. buttons.Children.Add(copy);
  192. var save = new Button()
  193. {
  194. Content = new Image() { Source = Wpf.Resources.download.AsBitmapImage() },
  195. BorderBrush = new SolidColorBrush(Colors.Transparent),
  196. Background = new SolidColorBrush(Colors.Transparent),
  197. Height = 32,
  198. Width = 32,
  199. ToolTip = "Save Documents",
  200. Command = new ActionCommand(SaveDocuments)
  201. };
  202. buttons.Children.Add(save);
  203. var print = new Button()
  204. {
  205. Content = new Image() { Source = Wpf.Resources.print.AsBitmapImage(), Margin = new Thickness(2) },
  206. BorderBrush = new SolidColorBrush(Colors.Transparent),
  207. Background = new SolidColorBrush(Colors.Transparent),
  208. Height = 32,
  209. Width = 32,
  210. ToolTip = "Print Documents",
  211. Command = new ActionCommand(PrintDocuments)
  212. };
  213. buttons.Children.Add(print);
  214. var notes = new Label()
  215. {
  216. };
  217. notes.SetBinding(Label.ContentProperty, new Binding("Notes"));
  218. notes.SetValue(Grid.RowProperty, 1);
  219. notes.SetValue(Grid.ColumnProperty, 1);
  220. notes.SetValue(Grid.ColumnSpanProperty, 2);
  221. grid.Children.Add(notes);
  222. return grid;
  223. }
  224. private ContextMenu CreateContextMenu()
  225. {
  226. var menu = new ContextMenu();
  227. menu.Items.Add(new MenuItem()
  228. {
  229. Header = "View Documents",
  230. Command = new ActionCommand(ViewDocuments)
  231. });
  232. menu.Items.Add(new MenuItem()
  233. {
  234. Header = "Copy To Clipboard",
  235. Command = new ActionCommand(CopyDocuments)
  236. });
  237. menu.Items.Add(new MenuItem()
  238. {
  239. Header = "Save Documents",
  240. Command = new ActionCommand(SaveDocuments)
  241. });
  242. return menu;
  243. }
  244. private FrameworkElement CreateSimpleTemplate()
  245. {
  246. Grid grid = new Grid()
  247. {
  248. //Height = 150,
  249. ContextMenu = CreateContextMenu(),
  250. RowDefinitions =
  251. {
  252. new RowDefinition() { Height = new GridLength(1, GridUnitType.Star) },
  253. new RowDefinition() { Height = new GridLength(1, GridUnitType.Auto) },
  254. },
  255. ColumnDefinitions =
  256. {
  257. new ColumnDefinition() { Width = new GridLength(1, GridUnitType.Star) },
  258. }
  259. };
  260. Image thumbnail = new Image()
  261. {
  262. Stretch = Stretch.Uniform,
  263. Margin = new Thickness(5),
  264. //HorizontalAlignment = HorizontalAlignment.Stretch,
  265. //VerticalAlignment = VerticalAlignment.Stretch
  266. };
  267. thumbnail.SetBinding(Image.SourceProperty, new Binding("Thumbnail") { Converter = new BytesToBitmapImageConverter() });
  268. thumbnail.SetValue(Grid.RowProperty,0);
  269. grid.Children.Add(thumbnail);
  270. var filename = new Label()
  271. {
  272. HorizontalContentAlignment = HorizontalAlignment.Center,
  273. FontSize = 10
  274. };
  275. filename.SetBinding(Label.ContentProperty, new Binding("DocumentLink_FileName"));
  276. filename.SetValue(Grid.RowProperty,1);
  277. grid.Children.Add(filename);
  278. return grid;
  279. }
  280. private void GetDocuments(Action<Dictionary<string,byte[]>> action)
  281. {
  282. var ids = SelectedRows.Select(r => r.Get<IEntityDocument, Guid>(c => c.DocumentLink.ID)).ToArray();
  283. var files = Client.Query(
  284. new Filter<Document>(x => x.ID).InList(ids),
  285. Columns.None<Document>().Add(x => x.FileName).Add(x => x.Data)
  286. ).ToDictionary<Document, String, byte[]>(x => x.FileName, x => x.Data);
  287. action?.Invoke(files);
  288. }
  289. private static string SanitiseFileName(string filename)
  290. {
  291. var basefilename = Path.GetFileNameWithoutExtension(filename);
  292. var extension = Path.GetExtension(filename);
  293. return Path.ChangeExtension(string.Join("_", basefilename.Split(Path.GetInvalidFileNameChars())), extension);
  294. }
  295. private void ViewDocuments()
  296. {
  297. GetDocuments((files) =>
  298. {
  299. foreach (var file in files)
  300. {
  301. Task.Run(() =>
  302. {
  303. var tempfile = Path.Combine(System.IO.Path.GetTempPath(), SanitiseFileName(file.Key));
  304. try
  305. {
  306. File.WriteAllBytes(tempfile, file.Value);
  307. }
  308. catch
  309. {
  310. // Outlook likes to keep files open apparently, which breaks this code.
  311. }
  312. var info = new System.Diagnostics.ProcessStartInfo(tempfile);
  313. info.UseShellExecute = true;
  314. info.Verb = "Open";
  315. Process.Start(info);
  316. });
  317. }
  318. });
  319. }
  320. private void CopyDocuments()
  321. {
  322. if (SelectedRows?.Any() != true)
  323. return;
  324. GetDocuments((files) =>
  325. {
  326. System.Collections.Specialized.StringCollection FileCollection = new System.Collections.Specialized.StringCollection();
  327. foreach(var file in files)
  328. {
  329. var tempfile = Path.Combine(System.IO.Path.GetTempPath(), SanitiseFileName(file.Key));
  330. File.WriteAllBytes(tempfile, file.Value);
  331. FileCollection.Add(tempfile);
  332. }
  333. Clipboard.SetFileDropList(FileCollection);
  334. });
  335. }
  336. private void SaveDocuments()
  337. {
  338. if (SelectedRows?.Any() != true)
  339. return;
  340. using(var fbd = new System.Windows.Forms.FolderBrowserDialog())
  341. {
  342. var result = fbd.ShowDialog();
  343. if (result == System.Windows.Forms.DialogResult.OK && !string.IsNullOrWhiteSpace(fbd.SelectedPath))
  344. {
  345. var path = fbd.SelectedPath;
  346. GetDocuments(files =>
  347. {
  348. foreach (var file in files)
  349. File.WriteAllBytes(Path.Combine(path, SanitiseFileName(file.Key)), file.Value);
  350. });
  351. }
  352. }
  353. }
  354. private void PrintDocuments()
  355. {
  356. if (SelectedRows?.Any() != true)
  357. return;
  358. GetDocuments(files =>
  359. {
  360. Task.Run(() =>
  361. {
  362. foreach (var file in files)
  363. {
  364. var tempfile = Path.Combine(System.IO.Path.GetTempPath(), SanitiseFileName(file.Key));
  365. File.WriteAllBytes(tempfile, file.Value);
  366. var info = new System.Diagnostics.ProcessStartInfo(tempfile);
  367. info.CreateNoWindow = true;
  368. info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  369. info.UseShellExecute = true;
  370. info.Verb = "print";
  371. Process.Start(info);
  372. }
  373. });
  374. });
  375. }
  376. protected override DynamicGridColumns LoadColumns()
  377. {
  378. return new DynamicGridColumns();
  379. }
  380. protected override void DoReconfigure(DynamicGridOptions options)
  381. {
  382. base.DoReconfigure(options);
  383. options.SelectColumns = false;
  384. options.DragTarget = true;
  385. }
  386. public override int Order()
  387. {
  388. return int.MaxValue;
  389. }
  390. protected override void HandleDragOver(object sender, DragEventArgs e)
  391. {
  392. if (e.Data.GetDataPresent(DataFormats.FileDrop) || e.Data.GetDataPresent("FileGroupDescriptor"))
  393. {
  394. e.Effects = DragDropEffects.Copy;
  395. }
  396. else
  397. {
  398. e.Effects = DragDropEffects.None;
  399. }
  400. e.Handled = true;
  401. }
  402. protected override void HandleDragDrop(object sender, DragEventArgs e)
  403. {
  404. var result = DocumentUtils.HandleFileDrop(e);
  405. if(result is not null)
  406. {
  407. var docs = new List<Document>();
  408. foreach (var (filename, stream) in result)
  409. {
  410. var doc = new Document();
  411. doc.FileName = Path.GetFileName(filename).ToLower();
  412. if (stream is null)
  413. {
  414. doc.Data = File.ReadAllBytes(filename);
  415. doc.TimeStamp = new FileInfo(filename).LastWriteTime;
  416. }
  417. else
  418. {
  419. using var memoryStream = new MemoryStream();
  420. stream.CopyTo(memoryStream);
  421. doc.Data = memoryStream.ToArray();
  422. doc.TimeStamp = DateTime.Now;
  423. }
  424. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  425. docs.Add(doc);
  426. }
  427. AddDocuments(docs);
  428. }
  429. }
  430. protected override void OnDragEnd(Type entity, CoreTable table, DragEventArgs e)
  431. {
  432. if (entity == typeof(Document))
  433. {
  434. var refresh = false;
  435. var docIDS = table.Rows.Select(x => x.Get<Document, Guid>(x => x.ID)).ToArray();
  436. var columns = Columns.None<Document>().Add(x => x.ID);
  437. foreach (var column in VisibleColumns)
  438. {
  439. if (column.ColumnName.StartsWith("DocumentLink."))
  440. {
  441. columns.Add(string.Join('.', column.ColumnName.Split('.').Skip(1)));
  442. }
  443. }
  444. var docs = Client.Query(
  445. new Filter<Document>(x => x.ID).InList(docIDS),
  446. columns);
  447. foreach (var doc in docs.ToObjects<Document>())
  448. {
  449. var entityDocument = new TDocument();
  450. entityDocument.EntityLink.ID = Item.ID;
  451. entityDocument.DocumentLink.ID = doc.ID;
  452. entityDocument.DocumentLink.Synchronise(doc);
  453. SaveItem(entityDocument);
  454. refresh = true;
  455. }
  456. if (refresh)
  457. {
  458. DoChanged();
  459. Refresh(false, true);
  460. }
  461. }
  462. else
  463. {
  464. base.OnDragEnd(entity, table, e);
  465. }
  466. }
  467. private void AddDocuments(IList<Document> documents)
  468. {
  469. if (documents.Any())
  470. {
  471. Client.Save(documents, "Initial Upload");
  472. foreach (var doc in documents)
  473. {
  474. var newitem = CreateItem();
  475. var prop = GetOtherLink(newitem);
  476. prop.ID = doc.ID;
  477. prop.Synchronise(doc);
  478. SaveItem(newitem);
  479. }
  480. DoChanged();
  481. Refresh(false, true);
  482. }
  483. }
  484. protected override void DoAdd(bool OpenEditorOnDirectEdit = false)
  485. {
  486. var dlg = new OpenFileDialog();
  487. dlg.Multiselect = true;
  488. if (dlg.ShowDialog() == true)
  489. {
  490. using (new WaitCursor())
  491. {
  492. var docs = new List<Document>();
  493. foreach (var filename in dlg.FileNames)
  494. {
  495. // Create a Document
  496. var doc = new Document();
  497. doc.FileName = Path.GetFileName(filename).ToLower();
  498. doc.TimeStamp = new FileInfo(dlg.FileName).LastWriteTime;
  499. doc.Data = File.ReadAllBytes(filename);
  500. doc.CRC = CoreUtils.CalculateCRC(doc.Data);
  501. docs.Add(doc);
  502. }
  503. AddDocuments(docs);
  504. }
  505. }
  506. }
  507. protected override void Reload(
  508. Filters<TDocument> criteria, Columns<TDocument> columns, ref SortOrder<TDocument>? sort,
  509. CancellationToken token, Action<CoreTable?, Exception?> action)
  510. {
  511. base.Reload(criteria, columns, ref sort, token, (t,e) =>
  512. {
  513. if (token.IsCancellationRequested) return;
  514. action(t,e);
  515. // Download Hi Res images in the background and replace them when available
  516. if (t != null && SimpleTemplate)
  517. {
  518. var ids = t.ExtractValues<TDocument, Guid>(x => x.DocumentLink.ID).Distinct().ToArray();
  519. Client.Query(
  520. new Filter<Document>(x => x.ID).InList(ids),
  521. Columns.None<Document>().Add(x => x.ID).Add(x => x.Data),
  522. null,
  523. null,
  524. (d, _) =>
  525. {
  526. if (token.IsCancellationRequested) return;
  527. if (d == null)
  528. return;
  529. var docs = d.ToDictionary<Document, Guid, byte[]>(x => x.ID, x => x.Data);
  530. foreach (var row in t.Rows)
  531. {
  532. if (docs.TryGetValue(row.Get<TDocument, Guid>(x => x.DocumentLink.ID),
  533. out byte[]? data) && (data?.Any() == true))
  534. {
  535. if (ImageUtils.IsPdf(data))
  536. data = ImageUtils.PDFToBitmap(data, 0);
  537. row.Set<TDocument, byte[]>(x => x.Thumbnail!, data);
  538. }
  539. }
  540. Dispatcher.BeginInvoke(() => base.Refresh(false,false));
  541. }
  542. );
  543. }
  544. });
  545. }
  546. }