KMMeasureController.swift 39 KB

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