123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- //
- // KMHeaderFooterPropertyInfoController.swift
- // PDF Master
- //
- // Created by tangchao on 2022/12/27.
- //
- import Cocoa
- class KMHeaderFooterPropertyInfoController: NSViewController {
-
- @IBOutlet weak var fontBox: NSBox!
- var fontView = KMHeaderFooterFontInfoView()
- @IBOutlet weak var marginBox: NSBox!
- var marginView = KMHeaderFooterMarginInfoView()
- @IBOutlet weak var dateBox: NSBox!
- var dateView = KMHeaderFooterDateInfoView()
- @IBOutlet weak var pageBox: NSBox!
- var pageView = KMHeaderFooterPageInfoView()
- @IBOutlet weak var contentBox: NSBox!
- var contentView = KMHeaderFooterContentInfoView()
- @IBOutlet weak var pageRangeBox: NSBox!
-
- var pagePageView = KMConvertPageRangeSettingItemView.createFromNib()
-
- weak var delegate: KMWatermarkAdjectivePropertyDelegate!
-
- init() {
- super.init(nibName: "KMHeaderFooterPropertyInfoController", bundle: nil)
- }
-
- required init?(coder: NSCoder) {
- super.init(nibName: "KMHeaderFooterPropertyInfoController", bundle: nil)
- }
-
- private var myModel: KMHeaderFooterModel!
- var model: KMHeaderFooterModel {
- get {
- return myModel
- }
- set {
- myModel = newValue
-
- self.fontView.model = newValue
- self.marginView.model = newValue
- self.dateView.model = newValue
-
- self.pageView.leftComboBox.removeAllItems()
- self.pageView.leftComboBox.addItems(withObjectValues: KMWatermarkAdjectiveTools.getPageFormats())
- if (newValue.pageFormatString.isEmpty) {
- self.pageView.leftComboBox.stringValue = KMWatermarkAdjectiveTools.getPageFormats().first!
- } else {
- self.pageView.leftComboBox.stringValue = newValue.pageFormatString
- }
- self.pageView.rightComboBox.removeAllItems()
- for i in 0 ..< newValue.pageCount {
- self.pageView.rightComboBox.addItem(withObjectValue: i+1)
- }
-
- if (newValue.startString.isEmpty) {
- self.pageView.rightComboBox.stringValue = "1"
- } else {
- self.pageView.rightComboBox.stringValue = newValue.startString
- }
-
- self.contentView.model = newValue
- }
- }
-
- override func viewWillAppear() {
- super.viewWillAppear()
-
- /// 滚动到顶部
- DispatchQueue.main.async {
- let contentView = self.fontBox.enclosingScrollView!.contentView;
- contentView.scroll(to: NSMakePoint(0, NSHeight(self.fontBox.enclosingScrollView!.documentView!.frame)-NSHeight(contentView.frame)))
- }
- }
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- let fontView = self.fontView
- fontView.frame = self.fontBox.contentView!.bounds
- fontView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.fontBox.contentView?.addSubview(fontView)
- fontView.itemClick = { [weak self] (index: Int, value: Any) in
- if (index == 1) { /// 字体名称
- switch self!.model.textFont {
- case .font(name: _, size: let size):
- self!.model.textFont = .font(name: value as! String, size: size)
- default:
- break
- }
-
- if (self!.delegate != nil) {
- self!.delegate.propertyInfoDidChange(model: self!.model)
- }
- } else if (index == 2) { /// 字体大小
- switch self!.model.textFont {
- case .font(name: let name, size: _):
- self!.model.textFont = .font(name: name, size: CGFloat(value as! Int))
- default:
- break
- }
-
- if (self!.delegate != nil) {
- self!.delegate.propertyInfoDidChange(model: self!.model)
- }
- } else if (index == 3) { /// 字体颜色
- let panel = NSColorPanel.shared
- panel.setTarget(self!)
- panel.setAction(#selector(self!.colorPanelAction))
- panel.orderFront(nil)
- }
- }
-
- let marginView = self.marginView
- marginView.frame = self.marginBox.contentView!.bounds
- marginView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.marginBox.contentView?.addSubview(marginView)
- marginView.itemClick = { [weak self] (index: Int, value: Any) in
- if (index == 1) { /// top
- self!.model.topMargin = value as! Double
- } else if (index == 2) { /// bottom
- self!.model.bottomMargin = value as! Double
- } else if (index == 3) { /// left
- self!.model.leftMargin = value as! Double
- } else if (index == 4) { /// right
- self!.model.rightMargin = value as! Double
- }
-
- if (self!.delegate != nil) {
- self!.delegate.propertyInfoDidChange(model: self!.model)
- }
- }
-
- let dateView = self.dateView
- dateView.frame = self.dateBox.contentView!.bounds
- dateView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.dateBox.contentView?.addSubview(dateView)
- dateView.itemClick = { [weak self] (index: Int, value: Any) in
-
- self!.model.dateFormatString = value as! String
- if (self!.delegate != nil) {
- self!.delegate.propertyInfoDidChange(model: self!.model)
- }
- }
-
- let pageView = self.pageView
- pageView.frame = self.pageBox.contentView!.bounds
- pageView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.pageBox.contentView?.addSubview(pageView)
- pageView.leftLabel.stringValue = NSLocalizedString("Page:", comment: "")
- pageView.itemClick = { [weak self] (index: Int, value: Any) in
- if (index == 1) { /// 页面
- self!.model.pageFormatString = KMWatermarkAdjectiveTools.getPageFormats()[value as! Int]
- } else if (index == 2) { /// 起始页
- self!.model.startString = "\(value as! Int+1)"
- }
- if (self!.delegate != nil) {
- self!.delegate.propertyInfoDidChange(model: self!.model)
- }
- }
-
- let contentView = self.contentView
- contentView.frame = self.contentBox.contentView!.bounds
- contentView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.contentBox.contentView?.addSubview(contentView)
- contentView.addAction = { [weak self]
- (index: Int, sender: NSButton) in
- let menu = NSMenu()
- let item = menu.addItem(withTitle: NSLocalizedString("Insert Page Number", comment: ""), action: #selector(self!.insertPageAction), keyEquivalent: "")
- item.target = self
- let item2 = menu.addItem(withTitle: NSLocalizedString("Insert Date", comment: ""), action: #selector(self!.insertDateAction), keyEquivalent: "")
- item2.target = self
- menu.popUp(positioning: nil, at: NSPoint(x: sender.bounds.midX, y: sender.bounds.maxY), in: sender)
- }
- contentView.itemClick = { [weak self] (index: Int, value: Any) in
- self!.updateModelForContentInfo(index: index, value: value as! String)
- }
-
- let pageRangeView = self.pagePageView
- pageRangeView?.frame = self.pageRangeBox.contentView!.bounds
- pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.pageRangeBox.contentView?.addSubview(pageRangeView!)
- pageRangeView!.pageSizeComboBox.removeAllItems()
- pageRangeView!.pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")])
- pageRangeView!.pageSizeComboBox.selectItem(at: 0)
-
- pageRangeView?.isTextDidEndCallback = true
-
- pageRangeView?.itemClick = { [unowned self] index, _ in
- self.model.pageRangeType = index
-
- if (self.delegate != nil) {
- self.delegate.propertyInfoDidChange(model: self.model)
- }
- }
-
- pageRangeView?.textDidEndEditingCallback = { [weak self] (textString: String) in
- let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: self!.model.pageCount)
- if (array.count <= 0) {
- let alert = NSAlert()
- alert.alertStyle = .warning
- alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
- alert.runModal()
- return
- }
- var pageString: String = ""
- for i in 0 ..< array.count {
- pageString.append("\(array[i]-1)")
- if (i != array.count-1) {
- pageString.append(",")
- }
- }
- self!.model.pageRangeString = pageString
-
- if (self!.delegate != nil) {
- self!.delegate.propertyInfoDidChange(model: self!.model)
- }
- }
- }
-
- private func updateModelForContentInfo(index: Int, value: String) {
- if (index == 1) {
- self.model.topLeftString = value
- } else if (index == 2) {
- self.model.topCenterString = value
- } else if (index == 3) {
- self.model.topRightString = value
- } else if (index == 4) {
- self.model.bottomLeftString = value
- } else if (index == 5) {
- self.model.bottomCenterString = value
- } else if (index == 6) {
- self.model.bottomRightString = value
- }
-
- if (self.delegate != nil) {
- self.delegate.propertyInfoDidChange(model: self.model)
- }
- }
-
- @objc func colorPanelAction(sender: NSColorPanel) {
- let color = KMWatermarkAdjectiveTools.parseColor(color: sender.color)
- self.model.textColor = .color(red: color.red, green: color.green, blue: color.blue, alpha: color.alpha)
-
- self.fontView.colorView.layer?.backgroundColor = sender.color.cgColor
- if (self.delegate != nil) {
- self.delegate.propertyInfoDidChange(model: self.model)
- }
- }
- @objc private func insertPageAction(sender: NSMenuItem) {
- let string: String = "<<\(self.pageView.leftComboBox.stringValue)>>"
- self.contentView.textView.string = "\(self.contentView.textView.string)".appending(string)
- self.updateModelForContentInfo(index: self.contentView.indexForSelectedItem, value: self.contentView.textView.string)
- }
-
- @objc private func insertDateAction(sender: NSMenuItem) {
- let string: String = "<<\(self.myModel.dateFormatString)>>"
- self.contentView.textView.string = "\(self.contentView.textView.string)".appending(string)
- self.updateModelForContentInfo(index: self.contentView.indexForSelectedItem, value: self.contentView.textView.string)
- }
- }
|