|
@@ -11,7 +11,7 @@ typealias KMAnnotationSelfSignViewControllerCallBack = (CAnnotationType) -> Void
|
|
|
|
|
|
@objcMembers class KMAnnotationSelfSignViewController: NSViewController {
|
|
|
|
|
|
- var _annotations: [KMSelfSignAnnotation]?
|
|
|
+ var _annotations: [CSelfSignAnnotation]?
|
|
|
var selfSignType: CAnnotationType = .signText
|
|
|
var pdfview: CPDFListView?
|
|
|
var callBack: KMAnnotationSelfSignViewControllerCallBack?
|
|
@@ -136,89 +136,14 @@ typealias KMAnnotationSelfSignViewControllerCallBack = (CAnnotationType) -> Void
|
|
|
|
|
|
// MARK: Set & Get
|
|
|
|
|
|
- var annotations: [KMSelfSignAnnotation] {
|
|
|
- set {
|
|
|
- _annotations = newValue
|
|
|
-
|
|
|
- annotation = newValue.first
|
|
|
- }
|
|
|
+ var annotations: [CSelfSignAnnotation] {
|
|
|
get {
|
|
|
- return _annotations!
|
|
|
+ return annotationModel?.annotations as! [CSelfSignAnnotation]
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// MARK: Private Method
|
|
|
|
|
|
- func drawImage(with type: CAnnotationType) -> NSImage? {
|
|
|
- let imageSize = NSSize(width: 50, height: 50)
|
|
|
- guard imageSize.width != 0 && imageSize.height != 0 else {
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- let image = NSImage(size: imageSize)
|
|
|
-
|
|
|
- let bounds = NSRect(x: 0, y: 0, width: 50, height: 50)
|
|
|
- image.lockFocus()
|
|
|
- NSGraphicsContext.current?.imageInterpolation = .high
|
|
|
- guard let context = NSGraphicsContext.current?.cgContext else {
|
|
|
- image.unlockFocus()
|
|
|
- return nil
|
|
|
- }
|
|
|
-
|
|
|
- var paintColor: NSColor
|
|
|
- var lineWidth: CGFloat
|
|
|
- if let annotation = annotation {
|
|
|
- paintColor = annotation.lineColor
|
|
|
- lineWidth = annotation.lineAnnotationWidth
|
|
|
- } else {
|
|
|
- paintColor = KMSelfSignAnnotation.fetchStoredColor(type)
|
|
|
- lineWidth = KMSelfSignAnnotation.fetchStoredLineWidth(type)
|
|
|
- }
|
|
|
-
|
|
|
- if type == .signTure {
|
|
|
- context.move(to: CGPoint(x: bounds.minX + lineWidth/2, y: bounds.midY + lineWidth/2))
|
|
|
- context.addLine(to: CGPoint(x: bounds.minX + bounds.width/3.0, y: bounds.minY + lineWidth/2))
|
|
|
- context.addLine(to: CGPoint(x: bounds.maxX - lineWidth/2, y: bounds.maxY - lineWidth/2))
|
|
|
- context.setLineWidth(lineWidth)
|
|
|
- context.setStrokeColor(paintColor.cgColor)
|
|
|
- context.setFillColor(paintColor.cgColor)
|
|
|
- context.strokePath()
|
|
|
- } else if type == .signFalse {
|
|
|
- context.move(to: CGPoint(x: bounds.minX + lineWidth/2, y: bounds.minY + lineWidth/2))
|
|
|
- context.addLine(to: CGPoint(x: bounds.maxX - lineWidth/2, y: bounds.maxY - lineWidth/2))
|
|
|
- context.setLineWidth(lineWidth)
|
|
|
- context.setStrokeColor(paintColor.cgColor)
|
|
|
- context.setFillColor(paintColor.cgColor)
|
|
|
- context.strokePath()
|
|
|
- context.move(to: CGPoint(x: bounds.minX + lineWidth/2, y: bounds.maxY - lineWidth/2))
|
|
|
- context.addLine(to: CGPoint(x: bounds.maxX - lineWidth/2, y: bounds.minY + lineWidth/2))
|
|
|
- context.setStrokeColor(paintColor.cgColor)
|
|
|
- context.setFillColor(paintColor.cgColor)
|
|
|
- context.strokePath()
|
|
|
- } else if type == .signLine {
|
|
|
- let path = NSBezierPath(roundedRect: NSRect(x: bounds.minX + 1, y: bounds.midY - lineWidth/2, width: bounds.width - 2, height: lineWidth), xRadius: lineWidth/2.0, yRadius: lineWidth/2.0)
|
|
|
- paintColor.set()
|
|
|
- path.fill()
|
|
|
- } else if type == .signCircle {
|
|
|
- let r = min(bounds.width, bounds.height)/3.0
|
|
|
- let path = NSBezierPath(roundedRect: bounds.insetBy(dx: lineWidth/2, dy: lineWidth/2), xRadius: r, yRadius: r)
|
|
|
-
|
|
|
- path.lineWidth = lineWidth
|
|
|
- paintColor.set()
|
|
|
- path.stroke()
|
|
|
- } else if type == .signDot {
|
|
|
- context.setStrokeColor(NSColor.black.cgColor)
|
|
|
- context.setLineWidth(1.0)
|
|
|
- context.addArc(center: CGPoint(x: bounds.midX, y: bounds.midY), radius: min(bounds.width/2.0 - 1, bounds.height/2.0 - 1), startAngle: 0, endAngle: CGFloat.pi * 2, clockwise: true)
|
|
|
-
|
|
|
- context.setStrokeColor(paintColor.cgColor)
|
|
|
- context.setFillColor(paintColor.cgColor)
|
|
|
- context.fillPath()
|
|
|
- }
|
|
|
- image.unlockFocus()
|
|
|
- return image
|
|
|
- }
|
|
|
-
|
|
|
func typeSelected(_ type: CAnnotationType) {
|
|
|
let buttons: [NSButton] = [falseTypeButton, trueTypeButton, circletypeButton, lineTypeButton, dotTypeButton]
|
|
|
|
|
@@ -252,6 +177,16 @@ typealias KMAnnotationSelfSignViewControllerCallBack = (CAnnotationType) -> Void
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private func updateAnnotation() {
|
|
|
+ if annotationModel?.annotation != nil {
|
|
|
+ for tAnnotation in annotations {
|
|
|
+ tAnnotation.updateAppearanceStream()
|
|
|
+ pdfView?.setNeedsDisplayAnnotationViewFor(tAnnotation.page)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ imageView.image = annotationModel?.annotationImage
|
|
|
+ }
|
|
|
+
|
|
|
// MARK: Outlet Methods
|
|
|
|
|
|
@IBAction func opacityComboboxAction(_ sender: NSComboBox) {
|
|
@@ -269,7 +204,6 @@ typealias KMAnnotationSelfSignViewControllerCallBack = (CAnnotationType) -> Void
|
|
|
var opacity: CGFloat = 0.0
|
|
|
|
|
|
originalColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
|
|
|
-
|
|
|
switch sender.stringValue {
|
|
|
case "25%":
|
|
|
opacity = 0.25
|
|
@@ -282,97 +216,62 @@ typealias KMAnnotationSelfSignViewControllerCallBack = (CAnnotationType) -> Void
|
|
|
default:
|
|
|
break
|
|
|
}
|
|
|
-
|
|
|
- opacitySlider.doubleValue = Double(opacity)
|
|
|
- opacityComboBox.stringValue = "\(Int(opacity * 100))%"
|
|
|
-
|
|
|
- let newColor = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
|
|
|
-
|
|
|
- if let annotation = annotation {
|
|
|
- for tAnnotation in annotations {
|
|
|
- tAnnotation.lineColor = newColor
|
|
|
- }
|
|
|
- pdfview?.needsDisplay = true
|
|
|
- } else {
|
|
|
- KMSelfSignAnnotation.changeStoredColor(selfSignType, color: newColor)
|
|
|
- }
|
|
|
-
|
|
|
- colorPickView.color = newColor
|
|
|
- imageView.image = drawImage(with: selfSignType)
|
|
|
+ annotationModel?.setOpacity(opacity)
|
|
|
+ opacitySlider.floatValue = Float(opacity)
|
|
|
+ opacitySlider.toolTip = "\(Int(opacity * 100))%"
|
|
|
+ updateAnnotation()
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@IBAction func colorPickViewAction(_ sender: Any) {
|
|
|
- if let annotation = annotation {
|
|
|
- for tAnnotation in annotations {
|
|
|
- tAnnotation.lineColor = colorPickView.color!
|
|
|
+ var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
|
|
|
+ if let textColor = colorPickView.color {
|
|
|
+ textColor.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
|
|
|
+ if fabs(opacity - 0) < 0.001 {
|
|
|
+ if annotationModel?.annotationType == .signTure || annotationModel?.annotationType == .signFalse || annotationModel?.annotationType == .signDot || annotationModel?.annotationType == .signLine || annotationModel?.annotationType == .signCircle {
|
|
|
+ annotationModel?.setOpacity(0)
|
|
|
+ annotationModel?.setColor(.black)
|
|
|
+ }
|
|
|
+
|
|
|
+ } else {
|
|
|
+ if annotationModel?.annotationType == .signTure || annotationModel?.annotationType == .signFalse || annotationModel?.annotationType == .signDot || annotationModel?.annotationType == .signLine || annotationModel?.annotationType == .signCircle {
|
|
|
+ annotationModel?.setOpacity(opacity)
|
|
|
+ annotationModel?.setColor(textColor)
|
|
|
+ }
|
|
|
}
|
|
|
- pdfview?.needsDisplay = true
|
|
|
- } else {
|
|
|
- KMSelfSignAnnotation.changeStoredColor(selfSignType, color: colorPickView.color!)
|
|
|
+ opacitySlider.floatValue = Float(opacity)
|
|
|
}
|
|
|
- imageView.image = drawImage(with: selfSignType)
|
|
|
+ updateAnnotation()
|
|
|
}
|
|
|
|
|
|
@IBAction func opacitySliderAction(_ sender: Any) {
|
|
|
- var originalColor: NSColor?
|
|
|
-
|
|
|
- if let annotation = annotation {
|
|
|
- originalColor = annotation.lineColor
|
|
|
- } else {
|
|
|
- originalColor = KMSelfSignAnnotation.fetchStoredColor(selfSignType)
|
|
|
- }
|
|
|
-
|
|
|
- var red: CGFloat = 0.0, green: CGFloat = 0.0, blue: CGFloat = 0.0, opacity: CGFloat = 0.0
|
|
|
- originalColor!.usingColorSpaceName(.calibratedRGB)?.getRed(&red, green: &green, blue: &blue, alpha: &opacity)
|
|
|
-
|
|
|
- opacity = CGFloat(opacitySlider.floatValue)
|
|
|
- opacityComboBox.stringValue = String(format: "%d%%", Int(opacity * 100))
|
|
|
-
|
|
|
- let newColor = NSColor(calibratedRed: red, green: green, blue: blue, alpha: opacity)
|
|
|
-
|
|
|
- if let annotation = annotation {
|
|
|
- for tAnnotation in annotations {
|
|
|
- tAnnotation.lineColor = newColor
|
|
|
- }
|
|
|
- pdfview?.needsDisplay = true
|
|
|
- } else {
|
|
|
- KMSelfSignAnnotation.changeStoredColor(selfSignType, color: newColor)
|
|
|
+ var opcity = CGFloat(opacitySlider.floatValue)
|
|
|
+ if opcity == 0 {
|
|
|
+ opcity = 0.0001
|
|
|
}
|
|
|
-
|
|
|
- imageView.image = drawImage(with: selfSignType)
|
|
|
- colorPickView.color = newColor
|
|
|
+ annotationModel?.setOpacity(opcity)
|
|
|
+ opacityComboBox.stringValue = "\(Int(opcity * 100))%"
|
|
|
+ updateAnnotation()
|
|
|
}
|
|
|
|
|
|
- @IBAction func lineWidthSliderAction(_ sender: NSSlider) {
|
|
|
- if let annotation = annotation {
|
|
|
- for tAnnotation in annotations {
|
|
|
- tAnnotation.lineAnnotationWidth = CGFloat(sender.floatValue)
|
|
|
- }
|
|
|
- } else {
|
|
|
- KMSelfSignAnnotation.changeStoredLineWidth(selfSignType, lineWidth: CGFloat(sender.floatValue))
|
|
|
- }
|
|
|
+ @IBAction func lineWidthSliderAction(_ sender: NSSlider) {
|
|
|
+ annotationModel?.setLineWidth(CGFloat(lineWidthSlider.floatValue))
|
|
|
+ lineWidthComboBox.stringValue = String(format: "%0.1f pt", (annotationModel?.lineWidth())!)
|
|
|
|
|
|
- lineWidthComboBox.stringValue = String(format: "%.1f pt", sender.floatValue)
|
|
|
- imageView.image = drawImage(with: selfSignType)
|
|
|
+ updateAnnotation()
|
|
|
}
|
|
|
|
|
|
@IBAction func lineWidthComboboxAction(_ sender: NSComboBox) {
|
|
|
- var lineWidth = sender.floatValue
|
|
|
+ var lineWidth = lineWidthComboBox.floatValue
|
|
|
if lineWidth > 18 {
|
|
|
lineWidth = 18
|
|
|
+ } else if lineWidth < 0.5 {
|
|
|
+ lineWidth = 0.5
|
|
|
}
|
|
|
+ annotationModel?.setLineWidth(CGFloat(lineWidth))
|
|
|
+ lineWidthSlider.floatValue = Float((annotationModel?.lineWidth())!)
|
|
|
|
|
|
- if let annotation = annotation {
|
|
|
- for tAnnotation in annotations {
|
|
|
- tAnnotation.lineAnnotationWidth = CGFloat(lineWidth)
|
|
|
- }
|
|
|
- } else {
|
|
|
- KMSelfSignAnnotation.changeStoredLineWidth(selfSignType, lineWidth: CGFloat(lineWidth))
|
|
|
- }
|
|
|
-
|
|
|
- lineWidthSlider.floatValue = lineWidth
|
|
|
- sender.stringValue = String(format: "%0.1f pt", lineWidth)
|
|
|
- imageView.image = drawImage(with: selfSignType)
|
|
|
+ updateAnnotation()
|
|
|
}
|
|
|
|
|
|
@IBAction func buttonClicked_TypeAwitch(_ sender: NSButton) {
|
|
@@ -402,7 +301,7 @@ typealias KMAnnotationSelfSignViewControllerCallBack = (CAnnotationType) -> Void
|
|
|
|
|
|
pdfview?.needsDisplay = true
|
|
|
selfSignType = annotation!.annotationType
|
|
|
- imageView.image = drawImage(with: selfSignType)
|
|
|
+ imageView.image = annotationModel?.annotationImage
|
|
|
typeSelected(selfSignType)
|
|
|
|
|
|
if let callBack = callBack {
|