InsertDialogViewModel.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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.Drawing.Printing;
  11. using System.Linq;
  12. using System.Text;
  13. using System.Threading.Tasks;
  14. using System.Windows;
  15. namespace PDF_Master.ViewModels.Dialog.PageEditDialogs
  16. {
  17. public class InsertDialogViewModel : BindableBase, IDialogAware
  18. {
  19. public string Title => "";
  20. /// <summary>
  21. /// 数据模型
  22. /// </summary>
  23. private CustomInsertModel Model = new CustomInsertModel();
  24. public event Action<IDialogResult> RequestClose;
  25. private Size size = new Size();
  26. private string currentPageSize;
  27. /// <summary>
  28. /// 当前页的尺寸大小 括号显示的形式
  29. /// </summary>
  30. public string CurrentPageSize
  31. {
  32. get { return currentPageSize; }
  33. set
  34. {
  35. SetProperty(ref currentPageSize, value);
  36. }
  37. }
  38. private int listSelectedIndex;
  39. public int ListSelectedIndex
  40. {
  41. get { return listSelectedIndex; }
  42. set
  43. {
  44. SetProperty(ref listSelectedIndex, value);
  45. }
  46. }
  47. private int itemSelectedIndex = 0;
  48. /// <summary>
  49. /// 自定义页面的选中索引
  50. /// </summary>
  51. public int ItemSelectedIndex
  52. {
  53. get { return itemSelectedIndex; }
  54. set
  55. {
  56. SetProperty(ref itemSelectedIndex, value);
  57. if (Model != null)
  58. {
  59. Model.filepath = Pages[itemSelectedIndex].FilePath;
  60. }
  61. }
  62. }
  63. private string customWidth;
  64. /// <summary>
  65. /// 自定义页面宽度
  66. /// </summary>
  67. public string CustomWidth
  68. {
  69. get { return customWidth; }
  70. set
  71. {
  72. SetProperty(ref customWidth, value);
  73. }
  74. }
  75. private string customHeight;
  76. /// <summary>
  77. /// 自定义页面高度
  78. /// </summary>
  79. public string CustomHeight
  80. {
  81. get { return customHeight; }
  82. set
  83. {
  84. SetProperty(ref customHeight, value);
  85. }
  86. }
  87. private int unitsSelectedIndex = 0;
  88. /// <summary>
  89. /// 单位下拉框的选中项索引
  90. /// </summary>
  91. public int UnitsSelectedIndex
  92. {
  93. get { return unitsSelectedIndex; }
  94. set
  95. {
  96. SetProperty(ref unitsSelectedIndex, value);
  97. }
  98. }
  99. private int pageSizeSelectedIndex = 0;
  100. /// <summary>
  101. /// 页面尺寸选中项
  102. /// </summary>
  103. public int PageSizeSelectedIndex
  104. {
  105. get { return pageSizeSelectedIndex; }
  106. set
  107. {
  108. SetProperty(ref pageSizeSelectedIndex, value);
  109. if (pageSizeSelectedIndex != -1)
  110. {
  111. PageSizeInfo pageSizeInfo = PageSizeInfos[pageSizeSelectedIndex];
  112. if (pageSizeInfo != null)
  113. {
  114. CustomWidth = pageSizeInfo.Width;
  115. CustomHeight = pageSizeInfo.Height;
  116. Model.width = Convert.ToInt32(pageSizeInfo.Width);
  117. Model.height = Convert.ToInt32(pageSizeInfo.Height);
  118. //V_Width = CustomWidth;
  119. //V_Height = CustomHeight;
  120. //H_Width = CustomHeight;
  121. //H_Height = CustomWidth;
  122. }
  123. }
  124. }
  125. }
  126. private bool isCurrentIsEnabled = true;
  127. public bool IsCurrentIsEnabled
  128. {
  129. get { return isCurrentIsEnabled; }
  130. set
  131. {
  132. SetProperty(ref isCurrentIsEnabled, value);
  133. }
  134. }
  135. private bool isCurrentSelected = true;
  136. public bool IsCurrentSelected
  137. {
  138. get { return isCurrentSelected; }
  139. set { SetProperty(ref isCurrentSelected, value); }
  140. }
  141. private bool isStandSelcted;
  142. public bool IsStandSelected
  143. {
  144. get { return isStandSelcted; }
  145. set
  146. {
  147. SetProperty(ref isStandSelcted, value);
  148. }
  149. }
  150. private bool isCustomSelected;
  151. public bool IsCustomSelected
  152. {
  153. get { return isCustomSelected; }
  154. set
  155. {
  156. SetProperty(ref isCustomSelected, value);
  157. }
  158. }
  159. private bool isVerticalSelected = true;
  160. public bool IsVerticalSelected
  161. {
  162. get { return isVerticalSelected; }
  163. set
  164. {
  165. SetProperty(ref isVerticalSelected, value);
  166. if (isVerticalSelected)
  167. {
  168. //if (V_Width != null && V_Height != null)
  169. //{
  170. // CustomHeight = V_Height;
  171. // CustomWidth = V_Width;
  172. //}
  173. if (Model.height != 0 && Model.width != 0)
  174. {
  175. CustomHeight = Model.height.ToString();
  176. CustomWidth = Model.width.ToString();
  177. }
  178. }
  179. }
  180. }
  181. private bool isHorizontalSelected;
  182. public bool IsHorizontalSelected
  183. {
  184. get { return isHorizontalSelected; }
  185. set
  186. {
  187. SetProperty(ref isHorizontalSelected, value);
  188. if (isHorizontalSelected)
  189. {
  190. //if (H_Width != null && H_Height != null)
  191. //{
  192. // CustomHeight = H_Height;
  193. // CustomWidth = H_Width;
  194. //}
  195. if (Model.height != 0 && Model.width != 0)
  196. {
  197. CustomHeight = Model.width.ToString();
  198. CustomWidth = Model.height.ToString();
  199. }
  200. }
  201. }
  202. }
  203. //public string H_Width = null;
  204. //public string H_Height = null;
  205. //public string V_Width = null;
  206. //public string V_Height = null;
  207. /// <summary>
  208. /// 自定义页面的路径集合
  209. /// </summary>
  210. public ObservableCollection<CustomPageItem> Pages { get; set; }
  211. /// <summary>
  212. /// 页面单位集合
  213. /// </summary>
  214. public List<string> Units { get; set; }
  215. public List<string> PageSize { get; set; }
  216. public List<PageSizeInfo> PageSizeInfos { get; set; }
  217. public DelegateCommand CancelCommand { get; set; }
  218. public DelegateCommand InsertCommnad { get; set; }
  219. public InsertDialogViewModel()
  220. {
  221. InitPageSource();
  222. InitUnits();
  223. InitPage();
  224. CancelCommand = new DelegateCommand(cancel);
  225. InsertCommnad = new DelegateCommand(insert);
  226. }
  227. private void InitPage()
  228. {
  229. PageSize = new List<string>();
  230. //PageSize.Add("Letter(8.5x11 inches)");
  231. //PageSize.Add("Legal (210 x 297mm)");
  232. //PageSize.Add("A3 (297 x 420mm)");
  233. //PageSize.Add("A4 (210 x 297mm)");
  234. //PageSize.Add("A5 (148 x 210mm)");
  235. //PageSize.Add("B4 (250 x 353mm)");
  236. //PageSize.Add("B5 (176 x 250mm)");
  237. //PageSize.Add("Executive (7.25x10.5 inches)");
  238. //PageSize.Add("US4x6 (4x6 inches)");
  239. //PageSize.Add("US4x8 (4x8 inches)");
  240. //PageSize.Add("US5x7 (5x7 inches)");
  241. //PageSize.Add("Comm10 (4.125x9.5 inches)");
  242. PageSizeInfos = new List<PageSizeInfo>() {
  243. new PageSizeInfo(){ Name="A3",Width="297",Height="420",Unit="mm"},
  244. new PageSizeInfo(){ Name="A4",Width="210",Height="297",Unit="mm"},
  245. new PageSizeInfo(){ Name="A5",Width="148",Height="210",Unit="mm"},
  246. new PageSizeInfo(){ Name="B5",Width="176",Height="250",Unit="mm"},
  247. new PageSizeInfo(){ Name="10号信封",Width="105",Height="241",Unit="mm"},
  248. new PageSizeInfo(){ Name="Choukei 3信封",Width="120",Height="235",Unit="mm"},
  249. new PageSizeInfo(){ Name="DL信封",Width="110",Height="220",Unit="mm"},
  250. new PageSizeInfo(){ Name="JIS B5",Width="182",Height="257",Unit="mm"},
  251. new PageSizeInfo(){ Name="ROC 16K",Width="197",Height="273",Unit="mm"},
  252. new PageSizeInfo(){ Name="超大B/A3型",Width="330",Height="483",Unit="mm"},
  253. new PageSizeInfo(){ Name="小报用纸",Width="279",Height="432",Unit="mm"},
  254. new PageSizeInfo(){ Name="小报用纸(特大)",Width="305",Height="457",Unit="mm"},
  255. new PageSizeInfo(){ Name="美国法定用纸",Width="216",Height="356",Unit="mm"},
  256. new PageSizeInfo(){ Name="美国信纸",Width="216",Height="279",Unit="mm"},
  257. };
  258. foreach (var item in PageSizeInfos)
  259. {
  260. PageSize.Add($"{item.Name}({item.Width} x {item.Height} {item.Unit})");
  261. }
  262. }
  263. /// <summary>
  264. /// 初始化页面大小单位集合
  265. /// </summary>
  266. private void InitUnits()
  267. {
  268. Units = new List<string>();
  269. Units.Add("mm");
  270. Units.Add("cm");
  271. Units.Add("in");
  272. }
  273. /// <summary>
  274. /// 初始化自定义页面集合
  275. /// </summary>
  276. private void InitPageSource()
  277. {
  278. Pages = new ObservableCollection<CustomPageItem>();
  279. Pages.Add(new CustomPageItem() { Name = "空白页", FilePath = "" });
  280. Pages.Add(new CustomPageItem() { Name = "横线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.jpg") });
  281. Pages.Add(new CustomPageItem() { Name = "五线谱", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\Staff.jpg") });
  282. Pages.Add(new CustomPageItem() { Name = "格子线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\GridLine.jpg") });
  283. }
  284. private void cancel()
  285. {
  286. RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
  287. }
  288. private void insert()
  289. {
  290. //最后统一处理页面尺寸
  291. if (IsCurrentSelected)
  292. {
  293. Model.width = (int)size.Width;
  294. Model.height = (int)size.Height;
  295. }
  296. else if (IsStandSelected)
  297. {
  298. PageSizeInfo pageSizeInfo = PageSizeInfos[PageSizeSelectedIndex];
  299. if (pageSizeInfo != null)
  300. {
  301. Model.width = Convert.ToInt32(pageSizeInfo.Width);
  302. Model.height = Convert.ToInt32(pageSizeInfo.Height);
  303. }
  304. }
  305. else
  306. {
  307. int width = (int)size.Width;
  308. int height = (int)size.Height;
  309. int.TryParse(customWidth, out width);
  310. int.TryParse(customHeight, out height);
  311. if (width <= 0)
  312. {
  313. width = (int)size.Width;
  314. }
  315. if (height <= 0)
  316. {
  317. height = (int)size.Height;
  318. }
  319. switch (unitsSelectedIndex)
  320. {
  321. case 1:
  322. width = width / 10;
  323. height = height / 10;
  324. break;
  325. case 2:
  326. width = (int)(width / 25.4);
  327. height = (int)(height / 25.4);
  328. break;
  329. case 0:
  330. default:
  331. break;
  332. }
  333. Model.width = width;
  334. Model.height = height;
  335. }
  336. //方向处理
  337. if (isVerticalSelected)
  338. {
  339. if (Model.height <= Model.width)
  340. {
  341. //纵向 需要重新定义宽高
  342. var temp = Model.height;
  343. Model.height = Model.width;
  344. Model.width = temp;
  345. }
  346. }
  347. else
  348. {
  349. if (Model.height > Model.width)
  350. {
  351. var temp = Model.height;
  352. Model.height = Model.width;
  353. Model.width = temp;
  354. }
  355. }
  356. DialogParameters valuePairs = new DialogParameters();
  357. //将mm转换成像素单位
  358. Model.width = (int)CommonHelper.GetPageSizeFomrUnit(Model.width);
  359. Model.height = (int)CommonHelper.GetPageSizeFomrUnit(Model.height);
  360. valuePairs.Add(ParameterNames.DataModel, Model);
  361. RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
  362. }
  363. #region 弹窗接口
  364. public bool CanCloseDialog()
  365. {
  366. return true;
  367. }
  368. public void OnDialogClosed()
  369. {
  370. }
  371. public void OnDialogOpened(IDialogParameters parameters)
  372. {
  373. if (parameters != null)
  374. {
  375. size = parameters.GetValue<Size>(ParameterNames.CurrentPageSize);
  376. ListSelectedIndex = parameters.GetValue<int>(ParameterNames.PageEditSelectedIndex);
  377. //未选择页面时,【当前页】项置灰不可点击,默认选中【标准】A4
  378. if (ListSelectedIndex == -1)
  379. {
  380. IsCurrentIsEnabled = false;
  381. IsStandSelected = true;
  382. PageSizeSelectedIndex = 1;
  383. PageSizeInfo pageSizeInfo = PageSizeInfos[PageSizeSelectedIndex];
  384. if (pageSizeInfo != null)
  385. {
  386. CustomWidth = pageSizeInfo.Width;
  387. CustomHeight = pageSizeInfo.Height;
  388. Model.width = Convert.ToInt32(pageSizeInfo.Width);
  389. Model.height = Convert.ToInt32(pageSizeInfo.Height);
  390. }
  391. }
  392. else
  393. {
  394. IsCurrentIsEnabled = true;
  395. CurrentPageSize = $"({size.Width.ToString("F0")}mm*{size.Height.ToString("F0")}mm)";
  396. CustomWidth = size.Width.ToString("F0");
  397. CustomHeight = size.Height.ToString("F0");
  398. Model.width = (int)size.Width;
  399. Model.height = (int)size.Height;
  400. }
  401. //V_Width = CustomWidth;
  402. //V_Height = CustomHeight;
  403. //H_Width = CustomHeight;
  404. //H_Height = CustomWidth;
  405. //IsVerticalSelected = true;
  406. }
  407. }
  408. #endregion 弹窗接口
  409. }
  410. }