123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- //
- // KMPageEditSplitSettingView.swift
- // PDF Master
- //
- // Created by tangchao on 2023/1/12.
- //
- import Cocoa
- class KMPageEditSplitSettingView: KMPageEditSettingBaseView {
- @IBOutlet weak var typeLabel: NSTextField!
-
- @IBOutlet weak var oneFileForOnePageRadio: NSButton!
- @IBOutlet weak var oneFileForOnePageView: NSView!
- @IBOutlet weak var oneFileForOnePageTextField: NSTextField!
- @IBOutlet weak var oneFileForOnePageStepper: NSStepper!
- @IBOutlet weak var oneFileForOnePageOtherLabel: NSTextField!
-
- @IBOutlet weak var splitMuitiFileRadio: NSButton!
- @IBOutlet weak var splitMuitiFileView: NSView!
- @IBOutlet weak var splitMuitiFileStepper: NSStepper!
- @IBOutlet weak var splitMuitiFileTextField: NSTextField!
- @IBOutlet weak var splitMuitiFileOtherLabel: NSTextField!
-
- @IBOutlet weak var pageRangeRadio: NSButton!
- @IBOutlet weak var pageRangeComboBox: NSComboBox!
-
-
- @IBOutlet weak var nameLabel: NSTextField!
- @IBOutlet weak var nameView: NSView!
- @IBOutlet weak var nameViewLabel: NSTextField!
- @IBOutlet weak var prefixForOrigFileNameCheck: NSButton!
- @IBOutlet weak var tagCheck: NSButton!
- @IBOutlet weak var sepCheck: NSButton!
- @IBOutlet weak var tagView: NSView!
- @IBOutlet weak var tagTextField: NSTextField!
- @IBOutlet weak var sepView: NSView!
- @IBOutlet weak var sepTextView: NSTextField!
-
- var type: Int {
- get {
- -1
- }
- set {
- if (newValue == 0) {
- self.selectRadio(radio: self.oneFileForOnePageRadio)
- } else if (newValue == 1) {
- self.selectRadio(radio: self.splitMuitiFileRadio)
- } else if (newValue == 2) {
- self.selectRadio(radio: self.pageRangeRadio)
- }
- }
- }
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- self.typeLabel.stringValue = NSLocalizedString("Splitting Method", comment: "")
- self.oneFileForOnePageRadio.title = NSLocalizedString("Average per", comment: "")
- self.oneFileForOnePageTextField.stringValue = "1"
- self.oneFileForOnePageStepper.minValue = 1
- self.oneFileForOnePageStepper.integerValue = 1
- self.oneFileForOnePageOtherLabel.stringValue = NSLocalizedString("page split into a PDF file", comment: "")
-
- self.splitMuitiFileRadio.title = NSLocalizedString("Average split into", comment: "")
- self.splitMuitiFileTextField.stringValue = "1"
- self.splitMuitiFileStepper.minValue = 1
- self.splitMuitiFileStepper.integerValue = 1
- self.splitMuitiFileOtherLabel.stringValue = NSLocalizedString("PDF files", comment: "")
-
- self.pageRangeRadio.title = NSLocalizedString("Split by page atmosphere", comment: "")
- self.pageRangeComboBox.removeAllItems()
- self.pageRangeComboBox.addItems(withObjectValues: ["Odd Pages","Even Pages","Customize"])
- self.pageRangeComboBox.placeholderString = "e.g. 1,3-5,10"
- self.pageRangeComboBox.selectItem(at: 0)
- self.pageRangeComboBox.isEditable = false
- self.pageRangeComboBox.delegate = self
-
- for radio in [self.oneFileForOnePageRadio, self.splitMuitiFileRadio, self.pageRangeRadio] {
- radio?.target = self
- radio?.action = #selector(radioAction)
- }
- self.selectRadio(radio: self.oneFileForOnePageRadio)
-
- self.nameLabel.stringValue = NSLocalizedString("File Naming", comment: "")
- self.prefixForOrigFileNameCheck.title = NSLocalizedString("Precede the original document name", comment: "")
- self.tagCheck.title = NSLocalizedString("Tags", comment: "")
- self.sepCheck.title = NSLocalizedString("Separator", comment: "")
- self.tagTextField.stringValue = "part"
- self.sepTextView.stringValue = "-"
-
- for textfield in [self.oneFileForOnePageTextField, self.splitMuitiFileTextField, self.tagTextField, self.sepTextView] {
- textfield?.isEditable = true
- textfield?.focusRingType = .none
- textfield?.isBordered = false
- textfield?.delegate = self
- }
- self.oneFileForOnePageTextField.formatter = TextFieldFormatter()
- self.splitMuitiFileTextField.formatter = TextFieldFormatter()
-
- for stepper in [self.oneFileForOnePageStepper, self.splitMuitiFileStepper] {
- stepper?.target = self
- stepper?.action = #selector(stepperAction)
- }
-
- for view in [self.oneFileForOnePageView, self.splitMuitiFileView] {
- view?.wantsLayer = true
- view?.layer?.backgroundColor = NSColor(white: 238.0/255.0, alpha: 1.0).cgColor
- view?.layer?.cornerRadius = 2
- }
-
- for view in [self.nameView, self.tagView, self.sepView] {
- view?.wantsLayer = true
- view?.layer?.borderWidth = 1
- view?.layer?.borderColor = NSColor.black.cgColor
- view?.layer?.cornerRadius = 2
- }
-
- for check in [self.prefixForOrigFileNameCheck, self.tagCheck, self.sepCheck] {
- check?.target = self
- check?.action = #selector(checkAction)
- }
- }
-
- @objc func radioAction(sender: NSButton) {
- selectRadio(radio: sender)
-
- guard let callback = self.itemClick else {
- return
- }
-
- var index: Int = 0
- if (sender.isEqual(to: self.splitMuitiFileRadio)) {
- index = 1
- } else if (sender.isEqual(to: self.pageRangeRadio)) {
- index = 2
- }
- callback(1, index)
- }
-
- @objc func stepperAction(sender: NSStepper) {
- var index: Int = 2
- if (self.oneFileForOnePageStepper.isEqual(to: sender)) {
- self.oneFileForOnePageTextField.stringValue = "\(sender.integerValue)"
- } else if (self.splitMuitiFileStepper.isEqual(to: sender)) {
- self.splitMuitiFileTextField.stringValue = "\(sender.integerValue)"
- index = 3
- }
-
- guard let callback = self.itemClick else {
- return
- }
-
- callback(index, sender.integerValue)
- }
-
- @objc func checkAction(sender: NSButton) {
- if (self.tagCheck.state == .on) {
- self.tagTextField.isEnabled = true
- } else {
- self.tagTextField.isEnabled = false
- }
-
- if (sepCheck.state == .on) {
- self.sepTextView.isEnabled = true
- } else {
- self.sepTextView.isEnabled = false
- }
-
- var index: Int = 6
- if (sender.isEqual(to: self.prefixForOrigFileNameCheck)) {
- index = 6
- } else if (sender.isEqual(to: self.tagCheck)) {
- index = 7
- } else if (sender.isEqual(to: self.sepCheck)) {
- index = 8
- }
-
- guard let callback = self.itemClick else {
- return
- }
- callback(index, sender.state == .on)
- }
-
- public func setPageRangeString(_ string: String) {
- self.pageRangeComboBox.stringValue = string
- self.pageRangeComboBox.isEditable = true
-
- self.window?.makeFirstResponder(self.pageRangeComboBox)
- }
-
- public func updateName(_ name: String) {
- self.nameViewLabel.stringValue = name
- }
-
- // MARK: Private Methods
-
- private func selectRadio(radio: NSButton) {
- for item in [self.oneFileForOnePageRadio, self.splitMuitiFileRadio, self.pageRangeRadio] {
- if (radio.isEqual(to: item)) {
- item?.state = .on
- } else {
- item?.state = .off
- }
- }
-
- if (self.oneFileForOnePageRadio.state == .on) {
- self.oneFileForOnePageTextField.isEnabled = true
- self.oneFileForOnePageStepper.isEnabled = true
- } else {
- self.oneFileForOnePageTextField.isEnabled = false
- self.oneFileForOnePageStepper.isEnabled = false
- }
-
- if (self.splitMuitiFileRadio.state == .on) {
- self.splitMuitiFileTextField.isEnabled = true
- self.splitMuitiFileStepper.isEnabled = true
- } else {
- self.splitMuitiFileTextField.isEnabled = false
- self.splitMuitiFileStepper.isEnabled = false
- }
-
- if (self.pageRangeRadio.state == .on) {
- self.pageRangeComboBox.isEnabled = true
- } else {
- self.pageRangeComboBox.isEnabled = false
- }
- }
-
- }
- extension KMPageEditSplitSettingView: NSTextFieldDelegate {
- func controlTextDidChange(_ obj: Notification) {
- if (self.oneFileForOnePageTextField.isEqual(to: obj.object)) {
- if (self.oneFileForOnePageTextField.integerValue <= 0) {
- self.oneFileForOnePageTextField.stringValue = "1"
- self.oneFileForOnePageStepper.integerValue = 1
- } else if (self.oneFileForOnePageTextField.integerValue > Int(self.oneFileForOnePageStepper.maxValue)) {
- self.oneFileForOnePageTextField.stringValue = "\(Int(self.oneFileForOnePageStepper.maxValue))"
- self.oneFileForOnePageStepper.integerValue = Int(self.oneFileForOnePageStepper.maxValue)
- } else {
- self.oneFileForOnePageStepper.integerValue = self.oneFileForOnePageTextField.integerValue
- }
-
- guard let callback = self.itemClick else {
- return
- }
-
- callback(2, self.oneFileForOnePageStepper.integerValue)
- } else if (self.splitMuitiFileTextField.isEqual(to: obj.object)) {
- if (self.splitMuitiFileTextField.integerValue <= 0) {
- self.splitMuitiFileTextField.stringValue = "1"
- self.splitMuitiFileStepper.integerValue = 1
- } else if (self.splitMuitiFileTextField.integerValue > Int(self.splitMuitiFileStepper.maxValue)) {
- self.splitMuitiFileTextField.stringValue = "\(Int(self.splitMuitiFileStepper.maxValue))"
- self.splitMuitiFileStepper.integerValue = Int(self.splitMuitiFileStepper.maxValue)
- } else {
- self.splitMuitiFileStepper.integerValue = self.splitMuitiFileTextField.integerValue
- }
-
- guard let callback = self.itemClick else {
- return
- }
-
- callback(3, self.splitMuitiFileStepper.integerValue)
- } else if (self.pageRangeComboBox.isEqual(to: obj.object)) {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(5, self.pageRangeComboBox.stringValue)
- } else if (self.tagTextField.isEqual(to: obj.object)) {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(9, self.tagTextField.stringValue)
- } else if (self.sepTextView.isEqual(to: obj.object)) {
- guard let callback = self.itemClick else {
- return
- }
-
- callback(10, self.sepTextView.stringValue)
- }
- }
- }
- extension KMPageEditSplitSettingView: NSComboBoxDelegate {
- func comboBoxSelectionDidChange(_ notification: Notification) {
- if (self.pageRangeComboBox.isEqual(to: notification.object)) {
- var index = self.pageRangeComboBox.indexOfSelectedItem
- if (index < 0) {
- index = 0
- }
-
- if (index == 0) { /// 奇数页
- self.pageRangeComboBox.isEditable = false
- } else if (index == 1) { /// 偶数页
- self.pageRangeComboBox.isEditable = false
- } else { /// ziding
- self.pageRangeComboBox.isEditable = true
-
- DispatchQueue.main.async {
- self.pageRangeComboBox.stringValue = ""
- }
- }
-
- guard let callback = self.itemClick else {
- return
- }
-
- callback(4, index)
- }
- }
- }
|