|
@@ -7,22 +7,540 @@
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
+enum KMBackgroundManagerType: Int {
|
|
|
+ case add = 0
|
|
|
+ case edit
|
|
|
+ case use
|
|
|
+}
|
|
|
+
|
|
|
+typealias KMAddBackgroundViewOperateCallBack = (_ background: KMBackgroundModel, _ countType: Int) -> ()
|
|
|
typealias KMAddBackgroundViewBatchAction = (_ view: KMAddBackgroundView, _ files: [KMFileAttribute]) -> Void
|
|
|
typealias KMAddBackgroundViewCancelAction = (_ view: KMAddBackgroundView) -> Void
|
|
|
|
|
|
-
|
|
|
-class KMAddBackgroundView: KMBaseXibView {
|
|
|
+class KMAddBackgroundView: KMBaseXibView, NSComboBoxDelegate {
|
|
|
+
|
|
|
+ @IBOutlet weak var pdfView: KMWatermarkPDFView!
|
|
|
+ @IBOutlet weak var previousButton: NSButton!
|
|
|
+ @IBOutlet weak var nextButton: NSButton!
|
|
|
+ @IBOutlet weak var currentPageIndexTextField: NSTextField!
|
|
|
+ @IBOutlet weak var totalPageCountlabel: NSTextField!
|
|
|
+
|
|
|
+ @IBOutlet weak var typeBox: NSBox!
|
|
|
+ @IBOutlet weak var colorButton: NSButton!
|
|
|
+ @IBOutlet weak var colorWell: NSColorWell!
|
|
|
+ @IBOutlet weak var fileButton: NSButton!
|
|
|
+
|
|
|
+ @IBOutlet weak var filePathLabel: NSTextField!
|
|
|
+ @IBOutlet weak var browseButton: NSButton!
|
|
|
+ @IBOutlet weak var ratioLabel: NSTextField!
|
|
|
+ @IBOutlet weak var ratioTextField: NSTextField!
|
|
|
+ @IBOutlet weak var ratioStepper: NSStepper!
|
|
|
+
|
|
|
+ @IBOutlet weak var appearanceBox: NSBox!
|
|
|
+ @IBOutlet weak var angleLabel: NSTextField!
|
|
|
+ @IBOutlet weak var angleTextField: NSTextField!
|
|
|
+ @IBOutlet weak var angleStepper: NSStepper!
|
|
|
+ @IBOutlet weak var left45IndicateView: KMAngleIndicateView!
|
|
|
+ @IBOutlet weak var horizontalIndicateView: KMAngleIndicateView!
|
|
|
+ @IBOutlet weak var right45IndicateView: KMAngleIndicateView!
|
|
|
+ @IBOutlet weak var alphaLabel: NSTextField!
|
|
|
+ @IBOutlet weak var alphaSlider: NSSlider!
|
|
|
+ @IBOutlet weak var alphaTextField: NSTextField!
|
|
|
+ @IBOutlet weak var alphaStepper: NSStepper!
|
|
|
+ @IBOutlet weak var postionView: KMPostionIndicateView!
|
|
|
+ @IBOutlet weak var verticalGapLabel: NSTextField!
|
|
|
+ @IBOutlet weak var verticalGapTextField: NSTextField!
|
|
|
+ @IBOutlet weak var verticalStepper: NSStepper!
|
|
|
+ @IBOutlet weak var horizontalGapLabel: NSTextField!
|
|
|
+ @IBOutlet weak var horizontalGapTextField: NSTextField!
|
|
|
+ @IBOutlet weak var horizontalStepper: NSStepper!
|
|
|
+ @IBOutlet weak var pageRangeComboBox: NSComboBox!
|
|
|
+ @IBOutlet weak var pageRangeLabel: NSTextField!
|
|
|
+
|
|
|
+ @IBOutlet weak var saveToTemplateButton: NSButton!
|
|
|
+ @IBOutlet weak var templateNameLabel: NSTextField!
|
|
|
+ @IBOutlet weak var templateNameTextField: NSTextField!
|
|
|
+
|
|
|
@IBOutlet weak var doneButton: NSButton!
|
|
|
@IBOutlet weak var cancelButton: NSButton!
|
|
|
@IBOutlet weak var batchButton: NSButton!
|
|
|
|
|
|
+ var background: KMBackgroundModel = KMBackgroundModel()
|
|
|
+ var originalBackground: KMBackgroundModel = KMBackgroundModel()
|
|
|
+ var filePath: String = Bundle.main.path(forResource: "Quick Start Guide", ofType: "pdf") ?? ""
|
|
|
+ var password: String = ""
|
|
|
+ var type: KMBackgroundManagerType = .add
|
|
|
+ var pdfDocument: CPDFDocument? {
|
|
|
+ didSet {
|
|
|
+ self.reloadData()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ var backgroundType: KMBackgroundType!
|
|
|
+
|
|
|
+
|
|
|
+ var initialID: String!
|
|
|
+ var currentType: Int!
|
|
|
+
|
|
|
var cancelAction: KMAddBackgroundViewCancelAction?
|
|
|
var batchAction: KMAddBackgroundViewCancelAction?
|
|
|
+ var operateCallBack: KMAddBackgroundViewOperateCallBack?
|
|
|
+
|
|
|
+ var onlyManagerTemplate: Bool = false
|
|
|
+
|
|
|
+ // MARK: - Dealloc
|
|
|
+
|
|
|
+ deinit {
|
|
|
+ NotificationCenter.default.removeObserver(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ // MARK: - Init Methods
|
|
|
+
|
|
|
+ convenience init?(baseFile filePath: String, background backgroundObject: KMBackgroundModel, password: String, type: KMBackgroundManagerType, fileType countType: Int) {
|
|
|
+ self.init()
|
|
|
+ self.filePath = filePath
|
|
|
+ self.password = password
|
|
|
+ self.background = backgroundObject
|
|
|
+ self.originalBackground = backgroundObject
|
|
|
+ self.initialID = backgroundObject.backgroundID
|
|
|
+ self.type = type
|
|
|
+ self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: self.filePath))
|
|
|
+
|
|
|
+ if pdfDocument!.isLocked {
|
|
|
+ pdfDocument!.unlock(withPassword: password)
|
|
|
+ }
|
|
|
+
|
|
|
+ if pdfDocument!.isLocked {
|
|
|
+ return nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override func setup() {
|
|
|
+ pdfView.background = background
|
|
|
+ pdfView.document = pdfDocument
|
|
|
+ pdfView.autoScales = true
|
|
|
+// pdfView.documentView?.enclosingScrollView?.hasVerticalScroller = false
|
|
|
+// pdfView.documentView?.enclosingScrollView?.hasHorizontalScroller = false
|
|
|
+
|
|
|
+ for i in 0..<3 {
|
|
|
+ for j in 0..<3 {
|
|
|
+ if i == Int(background.horizontalMode) && j == Int(background.verticalMode) {
|
|
|
+ postionView.style = KMPositionIndicateViewStyle(rawValue: i + 3 * j)!
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ postionView.styleChangedCallBack = { [weak self] in
|
|
|
+ guard let self = self else { return }
|
|
|
+ self.background.horizontalMode = self.postionView.style.rawValue % 3
|
|
|
+ self.background.verticalMode = self.postionView.style.rawValue / 3
|
|
|
+ self.pdfView.needsDisplay = true
|
|
|
+ if self.filePathLabel.stringValue.count > 0 {
|
|
|
+ self.doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ currentPageIndexTextField.stringValue = "1"
|
|
|
+// let numberFormatter = currentPageIndexTextField.formatter as! NumberFormatter
|
|
|
+// numberFormatter.maximum = NSNumber(value: pdfDocument.pageCount)
|
|
|
+
|
|
|
+
|
|
|
+ left45IndicateView.style = .left45
|
|
|
+ left45IndicateView.touchCallBack = { [weak self] in
|
|
|
+ guard let self = self else { return }
|
|
|
+ self.background.rotation = 45
|
|
|
+ self.angleStepper.doubleValue = 45
|
|
|
+ self.angleTextField.stringValue = "\(45)"
|
|
|
+ self.checkAngle()
|
|
|
+ if self.filePathLabel.stringValue.count > 0 {
|
|
|
+ self.doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ self.pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+
|
|
|
+ horizontalIndicateView.style = .horizontal
|
|
|
+ horizontalIndicateView.touchCallBack = { [weak self] in
|
|
|
+ guard let self = self else { return }
|
|
|
+ self.background.rotation = 0
|
|
|
+ self.angleStepper.doubleValue = 0
|
|
|
+ self.angleTextField.stringValue = "\(0)"
|
|
|
+ self.checkAngle()
|
|
|
+ if self.filePathLabel.stringValue.count > 0 {
|
|
|
+ self.doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ self.pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+
|
|
|
+ right45IndicateView.style = .right45
|
|
|
+ right45IndicateView.touchCallBack = { [weak self] in
|
|
|
+ guard let self = self else { return }
|
|
|
+ self.background.rotation = -45
|
|
|
+ self.angleStepper.doubleValue = -45
|
|
|
+ self.angleTextField.stringValue = "\(-45)"
|
|
|
+ self.checkAngle()
|
|
|
+ if self.filePathLabel.stringValue.count > 0 {
|
|
|
+ self.doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ self.pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+
|
|
|
+ checkAngle()
|
|
|
+
|
|
|
+ typeBox.titleFont = NSFont.systemFont(ofSize: 13)
|
|
|
+
|
|
|
+ colorWell.color = background.color ?? NSColor.red
|
|
|
+ templateNameTextField.stringValue = background.backgroundID
|
|
|
+
|
|
|
+ appearanceBox.titleFont = NSFont.systemFont(ofSize: 13)
|
|
|
+
|
|
|
+ if !onlyManagerTemplate {
|
|
|
+ saveToTemplateButton.isEnabled = false
|
|
|
+ }
|
|
|
+
|
|
|
+ if type == .use {
|
|
|
+ saveToTemplateButton.isHidden = true
|
|
|
+ saveToTemplateButton.state = .off
|
|
|
+ } else {
|
|
|
+ saveToTemplateButton.isHidden = false
|
|
|
+ saveToTemplateButton.state = .on
|
|
|
+ }
|
|
|
+
|
|
|
+ pageRangeComboBox.removeAllItems()
|
|
|
+ pageRangeComboBox.addItems(withObjectValues: [
|
|
|
+ NSLocalizedString("All Pages", comment: ""),
|
|
|
+ NSLocalizedString("Odd Pages Only", comment: ""),
|
|
|
+ NSLocalizedString("Even Pages Only", comment: ""),
|
|
|
+ NSLocalizedString("e.g. 1,3-5,10", comment: "")
|
|
|
+ ])
|
|
|
+ pageRangeComboBox.placeholderString = NSLocalizedString("e.g. 1,3-5,10", comment: "")
|
|
|
+ pageRangeComboBox.delegate = nil
|
|
|
+ pageRangeComboBox.selectItem(at: 0)
|
|
|
+ pageRangeComboBox.isEditable = false
|
|
|
+ pageRangeComboBox.delegate = self
|
|
|
+ }
|
|
|
+
|
|
|
+ override func addNotification() {
|
|
|
+ NotificationCenter.default.addObserver(self, selector: #selector(pageChangeNotification), name: NSNotification.Name("PDFViewPageChangedNotification"), object: self.pdfView)
|
|
|
+ }
|
|
|
+ override func updateLanguage() {
|
|
|
+ typeBox.title = NSLocalizedString("Source", comment: "")
|
|
|
+
|
|
|
+ colorButton.title = NSLocalizedString("Color", comment: "")
|
|
|
+ fileButton.title = NSLocalizedString("File", comment: "")
|
|
|
+ browseButton.title = NSLocalizedString("Choose...", comment: "")
|
|
|
+ ratioLabel.stringValue = "\(NSLocalizedString("Ratio", comment: "")):"
|
|
|
+
|
|
|
+ appearanceBox.title = NSLocalizedString("Appearance", comment: "")
|
|
|
+ angleLabel.stringValue = "\(NSLocalizedString("Rotation", comment: "")):"
|
|
|
+ alphaLabel.stringValue = "\(NSLocalizedString("Opacity", comment: "")):"
|
|
|
+ pageRangeLabel.stringValue = "\(NSLocalizedString("Page Range", comment: "")):"
|
|
|
+ horizontalGapLabel.stringValue = "X:"
|
|
|
+ verticalGapLabel.stringValue = "Y:"
|
|
|
+
|
|
|
+ saveToTemplateButton.title = NSLocalizedString("Add to Template", comment: "")
|
|
|
+ }
|
|
|
+
|
|
|
+ private func checkAngle() {
|
|
|
+ left45IndicateView.isSelcted = false
|
|
|
+ horizontalIndicateView.isSelcted = false
|
|
|
+ right45IndicateView.isSelcted = false
|
|
|
+
|
|
|
+ if background.rotation == -45 {
|
|
|
+ right45IndicateView.isSelcted = true
|
|
|
+ } else if background.rotation == 0 {
|
|
|
+ horizontalIndicateView.isSelcted = true
|
|
|
+ } else if background.rotation == 45 {
|
|
|
+ left45IndicateView.isSelcted = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override func reloadData() {
|
|
|
+ guard let pdfDocument = pdfDocument else { return }
|
|
|
+
|
|
|
+ totalPageCountlabel.stringValue = "/ \(pdfDocument.pageCount)"
|
|
|
+
|
|
|
+ filePathLabel.stringValue = background.imagePath
|
|
|
+ filePathLabel.placeholderString = NSLocalizedString("Select a File", comment: "")
|
|
|
+ angleStepper.doubleValue = Double(-background.rotation)
|
|
|
+ angleTextField.stringValue = "\(angleStepper.doubleValue)"
|
|
|
+ alphaSlider.doubleValue = background.opacity
|
|
|
+ alphaStepper.doubleValue = background.opacity
|
|
|
+ let opacity = round(background.opacity * 100) / 100
|
|
|
+ alphaTextField.stringValue = "\(Int(opacity * 100))%"
|
|
|
+ ratioStepper.doubleValue = background.scale
|
|
|
+ ratioTextField.stringValue = "\(Int(background.scale * 100))%"
|
|
|
+ if currentType == 0 {
|
|
|
+ changeTypeBoxState(true)
|
|
|
+ } else {
|
|
|
+ changeTypeBoxState(false)
|
|
|
+ }
|
|
|
+ pageRangeComboBox.delegate = nil
|
|
|
+ switch background.pageRangeType {
|
|
|
+ case .all:
|
|
|
+ pageRangeComboBox.isEditable = false
|
|
|
+ pageRangeComboBox.selectItem(at: 0)
|
|
|
+ case .odd:
|
|
|
+ pageRangeComboBox.isEditable = false
|
|
|
+ pageRangeComboBox.selectItem(at: 1)
|
|
|
+ case .even:
|
|
|
+ pageRangeComboBox.isEditable = false
|
|
|
+ pageRangeComboBox.selectItem(at: 2)
|
|
|
+ case .other:
|
|
|
+ pageRangeComboBox.isEditable = true
|
|
|
+ pageRangeComboBox.selectItem(at: 3)
|
|
|
+ pageRangeComboBox.stringValue = background.pagesString
|
|
|
+ window?.makeFirstResponder(pageRangeComboBox)
|
|
|
+ }
|
|
|
+ pageRangeComboBox.delegate = self
|
|
|
+ verticalStepper.doubleValue = background.verticalSpace
|
|
|
+ verticalGapTextField.stringValue = "\(verticalStepper.doubleValue)"
|
|
|
+ horizontalStepper.doubleValue = background.horizontalSpace
|
|
|
+ horizontalGapTextField.stringValue = "\(horizontalStepper.doubleValue)"
|
|
|
+ }
|
|
|
+
|
|
|
+ private func changeTypeBoxState(_ isColor: Bool) {
|
|
|
+ if isColor {
|
|
|
+ currentType = 0
|
|
|
+ colorButton.state = .on
|
|
|
+ colorWell.isEnabled = true
|
|
|
+ fileButton.state = .off
|
|
|
+ browseButton.isEnabled = false
|
|
|
+ ratioTextField.isEnabled = false
|
|
|
+ ratioStepper.isEnabled = false
|
|
|
+ background.color = colorWell.color
|
|
|
+ background.type = .color
|
|
|
+ background.scale = 1
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ } else {
|
|
|
+ currentType = 1
|
|
|
+ colorButton.state = .off
|
|
|
+ colorWell.isEnabled = false
|
|
|
+ fileButton.state = .on
|
|
|
+ browseButton.isEnabled = true
|
|
|
+ ratioTextField.isEnabled = true
|
|
|
+ ratioStepper.isEnabled = true
|
|
|
+ background.color = nil
|
|
|
+ background.type = .file
|
|
|
+ background.scale = ratioStepper.doubleValue
|
|
|
+ doneButton.isEnabled = filePathLabel.stringValue.count > 0
|
|
|
+ }
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+
|
|
|
+ // Other methods...
|
|
|
|
|
|
- override func draw(_ dirtyRect: NSRect) {
|
|
|
- super.draw(dirtyRect)
|
|
|
+ @objc func controlTextDidEndEditing(_ notification: Notification) {
|
|
|
+ guard let textField = notification.object as? NSTextField else { return }
|
|
|
+
|
|
|
+ switch textField {
|
|
|
+ case ratioTextField:
|
|
|
+ let formatter = textField.formatter as? NumberFormatter
|
|
|
+ if let floatValue = formatter?.number(from: textField.stringValue)?.floatValue {
|
|
|
+ ratioStepper.doubleValue = Double(floatValue)
|
|
|
+ background.scale = Double(floatValue)
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+ case angleTextField:
|
|
|
+ if let integerValue = Int(textField.stringValue) {
|
|
|
+ background.rotation = -integerValue
|
|
|
+ angleStepper.doubleValue = Double(-background.rotation)
|
|
|
+ checkAngle()
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+ case alphaTextField:
|
|
|
+ let formatter = textField.formatter as? NumberFormatter
|
|
|
+ if let floatValue = formatter?.number(from: textField.stringValue)?.floatValue {
|
|
|
+ alphaSlider.doubleValue = Double(floatValue)
|
|
|
+ alphaStepper.doubleValue = Double(floatValue)
|
|
|
+ background.opacity = Double(floatValue)
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+ case pageRangeComboBox:
|
|
|
+ if pageRangeComboBox.indexOfSelectedItem == -1 {
|
|
|
+ if !checkPageRangeValidate(pageRangeComboBox.stringValue) {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = "\(pdfDocument!.documentURL.lastPathComponent) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
|
|
|
+ alert.runModal()
|
|
|
+ window?.makeFirstResponder(pageRangeComboBox)
|
|
|
+ return
|
|
|
+ } else {
|
|
|
+ background.pagesString = pageRangeComboBox.stringValue
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ case verticalGapTextField:
|
|
|
+ if let integerValue = Int(verticalGapTextField.stringValue) {
|
|
|
+ background.verticalSpace = CGFloat(integerValue)
|
|
|
+ verticalStepper.doubleValue = background.verticalSpace
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+ case horizontalGapTextField:
|
|
|
+ if let integerValue = Int(horizontalGapTextField.stringValue) {
|
|
|
+ background.horizontalSpace = CGFloat(integerValue)
|
|
|
+ horizontalStepper.doubleValue = background.horizontalSpace
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+ case currentPageIndexTextField:
|
|
|
+ if let pageIndex = Int(currentPageIndexTextField.stringValue), let page = pdfDocument!.page(at: UInt(pageIndex - 1)) {
|
|
|
+ pdfView.go(to: page)
|
|
|
+ }
|
|
|
+ default:
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // Drawing code here.
|
|
|
+ @objc func comboBoxSelectionDidChange(_ notification: Notification) {
|
|
|
+ guard notification.object as? NSComboBox == pageRangeComboBox else { return }
|
|
|
+
|
|
|
+ pageRangeComboBox.isEditable = false
|
|
|
+ switch pageRangeComboBox.indexOfSelectedItem {
|
|
|
+ case 0:
|
|
|
+ background.pageRangeType = .all
|
|
|
+ case 1:
|
|
|
+ background.pageRangeType = .odd
|
|
|
+ case 2:
|
|
|
+ background.pageRangeType = .even
|
|
|
+ default:
|
|
|
+ background.pageRangeType = .other
|
|
|
+ pageRangeComboBox.stringValue = ""
|
|
|
+ pageRangeComboBox.isEditable = true
|
|
|
+ window?.makeFirstResponder(pageRangeComboBox)
|
|
|
+ }
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func checkPageRangeValidate(_ pageRangeString: String) -> Bool {
|
|
|
+ let fileAttribute = KMFileAttribute()
|
|
|
+ fileAttribute.filePath = pdfDocument!.documentURL.path
|
|
|
+ fileAttribute.bAllPage = false
|
|
|
+ fileAttribute.pagesString = pageRangeComboBox.stringValue
|
|
|
+
|
|
|
+ return fileAttribute.fetchSelectPages().count != 0
|
|
|
+ }
|
|
|
+
|
|
|
+ func saveAsPDF(with background: KMBackgroundModel, to path: String, autoOpen: Bool) {
|
|
|
+ DispatchQueue.global(qos: .default).async { [unowned self] in
|
|
|
+ var filePath = self.pdfDocument!.documentURL?.path
|
|
|
+ let password = self.password
|
|
|
+ if filePath == nil {
|
|
|
+ let writeSuccess = self.pdfDocument!.write(to: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))
|
|
|
+ if writeSuccess {
|
|
|
+ self.pdfDocument = CPDFDocument(url: URL(fileURLWithPath: kNewDocumentTempSavePath(NSLocalizedString("Untitled", comment: ""))))!
|
|
|
+ filePath = self.pdfDocument!.documentURL?.path
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ let document = CPDFDocument(url: URL(fileURLWithPath: filePath!))!
|
|
|
+ document.unlock(withPassword: password)
|
|
|
+
|
|
|
+
|
|
|
+ let tBackground: CPDFBackground = document.background()
|
|
|
+ tBackground.opacity = background.opacity
|
|
|
+ tBackground.scale = background.scale
|
|
|
+ tBackground.rotation = CGFloat(-background.rotation)
|
|
|
+ tBackground.horizontalAlignment = UInt(background.horizontalMode)
|
|
|
+ tBackground.verticalAlignment = UInt(background.verticalMode)
|
|
|
+ tBackground.xOffset = background.horizontalSpace
|
|
|
+ tBackground.yOffset = background.verticalSpace
|
|
|
+
|
|
|
+ if let color = background.color {
|
|
|
+ tBackground.color = color
|
|
|
+ tBackground.type = .color
|
|
|
+ } else if background.imagePath.count != 0 {
|
|
|
+ let image = NSImage(contentsOfFile: background.imagePath)!
|
|
|
+ tBackground.setImage(image)
|
|
|
+ tBackground.type = .image
|
|
|
+ }
|
|
|
+
|
|
|
+ if background.pagesString.count != 0 {
|
|
|
+ tBackground.pageString = background.pagesString
|
|
|
+ } else {
|
|
|
+ let pageString = "0-\(document.pageCount - 1)"
|
|
|
+ tBackground.pageString = pageString
|
|
|
+ }
|
|
|
+
|
|
|
+ tBackground.update()
|
|
|
+
|
|
|
+ // Save to temporary path
|
|
|
+ let documentPath = NSTemporaryDirectory()
|
|
|
+ let tempPath = (documentPath as NSString).appendingPathComponent((path as NSString).lastPathComponent)
|
|
|
+ try? FileManager.default.removeItem(atPath: tempPath)
|
|
|
+
|
|
|
+ let result = document.write(to: URL(fileURLWithPath: tempPath))
|
|
|
+ if result {
|
|
|
+ if FileManager.default.fileExists(atPath: path) {
|
|
|
+ try? FileManager.default.removeItem(atPath: path)
|
|
|
+ }
|
|
|
+ try? FileManager.default.moveItem(atPath: tempPath, toPath: path)
|
|
|
+ } else {
|
|
|
+ try? FileManager.default.removeItem(atPath: tempPath)
|
|
|
+ }
|
|
|
+
|
|
|
+ if result {
|
|
|
+ DispatchQueue.main.async {
|
|
|
+ let needSave = self.saveToTemplateButton.state == .on
|
|
|
+ if needSave {
|
|
|
+ if self.checkPageRangeValidate(self.pageRangeComboBox.stringValue) && self.pageRangeComboBox.indexOfSelectedItem == -1 {
|
|
|
+ self.background.pagesString = self.pageRangeComboBox.stringValue
|
|
|
+ }
|
|
|
+ KMBackgroundManager.defaultManager.addTemplate(model: self.background)
|
|
|
+ NotificationCenter.default.post(name: NSNotification.Name("KMBatchOperateWatermarksNotification"), object: self)
|
|
|
+ }
|
|
|
+ self.cancelAction?(self)
|
|
|
+ if autoOpen {
|
|
|
+ NSDocumentController.shared.openDocument(withContentsOf: URL(fileURLWithPath: path), display: true) { _, _, _ in }
|
|
|
+ } else {
|
|
|
+ NSWorkspace.shared.selectFile(path, inFileViewerRootedAtPath: "")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ func kNewDocumentTempSavePath(_ fileName: String) -> String {
|
|
|
+ let searchPath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last
|
|
|
+ let append1 = searchPath?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!)
|
|
|
+ let append2 = append1!.stringByAppendingPathComponent(String(format: "%@", fileName))
|
|
|
+ return append2
|
|
|
+ }
|
|
|
+
|
|
|
+ func isDamageImage(_ image: NSImage, imagePath path: String) -> Bool {
|
|
|
+ let addImageAnnotation = (NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last! as NSString).appendingPathComponent(Bundle.main.bundleIdentifier!).stringByAppendingPathComponent("addImageAnnotation")
|
|
|
+ if !FileManager.default.fileExists(atPath: addImageAnnotation) {
|
|
|
+ try? FileManager.default.createDirectory(atPath: addImageAnnotation, withIntermediateDirectories: false, attributes: nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ let data = image.tiffRepresentation!
|
|
|
+ let imageRep = NSBitmapImageRep(data: data)!
|
|
|
+ imageRep.size = image.size
|
|
|
+ var imageData: Data?
|
|
|
+
|
|
|
+ if path.lowercased() == "png" {
|
|
|
+ imageData = imageRep.representation(using: .png, properties: [:])
|
|
|
+ } else {
|
|
|
+ imageData = imageRep.representation(using: .jpeg, properties: [:])
|
|
|
+ }
|
|
|
+
|
|
|
+ let rPath = (addImageAnnotation as NSString).appendingPathComponent(tagString() + ".png")
|
|
|
+ return ((try? imageData?.write(to: URL(fileURLWithPath: rPath), options: .atomic)) == nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ func tagString() -> String {
|
|
|
+ let dateFormatter = DateFormatter()
|
|
|
+ dateFormatter.dateFormat = "yyMMddHHmmss"
|
|
|
+ return String(format: "%@%04d", dateFormatter.string(from: Date()), arc4random() % 10000)
|
|
|
+ }
|
|
|
+
|
|
|
+ // ... (remaining methods)
|
|
|
+ @objc func pageChangeNotification(notification: NSNotification) {
|
|
|
+ self.currentPageIndexTextField.stringValue = self.pdfView.currentPage().label;
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -41,4 +559,236 @@ extension KMAddBackgroundView {
|
|
|
callBack(self)
|
|
|
}
|
|
|
|
|
|
+ @IBAction func buttonClicked_SwitchBackgroundType(_ sender: Any) {
|
|
|
+ changeTypeBoxState(sender as? NSObject == colorButton)
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func colorWellChanged(_ sender: NSColorWell) {
|
|
|
+ background.color = sender.color
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func radioStepperAction(_ sender: NSStepper) {
|
|
|
+ ratioTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
|
|
|
+ background.scale = sender.doubleValue
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func angleStepperAction(_ sender: NSStepper) {
|
|
|
+ angleTextField.stringValue = "\(sender.doubleValue)"
|
|
|
+ background.rotation = Int(sender.doubleValue)
|
|
|
+ checkAngle()
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func alphaSliderAction(_ sender: NSSlider) {
|
|
|
+ background.opacity = sender.doubleValue
|
|
|
+ alphaStepper.doubleValue = sender.doubleValue
|
|
|
+ alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func verticalStepperAction(_ sender: NSStepper) {
|
|
|
+ verticalGapTextField.stringValue = "\(sender.doubleValue)"
|
|
|
+ background.verticalSpace = sender.doubleValue
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func horizentalStepperAction(_ sender: NSStepper) {
|
|
|
+ horizontalGapTextField.stringValue = "\(sender.doubleValue)"
|
|
|
+ background.horizontalSpace = sender.doubleValue
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func alphaSteperAction(_ sender: NSStepper) {
|
|
|
+ alphaTextField.stringValue = "\(Int(sender.doubleValue * 100))%"
|
|
|
+ background.opacity = sender.doubleValue
|
|
|
+ alphaStepper.doubleValue = sender.doubleValue
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+
|
|
|
+ if filePathLabel.stringValue.count > 0 {
|
|
|
+ doneButton.isEnabled = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func goPrevious(_ sender: Any) {
|
|
|
+ if pdfView.canGoToPreviousPage() {
|
|
|
+ pdfView.goToPreviousPage(nil)
|
|
|
+ }
|
|
|
+ let index = pdfDocument!.index(for: pdfView.currentPage())
|
|
|
+ currentPageIndexTextField.stringValue = "\(index + 1)"
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func goNext(_ sender: Any) {
|
|
|
+ if pdfView.canGoToNextPage() {
|
|
|
+ pdfView.goToNextPage(nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ let index = pdfDocument!.index(for: pdfView.currentPage())
|
|
|
+ currentPageIndexTextField.stringValue = "\(index + 1)"
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func buttonClicked_BrowserFile(_ sender: Any) {
|
|
|
+ let openPanel = NSOpenPanel()
|
|
|
+ openPanel.canChooseDirectories = false
|
|
|
+ openPanel.canChooseFiles = true
|
|
|
+ openPanel.allowsMultipleSelection = false
|
|
|
+ openPanel.allowedFileTypes = ["jpg", "cur", "bmp", "jpeg", "gif", "png", "tiff", "tif", "ico", "icns", "tga", "psd", "eps", "hdr", "jp2", "jpc", "pict", "sgi", "pdf"]
|
|
|
+
|
|
|
+ openPanel.beginSheetModal(for: window!) { (result) in
|
|
|
+ if result == .OK {
|
|
|
+ guard let url = openPanel.url else { return }
|
|
|
+ let filePath: NSString = url.path as NSString
|
|
|
+
|
|
|
+ if filePath.pathExtension.lowercased() == "pdf" {
|
|
|
+ let pdf = CPDFDocument(url: url)
|
|
|
+ if pdf?.isEncrypted == true {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if let image = NSImage(contentsOfFile: url.path), !self.isDamageImage(image, imagePath: url.path) {
|
|
|
+ self.filePathLabel.stringValue = url.path
|
|
|
+ self.doneButton.isEnabled = true
|
|
|
+ self.background.imagePath = url.path
|
|
|
+ self.background.backgroundID = url.path.lastPathComponent.deletingPathExtension
|
|
|
+ self.templateNameTextField.stringValue = url.path.lastPathComponent.deletingPathExtension
|
|
|
+ self.pdfView.needsDisplay = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // ... (remaining IBActions)
|
|
|
+
|
|
|
+ @IBAction func buttonClicked_Batch(_ sender: Any) {
|
|
|
+ if background.type == .color {
|
|
|
+ background.imagePath = ""
|
|
|
+ } else {
|
|
|
+ background.color = nil
|
|
|
+ if background.image == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if templateNameTextField.stringValue.count < 1 {
|
|
|
+ background.backgroundID = initialID
|
|
|
+ } else {
|
|
|
+ background.backgroundID = templateNameTextField.stringValue
|
|
|
+ }
|
|
|
+
|
|
|
+ let needSave = saveToTemplateButton.state == .on
|
|
|
+ if needSave {
|
|
|
+ KMBackgroundManager.defaultManager.addTemplate(model: background)
|
|
|
+ }
|
|
|
+
|
|
|
+ operateCallBack?(background, currentType)
|
|
|
+ }
|
|
|
+
|
|
|
+ @IBAction func buttonClicked_Done(_ sender: Any) {
|
|
|
+ guard let pdfDocument = pdfDocument else { return }
|
|
|
+ if background.type == .color {
|
|
|
+ background.imagePath = ""
|
|
|
+ } else {
|
|
|
+ background.color = nil
|
|
|
+ if background.image == nil {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if templateNameTextField.stringValue.isEmpty {
|
|
|
+ background.backgroundID = initialID
|
|
|
+ } else {
|
|
|
+ background.backgroundID = templateNameTextField.stringValue
|
|
|
+ }
|
|
|
+
|
|
|
+ // Avoid showing the page range alert twice
|
|
|
+ if checkPageRangeValidate(pageRangeComboBox.stringValue) {
|
|
|
+ background.pagesString = pageRangeComboBox.stringValue
|
|
|
+ pdfView.needsDisplay = true
|
|
|
+ window?.makeFirstResponder(self)
|
|
|
+ }
|
|
|
+
|
|
|
+ let needSave = saveToTemplateButton.state == .on
|
|
|
+ var pages = [Int]()
|
|
|
+
|
|
|
+ switch pageRangeComboBox.indexOfSelectedItem {
|
|
|
+ case 0:
|
|
|
+ pages = Array(0..<Int(pdfDocument.pageCount))
|
|
|
+ case 1:
|
|
|
+ pages = Array(stride(from: 0, to: Int(pdfDocument.pageCount), by: 2))
|
|
|
+ case 2:
|
|
|
+ pages = Array(stride(from: 1, to: Int(pdfDocument.pageCount), by: 2))
|
|
|
+ default:
|
|
|
+ let fileAttribute = KMFileAttribute()
|
|
|
+ fileAttribute.filePath = pdfDocument.documentURL?.path ?? ""
|
|
|
+ fileAttribute.bAllPage = false
|
|
|
+ fileAttribute.pagesString = pageRangeComboBox.stringValue
|
|
|
+
|
|
|
+ let selectPages = fileAttribute.fetchSelectPages()
|
|
|
+ if selectPages.count != 0 {
|
|
|
+ pages = selectPages.map { $0.intValue - 1 }
|
|
|
+ } else {
|
|
|
+ let alert = NSAlert()
|
|
|
+ alert.alertStyle = .critical
|
|
|
+ alert.messageText = "\(fileAttribute.filePath.lastPathComponent) \(NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: ""))"
|
|
|
+ alert.runModal()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ background.pagesString = pages.isEmpty ? "" : pages.map { "\($0)" }.joined(separator: ",")
|
|
|
+
|
|
|
+ switch type {
|
|
|
+ case .add:
|
|
|
+ if needSave {
|
|
|
+ if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
|
|
|
+ background.pagesString = pageRangeComboBox.stringValue
|
|
|
+ }
|
|
|
+ KMBackgroundManager.defaultManager.addTemplate(model: background)
|
|
|
+ }
|
|
|
+ operateCallBack?(background, currentType)
|
|
|
+ case .edit:
|
|
|
+ if needSave {
|
|
|
+ if checkPageRangeValidate(pageRangeComboBox.stringValue) && pageRangeComboBox.indexOfSelectedItem == -1 {
|
|
|
+ background.pagesString = pageRangeComboBox.stringValue
|
|
|
+ }
|
|
|
+ originalBackground = (background.copy() as? KMBackgroundModel)!
|
|
|
+// KMBackgroundManager.defaultManager.store()
|
|
|
+ }
|
|
|
+ operateCallBack?(originalBackground, currentType)
|
|
|
+ case .use:
|
|
|
+ let fileName = "\(pdfDocument.documentURL?.lastPathComponent ?? NSLocalizedString("Untitled", comment: ""))_Background"
|
|
|
+ let savePanelAccessoryViewController = KMSavePanelAccessoryController()
|
|
|
+ let savePanel = NSSavePanel()
|
|
|
+ savePanel.nameFieldStringValue = fileName
|
|
|
+ savePanel.allowedFileTypes = ["pdf"]
|
|
|
+ savePanel.accessoryView = savePanelAccessoryViewController.view
|
|
|
+ savePanel.beginSheetModal(for: window!) { result in
|
|
|
+ if result == .OK {
|
|
|
+ self.saveAsPDF(with: self.background, to: savePanel.url?.path ?? "", autoOpen: savePanelAccessoryViewController.openAutomaticButton.state == .on ? true : false)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|