KMMeasureController.swift 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. //
  2. // KMMeasureController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by Niehaoyu on 2024/12/7.
  6. //
  7. import Cocoa
  8. import KMComponentLibrary
  9. class KMMeasureController: NSViewController {
  10. @IBOutlet var typeSegmented: ComponentSegmented!
  11. @IBOutlet var infoBGView: NSView!
  12. @IBOutlet var infoTopConst: NSLayoutConstraint!
  13. //Color
  14. @IBOutlet var colorBGView: NSView!
  15. @IBOutlet var colorLabel: NSTextField!
  16. @IBOutlet var fontColorGroup: ComponentCColorGroup!
  17. @IBOutlet var ColorGroup: ComponentCColorGroup!
  18. @IBOutlet var fillColorGroup: ComponentCColorGroup!
  19. @IBOutlet var fillColorImage: NSImageView!
  20. @IBOutlet var opacitySlider: ComponentSlider!
  21. @IBOutlet var opacitySelect: ComponentSelect!
  22. @IBOutlet var colorBGViewHeightConst: NSLayoutConstraint!
  23. @IBOutlet var opacityViewTopConst: NSLayoutConstraint!
  24. //Line
  25. @IBOutlet var lineBGView: NSView!
  26. @IBOutlet var lineLabel: NSTextField!
  27. @IBOutlet var lineTypeSelector: ComponentCSelectorGroup!
  28. @IBOutlet var lineWidthSlider: ComponentSlider!
  29. @IBOutlet var lineWidthSelect: ComponentSelect!
  30. @IBOutlet var linedashInfoView: NSView!
  31. @IBOutlet var lineDashSlider: ComponentSlider!
  32. @IBOutlet var lineDashSelect: ComponentSelect!
  33. @IBOutlet var lineBGViewHeightConst: NSLayoutConstraint!
  34. @IBOutlet var arrowTypeView: NSView!
  35. @IBOutlet var arrowTypeTopConst: NSLayoutConstraint!
  36. @IBOutlet var arrow_leftTypeSelect: ComponentSelect!
  37. @IBOutlet var arrow_RightTypeSelect: ComponentSelect!
  38. //Font
  39. @IBOutlet var fontBGView: NSView!
  40. @IBOutlet var fontLabel: NSTextField!
  41. @IBOutlet var fontNameSelect: ComponentSelect!
  42. @IBOutlet var fontStyleSelect: ComponentSelect!
  43. @IBOutlet var fontSizeSelect: ComponentSelect!
  44. //Display Label
  45. @IBOutlet var displayBGView: NSView!
  46. @IBOutlet var displayLabel: NSTextField!
  47. @IBOutlet var displayLengthCheckBox: ComponentCheckBox!
  48. @IBOutlet var displayArea_CheckBox: ComponentCheckBox!
  49. private let line_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_line_gray"), iconImage_active: NSImage(named: "measure_line"))
  50. private let multiLine_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_multiLine_gray"), iconImage_active: NSImage(named: "measure_multiLine"))
  51. private let polygon_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_polygon_gray"), iconImage_active: NSImage(named: "measure_polygon"))
  52. private let rectangle_Property = ComponentSegmentedProperty(size: .s, active: false, iconImage: NSImage(named: "measure_rectangle_gray"), iconImage_active: NSImage(named: "measure_rectangle"))
  53. private var familyNames = CPDFFont.familyNames
  54. private let solidProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_solid"))
  55. private let dashProperty = ComponentCSelectorProperty.init(size: .s, state: .normal, text: "", iconImage: NSImage(named: "lineStyle_dash"))
  56. private var annotations: [CPDFAnnotation] = []
  57. var annotationType: CAnnotationType = .measureLine
  58. var pdfView: CPDFListView?
  59. var viewManager: KMPDFViewManager?
  60. //MARK: - func
  61. override func viewDidAppear() {
  62. super.viewDidAppear()
  63. opacitySlider.reloadData()
  64. lineWidthSlider.reloadData()
  65. lineDashSlider.reloadData()
  66. }
  67. override func viewDidLoad() {
  68. super.viewDidLoad()
  69. // Do view setup here.
  70. annotationType = CPDFMeasureDefaultInfo.default_measureType()
  71. setupProperty()
  72. reloadData()
  73. }
  74. func setupProperty() {
  75. if true {
  76. var itemArr: [ComponentSegmentedProperty] = []
  77. itemArr.append(line_Property)
  78. itemArr.append(multiLine_Property)
  79. itemArr.append(polygon_Property)
  80. itemArr.append(rectangle_Property)
  81. typeSegmented.updateItemProperty(itemArr)
  82. }
  83. typeSegmented.delegate = self
  84. //Color
  85. colorLabel.stringValue = KMLocalizedString("Color")
  86. colorLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  87. colorLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  88. fontColorGroup.delegate = self
  89. ColorGroup.delegate = self
  90. fillColorGroup.delegate = self
  91. opacitySlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  92. opacitySlider.delegate = self
  93. opacitySelect.properties = ComponentSelectProperties(size: .s,
  94. state: .normal,
  95. creatable: true,
  96. text: "100%",
  97. textUnit: "%",
  98. regexString: "0123456789%")
  99. if true {
  100. var opacityItems: [ComponentMenuitemProperty] = []
  101. for string in ["25%", "50%", "75%", "100%"] {
  102. let item = ComponentMenuitemProperty(type: .normal, text: string)
  103. opacityItems.append(item)
  104. }
  105. opacitySelect.updateMenuItemsArr(opacityItems)
  106. }
  107. opacitySelect.delegate = self
  108. //Line
  109. lineLabel.stringValue = KMLocalizedString("Line")
  110. lineLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  111. lineLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  112. lineTypeSelector.updateItemProperty([solidProperty, dashProperty])
  113. lineTypeSelector.delegate = self
  114. lineWidthSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  115. lineWidthSlider.delegate = self
  116. lineWidthSelect.properties = ComponentSelectProperties(size: .s,
  117. state: .normal,
  118. creatable: true,
  119. text: "2",
  120. textUnit: " pt",
  121. regexString: "0123456789 pt")
  122. if true {
  123. var opacityItems: [ComponentMenuitemProperty] = []
  124. for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] {
  125. let item = ComponentMenuitemProperty(type: .normal, text: string)
  126. opacityItems.append(item)
  127. }
  128. lineWidthSelect.updateMenuItemsArr(opacityItems)
  129. }
  130. lineWidthSelect.delegate = self
  131. lineDashSlider.properties = ComponentSliderProperty(size: .m, percent: 1)
  132. lineDashSlider.delegate = self
  133. lineDashSelect.properties = ComponentSelectProperties(size: .s,
  134. state: .normal,
  135. creatable: true,
  136. text: "2",
  137. textUnit: " pt",
  138. regexString: "0123456789 pt")
  139. if true {
  140. var opacityItems: [ComponentMenuitemProperty] = []
  141. for string in ["1 pt", "2 pt", "4 pt", "6 pt", "8 pt", "10 pt"] {
  142. let item = ComponentMenuitemProperty(type: .normal, text: string)
  143. opacityItems.append(item)
  144. }
  145. lineDashSelect.updateMenuItemsArr(opacityItems)
  146. }
  147. lineDashSelect.delegate = self
  148. //ArrowType
  149. arrow_leftTypeSelect.properties = ComponentSelectProperties(size: .s,
  150. state: .normal,
  151. creatable: false)
  152. if true {
  153. let stringItems = ["None", "Arrow Open", "Arrow Close", "Square", "Oval", "Diamond", "Butt", "Slash", "R Open Arrow", "R Close Arrow"]
  154. let imageNames = ["arrowType_line", "arrowType_left_Arrow_open", "arrowType_left_Arrow_close",
  155. "arrowType_left_Square", "arrowType_left_Oval", "arrowType_left_Diamond",
  156. "arrowType_left_Butt", "arrowType_left_Slash", "arrowType_left_Open_arrow",
  157. "arrowType_left_Close_arrow"]
  158. var opacityItems: [ComponentMenuitemProperty] = []
  159. for i in 0...stringItems.count-1 {
  160. let string = stringItems[i]
  161. let imageName = imageNames[i]
  162. let item = ComponentMenuitemProperty(type: .normal, lefticon: NSImage(named: imageName), text: string, identifier: string)
  163. opacityItems.append(item)
  164. }
  165. arrow_leftTypeSelect.updateMenuItemsArr(opacityItems)
  166. }
  167. arrow_leftTypeSelect.delegate = self
  168. arrow_RightTypeSelect.properties = ComponentSelectProperties(size: .s,
  169. state: .normal,
  170. creatable: false)
  171. if true {
  172. let stringItems = ["None", "Arrow Open", "Arrow Close", "Square", "Oval", "Diamond", "Butt", "Slash", "R Open Arrow", "R Close Arrow"]
  173. let imageNames = ["arrowType_line", "arrowType_right_Arrow_open", "arrowType_right_Arrow_close",
  174. "arrowType_right_Square", "arrowType_right_Oval", "arrowType_right_Diamond",
  175. "arrowType_right_Butt", "arrowType_right_Slash", "arrowType_right_Open_arrow",
  176. "arrowType_right_Close_arrow"]
  177. var opacityItems: [ComponentMenuitemProperty] = []
  178. for i in 0...stringItems.count-1 {
  179. let string = stringItems[i]
  180. let imageName = imageNames[i]
  181. let item = ComponentMenuitemProperty(type: .normal, lefticon: NSImage(named: imageName), text: string, identifier: string)
  182. opacityItems.append(item)
  183. }
  184. arrow_RightTypeSelect.updateMenuItemsArr(opacityItems)
  185. }
  186. arrow_RightTypeSelect.delegate = self
  187. //Font
  188. fontNameSelect.properties = ComponentSelectProperties(size: .s,
  189. state: .normal,
  190. text: "")
  191. if true {
  192. var menuItemArr: [ComponentMenuitemProperty] = []
  193. for string in familyNames {
  194. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  195. menuItemArr.append(item)
  196. }
  197. fontNameSelect.updateMenuItemsArr(menuItemArr)
  198. }
  199. fontNameSelect.delegate = self
  200. fontStyleSelect.properties = ComponentSelectProperties(size: .s,
  201. state: .normal,
  202. text: "")
  203. fontStyleSelect.delegate = self
  204. fontSizeSelect.properties = ComponentSelectProperties(size: .s,
  205. state: .normal,
  206. creatable: true,
  207. text: "12 pt",
  208. textUnit: " pt",
  209. regexString: "0123456789")
  210. if true {
  211. var sizeItemArr: [ComponentMenuitemProperty] = []
  212. for string in KMHeaderFooterManager.getFontSize() {
  213. let item = ComponentMenuitemProperty(type: .normal, text: string + " pt", identifier: string)
  214. sizeItemArr.append(item)
  215. }
  216. fontSizeSelect.updateMenuItemsArr(sizeItemArr)
  217. }
  218. fontSizeSelect.delegate = self
  219. //Display Label
  220. displayLabel.stringValue = KMLocalizedString("Display Label")
  221. displayLabel.textColor = ComponentLibrary.shared.getComponentColorFromKey("colorText/2")
  222. displayLabel.font = ComponentLibrary.shared.getFontFromKey("mac/body-s-medium")
  223. displayLengthCheckBox.properties = ComponentCheckBoxProperty(size: .m, text: KMLocalizedString("Length"))
  224. displayLengthCheckBox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  225. displayArea_CheckBox.properties = ComponentCheckBoxProperty(size: .m, text: KMLocalizedString("Area"))
  226. displayArea_CheckBox.setTarget(self, action: #selector(checkBoxClicked(_:)))
  227. }
  228. func reloadData() {
  229. guard let pdfView = self.pdfView else {
  230. return
  231. }
  232. self.annotations.removeAll()
  233. let allAnnotations: [CPDFAnnotation] = pdfView.activeAnnotations as? [CPDFAnnotation] ?? []
  234. for annotation in allAnnotations {
  235. if annotation is CPDFLineAnnotation {
  236. if let _ = (annotation as! CPDFLineAnnotation).measureInfo {
  237. annotations.append((annotation as! CPDFLineAnnotation))
  238. }
  239. } else if annotation is CPDFPolylineAnnotation {
  240. annotations.append((annotation as! CPDFPolylineAnnotation))
  241. } else if annotation is CPDFPolygonAnnotation {
  242. annotations.append((annotation as! CPDFPolygonAnnotation))
  243. }
  244. }
  245. var firstAnnotation: CPDFAnnotation?
  246. if annotations.count > 0 {
  247. firstAnnotation = annotations.first
  248. }
  249. if viewManager?.subToolMode == .Measure {
  250. infoTopConst.constant = 56
  251. typeSegmented.isHidden = false
  252. } else {
  253. infoTopConst.constant = 0
  254. typeSegmented.isHidden = true
  255. }
  256. line_Property.active = false
  257. multiLine_Property.active = false
  258. polygon_Property.active = false
  259. rectangle_Property.active = false
  260. if annotationType == .measureLine {
  261. line_Property.active = true
  262. } else if annotationType == .measurePolyLine {
  263. multiLine_Property.active = true
  264. } else if annotationType == .measurePolyGon {
  265. polygon_Property.active = true
  266. } else if annotationType == .measureSquare {
  267. rectangle_Property.active = true
  268. }
  269. typeSegmented.reloadData()
  270. colorBGViewHeightConst.constant = 192
  271. fillColorGroup.isHidden = false
  272. fillColorImage.isHidden = false
  273. opacityViewTopConst.constant = 48
  274. displayBGView.isHidden = false
  275. arrowTypeView.isHidden = false
  276. if annotationType == .measureLine ||
  277. annotationType == .measurePolyLine {
  278. colorBGViewHeightConst.constant = 152
  279. fillColorGroup.isHidden = true
  280. fillColorImage.isHidden = true
  281. opacityViewTopConst.constant = 8
  282. displayBGView.isHidden = true
  283. if annotationType == .measurePolyLine {
  284. arrowTypeView.isHidden = true
  285. }
  286. } else if annotationType == .measurePolyGon ||
  287. annotationType == .measureSquare {
  288. arrowTypeView.isHidden = true
  289. }
  290. if true {
  291. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.measureFontColors
  292. if colors.count > 4 {
  293. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  294. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  295. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  296. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  297. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  298. fontColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  299. }
  300. }
  301. if true {
  302. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.measure_Border_Colors
  303. if colors.count > 4 {
  304. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  305. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  306. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  307. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  308. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  309. ColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  310. }
  311. }
  312. if true {
  313. let colors: [NSColor] = KMAnnotationPropertiesColorManager.manager.measure_Fill_Colors
  314. if colors.count > 4 {
  315. let colorAProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[0])
  316. let colorBProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[1])
  317. let colorCProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[2])
  318. let colorDProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: false, color: colors[3])
  319. let colorEProperty = ComponentCColorProperty(colorType: .color, state: .normal, isCustom: true, color: colors[4])
  320. fillColorGroup.setUpWithColorPropertys([colorAProperty, colorBProperty, colorCProperty, colorDProperty], customItemProperty: colorEProperty)
  321. }
  322. }
  323. //Color
  324. var fontColor: NSColor?
  325. var borderColor: NSColor?
  326. var fillColor: NSColor?
  327. var opacity: CGFloat = 0
  328. //Line
  329. var lineBorderStyle: CPDFBorderStyle?
  330. var lineBorderWidth: CGFloat?
  331. var dashPattern: CGFloat?
  332. var startLineStyle: CPDFLineStyle = .none
  333. var endLineStyle: CPDFLineStyle = .none
  334. //Font
  335. var fontNameString: String?
  336. var fontStyleString: String?
  337. var fontSize: CGFloat?
  338. //Display Label
  339. var displayLength: Bool = false
  340. var displayArea: Bool = false
  341. if annotations.count == 0 {
  342. //Color
  343. fontColor = CPDFMeasureDefaultInfo.default_measure_FontColor(annotationType: self.annotationType)
  344. borderColor = CPDFMeasureDefaultInfo.default_measure_BorderColor(annotationType: self.annotationType)
  345. fillColor = CPDFMeasureDefaultInfo.default_measure_FillColor(annotationType: self.annotationType)
  346. opacity = CPDFMeasureDefaultInfo.default_measure_Opacity(annotationType: self.annotationType)
  347. //Line
  348. lineBorderStyle = CPDFMeasureDefaultInfo.default_measure_LineStyle(annotationType: self.annotationType)
  349. lineBorderWidth = CPDFMeasureDefaultInfo.default_measure_LineWidth(annotationType: self.annotationType)
  350. var dash = 1.0
  351. for dashPattern in CPDFMeasureDefaultInfo.default_measure_DashPattern(annotationType: self.annotationType) {
  352. if let value = dashPattern as? CGFloat {
  353. dash = value
  354. break
  355. }
  356. }
  357. dashPattern = dash
  358. startLineStyle = CPDFMeasureDefaultInfo.default_measure_Arrow_StartLineStyle(annotationType: self.annotationType)
  359. endLineStyle = CPDFMeasureDefaultInfo.default_measure_Arrow_EndLineStyle(annotationType: self.annotationType)
  360. //Font
  361. fontNameString = CPDFMeasureDefaultInfo.default_measure_FontName(annotationType: self.annotationType)
  362. fontStyleString = CPDFMeasureDefaultInfo.default_measure_FontStyle(annotationType: self.annotationType)
  363. fontSize = CPDFMeasureDefaultInfo.default_measure_FontSize(annotationType: self.annotationType)
  364. displayLength = CPDFMeasureDefaultInfo.default_measure_DisplayLength(annotationType: self.annotationType)
  365. displayArea = CPDFMeasureDefaultInfo.default_measure_DisplayArea(annotationType: self.annotationType)
  366. } else if let annotation = firstAnnotation {
  367. if let lineAnnotation = annotation as? CPDFLineAnnotation, let _ = lineAnnotation.measureInfo {
  368. fontColor = lineAnnotation.fontColor
  369. borderColor = lineAnnotation.color
  370. opacity = lineAnnotation.opacity
  371. lineBorderStyle = lineAnnotation.borderStyle()
  372. lineBorderWidth = lineAnnotation.borderWidth
  373. var dash = 1.0
  374. for dashPattern in lineAnnotation.dashPattern() {
  375. if let value = dashPattern as? CGFloat {
  376. dash = value
  377. break
  378. }
  379. }
  380. dashPattern = dash
  381. startLineStyle = lineAnnotation.startLineStyle
  382. endLineStyle = lineAnnotation.endLineStyle
  383. fontNameString = lineAnnotation.cFont.familyName
  384. fontStyleString = lineAnnotation.cFont.styleName
  385. fontSize = lineAnnotation.fontSize
  386. } else if let polylineAnnot = annotation as? CPDFPolylineAnnotation {
  387. fontColor = polylineAnnot.fontColor
  388. borderColor = polylineAnnot.color
  389. opacity = polylineAnnot.opacity
  390. lineBorderStyle = polylineAnnot.borderStyle()
  391. lineBorderWidth = polylineAnnot.borderWidth
  392. var dash = 1.0
  393. for dashPattern in polylineAnnot.dashPattern() {
  394. if let value = dashPattern as? CGFloat {
  395. dash = value
  396. break
  397. }
  398. }
  399. dashPattern = dash
  400. fontNameString = polylineAnnot.cFont.familyName
  401. fontStyleString = polylineAnnot.cFont.styleName
  402. fontSize = polylineAnnot.fontSize
  403. } else if let polylineAnnot = annotation as? CPDFPolygonAnnotation {
  404. fontColor = polylineAnnot.fontColor
  405. borderColor = polylineAnnot.color
  406. fillColor = polylineAnnot.interiorColor
  407. opacity = polylineAnnot.opacity
  408. lineBorderStyle = polylineAnnot.borderStyle()
  409. lineBorderWidth = polylineAnnot.borderWidth
  410. var dash = 1.0
  411. for dashPattern in polylineAnnot.dashPattern() {
  412. if let value = dashPattern as? CGFloat {
  413. dash = value
  414. break
  415. }
  416. }
  417. dashPattern = dash
  418. fontNameString = polylineAnnot.cFont.familyName
  419. fontStyleString = polylineAnnot.cFont.styleName
  420. fontSize = polylineAnnot.fontSize
  421. if let me = polylineAnnot.measureInfo {
  422. if me.captionType.contains(.length) {
  423. displayLength = true
  424. }
  425. if me.captionType.contains(.area) {
  426. displayArea = true
  427. }
  428. }
  429. }
  430. }
  431. //Color
  432. fontColorGroup.currentColor = fontColor
  433. fontColorGroup.refreshUI()
  434. ColorGroup.currentColor = borderColor
  435. ColorGroup.refreshUI()
  436. fillColorGroup.currentColor = fillColor
  437. fillColorGroup.refreshUI()
  438. opacitySlider.properties.percent = opacity
  439. opacitySlider.reloadData()
  440. opacitySelect.properties.text = String(format: "%.0f%@", opacity*100, "%")
  441. opacitySelect.reloadData()
  442. //Line
  443. dashProperty.state = .normal
  444. solidProperty.state = .normal
  445. if lineBorderStyle == .dashed {
  446. dashProperty.state = .pressed
  447. } else if lineBorderStyle == .solid {
  448. solidProperty.state = .pressed
  449. }
  450. lineTypeSelector.reloadData()
  451. var percent: CGFloat = 0
  452. if let value = lineBorderWidth, value > 0 {
  453. percent = value/18
  454. lineWidthSlider.properties.percent = percent
  455. lineWidthSelect.properties.text = String(format: "%.0f%@", value, " pt")
  456. } else {
  457. lineWidthSlider.properties.percent = 0
  458. lineWidthSelect.properties.text = "0 pt"
  459. }
  460. lineWidthSlider.reloadData()
  461. lineWidthSelect.reloadData()
  462. linedashInfoView.isHidden = true
  463. arrowTypeTopConst.constant = 8
  464. lineBGViewHeightConst.constant = 112
  465. if arrowTypeView.isHidden {
  466. lineBGViewHeightConst.constant = 72
  467. }
  468. if lineBorderStyle == .dashed {
  469. linedashInfoView.isHidden = false
  470. arrowTypeTopConst.constant = 48
  471. lineBGViewHeightConst.constant = 152
  472. if arrowTypeView.isHidden {
  473. lineBGViewHeightConst.constant = 112
  474. }
  475. var dash = 1.0
  476. if let value = dashPattern {
  477. dash = value
  478. }
  479. let percent: CGFloat = CGFloat(dash)/18
  480. lineDashSlider.properties.percent = percent
  481. lineDashSlider.reloadData()
  482. lineDashSelect.properties.text = String(format: "%.0f%@", CGFloat(dash), " pt")
  483. lineDashSelect.reloadData()
  484. }
  485. arrow_leftTypeSelect.selectItemAtIndex(startLineStyle.rawValue)
  486. arrow_RightTypeSelect.selectItemAtIndex(endLineStyle.rawValue)
  487. //Font
  488. if let familyName = fontNameString, familyName != "-" {
  489. var styleNames = CPDFFont.fontNames(forFamilyName: familyName)
  490. var menuItemArr: [ComponentMenuitemProperty] = []
  491. if styleNames.count == 0 {
  492. styleNames.append("Regular")
  493. }
  494. for string in styleNames {
  495. let item = ComponentMenuitemProperty(type: .normal, text: string, identifier: string)
  496. menuItemArr.append(item)
  497. }
  498. fontStyleSelect.updateMenuItemsArr(menuItemArr)
  499. } else {
  500. fontStyleSelect.updateMenuItemsArr([])
  501. }
  502. if let value = fontNameString {
  503. fontNameSelect.properties.text = value
  504. } else {
  505. fontNameSelect.properties.text = "-"
  506. }
  507. fontNameSelect.reloadData()
  508. if let value = fontStyleString {
  509. fontStyleSelect.properties.text = value
  510. } else {
  511. fontStyleSelect.properties.text = "-"
  512. }
  513. fontStyleSelect.reloadData()
  514. if let value = fontSize {
  515. fontSizeSelect.properties.text = String(format: "%.0f", value)
  516. } else {
  517. fontSizeSelect.properties.text = "-"
  518. }
  519. fontSizeSelect.reloadData()
  520. displayLengthCheckBox.properties.checkboxType = displayLength ? .selected : .normal
  521. displayLengthCheckBox.reloadData()
  522. displayArea_CheckBox.properties.checkboxType = displayArea ? .selected : .normal
  523. displayArea_CheckBox.reloadData()
  524. }
  525. //MARK: - Action
  526. @objc func checkBoxClicked(_ sender: ComponentCheckBox) {
  527. if sender == displayLengthCheckBox || sender == displayArea_CheckBox {
  528. let isLength = displayLengthCheckBox.properties.checkboxType == .selected
  529. let isArea = displayArea_CheckBox.properties.checkboxType == .selected
  530. var captionType: CPDFCaptionType = CPDFCaptionType(rawValue: 0)
  531. if isLength && isArea {
  532. captionType = [.length, .area]
  533. } else if isLength {
  534. captionType = .length
  535. } else if isArea {
  536. captionType = .area
  537. }
  538. CPDFAnnotation.updateAnnotations(annotations, newcaptionType: captionType, withPDFView: pdfView)
  539. CPDFMeasureDefaultInfo.update_default_measure_CaptionType(annotationType, isLength, isArea)
  540. }
  541. }
  542. //MARK: - Mouse
  543. override func mouseDown(with event: NSEvent) {
  544. super.mouseDown(with: event)
  545. view.window?.makeFirstResponder(nil)
  546. }
  547. }
  548. //MARK: - ComponentCColorDelegate
  549. extension KMMeasureController: ComponentCColorDelegate {
  550. func componentCColorDidChooseColor(_ view: NSView, _ color: NSColor?) {
  551. if view == fontColorGroup {
  552. CPDFAnnotation.updateAnnotations(annotations, newFontColor: color, withPDFView: pdfView)
  553. CPDFMeasureDefaultInfo.update_default_measure_Color(annotationType: self.annotationType, color)
  554. } else if view == ColorGroup {
  555. CPDFAnnotation.updateAnnotations(annotations, newColor: color, withPDFView: pdfView)
  556. CPDFMeasureDefaultInfo.update_default_measure_BorderColor(annotationType: self.annotationType, color)
  557. } else if view == fillColorGroup {
  558. CPDFAnnotation.updateAnnotations(annotations, newInteriorColor: color, withPDFView: pdfView)
  559. CPDFMeasureDefaultInfo.update_default_measure_FillColor(annotationType: self.annotationType, color)
  560. }
  561. reloadData()
  562. }
  563. }
  564. //MARK: - ComponentSliderDelegate
  565. extension KMMeasureController: ComponentSliderDelegate {
  566. func componentSliderDidUpdate(_ view: ComponentSlider) {
  567. if view == opacitySlider {
  568. let value = view.properties.percent
  569. CPDFAnnotation.updateAnnotations(annotations, newInteriorOpacity: value, withPDFView: pdfView)
  570. CPDFMeasureDefaultInfo.update_default_measure_Opacity(annotationType: self.annotationType, value)
  571. } else if view == lineWidthSlider {
  572. let value = view.properties.percent * 18
  573. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  574. CPDFMeasureDefaultInfo.update_default_measure_LineWidth(annotationType: self.annotationType, value)
  575. } else if view == lineDashSlider {
  576. let value = view.properties.percent * 18
  577. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  578. CPDFMeasureDefaultInfo.update_default_measure_DashPattern(annotationType: self.annotationType, value)
  579. }
  580. reloadData()
  581. }
  582. }
  583. //MARK: - ComponentSelectDelegate
  584. extension KMMeasureController: ComponentSelectDelegate {
  585. func componentSelectTextDidEndEditing(_ view: ComponentSelect, removeUnit text: String?) {
  586. if let result = text {
  587. if view == opacitySelect {
  588. let value = max(0, min(1, result.stringToCGFloat()/100))
  589. CPDFAnnotation.updateAnnotations(annotations, newInteriorOpacity: value, withPDFView: pdfView)
  590. CPDFMeasureDefaultInfo.update_default_measure_Opacity(annotationType: self.annotationType, value)
  591. } else if view == lineWidthSelect {
  592. var value = result.stringToCGFloat()
  593. if value > 18 {
  594. value = 18
  595. } else if value < 1 {
  596. value = 1
  597. }
  598. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  599. CPDFMeasureDefaultInfo.update_default_measure_LineWidth(annotationType: self.annotationType, value)
  600. } else if view == lineDashSelect {
  601. var value = result.stringToCGFloat()
  602. if value > 18 {
  603. value = 18
  604. } else if value < 1 {
  605. value = 1
  606. }
  607. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  608. CPDFMeasureDefaultInfo.update_default_measure_DashPattern(annotationType: self.annotationType, value)
  609. } else if view == fontSizeSelect {
  610. let size = result.stringToCGFloat()
  611. CPDFAnnotation.updateAnnotations(annotations, newFontSize: size, withPDFView: pdfView)
  612. CPDFMeasureDefaultInfo.update_default_measure_FontSize(self.annotationType, size)
  613. }
  614. reloadData()
  615. }
  616. }
  617. func componentSelectDidSelect(view: ComponentSelect?, menuItemProperty: ComponentMenuitemProperty?) {
  618. if var result = menuItemProperty?.text {
  619. if let textUnit = view?.properties.textUnit {
  620. result = result.stringByDeleteCharString(textUnit)
  621. }
  622. if view == opacitySelect {
  623. let value = max(0, min(1, result.stringToCGFloat()/100))
  624. CPDFAnnotation.updateAnnotations(annotations, newInteriorOpacity: value, withPDFView: pdfView)
  625. CPDFMeasureDefaultInfo.update_default_measure_Opacity(annotationType: self.annotationType, value)
  626. } else if view == lineWidthSelect {
  627. var value = result.stringToCGFloat()
  628. if value > 18 {
  629. value = 18
  630. } else if value < 1 {
  631. value = 1
  632. }
  633. CPDFAnnotation.updateAnnotations(annotations, newLineWidth: value, withPDFView: pdfView)
  634. CPDFMeasureDefaultInfo.update_default_measure_LineWidth(annotationType: self.annotationType, value)
  635. } else if view == lineDashSelect {
  636. var value = result.stringToCGFloat()
  637. if value > 18 {
  638. value = 18
  639. } else if value < 1 {
  640. value = 1
  641. }
  642. CPDFAnnotation.updateAnnotations(annotations, newDashPattern: value, withPDFView: pdfView)
  643. CPDFMeasureDefaultInfo.update_default_measure_DashPattern(annotationType: self.annotationType, value)
  644. } else if view == arrow_leftTypeSelect {
  645. let idx = view?.indexOfSelect() ?? 0
  646. CPDFAnnotation.updateAnnotations(annotations, newStartLineStyle: CPDFLineStyle(rawValue: idx) ?? .none, withPDFView: pdfView)
  647. CPDFMeasureDefaultInfo.update_default_measure_StartLineStyle(self.annotationType, CPDFLineStyle(rawValue: idx) ?? .none)
  648. } else if view == arrow_RightTypeSelect {
  649. let idx = view?.indexOfSelect() ?? 0
  650. CPDFAnnotation.updateAnnotations(annotations, newEndLineStyle: CPDFLineStyle(rawValue: idx) ?? .none, withPDFView: pdfView)
  651. CPDFMeasureDefaultInfo.update_default_measure_EndLineStyle(self.annotationType, CPDFLineStyle(rawValue: idx) ?? .none)
  652. }else if view == fontNameSelect {
  653. var styleName = "Regular"
  654. let styleNames = CPDFFont.fontNames(forFamilyName: result)
  655. if let first = styleNames.first {
  656. styleName = first
  657. }
  658. CPDFAnnotation.updateAnnotations(annotations, newCFont: CPDFFont(familyName: result, fontStyle: styleName), withPDFView: pdfView)
  659. CPDFMeasureDefaultInfo.update_default_measure_Font(self.annotationType, CPDFFont(familyName: result, fontStyle: styleName))
  660. } else if view == fontStyleSelect {
  661. let familyName = fontNameSelect.properties.text ?? "Helvetica"
  662. CPDFAnnotation.updateAnnotations(annotations, newCFont: CPDFFont(familyName: familyName, fontStyle: result), withPDFView: pdfView)
  663. CPDFMeasureDefaultInfo.update_default_measure_Font(self.annotationType, CPDFFont(familyName: familyName, fontStyle: result))
  664. } else if view == fontSizeSelect {
  665. let size = result.stringToCGFloat()
  666. CPDFAnnotation.updateAnnotations(annotations, newFontSize: size, withPDFView: pdfView)
  667. CPDFMeasureDefaultInfo.update_default_measure_FontSize(self.annotationType, size)
  668. }
  669. reloadData()
  670. }
  671. }
  672. }
  673. //MARK: - ComponentCSelectorGroupDelegate
  674. extension KMMeasureController: ComponentCSelectorGroupDelegate {
  675. func componentCSelectorGroupDidChoose(_ view: ComponentCSelectorGroup, _ item: ComponentCSelectorItem) {
  676. if view == lineTypeSelector {
  677. if item.properties == solidProperty {
  678. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .solid, withPDFView: pdfView)
  679. CPDFMeasureDefaultInfo.update_default_measure_Style(annotationType: self.annotationType, .solid)
  680. } else if item.properties == dashProperty {
  681. CPDFAnnotation.updateAnnotations(annotations, newBorderStyle: .dashed, withPDFView: pdfView)
  682. CPDFMeasureDefaultInfo.update_default_measure_Style(annotationType: self.annotationType, .dashed)
  683. }
  684. }
  685. reloadData()
  686. }
  687. }
  688. //MARK: - ComponentSegmentedDelegate
  689. extension KMMeasureController: ComponentSegmentedDelegate {
  690. func componentSegmentedDidSelected(_ view: ComponentSegmented, item: ComponentSegmentedItem) {
  691. if item.properties == line_Property {
  692. annotationType = .measureLine
  693. } else if item.properties == multiLine_Property {
  694. annotationType = .measurePolyLine
  695. } else if item.properties == polygon_Property {
  696. annotationType = .measurePolyGon
  697. } else if item.properties == rectangle_Property {
  698. annotationType = .measureSquare
  699. }
  700. CPDFMeasureDefaultInfo.update_default_measureType(annotationType: annotationType)
  701. self.pdfView?.annotationType = annotationType
  702. reloadData()
  703. }
  704. }