KMPageEditSplitWindowController.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // KMPageEditSplitWindowController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/12.
  6. //
  7. class KMPageEditSplitWindowController: KMPageEditSettingBaseWindowController {
  8. var hasPreView = false
  9. override func initSubViews() {
  10. super.initSubViews()
  11. if (self.hasPreView) {
  12. let controller = KMPDFInsertPreviewViewController(nibName: "KMPDFInsertPreviewViewController", bundle: nil)
  13. controller.pdfDocument = CPDFDocument(url: self.model?.documentURL)
  14. self.contentBox.contentView?.addSubview(controller.view)
  15. controller.view.frame = NSMakeRect(0, 40, 176+16*2, 300)
  16. controller.view.autoresizingMask = [.maxYMargin]
  17. if let settingView: KMPageEditSplitSettingView = KMPageEditSplitSettingView.createFromNib() {
  18. self.contentBox.contentView?.addSubview(settingView)
  19. let settingViewLeft: CGFloat = 176+16+8
  20. settingView.frame = NSRect(x: settingViewLeft, y: 0, width: NSWidth(self.contentBox.frame)-settingViewLeft, height: NSHeight(self.contentBox.frame))
  21. settingView.autoresizingMask = [.width, .height]
  22. self.settingView = settingView
  23. settingView.pageCount = self.model?.pageCount ?? 0
  24. settingView.type = (self.model as? KMPageEditSplitSettingModel)?.type ?? 0
  25. if (settingView.type == 2) {
  26. settingView.setPageRangeString((self.model as? KMPageEditSplitSettingModel)?.pageRangeString ?? "")
  27. }
  28. settingView.updateName(self.model?.outputFileName ?? "")
  29. }
  30. } else {
  31. if let settingView: KMPageEditSplitSettingView = KMPageEditSplitSettingView.createFromNib() {
  32. self.contentBox.contentView = settingView
  33. self.settingView = settingView
  34. settingView.pageCount = self.model?.pageCount ?? 0
  35. settingView.type = (self.model as? KMPageEditSplitSettingModel)?.type ?? 0
  36. if ((self.model as! KMPageEditSplitSettingModel).type == 2) {
  37. settingView.setPageRangeString((self.model as? KMPageEditSplitSettingModel)?.pageRangeString ?? "")
  38. }
  39. settingView.updateName(self.model!.outputFileName)
  40. }
  41. }
  42. self.settingView?.itemClick = { [unowned self] index, value in
  43. if (index == 1) { /// 类型切换
  44. (self.model as? KMPageEditSplitSettingModel)?.type = value as? Int ?? 0
  45. return
  46. }
  47. if (index == 2) { /// 平均每几页拆分
  48. (self.model as? KMPageEditSplitSettingModel)?.avgNumberForOneFile = value as? Int ?? 0
  49. return
  50. }
  51. if (index == 3) { /// 平均拆分几个文件
  52. (self.model as? KMPageEditSplitSettingModel)?.avgSplitNumberFile = value as? Int ?? 0
  53. return
  54. }
  55. if (index == 4) { /// 页面范围切换
  56. (self.model as? KMPageEditSplitSettingModel)?.pageRangeType = value as? Int ?? 0
  57. return
  58. }
  59. if (index == 5) { /// 页面范围输入
  60. var pageCount = 0
  61. if let _pageCount = self.model?.pageCount {
  62. pageCount = _pageCount
  63. }
  64. if let data = value as? String, let _value = Int(data) {
  65. if (_value < 1 || _value > pageCount) {
  66. let alert = NSAlert()
  67. alert.messageText = NSLocalizedString("Page range must between 1 and \(pageCount)", comment: "")
  68. alert.informativeText = NSLocalizedString("Out of page range", comment: "")
  69. alert.runModal()
  70. (self.settingView as? KMPageEditSplitSettingView)?.setPageRangeString("1")
  71. (self.model as? KMPageEditSplitSettingModel)?.pageRangeType = 2
  72. (self.model as? KMPageEditSplitSettingModel)?.pageRangeString = "1"
  73. return
  74. }
  75. }
  76. (self.model as? KMPageEditSplitSettingModel)?.pageRangeType = 2
  77. (self.model as? KMPageEditSplitSettingModel)?.pageRangeString = value as? String ?? ""
  78. return
  79. }
  80. if (index == 6) { /// 将原始文档名前置
  81. (self.model as? KMPageEditSplitSettingModel)?.origFileNameIsPrefix = value as? Bool ?? false
  82. (self.settingView as? KMPageEditSplitSettingView)?.updateName(self.model?.outputFileName ?? "")
  83. return
  84. }
  85. if (index == 7) { /// 标签
  86. (self.model as? KMPageEditSplitSettingModel)?.hasTag = value as? Bool ?? false
  87. (self.settingView as? KMPageEditSplitSettingView)?.updateName(self.model?.outputFileName ?? "")
  88. return
  89. }
  90. if (index == 8) { /// 分隔符
  91. (self.model as? KMPageEditSplitSettingModel)?.hasSep = value as? Bool ?? false
  92. (self.settingView as? KMPageEditSplitSettingView)?.updateName(self.model?.outputFileName ?? "")
  93. return
  94. }
  95. if (index == 9) { /// 分隔符
  96. (self.model as? KMPageEditSplitSettingModel)?.tagString = value as? String ?? ""
  97. (self.settingView as? KMPageEditSplitSettingView)?.updateName(self.model?.outputFileName ?? "")
  98. return
  99. }
  100. if (index == 10) { /// 分隔符
  101. (self.model as? KMPageEditSplitSettingModel)?.sepString = value as? String ?? ""
  102. (self.settingView as? KMPageEditSplitSettingView)?.updateName(self.model?.outputFileName ?? "")
  103. return
  104. }
  105. }
  106. }
  107. override func initDefaultValue() {
  108. super.initDefaultValue()
  109. if (self.hasPreView) {
  110. self.setWindowSize(NSSize(width: 583, height: 481))
  111. self.titleLabel.stringValue = NSLocalizedString("Split", comment: "")
  112. } else {
  113. self.setWindowSize(NSSize(width: 480, height: 450))
  114. self.titleLabel.stringValue = NSLocalizedString("Split Page", comment: "")
  115. }
  116. self.funcVC?.stringValue = NSLocalizedString("Split", comment: "")
  117. self.funcVC?.updateUI()
  118. }
  119. override func funcButtonAction() {
  120. guard let _model = self.model as? KMPageEditSplitSettingModel else {
  121. super.funcButtonAction()
  122. return
  123. }
  124. if (_model.pageRangeType == 2) {
  125. let pages = KMPageRangeTools.findSelectPage(pageRangeString: _model.pageRangeString, pageCount: _model.pageCount)
  126. if (pages.isEmpty) {
  127. let alert = NSAlert()
  128. alert.messageText = NSLocalizedString("Page range must between 1 and \(_model.pageCount)", comment: "")
  129. alert.informativeText = NSLocalizedString("Out of page range", comment: "")
  130. alert.runModal()
  131. (self.settingView as? KMPageEditSplitSettingView)?.setPageRangeString("1")
  132. _model.pageRangeString = "1"
  133. return
  134. }
  135. }
  136. super.funcButtonAction()
  137. }
  138. }