KMOCRPresenter.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // KMOCRPresenter.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2022/12/8.
  6. //
  7. import Cocoa
  8. class KMOCRPresenter: NSObject {
  9. lazy var model: KMOCRModel! = KMOCRModel()
  10. var view: NSView?
  11. fileprivate weak var delegate: KMOCRPresenterDelegate?
  12. /**
  13. 初始化presenter
  14. */
  15. func initPresenter(delegate: KMOCRPresenterDelegate, data: KMOCRModel) {
  16. self.delegate = delegate
  17. self.model = model
  18. self.delegate?.showData(presenter: self, data: self.model)
  19. }
  20. func cleanAll() {
  21. self.delegate?.showData(presenter: self, data: self.model)
  22. }
  23. }
  24. extension KMOCRPresenter: KMOCRPresenterProtocol {
  25. func selectLanguage(sender: NSButton) {
  26. KMGOCRManager.default().ocrType = .apple
  27. let languageArray:[NSDictionary] = (KMGOCRManager.languages() as? Array)!
  28. let menu = NSMenu.init()
  29. for dic in languageArray {
  30. let item = menu.addItem(withTitle: NSLocalizedString(dic[KMGOCRLanguageStringKey] as! String, comment: ""),
  31. action: #selector(language),
  32. keyEquivalent: "")
  33. item.target = self
  34. item.representedObject = dic
  35. }
  36. menu.popUp(positioning: menu.item(at: 0), at: CGPoint(x: 0, y: (sender as NSView).fittingSize.height), in: sender as NSView)
  37. }
  38. func saveTXT(sender: NSButton) {
  39. if sender.state == .on {
  40. self.model.exportOptions.insert(.txt)
  41. } else {
  42. self.model.exportOptions.remove(.txt)
  43. }
  44. if self.delegate != nil {
  45. self.delegate!.showData(presenter: self, data: self.model)
  46. }
  47. }
  48. func savePDF(sender: NSButton) {
  49. if sender.state == .on {
  50. self.model.exportOptions.insert(.pdf)
  51. } else {
  52. self.model.exportOptions.remove(.pdf)
  53. }
  54. if self.delegate != nil {
  55. self.delegate!.showData(presenter: self, data: self.model)
  56. }
  57. }
  58. func export(filePath: String) {
  59. self.model.exportFilePath = filePath
  60. if self.delegate != nil {
  61. self.delegate!.export(presenter: self, data: self.model)
  62. }
  63. }
  64. @objc func language(sender: NSMenuItem) {
  65. KMPrint(sender.representedObject!)
  66. let dic: NSDictionary = sender.representedObject as! NSDictionary
  67. self.model.languageDic = dic
  68. self.model.languageType = KMOCRLanguageType.init(rawValue: dic[KMGOCRLanguageCodeKey] as! String)
  69. if (self.delegate != nil) {
  70. self.delegate?.showData(presenter: self, data: self.model)
  71. }
  72. }
  73. func free() {
  74. view = nil
  75. delegate = nil
  76. }
  77. }
  78. protocol KMOCRPresenterDelegate: AnyObject {
  79. /**
  80. @abstract 回调,返回处理后的数据
  81. @param presenter
  82. @param data 返回数据
  83. */
  84. func showData(presenter: KMOCRPresenter, data: KMOCRModel)
  85. /**
  86. @abstract 导出成功回调
  87. */
  88. func export(presenter: KMOCRPresenter, data: KMOCRModel)
  89. }
  90. protocol KMOCRPresenterProtocol {
  91. /**
  92. @abstract 选择语言
  93. */
  94. func selectLanguage(sender: NSButton)
  95. /**
  96. @abstract 保存TXT
  97. */
  98. func saveTXT(sender: NSButton)
  99. /**
  100. @abstract 保存PDF
  101. */
  102. func savePDF(sender: NSButton)
  103. /**
  104. @abstract 导出
  105. */
  106. func export(filePath: String)
  107. /**
  108. @abstract 解除绑定
  109. */
  110. func free()
  111. }