123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- //
- // KMBatesController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/8.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMBatesControllerDelegate: AnyObject {
-
- @objc optional func kmBatesControllerDidUpdateMode(_ view: KMBatesController)
-
- //数据发生变化时处理
- @objc optional func kmBatesControllerDidModelDataUpdated(_ view: KMBatesController)
- }
- class KMBatesController: NSViewController {
-
- @IBOutlet var contendView: NSView!
-
- @IBOutlet var preInfoView: NSView!
- @IBOutlet var propertyInfoView: NSView!
-
- var documentPreview: KMEditDocumentController = KMEditDocumentController.init()
- var propertyController: KMBatesPropertyController = KMBatesPropertyController.init()
- var templatesController: KMBatesTemplateController = KMBatesTemplateController()
-
- var saveTemplateMessage: ComponentMessage = ComponentMessage()
-
- weak open var delegate: KMBatesControllerDelegate?
-
- var totalPDFCount: Int = 1
- var pdfDocument: CPDFDocument?
-
- var batesModel: KMBatesModel? {
- get {
- if editSubType == .template {
- return templatesController.selectedDataModel
- } else if editSubType == .add ||
- editSubType == .edit {
- return propertyController.batesModel
- }
- return nil
- }
- }
-
- var editSubType: KMPDFEditSubModeType = .none
-
- 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() {
- propertyController.totalPDFCount = totalPDFCount
-
- documentPreview.pdfDocument = pdfDocument
- documentPreview.reloadData()
-
- if editSubType == .template {
- templatesController.reloadData()
- } else if editSubType == .add {
- propertyController.editSubType = editSubType
- propertyController.reloadData()
- } else if editSubType == .edit {
- propertyController.editSubType = editSubType
- propertyController.reloadData()
- }
- }
-
- func updatePDFDocumentBates() {
- if let data = batesModel {
- let cPDFBates = documentPreview.pdfDocument?.bates()
- cPDFBates?.pageString = "0"
-
- let fontSize = data.fontsize
- let fontName: String = data.fontName
-
- let font = NSFont.boldSystemFont(ofSize:fontSize)
- let style = NSMutableParagraphStyle()
- style.alignment = .center
- style.lineBreakMode = .byCharWrapping
- let size: NSSize = "text".boundingRect(with: NSSize(width: 1000, height: 1000), options: NSString.DrawingOptions(rawValue: 3), attributes: [NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style]).size
- cPDFBates?.margin = NSEdgeInsetsMake(max(CGFloat(data.topMargin)-size.height, 0), CGFloat(data.leftMargin), max(CGFloat(data.bottomMargin)-size.height, 0), CGFloat(data.rightMargin))
-
- let strings = KMBatesManager.parseModel(model: data, UInt(totalPDFCount))
- var count: Int = 0
- let color = data.color
-
- for text in strings {
- cPDFBates?.setText(text, at: UInt(count))
- cPDFBates?.setTextColor(color, at: UInt(count))
- cPDFBates?.setFontSize(fontSize, at: UInt(count))
- cPDFBates?.setFontName(fontName, at: UInt(count))
- count += 1
- }
- cPDFBates?.update()
-
- self.documentPreview.refreshUI()
- } else {
- pdfDocument?.bates().clear()
- documentPreview.refreshUI()
- }
-
- }
-
- }
- //MARK: - KMHeaderPropertyControllerDelegate
- extension KMBatesController: KMBatesPropertyControllerDelegate {
- func batesPropertyControllerDidUpdate(_ controller: KMBatesPropertyController) {
- updatePDFDocumentBates()
-
- }
-
- func batesPropertyControllerDidChangetoTemplate(_ controller: KMBatesPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmBatesControllerDidUpdateMode?(self)
-
- templatesController.selectedDataModel = nil
- templatesController.reloadData()
-
- updatePDFDocumentBates()
-
- delegate?.kmBatesControllerDidModelDataUpdated?(self)
- }
-
- func batesPropertyControllerSaveTemplateSuccess(_ controller: KMBatesPropertyController, _ data: KMBatesModel) {
- 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?.kmBatesControllerDidUpdateMode?(self)
-
- templatesController.selectedDataModel = data
- templatesController.reloadData()
-
- KMBatesManager.defaultManager.defaultModel = KMBatesModel()
-
- delegate?.kmBatesControllerDidModelDataUpdated?(self)
- }
-
- func batesPropertyControllerCancelTemplateEdit(_ controller: KMBatesPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmBatesControllerDidUpdateMode?(self)
-
- templatesController.selectedDataModel = controller.batesModel
- templatesController.reloadData()
-
- delegate?.kmBatesControllerDidModelDataUpdated?(self)
- }
-
- func batesPropertyControllerFinishTemplateEdit(_ controller: KMBatesPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmBatesControllerDidUpdateMode?(self)
-
- templatesController.selectedDataModel = controller.batesModel
- templatesController.reloadData()
-
- delegate?.kmBatesControllerDidModelDataUpdated?(self)
- }
- }
- //MARK: - KMHFTemplateControllerDelegate
- extension KMBatesController: KMBatesTemplateControllerDelegate {
- func templateControllerDidSelectedChanged(_ controller: KMBatesTemplateController) {
- updatePDFDocumentBates()
-
- delegate?.kmBatesControllerDidModelDataUpdated?(self)
- }
-
- func templateControllerDidAddData(_ controller: KMBatesTemplateController) {
- editSubType = .add
- resetUI()
- delegate?.kmBatesControllerDidUpdateMode?(self)
-
- propertyController.clearData()
- propertyController.batesModel = KMBatesManager.defaultManager.defaultModel
- propertyController.reloadData()
-
- delegate?.kmBatesControllerDidModelDataUpdated?(self)
-
- templatesController.selectedDataModel = nil
- templatesController.reloadData()
-
- }
-
- func templateControllerDidEditData(_ controller: KMBatesTemplateController, _ data: KMBatesModel) {
- editSubType = .edit
- resetUI()
- delegate?.kmBatesControllerDidUpdateMode?(self)
-
- if data.bottomLeftString.count > 0 {
- propertyController.positionSelRow = 0
- propertyController.positionSelColumn = 0
- } else if data.bottomCenterString.count > 0 {
- propertyController.positionSelRow = 0
- propertyController.positionSelColumn = 1
- } else if data.bottomRightString.count > 0 {
- propertyController.positionSelRow = 0
- propertyController.positionSelColumn = 2
- } else if data.topLeftString.count > 0 {
- propertyController.positionSelRow = 1
- propertyController.positionSelColumn = 0
- } else if data.topCenterString.count > 0 {
- propertyController.positionSelRow = 1
- propertyController.positionSelColumn = 1
- } else if data.topRightString.count > 0 {
- propertyController.positionSelRow = 1
- propertyController.positionSelColumn = 2
- }
-
- propertyController.batesModel = data
- propertyController.originalDataDict = KMBatesManager.defaultManager.parseModel(model: data)
- propertyController.reloadData()
-
- delegate?.kmBatesControllerDidModelDataUpdated?(self)
-
- templatesController.selectedDataModel = nil
- templatesController.reloadData()
-
- }
-
- }
|