InsertDialogViewModel.cs 16 KB

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