KMWatermarkPropertyController.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // KMWatermarkPropertyController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/15.
  6. //
  7. import Cocoa
  8. ///
  9. typealias KMWatermarkPropertyControllerTemplateModelDidSelected = (_ model: KMWatermarkModel)->()
  10. typealias KMWatermarkPropertyControllerTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMWatermarkModel)->()
  11. class KMWatermarkPropertyController: KMWatermarkAdjectivePropertyBaseController {
  12. var textController: KMWatermarkAdjectiveListController!
  13. var fileController: KMWatermarkAdjectiveListController!
  14. var templateModelDidSelected: KMWatermarkPropertyControllerTemplateModelDidSelected!
  15. var templateMenuItemClick: KMWatermarkPropertyControllerTemplateMenuItemClick!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.showBack(show: false)
  19. self.titleLabel.stringValue = NSLocalizedString("Watermark", comment: "")
  20. self.segementControl.items = [NSLocalizedString("Text", comment: ""),NSLocalizedString("File", comment: "")]
  21. self.segementControl.itemClick = {
  22. [self] (index: Int) in
  23. self.tabView.selectTabViewItem(at: index)
  24. }
  25. var textModels: Array<KMWatermarkModel> = []
  26. var fileModels: Array<KMWatermarkModel> = []
  27. for model in KMWatermarkManager.defaultManager.watermarks {
  28. if ((model.image) != nil) { /// 文件
  29. fileModels.append(model)
  30. } else { /// 文本
  31. textModels.append(model)
  32. }
  33. }
  34. let item = NSTabViewItem(identifier: "tabID")
  35. item.label = "tab1";
  36. let controller = KMWatermarkAdjectiveListController()
  37. item.viewController = controller
  38. controller.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new watermark.", comment: "")
  39. controller.delegate = self
  40. textController = controller
  41. let item2 = NSTabViewItem(identifier: "tablID2")
  42. item2.label = "tab2";
  43. let controller2 = KMWatermarkAdjectiveListController()
  44. item2.viewController = controller2
  45. controller2.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new watermark.", comment: "")
  46. controller2.delegate = self
  47. fileController = controller2
  48. self.tabView.addTabViewItem(item)
  49. self.tabView.addTabViewItem(item2)
  50. controller.initDatas(datas: textModels)
  51. controller2.initDatas(datas: fileModels)
  52. }
  53. override func initDatas() {
  54. }
  55. override func updateDatas() {
  56. var textModels: Array<KMWatermarkModel> = []
  57. var fileModels: Array<KMWatermarkModel> = []
  58. for model in KMWatermarkManager.defaultManager.watermarks {
  59. if ((model.image) != nil) { /// 文件
  60. fileModels.append(model)
  61. } else { /// 文本
  62. textModels.append(model)
  63. }
  64. }
  65. textController.initDatas(datas: textModels)
  66. fileController.initDatas(datas: fileModels)
  67. }
  68. func addTextModel(model: KMWatermarkModel) {
  69. let templateModel = KMWatermarkTemplateModel()
  70. templateModel.initModel(model: model)
  71. textController.datas.insert(templateModel, at: 0)
  72. textController.dealSelectModel(selectedModel: templateModel)
  73. textController.refreshUI()
  74. }
  75. func addFileModel(model: KMWatermarkModel) {
  76. let templateModel = KMWatermarkTemplateModel()
  77. templateModel.initModel(model: model)
  78. fileController.datas.insert(templateModel, at: 0)
  79. fileController.dealSelectModel(selectedModel: templateModel)
  80. fileController.refreshUI()
  81. }
  82. }
  83. extension KMWatermarkPropertyController: KMWatermarkAdjectiveListControllerDelegate {
  84. func controller(controller: KMWatermarkAdjectiveListController, shouldSelectAtRow: Int) -> Bool {
  85. if self.preView != nil {
  86. let watermarks = self.preView.document.watermarks()
  87. if (watermarks == nil || watermarks!.count == 0) {
  88. return true
  89. }
  90. // 替换水印
  91. let alert = NSAlert()
  92. alert.alertStyle = .warning
  93. alert.messageText = NSLocalizedString("文件中已添加水印,您要替换现有的水印吗?", comment: "")
  94. alert.addButton(withTitle: NSLocalizedString("替换", comment: ""))
  95. alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
  96. let result = alert.runModal()
  97. if (result != .alertFirstButtonReturn) {
  98. return false
  99. }
  100. /// 替换
  101. for i in 0 ..< watermarks!.count {
  102. let model = watermarks![i]
  103. self.preView.document.removeWatermark(model)
  104. self.preView.setNeedsDisplayForVisiblePages()
  105. }
  106. }
  107. return true
  108. }
  109. func controller(controller: KMWatermarkAdjectiveListController, didSelectAtRow: Int) {
  110. if (didSelectAtRow < controller.datas.count) {
  111. let model: KMWatermarkTemplateModel = controller.datas[didSelectAtRow] as! KMWatermarkTemplateModel
  112. guard let callback = templateModelDidSelected else {
  113. return
  114. }
  115. callback(model)
  116. }
  117. }
  118. func controller(controller: KMWatermarkAdjectiveListController, shouldRemoveAtRow: Int) -> Bool {
  119. return true
  120. }
  121. func controller(controller: KMWatermarkAdjectiveListController, didRemoveAtRow: Int) {
  122. let model: KMWatermarkTemplateModel = controller.datas[didRemoveAtRow] as! KMWatermarkTemplateModel
  123. let result = KMWatermarkManager.defaultManager.removeWatermark(watermark: model)
  124. if (result) {
  125. controller.datas.remove(at: didRemoveAtRow)
  126. controller.refreshUI()
  127. }
  128. }
  129. func controller(controller: KMWatermarkAdjectiveListController, shouldEditAtRow: Int) -> Bool {
  130. return true
  131. }
  132. func controller(controller: KMWatermarkAdjectiveListController, didEditAtRow: Int) {
  133. let model: KMWatermarkTemplateModel = controller.datas[didEditAtRow] as! KMWatermarkTemplateModel
  134. guard let callback = templateMenuItemClick else {
  135. return
  136. }
  137. callback(1, model)
  138. }
  139. func controller(controller: KMWatermarkAdjectiveListController, item: NSMenuItem, itemIndex: Int) {
  140. if (itemIndex == 0) { /// 编辑
  141. let model: KMWatermarkTemplateModel = controller.datas[item.tag] as! KMWatermarkTemplateModel
  142. guard let callback = templateMenuItemClick else {
  143. return
  144. }
  145. callback(1, model)
  146. } else if (itemIndex == 1) { /// 删除
  147. if (item.tag < controller.datas.count) {
  148. let model: KMWatermarkTemplateModel = controller.datas[item.tag] as! KMWatermarkTemplateModel
  149. let result = KMWatermarkManager.defaultManager.removeWatermark(watermark: model)
  150. if (result) {
  151. controller.datas.remove(at: item.tag)
  152. controller.refreshUI()
  153. }
  154. }
  155. } else if (itemIndex == 2) { /// 删除全部
  156. let alert = NSAlert()
  157. alert.messageText = "确认删除全部模板吗?"
  158. alert.addButton(withTitle: "删除")
  159. alert.addButton(withTitle: "取消")
  160. let result = alert.runModal()
  161. if (result == .alertSecondButtonReturn) { /// 取消
  162. return
  163. }
  164. if (textController.isEqual(to: controller)) { /// 文本
  165. let result = KMWatermarkManager.defaultManager.removeAllTextWatermarks()
  166. if (result) {
  167. controller.datas.removeAll()
  168. controller.refreshUI()
  169. }
  170. } else if (fileController.isEqual(to: controller)) { /// 文件
  171. let result = KMWatermarkManager.defaultManager.removeAllFileWatermarks()
  172. if (result) {
  173. controller.datas.removeAll()
  174. controller.refreshUI()
  175. }
  176. }
  177. }
  178. }
  179. }