// // KMWatermarkPropertyInfoController.swift // PDF Master // // Created by tangchao on 2022/12/16. // import Cocoa enum KMWatermarkPropertyType: Int { case text = 0 case file = 1 } class KMWatermarkPropertyInfoController: NSViewController { @IBOutlet weak var scrollView: NSScrollView! @IBOutlet weak var infoBox: NSBox! @IBOutlet weak var infoBoxHeightConst: NSLayoutConstraint! @IBOutlet weak var outsideBox: NSBox! @IBOutlet weak var outsideHeightConst: NSLayoutConstraint! @IBOutlet weak var positionBox: NSBox! @IBOutlet weak var plainBox: NSBox! @IBOutlet weak var pageRangeBox: NSBox! var model: KMWatermarkModel! var type: KMWatermarkPropertyType = .text var fileInfoView: KMWatermarkFileView! var textInfoView: KMWatermarkTextView! var outsideView: KMWatermarkAdjectiveOutsideView! var fileOutSideView: KMWatermarkFileOutsideView! var positiomView: KMWatermarkAdjectivePositionView! var plainView: KMWatermarkAdjectivePlainView! var pageRangeView: KMConvertPageRangeSettingItemView! weak var delegate: KMWatermarkAdjectivePropertyDelegate! override func viewDidLoad() { super.viewDidLoad() // Do view setup here. self.scrollView.hasVerticalScroller = false self.scrollView.hasHorizontalScroller = false if (self.type == .text) { self.infoBoxHeightConst.constant = 178 var infoView = KMWatermarkTextView.createFromNib() infoView?.setModel(model: self.model) infoView?.frame = self.infoBox.contentView!.bounds infoView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.infoBox.contentView?.addSubview(infoView!) self.textInfoView = infoView infoView?.itemClick = { (itemID: Int, itemValue: Any) in if (itemID == 1) { self.model.text = itemValue as! String } else if (itemID == 2) { self.model.textFont = .font(name: itemValue as! String, size: self.model.getTextFontSize()) } else if (itemID == 3) { let size: Int = itemValue as! Int self.model.textFont = .font(name: self.model.getTextFontName(), size: CGFloat(size)) } else if (itemID == 4) { let aligement: Int = itemValue as! Int if (aligement == 0) { self.model.textAligement = .left } else if (aligement == 1) { self.model.textAligement = .center } else if (aligement == 2) { self.model.textAligement = .right } } else if (itemID == 5) { let color = KMWatermarkAdjectiveTools.parseColor(color: itemValue as! NSColor) self.model.textColor = .color(red: color.red, green: color.green, blue: color.blue, alpha: color.alpha) } self.updateModel(model: self.model) } } else { self.infoBoxHeightConst.constant = 50 var infoView = KMWatermarkFileView.createFromNib() infoView?.frame = self.infoBox.contentView!.bounds infoView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.infoBox.contentView?.addSubview(infoView!) self.fileInfoView = infoView self.outsideBox.isHidden = true self.positionBox.isHidden = true self.plainBox.isHidden = true self.pageRangeBox.isHidden = true infoView?.itemClick = { (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.plainBox.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.imagePath = panel.url?.path self.model.image = image self.model.text = "" self.model.watermarkID = panel.url?.lastPathComponent.components(separatedBy: ".").first self.modelDidChange(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 self.model.text = "" self.model.watermarkID = panel.url?.lastPathComponent.components(separatedBy: ".").first self.modelDidChange(self.model) } } } } /// 外观 self.outsideHeightConst.constant = 150 var outsideView = KMWatermarkAdjectiveOutsideView() outsideView.frame = self.outsideBox.contentView!.frame outsideView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) outsideView.setModel(model: model) self.outsideBox.contentView?.addSubview(outsideView) self.outsideView = outsideView outsideView.itemClick = { (itemID: Int, itemValue: Any) in if (itemID == 1) { self.model.rotation = -CGFloat(itemValue as! Int) } else if (itemID == 2) { self.model.opacity = CGFloat(itemValue as! Int)/100.0 } else if (itemID == 3) { let value: Int = itemValue as! Int if (value == 0) { self.model.isFront = true } else { self.model.isFront = false } } else if (itemID == 4) { self.model.scale = CGFloat((itemValue as! Int))/100.0 } self.modelDidChange(self.model) } /// 位置 var positionView = KMWatermarkAdjectivePositionView.createFromNib() positionView?.frame = self.positionBox.contentView!.bounds positionView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.positionBox.contentView?.addSubview(positionView!) self.positiomView = positionView positionView?.itemClick = { (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) } self.modelDidChange(self.model) } /// 平铺 var plainView = KMWatermarkAdjectivePlainView.createFromNib() plainView?.frame = self.plainBox.contentView!.bounds plainView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.plainBox.contentView?.addSubview(plainView!) self.plainView = plainView plainView?.setModel(model: self.model) plainView?.itemClick = { (itemID: Int, itemValue: Any) in if (itemID == 1) { self.model.isTilePage = itemValue as! Bool } else if (itemID == 2) { self.model.tileHorizontalSpace = CGFloat(itemValue as! Double) } else if (itemID == 3) { self.model.tileVerticalSpace = CGFloat(itemValue as! Double) } self.modelDidChange(self.model) } var pageRangeView = KMConvertPageRangeSettingItemView.createFromNib() pageRangeView?.frame = pageRangeBox.contentView!.bounds pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) 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) self.pageRangeView = pageRangeView pageRangeView?.isTextDidEndCallback = true pageRangeView?.itemClick = { [unowned self] index, _ in self.model.pageRangeType = index self.modelDidChange(self.model) } pageRangeView?.textDidEndEditingCallback = { [unowned self] (textString: String) in let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: Int(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.pagesString = pageString self.modelDidChange(self.model) } } override func viewDidAppear() { super.viewDidAppear() /// 滚动到顶部 let contentView = self.scrollView.contentView; contentView.scroll(to: NSMakePoint(0, NSHeight(self.scrollView.documentView!.frame)-NSHeight(contentView.frame))) } 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 } private func modelDidChange(_ model: KMWatermarkModel) { self.delegate.propertyInfoDidChange(model: model) } func updateModel(model: KMWatermarkModel) { self.model = model self.outsideBox.isHidden = false self.plainBox.isHidden = false self.positionBox.isHidden = false self.pageRangeBox.isHidden = false if (self.textInfoView != nil) { self.textInfoView.setModel(model: model) } if (self.fileInfoView != nil) { self.fileInfoView.setModel(model: model) self.fileInfoView.tipLabel.stringValue = model.imagePath.lastPathComponent } if (self.outsideView != nil) { self.outsideView.setModel(model: model) } if (self.fileOutSideView != nil) { self.fileOutSideView.setModel(model: model) } if (self.positiomView != nil) { self.positiomView.setModel(model: model) } if (self.plainView != nil) { self.plainView.setModel(model: model) } } }