KMPageEditSplitWindowController.swift 7.1 KB

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