KMRightSideViewController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. _ = KMEditPDFTextManager.manager.updateTextFontNames(listView: self.listView)
  170. self.eidtPDFTextProperty = KMEditPDFTextPropertyViewController()
  171. self.eidtPDFTextProperty.listView = self.listView
  172. self.eidtPDFTextProperty.view.frame = self.view.bounds
  173. self.eidtPDFTextProperty.view.autoresizingMask = [.height]
  174. self.contextBox.contentView = self.eidtPDFTextProperty.view
  175. }
  176. private func initEditPDFImagePropertyViewController() {
  177. self.eidtPDFImageProperty = KMEditImagePropertyViewController()
  178. self.eidtPDFImageProperty.listView = self.listView
  179. self.eidtPDFImageProperty.delegate = self
  180. self.eidtPDFImageProperty.view.frame = self.view.bounds
  181. self.eidtPDFImageProperty.view.autoresizingMask = [.height]
  182. self.contextBox.contentView = self.eidtPDFImageProperty.view
  183. }
  184. private func initCipherTextView() {
  185. self.cipherTextView = CipherTextView.createFromNib()
  186. self.cipherTextView.frame = self.view.bounds
  187. self.cipherTextView.autoresizingMask = [.height]
  188. self.cipherTextView.setUp()
  189. self.contextBox.contentView = self.cipherTextView
  190. }
  191. private func initAnnotationProperts() {
  192. self.annotationProperties = KMAnnotationPropertiesViewController()
  193. self.annotationProperties.view.frame = self.view.bounds
  194. self.annotationProperties.pdfView = self.listView
  195. self.annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  196. self.contextBox.contentView = self.annotationProperties.view
  197. if self.listView.toolMode == .noteToolMode || self.listView.toolMode == .formToolMode
  198. || self.listView.toolMode == .selfSignMode {
  199. self.reloadDataWithPDFView(pdfView: self.listView, isShow: true)
  200. }
  201. }
  202. var curcontroller: NSViewController?
  203. private func initBatesViewController() {
  204. let controller = KMBatesPropertyHomeController()
  205. controller.pageCount = Int(self.listView.document.pageCount)
  206. controller.view.frame = self.view.bounds
  207. controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  208. self.contextBox.contentView = controller.view
  209. curcontroller = controller
  210. controller.modelDidChange = { [weak self] model in
  211. guard let callback = self?.propertyDidChange else {
  212. return
  213. }
  214. self!.model = model
  215. callback(model)
  216. }
  217. }
  218. private func initHeaderFooterViewController() {
  219. let controller = KMHeaderFooterPropertyMainController()
  220. controller.pageCount = Int(self.listView.document.pageCount)
  221. controller.view.frame = self.view.bounds
  222. controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  223. self.contextBox.contentView = controller.view
  224. curcontroller = controller
  225. controller.modelDidChange = { [weak self] model in
  226. guard let callback = self?.propertyDidChange else {
  227. return
  228. }
  229. self!.model = model
  230. callback(model)
  231. }
  232. }
  233. private func initBackgroundViewController() {
  234. // let controller = KMBackgroundPropertyHomeController()
  235. // controller.preView = self.listView
  236. // controller.view.frame = self.view.bounds
  237. // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  238. // self.contextBox.contentView = controller.view
  239. //
  240. // controller.modelDidChange = {
  241. // [weak self] (model: KMBackgroundModel?) -> () in
  242. // guard let callback = self?.propertyDidChange else {
  243. // return
  244. // }
  245. //
  246. // self!.model = model
  247. // callback(model)
  248. // }
  249. }
  250. private func initWatermarkViewController() {
  251. // let controller = KMWatermarkPropertyHomeController()
  252. // controller.preView = self.listView
  253. // controller.view.frame = self.view.bounds
  254. // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  255. // self.contextBox.contentView = controller.view
  256. //
  257. // controller.modelDidChange = {
  258. // [weak self] (model: KMWatermarkModel?) -> () in
  259. // guard let callback = self?.propertyDidChange else {
  260. // return
  261. // }
  262. //
  263. // self!.model = model
  264. // callback(model)
  265. // }
  266. }
  267. //MARK: CipherTextViewDelegate
  268. func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) {
  269. if buttonIndex == 0 {
  270. }
  271. }
  272. }
  273. extension KMRightSideViewController: KMEditImagePropertyViewControllerDelegate {
  274. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  275. self.delegate?.editImagePropertyViewControllerDidChanged(controller: controller, type: type)
  276. }
  277. }