SecondaryWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. private void PaneActionClick(object sender, RoutedEventArgs e)
  189. {
  190. var button = (Control)sender;
  191. var action = (PanelAction)button.Tag;
  192. action.Execute();
  193. }
  194. private void ReloadModules(string section, DataModel model)
  195. {
  196. if (!ClientFactory.IsSupported<CustomModule>())
  197. return;
  198. Actions.IsLauncherButtonVisible = Security.IsAllowed<CanCustomiseModules>();
  199. while (Actions.Items.Count > 2)
  200. Actions.Items.RemoveAt(Actions.Items.Count - 1);
  201. _panel.CreateToolbarButtons(this);
  202. new Client<CustomModule>().Query(
  203. new Filter<CustomModule>(x => x.Section).IsEqualTo(section)
  204. .And(x => x.DataModel).IsEqualTo(model.Name)
  205. .And(x => x.Visible).IsEqualTo(true),
  206. new Columns<CustomModule>(x => x.ID)
  207. .Add(x => x.Name)
  208. .Add(x => x.Script)
  209. .Add(x => x.Thumbnail.ID),
  210. new SortOrder<CustomModule>(x => x.Name),
  211. (modules, e1) =>
  212. {
  213. if (modules != null)
  214. {
  215. var ids = modules.ExtractValues<CustomModule, Guid>(x => x.Thumbnail.ID).ToArray();
  216. new Client<Document>().Query(
  217. new Filter<Document>(x => x.ID).InList(ids),
  218. new Columns<Document>(x => x.ID)
  219. .Add(x => x.Data),
  220. null,
  221. (documents, e2) =>
  222. {
  223. Dispatcher.Invoke(() =>
  224. {
  225. CreateModules(modules, documents);
  226. CheckModuleSeparator();
  227. });
  228. }
  229. );
  230. }
  231. else
  232. {
  233. Dispatcher.Invoke(() => { CheckModuleSeparator(); });
  234. }
  235. }
  236. );
  237. }
  238. private void CheckModuleSeparator()
  239. {
  240. Separator.Visibility = Actions.Items.Count > 2
  241. ? Visibility.Visible
  242. : Visibility.Collapsed;
  243. }
  244. private void CreateModules(CoreTable modules, CoreTable documents)
  245. {
  246. foreach (var module in modules.Rows)
  247. {
  248. var bmp = PRSDesktop.Resources.edit;
  249. var document = documents.Rows.FirstOrDefault(r =>
  250. r.Get<Document, Guid>(c => c.ID) == module.Get<CustomModule, Guid>(c => c.Thumbnail.ID));
  251. if (document != null)
  252. bmp = new ImageConverter().ConvertFrom(document.Get<Document, byte[]>(c => c.Data)) as Bitmap;
  253. var button = new RibbonButton
  254. {
  255. Label = module.Get<CustomModule, string>(c => c.Name),
  256. LargeIcon = bmp.AsBitmapImage(),
  257. SizeForm = SizeForm.Large,
  258. MinWidth = 60,
  259. Tag = module.Get<CustomModule, string>(c => c.Script)
  260. };
  261. button.Click += Module_Click;
  262. Actions.Items.Add(button);
  263. }
  264. }
  265. private void Module_Click(object sender, RoutedEventArgs e)
  266. {
  267. var item = (RibbonButton)sender;
  268. var code = (string)item.Tag;
  269. if (!string.IsNullOrWhiteSpace(code))
  270. try
  271. {
  272. var script = new ScriptDocument(code);
  273. if (script.Compile())
  274. {
  275. script.SetValue("Data", _panel.Selected());
  276. var result = script.Execute();
  277. if (result)
  278. _panel.Refresh();
  279. }
  280. else
  281. {
  282. MessageBox.Show("Unable to Compile Script!");
  283. }
  284. }
  285. catch (Exception err)
  286. {
  287. MessageBox.Show(CoreUtils.FormatException(err));
  288. }
  289. else
  290. MessageBox.Show("Unable to load " + item.Label);
  291. }
  292. private void ManageModulesClick(object sender, RoutedEventArgs e)
  293. {
  294. var model = _panel.DataModel(Selection.None);
  295. var section = _panel.SectionName;
  296. var manager = new CustomModuleManager
  297. {
  298. DataModel = model,
  299. Section = section
  300. };
  301. manager.ShowDialog();
  302. ReloadModules(section, model);
  303. }
  304. #endregion
  305. }
  306. }