KMRightSideViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // KMRightSideViewController.swift
  3. // PDF Master
  4. //
  5. // Created by Niehaoyu on 2022/10/26.
  6. //
  7. import Cocoa
  8. enum RightSubViewType : Int {
  9. case None
  10. case CipherTextType
  11. case EditPDFAddText
  12. case EditPDFAddImage
  13. case AnnotationProperts
  14. case Bates
  15. case Headerfooter
  16. case Background
  17. case Watermark
  18. }
  19. typealias KMRightSidePropertyDidChange = (_ model: AnyObject?) -> ()
  20. class KMRightSideViewController: NSViewController,CipherTextViewDelegate {
  21. var cipherTextView : CipherTextView!
  22. var eidtPDFTextProperty : KMEditPDFTextPropertyViewController!
  23. var eidtPDFImageProperty : KMEditImagePropertyViewController!
  24. var annotationProperties : KMAnnotationPropertiesViewController!
  25. var listView : CPDFListView!
  26. var _subViewType : RightSubViewType?
  27. @IBOutlet weak var contextBox: NSBox!
  28. var emptyVC : KMRightSideEmptyVC!
  29. var emptyVC_link: KMLinkAnnotationPropertyEmptyController?
  30. var _isHidden: Bool = false
  31. var propertyDidChange: KMRightSidePropertyDidChange!
  32. var model: AnyObject?
  33. weak var delegate: KMEditImagePropertyViewControllerDelegate?
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. // Do view setup here.
  37. // self.view.wantsLayer = true
  38. // self.view.layer?.backgroundColor = NSColor.red.cgColor
  39. emptyVC = KMRightSideEmptyVC.init(nibName: "KMRightSideEmptyVC", bundle: nil)
  40. contextBox.contentView = emptyVC.view
  41. self.emptyVC_link = KMLinkAnnotationPropertyEmptyController()
  42. }
  43. public func reloadDataWithPDFView(pdfView:CPDFListView,isShow:Bool) {
  44. if self.annotationProperties == nil {
  45. self.subViewType = .AnnotationProperts
  46. }
  47. if(!isShow) {
  48. self.isHidden = true
  49. return
  50. }
  51. self.annotationProperties.pdfView = pdfView
  52. var selectedAnnotation : CPDFAnnotation? = nil
  53. var activeAnnotations : [CPDFAnnotation] = []
  54. if pdfView.activeAnnotations != nil && pdfView.activeAnnotations.count > 0 {
  55. selectedAnnotation = pdfView.activeAnnotations.firstObject as? CPDFAnnotation
  56. for annotation in pdfView.activeAnnotations {
  57. activeAnnotations.append(annotation as! CPDFAnnotation)
  58. }
  59. }
  60. let annotationType = pdfView.annotationType
  61. if KMAnnotationPropertiesViewController.height(with: selectedAnnotation) > 0 {
  62. self.annotationProperties.annotations = activeAnnotations
  63. self.isHidden = !(activeAnnotations.count > 0)
  64. } else if self.listView.toolMode == .noteToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  65. self.isHidden = false
  66. if pdfView.activeAnnotation is CPDFLinkAnnotation {
  67. if activeAnnotations.count > 0 {
  68. self.annotationProperties.annotations = activeAnnotations
  69. } else {
  70. self.annotationProperties.annotations = []
  71. self.annotationProperties.annotationMode = annotationType
  72. }
  73. } else {
  74. self.annotationProperties.annotations = []
  75. self.annotationProperties.annotationMode = annotationType
  76. }
  77. } else if listView.toolMode == .formToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  78. self.isHidden = false
  79. self.annotationProperties.annotations = []
  80. self.annotationProperties.annotationMode = annotationType
  81. } else if listView.toolMode == .selfSignMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  82. self.isHidden = false
  83. self.annotationProperties.annotations = []
  84. self.annotationProperties.annotationMode = annotationType
  85. } else {
  86. self.isHidden = true
  87. }
  88. if (self.isHidden) {
  89. self.annotationProperties.isEmptyAnnotation = self.isHidden
  90. } else {
  91. var isShowEmpty = self.isHidden;
  92. for annotation in activeAnnotations {
  93. if (annotation is CPDFSignatureAnnotation) || (annotation is CPDFStampAnnotation) {
  94. if (annotation is CPDFStampAnnotation) {
  95. if (annotation is CSelfSignAnnotation) {
  96. break
  97. }
  98. }
  99. isShowEmpty = true;
  100. break
  101. }
  102. }
  103. if annotationType == .link && activeAnnotations.count == 0 {
  104. self.isHidden = true
  105. } else if (annotationType == .stamp || annotationType == .signSignature) && isShowEmpty {
  106. let continuousAddStamp = UserDefaults.standard.bool(forKey: "KMContinuousAdditionStamp")
  107. if continuousAddStamp {
  108. isShowEmpty = false
  109. self.annotationProperties.isContinuousAddStamp = true
  110. UserDefaults.standard.setValue(false, forKey: "KMContinuousAdditionStamp")
  111. UserDefaults.standard.synchronize()
  112. }
  113. } else {
  114. if isShowEmpty {
  115. } else {
  116. if _subViewType == RightSubViewType.AnnotationProperts {
  117. self.contextBox.contentView = self.annotationProperties.view
  118. }
  119. }
  120. }
  121. self.annotationProperties.isEmptyAnnotation = isShowEmpty
  122. }
  123. }
  124. //MARK: Setter && Get
  125. var subViewType : RightSubViewType? {
  126. set {
  127. _subViewType = newValue
  128. if _subViewType == RightSubViewType.CipherTextType {
  129. self.initCipherTextView()
  130. } else if _subViewType == RightSubViewType.EditPDFAddText{
  131. self.initEditPDFTextPropertyViewController()
  132. } else if _subViewType == RightSubViewType.EditPDFAddImage{
  133. self.initEditPDFImagePropertyViewController()
  134. } else if _subViewType == RightSubViewType.AnnotationProperts {
  135. self.initAnnotationProperts()
  136. } else if _subViewType == .Bates {
  137. self.initBatesViewController()
  138. } else if subViewType == .Headerfooter {
  139. self.initHeaderFooterViewController()
  140. } else if subViewType == .Background {
  141. self.initBackgroundViewController()
  142. } else if subViewType == .Watermark {
  143. self.initWatermarkViewController()
  144. }
  145. }
  146. get {
  147. return _subViewType
  148. }
  149. }
  150. var isHidden: Bool {
  151. get {
  152. return _isHidden
  153. }
  154. set {
  155. _isHidden = newValue
  156. if _isHidden {
  157. if (self.listView.annotationType == .link) {
  158. self.contextBox.contentView = self.emptyVC_link?.view
  159. } else {
  160. self.contextBox.contentView = emptyVC.view
  161. }
  162. }
  163. }
  164. }
  165. //MARK: InitSubViews
  166. func removeSubViews() {
  167. }
  168. private func initEditPDFTextPropertyViewController() {
  169. self.eidtPDFTextProperty = KMEditPDFTextPropertyViewController()
  170. self.eidtPDFTextProperty.listView = self.listView
  171. self.eidtPDFTextProperty.view.frame = self.view.bounds
  172. self.eidtPDFTextProperty.view.autoresizingMask = [.height]
  173. self.contextBox.contentView = self.eidtPDFTextProperty.view
  174. }
  175. private func initEditPDFImagePropertyViewController() {
  176. self.eidtPDFImageProperty = KMEditImagePropertyViewController()
  177. self.eidtPDFImageProperty.listView = self.listView
  178. self.eidtPDFImageProperty.delegate = self
  179. self.eidtPDFImageProperty.view.frame = self.view.bounds
  180. self.eidtPDFImageProperty.view.autoresizingMask = [.height]
  181. self.contextBox.contentView = self.eidtPDFImageProperty.view
  182. }
  183. private func initCipherTextView() {
  184. self.cipherTextView = CipherTextView.createFromNib()
  185. self.cipherTextView.frame = self.view.bounds
  186. self.cipherTextView.autoresizingMask = [.height]
  187. self.cipherTextView.setUp()
  188. self.contextBox.contentView = self.cipherTextView
  189. }
  190. private func initAnnotationProperts() {
  191. self.annotationProperties = KMAnnotationPropertiesViewController()
  192. self.annotationProperties.view.frame = self.view.bounds
  193. self.annotationProperties.pdfView = self.listView
  194. self.annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  195. self.contextBox.contentView = self.annotationProperties.view
  196. if self.listView.toolMode == .noteToolMode || self.listView.toolMode == .formToolMode
  197. || self.listView.toolMode == .selfSignMode {
  198. self.reloadDataWithPDFView(pdfView: self.listView, isShow: true)
  199. }
  200. }
  201. var curcontroller: NSViewController?
  202. private func initBatesViewController() {
  203. let controller = KMBatesPropertyHomeController()
  204. controller.pageCount = Int(self.listView.document.pageCount)
  205. controller.view.frame = self.view.bounds
  206. controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  207. self.contextBox.contentView = controller.view
  208. curcontroller = controller
  209. controller.modelDidChange = { [weak self] model in
  210. guard let callback = self?.propertyDidChange else {
  211. return
  212. }
  213. self!.model = model
  214. callback(model)
  215. }
  216. }
  217. private func initHeaderFooterViewController() {
  218. let controller = KMHeaderFooterPropertyMainController()
  219. controller.pageCount = Int(self.listView.document.pageCount)
  220. controller.view.frame = self.view.bounds
  221. controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  222. self.contextBox.contentView = controller.view
  223. curcontroller = controller
  224. controller.modelDidChange = { [weak self] model in
  225. guard let callback = self?.propertyDidChange else {
  226. return
  227. }
  228. self!.model = model
  229. callback(model)
  230. }
  231. }
  232. private func initBackgroundViewController() {
  233. // let controller = KMBackgroundPropertyHomeController()
  234. // controller.preView = self.listView
  235. // controller.view.frame = self.view.bounds
  236. // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  237. // self.contextBox.contentView = controller.view
  238. //
  239. // controller.modelDidChange = {
  240. // [weak self] (model: KMBackgroundModel?) -> () in
  241. // guard let callback = self?.propertyDidChange else {
  242. // return
  243. // }
  244. //
  245. // self!.model = model
  246. // callback(model)
  247. // }
  248. }
  249. private func initWatermarkViewController() {
  250. // let controller = KMWatermarkPropertyHomeController()
  251. // controller.preView = self.listView
  252. // controller.view.frame = self.view.bounds
  253. // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  254. // self.contextBox.contentView = controller.view
  255. //
  256. // controller.modelDidChange = {
  257. // [weak self] (model: KMWatermarkModel?) -> () in
  258. // guard let callback = self?.propertyDidChange else {
  259. // return
  260. // }
  261. //
  262. // self!.model = model
  263. // callback(model)
  264. // }
  265. }
  266. //MARK: CipherTextViewDelegate
  267. func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) {
  268. if buttonIndex == 0 {
  269. }
  270. }
  271. }
  272. extension KMRightSideViewController: KMEditImagePropertyViewControllerDelegate {
  273. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  274. self.delegate?.editImagePropertyViewControllerDidChanged(controller: controller, type: type)
  275. }
  276. }