InsertDialogViewModel.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. using PDF_Master.Helper;
  2. using PDF_Master.Model;
  3. using PDF_Master.Model.PageEdit;
  4. using Prism.Commands;
  5. using Prism.Mvvm;
  6. using Prism.Services.Dialogs;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Collections.ObjectModel;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. namespace PDF_Master.ViewModels.Dialog.PageEditDialogs
  15. {
  16. public class InsertDialogViewModel : BindableBase, IDialogAware
  17. {
  18. public string Title => "";
  19. /// <summary>
  20. /// 数据模型
  21. /// </summary>
  22. private CustomInsertModel Model = new CustomInsertModel();
  23. public event Action<IDialogResult> RequestClose;
  24. private Size size = new Size();
  25. private string currentPageSize;
  26. /// <summary>
  27. /// 当前页的尺寸大小 括号显示的形式
  28. /// </summary>
  29. public string CurrentPageSize
  30. {
  31. get { return currentPageSize; }
  32. set
  33. {
  34. SetProperty(ref currentPageSize, value);
  35. }
  36. }
  37. private int itemSelectedIndex = 0;
  38. /// <summary>
  39. /// 自定义页面的选中索引
  40. /// </summary>
  41. public int ItemSelectedIndex
  42. {
  43. get { return itemSelectedIndex; }
  44. set
  45. {
  46. SetProperty(ref itemSelectedIndex, value);
  47. if (Model != null)
  48. {
  49. Model.filepath = Pages[itemSelectedIndex].FilePath;
  50. }
  51. }
  52. }
  53. private string customWidth;
  54. /// <summary>
  55. /// 自定义页面宽度
  56. /// </summary>
  57. public string CustomWidth
  58. {
  59. get { return customWidth; }
  60. set
  61. {
  62. SetProperty(ref customWidth, value);
  63. }
  64. }
  65. private string customHeight;
  66. /// <summary>
  67. /// 自定义页面高度
  68. /// </summary>
  69. public string CustomHeight
  70. {
  71. get { return customHeight; }
  72. set
  73. {
  74. SetProperty(ref customHeight, value);
  75. }
  76. }
  77. private int unitsSelectedIndex = 0;
  78. /// <summary>
  79. /// 单位下拉框的选中项索引
  80. /// </summary>
  81. public int UnitsSelectedIndex
  82. {
  83. get { return unitsSelectedIndex; }
  84. set
  85. {
  86. SetProperty(ref unitsSelectedIndex, value);
  87. }
  88. }
  89. private int pageSizeSelectedIndex = 0;
  90. /// <summary>
  91. /// 页面尺寸选中项
  92. /// </summary>
  93. public int PageSizeSelectedIndex
  94. {
  95. get { return pageSizeSelectedIndex; }
  96. set
  97. {
  98. SetProperty(ref pageSizeSelectedIndex, value);
  99. }
  100. }
  101. private bool isCurrentSelected = true;
  102. public bool IsCurrentSelected
  103. {
  104. get { return isCurrentSelected; }
  105. set { SetProperty(ref isCurrentSelected, value); }
  106. }
  107. private bool isStandSelcted;
  108. public bool IsStandSelected
  109. {
  110. get { return isStandSelcted; }
  111. set
  112. {
  113. SetProperty(ref isStandSelcted, value);
  114. }
  115. }
  116. private bool isCustomSelected;
  117. public bool IsCustomSelected
  118. {
  119. get { return isCustomSelected; }
  120. set
  121. {
  122. SetProperty(ref isCustomSelected, value);
  123. }
  124. }
  125. private bool isVerticalSelected = true;
  126. public bool IsVerticalSelected
  127. {
  128. get { return isVerticalSelected; }
  129. set
  130. {
  131. SetProperty(ref isVerticalSelected, value);
  132. }
  133. }
  134. private bool isHorizontalSelected;
  135. public bool IsHorizontalSelected
  136. {
  137. get { return isHorizontalSelected; }
  138. set
  139. {
  140. SetProperty(ref isHorizontalSelected, value);
  141. }
  142. }
  143. /// <summary>
  144. /// 自定义页面的路径集合
  145. /// </summary>
  146. public ObservableCollection<CustomPageItem> Pages { get; set; }
  147. /// <summary>
  148. /// 页面单位集合
  149. /// </summary>
  150. public List<string> Units { get; set; }
  151. public List<string> PageSize { get; set; }
  152. public DelegateCommand CancelCommand { get; set; }
  153. public DelegateCommand InsertCommnad { get; set; }
  154. public InsertDialogViewModel()
  155. {
  156. InitPageSource();
  157. InitUnits();
  158. InitPage();
  159. CancelCommand = new DelegateCommand(cancel);
  160. InsertCommnad = new DelegateCommand(insert);
  161. }
  162. private void InitPage()
  163. {
  164. PageSize = new List<string>();
  165. PageSize.Add("Letter(8.5x11 inches)");
  166. PageSize.Add("Legal (210 x 297mm)");
  167. PageSize.Add("A3 (297 x 420mm)");
  168. PageSize.Add("A4 (210 x 297mm)");
  169. PageSize.Add("A5 (148 x 210mm)");
  170. PageSize.Add("B4 (250 x 353mm)");
  171. PageSize.Add("B5 (176 x 250mm)");
  172. PageSize.Add("Executive (7.25x10.5 inches)");
  173. PageSize.Add("US4x6 (4x6 inches)");
  174. PageSize.Add("US4x8 (4x8 inches)");
  175. PageSize.Add("US5x7 (5x7 inches)");
  176. PageSize.Add("Comm10 (4.125x9.5 inches)");
  177. }
  178. /// <summary>
  179. /// 初始化页面大小单位集合
  180. /// </summary>
  181. private void InitUnits()
  182. {
  183. Units = new List<string>();
  184. Units.Add("mm");
  185. Units.Add("cm");
  186. Units.Add("in");
  187. }
  188. /// <summary>
  189. /// 初始化自定义页面集合
  190. /// </summary>
  191. private void InitPageSource()
  192. {
  193. Pages = new ObservableCollection<CustomPageItem>();
  194. Pages.Add(new CustomPageItem() { Name = "空白页", FilePath = "" });
  195. Pages.Add(new CustomPageItem() { Name = "横线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.jpg") });
  196. Pages.Add(new CustomPageItem() { Name = "五线谱", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\Staff.jpg") });
  197. Pages.Add(new CustomPageItem() { Name = "格子线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\GridLine.jpg") });
  198. }
  199. private void cancel()
  200. {
  201. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  202. }
  203. private void insert()
  204. {
  205. //最后统一处理页面尺寸
  206. if(IsCurrentSelected)
  207. {
  208. Model.width = (int)size.Width;
  209. Model.height = (int)size.Height;
  210. }
  211. else if(IsStandSelected)
  212. {
  213. //TODO:
  214. }
  215. else
  216. {
  217. int width = (int)size.Width;
  218. int height = (int)size.Height;
  219. int.TryParse(customWidth,out width);
  220. int.TryParse(customHeight,out height);
  221. if(width <= 0)
  222. {
  223. width = (int)size.Width;
  224. }
  225. if(height<=0)
  226. {
  227. height = (int)size.Height;
  228. }
  229. switch (unitsSelectedIndex)
  230. {
  231. case 1:
  232. width = width / 10;
  233. height = height / 10;
  234. break;
  235. case 2:
  236. width = (int)(width / 25.4);
  237. height = (int)(height / 25.4);
  238. break;
  239. case 0:
  240. default:
  241. break;
  242. }
  243. Model.width = width;
  244. Model.height = height;
  245. }
  246. //方向处理
  247. if (isVerticalSelected)
  248. {
  249. if (Model.height <= Model.width)
  250. {
  251. var temp = Model.height;
  252. Model.height = Model.width;
  253. Model.width = Model.height;
  254. }
  255. }
  256. else
  257. {
  258. if (Model.height > Model.width)
  259. {
  260. var temp = Model.height;
  261. Model.height = Model.width;
  262. Model.width = temp;
  263. }
  264. }
  265. DialogParameters valuePairs = new DialogParameters();
  266. //将mm转换成像素单位
  267. Model.width = (int)CommonHelper.GetPageSizeFomrUnit(Model.width);
  268. Model.height = (int)CommonHelper.GetPageSizeFomrUnit(Model.height);
  269. valuePairs.Add(ParameterNames.DataModel, Model);
  270. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  271. }
  272. #region 弹窗接口
  273. public bool CanCloseDialog()
  274. {
  275. return true;
  276. }
  277. public void OnDialogClosed()
  278. {
  279. }
  280. public void OnDialogOpened(IDialogParameters parameters)
  281. {
  282. if (parameters != null)
  283. {
  284. size = parameters.GetValue<Size>("CurrentPageSize");
  285. CurrentPageSize = $"({size.Width.ToString("F0")}mm*{size.Height.ToString("F0")}mm)";
  286. CustomWidth = size.Width.ToString("F2");
  287. CustomHeight = size.Height.ToString("F2");
  288. Model.width = (int)size.Width;
  289. Model.height = (int)size.Height;
  290. }
  291. }
  292. #endregion
  293. }
  294. }