InsertDialogViewModel.cs 17 KB

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