KMBatesController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. //
  2. // KMBatesController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMBatesControllerDelegate: AnyObject {
  10. @objc optional func kmBatesControllerDidUpdateMode(_ view: KMBatesController)
  11. //数据发生变化时处理
  12. @objc optional func kmBatesControllerDidModelDataUpdated(_ view: KMBatesController)
  13. }
  14. class KMBatesController: NSViewController {
  15. @IBOutlet var contendView: NSView!
  16. @IBOutlet var preInfoView: NSView!
  17. @IBOutlet var propertyInfoView: NSView!
  18. var documentPreview: KMEditDocumentController = KMEditDocumentController.init()
  19. var propertyController: KMBatesPropertyController = KMBatesPropertyController.init()
  20. var templatesController: KMBatesTemplateController = KMBatesTemplateController()
  21. var saveTemplateMessage: ComponentMessage = ComponentMessage()
  22. weak open var delegate: KMBatesControllerDelegate?
  23. var totalPDFCount: Int = 1
  24. var pdfDocument: CPDFDocument?
  25. var batesModel: KMBatesModel? {
  26. get {
  27. if editSubType == .template {
  28. return templatesController.selectedDataModel
  29. } else if editSubType == .add ||
  30. editSubType == .edit {
  31. return propertyController.batesModel
  32. }
  33. return nil
  34. }
  35. }
  36. var editSubType: KMPDFEditSubModeType = .none
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. // Do view setup here.
  40. documentPreview.view.frame = preInfoView.bounds
  41. documentPreview.view.autoresizingMask = [.width, .height]
  42. preInfoView.addSubview(documentPreview.view)
  43. propertyController.view.frame = propertyInfoView.bounds
  44. propertyController.view.autoresizingMask = [.width, .height]
  45. propertyController.delegate = self
  46. propertyController.view.isHidden = true
  47. propertyInfoView.addSubview(propertyController.view)
  48. templatesController.view.frame = propertyInfoView.bounds
  49. templatesController.view.autoresizingMask = [.width, .height]
  50. templatesController.delegate = self
  51. templatesController.view.isHidden = true
  52. propertyInfoView.addSubview(templatesController.view)
  53. setupProperty()
  54. }
  55. func setupProperty() {
  56. saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template"))
  57. }
  58. func resetUI() {
  59. propertyController.view.isHidden = true
  60. templatesController.view.isHidden = true
  61. if editSubType == .template {
  62. templatesController.view.isHidden = false
  63. } else if editSubType == .add {
  64. propertyController.view.isHidden = false
  65. propertyController.editSubType = editSubType
  66. } else if editSubType == .edit {
  67. propertyController.view.isHidden = false
  68. propertyController.editSubType = editSubType
  69. }
  70. }
  71. func reloadData() {
  72. propertyController.totalPDFCount = totalPDFCount
  73. documentPreview.pdfDocument = pdfDocument
  74. documentPreview.reloadData()
  75. if editSubType == .template {
  76. templatesController.reloadData()
  77. } else if editSubType == .add {
  78. propertyController.editSubType = editSubType
  79. propertyController.reloadData()
  80. } else if editSubType == .edit {
  81. propertyController.editSubType = editSubType
  82. propertyController.reloadData()
  83. }
  84. }
  85. func updatePDFDocumentBates() {
  86. if let data = batesModel {
  87. let cPDFBates = documentPreview.pdfDocument?.bates()
  88. cPDFBates?.pageString = "0"
  89. let fontSize = data.fontsize
  90. let fontName: String = data.fontName
  91. let font = NSFont.boldSystemFont(ofSize:fontSize)
  92. let style = NSMutableParagraphStyle()
  93. style.alignment = .center
  94. style.lineBreakMode = .byCharWrapping
  95. let size: NSSize = "text".boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: [NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style]).size
  96. cPDFBates?.margin = NSEdgeInsetsMake(max(CGFloat(data.topMargin)-size.height, 0), CGFloat(data.leftMargin), max(CGFloat(data.bottomMargin)-size.height, 0), CGFloat(data.rightMargin))
  97. let strings = KMBatesManager.parseModel(model: data, UInt(totalPDFCount))
  98. var count: Int = 0
  99. let color = data.color
  100. for text in strings {
  101. cPDFBates?.setText(text, at: UInt(count))
  102. cPDFBates?.setTextColor(color, at: UInt(count))
  103. cPDFBates?.setFontSize(fontSize, at: UInt(count))
  104. cPDFBates?.setFontName(fontName, at: UInt(count))
  105. count += 1
  106. }
  107. cPDFBates?.update()
  108. self.documentPreview.refreshUI()
  109. } else {
  110. pdfDocument?.bates().clear()
  111. documentPreview.refreshUI()
  112. }
  113. }
  114. }
  115. //MARK: - KMHeaderPropertyControllerDelegate
  116. extension KMBatesController: KMBatesPropertyControllerDelegate {
  117. func batesPropertyControllerDidUpdate(_ controller: KMBatesPropertyController) {
  118. updatePDFDocumentBates()
  119. }
  120. func batesPropertyControllerDidChangetoTemplate(_ controller: KMBatesPropertyController) {
  121. editSubType = .template
  122. resetUI()
  123. delegate?.kmBatesControllerDidUpdateMode?(self)
  124. templatesController.selectedDataModel = nil
  125. templatesController.reloadData()
  126. updatePDFDocumentBates()
  127. delegate?.kmBatesControllerDidModelDataUpdated?(self)
  128. }
  129. func batesPropertyControllerSaveTemplateSuccess(_ controller: KMBatesPropertyController, _ data: KMBatesModel) {
  130. saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2,
  131. CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8,
  132. saveTemplateMessage.properties.propertyInfo.viewWidth,
  133. saveTemplateMessage.properties.propertyInfo.viewHeight)
  134. saveTemplateMessage.reloadData()
  135. saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2)
  136. editSubType = .template
  137. resetUI()
  138. delegate?.kmBatesControllerDidUpdateMode?(self)
  139. templatesController.selectedDataModel = data
  140. templatesController.reloadData()
  141. KMBatesManager.defaultManager.defaultModel = KMBatesModel()
  142. delegate?.kmBatesControllerDidModelDataUpdated?(self)
  143. }
  144. func batesPropertyControllerCancelTemplateEdit(_ controller: KMBatesPropertyController) {
  145. editSubType = .template
  146. resetUI()
  147. delegate?.kmBatesControllerDidUpdateMode?(self)
  148. templatesController.selectedDataModel = controller.batesModel
  149. templatesController.reloadData()
  150. delegate?.kmBatesControllerDidModelDataUpdated?(self)
  151. }
  152. func batesPropertyControllerFinishTemplateEdit(_ controller: KMBatesPropertyController) {
  153. editSubType = .template
  154. resetUI()
  155. delegate?.kmBatesControllerDidUpdateMode?(self)
  156. templatesController.selectedDataModel = controller.batesModel
  157. templatesController.reloadData()
  158. delegate?.kmBatesControllerDidModelDataUpdated?(self)
  159. }
  160. }
  161. //MARK: - KMHFTemplateControllerDelegate
  162. extension KMBatesController: KMBatesTemplateControllerDelegate {
  163. func templateControllerDidSelectedChanged(_ controller: KMBatesTemplateController) {
  164. updatePDFDocumentBates()
  165. delegate?.kmBatesControllerDidModelDataUpdated?(self)
  166. }
  167. func templateControllerDidAddData(_ controller: KMBatesTemplateController) {
  168. editSubType = .add
  169. resetUI()
  170. delegate?.kmBatesControllerDidUpdateMode?(self)
  171. propertyController.clearData()
  172. propertyController.batesModel = KMBatesManager.defaultManager.defaultModel
  173. propertyController.reloadData()
  174. delegate?.kmBatesControllerDidModelDataUpdated?(self)
  175. templatesController.selectedDataModel = nil
  176. templatesController.reloadData()
  177. }
  178. func templateControllerDidEditData(_ controller: KMBatesTemplateController, _ data: KMBatesModel) {
  179. editSubType = .edit
  180. resetUI()
  181. delegate?.kmBatesControllerDidUpdateMode?(self)
  182. if data.bottomLeftString.count > 0 {
  183. propertyController.positionSelRow = 0
  184. propertyController.positionSelColumn = 0
  185. } else if data.bottomCenterString.count > 0 {
  186. propertyController.positionSelRow = 0
  187. propertyController.positionSelColumn = 1
  188. } else if data.bottomRightString.count > 0 {
  189. propertyController.positionSelRow = 0
  190. propertyController.positionSelColumn = 2
  191. } else if data.topLeftString.count > 0 {
  192. propertyController.positionSelRow = 1
  193. propertyController.positionSelColumn = 0
  194. } else if data.topCenterString.count > 0 {
  195. propertyController.positionSelRow = 1
  196. propertyController.positionSelColumn = 1
  197. } else if data.topRightString.count > 0 {
  198. propertyController.positionSelRow = 1
  199. propertyController.positionSelColumn = 2
  200. }
  201. propertyController.batesModel = data
  202. propertyController.originalDataDict = KMBatesManager.defaultManager.parseModel(model: data)
  203. propertyController.reloadData()
  204. delegate?.kmBatesControllerDidModelDataUpdated?(self)
  205. templatesController.selectedDataModel = nil
  206. templatesController.reloadData()
  207. }
  208. }