// // KMBackgroundFilePropertyInfoController.swift // PDF Master // // Created by tangchao on 2022/12/25. // import Cocoa class KMBackgroundFilePropertyInfoController: NSViewController { @IBOutlet weak var selectFileBox: NSBox! var fileInfoView: KMWatermarkFileView = KMWatermarkFileView.createFromNib()! @IBOutlet weak var outsideBox: NSBox! var outsideView: KMWatermarkAdjectiveOutsideView = KMBackgroundOutsideView() @IBOutlet weak var positionBox: NSBox! var positionView: KMWatermarkAdjectivePositionView = KMWatermarkAdjectivePositionView.createFromNib()! @IBOutlet weak var pageRangeBox: NSBox! var pagePageView = KMConvertPageRangeSettingItemView.createFromNib() weak var delegate: KMBackgroundPropertyInfoDelegate! private var myModel: KMBackgroundModel! var model: KMBackgroundModel { get { return myModel } set { myModel = newValue self.outsideView.rComboBox.stringValue = "\(abs(Int(-newValue.rotation)))" self.outsideView.alphaComboBox.stringValue = "\(Int(newValue.opacity*100))%" self.outsideView.pageTargetScaleComboBox.stringValue = "\(Int(newValue.scale*100))" if (newValue.horizontalMode == 0) { if (model.verticalMode == 0) { self.positionView.myPositionView.selectPosition(positon: .leftTop) } else if (model.verticalMode == 1) { self.positionView.myPositionView.selectPosition(positon: .top) } else if (model.verticalMode == 2) { self.positionView.myPositionView.selectPosition(positon: .rightTop) } } if (newValue.horizontalMode == 1) { if (model.verticalMode == 0) { self.positionView.myPositionView.selectPosition(positon: .leftMiddle) } else if (model.verticalMode == 1) { self.positionView.myPositionView.selectPosition(positon: .middle) } else if (model.verticalMode == 2) { self.positionView.myPositionView.selectPosition(positon: .rightMiddle) } } if (newValue.horizontalMode == 2) { if (model.verticalMode == 0) { self.positionView.myPositionView.selectPosition(positon: .leftBottom) } else if (model.verticalMode == 1) { self.positionView.myPositionView.selectPosition(positon: .bottom) } else if (model.verticalMode == 2) { self.positionView.myPositionView.selectPosition(positon: .rightBottom) } } self.positionView.xOffsetStepper.value = newValue.horizontalSpace self.positionView.yOffsetStepper.value = newValue.verticalSpace } } override func viewDidLoad() { super.viewDidLoad() self.outsideBox.enclosingScrollView?.drawsBackground = false var infoView = self.fileInfoView infoView.frame = self.selectFileBox.contentView!.bounds infoView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.selectFileBox.contentView?.addSubview(infoView) self.outsideBox.isHidden = true self.positionBox.isHidden = true self.pageRangeBox.isHidden = true infoView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in let panel = NSOpenPanel() panel.allowsMultipleSelection = false panel.allowedFileTypes = ["pdf", "png", "jpeg"] panel.beginSheetModal(for: self!.view.window!) { response in if response == .cancel { return } self!.fileInfoView.tipLabel.stringValue = panel.url!.lastPathComponent self!.outsideBox.isHidden = false self!.positionBox.isHidden = false self!.pageRangeBox.isHidden = false if (panel.url?.pathExtension.lowercased() == "pdf") { let document = CPDFDocument(url: panel.url) if ((document?.isEncrypted)!) { return } let convert = KMPDFConvert() convert.convertType = .png convert.filePath = panel.url!.path convert.pages = [1] convert.outputFolderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!))! convert.outputFileName = "pdfConvertPng" KMPDFConvertManager.defaultManager.convert(convert: convert) { finished, error in if (finished) { if (FileManager.default.fileExists(atPath: convert.outputFilePath)) { let tagString = self!.tagString() let imagePath: String = (FileManager.default.subpaths(atPath: convert.outputFilePath)?.first)! var pdfPath: String = "\(convert.outputFilePath)/\(imagePath)" let newPath: String = "\(convert.outputFilePath)/\(tagString))/\(tagString).png" let result = try?FileManager.default.copyItem(atPath: pdfPath, toPath: newPath) if ((result) != nil) { pdfPath = newPath try?FileManager.default.removeItem(atPath: convert.outputFilePath) } let image = NSImage(contentsOfFile: pdfPath) if (image!.size.width <= 0 || image!.size.height <= 0 ) { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = "文件\(panel.url?.lastPathComponent)已损坏" alert.runModal() return } self!.model.color = nil self!.model.imagePath = panel.url!.path self!.model.image = image if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } } } } else { let image = NSImage(contentsOf: panel.url!) if (image!.size.width <= 0 || image!.size.height <= 0 ) { let alert = NSAlert() alert.alertStyle = .critical alert.messageText = "文件\(panel.url?.lastPathComponent)已损坏" alert.runModal() return } self!.model.imagePath = panel.url!.path self!.model.image = image if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } } } let outsideView = self.outsideView outsideView.frame = self.outsideBox.contentView!.bounds outsideView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.outsideBox.contentView?.addSubview(outsideView) outsideView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in if (itemID == 1) { /// 旋转 self!.model.rotation = -(itemValue as! Int) } else if (itemID == 2) { /// 透明度 self!.model.opacity = CGFloat(itemValue as! Int)/100.0 } else if (itemID == 4) { /// 缩放 self!.model.scale = CGFloat((itemValue as! Int))/100.0 } if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } let positionView = self.positionView positionView.frame = self.positionBox.contentView!.bounds positionView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.positionBox.contentView?.addSubview(positionView) positionView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in if (itemID == 1) { let position: KMWatermarkPosition = itemValue as! KMWatermarkPosition if (position == .leftTop || position == .top || position == .rightTop) { self!.model.verticalMode = 0 } else if (position == .leftMiddle || position == .middle || position == .rightMiddle) { self!.model.verticalMode = 1 } else if (position == .leftBottom || position == .bottom || position == .rightBottom) { self!.model.verticalMode = 2 } if (position == .leftTop || position == .leftMiddle || position == .leftBottom) { self!.model.horizontalMode = 0 } else if (position == .top || position == .middle || position == .bottom) { self!.model.horizontalMode = 1 } else if (position == .rightTop || position == .rightMiddle || position == .rightBottom) { self!.model.horizontalMode = 2 } } else if (itemID == 2) { self!.model.horizontalSpace = CGFloat(itemValue as! Double) } else if (itemID == 3) { self!.model.verticalSpace = CGFloat(itemValue as! Double) } if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } let pageRangeView = self.pagePageView pageRangeView?.frame = self.pageRangeBox.contentView!.bounds pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.pageRangeBox.contentView?.addSubview(pageRangeView!) pageRangeView!.pageSizeComboBox.removeAllItems() pageRangeView!.pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")]) pageRangeView!.pageSizeComboBox.selectItem(at: 0) pageRangeView?.isTextDidEndCallback = true pageRangeView?.itemClick = { [unowned self] index, _ in self.model.pageRangeType = index if (self.delegate != nil) { self.delegate.propertyInfoDidChange(model: self.model) } } pageRangeView?.textDidEndEditingCallback = { [weak self] (textString: String) in let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: self!.model.pageCount) if (array.count <= 0) { let alert = NSAlert() alert.alertStyle = .warning alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "") alert.runModal() return } var pageString: String = "" for i in 0 ..< array.count { pageString.append("\(array[i]-1)") if (i != array.count-1) { pageString.append(",") } } self!.model.pageRangeString = pageString if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } } private func tagString() -> String { var result: String = "" let dateFormatter = DateFormatter() dateFormatter.dateFormat = "yyMMddHHmmss" result.append(dateFormatter.string(from: Date())) result = result.appendingFormat("%04d", arc4random()%10000) return result } }