// // KMImageToPDFChooseView.swift // PDF Master // // Created by lizhe on 2022/11/22. // import Cocoa class KMImageToPDFChooseView: KMBaseXibView { @IBOutlet weak var titleLabel: NSTextField! @IBOutlet weak var exportTitleLable: NSTextField! @IBOutlet weak var newPDFButton: KMImageTitleButton! @IBOutlet weak var mergePDFButton: KMImageTitleButton! @IBOutlet weak var insertPDFButton: KMImageTitleButton! @IBOutlet weak var exportSelectFileButton: NSButton! @IBOutlet weak var exportSelectFileLabel: NSTextField! @IBOutlet weak var filePathContentView: NSView! @IBOutlet weak var fileImageContentView: NSView! @IBOutlet weak var OCRTilteLable: NSTextField! @IBOutlet weak var needRecognitionButton: KMImageTitleButton! @IBOutlet weak var selectLanguageLabel: NSTextField! @IBOutlet weak var selectLanguageButton: NSButton! @IBOutlet weak var saveTXTButton: KMImageTitleButton! @IBOutlet weak var exportPDFButton: NSButton! var delegate: KMImageToPDFChooseViewDelegate? lazy var presenter: KMImageToPDFChoosePresenter! = KMImageToPDFChoosePresenter() lazy var OCRPresenter: KMOCRPresenter! = KMOCRPresenter() lazy var data: KMImageToPDFChooseModel! = KMImageToPDFChooseModel() deinit { self.delegate = nil } override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) KMPrint("Drawing code here.") } override func setup() { self.presenter.initPresenter(delegate: self, data: KMImageToPDFChooseModel()) self.OCRPresenter.initPresenter(delegate: self, data: KMOCRModel()) self.newPDFButton.imageName = "btn_radio_unsel_on" self.newPDFButton.action = { [unowned self] (view, button) in self.presenter.newPDF(state: button.state) } self.mergePDFButton.imageName = "btn_radio_unsel_on" self.mergePDFButton.action = { [unowned self] (view, button) in self.presenter.mergePDF(state: button.state) } self.insertPDFButton.imageName = "btn_radio_unsel_on" self.insertPDFButton.action = { [unowned self] (view, button) in self.presenter.insert(state: button.state) } self.needRecognitionButton.imageName = "btn_checkbox_unsel_on" self.needRecognitionButton.action = { [unowned self] (view, button) in self.presenter.needRecognition(state: button.state) } self.saveTXTButton.imageName = "btn_checkbox_unsel_on" self.saveTXTButton.action = { [unowned self] (view, button) in self.OCRPresenter.saveTXT(sender: button) } } override func updateUI() { super.updateUI() self.wantsLayer = true self.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor self.titleLabel.font = NSFont.SFProTextSemiboldFont(16.0) self.titleLabel.textColor = NSColor.km_init(hex: "#252629") self.exportTitleLable.font = NSFont.SFProTextRegularFont(12.0) self.exportTitleLable.textColor = NSColor.km_init(hex: "#616469") self.OCRTilteLable.font = NSFont.SFProTextRegularFont(12.0) self.OCRTilteLable.textColor = NSColor.km_init(hex: "#616469") self.exportSelectFileLabel.font = NSFont.SFProTextRegularFont(14.0) self.exportSelectFileLabel.textColor = NSColor.km_init(hex: "#94989C") self.filePathContentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF")) self.filePathContentView.border() self.fileImageContentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF")) self.fileImageContentView.border() self.selectLanguageLabel.font = NSFont.SFProTextRegularFont(14.0) self.selectLanguageLabel.textColor = NSColor.km_init(hex: "#252629") self.newPDFButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0) self.newPDFButton.titleLabel.textColor = NSColor.km_init(hex: "#252629") self.mergePDFButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0) self.mergePDFButton.titleLabel.textColor = NSColor.km_init(hex: "#252629") self.insertPDFButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0) self.insertPDFButton.titleLabel.textColor = NSColor.km_init(hex: "#252629") self.needRecognitionButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0) self.needRecognitionButton.titleLabel.textColor = NSColor.km_init(hex: "#252629") self.saveTXTButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0) self.saveTXTButton.titleLabel.textColor = NSColor.km_init(hex: "#252629") self.exportPDFButton.wantsLayer = true self.exportPDFButton.layer?.cornerRadius = 4 self.exportPDFButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF") self.exportPDFButton.font = NSFont.SFProTextRegularFont(14.0) self.exportPDFButton.layer?.backgroundColor = NSColor.km_init(hex: "#BDDFFD").cgColor } override func updateLanguage() { super.updateLanguage() self.titleLabel.stringValue = NSLocalizedString("Settings", comment: "") self.exportTitleLable.stringValue = NSLocalizedString("Export", comment: "") self.OCRTilteLable.stringValue = NSLocalizedString("OCR Settings", comment: "") self.exportSelectFileLabel.stringValue = NSLocalizedString("Select File", comment: "") self.selectLanguageLabel.stringValue = NSLocalizedString("English", comment: "") self.newPDFButton.title = NSLocalizedString("Create PDF for each image", comment: "") self.mergePDFButton.title = NSLocalizedString("Merge as one PDF document", comment: "") self.insertPDFButton.title = NSLocalizedString("Insert existing PDF document", comment: "") self.needRecognitionButton.title = NSLocalizedString("Recognize text if needed", comment: "") self.saveTXTButton.title = NSLocalizedString("Save as TXT", comment: "") self.exportPDFButton.title = NSLocalizedString("Export", comment: "") } //刷新界面UI 和 数据 override func reloadData() { super.reloadData() let exportOptions = self.data.options ?? .PDF let exportType = self.data.exportPDFType ?? .new if exportOptions.contains(.OCR) { self.needRecognitionButton.imageName = "btn_checkbox_sel_on" self.selectLanguageButton.isEnabled = true self.saveTXTButton.isEnabled = true } else { self.needRecognitionButton.imageName = "btn_checkbox_unsel_on" self.selectLanguageButton.isEnabled = false self.saveTXTButton.isEnabled = false } if self.data.OCRModel.exportOptions.contains(.txt) { self.saveTXTButton.imageName = "btn_checkbox_sel_on" } else { self.saveTXTButton.imageName = "btn_checkbox_unsel_on" } self.exportSelectFileButton.isEnabled = false self.filePathContentView.alphaValue = 0.5 self.fileImageContentView.alphaValue = 0.5 let array = [self.newPDFButton, self.insertPDFButton, self.mergePDFButton] for button in array { button?.imageName = "btn_radio_unsel_on" } switch exportType { case .new: self.newPDFButton.imageName = "btn_radio_sel_on" case .insert: self.insertPDFButton.imageName = "btn_radio_sel_on" self.exportSelectFileButton.isEnabled = true self.filePathContentView.alphaValue = 1 self.fileImageContentView.alphaValue = 1 case .merge: self.mergePDFButton.imageName = "btn_radio_sel_on" } self.selectLanguageLabel.stringValue = self.data.OCRModel.languageDic[KMGOCRLanguageStringKey] as! String if self.data.insertFilePath != "" { self.exportSelectFileLabel.stringValue = self.data.insertFilePath } if (self.data.imageFilePaths?.count != 0) { self.exportPDFButton.layer?.backgroundColor = NSColor.km_init(hex: "#1770F4").cgColor self.exportPDFButton.isEnabled = true } else { self.exportPDFButton.layer?.backgroundColor = NSColor.km_init(hex: "#BDDFFD").cgColor self.exportPDFButton.isEnabled = false } } } protocol KMImageToPDFChooseViewAction {} extension KMImageToPDFChooseView: KMImageToPDFChooseViewAction { @IBAction func selectFileAction(_ sender: NSButton) { self.presenter.selectFile(state: sender.state) KMBatchProcessingView.openfiles(window: window!) { openPanel in openPanel.canChooseDirectories = false //是否允许选择目录 openPanel.canChooseFiles = true //是否可以选择文件 openPanel.allowsMultipleSelection = false //是否允许多选 openPanel.allowedFileTypes = ["pdf"] } completion: { [unowned self] (panel, data) in if data.count != 0 { self.presenter.addFile(filePath: data.first!.path) } } } @IBAction func selectLanguageAction(_ sender: NSButton) { if self.data.options.contains(.OCR) { self.OCRPresenter.selectLanguage(sender: sender) } } @IBAction func exportAction(_ sender: NSButton) { if self.data.exportPDFType == .insert && self.data.insertFilePath != "" { self.presenter.export(filePath: self.data.exportFilePath) } else { // if self.data.exportFilePath == "" { KMBatchProcessingView.openfiles(window: self.window!) { openPanel in openPanel.prompt = "" openPanel.canChooseDirectories = true //是否允许选择目录 openPanel.canChooseFiles = false //是否可以选择文件 openPanel.allowsMultipleSelection = false //是否允许多选 openPanel.canCreateDirectories = true //是否允许创建文件夹 } completion: { [unowned self](panel ,data) in if data.count != 0 { self.presenter.export(filePath: data.first!.path) } } // } else { // self.presenter.export(filePath: self.data.exportFilePath) // } } } } extension KMImageToPDFChooseView: KMImageToPDFChoosePresenterDelegate { func showData(presenter: KMImageToPDFChoosePresenter, data: KMImageToPDFChooseModel) { self.data = data self.reloadData() } func export(presenter: KMImageToPDFChoosePresenter, data: KMImageToPDFChooseModel) { if (self.delegate != nil) { self.delegate?.exportAction(data: data) } } } protocol KMImageToPDFChooseViewDelegate: NSObject { /** 导出 */ func exportAction(data: KMImageToPDFChooseModel) } extension KMImageToPDFChooseView: KMOCRPresenterDelegate { func export(presenter: KMOCRPresenter, data: KMOCRModel) { } func showData(presenter: KMOCRPresenter, data: KMOCRModel) { self.data.OCRModel = data self.reloadData() } }