KMBackgroundController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // KMBackgroundController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/23.
  6. //
  7. import Cocoa
  8. /// 背景 预览控制器
  9. class KMBackgroundController: KMWatermarkAdjectivePreViewBaseController {
  10. override func viewDidLoad() {
  11. super.viewDidLoad()
  12. let itemTitles = [[NSLocalizedString("Add Background", comment: ""), NSLocalizedString("Delete Background", comment: "")], [NSLocalizedString("Batch", comment: "")]]
  13. var itemModels: Array<Array<KMWatermarkAdjectiveTopBarItemModel>> = []
  14. for items in itemTitles {
  15. var array: Array<KMWatermarkAdjectiveTopBarItemModel> = []
  16. for title in items {
  17. let model = KMWatermarkAdjectiveTopBarItemModel()
  18. model.iconName = ""
  19. model.itemTitle = title
  20. array.append(model)
  21. }
  22. itemModels.append(array)
  23. }
  24. self.topBarView.initItemData(itemArrays: itemModels)
  25. self.topBarView.selectTopItem(index: 0)
  26. let preView: KMWatermarkPDFView_OC = KMWatermarkPDFView_OC()
  27. self.preView = preView
  28. self.preView.frame = self.preViewBox.contentView!.bounds
  29. self.preView.autoresizingMask = [.width, .height]
  30. self.preViewBox.contentView?.addSubview(self.preView)
  31. self.preView.setDisplay(.singlePage)
  32. let controller = KMBackgroundPropertyHomeController()
  33. controller.preView = self.preView
  34. controller.view.frame = self.rightBox.contentView!.bounds
  35. controller.view.autoresizingMask = [.width, .height]
  36. self.right_gotoViewController(viewController: controller)
  37. controller.modelDidChange = { [weak self] model in
  38. if (model == nil) {
  39. self!.topBarView.isCanApply(can: false)
  40. } else {
  41. self!.topBarView.isCanApply(can: true)
  42. }
  43. self!.model = model
  44. self!.model?.pageCount = Int((self?.preView.document.pageCount)!)
  45. (self?.preView as! KMWatermarkPDFView_OC).model = model
  46. }
  47. }
  48. override func viewDidAppear() {
  49. super.viewDidAppear()
  50. (self.rightViewController as! KMBackgroundPropertyHomeController).pageCount = Int(self.preView.document.pageCount)
  51. }
  52. override func applyAction() {
  53. if (self.model == nil) {
  54. let alert = NSAlert()
  55. alert.alertStyle = .critical
  56. alert.messageText = NSLocalizedString("Failure", comment: "")
  57. alert.runModal()
  58. return
  59. }
  60. self.beginLoading()
  61. self.addBackground(model: self.model! as! KMBackgroundModel, toPath: self.preView.document.documentURL.path) {
  62. result in
  63. DispatchQueue.main.async {
  64. self.preView.layoutDocumentView()
  65. self.preView.setNeedsDisplayForVisiblePages()
  66. }
  67. self.endLoading()
  68. if (result) {
  69. guard let callback = self.itemClick else {
  70. return
  71. }
  72. callback(1, nil)
  73. } else {
  74. let alert = NSAlert()
  75. alert.alertStyle = .critical
  76. alert.messageText = NSLocalizedString("Failure", comment: "")
  77. alert.runModal()
  78. }
  79. }
  80. }
  81. override func topItemClick(index: Int) {
  82. if (index == 0) { /// 添加
  83. return
  84. }
  85. if (index == 2) { /// 批量
  86. KMBatchWindowController.openFile(nil, .Background)
  87. return
  88. }
  89. /// 移除
  90. self.beginLoading()
  91. self.deleteBackground(toPath: self.preView.document.documentURL.path) {
  92. result in
  93. self.endLoading()
  94. if (result) {
  95. DispatchQueue.main.async {
  96. self.preView.document = CPDFDocument(url: self.preView.document.documentURL)
  97. }
  98. } else {
  99. let alert = NSAlert()
  100. alert.alertStyle = .critical
  101. alert.messageText = NSLocalizedString("Failure", comment: "")
  102. alert.runModal()
  103. }
  104. }
  105. }
  106. func gotoPropertyListController() {
  107. let propertyController = KMBackgroundPropertyController()
  108. propertyController.preView = self.preView
  109. propertyController.view.frame = self.rightBox.contentView!.bounds
  110. propertyController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  111. self.right_gotoViewController(viewController: propertyController)
  112. propertyController.rightButtonClick = {
  113. (index: Int) -> () in
  114. /// 创建背景
  115. self.gotoCreateController()
  116. }
  117. propertyController.menuItemClick = {
  118. (itemIndex: Int, model: KMBackgroundModel) in
  119. self.gotoEditController(model: model)
  120. }
  121. propertyController.templateDidSelected = {
  122. (model: KMBackgroundModel, isSelected: Bool) in
  123. if (isSelected) {
  124. self.model = model
  125. self.topBarView.isCanApply(can: true)
  126. } else {
  127. self.model = nil
  128. self.topBarView.isCanApply(can: false)
  129. }
  130. }
  131. }
  132. func gotoCreateController() {
  133. let createController = KMCreateBackgroundController()
  134. createController.view.frame = self.rightBox.contentView!.bounds
  135. createController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  136. createController.preView = self.preView
  137. self.topBarView.isCanApply(can: true)
  138. self.right_gotoViewController(viewController: createController)
  139. createController.backClick = {
  140. self.gotoPropertyListController()
  141. }
  142. createController.rightButtonClick = {
  143. (index: Int) -> () in
  144. /// 保存到模板
  145. let templateModel = (self.rightViewController as! KMCreateBackgroundController).fetchTemplate()
  146. let result = KMBackgroundManager.defaultManager.addTemplate(model: templateModel as! KMBackgroundModel)
  147. if (result) {
  148. /// 返回模板列表
  149. self.gotoPropertyListController()
  150. let propertyConroller: KMBackgroundPropertyController = self.rightViewController as! KMBackgroundPropertyController
  151. if (index == 0) { /// 颜色
  152. propertyConroller.selectTabIndex(index: 0)
  153. } else if (index == 1) { /// 文件
  154. propertyConroller.selectTabIndex(index: 1)
  155. }
  156. }
  157. }
  158. // createController.modelDidChange = {
  159. // (model: KMBackgroundModel) in
  160. /// 模型已经修改,可以再这里刷新预览视图
  161. // self.model = model
  162. // }
  163. }
  164. func gotoEditController(model: KMBackgroundModel) {
  165. let editController = KMEditBackgroundController()
  166. editController.view.frame = self.rightBox.contentView!.bounds
  167. editController.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  168. editController.editModel = model
  169. self.right_gotoViewController(viewController: editController)
  170. editController.backClick = {
  171. let editController: KMEditBackgroundController = self.rightViewController as! KMEditBackgroundController
  172. if (editController.isEdited) {
  173. KMBackgroundManager.defaultManager.updateTemplate(model: editController.editModel)
  174. }
  175. self.gotoPropertyListController()
  176. }
  177. }
  178. func addBackground(model: KMBackgroundModel, toPath: String, completion: @escaping (_ result: Bool) -> ()) {
  179. DispatchQueue.global().async {
  180. let document: CPDFDocument = self.applyDocument!
  181. var property = document.background()
  182. property!.scale = model.scale
  183. property!.rotation = CGFloat(-model.rotation)
  184. property!.opacity = model.opacity
  185. property?.xOffset = model.horizontalSpace
  186. property?.yOffset = model.verticalSpace
  187. property?.horizontalAlignment = UInt(model.horizontalMode)
  188. property?.verticalAlignment = UInt(model.verticalMode)
  189. if (model.type == .color) {
  190. property?.color = model.color
  191. property?.type = .color
  192. } else if (model.type == .file) {
  193. property?.setImage(NSImage(contentsOfFile: model.imagePath))
  194. // property?.setImage(model.image)
  195. property?.type = .image
  196. }
  197. let pagesString = self.findPagesString(model)
  198. if (pagesString.isEmpty) {
  199. property?.pageString = "0-\(document.pageCount-1)"
  200. } else {
  201. property?.pageString = pagesString
  202. }
  203. property?.update()
  204. /// 保存到临时路径
  205. let documentPath = NSTemporaryDirectory()
  206. let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
  207. if (FileManager.default.fileExists(atPath: tempPath)) {
  208. try?FileManager.default.removeItem(atPath: tempPath)
  209. }
  210. let result = document.write(to: URL(fileURLWithPath: tempPath))
  211. if (result) {
  212. if (FileManager.default.fileExists(atPath: toPath)) {
  213. try?FileManager.default.removeItem(atPath: toPath)
  214. }
  215. try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
  216. } else {
  217. try?FileManager.default.removeItem(atPath: tempPath)
  218. }
  219. DispatchQueue.main.async {
  220. completion(result)
  221. }
  222. }
  223. }
  224. func deleteBackground(toPath: String, completion: @escaping (_ result: Bool) -> ()) {
  225. if (self.preView.document.allowsPrinting == false || self.preView.document.allowsCopying == false) {
  226. let alert = NSAlert()
  227. alert.alertStyle = .critical
  228. alert.messageText = NSLocalizedString("This PDF document's user permissions does not allow modifying, content copying and printing.", comment: "")
  229. alert.runModal()
  230. completion(false)
  231. return
  232. }
  233. DispatchQueue.global().async {
  234. let document: CPDFDocument = self.preView.document
  235. var property = document.background()
  236. property?.clear()
  237. /// 保存到临时路径
  238. let documentPath = NSTemporaryDirectory()
  239. let tempPath: String = "\(documentPath)/\(toPath.lastPathComponent)"
  240. if (FileManager.default.fileExists(atPath: tempPath)) {
  241. try?FileManager.default.removeItem(atPath: tempPath)
  242. }
  243. let result = document.write(to: URL(fileURLWithPath: tempPath))
  244. if (result) {
  245. if (FileManager.default.fileExists(atPath: toPath)) {
  246. try?FileManager.default.removeItem(atPath: toPath)
  247. }
  248. try?FileManager.default.moveItem(atPath: tempPath, toPath: toPath)
  249. } else {
  250. try?FileManager.default.removeItem(atPath: tempPath)
  251. }
  252. DispatchQueue.main.async {
  253. completion(result)
  254. }
  255. }
  256. }
  257. }