KMWatermarkPropertyHomeController.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // KMWatermarkPropertyHomeController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/1/5.
  6. //
  7. import Cocoa
  8. /// 水印 属性主控制器
  9. class KMWatermarkPropertyHomeController: KMWatermarkAdjectivePropertyMainController {
  10. var preView: CPDFView!
  11. override func viewDidLoad() {
  12. super.viewDidLoad()
  13. }
  14. override func gotoHomeController(_ selectedModelID: String) {
  15. let propertyController = KMWatermarkPropertyController()
  16. propertyController.preView = self.preView
  17. self.showController(propertyController)
  18. propertyController.rightButtonClick = { [weak self] index in
  19. /// 创建水印
  20. self!.gotoCreateController()
  21. }
  22. propertyController.templateModelDidSelected = { [weak self] model in
  23. let templateModel: KMWatermarkTemplateModel = model as! KMWatermarkTemplateModel
  24. if (templateModel.isSelected) {
  25. self?.model = model
  26. } else {
  27. self?.model = nil
  28. }
  29. guard let callback = self?.modelDidChange else {
  30. return
  31. }
  32. callback(self?.model)
  33. }
  34. propertyController.templateMenuItemClick = { [weak self] index, model in
  35. if (index == 1) {
  36. let propertyController: KMWatermarkPropertyController = self!.currentController as! KMWatermarkPropertyController
  37. let index: Int = propertyController.tabView.indexOfTabViewItem(propertyController.tabView.selectedTabViewItem!)
  38. self?.gotoEditController(model)
  39. let editController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
  40. editController.updateModel(model: model, type: index)
  41. }
  42. }
  43. }
  44. override func gotoCreateController() {
  45. if self.preView != nil {
  46. let watermarks = self.preView.document.watermarks()
  47. if (watermarks != nil && watermarks!.count > 0) { /// /// 替换水印
  48. let alert = NSAlert()
  49. alert.alertStyle = .warning
  50. alert.messageText = NSLocalizedString("文件中已添加水印,您要替换现有的水印吗?", comment: "")
  51. alert.addButton(withTitle: NSLocalizedString("替换", comment: ""))
  52. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  53. let result = alert.runModal()
  54. if (result != .alertFirstButtonReturn) {
  55. return
  56. }
  57. /// 替换
  58. for i in 0 ..< watermarks!.count {
  59. let model = watermarks![i]
  60. self.preView.document.removeWatermark(model)
  61. }
  62. self.preView.setNeedsDisplayForVisiblePages()
  63. }
  64. }
  65. let createController = KMWatermarkPropertyCreateController()
  66. createController.preView = preView
  67. createController.model = self.createWatermarkTemplate()
  68. createController.delegate = self
  69. self.showController(createController)
  70. createController.backClick = { [weak self] in
  71. self?.gotoHomeController("")
  72. }
  73. createController.rightButtonClick = { [weak self] index in
  74. /// 保存模块
  75. let createController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
  76. let result = KMWatermarkManager.defaultManager.addWatermark(watermark: createController.model as! KMWatermarkModel)
  77. if (result) {
  78. self?.gotoHomeController("")
  79. }
  80. }
  81. self.model = createController.model as! KMWatermarkModel
  82. guard let callback = self.modelDidChange else {
  83. return
  84. }
  85. callback(self.model!)
  86. }
  87. override func gotoEditController(_ model: KMWatermarkAdjectiveBaseModel) {
  88. let editController = KMWatermarkPropertyCreateController()
  89. editController.isEdit = true
  90. editController.preView = self.preView
  91. editController.model = model
  92. editController.flagModel = model.copy() as AnyObject
  93. editController.delegate = self
  94. self.showController(editController)
  95. editController.backClick = { [weak self] in
  96. let editController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
  97. let result = KMWatermarkManager.defaultManager.updateWatermark(watermark: editController.model as! KMWatermarkModel)
  98. if (result) {
  99. self?.gotoHomeController("")
  100. let propertyController: KMWatermarkPropertyController = self!.currentController as! KMWatermarkPropertyController
  101. propertyController.updateDatas()
  102. }
  103. }
  104. editController.rightButtonClick = { [weak self] index in
  105. let editController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
  106. editController.resume()
  107. }
  108. }
  109. private func createWatermarkTemplate() -> KMWatermarkModel {
  110. let model = KMWatermarkModel()
  111. model.textFont = .font(name: "Helvetica", size: 48)
  112. model.textColor = .color(red: 0, green: 0, blue: 0, alpha: 1.0)
  113. model.opacity = 1
  114. model.pageRangeType = .all
  115. model.scale = 0.5
  116. model.verticalMode = 1
  117. model.horizontalMode = 1
  118. model.isFront = true
  119. model.tileVerticalSpace = 6
  120. model.tileHorizontalSpace = 6
  121. model.isTilePage = false
  122. model.pagesString = ""
  123. model.isFront = true
  124. model.text = NSLocalizedString("Watermark", comment: "")
  125. model.watermarkID = KMWatermarkManager.defaultManager.fetchAvailableName()
  126. return model
  127. }
  128. }
  129. extension KMWatermarkPropertyHomeController: KMWatermarkAdjectivePropertyDelegate {
  130. func propertyInfoDidChange(model: AnyObject) {
  131. // self.model = (model as! km)
  132. self.model = model as! KMWatermarkAdjectiveBaseModel
  133. guard let callback = self.modelDidChange else {
  134. return
  135. }
  136. callback(self.model!)
  137. }
  138. }