CAreaSettingWindowController.swift 12 KB

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