KMPageEditSplitSettingView.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // KMPageEditSplitSettingView.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/1/12.
  6. //
  7. import Cocoa
  8. class KMPageEditSplitSettingView: KMPageEditSettingBaseView {
  9. @IBOutlet weak var typeLabel: NSTextField!
  10. @IBOutlet weak var oneFileForOnePageRadio: NSButton!
  11. @IBOutlet weak var oneFileForOnePageView: NSView!
  12. @IBOutlet weak var oneFileForOnePageTextField: NSTextField!
  13. @IBOutlet weak var oneFileForOnePageStepper: NSStepper!
  14. @IBOutlet weak var oneFileForOnePageOtherLabel: NSTextField!
  15. @IBOutlet weak var splitMuitiFileRadio: NSButton!
  16. @IBOutlet weak var splitMuitiFileView: NSView!
  17. @IBOutlet weak var splitMuitiFileStepper: NSStepper!
  18. @IBOutlet weak var splitMuitiFileTextField: NSTextField!
  19. @IBOutlet weak var splitMuitiFileOtherLabel: NSTextField!
  20. @IBOutlet weak var pageRangeRadio: NSButton!
  21. @IBOutlet weak var pageRangeComboBox: NSComboBox!
  22. @IBOutlet weak var nameLabel: NSTextField!
  23. @IBOutlet weak var nameView: NSView!
  24. @IBOutlet weak var nameViewLabel: NSTextField!
  25. @IBOutlet weak var prefixForOrigFileNameCheck: NSButton!
  26. @IBOutlet weak var tagCheck: NSButton!
  27. @IBOutlet weak var sepCheck: NSButton!
  28. @IBOutlet weak var tagView: NSView!
  29. @IBOutlet weak var tagTextField: NSTextField!
  30. @IBOutlet weak var sepView: NSView!
  31. @IBOutlet weak var sepTextView: NSTextField!
  32. var type: Int {
  33. get {
  34. -1
  35. }
  36. set {
  37. if (newValue == 0) {
  38. self.selectRadio(radio: self.oneFileForOnePageRadio)
  39. } else if (newValue == 1) {
  40. self.selectRadio(radio: self.splitMuitiFileRadio)
  41. } else if (newValue == 2) {
  42. self.selectRadio(radio: self.pageRangeRadio)
  43. }
  44. }
  45. }
  46. override func awakeFromNib() {
  47. super.awakeFromNib()
  48. self.typeLabel.stringValue = NSLocalizedString("Splitting Method", comment: "")
  49. self.oneFileForOnePageRadio.title = NSLocalizedString("Average per", comment: "")
  50. self.oneFileForOnePageTextField.stringValue = "1"
  51. self.oneFileForOnePageStepper.minValue = 1
  52. self.oneFileForOnePageStepper.integerValue = 1
  53. self.oneFileForOnePageOtherLabel.stringValue = NSLocalizedString("page split into a PDF file", comment: "")
  54. self.splitMuitiFileRadio.title = NSLocalizedString("Average split into", comment: "")
  55. self.splitMuitiFileTextField.stringValue = "1"
  56. self.splitMuitiFileStepper.minValue = 1
  57. self.splitMuitiFileStepper.integerValue = 1
  58. self.splitMuitiFileOtherLabel.stringValue = NSLocalizedString("PDF files", comment: "")
  59. self.pageRangeRadio.title = NSLocalizedString("Split by page atmosphere", comment: "")
  60. self.pageRangeComboBox.removeAllItems()
  61. self.pageRangeComboBox.addItems(withObjectValues: ["Odd Pages","Even Pages","Customize"])
  62. self.pageRangeComboBox.placeholderString = "e.g. 1,3-5,10"
  63. self.pageRangeComboBox.selectItem(at: 0)
  64. self.pageRangeComboBox.isEditable = false
  65. self.pageRangeComboBox.delegate = self
  66. for radio in [self.oneFileForOnePageRadio, self.splitMuitiFileRadio, self.pageRangeRadio] {
  67. radio?.target = self
  68. radio?.action = #selector(radioAction)
  69. }
  70. self.selectRadio(radio: self.oneFileForOnePageRadio)
  71. self.nameLabel.stringValue = NSLocalizedString("File Naming", comment: "")
  72. self.prefixForOrigFileNameCheck.title = NSLocalizedString("Precede the original document name", comment: "")
  73. self.tagCheck.title = NSLocalizedString("Tags", comment: "")
  74. self.sepCheck.title = NSLocalizedString("Separator", comment: "")
  75. self.tagTextField.stringValue = "part"
  76. self.sepTextView.stringValue = "-"
  77. for textfield in [self.oneFileForOnePageTextField, self.splitMuitiFileTextField, self.tagTextField, self.sepTextView] {
  78. textfield?.isEditable = true
  79. textfield?.focusRingType = .none
  80. textfield?.isBordered = false
  81. textfield?.delegate = self
  82. }
  83. self.oneFileForOnePageTextField.formatter = TextFieldFormatter()
  84. self.splitMuitiFileTextField.formatter = TextFieldFormatter()
  85. for stepper in [self.oneFileForOnePageStepper, self.splitMuitiFileStepper] {
  86. stepper?.target = self
  87. stepper?.action = #selector(stepperAction)
  88. }
  89. for view in [self.oneFileForOnePageView, self.splitMuitiFileView] {
  90. view?.wantsLayer = true
  91. view?.layer?.backgroundColor = NSColor(white: 238.0/255.0, alpha: 1.0).cgColor
  92. view?.layer?.cornerRadius = 2
  93. }
  94. for view in [self.nameView, self.tagView, self.sepView] {
  95. view?.wantsLayer = true
  96. view?.layer?.borderWidth = 1
  97. view?.layer?.borderColor = NSColor.black.cgColor
  98. view?.layer?.cornerRadius = 2
  99. }
  100. for check in [self.prefixForOrigFileNameCheck, self.tagCheck, self.sepCheck] {
  101. check?.target = self
  102. check?.action = #selector(checkAction)
  103. }
  104. }
  105. @objc func radioAction(sender: NSButton) {
  106. selectRadio(radio: sender)
  107. guard let callback = self.itemClick else {
  108. return
  109. }
  110. var index: Int = 0
  111. if (sender.isEqual(to: self.splitMuitiFileRadio)) {
  112. index = 1
  113. } else if (sender.isEqual(to: self.pageRangeRadio)) {
  114. index = 2
  115. }
  116. callback(1, index)
  117. }
  118. @objc func stepperAction(sender: NSStepper) {
  119. var index: Int = 2
  120. if (self.oneFileForOnePageStepper.isEqual(to: sender)) {
  121. self.oneFileForOnePageTextField.stringValue = "\(sender.integerValue)"
  122. } else if (self.splitMuitiFileStepper.isEqual(to: sender)) {
  123. self.splitMuitiFileTextField.stringValue = "\(sender.integerValue)"
  124. index = 3
  125. }
  126. guard let callback = self.itemClick else {
  127. return
  128. }
  129. callback(index, sender.integerValue)
  130. }
  131. @objc func checkAction(sender: NSButton) {
  132. if (self.tagCheck.state == .on) {
  133. self.tagTextField.isEnabled = true
  134. } else {
  135. self.tagTextField.isEnabled = false
  136. }
  137. if (sepCheck.state == .on) {
  138. self.sepTextView.isEnabled = true
  139. } else {
  140. self.sepTextView.isEnabled = false
  141. }
  142. var index: Int = 6
  143. if (sender.isEqual(to: self.prefixForOrigFileNameCheck)) {
  144. index = 6
  145. } else if (sender.isEqual(to: self.tagCheck)) {
  146. index = 7
  147. } else if (sender.isEqual(to: self.sepCheck)) {
  148. index = 8
  149. }
  150. guard let callback = self.itemClick else {
  151. return
  152. }
  153. callback(index, sender.state == .on)
  154. }
  155. public func setPageRangeString(_ string: String) {
  156. self.pageRangeComboBox.stringValue = string
  157. self.pageRangeComboBox.isEditable = true
  158. self.window?.makeFirstResponder(self.pageRangeComboBox)
  159. }
  160. public func updateName(_ name: String) {
  161. self.nameViewLabel.stringValue = name
  162. }
  163. // MARK: Private Methods
  164. private func selectRadio(radio: NSButton) {
  165. for item in [self.oneFileForOnePageRadio, self.splitMuitiFileRadio, self.pageRangeRadio] {
  166. if (radio.isEqual(to: item)) {
  167. item?.state = .on
  168. } else {
  169. item?.state = .off
  170. }
  171. }
  172. if (self.oneFileForOnePageRadio.state == .on) {
  173. self.oneFileForOnePageTextField.isEnabled = true
  174. self.oneFileForOnePageStepper.isEnabled = true
  175. } else {
  176. self.oneFileForOnePageTextField.isEnabled = false
  177. self.oneFileForOnePageStepper.isEnabled = false
  178. }
  179. if (self.splitMuitiFileRadio.state == .on) {
  180. self.splitMuitiFileTextField.isEnabled = true
  181. self.splitMuitiFileStepper.isEnabled = true
  182. } else {
  183. self.splitMuitiFileTextField.isEnabled = false
  184. self.splitMuitiFileStepper.isEnabled = false
  185. }
  186. if (self.pageRangeRadio.state == .on) {
  187. self.pageRangeComboBox.isEnabled = true
  188. } else {
  189. self.pageRangeComboBox.isEnabled = false
  190. }
  191. }
  192. }
  193. extension KMPageEditSplitSettingView: NSTextFieldDelegate {
  194. func controlTextDidChange(_ obj: Notification) {
  195. if (self.oneFileForOnePageTextField.isEqual(to: obj.object)) {
  196. if (self.oneFileForOnePageTextField.integerValue <= 0) {
  197. self.oneFileForOnePageTextField.stringValue = "1"
  198. self.oneFileForOnePageStepper.integerValue = 1
  199. } else if (self.oneFileForOnePageTextField.integerValue > Int(self.oneFileForOnePageStepper.maxValue)) {
  200. self.oneFileForOnePageTextField.stringValue = "\(Int(self.oneFileForOnePageStepper.maxValue))"
  201. self.oneFileForOnePageStepper.integerValue = Int(self.oneFileForOnePageStepper.maxValue)
  202. } else {
  203. self.oneFileForOnePageStepper.integerValue = self.oneFileForOnePageTextField.integerValue
  204. }
  205. guard let callback = self.itemClick else {
  206. return
  207. }
  208. callback(2, self.oneFileForOnePageStepper.integerValue)
  209. } else if (self.splitMuitiFileTextField.isEqual(to: obj.object)) {
  210. if (self.splitMuitiFileTextField.integerValue <= 0) {
  211. self.splitMuitiFileTextField.stringValue = "1"
  212. self.splitMuitiFileStepper.integerValue = 1
  213. } else if (self.splitMuitiFileTextField.integerValue > Int(self.splitMuitiFileStepper.maxValue)) {
  214. self.splitMuitiFileTextField.stringValue = "\(Int(self.splitMuitiFileStepper.maxValue))"
  215. self.splitMuitiFileStepper.integerValue = Int(self.splitMuitiFileStepper.maxValue)
  216. } else {
  217. self.splitMuitiFileStepper.integerValue = self.splitMuitiFileTextField.integerValue
  218. }
  219. guard let callback = self.itemClick else {
  220. return
  221. }
  222. callback(3, self.splitMuitiFileStepper.integerValue)
  223. } else if (self.pageRangeComboBox.isEqual(to: obj.object)) {
  224. guard let callback = self.itemClick else {
  225. return
  226. }
  227. callback(5, self.pageRangeComboBox.stringValue)
  228. } else if (self.tagTextField.isEqual(to: obj.object)) {
  229. guard let callback = self.itemClick else {
  230. return
  231. }
  232. callback(9, self.tagTextField.stringValue)
  233. } else if (self.sepTextView.isEqual(to: obj.object)) {
  234. guard let callback = self.itemClick else {
  235. return
  236. }
  237. callback(10, self.sepTextView.stringValue)
  238. }
  239. }
  240. }
  241. extension KMPageEditSplitSettingView: NSComboBoxDelegate {
  242. func comboBoxSelectionDidChange(_ notification: Notification) {
  243. if (self.pageRangeComboBox.isEqual(to: notification.object)) {
  244. var index = self.pageRangeComboBox.indexOfSelectedItem
  245. if (index < 0) {
  246. index = 0
  247. }
  248. if (index == 0) { /// 奇数页
  249. self.pageRangeComboBox.isEditable = false
  250. } else if (index == 1) { /// 偶数页
  251. self.pageRangeComboBox.isEditable = false
  252. } else { /// ziding
  253. self.pageRangeComboBox.isEditable = true
  254. DispatchQueue.main.async {
  255. self.pageRangeComboBox.stringValue = ""
  256. }
  257. }
  258. guard let callback = self.itemClick else {
  259. return
  260. }
  261. callback(4, index)
  262. }
  263. }
  264. }