CDistanceSettingWindowController.swift 12 KB

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