123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245 |
- //
- // KMBackgroundController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/7.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMBackgroundControllerDelegate: AnyObject {
-
- @objc optional func kmBackgroundControllerDidUpdateMode(_ view: KMBackgroundController)
-
- //数据发生变化时处理
- @objc optional func kmBackgroundControllerDidWatermarkUpdated(_ view: KMBackgroundController)
- }
- class KMBackgroundController: NSViewController {
-
- @IBOutlet var contendView: NSView!
-
- @IBOutlet var preInfoView: NSView!
- @IBOutlet var propertyInfoView: NSView!
-
- var documentPreview: KMEditDocumentController = KMEditDocumentController.init()
- var propertyController: KMBackgroundPropertyController = KMBackgroundPropertyController.init()
- var templatesController: KMBGTemplateController = KMBGTemplateController()
-
- var saveTemplateMessage: ComponentMessage = ComponentMessage()
-
- var pdfDocument: CPDFDocument?
-
- var backgroundModel: KMBackgroundModel?
-
- var editSubType: KMPDFEditSubModeType = .none
-
- weak open var delegate: KMBackgroundControllerDelegate?
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do view setup here.
-
- documentPreview.view.frame = preInfoView.bounds
- documentPreview.view.autoresizingMask = [.width, .height]
- preInfoView.addSubview(documentPreview.view)
-
- propertyController.view.frame = propertyInfoView.bounds
- propertyController.view.autoresizingMask = [.width, .height]
- propertyController.delegate = self
- propertyController.view.isHidden = true
- propertyInfoView.addSubview(propertyController.view)
-
- templatesController.view.frame = propertyInfoView.bounds
- templatesController.view.autoresizingMask = [.width, .height]
- templatesController.delegate = self
- templatesController.view.isHidden = true
- propertyInfoView.addSubview(templatesController.view)
-
- setupProperty()
- }
-
- func setupProperty() {
- saveTemplateMessage.properties = ComponentMessageProperty(messageType: .success, title: KMLocalizedString("Saved Template"))
-
- }
-
- func resetUI() {
- propertyController.view.isHidden = true
- templatesController.view.isHidden = true
-
- if editSubType == .template {
- templatesController.view.isHidden = false
-
- } else if editSubType == .add {
- propertyController.view.isHidden = false
- propertyController.editSubType = editSubType
-
- } else if editSubType == .edit {
- propertyController.view.isHidden = false
- propertyController.editSubType = editSubType
-
- }
- }
-
- func reloadData() {
- if editSubType == .template {
- templatesController.reloadData()
-
- backgroundModel = templatesController.selectedBackground
- } else if editSubType == .add {
- propertyController.editSubType = editSubType
- propertyController.backgroundData = KMBackgroundManager.defaultManager.defaultModel
- propertyController.reloadData()
-
- backgroundModel = propertyController.backgroundData
- } else if editSubType == .edit {
- propertyController.editSubType = editSubType
- propertyController.reloadData()
-
- backgroundModel = propertyController.backgroundData
- }
- documentPreview.pdfDocument = pdfDocument
- documentPreview.reloadData()
-
- updatePDFDocumentBackground()
- delegate?.kmBackgroundControllerDidWatermarkUpdated?(self)
- }
-
- func updatePDFDocumentBackground() {
- if let data = backgroundModel {
- let cPDFBackground = documentPreview.pdfDocument?.background()
- cPDFBackground?.type = data.type
- if cPDFBackground?.type == .color {
- cPDFBackground?.color = data.color
- } else if cPDFBackground?.type == .image {
- if data.imagePath != nil {
- cPDFBackground?.setImage(data.image())
- } else {
- cPDFBackground?.setImage(nil)
- }
- }
- cPDFBackground?.scale = data.scale
- cPDFBackground?.rotation = data.rotation
- cPDFBackground?.opacity = data.opacity
- cPDFBackground?.verticalAlignment = UInt(data.verticalMode)
- cPDFBackground?.horizontalAlignment = UInt(data.horizontalMode)
- cPDFBackground?.xOffset = data.horizontalSpace
- cPDFBackground?.yOffset = data.verticalSpace
- cPDFBackground?.isAllowsView = true
-
- cPDFBackground?.pageString = "0"
- cPDFBackground?.update()
-
- self.documentPreview.refreshUI()
-
- } else {
- pdfDocument?.background().clear()
- documentPreview.refreshUI()
- }
- }
-
- override func mouseUp(with event: NSEvent) {
- super.mouseUp(with: event)
-
-
- }
-
- }
- //MARK: - KMBackgroundPropertyControllerDelegate
- extension KMBackgroundController: KMBackgroundPropertyControllerDelegate {
- func backgroundPropertyControllerDidUpdate(_ controller: KMBackgroundPropertyController) {
- backgroundModel = controller.backgroundData
-
- updatePDFDocumentBackground()
-
- delegate?.kmBackgroundControllerDidWatermarkUpdated?(self)
- }
-
- func backgroundPropertyControllerDidChangetoTemplate(_ controller: KMBackgroundPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmBackgroundControllerDidUpdateMode?(self)
- backgroundModel = templatesController.selectedBackground
- reloadData()
- }
-
- func backgroundPropertyControllerSaveTemplateSuccess(_ controller: KMBackgroundPropertyController, _ data: KMBackgroundModel) {
- saveTemplateMessage.frame = CGRectMake((CGRectGetWidth(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewWidth)/2,
- CGRectGetHeight(self.view.frame) - saveTemplateMessage.properties.propertyInfo.viewHeight - 8,
- saveTemplateMessage.properties.propertyInfo.viewWidth,
- saveTemplateMessage.properties.propertyInfo.viewHeight)
- saveTemplateMessage.reloadData()
- saveTemplateMessage.show(inView: self.view, autoHideSeconde: 2)
-
- editSubType = .template
- resetUI()
- delegate?.kmBackgroundControllerDidUpdateMode?(self)
-
- templatesController.selectedBackground = data
- reloadData()
- }
-
- func backgroundPropertyControllerCancelTemplateEdit(_ controller: KMBackgroundPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmBackgroundControllerDidUpdateMode?(self)
-
- templatesController.selectedBackground = controller.backgroundData
-
- reloadData()
-
- }
-
- func backgroundPropertyControllerFinishTemplateEdit(_ controller: KMBackgroundPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmBackgroundControllerDidUpdateMode?(self)
-
- templatesController.selectedBackground = controller.backgroundData
-
- reloadData()
- }
- }
- //MARK: - KMBGTemplateControllerDelegate
- extension KMBackgroundController: KMBGTemplateControllerDelegate {
- func templateControllerDidSelectedChanged(_ controller: KMBGTemplateController) {
- backgroundModel = controller.selectedBackground
-
- updatePDFDocumentBackground()
-
- delegate?.kmBackgroundControllerDidWatermarkUpdated?(self)
- }
-
- func templateControllerDidAddData(_ controller: KMBGTemplateController) {
- editSubType = .add
- resetUI()
- delegate?.kmBackgroundControllerDidUpdateMode?(self)
-
- propertyController.backgroundData = KMBackgroundManager.defaultManager.defaultModel
-
- backgroundModel = propertyController.backgroundData
-
- reloadData()
-
- documentPreview.layoutDocumentView()
- }
-
- func templateControllerDidEditData(_ controller: KMBGTemplateController, _ data: KMBackgroundModel) {
- editSubType = .edit
- resetUI()
- delegate?.kmBackgroundControllerDidUpdateMode?(self)
-
- propertyController.originalDataDict = KMBackgroundManager.defaultManager.parseModelToDict(model: data)
- propertyController.backgroundData = data
- propertyController.reloadData()
-
- backgroundModel = propertyController.backgroundData
-
- documentPreview.layoutDocumentView()
- }
- }
|