1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024 |
- //
- // KMBatchOperateAddWatermarkViewController.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/11/3.
- //
- import Cocoa
- enum KMWatermarkType: Int {
- case txt = 0
- case file
- }
- class KMCollectionViewFlowLayout: NSCollectionViewFlowLayout {
- override func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool {
- self.collectionView?.reloadSections(IndexSet(integer: 0))
- return true
- }
- }
- class KMBatchOperateAddWatermarkViewController: KMBatchOperateBaseViewController {
- var isBackground = false
- var onlyManagerTemplate = false
-
- weak var pdfView: CPDFView?
- var isBatchOperation = false //是否在批量界面
-
- @IBOutlet var topBaseView: NSView!
- @IBOutlet var titleLabel: NSTextField!
- @IBOutlet var addButton: NSButton!
- @IBOutlet var textButton: NSButton!
- @IBOutlet var fileButton: NSButton!
- @IBOutlet var textBox: NSBox!
- @IBOutlet var fileBox: NSBox!
- @IBOutlet var actionButton: NSButton!
- @IBOutlet var collectionView: NSCollectionView!
- @IBOutlet var bottomBaseView: NSView!
- @IBOutlet var managerTemplateTitleLabel: NSTextField!
-
- @IBOutlet var blankView: KMBlankView!
-
- @IBOutlet var managerTemplateButtonTopConstraint: NSLayoutConstraint!
- @IBOutlet var managerTemplateButtonHeightConstraint: NSLayoutConstraint!
- @IBOutlet var topHeightConstraint: NSLayoutConstraint!
- @IBOutlet var addButtonHeightConstraint: NSLayoutConstraint!
- @IBOutlet var addButtonBottomConstraint: NSLayoutConstraint!
-
- private var _textTemplateArray: [NSObject]?
- private var _fileTemplateArray: [NSObject]?
-
- private var _currentTextData: KMWatermarkModel?
- private var _currentFileData: KMWatermarkModel?
-
- private var _currentColorData: KMBackgroundModel?
- private var _currentImageData: KMBackgroundModel?
-
- private var _currentBackgroundType: KMBackgroundType = .color
- var currentBackgroundType: KMBackgroundType {
- get {
- return self._currentBackgroundType
- }
- set {
- if (self._currentBackgroundType != newValue) {
- self._currentBackgroundType = newValue
-
- if (self._currentBackgroundType == .color) {
- self.textBox.fillColor = KMAppearance.Interactive.a0Color()
- self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
-
- self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
- self.fileButton.setTitleColor(KMAppearance.Layout.h1Color())
- } else {
- self.textBox.fillColor = KMAppearance.Layout.l_1Color()
- self.fileBox.fillColor = KMAppearance.Interactive.a0Color()
-
- self.fileButton.setTitleColor(KMAppearance.Layout.w0Color())
- self.textButton.setTitleColor(KMAppearance.Layout.h1Color())
- }
- self.collectionView.reloadData()
- }
- }
- }
-
- private var _currentType: KMWatermarkType = .txt
- var currentType: KMWatermarkType {
- set {
- if self.currentType != newValue {
- self._currentType = newValue
-
- if (currentType == .txt) {
- self.textBox.fillColor = KMAppearance.Interactive.a0Color()
- self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
- self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
- self.fileButton.setTitleColor(KMAppearance.Layout.h1Color())
- } else {
- self.textBox.fillColor = KMAppearance.Layout.l_1Color()
- self.fileBox.fillColor = KMAppearance.Interactive.a0Color()
- self.fileButton.setTitleColor(KMAppearance.Layout.w0Color())
- self.textButton.setTitleColor(KMAppearance.Layout.h1Color())
- }
- self.collectionView.reloadData()
- }
- }
- get {
- return self._currentType
- }
- }
-
- private var _haveFiles = false
-
- deinit {
- KMPrint("KMBatchOperateAddWatermarkViewController deinit.")
- NotificationCenter.default.removeObserver(self)
- }
- override func viewDidLoad() {
- super.viewDidLoad()
-
- self._localizedlanguage()
- self._configuiUI()
- self._loadData()
-
- self.interfaceStatus = .PrepareProcess
-
- NotificationCenter.default.addObserver(self, selector: #selector(_watermarksNotification), name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: nil)
-
- self.collectionView.reloadData()
- }
-
- func watermarkInterfaceSelectWatermark(_ watermark: KMWatermarkModel) {
- if(watermark.image != nil) {
- self._currentFileData = watermark
- }else {
- self._currentTextData = watermark
- }
- self._loadData()
- self.currentType = watermark.image != nil ? .file : .txt
- let arr = self.currentType == .file ? self._fileTemplateArray : self._textTemplateArray
- let index = arr?.firstIndex(of: watermark)
- if (index != NSNotFound) {
- let indexpath = IndexPath(item: index!, section: 0)
- var set = Set<IndexPath>()
- set.insert(indexpath)
- self.collectionView.selectItems(at: set, scrollPosition: .bottom)
- }
- }
-
- func backgroundInterfaceSelectBackGround(_ background: KMBackgroundModel) {
- if(background.type == .file) {
- self._currentImageData = background
- }else {
- self._currentColorData = background
- }
-
- self._loadData()
- self.currentBackgroundType = background.type
- let arr = self.currentBackgroundType == .file ? self._fileTemplateArray : self._textTemplateArray
- let index = arr?.firstIndex(of: background)
- if (index != NSNotFound) {
- let indexpath = IndexPath(item: index!, section: 0)
- var set = Set<IndexPath>()
- set.insert(indexpath)
- self.collectionView.selectItems(at: set, scrollPosition: .bottom)
- }
- }
-
- override var interfaceStatus: KMBatchOperateInterfaceStatus? {
- didSet {
- if (self.interfaceStatus == .PrepareProcess) {
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
- var files: [URL] = []
- for url in self.successFilePathURLArray ?? [] {
- if FileManager.default.fileExists(atPath: url.path) {
- files.append(url)
- }
- }
- if (files.count > 0) {
- let workspace = NSWorkspace.shared
- workspace.activateFileViewerSelecting(files)
- }
- }
- self.collectionView.isSelectable = true
- self.actionButton.tag = 1
- self.actionButton.title = KMLocalizedString("Apply", nil)
- self.addButton.isEnabled = true
- } else {
- self.collectionView.isSelectable = false
- self.addButton.isEnabled = false
- self.actionButton.tag = 0
- self.actionButton.title = KMLocalizedString("Cancel", nil);
- }
- self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
- }
- }
-
- //button Actions,切换水印类型
- @IBAction func buttonClicked_SwitchWaterMarkType(_ sender: NSButton) {
- if (self.isBackground) {
- self.currentBackgroundType = KMBackgroundType(rawValue: sender.tag) ?? .color
- } else {
- self.currentType = KMWatermarkType(rawValue: sender.tag) ?? .txt
- }
- self._updateActionButtonbackgroundColor()
- }
-
- @IBAction func buttonClicked_addWaterMark(_ sender: NSButton) {
- if (!self.onlyManagerTemplate) {
- if (!self._haveFiles) {
- return
- }
- }
-
- self.view.window?.makeFirstResponder(nil)
-
-
- if (self.onlyManagerTemplate) {
- if (sender.tag == 1) {
- var watermark: KMWatermarkModel?
- var background: KMBackgroundModel?
- let indexSet = self._isSelectIndex()
- if (!indexSet) {
- return
- } else {
- if (!self.isBackground) {
- if (self.currentType == .txt) {
- watermark = self._currentTextData
- } else {
- watermark = self._currentFileData
- }
- } else {
- if (self.currentBackgroundType == .color) {
- background = self._currentColorData
- } else {
- background = self._currentImageData
- }
- }
- }
- let openPanel = NSOpenPanel()
- openPanel.canChooseFiles = false
- openPanel.canChooseDirectories = true
- openPanel.canCreateDirectories = true
- openPanel.beginSheetModal(for: self.view.window!) { result in
- if (result == .OK) {
- for fileURL in openPanel.urls {
- self.hiddenWindowCloseButtonIfNeeded()
- self.successFilePathURLArray?.removeAll()
- if (!self.isBackground) {
- let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddWatermark)
- file.addWatermarkInfo.savePath = fileURL.path
- file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
- file.addWatermarkInfo.pageRangeString = watermark?.pagesString ?? ""
- let op = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark!)
- op.delegate = self
- self.queue?.addOperation(op)
- } else {
- let file = KMBatchOperateFile(filePath: self.pdfView?.document.documentURL.path ?? "", type: .AddBackground)
- file.addBackgroundInfo.savePath = fileURL.path
- file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice(rawValue: watermark?.pageRangeType.rawValue ?? 0) ?? .All
- file.addBackgroundInfo.pageRangeString = background?.pagesString ?? ""
- let op = KMBatchAddBackgroundOperation(file: file, backgroundM: background!)
- op.delegate = self
- self.queue?.addOperation(op)
- }
- if let cnt = self.queue?.operations.count, cnt > 0 {
- self.interfaceStatus = .Processing
- }
- }
- }
- }
- } else if (sender.tag == 0) {
- if let cnt = self.queue?.operations.count, cnt > 0 {
- self.queue?.cancelAllOperations()
- }
- self.interfaceStatus = .PrepareProcess
- }
- } else {
- //点击开始
- if (sender.tag == 1) {
- if (!self._checkAndResetTask()) {
- return;
- }
- var watermark: KMWatermarkModel?
- var background: KMBackgroundModel?
- let indexSet = self._isSelectIndex()
- if (!indexSet || self.files!.count < 1) {
- return;
- } else {
- if (!self.isBackground) {
- if (self.currentType == .txt) {
- watermark = self._currentTextData
- } else {
- watermark = self._currentFileData
- }
- } else {
- if (self.currentBackgroundType == .color) {
- background = self._currentColorData
- } else {
- background = self._currentImageData
- }
- }
- }
-
- if (self.isBackground) {
- self._choosePathAndBeginOperation(background)
- } else {
- self._choosePathAndBeginOperation(watermark)
- }
- } else if (sender.tag == 0) {
- if let cnt = self.queue?.operations.count, cnt > 0 {
- self.queue?.cancelAllOperations()
- }
- self.interfaceStatus = .PrepareProcess
- }
- }
- }
-
- @IBAction func buttonClicked_addtemplate(_ sender: NSButton) {
- let filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
- var cdocument = self.pdfView?.document
- if isBatchOperation {
- cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
- }
- if isBackground {
- let controller = KMBackgroundWindowController(windowNibName: "KMBackgroundWindowController")
- controller.isBatch = isBatchOperation
- controller.type = .add
- controller.currentType = self.currentBackgroundType.rawValue
- controller.pdfDocument = cdocument
- controller.cancelAction = { [weak self] controller in
- self?.km_endSheet()
- }
- self.km_beginSheet(windowC: controller)
- controller.operateCallBack = { controller, background, countType in
- self.currentBackgroundType = KMBackgroundType(rawValue: countType) ?? .color
- self._loadData()
- var haveBackgrounds = false
- if self.currentBackgroundType == .color {
- self._currentColorData = background
- if self._textTemplateArray?.count ?? 0 > 0 {
- haveBackgrounds = true
- }
- } else if self.currentBackgroundType == .file {
- self._currentImageData = background
- if self._fileTemplateArray?.count ?? 0 > 0 {
- haveBackgrounds = true
- }
- }
- if haveBackgrounds {
- let indexPath = IndexPath(item: 0, section: 0)
- var set = Set<IndexPath>()
- set.insert(indexPath)
- self.collectionView.selectItems(at: set, scrollPosition: .bottom)
- }
- self._postNotification()
- }
- }else{
- let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
- controller.isBatch = isBatchOperation
- controller.type = .add
- controller.pdfDocument = cdocument
- controller.currentType = self.currentType.rawValue
- controller.cancelAction = { [weak self] wmWindowC in
- self?.km_endSheet()
- }
- controller.operateCallBack = { [unowned self] controller, watermark, countType in
- self.currentType = KMWatermarkType(rawValue: countType) ?? .txt
- self._loadData()
- var haveWaters = false
- if self.currentType == .txt {
- self._currentTextData = watermark
- if self._textTemplateArray?.count ?? 0 > 0 {
- haveWaters = true
- }
- } else if self.currentType == .file {
- self._currentFileData = watermark
- if self._fileTemplateArray?.count ?? 0 > 0 {
- haveWaters = true
- }
- }
- if haveWaters {
- let indexPath = IndexPath(item: 0, section: 0)
- var set = Set<IndexPath>()
- set.insert(indexPath)
- self.collectionView.selectItems(at: set, scrollPosition: .bottom)
- }
- self._postNotification()
- }
-
- self.km_beginSheet(windowC: controller)
- }
- }
-
- }
- // MARK: - Private Methods
- extension KMBatchOperateAddWatermarkViewController {
- private func _localizedlanguage() {
- self.addButton.title = " " + KMLocalizedString("Add Template", nil)
- self.addButton.imageHugsTitle = true
- self.fileButton.title = KMLocalizedString("File", nil)
- self.actionButton.title = KMLocalizedString("Apply", nil)
- if (!self.isBackground) {
- self.titleLabel.stringValue = KMLocalizedString("Watermark", nil)
- self.textButton.title = KMLocalizedString("Text", nil)
- } else {
- self.titleLabel.stringValue = KMLocalizedString("Background", nil)
- self.textButton.title = KMLocalizedString("Color", nil)
- }
- if (self.onlyManagerTemplate) {
- self.titleLabel.isHidden = true
- self.addButton.isHidden = false
- self.managerTemplateTitleLabel.isHidden = false
- } else {
- self.titleLabel.isHidden = false
- self.addButton.isHidden = false
- self.managerTemplateTitleLabel.isHidden = true
- }
-
- self.managerTemplateTitleLabel.stringValue = KMLocalizedString("Manage Templates", nil)
- }
-
- private func _configuiUI() {
- self.topBaseView.wantsLayer = true
- self.topBaseView.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
-
- self.collectionView.wantsLayer = true
- self.collectionView.isSelectable = true
- self.collectionView.allowsMultipleSelection = false
- self.collectionView.allowsEmptySelection = false
- self.collectionView.enclosingScrollView?.borderType = .noBorder
- self.collectionView.enclosingScrollView?.drawsBackground = false
-
- self.collectionView.register(KMWatermarkCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"))
- let v = NSView(frame: NSMakeRect(0, 0, 100, 100))
- v.wantsLayer = true
- v.layer?.backgroundColor = .clear
-
- self.collectionView.backgroundView = v;
- self.collectionView.enclosingScrollView?.horizontalScrollElasticity = .none
- self.collectionView.enclosingScrollView?.verticalScrollElasticity = .none
-
- self.titleLabel.font = .boldSystemFont(ofSize: 14)
- self.titleLabel.textColor = KMAppearance.Layout.h0Color()
-
- self.actionButton.font = .systemFont(ofSize: 13)
- self.fileButton.font = .systemFont(ofSize: 13)
- self.textButton.font = .systemFont(ofSize: 13)
-
- self.actionButton.wantsLayer = true
- self.addButton.wantsLayer = true
- if (self.onlyManagerTemplate) {
-
- self.topHeightConstraint.constant = 40
- self.addButton.imagePosition = .imageLeft
- self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
- self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
-
- self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
- self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
- self.actionButton.imagePosition = .noImage
- } else {
- self.topHeightConstraint.constant = 0
- self.addButton.imagePosition = .imageLeft
- self.addButton.image = NSImage(named: "KMImageNameHeaderFooterAddBtn")
- self.addButton.setTitleColor(KMAppearance.Layout.h0Color())
-
- self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
- self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
- self.actionButton.imagePosition = .noImage
- }
-
- if (!self.onlyManagerTemplate) {
- if let cnt = self.files?.count, cnt > 0 {
- self._haveFiles = true
- } else {
- self._haveFiles = false
- }
- self._updateActionButtonbackgroundColor()
- }
-
- self.addButton.layer?.cornerRadius = 1.0
- self.actionButton.layer?.cornerRadius = 1.0
-
- self.actionButton.layer?.cornerRadius = 1.0
-
- self.textButton.wantsLayer = true
- self.fileButton.wantsLayer = true
- self.textBox.fillColor = KMAppearance.Interactive.a0Color()
- self.fileBox.fillColor = KMAppearance.Layout.l_1Color()
-
- self.textButton.setTitleColor(KMAppearance.Layout.w0Color())
- self.fileButton.setTitleColor(KMAppearance.Layout.h0Color())
- self.managerTemplateTitleLabel.font = .systemFont(ofSize: 14)
- self.managerTemplateTitleLabel.textColor = KMAppearance.Layout.h0Color()
-
- self.view.addSubview(self.blankView)
- self.blankView.mas_makeConstraints { make in
- make?.top.equalTo()(self.topBaseView.mas_bottom)
- make?.left.right().equalTo()(self.view)
- make?.bottom.equalTo()(self.bottomBaseView.mas_top)
- make?.height.greaterThanOrEqualTo()(200)
- }
- self.blankView.titleLabel.stringValue = KMLocalizedString("No Templates", nil)
-
- if (self.isBackground) {
- self.blankView.imageView.image = NSImage(named: KMImageNameEmptyBackground)
- }
- self.blankView.wantsLayer = true
- let menu = NSMenu()
- if(!self.isBackground) {
- if(!self.isBatchOperation) {
- _ = menu.addItem(title: KMLocalizedString("Batch Add Watermarks", nil), action: #selector(_buttonItemClick_addBatch), target: self)
- }
- _ = menu.addItem(title: KMLocalizedString("Remove All Watermark Templates", nil), action: #selector(_buttonItemClick_CleanAll), target: self)
- } else {
- if(!self.isBatchOperation) {
- _ = menu.addItem(title: KMLocalizedString("Batch Add Background", nil), action: #selector(_buttonItemClick_addBatch), target: self)
- }
- _ = menu.addItem(title: KMLocalizedString("Remove All Background Templates", nil), action: #selector(_buttonItemClick_CleanAll), target: self)
- }
- self.view.menu = menu;
- }
-
- private func _loadData() {
- self._textTemplateArray = []
- self._fileTemplateArray = []
- if (!self.isBackground) {
- let watermarkArr = KMWatermarkManager.defaultManager.watermarks
- for i in 0 ..< watermarkArr.count {
- let watermark = watermarkArr[i]
- if ((watermark.image) != nil) {
- self._fileTemplateArray?.append(watermark)
- } else {
- self._textTemplateArray?.append(watermark)
- }
- }
- } else {
- let arr = KMBackgroundManager.defaultManager.datas
- for i in 0 ..< arr.count {
- let obj = arr[i]
- if (obj.type == .color) {
- self._textTemplateArray?.append(obj)
- } else {
- self._fileTemplateArray?.append(obj)
- }
- }
- }
- self.collectionView.reloadData()
- self._updateActionButtonbackgroundColor()
- NotificationCenter.default.addObserver(self, selector: #selector(_batchFilesCountNotification), name: NSNotification.Name("KMBatchFilesCountNotification"), object: nil)
- }
-
- private func _isSelectIndex() -> Bool {
- var indexSet = false
- if (!self.isBackground) {
- if(self.currentType == .txt) {
- if let data = self._currentTextData {
- indexSet = (self._textTemplateArray ?? []).contains(data)
- }
- }else {
- if let data = self._currentFileData {
- indexSet = (self._fileTemplateArray ?? []).contains(data)
- }
- }
- } else {
- if(self.currentBackgroundType == .color) {
- if let data = self._currentColorData {
- indexSet = (self._textTemplateArray ?? []).contains(data)
- }
- }else {
- if let data = self._currentImageData {
- indexSet = (self._fileTemplateArray ?? []).contains(data)
- }
- }
- }
- return indexSet;
- }
-
- private func _updateActionButtonbackgroundColor() {
- let indexSet = self._isSelectIndex()
- if (self.files?.count ?? 0 > 0 || self.pdfView?.document.documentURL.path.count ?? 0 > 0 ) && indexSet {
- self.actionButton.setTitleColor(KMAppearance.Layout.w0Color())
- self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
- } else {
- self.actionButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
- self.actionButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.6).cgColor
- }
- }
-
- @objc private func _buttonItemClick_addBatch(_ sender: Any?) {
- let baseWindowController = KMBatchOperateBaseWindowController(windowNibName: "KMBatchOperateBaseWindowController")
-
- let file = KMBatchOperateFile(filePath: self.pdfView!.document.documentURL.path, type: self.isBackground ? .AddBackground : .AddWatermark)
-
- baseWindowController.window?.makeKeyAndOrderFront(nil)
- baseWindowController.checkNeedPasswordSwitchToOperateType(operateType: self.isBackground ? .AddBackground : .AddWatermark, files: [file])
- }
-
- @objc private func _buttonItemClick_CleanAll(_ sender: Any?) {
- let alert = NSAlert()
- alert.alertStyle = .warning
- alert.messageText = ""
- alert.informativeText = KMLocalizedString("Are you sure to delete all templates?", nil)
- alert.addButton(withTitle: KMLocalizedString("Delete", nil))
- alert.addButton(withTitle: KMLocalizedString("Cancel", nil))
- alert.beginSheetModal(for: NSApp.mainWindow!) { returnCode in
- if returnCode == .alertFirstButtonReturn {
- self._deleteAllWatermark()
- }
- }
- }
- func editBackground(_ background: KMBackgroundModel) {
- var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
- if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
- filePath = self.pdfView?.document.documentURL.path ?? ""
- }
- let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
-
- let controller = KMBackgroundWindowController(windowNibName: "KMBackgroundWindowController")
- controller.isBatch = isBatchOperation
- controller.type = .edit
- controller.background = background
- controller.currentType = self.currentBackgroundType.rawValue
- controller.pdfDocument = cdocument
- controller.cancelAction = { [weak self] controller in
- self?.km_endSheet()
- }
- self.km_beginSheet(windowC: controller)
- controller.operateCallBack = {[weak self] controller, background, countType in
- self?.currentBackgroundType = KMBackgroundType(rawValue: countType) ?? .color
- self?._loadData()
- var haveBackgrounds = false
- if self?.currentBackgroundType == .color {
- self?._currentColorData = background
- if self?._textTemplateArray?.count ?? 0 > 0 {
- haveBackgrounds = true
- }
- } else if self?.currentBackgroundType == .file {
- self?._currentImageData = background
- if self?._fileTemplateArray?.count ?? 0 > 0 {
- haveBackgrounds = true
- }
- }
- if haveBackgrounds {
- let indexPath = IndexPath(item: 0, section: 0)
- var set = Set<IndexPath>()
- set.insert(indexPath)
- self?.collectionView.selectItems(at: set, scrollPosition: .bottom)
- }
- self?._postNotification()
- }
- }
- private func deleteBackground(_ background: KMBackgroundModel) {
- let _ = KMBackgroundManager.defaultManager.deleteTemplate(model: background)
- self._loadData()
- self._postNotification()
- }
-
- func editWatermark(_ waterMark: KMWatermarkModel) {
- if !isBackground {
- var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
- if self.pdfView?.document.documentURL.path.count ?? 0 > 0 {
- filePath = self.pdfView?.document.documentURL.path ?? ""
- }
-
- let cdocument = CPDFDocument(url: URL(fileURLWithPath: filePath))
- let controller = KMWatermarkWindowController(windowNibName: "KMWatermarkWindowController")
- controller.watermark = waterMark
- controller.isBatch = isBatchOperation
- controller.type = .edit
- controller.pdfDocument = cdocument
- controller.currentType = self.currentType.rawValue
- controller.cancelAction = { [weak self] wmWindowC in
- self?.km_endSheet()
- }
- controller.operateCallBack = {[weak self] controller, watermark, countType in
- self?._loadData()
- var haveWaters = false
- if self?.currentType == .txt {
- self?._currentTextData = watermark
- if self?._textTemplateArray?.count ?? 0 > 0 {
- haveWaters = true
- }
- } else if self?.currentType == .file {
- self?._currentFileData = watermark
- if self?._fileTemplateArray?.count ?? 0 > 0 {
- haveWaters = true
- }
- }
- if haveWaters {
- let indexPath = IndexPath(item: 0, section: 0)
- var set = Set<IndexPath>()
- set.insert(indexPath)
- self?.collectionView.selectItems(at: set, scrollPosition: .bottom)
- }
- self?._postNotification()
- }
- self.km_beginSheet(windowC: controller)
- } else {
- // Handle background case
- }
- }
-
- private func _deleteAllWatermark() {
- if(!self.isBackground) {
- for waterMark in self._textTemplateArray ?? [] {
- _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
- }
- for waterMark in self._fileTemplateArray ?? [] {
- _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark as! KMWatermarkModel)
- }
- } else {
- _ = KMBackgroundManager.defaultManager.deleteAllTemplates()
- }
-
- self._loadData()
- self._postNotification()
- }
-
- private func _deleteWatermark(_ waterMark: KMWatermarkModel) {
- // [[KMWatermarkManager defaultManager] removeWatermarkWithData:waterMark];
- let _ = KMWatermarkManager.defaultManager.removeWatermark(watermark: waterMark)
- self._loadData()
- self._postNotification()
- }
-
- private func _postNotification() {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: "KMBatchOperateWatermarksNotification"), object: self)
- }
-
- @objc private func _watermarksNotification(_ notification: NSNotification) {
- let addWatermark = notification.object as? KMBatchOperateAddWatermarkViewController
- if self.isEqual(to: addWatermark) == false {
- self._loadData()
-
- var haveWaters = false
- if (self.currentType == .txt) {
- if let cnt = self._textTemplateArray?.count, cnt > 0 {
- haveWaters = true
- }
- } else if (self.currentType == .file) {
- if let cnt = self._fileTemplateArray?.count, cnt > 0 {
- haveWaters = true
- }
- }
- if (haveWaters) {
- let indexpath = IndexPath(item: 0, section: 0)
- var set = Set<IndexPath>()
- set.insert(indexpath)
- self.collectionView.selectItems(at: set, scrollPosition: .bottom)
- }
- }
- }
-
- //检查是否有任务文件要加载,如果有,重置任务。如果返回YES,表示有任务并且任务已经重置,否则表示没有任务,需要中断操作
- private func _checkAndResetTask() -> Bool {
- if let cnt = self.files?.count, cnt < 1 {
- return false
- }
- for i in 0 ..< self.files!.count {
- let file = self.files![i]
- if (self.isBackground) {
- file.addBackgroundInfo.resetState()
- } else {
- file.addWatermarkInfo.resetState()
- }
- }
- return true
- }
-
- private func _choosePathAndBeginOperation(_ obj: Any?) {
- let openPanel = NSOpenPanel()
- openPanel.canChooseFiles = false
- openPanel.canChooseDirectories = true
- openPanel.canCreateDirectories = true
- openPanel.beginSheetModal(for: self.view.window!) { result in
- if (result == .OK) {
- for fileURL in openPanel.urls {
- self.choosePath = fileURL.path
- if (!self.isBackground) {
- self._beginAddWatermark(obj as! KMWatermarkModel)
- } else {
- self._beginAddBackground(obj as! KMBackgroundModel)
- }
- }
- }
- }
- }
-
- private func _beginAddWatermark(_ watermark: KMWatermarkModel) {
- self.hiddenWindowCloseButtonIfNeeded()
- self.successFilePathURLArray?.removeAll()
- for i in 0 ..< self.files!.count {
- let file = self.files![i]
- file.addWatermarkInfo.savePath = self.choosePath
-
- let operation = KMBatchAddWatermarkOperation(file: file, waterMarkM: watermark)
- operation.delegate = self
- self.queue?.addOperation(operation)
- }
- if let cnt = self.queue?.operations.count, cnt > 0 {
- self.interfaceStatus = .Processing
- }
- }
-
- private func _beginAddBackground(_ background: KMBackgroundModel) {
- self.hiddenWindowCloseButtonIfNeeded()
- self.successFilePathURLArray?.removeAll()
- for i in 0 ..< self.files!.count {
- let file = self.files![i]
- file.addBackgroundInfo.savePath = self.choosePath
- let operation = KMBatchAddBackgroundOperation(file: file, backgroundM: background)
- operation.delegate = self
- self.queue?.addOperation(operation)
- }
- if let cnt = self.queue?.operations.count, cnt > 0 {
- self.interfaceStatus = .Processing
- }
- }
-
- @objc private func _batchFilesCountNotification(_ notification: NSNotification) {
- let files: Array? = notification.object as? [KMBatchOperateFile]
- if let data = files?.first {
- if data.currentOperateType != self.operateType {
- return
- }
- }
- let viewC = notification.userInfo?[kObjectKey] as? NSViewController
- if let data = self.view.window?.isEqual(to: viewC?.view.window), data == false {
- return
- }
-
- if (!self.onlyManagerTemplate) {
- let files: Array? = notification.object as? [KMBatchOperateFile]
- self.files? = files ?? []
-
- if (files?.count ?? 0 > 0) {
- self._haveFiles = true
- } else {
- self._haveFiles = false
- }
- self._updateActionButtonbackgroundColor()
- }
- }
- }
- extension KMBatchOperateAddWatermarkViewController: NSMenuItemValidation {
- func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
- let action = menuItem.action
- if (action == #selector(_buttonItemClick_CleanAll)) {
- if (self._textTemplateArray!.count == 0 && self._fileTemplateArray!.count == 0) {
- return false
- }
- return true
- }
- return true
- }
- }
- extension KMBatchOperateAddWatermarkViewController: NSCollectionViewDelegate, NSCollectionViewDataSource, NSCollectionViewDelegateFlowLayout {
- func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
- var count = 0
- if (!self.isBackground) {
- if (self.currentType == .txt) {
- count = self._textTemplateArray?.count ?? 0
- } else {
- count = self._fileTemplateArray?.count ?? 0
- }
- } else {
- if (self.currentBackgroundType == .color) {
- count = self._textTemplateArray?.count ?? 0
- } else {
- count = self._fileTemplateArray?.count ?? 0
- }
- }
-
- self.blankView.isHidden = count != 0
- return count
- }
-
- func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
- let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMWatermarkCollectionViewItem"), for: indexPath)
- if (!self.isBackground) {
- var waterMark: KMWatermarkModel?
- if (self.currentType == .txt) {
- waterMark = (self._textTemplateArray![indexPath.item] as! KMWatermarkModel)
- if(self._currentTextData == waterMark) {
- item.isSelected = true
- }else {
- item.isSelected = false
- }
- } else {
- waterMark = (self._fileTemplateArray![indexPath.item] as! KMWatermarkModel)
- if(self._currentFileData == waterMark) {
- item.isSelected = true
- }else{
- item.isSelected = false
- }
- }
- // __block typeof(self) blockSelf = self;
- let _item = item as? KMWatermarkCollectionViewItem
- _item?.updateInterface(waterMark!)
- _item?.waterMarkOprateCallback = { [unowned self] type, wm in
- if (wm != nil) {
- if (type == .Edit) {
- if (self.currentType == .txt) {
- self._currentTextData = waterMark
- } else {
- self._currentFileData = waterMark
- }
- collectionView.reloadData()
- self.editWatermark(waterMark!)
- } else {
- self._deleteWatermark(waterMark!)
- }
- }
- };
- } else {
- var background: KMBackgroundModel?
- if (self.currentBackgroundType == .color) {
- background = (self._textTemplateArray![indexPath.item] as! KMBackgroundModel)
- if(self._currentColorData == background) {
- item.isSelected = true
- }else {
- item.isSelected = false
- }
- } else {
- background = (self._fileTemplateArray![indexPath.item] as! KMBackgroundModel)
- if(self._currentImageData == background) {
- item.isSelected = true
- }else {
- item.isSelected = false
- }
- }
-
- let _item = item as? KMWatermarkCollectionViewItem
- _item?.updateBackgroundInterface(background!)
- // __block typeof(self) blockSelf = self;
- _item?.backgroundOperateCallback = { [unowned self] type, bg in
- if (type == .Edit) {
- if (self.currentBackgroundType == .color) {
- self._currentColorData = background
- } else {
- self._currentImageData = background
- }
- collectionView.reloadData()
- self.editBackground(background!)
- } else {
- self.deleteBackground(background!)
- }
- } ;
- }
- self._updateActionButtonbackgroundColor()
-
- return item
- }
-
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
- if (collectionView.frame.size.width < 240) {
- return NSMakeSize(0, 0)
- } else {
- return NSMakeSize(104, 167)
- }
- }
-
- func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
- if (collectionView.frame.size.width < 32) {
- return NSEdgeInsetsMake(0, 0, 0, 0)
- }
- return NSEdgeInsetsMake(0, 16, 0, 16)
- }
-
- func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
- let indexPath = indexPaths.first
- guard let _indexPath = indexPath else {
- return
- }
- if (indexPath!.item < 0) {
- return
- }
- if (self.isBackground) {
- var background: KMBackgroundModel?
- if (self.currentBackgroundType == .color) {
- background = self._textTemplateArray![_indexPath.item] as? KMBackgroundModel
- self._currentColorData = background
- } else {
- background = self._fileTemplateArray![_indexPath.item] as? KMBackgroundModel
- self._currentImageData = background
- }
- for i in 0 ..< self.files!.count {
- let file = self.files![i]
- file.addBackgroundInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (background?.pageRangeType.rawValue)!)!
- file.addBackgroundInfo.pageRangeString = background?.pageRangeString
- }
-
- } else {
- var waterMark: KMWatermarkModel?
- if (self.currentType == .txt) {
- waterMark = self._textTemplateArray![_indexPath.item] as? KMWatermarkModel
- self._currentTextData = waterMark
- } else {
- waterMark = self._fileTemplateArray![_indexPath.item] as? KMWatermarkModel
- self._currentFileData = waterMark
- }
-
- for i in 0 ..< self.files!.count {
- let file = self.files![i]
- file.addWatermarkInfo.pageChoice = KMBatchOperatePageChoice.init(rawValue: (waterMark?.pageRangeType.rawValue)!)!
- file.addWatermarkInfo.pageRangeString = waterMark?.pageRangeString
- }
- }
- NotificationCenter.default.post(name: NSNotification.Name("kNeedChangePageRangeNotification"), object: nil)
- collectionView.reloadData()
- self._updateActionButtonbackgroundColor()
- }
- }
|