123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- //
- // KMWatermarkPropertyHomeController.swift
- // PDF Master
- //
- // Created by tangchao on 2023/1/5.
- //
- import Cocoa
- /// 水印 属性主控制器
- class KMWatermarkPropertyHomeController: KMWatermarkAdjectivePropertyMainController {
- var preView: CPDFView!
-
- override func viewDidLoad() {
- super.viewDidLoad()
- }
-
- override func gotoHomeController(_ selectedModelID: String) {
- let propertyController = KMWatermarkPropertyController()
- propertyController.preView = self.preView
- self.showController(propertyController)
-
- propertyController.rightButtonClick = { [weak self] index in
- /// 创建水印
- self!.gotoCreateController()
- }
- propertyController.templateModelDidSelected = { [weak self] model in
- let templateModel: KMWatermarkTemplateModel = model as! KMWatermarkTemplateModel
- if (templateModel.isSelected) {
- self?.model = model
- } else {
- self?.model = nil
- }
-
- guard let callback = self?.modelDidChange else {
- return
- }
- callback(self?.model)
- }
-
- propertyController.templateMenuItemClick = { [weak self] index, model in
- if (index == 1) {
- let propertyController: KMWatermarkPropertyController = self!.currentController as! KMWatermarkPropertyController
- let index: Int = propertyController.tabView.indexOfTabViewItem(propertyController.tabView.selectedTabViewItem!)
-
- self?.gotoEditController(model)
- let editController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
- editController.updateModel(model: model, type: index)
- }
- }
- }
-
- override func gotoCreateController() {
- if self.preView != nil {
- let watermarks = self.preView.document.watermarks()
- if (watermarks != nil && watermarks!.count > 0) { /// /// 替换水印
- 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
- }
- /// 替换
- for i in 0 ..< watermarks!.count {
- let model = watermarks![i]
- self.preView.document.removeWatermark(model)
- }
- self.preView.setNeedsDisplayForVisiblePages()
- }
- }
-
- let createController = KMWatermarkPropertyCreateController()
- createController.preView = preView
- createController.model = self.createWatermarkTemplate()
- createController.delegate = self
- self.showController(createController)
-
- createController.backClick = { [weak self] in
- self?.gotoHomeController("")
- }
- createController.rightButtonClick = { [weak self] index in
- /// 保存模块
- let createController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
- let result = KMWatermarkManager.defaultManager.addWatermark(watermark: createController.model as! KMWatermarkModel)
-
- if (result) {
- self?.gotoHomeController("")
- }
- }
-
- self.model = createController.model as! KMWatermarkModel
-
- guard let callback = self.modelDidChange else {
- return
- }
- callback(self.model!)
-
- }
-
- override func gotoEditController(_ model: KMWatermarkAdjectiveBaseModel) {
- let editController = KMWatermarkPropertyCreateController()
- editController.isEdit = true
- editController.preView = self.preView
- editController.model = model
- editController.flagModel = model.copy() as AnyObject
- editController.delegate = self
-
- self.showController(editController)
-
- editController.backClick = { [weak self] in
- let editController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
- let result = KMWatermarkManager.defaultManager.updateWatermark(watermark: editController.model as! KMWatermarkModel)
- if (result) {
- self?.gotoHomeController("")
-
- let propertyController: KMWatermarkPropertyController = self!.currentController as! KMWatermarkPropertyController
- propertyController.updateDatas()
- }
- }
- editController.rightButtonClick = { [weak self] index in
- let editController: KMWatermarkPropertyCreateController = self!.currentController as! KMWatermarkPropertyCreateController
- editController.resume()
- }
- }
-
- private func createWatermarkTemplate() -> KMWatermarkModel {
- let model = KMWatermarkModel()
-
- model.textFont = .font(name: "Helvetica", size: 48)
- model.textColor = .color(red: 0, green: 0, blue: 0, alpha: 1.0)
- model.opacity = 1
- model.pageRangeType = 0
- model.scale = 0.5
- model.verticalMode = 1
- model.horizontalMode = 1
- model.isFront = true
- model.tileVerticalSpace = 6
- model.tileHorizontalSpace = 6
- model.isTilePage = false
- model.pagesString = ""
- model.isFront = true
- model.text = NSLocalizedString("Watermark", comment: "")
- model.watermarkID = KMWatermarkManager.defaultManager.fetchAvailableName()
-
- return model
- }
-
- }
- extension KMWatermarkPropertyHomeController: KMWatermarkAdjectivePropertyDelegate {
- func propertyInfoDidChange(model: AnyObject) {
- // self.model = (model as! km)
- self.model = model as! KMWatermarkAdjectiveBaseModel
- guard let callback = self.modelDidChange else {
- return
- }
- callback(self.model!)
- }
- }
|