KMBatchOperateAddWatermarkViewController.swift 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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 = { [unowned 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 = { [unowned 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: CGRectMake(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. if (!self.onlyManagerTemplate) {
  746. let files: Array? = notification.object as? [KMBatchOperateFile]
  747. self.files? = files ?? []
  748. if (files?.count ?? 0 > 0) {
  749. self._haveFiles = true
  750. } else {
  751. self._haveFiles = false
  752. }
  753. self._updateActionButtonbackgroundColor()
  754. }
  755. }
  756. }
  757. extension KMBatchOperateAddWatermarkViewController: NSMenuItemValidation {
  758. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  759. let action = menuItem.action
  760. if (action == #selector(_buttonItemClick_CleanAll)) {
  761. if (self._textTemplateArray!.count == 0 && self._fileTemplateArray!.count == 0) {
  762. return false
  763. }
  764. return true
  765. }
  766. return true
  767. }
  768. }
  769. extension KMBatchOperateAddWatermarkViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
  770. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  771. var count = 0
  772. if (!self.isBackground) {
  773. if (self.currentType == .txt) {
  774. count = self._textTemplateArray?.count ?? 0
  775. } else {
  776. count = self._fileTemplateArray?.count ?? 0
  777. }
  778. } else {
  779. if (self.currentBackgroundType == .color) {
  780. count = self._textTemplateArray?.count ?? 0
  781. } else {
  782. count = self._fileTemplateArray?.count ?? 0
  783. }
  784. }
  785. self.blankView.isHidden = count != 0
  786. return count
  787. }
  788. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  789. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"), for: indexPath)
  790. if (!self.isBackground) {
  791. var waterMark: KMWatermarkModel?
  792. if (self.currentType == .txt) {
  793. waterMark = (self._textTemplateArray![indexPath.item] as! KMWatermarkModel)
  794. if(self._currentTextData == waterMark) {
  795. item.isSelected = true
  796. }else {
  797. item.isSelected = false
  798. }
  799. } else {
  800. waterMark = (self._fileTemplateArray![indexPath.item] as! KMWatermarkModel)
  801. if(self._currentFileData == waterMark) {
  802. item.isSelected = true
  803. }else{
  804. item.isSelected = false
  805. }
  806. }
  807. // __block typeof(self) blockSelf = self;
  808. let _item = item as? KMWatermarkCollectionViewItem
  809. _item?.updateInterface(waterMark!)
  810. _item?.waterMarkOprateCallback = { [unowned self] type, wm in
  811. if (wm != nil) {
  812. if (type == .Edit) {
  813. if (self.currentType == .txt) {
  814. self._currentTextData = waterMark
  815. } else {
  816. self._currentFileData = waterMark
  817. }
  818. collectionView.reloadData()
  819. self.editWatermark(waterMark!)
  820. } else {
  821. self._deleteWatermark(waterMark!)
  822. }
  823. }
  824. };
  825. } else {
  826. var background: KMBackgroundModel?
  827. if (self.currentBackgroundType == .color) {
  828. background = (self._textTemplateArray![indexPath.item] as! KMBackgroundModel)
  829. if(self._currentColorData == background) {
  830. item.isSelected = true
  831. }else {
  832. item.isSelected = false
  833. }
  834. } else {
  835. background = (self._fileTemplateArray![indexPath.item] as! KMBackgroundModel)
  836. if(self._currentImageData == background) {
  837. item.isSelected = true
  838. }else {
  839. item.isSelected = false
  840. }
  841. }
  842. let _item = item as? KMWatermarkCollectionViewItem
  843. _item?.updateBackgroundInterface(background!)
  844. // __block typeof(self) blockSelf = self;
  845. _item?.backgroundOperateCallback = { [unowned self] type, bg in
  846. if (type == .Edit) {
  847. if (self.currentBackgroundType == .color) {
  848. self._currentColorData = background
  849. } else {
  850. self._currentImageData = background
  851. }
  852. collectionView.reloadData()
  853. self.editBackground(background!)
  854. } else {
  855. self.deleteBackground(background!)
  856. }
  857. } ;
  858. }
  859. self._updateActionButtonbackgroundColor()
  860. return item
  861. }
  862. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  863. if (collectionView.frame.size.width < 240) {
  864. return CGSizeMake(0, 0)
  865. } else {
  866. return CGSizeMake(104, 167)
  867. }
  868. }
  869. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  870. if (collectionView.frame.size.width < 32) {
  871. return NSEdgeInsetsMake(0, 0, 0, 0)
  872. }
  873. return NSEdgeInsetsMake(0, 16, 0, 16)
  874. }
  875. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  876. let indexPath = indexPaths.first
  877. guard let _indexPath = indexPath else {
  878. return
  879. }
  880. if (indexPath!.item < 0) {
  881. return
  882. }
  883. if (self.isBackground) {
  884. var background: KMBackgroundModel?
  885. if (self.currentBackgroundType == .color) {
  886. background = self._textTemplateArray![_indexPath.item] as? KMBackgroundModel
  887. self._currentColorData = background
  888. } else {
  889. background = self._fileTemplateArray![_indexPath.item] as? KMBackgroundModel
  890. self._currentImageData = background
  891. }
  892. for i in 0 ..< self.files!.count {
  893. let file = self.files![i]
  894. file.addBackgroundInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (background?.pageRangeType.rawValue)!)!
  895. file.addBackgroundInfo.pageRangeString = background?.pageRangeString
  896. }
  897. } else {
  898. var waterMark: KMWatermarkModel?
  899. if (self.currentType == .txt) {
  900. waterMark = self._textTemplateArray![_indexPath.item] as? KMWatermarkModel
  901. self._currentTextData = waterMark
  902. } else {
  903. waterMark = self._fileTemplateArray![_indexPath.item] as? KMWatermarkModel
  904. self._currentFileData = waterMark
  905. }
  906. for i in 0 ..< self.files!.count {
  907. let file = self.files![i]
  908. file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (waterMark?.pageRangeType.rawValue)!)!
  909. file.addWatermarkInfo.pageRangeString = waterMark?.pageRangeString
  910. }
  911. }
  912. NotificationCenter.default.post(name: NSNotification.Name("kNeedChangePageRangeNotification"), object: nil)
  913. collectionView.reloadData()
  914. self._updateActionButtonbackgroundColor()
  915. }
  916. }