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.Reports.Common;
  16. using InABox.Scripting;
  17. using InABox.WPF;
  18. using PRSDesktop.Configuration;
  19. using Syncfusion.Windows.Tools;
  20. using Syncfusion.Windows.Tools.Controls;
  21. namespace PRSDesktop
  22. {
  23. /// <summary>
  24. /// Interaction logic for SecondaryWindow.xaml
  25. /// </summary>
  26. public partial class SecondaryWindow : RibbonWindow, IPanelHost
  27. {
  28. private readonly Guid _id = Guid.Empty;
  29. private readonly string _modulesection = "";
  30. private readonly IBasePanel _panel;
  31. private readonly string _type = "";
  32. private bool bLoaded;
  33. public SecondaryWindow(Guid id, string type, string modulesection, double left, double top, double width, double height)
  34. {
  35. _id = id;
  36. _modulesection = modulesection;
  37. _type = type;
  38. InitializeComponent();
  39. _ribbon.BackStageButton.Visibility = Visibility.Collapsed;
  40. Left = left;
  41. Top = top;
  42. Width = width;
  43. Height = height;
  44. _panel = Activator.CreateInstance(Type.GetType(type)) as IBasePanel;
  45. ContentBorder.Child = _panel as UIElement;
  46. Title = string.Format("{0} - PRS Desktop (Release {1})", modulesection, CoreUtils.GetVersion());
  47. (_ribbon.Items[0] as RibbonTab).Caption = modulesection;
  48. }
  49. private void Window_Loaded(object sender, RoutedEventArgs e)
  50. {
  51. _panel.Setup();
  52. _panel.Refresh();
  53. var model = _panel.DataModel(Selection.None);
  54. var section = _panel.SectionName;
  55. ReloadModules(section, model);
  56. ReloadReports(section, model);
  57. bLoaded = true;
  58. }
  59. private void RefreshMenu_Click(object sender, RoutedEventArgs e)
  60. {
  61. _panel.Refresh();
  62. }
  63. private void Wiki_Click(object sender, RoutedEventArgs e)
  64. {
  65. Process.Start("https://prs-software.com.au/wiki/index.php/" + _type.Replace(" ", "_").Replace("/", ""));
  66. }
  67. #region Save / Load Window Location
  68. private void SaveSettings()
  69. {
  70. if (App.IsClosing)
  71. return;
  72. var tuple = new Tuple<string, string, double, double, double, double>(
  73. _panel.GetType().EntityName(),
  74. _modulesection,
  75. Left,
  76. Top,
  77. Width,
  78. Height
  79. );
  80. App.DatabaseSettings.SecondaryWindows[_id] = tuple;
  81. new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);
  82. }
  83. private void Window_Closing(object sender, CancelEventArgs e)
  84. {
  85. if (App.IsClosing)
  86. return;
  87. App.DatabaseSettings.SecondaryWindows.Remove(_id);
  88. new LocalConfiguration<DatabaseSettings>(App.Profile).Save(App.DatabaseSettings);
  89. }
  90. private void Window_LocationChanged(object sender, EventArgs e)
  91. {
  92. if (bLoaded)
  93. SaveSettings();
  94. }
  95. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  96. {
  97. if (bLoaded)
  98. SaveSettings();
  99. }
  100. #endregion
  101. #region Report Buttons
  102. private void ReloadReports(string section, DataModel model)
  103. {
  104. Print.Visibility = Security.IsAllowed<CanDesignReports>() ? Visibility.Visible : Visibility.Collapsed;
  105. Print.LauncherButton.Tag = model;
  106. Print.IsLauncherButtonVisible = Security.IsAllowed<CanDesignReports>();
  107. Print.Items.Clear();
  108. var worker = new BackgroundWorker();
  109. IProgress<Tuple<string, Guid>> progress = new Progress<Tuple<string, Guid>>(report => CreateReportButton(report));
  110. worker.DoWork += (o, e) =>
  111. {
  112. var client = new Client<ReportTemplate>();
  113. var templates = client.Query(
  114. new Filter<ReportTemplate>(x => x.DataModel).IsEqualTo(model.Name)
  115. .And(x => x.Section).IsEqualTo(section)
  116. .And(x => x.Visible).IsEqualTo(true),
  117. new Columns<ReportTemplate>(x => x.ID, x => x.Name),
  118. new SortOrder<ReportTemplate>(x => x.Name)
  119. );
  120. foreach (var row in templates.Rows)
  121. progress.Report(
  122. new Tuple<string, Guid>(
  123. row.Get<ReportTemplate, string>(x => x.Name),
  124. row.Get<ReportTemplate, Guid>(x => x.ID)
  125. )
  126. );
  127. };
  128. worker.RunWorkerAsync();
  129. }
  130. public void CreateReportButton(Tuple<string, Guid> report)
  131. {
  132. Print.Visibility = Visibility.Visible;
  133. var button = new RibbonButton
  134. {
  135. Label = report.Item1,
  136. LargeIcon = PRSDesktop.Resources.printer.AsBitmapImage(),
  137. Tag = report.Item2,
  138. SizeForm = SizeForm.Large,
  139. MinWidth = 60
  140. };
  141. button.Click += ReportMenu_Checked;
  142. Print.Items.Add(button);
  143. }
  144. private void ReportMenu_Checked(object sender, RoutedEventArgs e)
  145. {
  146. var item = (RibbonButton)sender;
  147. var id = (Guid)item.Tag;
  148. var template = new Client<ReportTemplate>().Load(new Filter<ReportTemplate>(x => x.ID).IsEqualTo(id)).FirstOrDefault();
  149. var selection = Selection.None;
  150. if (template.SelectedRecords && template.AllRecords)
  151. selection = RecordSelectionDialog.Execute();
  152. else if (template.SelectedRecords)
  153. selection = Selection.Selected;
  154. else if (template.AllRecords)
  155. selection = Selection.All;
  156. else
  157. MessageBox.Show("Report must have either [Selected Records] or [All Records] checked to display!");
  158. if (selection != Selection.None)
  159. ReportUtils.PreviewReport(template, _panel.DataModel(selection), false, Security.IsAllowed<CanDesignReports>());
  160. ;
  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. }