KMRightSideViewController.swift 12 KB

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