KMHeaderFooterController.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // KMHeaderFooterController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/8.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMHeaderFooterControllerDelegate: AnyObject {
  10. @objc optional func kmHeaderFooterControllerDidUpdateMode(_ view: KMHeaderFooterController)
  11. //数据发生变化时处理
  12. @objc optional func kmHeaderFooterControllerDidModelDataUpdated(_ view: KMHeaderFooterController)
  13. }
  14. class KMHeaderFooterController: 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: KMHeaderPropertyController = KMHeaderPropertyController.init()
  20. var templatesController: KMHFTemplateController = KMHFTemplateController()
  21. var saveTemplateMessage: ComponentMessage = ComponentMessage()
  22. var totalPDFCount: Int = 1
  23. var pdfDocument: CPDFDocument?
  24. var headerFooterModel: KMHeaderFooterObject? {
  25. get {
  26. if editSubType == .template {
  27. return templatesController.selectedModel
  28. } else if editSubType == .add ||
  29. editSubType == .edit {
  30. return propertyController.headerFooterModel
  31. }
  32. return nil
  33. }
  34. }
  35. weak open var delegate: KMHeaderFooterControllerDelegate?
  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. documentPreview.pdfDocument = self.pdfDocument
  73. documentPreview.reloadData()
  74. propertyController.totalPDFCount = totalPDFCount
  75. if editSubType == .template {
  76. templatesController.reloadData()
  77. } else if editSubType == .add {
  78. propertyController.editSubType = editSubType
  79. propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter
  80. propertyController.reloadData()
  81. } else if editSubType == .edit {
  82. propertyController.reloadData()
  83. }
  84. updatePDFDocumentHeaderFooter()
  85. }
  86. func updatePDFDocumentHeaderFooter() {
  87. if let data = headerFooterModel {
  88. let cPDFHeaderFooter = documentPreview.pdfDocument?.headerFooter()
  89. cPDFHeaderFooter?.pageString = "0"
  90. let fontSize = data.fontsize
  91. let fontName: String = data.fontName
  92. let font = NSFont.boldSystemFont(ofSize:fontSize)
  93. let style = NSMutableParagraphStyle()
  94. style.alignment = .center
  95. style.lineBreakMode = .byCharWrapping
  96. 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
  97. cPDFHeaderFooter?.margin = NSEdgeInsetsMake(max(CGFloat(data.topMargin)-size.height, 0), CGFloat(data.leftMargin), max(CGFloat(data.bottomMargin)-size.height, 0), CGFloat(data.rightMargin))
  98. let strings = KMHeaderFooterObject.parseModel(model: data, pageCount: totalPDFCount)
  99. var count: Int = 0
  100. let color = data.color
  101. for text in strings {
  102. cPDFHeaderFooter?.setText(text, at: UInt(count))
  103. cPDFHeaderFooter?.setTextColor(color, at: UInt(count))
  104. cPDFHeaderFooter?.setFontSize(fontSize, at: UInt(count))
  105. cPDFHeaderFooter?.setFontName(fontName, at: UInt(count))
  106. count += 1
  107. }
  108. cPDFHeaderFooter?.update()
  109. documentPreview.refreshUI()
  110. } else {
  111. pdfDocument?.headerFooter().clear()
  112. documentPreview.refreshUI()
  113. }
  114. }
  115. }
  116. //MARK: - KMHeaderPropertyControllerDelegate
  117. extension KMHeaderFooterController: KMHeaderPropertyControllerDelegate {
  118. func headerFooterPropertyControllerDidUpdate(_ controller: KMHeaderPropertyController) {
  119. updatePDFDocumentHeaderFooter()
  120. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  121. }
  122. func headerFooterPropertyControllerDidChangetoTemplate(_ controller: KMHeaderPropertyController) {
  123. editSubType = .template
  124. resetUI()
  125. delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
  126. updatePDFDocumentHeaderFooter()
  127. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  128. }
  129. func headerFooterPropertyControllerSaveTemplateSuccess(_ controller: KMHeaderPropertyController, _ data: KMHeaderFooterObject) {
  130. editSubType = .template
  131. resetUI()
  132. delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
  133. templatesController.selectedModel = data
  134. templatesController.reloadData()
  135. updatePDFDocumentHeaderFooter()
  136. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  137. }
  138. func headerFooterPropertyControllerCancelTemplateEdit(_ controller: KMHeaderPropertyController) {
  139. editSubType = .template
  140. resetUI()
  141. delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
  142. templatesController.selectedModel = controller.headerFooterModel
  143. templatesController.reloadData()
  144. updatePDFDocumentHeaderFooter()
  145. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  146. }
  147. func headerFooterPropertyControllerFinishTemplateEdit(_ controller: KMHeaderPropertyController) {
  148. editSubType = .template
  149. resetUI()
  150. delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
  151. templatesController.selectedModel = controller.headerFooterModel
  152. templatesController.reloadData()
  153. updatePDFDocumentHeaderFooter()
  154. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  155. }
  156. }
  157. //MARK: - KMHFTemplateControllerDelegate
  158. extension KMHeaderFooterController: KMHFTemplateControllerDelegate {
  159. func templateControllerDidSelectedChanged(_ controller: KMHFTemplateController) {
  160. updatePDFDocumentHeaderFooter()
  161. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  162. }
  163. func templateControllerDidAddData(_ controller: KMHFTemplateController) {
  164. editSubType = .add
  165. resetUI()
  166. delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
  167. propertyController.clearData()
  168. propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter
  169. propertyController.reloadData()
  170. updatePDFDocumentHeaderFooter()
  171. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  172. }
  173. func templateControllerDidEditData(_ controller: KMHFTemplateController, _ data: KMHeaderFooterObject) {
  174. editSubType = .edit
  175. resetUI()
  176. delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
  177. propertyController.originalDataDict = KMHeaderFooterManager.defaultManager.parseModel(model: data)
  178. propertyController.headerFooterModel = data
  179. propertyController.reloadData()
  180. updatePDFDocumentHeaderFooter()
  181. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  182. }
  183. }