KMMeasureController.swift 39 KB

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