123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- //
- // KMWatermarkPropertyController.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/15.
- //
- import Cocoa
- ///
- typealias KMWatermarkPropertyControllerTemplateModelDidSelected = (_ model: KMWatermarkModel)->()
- typealias KMWatermarkPropertyControllerTemplateMenuItemClick = (_ itemIndex: Int, _ model: KMWatermarkModel)->()
- class KMWatermarkPropertyController: KMWatermarkAdjectivePropertyBaseController {
-
- var textController: KMWatermarkAdjectiveListController!
- var fileController: KMWatermarkAdjectiveListController!
-
- var templateModelDidSelected: KMWatermarkPropertyControllerTemplateModelDidSelected!
- var templateMenuItemClick: KMWatermarkPropertyControllerTemplateMenuItemClick!
- override func viewDidLoad() {
- super.viewDidLoad()
-
- self.showBack(show: false)
- self.titleLabel.stringValue = NSLocalizedString("Watermark", comment: "")
- self.segementControl.items = [NSLocalizedString("Text", comment: ""),NSLocalizedString("File", comment: "")]
- self.segementControl.itemClick = {
- [self] (index: Int) in
- self.tabView.selectTabViewItem(at: index)
- }
-
- var textModels: Array<KMWatermarkModel> = []
- var fileModels: Array<KMWatermarkModel> = []
- for model in KMWatermarkManager.defaultManager.watermarks {
- if ((model.image) != nil) { /// 文件
- fileModels.append(model)
- } else { /// 文本
- textModels.append(model)
- }
- }
-
- let item = NSTabViewItem(identifier: "tabID")
- item.label = "tab1";
- let controller = KMWatermarkAdjectiveListController()
- item.viewController = controller
- controller.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new watermark.", comment: "")
- controller.delegate = self
- textController = controller
-
- let item2 = NSTabViewItem(identifier: "tablID2")
- item2.label = "tab2";
- let controller2 = KMWatermarkAdjectiveListController()
- item2.viewController = controller2
- controller2.emptyView.despLabel.stringValue = NSLocalizedString("Click the Add button in the upper right corner to create a new watermark.", comment: "")
- controller2.delegate = self
- fileController = controller2
-
- self.tabView.addTabViewItem(item)
- self.tabView.addTabViewItem(item2)
-
- controller.initDatas(datas: textModels)
- controller2.initDatas(datas: fileModels)
- }
-
- override func initDatas() {
-
- }
-
- override func updateDatas() {
- var textModels: Array<KMWatermarkModel> = []
- var fileModels: Array<KMWatermarkModel> = []
- for model in KMWatermarkManager.defaultManager.watermarks {
- if ((model.image) != nil) { /// 文件
- fileModels.append(model)
- } else { /// 文本
- textModels.append(model)
- }
- }
-
- textController.initDatas(datas: textModels)
- fileController.initDatas(datas: fileModels)
- }
-
- func addTextModel(model: KMWatermarkModel) {
- let templateModel = KMWatermarkTemplateModel()
- templateModel.initModel(model: model)
-
- textController.datas.insert(templateModel, at: 0)
- textController.dealSelectModel(selectedModel: templateModel)
- textController.refreshUI()
- }
-
- func addFileModel(model: KMWatermarkModel) {
- let templateModel = KMWatermarkTemplateModel()
- templateModel.initModel(model: model)
-
- fileController.datas.insert(templateModel, at: 0)
- fileController.dealSelectModel(selectedModel: templateModel)
- fileController.refreshUI()
- }
- }
- extension KMWatermarkPropertyController: KMWatermarkAdjectiveListControllerDelegate {
- func controller(controller: KMWatermarkAdjectiveListController, shouldSelectAtRow: Int) -> Bool {
- if self.preView != nil {
- let watermarks = self.preView.document.watermarks()
- if (watermarks == nil || watermarks!.count == 0) {
- return true
- }
-
- // 替换水印
- let alert = NSAlert()
- alert.alertStyle = .warning
- alert.messageText = NSLocalizedString("文件中已添加水印,您要替换现有的水印吗?", comment: "")
- alert.addButton(withTitle: NSLocalizedString("替换", comment: ""))
- alert.addButton(withTitle: NSLocalizedString("Cancel", comment: ""))
- let result = alert.runModal()
- if (result != .alertFirstButtonReturn) {
- return false
- }
-
- /// 替换
- for i in 0 ..< watermarks!.count {
- let model = watermarks![i]
- self.preView.document.removeWatermark(model)
- self.preView.setNeedsDisplayForVisiblePages()
- }
- }
- return true
- }
- func controller(controller: KMWatermarkAdjectiveListController, didSelectAtRow: Int) {
- if (didSelectAtRow < controller.datas.count) {
- let model: KMWatermarkTemplateModel = controller.datas[didSelectAtRow] as! KMWatermarkTemplateModel
-
- guard let callback = templateModelDidSelected else {
- return
- }
-
- callback(model)
- }
- }
-
- func controller(controller: KMWatermarkAdjectiveListController, shouldRemoveAtRow: Int) -> Bool {
- return true
- }
-
- func controller(controller: KMWatermarkAdjectiveListController, didRemoveAtRow: Int) {
- let model: KMWatermarkTemplateModel = controller.datas[didRemoveAtRow] as! KMWatermarkTemplateModel
-
- let result = KMWatermarkManager.defaultManager.removeWatermark(watermark: model)
- if (result) {
- controller.datas.remove(at: didRemoveAtRow)
- controller.refreshUI()
- }
- }
-
- func controller(controller: KMWatermarkAdjectiveListController, shouldEditAtRow: Int) -> Bool {
- return true
- }
-
- func controller(controller: KMWatermarkAdjectiveListController, didEditAtRow: Int) {
- let model: KMWatermarkTemplateModel = controller.datas[didEditAtRow] as! KMWatermarkTemplateModel
-
- guard let callback = templateMenuItemClick else {
- return
- }
- callback(1, model)
- }
-
- func controller(controller: KMWatermarkAdjectiveListController, item: NSMenuItem, itemIndex: Int) {
- if (itemIndex == 0) { /// 编辑
- let model: KMWatermarkTemplateModel = controller.datas[item.tag] as! KMWatermarkTemplateModel
-
- guard let callback = templateMenuItemClick else {
- return
- }
- callback(1, model)
-
- } else if (itemIndex == 1) { /// 删除
- if (item.tag < controller.datas.count) {
- let model: KMWatermarkTemplateModel = controller.datas[item.tag] as! KMWatermarkTemplateModel
-
- let result = KMWatermarkManager.defaultManager.removeWatermark(watermark: model)
- if (result) {
- controller.datas.remove(at: item.tag)
- controller.refreshUI()
- }
- }
- } else if (itemIndex == 2) { /// 删除全部
- let alert = NSAlert()
- alert.messageText = "确认删除全部模板吗?"
- alert.addButton(withTitle: "删除")
- alert.addButton(withTitle: "取消")
- let result = alert.runModal()
- if (result == .alertSecondButtonReturn) { /// 取消
- return
- }
- if (textController.isEqual(to: controller)) { /// 文本
- let result = KMWatermarkManager.defaultManager.removeAllTextWatermarks()
- if (result) {
- controller.datas.removeAll()
- controller.refreshUI()
- }
- } else if (fileController.isEqual(to: controller)) { /// 文件
- let result = KMWatermarkManager.defaultManager.removeAllFileWatermarks()
- if (result) {
- controller.datas.removeAll()
- controller.refreshUI()
- }
- }
- }
- }
- }
|