123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- //
- // KMBatchProcessingView.swift
- // PDF Master
- //
- // Created by lizhe on 2022/11/16.
- //
- import Cocoa
- class KMBatchProcessingView: NSView {
- @IBOutlet var contentView: NSView!
-
- @IBOutlet weak var addBox: NSBox!
- @IBOutlet weak var tableView: KMBatchProcessingTableView!
- @IBOutlet weak var selectedFilesView: KMBatchSelectedFilesView!
- @IBOutlet weak var cleanButton: NSButton!
-
- var createFilePopover: NSPopover?
- var addVC: KMDesignSelect!
-
- weak var delegate: KMBatchProcessingViewDelegate?
- var inputType: DataNavigationViewButtonActionType? {
- didSet {
- self.selectedFilesView.inputType = inputType
- self.tableView.inputType = inputType
- if inputType == .ImageToPDF {
- addVC.removeAllItems()
- addVC.addItems(withObjectValues: ["Add Files", "Add Folder"])
- }
- }
- }
-
- /**
- @abstract 外部传入数据
- @param inputData 文件路劲
- */
- var inputData: [URL]! {
- didSet {
- self.tableView.inputData = inputData
- self.reloadData()
- }
- }
-
- override func draw(_ dirtyRect: NSRect) {
- super.draw(dirtyRect)
- // Drawing code here.
- }
-
- // MARK: 初始化
- public required init?(coder decoder: NSCoder) {
- super.init(coder: decoder)
- initContentView()
- setup()
- }
-
- override init(frame frameRect: NSRect) {
- super.init(frame: frameRect)
- initContentView()
- setup()
- }
-
- private func initContentView() {
- //绑定xib
- let resource = NSNib(nibNamed: String(describing: self.classForCoder.self),
- bundle: Bundle(for: self.classForCoder.self))!
- resource.instantiate(withOwner: self, topLevelObjects: nil)
- addSubview(contentView)
- contentView.translatesAutoresizingMaskIntoConstraints = false
- NSLayoutConstraint.activate([
- contentView.topAnchor.constraint(equalTo: topAnchor),
- contentView.leftAnchor.constraint(equalTo: leftAnchor),
- contentView.rightAnchor.constraint(equalTo: rightAnchor),
- contentView.bottomAnchor.constraint(equalTo: bottomAnchor)])
- contentView.updateConstraintsForSubtreeIfNeeded()
- }
-
- func setup() {
- addVC = KMDesignSelect.init(withType: .PopButton)
- addBox.fillColor = .clear
- addBox.contentView = addVC.view
- addVC.removeAllItems()
- addVC.addItems(withObjectValues: ["Add Files", "Add Folder", "Add Open Files"])
- addVC.selectItem(at: 0)
- addVC.popoverBehavior = .transient
- addVC.delete = self
-
- self.cleanButton.wantsLayer = true
- self.cleanButton.border()
-
- self.selectedFilesView.dragEnd = {[unowned self] (data) in
- self.inputData = data
- }
-
- self.selectedFilesView.addAction = {[unowned self] (sender) in
- self.addFiles()
- }
-
- self.tableView.delegate = self
- self.reloadData()
- }
-
- func reloadData() {
- if (self.tableView.data != nil) && self.tableView.data!.count != 0 {
- self.selectedFilesView.isHidden = true
- self.tableView.isHidden = false
- } else {
- self.selectedFilesView.isHidden = false
- self.tableView.isHidden = true
- }
-
- if self.tableView.data?.count != 0 {
- self.cleanButton.isEnabled = true
- } else {
- self.cleanButton.isEnabled = false
- }
-
- self.tableView.reloadData()
- }
- }
- protocol KMBatchProcessingViewAction{}
- extension KMBatchProcessingView: KMBatchProcessingViewAction {
-
- @IBAction func cleanAll(_ sender: Any) {
- if self.tableView.selectModels.count != 0 {
- self.tableView.presenter.deleteData(model: (self.tableView.selectModels.first)!)
- self.tableView.selectModels.removeObject(self.tableView.selectModels.first!)
-
- } else {
- self.tableView.presenter.cleanAll()
- self.tableView.selectModels.removeAll()
- }
-
- self.reloadData()
- }
-
- func addFiles() {
- KMBatchProcessingView.openfiles(window: window!) { [unowned self] openPanel in
- if self.inputType == .OCR {
- openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
- } else if self.inputType == .ImageToPDF {
- openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
- } else {
- openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
- }
-
- openPanel.prompt = "Open"
- openPanel.canChooseDirectories = false //是否允许选择目录
- openPanel.canChooseFiles = true //是否可以选择文件
- openPanel.allowsMultipleSelection = true //是否允许多选
-
- if openPanel.allowsMultipleSelection == true {
- openPanel.message = NSLocalizedString("Press the keyboard ⌘/⇧ to select multiple files", comment: "")
- }
- } completion: { [unowned self] (panel ,data) in
- if data.count != 0 {
- self.tableView.presenter.addData(data: data)
- self.reloadData()
- }
- }
- }
-
- func addFolder() {
- KMPrint("addFolder")
- KMBatchProcessingView.openfiles(window: window!) { [unowned self] openPanel in
- if self.inputType == .OCR {
- openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
- } else if self.inputType == .ImageToPDF {
- openPanel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes()
- } else {
- openPanel.allowedFileTypes = KMOCRModel.supportedTypes()
- }
-
- openPanel.prompt = "Open"
- openPanel.canChooseDirectories = true //是否允许选择目录
- openPanel.canChooseFiles = false //是否可以选择文件
- openPanel.allowsMultipleSelection = false //是否允许多选
- } completion: { [unowned self] (panel ,data) in
- if data.count != 0 {
- var filePaths: [URL] = []
-
- var types: [String] = []
- if self.inputType == .OCR {
- types = KMOCRModel.supportedTypes()
- } else if self.inputType == .ImageToPDF {
- types = KMBatchProcessingTableViewModel.supportedImageTypes()
- } else {
- types = KMOCRModel.supportedTypes()
- }
-
- KMMergeTools.findAllFiles(folder: data.first!, result: &filePaths, types: types)
- self.tableView.presenter.addData(data: filePaths)
- self.reloadData()
- }
- }
- }
-
- func addOpenFiles() {
- let array = ImageToPDFTools.fetchOpenDocumentFiles()
- self.tableView.presenter.addData(data: array)
- self.reloadData()
- }
-
- func canAddOpenFile() -> Bool {
- var result: Bool = false
- let array = ImageToPDFTools.fetchOpenDocumentFiles()
- if array.count != 0 {
- result = true
- }
- return result
- }
- }
- protocol KMBatchProcessingViewOpenPanel {}
- extension KMBatchProcessingView: KMBatchProcessingViewOpenPanel {
- /**
- @abstract 打开文件
- @param window 显示window
- @param title 标题
- */
- static func openfiles(window:NSWindow, openPanel:((_ openPanel: NSOpenPanel) -> Void)?, completion:@escaping/**逃逸闭包*/ (_ panel: NSOpenPanel ,_ data:[URL]) -> Void) {
- let panel = NSOpenPanel()
- if openPanel != nil {
- openPanel!(panel)
- } else {
- panel.prompt = ""
- panel.canChooseDirectories = false //是否允许选择目录
- panel.canChooseFiles = true //是否可以选择文件
- panel.allowsMultipleSelection = true //是否允许多选
- panel.allowedFileTypes = KMBatchProcessingTableViewModel.supportedImageTypes() //支持类型
-
- // let button = NSButton.init(checkboxWithTitle: "保存后打开文档", target: nil, action: nil)
- // button.state = .off
- // panel.accessoryView = button
- }
-
- panel.beginSheetModal(for: window, completionHandler: { result in
- if result != .OK {
- KMPrint("取消")
- completion(panel, [])
- } else {
- completion(panel, panel.urls)
- }
- })
- }
- }
- extension KMBatchProcessingView: KMBatchProcessingTableViewDelegate {
- func reloadData(data: [KMBatchProcessingTableViewModel]) {
- self.reloadData()
- if (self.delegate != nil) {
- self.delegate?.reloadData(data: data)
- }
- }
- }
- protocol KMBatchProcessingViewDelegate: NSObject {
- /**
- 数据刷新
- */
- func reloadData(data: [KMBatchProcessingTableViewModel])
- }
- extension KMBatchProcessingView: KMSelectPopButtonDelegate {
- func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
- if (self.addVC == obj) {
- let index = self.addVC.indexOfSelectedItem
- if index < 0 {
- return
- }
- let familyString = self.addVC.stringValue
-
- if familyString == NSLocalizedString("Add Files", comment: "") {
- self.addFiles()
- } else if familyString == NSLocalizedString("Add Folder", comment: "") {
- self.addFolder()
- } else if familyString == NSLocalizedString("Add Open Files", comment: "") {
- self.addOpenFiles()
- }
- }
- }
-
- func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
-
- }
- }
|