KMBackgroundController.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //
  2. // KMBackgroundController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/11/7.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. @objc protocol KMBackgroundControllerDelegate: AnyObject {
  10. @objc optional func kmBackgroundControllerDidUpdateMode(_ view: KMBackgroundController)
  11. //数据发生变化时处理
  12. @objc optional func kmBackgroundControllerDidWatermarkUpdated(_ view: KMBackgroundController)
  13. }
  14. class KMBackgroundController: 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: KMBackgroundPropertyController = KMBackgroundPropertyController.init()
  20. var templatesController: KMBGTemplateController = KMBGTemplateController()
  21. var saveTemplateMessage: ComponentMessage = ComponentMessage()
  22. var pdfDocument: CPDFDocument?
  23. var backgroundModel: KMBackgroundModel?
  24. var editSubType: KMPDFEditSubModeType = .none
  25. weak open var delegate: KMBackgroundControllerDelegate?
  26. override func viewDidLoad() {
  27. super.viewDidLoad()
  28. // Do view setup here.
  29. documentPreview.view.frame = preInfoView.bounds
  30. documentPreview.view.autoresizingMask = [.width, .height]
  31. preInfoView.addSubview(documentPreview.view)
  32. propertyController.view.frame = propertyInfoView.bounds
  33. propertyController.view.autoresizingMask = [.width, .height]
  34. propertyController.delegate = self
  35. propertyController.view.isHidden = true
  36. propertyInfoView.addSubview(propertyController.view)
  37. templatesController.view.frame = propertyInfoView.bounds
  38. templatesController.view.autoresizingMask = [.width, .height]
  39. templatesController.delegate = self
  40. templatesController.view.isHidden = true
  41. propertyInfoView.addSubview(templatesController.view)
  42. setupProperty()
  43. }
  44. func setupProperty() {
  45. saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template"))
  46. }
  47. func resetUI() {
  48. propertyController.view.isHidden = true
  49. templatesController.view.isHidden = true
  50. if editSubType == .template {
  51. templatesController.view.isHidden = false
  52. } else if editSubType == .add {
  53. propertyController.view.isHidden = false
  54. propertyController.editSubType = editSubType
  55. } else if editSubType == .edit {
  56. propertyController.view.isHidden = false
  57. propertyController.editSubType = editSubType
  58. }
  59. }
  60. func reloadData() {
  61. if editSubType == .template {
  62. templatesController.reloadData()
  63. backgroundModel = templatesController.selectedBackground
  64. } else if editSubType == .add {
  65. propertyController.editSubType = editSubType
  66. propertyController.backgroundData = KMBackgroundManager.defaultManager.defaultModel
  67. propertyController.reloadData()
  68. backgroundModel = propertyController.backgroundData
  69. } else if editSubType == .edit {
  70. propertyController.editSubType = editSubType
  71. propertyController.reloadData()
  72. backgroundModel = propertyController.backgroundData
  73. }
  74. documentPreview.pdfDocument = pdfDocument
  75. documentPreview.reloadData()
  76. updatePDFDocumentBackground()
  77. delegate?.kmBackgroundControllerDidWatermarkUpdated?(self)
  78. }
  79. func updatePDFDocumentBackground() {
  80. if let data = backgroundModel {
  81. let cPDFBackground = documentPreview.pdfDocument?.background()
  82. cPDFBackground?.type = data.type
  83. if cPDFBackground?.type == .color {
  84. cPDFBackground?.color = data.color
  85. } else if cPDFBackground?.type == .image {
  86. if data.imagePath != nil {
  87. cPDFBackground?.setImage(data.image())
  88. } else {
  89. cPDFBackground?.setImage(nil)
  90. }
  91. }
  92. cPDFBackground?.scale = data.scale
  93. cPDFBackground?.rotation = data.rotation
  94. cPDFBackground?.opacity = data.opacity
  95. cPDFBackground?.verticalAlignment = UInt(data.verticalMode)
  96. cPDFBackground?.horizontalAlignment = UInt(data.horizontalMode)
  97. cPDFBackground?.xOffset = data.horizontalSpace
  98. cPDFBackground?.yOffset = data.verticalSpace
  99. cPDFBackground?.isAllowsView = true
  100. cPDFBackground?.pageString = "0"
  101. cPDFBackground?.update()
  102. self.documentPreview.refreshUI()
  103. } else {
  104. pdfDocument?.background().clear()
  105. documentPreview.refreshUI()
  106. }
  107. }
  108. override func mouseUp(with event: NSEvent) {
  109. super.mouseUp(with: event)
  110. }
  111. }
  112. //MARK: - KMBackgroundPropertyControllerDelegate
  113. extension KMBackgroundController: KMBackgroundPropertyControllerDelegate {
  114. func backgroundPropertyControllerDidUpdate(_ controller: KMBackgroundPropertyController) {
  115. backgroundModel = controller.backgroundData
  116. updatePDFDocumentBackground()
  117. delegate?.kmBackgroundControllerDidWatermarkUpdated?(self)
  118. }
  119. func backgroundPropertyControllerDidChangetoTemplate(_ controller: KMBackgroundPropertyController) {
  120. editSubType = .template
  121. resetUI()
  122. delegate?.kmBackgroundControllerDidUpdateMode?(self)
  123. backgroundModel = templatesController.selectedBackground
  124. reloadData()
  125. }
  126. func backgroundPropertyControllerSaveTemplateSuccess(_ controller: KMBackgroundPropertyController, _ data: KMBackgroundModel) {
  127. saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2,
  128. CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8,
  129. saveTemplateMessage.properties.propertyInfo.viewWidth,
  130. saveTemplateMessage.properties.propertyInfo.viewHeight)
  131. saveTemplateMessage.reloadData()
  132. saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2)
  133. editSubType = .template
  134. resetUI()
  135. delegate?.kmBackgroundControllerDidUpdateMode?(self)
  136. templatesController.selectedBackground = data
  137. reloadData()
  138. }
  139. func backgroundPropertyControllerCancelTemplateEdit(_ controller: KMBackgroundPropertyController) {
  140. editSubType = .template
  141. resetUI()
  142. delegate?.kmBackgroundControllerDidUpdateMode?(self)
  143. templatesController.selectedBackground = controller.backgroundData
  144. reloadData()
  145. }
  146. func backgroundPropertyControllerFinishTemplateEdit(_ controller: KMBackgroundPropertyController) {
  147. editSubType = .template
  148. resetUI()
  149. delegate?.kmBackgroundControllerDidUpdateMode?(self)
  150. templatesController.selectedBackground = controller.backgroundData
  151. reloadData()
  152. }
  153. }
  154. //MARK: - KMBGTemplateControllerDelegate
  155. extension KMBackgroundController: KMBGTemplateControllerDelegate {
  156. func templateControllerDidSelectedChanged(_ controller: KMBGTemplateController) {
  157. backgroundModel = controller.selectedBackground
  158. updatePDFDocumentBackground()
  159. delegate?.kmBackgroundControllerDidWatermarkUpdated?(self)
  160. }
  161. func templateControllerDidAddData(_ controller: KMBGTemplateController) {
  162. editSubType = .add
  163. resetUI()
  164. delegate?.kmBackgroundControllerDidUpdateMode?(self)
  165. propertyController.backgroundData = KMBackgroundManager.defaultManager.defaultModel
  166. backgroundModel = propertyController.backgroundData
  167. reloadData()
  168. documentPreview.layoutDocumentView()
  169. }
  170. func templateControllerDidEditData(_ controller: KMBGTemplateController, _ data: KMBackgroundModel) {
  171. editSubType = .edit
  172. resetUI()
  173. delegate?.kmBackgroundControllerDidUpdateMode?(self)
  174. propertyController.originalDataDict = KMBackgroundManager.defaultManager.parseModelToDict(model: data)
  175. propertyController.backgroundData = data
  176. propertyController.reloadData()
  177. backgroundModel = propertyController.backgroundData
  178. documentPreview.layoutDocumentView()
  179. }
  180. }