KMRightSideViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. case ViewSettings
  19. }
  20. typealias KMRightSidePropertyDidChange = (_ model: AnyObject?) -> ()
  21. class KMRightSideViewController: NSViewController,CipherTextViewDelegate {
  22. var cipherTextView : CipherTextView!
  23. var eidtPDFTextProperty : KMEditPDFTextPropertyViewController!
  24. var eidtPDFImageProperty : KMEditImagePropertyViewController!
  25. var annotationProperties : KMAnnotationPropertiesViewController!
  26. var listView : CPDFListView!
  27. var _subViewType : RightSubViewType?
  28. @IBOutlet weak var contextBox: NSBox!
  29. var emptyVC : KMRightSideEmptyVC!
  30. var emptyVC_link: KMLinkAnnotationPropertyEmptyController?
  31. var _isHidden: Bool = false
  32. var propertyDidChange: KMRightSidePropertyDidChange!
  33. var model: AnyObject?
  34. weak var delegate: KMEditImagePropertyViewControllerDelegate?
  35. weak var mainController: KMMainViewController?
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. // Do view setup here.
  39. // self.view.wantsLayer = true
  40. // self.view.layer?.backgroundColor = NSColor.red.cgColor
  41. emptyVC = KMRightSideEmptyVC.init(nibName: "KMRightSideEmptyVC", bundle: nil)
  42. contextBox.contentView = emptyVC.view
  43. self.emptyVC_link = KMLinkAnnotationPropertyEmptyController()
  44. }
  45. public func reloadDataWithPDFView(pdfView:CPDFListView,isShow:Bool) {
  46. if self.annotationProperties == nil {
  47. self.subViewType = .AnnotationProperts
  48. }
  49. if(!isShow) {
  50. self.isHidden = true
  51. return
  52. }
  53. self.annotationProperties.pdfView = pdfView
  54. var selectedAnnotation : CPDFAnnotation? = nil
  55. var activeAnnotations : [CPDFAnnotation] = []
  56. if pdfView.activeAnnotations != nil && pdfView.activeAnnotations.count > 0 {
  57. selectedAnnotation = pdfView.activeAnnotations.firstObject as? CPDFAnnotation
  58. for annotation in pdfView.activeAnnotations {
  59. activeAnnotations.append(annotation as! CPDFAnnotation)
  60. }
  61. }
  62. let annotationType = pdfView.annotationType
  63. if KMAnnotationPropertiesViewController.height(with: selectedAnnotation) > 0 {
  64. self.annotationProperties.annotations = activeAnnotations
  65. self.isHidden = !(activeAnnotations.count > 0)
  66. } else if self.listView.toolMode == .noteToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  67. self.isHidden = false
  68. if pdfView.activeAnnotation is CPDFLinkAnnotation {
  69. if activeAnnotations.count > 0 {
  70. self.annotationProperties.annotations = activeAnnotations
  71. } else {
  72. self.annotationProperties.annotations = []
  73. self.annotationProperties.annotationMode = annotationType
  74. }
  75. } else {
  76. self.annotationProperties.annotations = []
  77. self.annotationProperties.annotationMode = annotationType
  78. }
  79. } else if listView.toolMode == .formToolMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  80. self.isHidden = false
  81. self.annotationProperties.annotations = []
  82. self.annotationProperties.annotationMode = annotationType
  83. } else if listView.toolMode == .selfSignMode && KMAnnotationPropertiesViewController.height(withAnnotationMode: pdfView.annotationType) > 0 {
  84. self.isHidden = false
  85. self.annotationProperties.annotations = []
  86. self.annotationProperties.annotationMode = annotationType
  87. } else {
  88. self.isHidden = true
  89. }
  90. if (self.isHidden) {
  91. self.annotationProperties.isEmptyAnnotation = self.isHidden
  92. } else {
  93. var isShowEmpty = self.isHidden;
  94. for annotation in activeAnnotations {
  95. if (annotation is CPDFSignatureAnnotation) || (annotation is CPDFStampAnnotation) {
  96. if (annotation is CPDFStampAnnotation) {
  97. if (annotation is CSelfSignAnnotation) {
  98. break
  99. }
  100. }
  101. isShowEmpty = true;
  102. break
  103. }
  104. }
  105. if annotationType == .link && activeAnnotations.count == 0 {
  106. self.isHidden = true
  107. } else if (annotationType == .stamp || annotationType == .signSignature) && isShowEmpty {
  108. let continuousAddStamp = UserDefaults.standard.bool(forKey: "KMContinuousAdditionStamp")
  109. if continuousAddStamp {
  110. isShowEmpty = false
  111. self.annotationProperties.isContinuousAddStamp = true
  112. UserDefaults.standard.setValue(false, forKey: "KMContinuousAdditionStamp")
  113. UserDefaults.standard.synchronize()
  114. }
  115. } else {
  116. if isShowEmpty {
  117. } else {
  118. if _subViewType == RightSubViewType.AnnotationProperts {
  119. self.contextBox.contentView = self.annotationProperties.view
  120. }
  121. }
  122. }
  123. self.annotationProperties.isEmptyAnnotation = isShowEmpty
  124. }
  125. }
  126. //MARK: Setter && Get
  127. var subViewType : RightSubViewType? {
  128. set {
  129. _subViewType = newValue
  130. if _subViewType == RightSubViewType.CipherTextType {
  131. self.initCipherTextView()
  132. } else if _subViewType == RightSubViewType.EditPDFAddText{
  133. self.initEditPDFTextPropertyViewController()
  134. } else if _subViewType == RightSubViewType.EditPDFAddImage{
  135. self.initEditPDFImagePropertyViewController()
  136. } else if _subViewType == RightSubViewType.AnnotationProperts {
  137. self.initAnnotationProperts()
  138. } else if _subViewType == .Bates {
  139. self.initBatesViewController()
  140. } else if subViewType == .Headerfooter {
  141. self.initHeaderFooterViewController()
  142. } else if subViewType == .Background {
  143. self.initBackgroundViewController()
  144. } else if subViewType == .Watermark {
  145. self.initWatermarkViewController()
  146. } else if subViewType == .ViewSettings {
  147. self.initViewSettingsViewController()
  148. }
  149. }
  150. get {
  151. return _subViewType
  152. }
  153. }
  154. var isHidden: Bool {
  155. get {
  156. return _isHidden
  157. }
  158. set {
  159. _isHidden = newValue
  160. if _isHidden {
  161. if (self.listView.annotationType == .link) {
  162. self.contextBox.contentView = self.emptyVC_link?.view
  163. } else {
  164. self.contextBox.contentView = emptyVC.view
  165. }
  166. }
  167. }
  168. }
  169. //MARK: InitSubViews
  170. func removeSubViews() {
  171. }
  172. private func initEditPDFTextPropertyViewController() {
  173. _ = KMEditPDFTextManager.manager.updateTextFontNames(listView: self.listView)
  174. self.eidtPDFTextProperty = KMEditPDFTextPropertyViewController()
  175. self.eidtPDFTextProperty.listView = self.listView
  176. self.eidtPDFTextProperty.view.frame = self.view.bounds
  177. self.eidtPDFTextProperty.view.autoresizingMask = [.height]
  178. self.contextBox.contentView = self.eidtPDFTextProperty.view
  179. }
  180. private func initEditPDFImagePropertyViewController() {
  181. self.eidtPDFImageProperty = KMEditImagePropertyViewController()
  182. self.eidtPDFImageProperty.listView = self.listView
  183. self.eidtPDFImageProperty.delegate = self
  184. self.eidtPDFImageProperty.view.frame = self.view.bounds
  185. self.eidtPDFImageProperty.view.autoresizingMask = [.height]
  186. self.contextBox.contentView = self.eidtPDFImageProperty.view
  187. }
  188. private func initCipherTextView() {
  189. self.cipherTextView = CipherTextView.createFromNib()
  190. self.cipherTextView.frame = self.view.bounds
  191. self.cipherTextView.autoresizingMask = [.height]
  192. self.cipherTextView.setUp()
  193. self.contextBox.contentView = self.cipherTextView
  194. }
  195. private func initAnnotationProperts() {
  196. self.annotationProperties = KMAnnotationPropertiesViewController()
  197. self.annotationProperties.view.frame = self.view.bounds
  198. self.annotationProperties.pdfView = self.listView
  199. self.annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  200. self.contextBox.contentView = self.annotationProperties.view
  201. if self.listView.toolMode == .noteToolMode || self.listView.toolMode == .formToolMode
  202. || self.listView.toolMode == .selfSignMode {
  203. self.reloadDataWithPDFView(pdfView: self.listView, isShow: true)
  204. }
  205. }
  206. var curcontroller: NSViewController?
  207. private func initBatesViewController() {
  208. // let controller = KMBatesPropertyHomeController()
  209. // controller.pageCount = Int(self.listView.document.pageCount)
  210. // controller.view.frame = self.view.bounds
  211. // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  212. // self.contextBox.contentView = controller.view
  213. // curcontroller = controller
  214. //
  215. // controller.modelDidChange = { [weak self] model in
  216. // guard let callback = self?.propertyDidChange else {
  217. // return
  218. // }
  219. //
  220. // self!.model = model
  221. // callback(model)
  222. // }
  223. let Controller: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  224. Controller.onlyManagerTemplate = true
  225. Controller.isBatchOperation = false
  226. Controller.pdfView = self.listView
  227. Controller.isBatchOperation = false
  228. Controller.isBates = true
  229. // self.titleLabel.stringValue = KMLocalizedString("Manage Templates", nil)
  230. self.contextBox.contentView? = Controller.view
  231. curcontroller = Controller
  232. Controller.view.mas_makeConstraints { make in
  233. make?.edges.equalTo()(self.view)
  234. }
  235. }
  236. private func initHeaderFooterViewController() {
  237. // let controller = KMHeaderFooterPropertyMainController()
  238. // controller.pageCount = Int(self.listView.document.pageCount)
  239. // controller.view.frame = self.view.bounds
  240. // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  241. // self.contextBox.contentView = controller.view
  242. // curcontroller = controller
  243. //
  244. // controller.modelDidChange = { [weak self] model in
  245. // guard let callback = self?.propertyDidChange else {
  246. // return
  247. // }
  248. //
  249. // self!.model = model
  250. // callback(model)
  251. // }
  252. let headerFooterViewController: KMBatchOperateAddHeaderFooterViewController = KMBatchOperateAddHeaderFooterViewController(files: [])
  253. headerFooterViewController.onlyManagerTemplate = true
  254. headerFooterViewController.isBatchOperation = false
  255. headerFooterViewController.pdfView = self.listView
  256. // self.titleLabel.stringValue = KMLocalizedString("Manage Templates", nil)
  257. self.contextBox.contentView? = headerFooterViewController.view
  258. curcontroller = headerFooterViewController
  259. headerFooterViewController.view.mas_makeConstraints { make in
  260. make?.edges.equalTo()(self.view)
  261. }
  262. }
  263. private func initBackgroundViewController() {
  264. // let controller = KMBackgroundPropertyHomeController()
  265. // controller.preView = self.listView
  266. // controller.view.frame = self.view.bounds
  267. // controller.view.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  268. // self.contextBox.contentView = controller.view
  269. //
  270. // controller.modelDidChange = {
  271. // [weak self] (model: KMBackgroundModel?) -> () in
  272. // guard let callback = self?.propertyDidChange else {
  273. // return
  274. // }
  275. //
  276. // self!.model = model
  277. // callback(model)
  278. // }
  279. let Controller: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  280. Controller.onlyManagerTemplate = true
  281. Controller.isBatchOperation = false
  282. Controller.pdfView = self.listView
  283. Controller.isBackground = true
  284. self.contextBox.contentView? = Controller.view
  285. // self.titleLabel.stringValue = KMLocalizedString("Manage Templates", nil)
  286. curcontroller = Controller
  287. Controller.view.mas_makeConstraints { make in
  288. // make?.left.equalTo()(self.view)
  289. // make?.right.equalTo()(self.view)
  290. // make?.bottom.equalTo()(self.view)
  291. // make?.top.equalTo()(self.view)?.offset()(40)
  292. make?.edges.equalTo()(self.view)
  293. }
  294. }
  295. private func initWatermarkViewController() {
  296. let addWatermarkViewController: KMBatchOperateAddWatermarkViewController = KMBatchOperateAddWatermarkViewController(files: [])
  297. addWatermarkViewController.onlyManagerTemplate = true
  298. addWatermarkViewController.isBatchOperation = false
  299. addWatermarkViewController.pdfView = self.listView
  300. self.contextBox.contentView? = addWatermarkViewController.view
  301. // self.titleLabel.stringValue = KMLocalizedString("Manage Templates", nil)
  302. curcontroller = addWatermarkViewController
  303. addWatermarkViewController.view.mas_makeConstraints { make in
  304. // make?.left.equalTo()(self.view)
  305. // make?.right.equalTo()(self.view)
  306. // make?.bottom.equalTo()(self.view)
  307. // make?.top.equalTo()(self.view)?.offset()(40)
  308. make?.edges.equalTo()(self.view)
  309. }
  310. }
  311. private func initViewSettingsViewController() {
  312. annotationProperties = KMAnnotationPropertiesViewController()
  313. annotationProperties.view.frame = self.view.bounds
  314. annotationProperties.view.autoresizingMask = [.height,.maxXMargin]
  315. annotationProperties.mainController = mainController
  316. annotationProperties.pdfView = listView
  317. annotationProperties.openPropertiesType = .pageDisplay
  318. annotationProperties.pageDisplayReaderMode = { [weak self] isReaderMode in
  319. guard let self = self else { return }
  320. self.mainController!.readMode(self)
  321. }
  322. annotationProperties.isEmptyAnnotation = false
  323. contextBox.contentView = self.annotationProperties.view
  324. }
  325. //MARK: CipherTextViewDelegate
  326. func cipherTextViewButtonClicked(textView: CipherTextView, buttonIndex: Int) {
  327. if buttonIndex == 0 {
  328. }
  329. }
  330. }
  331. extension KMRightSideViewController: KMEditImagePropertyViewControllerDelegate {
  332. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType) {
  333. self.delegate?.editImagePropertyViewControllerDidChanged(controller: controller, type: type)
  334. }
  335. }