KMBatchOperateAddWatermarkViewController.swift 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. //
  2. // KMBatchOperateAddWatermarkViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/3.
  6. //
  7. import Cocoa
  8. enum KMWatermarkType: Int {
  9. case txt = 0
  10. case file
  11. }
  12. class KMCollectionViewFlowLayout: NSCollectionViewFlowLayout {
  13. override func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
  14. self.collectionView?.reloadSections(IndexSet(integer: 0))
  15. return true
  16. }
  17. }
  18. class KMBatchOperateAddWatermarkViewController: KMBatchOperateBaseViewController {
  19. var isBackground = false
  20. var onlyManagerTemplate = false
  21. weak var pdfView: CPDFView?
  22. var isBatchOperation = false //是否在批量界面
  23. @IBOutlet var topBaseView: NSView!
  24. @IBOutlet var titleLabel: NSTextField!
  25. @IBOutlet var addButton: NSButton!
  26. @IBOutlet var textButton: NSButton!
  27. @IBOutlet var fileButton: NSButton!
  28. @IBOutlet var textBox: NSBox!
  29. @IBOutlet var fileBox: NSBox!
  30. @IBOutlet var actionButton: NSButton!
  31. @IBOutlet var collectionView: NSCollectionView!
  32. @IBOutlet var bottomBaseView: NSView!
  33. @IBOutlet var managerTemplateTitleLabel: NSTextField!
  34. @IBOutlet var blankView: KMBlankView!
  35. @IBOutlet var managerTemplateButtonTopConstraint: NSLayoutConstraint!
  36. @IBOutlet var managerTemplateButtonHeightConstraint: NSLayoutConstraint!
  37. @IBOutlet var topHeightConstraint: NSLayoutConstraint!
  38. @IBOutlet var addButtonHeightConstraint: NSLayoutConstraint!
  39. @IBOutlet var addButtonBottomConstraint: NSLayoutConstraint!
  40. private var _textTemplateArray: [NSObject]?
  41. private var _fileTemplateArray: [NSObject]?
  42. private var _currentTextData: KMWatermarkModel?
  43. private var _currentFileData: KMWatermarkModel?
  44. private var _currentColorData: KMBackgroundModel?
  45. private var _currentImageData: KMBackgroundModel?
  46. private var _currentBackgroundType: KMBackgroundType = .color
  47. var currentBackgroundType: KMBackgroundType {
  48. get {
  49. return self._currentBackgroundType
  50. }
  51. set {
  52. if (self._currentBackgroundType != newValue) {
  53. self._currentBackgroundType = newValue
  54. if (self._currentBackgroundType == .color) {
  55. self.textBox.fillColor = KMAppearance.Interactive.a0Color()
  56. self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
  57. self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
  58. self.fileButton.setTitleColor(KMAppearance.Layout.h1Color())
  59. } else {
  60. self.textBox.fillColor = KMAppearance.Layout.l_1Color()
  61. self.fileBox.fillColor = KMAppearance.Interactive.a0Color()
  62. self.fileButton.setTitleColor(KMAppearance.Layout.w0Color())
  63. self.textButton.setTitleColor(KMAppearance.Layout.h1Color())
  64. }
  65. self.collectionView.reloadData()
  66. }
  67. }
  68. }
  69. private var _currentType: KMWatermarkType = .txt
  70. var currentType: KMWatermarkType {
  71. set {
  72. if self.currentType != newValue {
  73. self._currentType = newValue
  74. if (currentType == .txt) {
  75. self.textBox.fillColor = KMAppearance.Interactive.a0Color()
  76. self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
  77. self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
  78. self.fileButton.setTitleColor(KMAppearance.Layout.h1Color())
  79. } else {
  80. self.textBox.fillColor = KMAppearance.Layout.l_1Color()
  81. self.fileBox.fillColor = KMAppearance.Interactive.a0Color()
  82. self.fileButton.setTitleColor(KMAppearance.Layout.w0Color())
  83. self.textButton.setTitleColor(KMAppearance.Layout.h1Color())
  84. }
  85. self.collectionView.reloadData()
  86. }
  87. }
  88. get {
  89. return self._currentType
  90. }
  91. }
  92. private var _haveFiles = false
  93. deinit {
  94. KMPrint("KMBatchOperateAddWatermarkViewController deinit.")
  95. NotificationCenter.default.removeObserver(self)
  96. }
  97. override func viewDidLoad() {
  98. super.viewDidLoad()
  99. self._localizedlanguage()
  100. self._configuiUI()
  101. self._loadData()
  102. self.interfaceStatus = .PrepareProcess
  103. NotificationCenter.default.addObserver(self, selector: #selector(_watermarksNotification), name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: nil)
  104. self.collectionView.reloadData()
  105. }
  106. func watermarkInterfaceSelectWatermark(_ watermark: KMWatermarkModel) {
  107. if(watermark.image != nil) {
  108. self._currentFileData = watermark
  109. }else {
  110. self._currentTextData = watermark
  111. }
  112. self._loadData()
  113. self.currentType = watermark.image != nil ? .file : .txt
  114. let arr = self.currentType == .file ? self._fileTemplateArray : self._textTemplateArray
  115. let index = arr?.firstIndex(of: watermark)
  116. if (index != NSNotFound) {
  117. let indexpath = IndexPath(item: index!, section: 0)
  118. var set = Set<IndexPath>()
  119. set.insert(indexpath)
  120. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  121. }
  122. }
  123. func backgroundInterfaceSelectBackGround(_ background: KMBackgroundModel) {
  124. if(background.type == .file) {
  125. self._currentImageData = background
  126. }else {
  127. self._currentColorData = background
  128. }
  129. self._loadData()
  130. self.currentBackgroundType = background.type
  131. let arr = self.currentBackgroundType == .file ? self._fileTemplateArray : self._textTemplateArray
  132. let index = arr?.firstIndex(of: background)
  133. if (index != NSNotFound) {
  134. let indexpath = IndexPath(item: index!, section: 0)
  135. var set = Set<IndexPath>()
  136. set.insert(indexpath)
  137. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  138. }
  139. }
  140. override var interfaceStatus: KMBatchOperateInterfaceStatus? {
  141. didSet {
  142. if (self.interfaceStatus == .PrepareProcess) {
  143. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  144. var files: [URL] = []
  145. for url in self.successFilePathURLArray ?? [] {
  146. if FileManager.default.fileExists(atPath: url.path) {
  147. files.append(url)
  148. }
  149. }
  150. if (files.count > 0) {
  151. let workspace = NSWorkspace.shared
  152. workspace.activateFileViewerSelecting(files)
  153. }
  154. }
  155. self.collectionView.isSelectable = true
  156. self.actionButton.tag = 1
  157. self.actionButton.title = KMLocalizedString("Apply", nil)
  158. self.addButton.isEnabled = true
  159. } else {
  160. self.collectionView.isSelectable = false
  161. self.addButton.isEnabled = false
  162. self.actionButton.tag = 0
  163. self.actionButton.title = KMLocalizedString("Cancel", nil);
  164. }
  165. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  166. }
  167. }
  168. //button Actions,切换水印类型
  169. @IBAction func buttonClicked_SwitchWaterMarkType(_ sender: NSButton) {
  170. if (self.isBackground) {
  171. self.currentBackgroundType = KMBackgroundType(rawValue: sender.tag) ?? .color
  172. } else {
  173. self.currentType = KMWatermarkType(rawValue: sender.tag) ?? .txt
  174. }
  175. self._updateActionButtonbackgroundColor()
  176. }
  177. @IBAction func buttonClicked_addWaterMark(_ sender: NSButton) {
  178. if (!self.onlyManagerTemplate) {
  179. if (!self._haveFiles) {
  180. return
  181. }
  182. }
  183. self.view.window?.makeFirstResponder(nil)
  184. if (self.onlyManagerTemplate) {
  185. if (sender.tag == 1) {
  186. var watermark: KMWatermarkModel?
  187. var background: KMBackgroundModel?
  188. let indexSet = self._isSelectIndex()
  189. if (!indexSet) {
  190. return
  191. } else {
  192. if (!self.isBackground) {
  193. if (self.currentType == .txt) {
  194. watermark = self._currentTextData
  195. } else {
  196. watermark = self._currentFileData
  197. }
  198. } else {
  199. if (self.currentBackgroundType == .color) {
  200. background = self._currentColorData
  201. } else {
  202. background = self._currentImageData
  203. }
  204. }
  205. }
  206. let openPanel = NSOpenPanel()
  207. openPanel.canChooseFiles = false
  208. openPanel.canChooseDirectories = true
  209. openPanel.canCreateDirectories = true
  210. openPanel.beginSheetModal(for: self.view.window!) { result in
  211. if (result == .OK) {
  212. for fileURL in openPanel.urls {
  213. self.hiddenWindowCloseButtonIfNeeded()
  214. self.successFilePathURLArray?.removeAll()
  215. if (!self.isBackground) {
  216. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddWatermark)
  217. file.addWatermarkInfo.savePath = fileURL.path
  218. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
  219. file.addWatermarkInfo.pageRangeString = watermark?.pagesString ?? ""
  220. let op = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark!)
  221. op.delegate = self
  222. self.queue?.addOperation(op)
  223. } else {
  224. let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddBackground)
  225. file.addBackgroundInfo.savePath = fileURL.path
  226. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
  227. file.addBackgroundInfo.pageRangeString = background?.pagesString ?? ""
  228. let op = KMBatchAddBackgroundOperation(file: file, backgroundM: background!)
  229. op.delegate = self
  230. self.queue?.addOperation(op)
  231. }
  232. if let cnt = self.queue?.operations.count, cnt > 0 {
  233. self.interfaceStatus = .Processing
  234. }
  235. }
  236. }
  237. }
  238. } else if (sender.tag == 0) {
  239. if let cnt = self.queue?.operations.count, cnt > 0 {
  240. self.queue?.cancelAllOperations()
  241. }
  242. self.interfaceStatus = .PrepareProcess
  243. }
  244. } else {
  245. //点击开始
  246. if (sender.tag == 1) {
  247. if (!self._checkAndResetTask()) {
  248. return;
  249. }
  250. var watermark: KMWatermarkModel?
  251. var background: KMBackgroundModel?
  252. let indexSet = self._isSelectIndex()
  253. if (!indexSet || self.files!.count < 1) {
  254. return;
  255. } else {
  256. if (!self.isBackground) {
  257. if (self.currentType == .txt) {
  258. watermark = self._currentTextData
  259. } else {
  260. watermark = self._currentFileData
  261. }
  262. } else {
  263. if (self.currentBackgroundType == .color) {
  264. background = self._currentColorData
  265. } else {
  266. background = self._currentImageData
  267. }
  268. }
  269. }
  270. if (self.isBackground) {
  271. self._choosePathAndBeginOperation(background)
  272. } else {
  273. self._choosePathAndBeginOperation(watermark)
  274. }
  275. } else if (sender.tag == 0) {
  276. if let cnt = self.queue?.operations.count, cnt > 0 {
  277. self.queue?.cancelAllOperations()
  278. }
  279. self.interfaceStatus = .PrepareProcess
  280. }
  281. }
  282. }
  283. @IBAction func buttonClicked_addtemplate(_ sender: NSButton) {
  284. let filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  285. var cdocument = self.pdfView?.document
  286. if isBatchOperation {
  287. cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  288. }
  289. if isBackground {
  290. let controller = KMBackgroundWindowController(windowNibName: "KMBackgroundWindowController")
  291. controller.isBatch = isBatchOperation
  292. controller.type = .add
  293. controller.currentType = self.currentBackgroundType.rawValue
  294. controller.pdfDocument = cdocument
  295. controller.cancelAction = { [weak self] controller in
  296. self?.km_endSheet()
  297. }
  298. self.km_beginSheet(windowC: controller)
  299. controller.operateCallBack = { controller, background, countType in
  300. self.currentBackgroundType = KMBackgroundType(rawValue: countType) ?? .color
  301. self._loadData()
  302. var haveBackgrounds = false
  303. if self.currentBackgroundType == .color {
  304. self._currentColorData = background
  305. if self._textTemplateArray?.count ?? 0 > 0 {
  306. haveBackgrounds = true
  307. }
  308. } else if self.currentBackgroundType == .file {
  309. self._currentImageData = background
  310. if self._fileTemplateArray?.count ?? 0 > 0 {
  311. haveBackgrounds = true
  312. }
  313. }
  314. if haveBackgrounds {
  315. let indexPath = IndexPath(item: 0, section: 0)
  316. var set = Set<IndexPath>()
  317. set.insert(indexPath)
  318. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  319. }
  320. self._postNotification()
  321. }
  322. }else{
  323. let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
  324. controller.isBatch = isBatchOperation
  325. controller.type = .add
  326. controller.pdfDocument = cdocument
  327. controller.currentType = self.currentType.rawValue
  328. controller.cancelAction = { [weak self] wmWindowC in
  329. self?.km_endSheet()
  330. }
  331. controller.operateCallBack = { [unowned self] controller, watermark, countType in
  332. self.currentType = KMWatermarkType(rawValue: countType) ?? .txt
  333. self._loadData()
  334. var haveWaters = false
  335. if self.currentType == .txt {
  336. self._currentTextData = watermark
  337. if self._textTemplateArray?.count ?? 0 > 0 {
  338. haveWaters = true
  339. }
  340. } else if self.currentType == .file {
  341. self._currentFileData = watermark
  342. if self._fileTemplateArray?.count ?? 0 > 0 {
  343. haveWaters = true
  344. }
  345. }
  346. if haveWaters {
  347. let indexPath = IndexPath(item: 0, section: 0)
  348. var set = Set<IndexPath>()
  349. set.insert(indexPath)
  350. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  351. }
  352. self._postNotification()
  353. }
  354. self.km_beginSheet(windowC: controller)
  355. }
  356. }
  357. }
  358. // MARK: - Private Methods
  359. extension KMBatchOperateAddWatermarkViewController {
  360. private func _localizedlanguage() {
  361. self.addButton.title = " " + KMLocalizedString("Add Template", nil)
  362. self.addButton.imageHugsTitle = true
  363. self.fileButton.title = KMLocalizedString("File", nil)
  364. self.actionButton.title = KMLocalizedString("Apply", nil)
  365. if (!self.isBackground) {
  366. self.titleLabel.stringValue = KMLocalizedString("Watermark", nil)
  367. self.textButton.title = KMLocalizedString("Text", nil)
  368. } else {
  369. self.titleLabel.stringValue = KMLocalizedString("Background", nil)
  370. self.textButton.title = KMLocalizedString("Color", nil)
  371. }
  372. if (self.onlyManagerTemplate) {
  373. self.titleLabel.isHidden = true
  374. self.addButton.isHidden = false
  375. self.managerTemplateTitleLabel.isHidden = false
  376. } else {
  377. self.titleLabel.isHidden = false
  378. self.addButton.isHidden = false
  379. self.managerTemplateTitleLabel.isHidden = true
  380. }
  381. self.managerTemplateTitleLabel.stringValue = KMLocalizedString("Manage Templates", nil)
  382. }
  383. private func _configuiUI() {
  384. self.topBaseView.wantsLayer = true
  385. self.topBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  386. self.collectionView.wantsLayer = true
  387. self.collectionView.isSelectable = true
  388. self.collectionView.allowsMultipleSelection = false
  389. self.collectionView.allowsEmptySelection = false
  390. self.collectionView.enclosingScrollView?.borderType = .noBorder
  391. self.collectionView.enclosingScrollView?.drawsBackground = false
  392. self.collectionView.register(KMWatermarkCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"))
  393. let v = NSView(frame: NSMakeRect(0, 0, 100, 100))
  394. v.wantsLayer = true
  395. v.layer?.backgroundColor = .clear
  396. self.collectionView.backgroundView = v;
  397. self.collectionView.enclosingScrollView?.horizontalScrollElasticity = .none
  398. self.collectionView.enclosingScrollView?.verticalScrollElasticity = .none
  399. self.titleLabel.font = .boldSystemFont(ofSize: 14)
  400. self.titleLabel.textColor = KMAppearance.Layout.h0Color()
  401. self.actionButton.font = .systemFont(ofSize: 13)
  402. self.fileButton.font = .systemFont(ofSize: 13)
  403. self.textButton.font = .systemFont(ofSize: 13)
  404. self.actionButton.wantsLayer = true
  405. self.addButton.wantsLayer = true
  406. if (self.onlyManagerTemplate) {
  407. self.topHeightConstraint.constant = 40
  408. self.addButton.imagePosition = .imageLeft
  409. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  410. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  411. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  412. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  413. self.actionButton.imagePosition = .noImage
  414. } else {
  415. self.topHeightConstraint.constant = 0
  416. self.addButton.imagePosition = .imageLeft
  417. self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
  418. self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
  419. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  420. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  421. self.actionButton.imagePosition = .noImage
  422. }
  423. if (!self.onlyManagerTemplate) {
  424. if let cnt = self.files?.count, cnt > 0 {
  425. self._haveFiles = true
  426. } else {
  427. self._haveFiles = false
  428. }
  429. self._updateActionButtonbackgroundColor()
  430. }
  431. self.addButton.layer?.cornerRadius = 1.0
  432. self.actionButton.layer?.cornerRadius = 1.0
  433. self.actionButton.layer?.cornerRadius = 1.0
  434. self.textButton.wantsLayer = true
  435. self.fileButton.wantsLayer = true
  436. self.textBox.fillColor = KMAppearance.Interactive.a0Color()
  437. self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
  438. self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
  439. self.fileButton.setTitleColor(KMAppearance.Layout.h0Color())
  440. self.managerTemplateTitleLabel.font = .systemFont(ofSize: 14)
  441. self.managerTemplateTitleLabel.textColor = KMAppearance.Layout.h0Color()
  442. self.view.addSubview(self.blankView)
  443. self.blankView.mas_makeConstraints { make in
  444. make?.top.equalTo()(self.topBaseView.mas_bottom)
  445. make?.left.right().equalTo()(self.view)
  446. make?.bottom.equalTo()(self.bottomBaseView.mas_top)
  447. make?.height.greaterThanOrEqualTo()(200)
  448. }
  449. self.blankView.titleLabel.stringValue = KMLocalizedString("No Templates", nil)
  450. if (self.isBackground) {
  451. self.blankView.imageView.image = NSImage(named: KMImageNameEmptyBackground)
  452. }
  453. self.blankView.wantsLayer = true
  454. let menu = NSMenu()
  455. if(!self.isBackground) {
  456. if(!self.isBatchOperation) {
  457. _ = menu.addItem(title: KMLocalizedString("Batch Add Watermarks", nil), action: #selector(_buttonItemClick_addBatch), target: self)
  458. }
  459. _ = menu.addItem(title: KMLocalizedString("Remove All Watermark Templates", nil), action: #selector(_buttonItemClick_CleanAll), target: self)
  460. } else {
  461. if(!self.isBatchOperation) {
  462. _ = menu.addItem(title: KMLocalizedString("Batch Add Background", nil), action: #selector(_buttonItemClick_addBatch), target: self)
  463. }
  464. _ = menu.addItem(title: KMLocalizedString("Remove All Background Templates", nil), action: #selector(_buttonItemClick_CleanAll), target: self)
  465. }
  466. self.view.menu = menu;
  467. }
  468. private func _loadData() {
  469. self._textTemplateArray = []
  470. self._fileTemplateArray = []
  471. if (!self.isBackground) {
  472. let watermarkArr = KMWatermarkManager.defaultManager.watermarks
  473. for i in 0 ..< watermarkArr.count {
  474. let watermark = watermarkArr[i]
  475. if ((watermark.image) != nil) {
  476. self._fileTemplateArray?.append(watermark)
  477. } else {
  478. self._textTemplateArray?.append(watermark)
  479. }
  480. }
  481. } else {
  482. let arr = KMBackgroundManager.defaultManager.datas
  483. for i in 0 ..< arr.count {
  484. let obj = arr[i]
  485. if (obj.type == .color) {
  486. self._textTemplateArray?.append(obj)
  487. } else {
  488. self._fileTemplateArray?.append(obj)
  489. }
  490. }
  491. }
  492. self.collectionView.reloadData()
  493. self._updateActionButtonbackgroundColor()
  494. NotificationCenter.default.addObserver(self, selector: #selector(_batchFilesCountNotification), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
  495. }
  496. private func _isSelectIndex() -> Bool {
  497. var indexSet = false
  498. if (!self.isBackground) {
  499. if(self.currentType == .txt) {
  500. if let data = self._currentTextData {
  501. indexSet = (self._textTemplateArray ?? []).contains(data)
  502. }
  503. }else {
  504. if let data = self._currentFileData {
  505. indexSet = (self._fileTemplateArray ?? []).contains(data)
  506. }
  507. }
  508. } else {
  509. if(self.currentBackgroundType == .color) {
  510. if let data = self._currentColorData {
  511. indexSet = (self._textTemplateArray ?? []).contains(data)
  512. }
  513. }else {
  514. if let data = self._currentImageData {
  515. indexSet = (self._fileTemplateArray ?? []).contains(data)
  516. }
  517. }
  518. }
  519. return indexSet;
  520. }
  521. private func _updateActionButtonbackgroundColor() {
  522. let indexSet = self._isSelectIndex()
  523. if (self.files?.count ?? 0 > 0 || self.pdfView?.document.documentURL.path.count ?? 0 > 0 ) && indexSet {
  524. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
  525. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  526. } else {
  527. self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  528. self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
  529. }
  530. }
  531. @objc private func _buttonItemClick_addBatch(_ sender: Any?) {
  532. let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
  533. let file = KMBatchOperateFile(filePath: self.pdfView!.document.documentURL.path, type: self.isBackground ? .AddBackground : .AddWatermark)
  534. baseWindowController.window?.makeKeyAndOrderFront(nil)
  535. baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: self.isBackground ? .AddBackground : .AddWatermark, files: [file])
  536. }
  537. @objc private func _buttonItemClick_CleanAll(_ sender: Any?) {
  538. let alert = NSAlert()
  539. alert.alertStyle = .warning
  540. alert.messageText = ""
  541. alert.informativeText = KMLocalizedString("Are you sure to delete all templates?", nil)
  542. alert.addButton(withTitle: KMLocalizedString("Delete", nil))
  543. alert.addButton(withTitle: KMLocalizedString("Cancel", nil))
  544. alert.beginSheetModal(for: NSApp.mainWindow!) { returnCode in
  545. if returnCode == .alertFirstButtonReturn {
  546. self._deleteAllWatermark()
  547. }
  548. }
  549. }
  550. func editBackground(_ background: KMBackgroundModel) {
  551. var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  552. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  553. filePath = self.pdfView?.document.documentURL.path ?? ""
  554. }
  555. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  556. let controller = KMBackgroundWindowController(windowNibName: "KMBackgroundWindowController")
  557. controller.isBatch = isBatchOperation
  558. controller.type = .edit
  559. controller.background = background
  560. controller.currentType = self.currentBackgroundType.rawValue
  561. controller.pdfDocument = cdocument
  562. controller.cancelAction = { [weak self] controller in
  563. self?.km_endSheet()
  564. }
  565. self.km_beginSheet(windowC: controller)
  566. controller.operateCallBack = {[weak self] controller, background, countType in
  567. self?.currentBackgroundType = KMBackgroundType(rawValue: countType) ?? .color
  568. self?._loadData()
  569. var haveBackgrounds = false
  570. if self?.currentBackgroundType == .color {
  571. self?._currentColorData = background
  572. if self?._textTemplateArray?.count ?? 0 > 0 {
  573. haveBackgrounds = true
  574. }
  575. } else if self?.currentBackgroundType == .file {
  576. self?._currentImageData = background
  577. if self?._fileTemplateArray?.count ?? 0 > 0 {
  578. haveBackgrounds = true
  579. }
  580. }
  581. if haveBackgrounds {
  582. let indexPath = IndexPath(item: 0, section: 0)
  583. var set = Set<IndexPath>()
  584. set.insert(indexPath)
  585. self?.collectionView.selectItems(at: set, scrollPosition: .bottom)
  586. }
  587. self?._postNotification()
  588. }
  589. }
  590. private func deleteBackground(_ background: KMBackgroundModel) {
  591. let _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background)
  592. self._loadData()
  593. self._postNotification()
  594. }
  595. func editWatermark(_ waterMark: KMWatermarkModel) {
  596. if !isBackground {
  597. var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
  598. if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
  599. filePath = self.pdfView?.document.documentURL.path ?? ""
  600. }
  601. let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
  602. let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
  603. controller.watermark = waterMark
  604. controller.isBatch = isBatchOperation
  605. controller.type = .edit
  606. controller.pdfDocument = cdocument
  607. controller.currentType = self.currentType.rawValue
  608. controller.cancelAction = { [weak self] wmWindowC in
  609. self?.km_endSheet()
  610. }
  611. controller.operateCallBack = {[weak self] controller, watermark, countType in
  612. self?._loadData()
  613. var haveWaters = false
  614. if self?.currentType == .txt {
  615. self?._currentTextData = watermark
  616. if self?._textTemplateArray?.count ?? 0 > 0 {
  617. haveWaters = true
  618. }
  619. } else if self?.currentType == .file {
  620. self?._currentFileData = watermark
  621. if self?._fileTemplateArray?.count ?? 0 > 0 {
  622. haveWaters = true
  623. }
  624. }
  625. if haveWaters {
  626. let indexPath = IndexPath(item: 0, section: 0)
  627. var set = Set<IndexPath>()
  628. set.insert(indexPath)
  629. self?.collectionView.selectItems(at: set, scrollPosition: .bottom)
  630. }
  631. self?._postNotification()
  632. }
  633. self.km_beginSheet(windowC: controller)
  634. } else {
  635. // Handle background case
  636. }
  637. }
  638. private func _deleteAllWatermark() {
  639. if(!self.isBackground) {
  640. for waterMark in self._textTemplateArray ?? [] {
  641. _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  642. }
  643. for waterMark in self._fileTemplateArray ?? [] {
  644. _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
  645. }
  646. } else {
  647. _ = KMBackgroundManager.defaultManager.deleteAllTemplates()
  648. }
  649. self._loadData()
  650. self._postNotification()
  651. }
  652. private func _deleteWatermark(_ waterMark: KMWatermarkModel) {
  653. // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
  654. let _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark)
  655. self._loadData()
  656. self._postNotification()
  657. }
  658. private func _postNotification() {
  659. NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMBatchOperateWatermarksNotification"), object: self)
  660. }
  661. @objc private func _watermarksNotification(_ notification: NSNotification) {
  662. let addWatermark = notification.object as? KMBatchOperateAddWatermarkViewController
  663. if self.isEqual(to: addWatermark) == false {
  664. self._loadData()
  665. var haveWaters = false
  666. if (self.currentType == .txt) {
  667. if let cnt = self._textTemplateArray?.count, cnt > 0 {
  668. haveWaters = true
  669. }
  670. } else if (self.currentType == .file) {
  671. if let cnt = self._fileTemplateArray?.count, cnt > 0 {
  672. haveWaters = true
  673. }
  674. }
  675. if (haveWaters) {
  676. let indexpath = IndexPath(item: 0, section: 0)
  677. var set = Set<IndexPath>()
  678. set.insert(indexpath)
  679. self.collectionView.selectItems(at: set, scrollPosition: .bottom)
  680. }
  681. }
  682. }
  683. //检查是否有任务文件要加载,如果有,重置任务。如果返回YES,表示有任务并且任务已经重置,否则表示没有任务,需要中断操作
  684. private func _checkAndResetTask() -> Bool {
  685. if let cnt = self.files?.count, cnt < 1 {
  686. return false
  687. }
  688. for i in 0 ..< self.files!.count {
  689. let file = self.files![i]
  690. if (self.isBackground) {
  691. file.addBackgroundInfo.resetState()
  692. } else {
  693. file.addWatermarkInfo.resetState()
  694. }
  695. }
  696. return true
  697. }
  698. private func _choosePathAndBeginOperation(_ obj: Any?) {
  699. let openPanel = NSOpenPanel()
  700. openPanel.canChooseFiles = false
  701. openPanel.canChooseDirectories = true
  702. openPanel.canCreateDirectories = true
  703. openPanel.beginSheetModal(for: self.view.window!) { result in
  704. if (result == .OK) {
  705. for fileURL in openPanel.urls {
  706. self.choosePath = fileURL.path
  707. if (!self.isBackground) {
  708. self._beginAddWatermark(obj as! KMWatermarkModel)
  709. } else {
  710. self._beginAddBackground(obj as! KMBackgroundModel)
  711. }
  712. }
  713. }
  714. }
  715. }
  716. private func _beginAddWatermark(_ watermark: KMWatermarkModel) {
  717. self.hiddenWindowCloseButtonIfNeeded()
  718. self.successFilePathURLArray?.removeAll()
  719. for i in 0 ..< self.files!.count {
  720. let file = self.files![i]
  721. file.addWatermarkInfo.savePath = self.choosePath
  722. let operation = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark)
  723. operation.delegate = self
  724. self.queue?.addOperation(operation)
  725. }
  726. if let cnt = self.queue?.operations.count, cnt > 0 {
  727. self.interfaceStatus = .Processing
  728. }
  729. }
  730. private func _beginAddBackground(_ background: KMBackgroundModel) {
  731. self.hiddenWindowCloseButtonIfNeeded()
  732. self.successFilePathURLArray?.removeAll()
  733. for i in 0 ..< self.files!.count {
  734. let file = self.files![i]
  735. file.addBackgroundInfo.savePath = self.choosePath
  736. let operation = KMBatchAddBackgroundOperation(file: file, backgroundM: background)
  737. operation.delegate = self
  738. self.queue?.addOperation(operation)
  739. }
  740. if let cnt = self.queue?.operations.count, cnt > 0 {
  741. self.interfaceStatus = .Processing
  742. }
  743. }
  744. @objc private func _batchFilesCountNotification(_ notification: NSNotification) {
  745. let files: Array? = notification.object as? [KMBatchOperateFile]
  746. if let data = files?.first {
  747. if data.currentOperateType != self.operateType {
  748. return
  749. }
  750. }
  751. let viewC = notification.userInfo?[kObjectKey] as? NSViewController
  752. if let data = self.view.window?.isEqual(to: viewC?.view.window), data == false {
  753. return
  754. }
  755. if (!self.onlyManagerTemplate) {
  756. let files: Array? = notification.object as? [KMBatchOperateFile]
  757. self.files? = files ?? []
  758. if (files?.count ?? 0 > 0) {
  759. self._haveFiles = true
  760. } else {
  761. self._haveFiles = false
  762. }
  763. self._updateActionButtonbackgroundColor()
  764. }
  765. }
  766. }
  767. extension KMBatchOperateAddWatermarkViewController: NSMenuItemValidation {
  768. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  769. let action = menuItem.action
  770. if (action == #selector(_buttonItemClick_CleanAll)) {
  771. if (self._textTemplateArray!.count == 0 && self._fileTemplateArray!.count == 0) {
  772. return false
  773. }
  774. return true
  775. }
  776. return true
  777. }
  778. }
  779. extension KMBatchOperateAddWatermarkViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  780. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  781. var count = 0
  782. if (!self.isBackground) {
  783. if (self.currentType == .txt) {
  784. count = self._textTemplateArray?.count ?? 0
  785. } else {
  786. count = self._fileTemplateArray?.count ?? 0
  787. }
  788. } else {
  789. if (self.currentBackgroundType == .color) {
  790. count = self._textTemplateArray?.count ?? 0
  791. } else {
  792. count = self._fileTemplateArray?.count ?? 0
  793. }
  794. }
  795. self.blankView.isHidden = count != 0
  796. return count
  797. }
  798. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  799. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"), for: indexPath)
  800. if (!self.isBackground) {
  801. var waterMark: KMWatermarkModel?
  802. if (self.currentType == .txt) {
  803. waterMark = (self._textTemplateArray![indexPath.item] as! KMWatermarkModel)
  804. if(self._currentTextData == waterMark) {
  805. item.isSelected = true
  806. }else {
  807. item.isSelected = false
  808. }
  809. } else {
  810. waterMark = (self._fileTemplateArray![indexPath.item] as! KMWatermarkModel)
  811. if(self._currentFileData == waterMark) {
  812. item.isSelected = true
  813. }else{
  814. item.isSelected = false
  815. }
  816. }
  817. // __block typeof(self) blockSelf = self;
  818. let _item = item as? KMWatermarkCollectionViewItem
  819. _item?.updateInterface(waterMark!)
  820. _item?.waterMarkOprateCallback = { [unowned self] type, wm in
  821. if (wm != nil) {
  822. if (type == .Edit) {
  823. if (self.currentType == .txt) {
  824. self._currentTextData = waterMark
  825. } else {
  826. self._currentFileData = waterMark
  827. }
  828. collectionView.reloadData()
  829. self.editWatermark(waterMark!)
  830. } else {
  831. self._deleteWatermark(waterMark!)
  832. }
  833. }
  834. };
  835. } else {
  836. var background: KMBackgroundModel?
  837. if (self.currentBackgroundType == .color) {
  838. background = (self._textTemplateArray![indexPath.item] as! KMBackgroundModel)
  839. if(self._currentColorData == background) {
  840. item.isSelected = true
  841. }else {
  842. item.isSelected = false
  843. }
  844. } else {
  845. background = (self._fileTemplateArray![indexPath.item] as! KMBackgroundModel)
  846. if(self._currentImageData == background) {
  847. item.isSelected = true
  848. }else {
  849. item.isSelected = false
  850. }
  851. }
  852. let _item = item as? KMWatermarkCollectionViewItem
  853. _item?.updateBackgroundInterface(background!)
  854. // __block typeof(self) blockSelf = self;
  855. _item?.backgroundOperateCallback = { [unowned self] type, bg in
  856. if (type == .Edit) {
  857. if (self.currentBackgroundType == .color) {
  858. self._currentColorData = background
  859. } else {
  860. self._currentImageData = background
  861. }
  862. collectionView.reloadData()
  863. self.editBackground(background!)
  864. } else {
  865. self.deleteBackground(background!)
  866. }
  867. } ;
  868. }
  869. self._updateActionButtonbackgroundColor()
  870. return item
  871. }
  872. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  873. if (collectionView.frame.size.width < 240) {
  874. return NSMakeSize(0, 0)
  875. } else {
  876. return NSMakeSize(104, 167)
  877. }
  878. }
  879. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  880. if (collectionView.frame.size.width < 32) {
  881. return NSEdgeInsetsMake(0, 0, 0, 0)
  882. }
  883. return NSEdgeInsetsMake(0, 16, 0, 16)
  884. }
  885. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  886. let indexPath = indexPaths.first
  887. guard let _indexPath = indexPath else {
  888. return
  889. }
  890. if (indexPath!.item < 0) {
  891. return
  892. }
  893. if (self.isBackground) {
  894. var background: KMBackgroundModel?
  895. if (self.currentBackgroundType == .color) {
  896. background = self._textTemplateArray![_indexPath.item] as? KMBackgroundModel
  897. self._currentColorData = background
  898. } else {
  899. background = self._fileTemplateArray![_indexPath.item] as? KMBackgroundModel
  900. self._currentImageData = background
  901. }
  902. for i in 0 ..< self.files!.count {
  903. let file = self.files![i]
  904. file.addBackgroundInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (background?.pageRangeType.rawValue)!)!
  905. file.addBackgroundInfo.pageRangeString = background?.pageRangeString
  906. }
  907. } else {
  908. var waterMark: KMWatermarkModel?
  909. if (self.currentType == .txt) {
  910. waterMark = self._textTemplateArray![_indexPath.item] as? KMWatermarkModel
  911. self._currentTextData = waterMark
  912. } else {
  913. waterMark = self._fileTemplateArray![_indexPath.item] as? KMWatermarkModel
  914. self._currentFileData = waterMark
  915. }
  916. for i in 0 ..< self.files!.count {
  917. let file = self.files![i]
  918. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (waterMark?.pageRangeType.rawValue)!)!
  919. file.addWatermarkInfo.pageRangeString = waterMark?.pageRangeString
  920. }
  921. }
  922. NotificationCenter.default.post(name: NSNotification.Name("kNeedChangePageRangeNotification"), object: nil)
  923. collectionView.reloadData()
  924. self._updateActionButtonbackgroundColor()
  925. }
  926. }