CAreaSettingWindowController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // CAreaSettingWindowController.swift
  3. // Cisdem PDFMaster
  4. //
  5. // Created by wanjun on 2024/7/16.
  6. //
  7. import Cocoa
  8. @objc protocol CAreaSettingWindowControllerDelegate: AnyObject {
  9. @objc optional func areaSettingWindowController(_ areaSettingWindowController: CAreaSettingWindowController, measureInfo: CPDFAreaMeasureInfo?)
  10. }
  11. class CAreaSettingWindowController: NSWindowController, NSTextFieldDelegate {
  12. @IBOutlet weak var rulerBaseTextField: NSTextField!
  13. @IBOutlet weak var rulerBasePopButton: NSPopUpButton!
  14. @IBOutlet weak var rulerTranslateTextField: NSTextField!
  15. @IBOutlet weak var rulerTranslatePopButton: NSPopUpButton!
  16. @IBOutlet weak var precisionPopButton: NSPopUpButton!
  17. @IBOutlet weak var isLengthCaptionButton: NSButton!
  18. @IBOutlet weak var isAreaCaptionButton: NSButton!
  19. @IBOutlet weak var doneButton: NSButton!
  20. @objc weak var delegate: CAreaSettingWindowControllerDelegate?
  21. private var measureInfo: CPDFAreaMeasureInfo
  22. private var modalSession: NSApplication.ModalSession?
  23. @objc init(measureInfo: CPDFAreaMeasureInfo) {
  24. self.measureInfo = measureInfo
  25. super.init(window: nil)
  26. // self.windowNibName = "CAreaSettingWindowController"
  27. Bundle.main.loadNibNamed("CAreaSettingWindowController", owner: self, topLevelObjects: nil)
  28. commonInit()
  29. }
  30. required init?(coder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. override func windowDidLoad() {
  34. super.windowDidLoad()
  35. // commonInit()
  36. }
  37. private func commonInit() {
  38. let me = measureInfo
  39. switch me.captionType {
  40. case CPDFCaptionType(rawValue: 0):
  41. isAreaCaptionButton.state = .off
  42. isLengthCaptionButton.state = .off
  43. case .area:
  44. isAreaCaptionButton.state = .on
  45. isLengthCaptionButton.state = .off
  46. case .length:
  47. isAreaCaptionButton.state = .off
  48. isLengthCaptionButton.state = .on
  49. case [.area, .length]:
  50. isAreaCaptionButton.state = .on
  51. isLengthCaptionButton.state = .on
  52. default:
  53. break
  54. }
  55. rulerBaseTextField.stringValue = String(format: "%0.0f", me.rulerBase)
  56. rulerTranslateTextField.stringValue = String(format: "%0.0f", me.rulerTranslate)
  57. rulerBasePopButton.removeAllItems()
  58. rulerBasePopButton.addItems(withTitles: ["in", "cm", "mm"])
  59. selectRulerBasePopButton(measureInfo: me)
  60. rulerTranslatePopButton.removeAllItems()
  61. rulerTranslatePopButton.addItems(withTitles: ["pt", "cm", "in", "mm", "m", "km", "ft", "yd", "mi"])
  62. selectRulerTranslatePopButton(measureInfo: me)
  63. precisionPopButton.removeAllItems()
  64. precisionPopButton.addItems(withTitles: ["1", "0.1", "0.01", "0.001", "0.0001"])
  65. selectPrecisionPopButton(measureInfo: me)
  66. rulerBaseTextField.delegate = self
  67. rulerTranslateTextField.delegate = self
  68. }
  69. private func selectRulerBasePopButton(measureInfo: CPDFAreaMeasureInfo) {
  70. let constants = CPDFMeasureConstants.sharedInstance()
  71. if measureInfo.rulerBaseUnit == constants.cpdfIn {
  72. rulerBasePopButton.selectItem(withTitle: "in")
  73. } else if measureInfo.rulerBaseUnit == constants.cpdfCm {
  74. rulerBasePopButton.selectItem(withTitle: "cm")
  75. } else if measureInfo.rulerBaseUnit == constants.cpdfMm {
  76. rulerBasePopButton.selectItem(withTitle: "mm")
  77. }
  78. }
  79. private func selectRulerTranslatePopButton(measureInfo: CPDFAreaMeasureInfo) {
  80. let constants = CPDFMeasureConstants.sharedInstance()
  81. if measureInfo.rulerTranslateUnit == constants.cpdfPt {
  82. rulerTranslatePopButton.selectItem(withTitle: "pt")
  83. } else if measureInfo.rulerTranslateUnit == constants.cpdfIn {
  84. rulerTranslatePopButton.selectItem(withTitle: "in")
  85. } else if measureInfo.rulerTranslateUnit == constants.cpdfCm {
  86. rulerTranslatePopButton.selectItem(withTitle: "cm")
  87. } else if measureInfo.rulerTranslateUnit == constants.cpdfMm {
  88. rulerTranslatePopButton.selectItem(withTitle: "mm")
  89. } else if measureInfo.rulerTranslateUnit == constants.cpdfM {
  90. rulerTranslatePopButton.selectItem(withTitle: "m")
  91. } else if measureInfo.rulerTranslateUnit == constants.cpdfKm {
  92. rulerTranslatePopButton.selectItem(withTitle: "km")
  93. } else if measureInfo.rulerTranslateUnit == constants.cpdfFt {
  94. rulerTranslatePopButton.selectItem(withTitle: "ft")
  95. } else if measureInfo.rulerTranslateUnit == constants.cpdfYd {
  96. rulerTranslatePopButton.selectItem(withTitle: "yd")
  97. } else if measureInfo.rulerTranslateUnit == constants.cpdfMi {
  98. rulerTranslatePopButton.selectItem(withTitle: "mi")
  99. }
  100. }
  101. private func selectPrecisionPopButton(measureInfo: CPDFAreaMeasureInfo) {
  102. switch measureInfo.precision {
  103. case 1:
  104. precisionPopButton.selectItem(withTitle: "1")
  105. case 10:
  106. precisionPopButton.selectItem(withTitle: "0.1")
  107. case 100:
  108. precisionPopButton.selectItem(withTitle: "0.01")
  109. case 1000:
  110. precisionPopButton.selectItem(withTitle: "0.001")
  111. case 10000:
  112. precisionPopButton.selectItem(withTitle: "0.0001")
  113. default:
  114. break
  115. }
  116. }
  117. @IBAction func startModal(_ sender: Any) {
  118. NSApp.stopModal()
  119. self.modalSession = NSApp.beginModalSession(for: self.window!)
  120. var modalCode: NSApplication.ModalResponse
  121. repeat {
  122. modalCode = NSApp.runModalSession(self.modalSession!)
  123. } while modalCode == .continue
  124. }
  125. @IBAction func endModal(_ sender: Any) {
  126. if let session = modalSession {
  127. NSApp.stopModal()
  128. NSApp.endModalSession(session)
  129. self.window?.orderOut(self)
  130. }
  131. }
  132. func controlTextDidChange(_ obj: Notification) {
  133. if rulerBaseTextField.stringValue.count > 6 {
  134. rulerBaseTextField.stringValue = String(rulerBaseTextField.stringValue.prefix(6))
  135. } else if rulerTranslateTextField.stringValue.count > 6 {
  136. rulerTranslateTextField.stringValue = String(rulerTranslateTextField.stringValue.prefix(6))
  137. }
  138. let rulerBaseText = rulerBaseTextField.stringValue
  139. let rulerTranslateText = rulerTranslateTextField.stringValue
  140. let nonDigits = CharacterSet.decimalDigits.inverted
  141. if rulerBaseText.rangeOfCharacter(from: nonDigits) != nil ||
  142. rulerTranslateText.rangeOfCharacter(from: nonDigits) != nil ||
  143. rulerBaseText.isEmpty ||
  144. rulerTranslateText.isEmpty ||
  145. rulerBaseText == "0" ||
  146. rulerTranslateText == "0" {
  147. doneButton.isEnabled = false
  148. } else {
  149. doneButton.isEnabled = true
  150. }
  151. }
  152. @IBAction func buttonItemClick_Rulerbase(_ sender: NSPopUpButton) {
  153. let selectIndex = sender.indexOfSelectedItem
  154. let constants = CPDFMeasureConstants.sharedInstance()
  155. var rulerBaseUnit = ""
  156. switch selectIndex {
  157. case 0:
  158. rulerBaseUnit = constants.cpdfIn
  159. case 1:
  160. rulerBaseUnit = constants.cpdfCm
  161. case 2:
  162. rulerBaseUnit = constants.cpdfMm
  163. default:
  164. break
  165. }
  166. measureInfo.rulerBaseUnit = rulerBaseUnit
  167. }
  168. @IBAction func buttonItemClick_Rulertranslate(_ sender: NSPopUpButton) {
  169. let selectIndex = sender.indexOfSelectedItem
  170. let constants = CPDFMeasureConstants.sharedInstance()
  171. var rulerTranslateUnit = ""
  172. var factor: CGFloat = 0
  173. switch selectIndex {
  174. case 0:
  175. rulerTranslateUnit = constants.cpdfPt
  176. factor = constants.ptPt
  177. case 1:
  178. rulerTranslateUnit = constants.cpdfCm
  179. factor = constants.ptCm
  180. case 2:
  181. rulerTranslateUnit = constants.cpdfIn
  182. factor = constants.ptIn
  183. case 3:
  184. rulerTranslateUnit = constants.cpdfMm
  185. factor = constants.ptMm
  186. case 4:
  187. rulerTranslateUnit = constants.cpdfM
  188. factor = constants.ptM
  189. case 5:
  190. rulerTranslateUnit = constants.cpdfKm
  191. factor = constants.ptKm
  192. case 6:
  193. rulerTranslateUnit = constants.cpdfFt
  194. factor = constants.ptFt
  195. case 7:
  196. rulerTranslateUnit = constants.cpdfYd
  197. factor = constants.ptYd
  198. case 8:
  199. rulerTranslateUnit = constants.cpdfMi
  200. factor = constants.ptMi
  201. default:
  202. break
  203. }
  204. measureInfo.rulerTranslateUnit = rulerTranslateUnit
  205. measureInfo.factor = factor
  206. }
  207. @IBAction func buttonItemClick_Precision(_ sender: NSPopUpButton) {
  208. let selectIndex = sender.indexOfSelectedItem
  209. let constants = CPDFMeasureConstants.sharedInstance()
  210. var precision = 0
  211. switch selectIndex {
  212. case 0:
  213. precision = constants.precisionValueZero
  214. case 1:
  215. precision = constants.precisionValueOne
  216. case 2:
  217. precision = constants.precisionValueTwo
  218. case 3:
  219. precision = constants.precisionValueThree
  220. case 4:
  221. precision = constants.precisionValueFour
  222. default:
  223. break
  224. }
  225. measureInfo.precision = precision
  226. }
  227. @IBAction func buttonItemClick_Cancel(_ sender: Any) {
  228. delegate?.areaSettingWindowController?(self, measureInfo: nil)
  229. endModal("")
  230. }
  231. @IBAction func buttonItemClick_Done(_ sender: Any) {
  232. if let delegate = delegate {
  233. measureInfo.rulerBase = CGFloat(Float(rulerBaseTextField.stringValue) ?? 0)
  234. measureInfo.rulerTranslate = CGFloat(Float(rulerTranslateTextField.stringValue) ?? 0)
  235. let isLength = isLengthCaptionButton.state == .on
  236. let isArea = isAreaCaptionButton.state == .on
  237. if isLength && isArea {
  238. measureInfo.captionType = [.length, .area]
  239. } else if isLength {
  240. measureInfo.captionType = .length
  241. } else if isArea {
  242. measureInfo.captionType = .area
  243. } else {
  244. measureInfo.captionType = CPDFCaptionType(rawValue: 0)
  245. }
  246. delegate.areaSettingWindowController?(self, measureInfo: measureInfo)
  247. }
  248. endModal("")
  249. }
  250. @IBAction func buttonItemClick_Length(_ sender: Any) {}
  251. @IBAction func buttonItemClick_Area(_ sender: Any) {}
  252. }