123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304 |
- //
- // KMBackgroundController.swift
- // PDF Master
- //
- // Created by tangchao on 2022/12/23.
- //
- import Cocoa
- /// 背景 预览控制器
- class KMBackgroundController: KMWatermarkAdjectivePreViewBaseController {
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- let itemTitles = [[NSLocalizedString("Add Background", comment: ""), NSLocalizedString("Delete Background", comment: "")], [NSLocalizedString("Batch", comment: "")]]
- var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
- for items in itemTitles {
- var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
- for title in items {
- let model = KMWatermarkAdjectiveTopBarItemModel()
- model.iconName = ""
- model.itemTitle = title
- array.append(model)
- }
- itemModels.append(array)
- }
-
- self.topBarView.initItemData(itemArrays: itemModels)
- self.topBarView.selectTopItem(index: 0)
-
- let preView: KMWatermarkPDFView_OC = KMWatermarkPDFView_OC()
- self.preView = preView
- self.preView.frame = self.preViewBox.contentView!.bounds
- self.preView.autoresizingMask = [.width, .height]
- self.preViewBox.contentView?.addSubview(self.preView)
- self.preView.setDisplay(.singlePage)
-
- let controller = KMBackgroundPropertyHomeController()
- controller.preView = self.preView
- controller.view.frame = self.rightBox.contentView!.bounds
- controller.view.autoresizingMask = [.width, .height]
- self.right_gotoViewController(viewController: controller)
- controller.modelDidChange = { [weak self] model in
- if (model == nil) {
- self!.topBarView.isCanApply(can: false)
- } else {
- self!.topBarView.isCanApply(can: true)
- }
-
- self!.model = model
- self!.model?.pageCount = Int((self?.preView.document.pageCount)!)
- (self?.preView as! KMWatermarkPDFView_OC).model = model
- }
- }
-
- override func viewDidAppear() {
- super.viewDidAppear()
-
- (self.rightViewController as! KMBackgroundPropertyHomeController).pageCount = Int(self.preView.document.pageCount)
- }
-
- override func applyAction() {
- if (self.model == nil) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("Failure", comment: "")
- alert.runModal()
- return
- }
- self.beginLoading()
- self.addBackground(model: self.model! as! KMBackgroundModel, toPath: self.preView.document.documentURL.path) {
- result in
- DispatchQueue.main.async {
- self.preView.layoutDocumentView()
- self.preView.setNeedsDisplayForVisiblePages()
- }
- self.endLoading()
- if (result) {
- guard let callback = self.itemClick else {
- return
- }
- callback(1, nil)
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("Failure", comment: "")
- alert.runModal()
- }
- }
- }
-
- override func topItemClick(index: Int) {
- if (index == 0) { /// 添加
- return
- }
- if (index == 2) { /// 批量
- KMBatchWindowController.openFile(nil, .Background)
- return
- }
-
- /// 移除
- self.beginLoading()
- self.deleteBackground(toPath: self.preView.document.documentURL.path) {
- result in
- self.endLoading()
- if (result) {
- DispatchQueue.main.async {
- self.preView.document = CPDFDocument(url: self.preView.document.documentURL)
- }
- } else {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("Failure", comment: "")
- alert.runModal()
- }
- }
- }
-
- func gotoPropertyListController() {
- let propertyController = KMBackgroundPropertyController()
- propertyController.preView = self.preView
- propertyController.view.frame = self.rightBox.contentView!.bounds
- propertyController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
-
- self.right_gotoViewController(viewController: propertyController)
-
- propertyController.rightButtonClick = {
- (index: Int) -> () in
-
- /// 创建背景
- self.gotoCreateController()
- }
-
- propertyController.menuItemClick = {
- (itemIndex: Int, model: KMBackgroundModel) in
- self.gotoEditController(model: model)
-
- }
-
- propertyController.templateDidSelected = {
- (model: KMBackgroundModel, isSelected: Bool) in
- if (isSelected) {
- self.model = model
- self.topBarView.isCanApply(can: true)
- } else {
- self.model = nil
- self.topBarView.isCanApply(can: false)
- }
- }
- }
-
- func gotoCreateController() {
- let createController = KMCreateBackgroundController()
- createController.view.frame = self.rightBox.contentView!.bounds
- createController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- createController.preView = self.preView
-
- self.topBarView.isCanApply(can: true)
-
- self.right_gotoViewController(viewController: createController)
-
- createController.backClick = {
- self.gotoPropertyListController()
- }
-
- createController.rightButtonClick = {
- (index: Int) -> () in
- /// 保存到模板
- let templateModel = (self.rightViewController as! KMCreateBackgroundController).fetchTemplate()
-
- let result = KMBackgroundManager.defaultManager.addTemplate(model: templateModel as! KMBackgroundModel)
- if (result) {
- /// 返回模板列表
- self.gotoPropertyListController()
- let propertyConroller: KMBackgroundPropertyController = self.rightViewController as! KMBackgroundPropertyController
- if (index == 0) { /// 颜色
- propertyConroller.selectTabIndex(index: 0)
- } else if (index == 1) { /// 文件
- propertyConroller.selectTabIndex(index: 1)
- }
- }
- }
- // createController.modelDidChange = {
- // (model: KMBackgroundModel) in
-
- /// 模型已经修改,可以再这里刷新预览视图
- // self.model = model
- // }
- }
-
- func gotoEditController(model: KMBackgroundModel) {
- let editController = KMEditBackgroundController()
- editController.view.frame = self.rightBox.contentView!.bounds
- editController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- editController.editModel = model
-
- self.right_gotoViewController(viewController: editController)
-
- editController.backClick = {
- let editController: KMEditBackgroundController = self.rightViewController as! KMEditBackgroundController
- if (editController.isEdited) {
- KMBackgroundManager.defaultManager.updateTemplate(model: editController.editModel)
- }
-
- self.gotoPropertyListController()
- }
- }
-
- func addBackground(model: KMBackgroundModel, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
- DispatchQueue.global().async {
- let document: CPDFDocument = self.applyDocument!
- var property = document.background()
-
- property!.scale = model.scale
- property!.rotation = CGFloat(-model.rotation)
- property!.opacity = model.opacity
- property?.xOffset = model.horizontalSpace
- property?.yOffset = model.verticalSpace
- property?.horizontalAlignment = UInt(model.horizontalMode)
- property?.verticalAlignment = UInt(model.verticalMode)
-
- if (model.type == .color) {
- property?.color = model.color
- property?.type = .color
- } else if (model.type == .file) {
- property?.setImage(NSImage(contentsOfFile: model.imagePath))
- // property?.setImage(model.image)
- property?.type = .image
- }
- let pagesString = self.findPagesString(model)
- if (pagesString.isEmpty) {
- property?.pageString = "0-\(document.pageCount-1)"
- } else {
- property?.pageString = pagesString
- }
- property?.update()
-
- /// 保存到临时路径
- let documentPath = NSTemporaryDirectory()
- let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
- if (FileManager.default.fileExists(atPath: tempPath)) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- let result = document.write(to: URL(fileURLWithPath: tempPath))
- if (result) {
- if (FileManager.default.fileExists(atPath: toPath)) {
- try?FileManager.default.removeItem(atPath: toPath)
- }
-
- try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- DispatchQueue.main.async {
- completion(result)
- }
- }
- }
-
- func deleteBackground(toPath: String, completion: @escaping (_ result: Bool) -> ()) {
- if (self.preView.document.allowsPrinting == false || self.preView.document.allowsCopying == false) {
- let alert = NSAlert()
- alert.alertStyle = .critical
- alert.messageText = NSLocalizedString("This PDF document's user permissions does not allow modifying, content copying and printing.", comment: "")
- alert.runModal()
-
- completion(false)
- return
- }
-
- DispatchQueue.global().async {
- let document: CPDFDocument = self.preView.document
- var property = document.background()
- property?.clear()
-
- /// 保存到临时路径
- let documentPath = NSTemporaryDirectory()
- let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
- if (FileManager.default.fileExists(atPath: tempPath)) {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- let result = document.write(to: URL(fileURLWithPath: tempPath))
- if (result) {
- if (FileManager.default.fileExists(atPath: toPath)) {
- try?FileManager.default.removeItem(atPath: toPath)
- }
-
- try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
- } else {
- try?FileManager.default.removeItem(atPath: tempPath)
- }
-
- DispatchQueue.main.async {
- completion(result)
- }
- }
- }
-
- }
|