KMHeaderFooterController.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 kmHeaderFooterControllerDidUpdateModeType(_ 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: KMHeaderFooterModel? {
  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. //MARK: - func
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. // Do view setup here.
  41. documentPreview.view.frame = preInfoView.bounds
  42. documentPreview.view.autoresizingMask = [.width, .height]
  43. preInfoView.addSubview(documentPreview.view)
  44. propertyController.view.frame = propertyInfoView.bounds
  45. propertyController.view.autoresizingMask = [.width, .height]
  46. propertyController.delegate = self
  47. propertyController.view.isHidden = true
  48. propertyInfoView.addSubview(propertyController.view)
  49. templatesController.view.frame = propertyInfoView.bounds
  50. templatesController.view.autoresizingMask = [.width, .height]
  51. templatesController.delegate = self
  52. templatesController.view.isHidden = true
  53. propertyInfoView.addSubview(templatesController.view)
  54. setupProperty()
  55. }
  56. func setupProperty() {
  57. saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template"))
  58. }
  59. func resetUI() {
  60. propertyController.view.isHidden = true
  61. templatesController.view.isHidden = true
  62. if editSubType == .template {
  63. templatesController.view.isHidden = false
  64. } else if editSubType == .add {
  65. propertyController.view.isHidden = false
  66. propertyController.editSubType = editSubType
  67. } else if editSubType == .edit {
  68. propertyController.view.isHidden = false
  69. propertyController.editSubType = editSubType
  70. }
  71. }
  72. func reloadData() {
  73. documentPreview.pdfDocument = self.pdfDocument
  74. documentPreview.reloadData()
  75. propertyController.totalPDFCount = totalPDFCount
  76. if editSubType == .template {
  77. templatesController.reloadData()
  78. } else if editSubType == .add {
  79. propertyController.editSubType = editSubType
  80. propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter
  81. propertyController.reloadData()
  82. } else if editSubType == .edit {
  83. propertyController.reloadData()
  84. }
  85. updatePDFDocumentHeaderFooter()
  86. }
  87. func updatePDFDocumentHeaderFooter() {
  88. if let data = headerFooterModel {
  89. let cPDFHeaderFooter = documentPreview.pdfDocument?.headerFooter()
  90. cPDFHeaderFooter?.pageString = "0"
  91. let fontSize = data.fontsize
  92. let fontName: String = data.fontName
  93. let font = NSFont.boldSystemFont(ofSize:fontSize)
  94. let style = NSMutableParagraphStyle()
  95. style.alignment = .center
  96. style.lineBreakMode = .byCharWrapping
  97. 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
  98. cPDFHeaderFooter?.margin = NSEdgeInsetsMake(max(CGFloat(data.topMargin)-size.height, 0), CGFloat(data.leftMargin), max(CGFloat(data.bottomMargin)-size.height, 0), CGFloat(data.rightMargin))
  99. let strings = KMHeaderFooterModel.parseModel(model: data, pageCount: totalPDFCount)
  100. var count: Int = 0
  101. let color = data.color
  102. for text in strings {
  103. cPDFHeaderFooter?.setText(text, at: UInt(count))
  104. cPDFHeaderFooter?.setTextColor(color, at: UInt(count))
  105. cPDFHeaderFooter?.setFontSize(fontSize, at: UInt(count))
  106. cPDFHeaderFooter?.setFontName(fontName, at: UInt(count))
  107. count += 1
  108. }
  109. cPDFHeaderFooter?.update()
  110. documentPreview.refreshUI()
  111. } else {
  112. pdfDocument?.headerFooter().clear()
  113. documentPreview.refreshUI()
  114. }
  115. }
  116. }
  117. //MARK: - KMHeaderPropertyControllerDelegate
  118. extension KMHeaderFooterController: KMHeaderPropertyControllerDelegate {
  119. func headerFooterPropertyControllerDidUpdate(_ controller: KMHeaderPropertyController) {
  120. updatePDFDocumentHeaderFooter()
  121. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  122. }
  123. func headerFooterPropertyControllerDidChangetoTemplate(_ controller: KMHeaderPropertyController) {
  124. editSubType = .template
  125. resetUI()
  126. delegate?.kmHeaderFooterControllerDidUpdateModeType?(self)
  127. templatesController.selectedModel = nil
  128. templatesController.reloadData()
  129. updatePDFDocumentHeaderFooter()
  130. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  131. }
  132. func headerFooterPropertyControllerSaveTemplateSuccess(_ controller: KMHeaderPropertyController, _ data: KMHeaderFooterModel) {
  133. editSubType = .template
  134. resetUI()
  135. delegate?.kmHeaderFooterControllerDidUpdateModeType?(self)
  136. templatesController.selectedModel = data
  137. templatesController.reloadData()
  138. updatePDFDocumentHeaderFooter()
  139. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  140. KMHeaderFooterManager.defaultManager.defaultHeaderFooter = KMHeaderFooterModel()
  141. }
  142. func headerFooterPropertyControllerCancelTemplateEdit(_ controller: KMHeaderPropertyController) {
  143. editSubType = .template
  144. resetUI()
  145. delegate?.kmHeaderFooterControllerDidUpdateModeType?(self)
  146. templatesController.selectedModel = controller.headerFooterModel
  147. templatesController.reloadData()
  148. updatePDFDocumentHeaderFooter()
  149. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  150. }
  151. func headerFooterPropertyControllerFinishTemplateEdit(_ controller: KMHeaderPropertyController) {
  152. editSubType = .template
  153. resetUI()
  154. delegate?.kmHeaderFooterControllerDidUpdateModeType?(self)
  155. templatesController.selectedModel = controller.headerFooterModel
  156. templatesController.reloadData()
  157. updatePDFDocumentHeaderFooter()
  158. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  159. }
  160. }
  161. //MARK: - KMHFTemplateControllerDelegate
  162. extension KMHeaderFooterController: KMHFTemplateControllerDelegate {
  163. func templateControllerDidSelectedChanged(_ controller: KMHFTemplateController) {
  164. updatePDFDocumentHeaderFooter()
  165. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  166. }
  167. func templateControllerDidAddData(_ controller: KMHFTemplateController) {
  168. editSubType = .add
  169. resetUI()
  170. delegate?.kmHeaderFooterControllerDidUpdateModeType?(self)
  171. propertyController.clearData()
  172. propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter
  173. propertyController.totalPDFCount = self.totalPDFCount
  174. propertyController.reloadData()
  175. updatePDFDocumentHeaderFooter()
  176. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  177. templatesController.selectedModel = nil
  178. templatesController.reloadData()
  179. }
  180. func templateControllerDidEditData(_ controller: KMHFTemplateController, _ data: KMHeaderFooterModel) {
  181. editSubType = .edit
  182. resetUI()
  183. delegate?.kmHeaderFooterControllerDidUpdateModeType?(self)
  184. propertyController.originalDataDict = KMHeaderFooterManager.defaultManager.parseModel(model: data)
  185. propertyController.headerFooterModel = data
  186. propertyController.reloadData()
  187. updatePDFDocumentHeaderFooter()
  188. delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
  189. templatesController.selectedModel = nil
  190. templatesController.reloadData()
  191. }
  192. }