123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- //
- // KMOCRPresenter.swift
- // PDF Master
- //
- // Created by lizhe on 2022/12/8.
- //
- import Cocoa
- class KMOCRPresenter: NSObject {
- lazy var model: KMOCRModel! = KMOCRModel()
- var view: NSView?
- fileprivate weak var delegate: KMOCRPresenterDelegate?
-
- /**
- 初始化presenter
- */
- func initPresenter(delegate: KMOCRPresenterDelegate, data: KMOCRModel) {
- self.delegate = delegate
- self.model = model
- self.delegate?.showData(presenter: self, data: self.model)
- }
-
- func cleanAll() {
- self.delegate?.showData(presenter: self, data: self.model)
- }
- }
- extension KMOCRPresenter: KMOCRPresenterProtocol {
- func selectLanguage(sender: NSButton) {
- KMGOCRManager.default().ocrType = .apple
- let languageArray:[NSDictionary] = (KMGOCRManager.languages() as? Array)!
-
- let menu = NSMenu.init()
- for dic in languageArray {
- let item = menu.addItem(withTitle: NSLocalizedString(dic[KMGOCRLanguageStringKey] as! String, comment: ""),
- action: #selector(language),
- keyEquivalent: "")
- item.target = self
- item.representedObject = dic
- }
-
- menu.popUp(positioning: menu.item(at: 0), at: CGPoint(x: 0, y: (sender as NSView).fittingSize.height), in: sender as NSView)
- }
-
- func saveTXT(sender: NSButton) {
- if sender.state == .on {
- self.model.exportOptions.insert(.txt)
- } else {
- self.model.exportOptions.remove(.txt)
- }
-
- if self.delegate != nil {
- self.delegate!.showData(presenter: self, data: self.model)
- }
- }
-
- func savePDF(sender: NSButton) {
- if sender.state == .on {
- self.model.exportOptions.insert(.pdf)
- } else {
- self.model.exportOptions.remove(.pdf)
- }
-
- if self.delegate != nil {
- self.delegate!.showData(presenter: self, data: self.model)
- }
- }
-
- func export(filePath: String) {
- self.model.exportFilePath = filePath
- if self.delegate != nil {
- self.delegate!.export(presenter: self, data: self.model)
- }
- }
-
- @objc func language(sender: NSMenuItem) {
- KMPrint(sender.representedObject!)
- let dic: NSDictionary = sender.representedObject as! NSDictionary
- self.model.languageDic = dic
- self.model.languageType = KMOCRLanguageType.init(rawValue: dic[KMGOCRLanguageCodeKey] as! String)
-
- if (self.delegate != nil) {
- self.delegate?.showData(presenter: self, data: self.model)
- }
- }
-
- func free() {
- view = nil
- delegate = nil
- }
-
- }
- protocol KMOCRPresenterDelegate: AnyObject {
- /**
- @abstract 回调,返回处理后的数据
- @param presenter
- @param data 返回数据
- */
- func showData(presenter: KMOCRPresenter, data: KMOCRModel)
- /**
- @abstract 导出成功回调
- */
- func export(presenter: KMOCRPresenter, data: KMOCRModel)
- }
- protocol KMOCRPresenterProtocol {
- /**
- @abstract 选择语言
- */
- func selectLanguage(sender: NSButton)
- /**
- @abstract 保存TXT
- */
- func saveTXT(sender: NSButton)
- /**
- @abstract 保存PDF
- */
- func savePDF(sender: NSButton)
- /**
- @abstract 导出
- */
- func export(filePath: String)
- /**
- @abstract 解除绑定
- */
- func free()
- }
|