DynamicDocumentGrid.cs 20 KB

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