123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- //
- // KMHeaderFooterController.swift
- // PDF Reader Pro
- //
- // Created by Niehaoyu on 2024/11/8.
- //
- import Cocoa
- import KMComponentLibrary
- @objc protocol KMHeaderFooterControllerDelegate: AnyObject {
-
- @objc optional func kmHeaderFooterControllerDidUpdateMode(_ view: KMHeaderFooterController)
-
- //数据发生变化时处理
- @objc optional func kmHeaderFooterControllerDidModelDataUpdated(_ view: KMHeaderFooterController)
- }
- class KMHeaderFooterController: NSViewController {
-
- @IBOutlet var contendView: NSView!
-
- @IBOutlet var preInfoView: NSView!
- @IBOutlet var propertyInfoView: NSView!
-
- var documentPreview: KMEditDocumentController = KMEditDocumentController.init()
- var propertyController: KMHeaderPropertyController = KMHeaderPropertyController.init()
- var templatesController: KMHFTemplateController = KMHFTemplateController()
-
- var saveTemplateMessage: ComponentMessage = ComponentMessage()
-
- var totalPDFCount: Int = 1
- var pdfDocument: CPDFDocument?
- var headerFooterModel: KMHeaderFooterObject? {
- get {
- if editSubType == .template {
- return templatesController.selectedModel
- } else if editSubType == .add ||
- editSubType == .edit {
- return propertyController.headerFooterModel
- }
- return nil
- }
- }
-
- weak open var delegate: KMHeaderFooterControllerDelegate?
-
- 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() {
- documentPreview.pdfDocument = self.pdfDocument
- documentPreview.reloadData()
-
- propertyController.totalPDFCount = totalPDFCount
-
- if editSubType == .template {
- templatesController.reloadData()
-
- } else if editSubType == .add {
- propertyController.editSubType = editSubType
- propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter
- propertyController.reloadData()
-
- } else if editSubType == .edit {
- propertyController.reloadData()
- }
-
- updatePDFDocumentHeaderFooter()
- }
-
- func updatePDFDocumentHeaderFooter() {
- if let data = headerFooterModel {
- let cPDFHeaderFooter = documentPreview.pdfDocument?.headerFooter()
- cPDFHeaderFooter?.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
-
- cPDFHeaderFooter?.margin = NSEdgeInsetsMake(max(CGFloat(data.topMargin)-size.height, 0), CGFloat(data.leftMargin), max(CGFloat(data.bottomMargin)-size.height, 0), CGFloat(data.rightMargin))
-
- let strings = KMHeaderFooterObject.parseModel(model: data, pageCount: totalPDFCount)
- var count: Int = 0
- let color = data.color
-
- for text in strings {
- cPDFHeaderFooter?.setText(text, at: UInt(count))
- cPDFHeaderFooter?.setTextColor(color, at: UInt(count))
- cPDFHeaderFooter?.setFontSize(fontSize, at: UInt(count))
- cPDFHeaderFooter?.setFontName(fontName, at: UInt(count))
- count += 1
- }
-
- cPDFHeaderFooter?.update()
-
- documentPreview.refreshUI()
- } else {
- pdfDocument?.headerFooter().clear()
- documentPreview.refreshUI()
- }
- }
- }
- //MARK: - KMHeaderPropertyControllerDelegate
- extension KMHeaderFooterController: KMHeaderPropertyControllerDelegate {
- func headerFooterPropertyControllerDidUpdate(_ controller: KMHeaderPropertyController) {
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
- }
-
- func headerFooterPropertyControllerDidChangetoTemplate(_ controller: KMHeaderPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
-
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
- }
-
- func headerFooterPropertyControllerSaveTemplateSuccess(_ controller: KMHeaderPropertyController, _ data: KMHeaderFooterObject) {
- editSubType = .template
- resetUI()
- delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
-
- templatesController.selectedModel = data
- templatesController.reloadData()
-
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
- }
-
- func headerFooterPropertyControllerCancelTemplateEdit(_ controller: KMHeaderPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
-
- templatesController.selectedModel = controller.headerFooterModel
- templatesController.reloadData()
-
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
-
- }
-
- func headerFooterPropertyControllerFinishTemplateEdit(_ controller: KMHeaderPropertyController) {
- editSubType = .template
- resetUI()
- delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
-
- templatesController.selectedModel = controller.headerFooterModel
- templatesController.reloadData()
-
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
- }
- }
- //MARK: - KMHFTemplateControllerDelegate
- extension KMHeaderFooterController: KMHFTemplateControllerDelegate {
- func templateControllerDidSelectedChanged(_ controller: KMHFTemplateController) {
-
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
- }
-
- func templateControllerDidAddData(_ controller: KMHFTemplateController) {
- editSubType = .add
- resetUI()
- delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
-
- propertyController.clearData()
- propertyController.headerFooterModel = KMHeaderFooterManager.defaultManager.defaultHeaderFooter
- propertyController.reloadData()
-
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
-
- }
-
- func templateControllerDidEditData(_ controller: KMHFTemplateController, _ data: KMHeaderFooterObject) {
- editSubType = .edit
- resetUI()
- delegate?.kmHeaderFooterControllerDidUpdateMode?(self)
-
- propertyController.originalDataDict = KMHeaderFooterManager.defaultManager.parseModel(model: data)
- propertyController.headerFooterModel = data
- propertyController.reloadData()
-
- updatePDFDocumentHeaderFooter()
-
- delegate?.kmHeaderFooterControllerDidModelDataUpdated?(self)
- }
-
- }
|