CDistanceSettingWindowController.swift 12 KB

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