SecondaryWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Media;
  9. using Comal.Classes;
  10. using InABox.Clients;
  11. using InABox.Configuration;
  12. using InABox.Core;
  13. using InABox.DynamicGrid;
  14. using InABox.Reports;
  15. using InABox.Core.Reports;
  16. using InABox.Scripting;
  17. using InABox.Wpf.Reports;
  18. using InABox.WPF;
  19. using PRSDesktop.Configuration;
  20. using Syncfusion.Windows.Tools;
  21. using Syncfusion.Windows.Tools.Controls;
  22. namespace PRSDesktop
  23. {
  24. /// <summary>
  25. /// Interaction logic for SecondaryWindow.xaml
  26. /// </summary>
  27. public partial class SecondaryWindow : RibbonWindow, IPanelHost
  28. {
  29. private readonly Guid _id = Guid.Empty;
  30. private readonly string _modulesection = "";
  31. private readonly IBasePanel _panel;
  32. private readonly string _type = "";
  33. private bool bLoaded;
  34. public SecondaryWindow(Guid id, string type, string modulesection, double left, double top, double width, double height)
  35. {
  36. _id = id;
  37. _modulesection = modulesection;
  38. _type = type;
  39. InitializeComponent();
  40. _ribbon.BackStageButton.Visibility = Visibility.Collapsed;
  41. Left = left;
  42. Top = top;
  43. Width = width;
  44. Height = height;
  45. _panel = Activator.CreateInstance(Type.GetType(type)) as IBasePanel;
  46. ContentBorder.Child = _panel as UIElement;
  47. Title = string.Format("{0} - PRS Desktop (Release {1})", modulesection, CoreUtils.GetVersion());
  48. (_ribbon.Items[0] as RibbonTab).Caption = modulesection;
  49. }
  50. private void Window_Loaded(object sender, RoutedEventArgs e)
  51. {
  52. _panel.Setup();
  53. _panel.Refresh();
  54. var model = _panel.DataModel(Selection.None);
  55. var section = _panel.SectionName;
  56. ReloadModules(section, model);
  57. ReloadReports(section, model);
  58. bLoaded = true;
  59. }
  60. private void RefreshMenu_Click(object sender, RoutedEventArgs e)
  61. {
  62. _panel.Refresh();
  63. }
  64. private void Wiki_Click(object sender, RoutedEventArgs e)
  65. {
  66. Process.Start("https://prsdigital.com.au/wiki/index.php/" + _type.Replace(" ", "_").Replace("/", ""));
  67. }
  68. #region Save / Load Window Location
  69. private void SaveSettings()
  70. {
  71. if (App.IsClosing)
  72. return;
  73. var tuple = new Tuple<string, string, double, double, double, double>(
  74. _panel.GetType().EntityName(),
  75. _modulesection,
  76. Left,
  77. Top,
  78. Width,
  79. Height
  80. );
  81. App.DatabaseSettings.SecondaryWindows[_id] = tuple;
  82. new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);
  83. }
  84. private void Window_Closing(object sender, CancelEventArgs e)
  85. {
  86. if (App.IsClosing)
  87. return;
  88. App.DatabaseSettings.SecondaryWindows.Remove(_id);
  89. new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);
  90. }
  91. private void Window_LocationChanged(object sender, EventArgs e)
  92. {
  93. if (bLoaded)
  94. SaveSettings();
  95. }
  96. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  97. {
  98. if (bLoaded)
  99. SaveSettings();
  100. }
  101. #endregion
  102. #region Report Buttons
  103. private void ReloadReports(string section, DataModel model)
  104. {
  105. Print.Visibility = Security.IsAllowed<CanDesignReports>() ? Visibility.Visible : Visibility.Collapsed;
  106. Print.LauncherButton.Tag = model;
  107. Print.IsLauncherButtonVisible = Security.IsAllowed<CanDesignReports>();
  108. Print.Items.Clear();
  109. var worker = new BackgroundWorker();
  110. IProgress<Tuple<string, Guid>> progress = new Progress<Tuple<string, Guid>>(report => CreateReportButton(report));
  111. worker.DoWork += (o, e) =>
  112. {
  113. var client = new Client<ReportTemplate>();
  114. var templates = client.Query(
  115. new Filter<ReportTemplate>(x => x.DataModel).IsEqualTo(model.Name)
  116. .And(x => x.Section).IsEqualTo(section)
  117. .And(x => x.Visible).IsEqualTo(true),
  118. new Columns<ReportTemplate>(x => x.ID, x => x.Name),
  119. new SortOrder<ReportTemplate>(x => x.Name)
  120. );
  121. foreach (var row in templates.Rows)
  122. progress.Report(
  123. new Tuple<string, Guid>(
  124. row.Get<ReportTemplate, string>(x => x.Name),
  125. row.Get<ReportTemplate, Guid>(x => x.ID)
  126. )
  127. );
  128. };
  129. worker.RunWorkerAsync();
  130. }
  131. public void CreateReportButton(Tuple<string, Guid> report)
  132. {
  133. Print.Visibility = Visibility.Visible;
  134. var button = new RibbonButton
  135. {
  136. Label = report.Item1,
  137. LargeIcon = PRSDesktop.Resources.printer.AsBitmapImage(),
  138. Tag = report.Item2,
  139. SizeForm = SizeForm.Large,
  140. MinWidth = 60
  141. };
  142. button.Click += ReportMenu_Checked;
  143. Print.Items.Add(button);
  144. }
  145. private void ReportMenu_Checked(object sender, RoutedEventArgs e)
  146. {
  147. var item = (RibbonButton)sender;
  148. var id = (Guid)item.Tag;
  149. var template = new Client<ReportTemplate>().Load(new Filter<ReportTemplate>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  150. var selection = Selection.None;
  151. if (template.SelectedRecords && template.AllRecords)
  152. selection = RecordSelectionDialog.Execute();
  153. else if (template.SelectedRecords)
  154. selection = Selection.Selected;
  155. else if (template.AllRecords)
  156. selection = Selection.All;
  157. else
  158. MessageBox.Show("Report must have either [Selected Records] or [All Records] checked to display!");
  159. if (selection != Selection.None)
  160. ReportUtils.PreviewReport(template, _panel.DataModel(selection), false, Security.IsAllowed<CanDesignReports>());
  161. }
  162. private void ManageReportsClick(object sender, RoutedEventArgs e)
  163. {
  164. var model = _panel.DataModel(Selection.None);
  165. var section = _panel.SectionName;
  166. var form = new ReportManager {
  167. DataModel = model,
  168. Section = section
  169. };
  170. form.ShowDialog();
  171. ReloadReports(section, model);
  172. }
  173. #endregion
  174. #region Modules
  175. public void CreatePanelAction(PanelAction action)
  176. {
  177. var button = new RibbonButton
  178. {
  179. Label = action.Caption,
  180. LargeIcon = action.Image.AsBitmapImage(),
  181. SizeForm = SizeForm.Large,
  182. MinWidth = 60,
  183. Tag = action
  184. };
  185. button.Click += PaneActionClick;
  186. Actions.Items.Add(button);
  187. }
  188. public void CreateSetupAction(PanelAction action) => CreatePanelAction(action);
  189. private void PaneActionClick(object sender, RoutedEventArgs e)
  190. {
  191. var button = (Control)sender;
  192. var action = (PanelAction)button.Tag;
  193. action.Execute();
  194. }
  195. private void ReloadModules(string section, DataModel model)
  196. {
  197. if (!ClientFactory.IsSupported<CustomModule>())
  198. return;
  199. Actions.IsLauncherButtonVisible = Security.IsAllowed<CanCustomiseModules>();
  200. while (Actions.Items.Count > 2)
  201. Actions.Items.RemoveAt(Actions.Items.Count - 1);
  202. _panel.CreateToolbarButtons(this);
  203. new Client<CustomModule>().Query(
  204. new Filter<CustomModule>(x => x.Section).IsEqualTo(section)
  205. .And(x => x.DataModel).IsEqualTo(model.Name)
  206. .And(x => x.Visible).IsEqualTo(true),
  207. new Columns<CustomModule>(x => x.ID)
  208. .Add(x => x.Name)
  209. .Add(x => x.Script)
  210. .Add(x => x.Thumbnail.ID),
  211. new SortOrder<CustomModule>(x => x.Name),
  212. (modules, e1) =>
  213. {
  214. if (modules != null)
  215. {
  216. var ids = modules.ExtractValues<CustomModule, Guid>(x => x.Thumbnail.ID).ToArray();
  217. new Client<Document>().Query(
  218. new Filter<Document>(x => x.ID).InList(ids),
  219. new Columns<Document>(x => x.ID)
  220. .Add(x => x.Data),
  221. null,
  222. (documents, e2) =>
  223. {
  224. Dispatcher.Invoke(() =>
  225. {
  226. CreateModules(modules, documents);
  227. CheckModuleSeparator();
  228. });
  229. }
  230. );
  231. }
  232. else
  233. {
  234. Dispatcher.Invoke(() => { CheckModuleSeparator(); });
  235. }
  236. }
  237. );
  238. }
  239. private void CheckModuleSeparator()
  240. {
  241. Separator.Visibility = Actions.Items.Count > 2
  242. ? Visibility.Visible
  243. : Visibility.Collapsed;
  244. }
  245. private void CreateModules(CoreTable modules, CoreTable documents)
  246. {
  247. foreach (var module in modules.Rows)
  248. {
  249. var bmp = PRSDesktop.Resources.edit;
  250. var document = documents.Rows.FirstOrDefault(r =>
  251. r.Get<Document, Guid>(c => c.ID) == module.Get<CustomModule, Guid>(c => c.Thumbnail.ID));
  252. if (document != null)
  253. bmp = new ImageConverter().ConvertFrom(document.Get<Document, byte[]>(c => c.Data)) as Bitmap;
  254. var button = new RibbonButton
  255. {
  256. Label = module.Get<CustomModule, string>(c => c.Name),
  257. LargeIcon = bmp.AsBitmapImage(),
  258. SizeForm = SizeForm.Large,
  259. MinWidth = 60,
  260. Tag = module.Get<CustomModule, string>(c => c.Script)
  261. };
  262. button.Click += Module_Click;
  263. Actions.Items.Add(button);
  264. }
  265. }
  266. private void Module_Click(object sender, RoutedEventArgs e)
  267. {
  268. var item = (RibbonButton)sender;
  269. var code = (string)item.Tag;
  270. if (!string.IsNullOrWhiteSpace(code))
  271. try
  272. {
  273. var script = new ScriptDocument(code);
  274. if (script.Compile())
  275. {
  276. script.SetValue("Data", _panel.Selected());
  277. var result = script.Execute();
  278. if (result)
  279. _panel.Refresh();
  280. }
  281. else
  282. {
  283. MessageBox.Show("Unable to Compile Script!");
  284. }
  285. }
  286. catch (Exception err)
  287. {
  288. MessageBox.Show(CoreUtils.FormatException(err));
  289. }
  290. else
  291. MessageBox.Show("Unable to load " + item.Label);
  292. }
  293. private void ManageModulesClick(object sender, RoutedEventArgs e)
  294. {
  295. var model = _panel.DataModel(Selection.None);
  296. var section = _panel.SectionName;
  297. var manager = new CustomModuleManager
  298. {
  299. DataModel = model,
  300. Section = section
  301. };
  302. manager.ShowDialog();
  303. ReloadModules(section, model);
  304. }
  305. #endregion
  306. }
  307. }