InsertDialogViewModel.cs 18 KB

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