KMImageToPDFChooseView.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. //
  2. // KMImageToPDFChooseView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2022/11/22.
  6. //
  7. import Cocoa
  8. class KMImageToPDFChooseView: KMBaseXibView {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var exportTitleLable: NSTextField!
  11. @IBOutlet weak var newPDFButton: KMImageTitleButton!
  12. @IBOutlet weak var mergePDFButton: KMImageTitleButton!
  13. @IBOutlet weak var insertPDFButton: KMImageTitleButton!
  14. @IBOutlet weak var exportSelectFileButton: NSButton!
  15. @IBOutlet weak var exportSelectFileLabel: NSTextField!
  16. @IBOutlet weak var filePathContentView: NSView!
  17. @IBOutlet weak var fileImageContentView: NSView!
  18. @IBOutlet weak var OCRTilteLable: NSTextField!
  19. @IBOutlet weak var needRecognitionButton: KMImageTitleButton!
  20. @IBOutlet weak var selectLanguageLabel: NSTextField!
  21. @IBOutlet weak var selectLanguageButton: NSButton!
  22. @IBOutlet weak var saveTXTButton: KMImageTitleButton!
  23. @IBOutlet weak var exportPDFButton: NSButton!
  24. var delegate: KMImageToPDFChooseViewDelegate?
  25. lazy var presenter: KMImageToPDFChoosePresenter! = KMImageToPDFChoosePresenter()
  26. lazy var OCRPresenter: KMOCRPresenter! = KMOCRPresenter()
  27. lazy var data: KMImageToPDFChooseModel! = KMImageToPDFChooseModel()
  28. deinit {
  29. self.delegate = nil
  30. }
  31. override func draw(_ dirtyRect: NSRect) {
  32. super.draw(dirtyRect)
  33. KMPrint("Drawing code here.")
  34. }
  35. override func setup() {
  36. self.presenter.initPresenter(delegate: self, data: KMImageToPDFChooseModel())
  37. self.OCRPresenter.initPresenter(delegate: self, data: KMOCRModel())
  38. self.newPDFButton.imageName = "btn_radio_unsel_on"
  39. self.newPDFButton.action = { [unowned self] (view, button) in
  40. self.presenter.newPDF(state: button.state)
  41. }
  42. self.mergePDFButton.imageName = "btn_radio_unsel_on"
  43. self.mergePDFButton.action = { [unowned self] (view, button) in
  44. self.presenter.mergePDF(state: button.state)
  45. }
  46. self.insertPDFButton.imageName = "btn_radio_unsel_on"
  47. self.insertPDFButton.action = { [unowned self] (view, button) in
  48. self.presenter.insert(state: button.state)
  49. }
  50. self.needRecognitionButton.imageName = "btn_checkbox_unsel_on"
  51. self.needRecognitionButton.action = { [unowned self] (view, button) in
  52. self.presenter.needRecognition(state: button.state)
  53. }
  54. self.saveTXTButton.imageName = "btn_checkbox_unsel_on"
  55. self.saveTXTButton.action = { [unowned self] (view, button) in
  56. self.OCRPresenter.saveTXT(sender: button)
  57. }
  58. }
  59. override func updateUI() {
  60. super.updateUI()
  61. self.wantsLayer = true
  62. self.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  63. self.titleLabel.font = NSFont.SFProTextSemiboldFont(16.0)
  64. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  65. self.exportTitleLable.font = NSFont.SFProTextRegularFont(12.0)
  66. self.exportTitleLable.textColor = NSColor.km_init(hex: "#616469")
  67. self.OCRTilteLable.font = NSFont.SFProTextRegularFont(12.0)
  68. self.OCRTilteLable.textColor = NSColor.km_init(hex: "#616469")
  69. self.exportSelectFileLabel.font = NSFont.SFProTextRegularFont(14.0)
  70. self.exportSelectFileLabel.textColor = NSColor.km_init(hex: "#94989C")
  71. self.filePathContentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
  72. self.filePathContentView.border()
  73. self.fileImageContentView.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
  74. self.fileImageContentView.border()
  75. self.selectLanguageLabel.font = NSFont.SFProTextRegularFont(14.0)
  76. self.selectLanguageLabel.textColor = NSColor.km_init(hex: "#252629")
  77. self.newPDFButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  78. self.newPDFButton.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  79. self.mergePDFButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  80. self.mergePDFButton.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  81. self.insertPDFButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  82. self.insertPDFButton.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  83. self.needRecognitionButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  84. self.needRecognitionButton.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  85. self.saveTXTButton.titleLabel.font = NSFont.SFProTextRegularFont(14.0)
  86. self.saveTXTButton.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  87. self.exportPDFButton.wantsLayer = true
  88. self.exportPDFButton.layer?.cornerRadius = 4
  89. self.exportPDFButton.contentTintColor = NSColor.km_init(hex: "#FFFFFF")
  90. self.exportPDFButton.font = NSFont.SFProTextRegularFont(14.0)
  91. self.exportPDFButton.layer?.backgroundColor = NSColor.km_init(hex: "#BDDFFD").cgColor
  92. }
  93. override func updateLanguage() {
  94. super.updateLanguage()
  95. self.titleLabel.stringValue = NSLocalizedString("Settings", comment: "")
  96. self.exportTitleLable.stringValue = NSLocalizedString("Export", comment: "")
  97. self.OCRTilteLable.stringValue = NSLocalizedString("OCR Settings", comment: "")
  98. self.exportSelectFileLabel.stringValue = NSLocalizedString("Select File", comment: "")
  99. self.selectLanguageLabel.stringValue = NSLocalizedString("English", comment: "")
  100. self.newPDFButton.title = NSLocalizedString("Create PDF for each image", comment: "")
  101. self.mergePDFButton.title = NSLocalizedString("Merge as one PDF document", comment: "")
  102. self.insertPDFButton.title = NSLocalizedString("Insert existing PDF document", comment: "")
  103. self.needRecognitionButton.title = NSLocalizedString("Recognize text if needed", comment: "")
  104. self.saveTXTButton.title = NSLocalizedString("Save as TXT", comment: "")
  105. self.exportPDFButton.title = NSLocalizedString("Export", comment: "")
  106. }
  107. //刷新界面UI 和 数据
  108. override func reloadData() {
  109. super.reloadData()
  110. let exportOptions = self.data.options ?? .PDF
  111. let exportType = self.data.exportPDFType ?? .new
  112. if exportOptions.contains(.OCR) {
  113. self.needRecognitionButton.imageName = "btn_checkbox_sel_on"
  114. self.selectLanguageButton.isEnabled = true
  115. self.saveTXTButton.isEnabled = true
  116. } else {
  117. self.needRecognitionButton.imageName = "btn_checkbox_unsel_on"
  118. self.selectLanguageButton.isEnabled = false
  119. self.saveTXTButton.isEnabled = false
  120. }
  121. if self.data.OCRModel.exportOptions.contains(.txt) {
  122. self.saveTXTButton.imageName = "btn_checkbox_sel_on"
  123. } else {
  124. self.saveTXTButton.imageName = "btn_checkbox_unsel_on"
  125. }
  126. self.exportSelectFileButton.isEnabled = false
  127. self.filePathContentView.alphaValue = 0.5
  128. self.fileImageContentView.alphaValue = 0.5
  129. let array = [self.newPDFButton, self.insertPDFButton, self.mergePDFButton]
  130. for button in array {
  131. button?.imageName = "btn_radio_unsel_on"
  132. }
  133. switch exportType {
  134. case .new:
  135. self.newPDFButton.imageName = "btn_radio_sel_on"
  136. case .insert:
  137. self.insertPDFButton.imageName = "btn_radio_sel_on"
  138. self.exportSelectFileButton.isEnabled = true
  139. self.filePathContentView.alphaValue = 1
  140. self.fileImageContentView.alphaValue = 1
  141. case .merge:
  142. self.mergePDFButton.imageName = "btn_radio_sel_on"
  143. }
  144. self.selectLanguageLabel.stringValue = self.data.OCRModel.languageDic[KMGOCRLanguageStringKey] as! String
  145. if self.data.insertFilePath != "" {
  146. self.exportSelectFileLabel.stringValue = self.data.insertFilePath
  147. }
  148. if (self.data.imageFilePaths?.count != 0) {
  149. self.exportPDFButton.layer?.backgroundColor = NSColor.km_init(hex: "#1770F4").cgColor
  150. self.exportPDFButton.isEnabled = true
  151. } else {
  152. self.exportPDFButton.layer?.backgroundColor = NSColor.km_init(hex: "#BDDFFD").cgColor
  153. self.exportPDFButton.isEnabled = false
  154. }
  155. }
  156. }
  157. protocol KMImageToPDFChooseViewAction {}
  158. extension KMImageToPDFChooseView: KMImageToPDFChooseViewAction {
  159. @IBAction func selectFileAction(_ sender: NSButton) {
  160. self.presenter.selectFile(state: sender.state)
  161. KMBatchProcessingView.openfiles(window: window!) { openPanel in
  162. openPanel.canChooseDirectories = false //是否允许选择目录
  163. openPanel.canChooseFiles = true //是否可以选择文件
  164. openPanel.allowsMultipleSelection = false //是否允许多选
  165. openPanel.allowedFileTypes = ["pdf"]
  166. } completion: { [unowned self] (panel, data) in
  167. if data.count != 0 {
  168. self.presenter.addFile(filePath: data.first!.path)
  169. }
  170. }
  171. }
  172. @IBAction func selectLanguageAction(_ sender: NSButton) {
  173. if self.data.options.contains(.OCR) {
  174. self.OCRPresenter.selectLanguage(sender: sender)
  175. }
  176. }
  177. @IBAction func exportAction(_ sender: NSButton) {
  178. if self.data.exportPDFType == .insert && self.data.insertFilePath != "" {
  179. self.presenter.export(filePath: self.data.exportFilePath)
  180. } else {
  181. // if self.data.exportFilePath == "" {
  182. KMBatchProcessingView.openfiles(window: self.window!) { openPanel in
  183. openPanel.prompt = ""
  184. openPanel.canChooseDirectories = true //是否允许选择目录
  185. openPanel.canChooseFiles = false //是否可以选择文件
  186. openPanel.allowsMultipleSelection = false //是否允许多选
  187. openPanel.canCreateDirectories = true //是否允许创建文件夹
  188. } completion: { [unowned self](panel ,data) in
  189. if data.count != 0 {
  190. self.presenter.export(filePath: data.first!.path)
  191. }
  192. }
  193. // } else {
  194. // self.presenter.export(filePath: self.data.exportFilePath)
  195. // }
  196. }
  197. }
  198. }
  199. extension KMImageToPDFChooseView: KMImageToPDFChoosePresenterDelegate {
  200. func showData(presenter: KMImageToPDFChoosePresenter, data: KMImageToPDFChooseModel) {
  201. self.data = data
  202. self.reloadData()
  203. }
  204. func export(presenter: KMImageToPDFChoosePresenter, data: KMImageToPDFChooseModel) {
  205. if (self.delegate != nil) {
  206. self.delegate?.exportAction(data: data)
  207. }
  208. }
  209. }
  210. protocol KMImageToPDFChooseViewDelegate: NSObject {
  211. /**
  212. 导出
  213. */
  214. func exportAction(data: KMImageToPDFChooseModel)
  215. }
  216. extension KMImageToPDFChooseView: KMOCRPresenterDelegate {
  217. func export(presenter: KMOCRPresenter, data: KMOCRModel) {
  218. }
  219. func showData(presenter: KMOCRPresenter, data: KMOCRModel) {
  220. self.data.OCRModel = data
  221. self.reloadData()
  222. }
  223. }