123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // KMBatesPropertyInfoController.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2022/12/28.
- //
- import Cocoa
- /// Bates 属性控制器
- class KMBatesPropertyInfoController: NSViewController {
- @IBOutlet weak var scrollView: NSScrollView!
-
- @IBOutlet weak var fontBox: NSBox!
- @IBOutlet weak var marginBox: NSBox!
- @IBOutlet weak var preSufFixBox: NSBox!
- @IBOutlet weak var pageBox: NSBox!
- @IBOutlet weak var contentBox: NSBox!
- @IBOutlet weak var pageRangeBox: NSBox!
-
- var fontView = KMHeaderFooterFontInfoView()
- var marginView = KMHeaderFooterMarginInfoView()
- var preFixView = KMBatesPrefixInfoView()
- var pageView = KMBatesPageInfoView()
- var contentView = KMHeaderFooterContentInfoView()
- var pagePageView = KMConvertPageRangeSettingItemView.createFromNib()
-
- weak var delegate: KMWatermarkAdjectivePropertyDelegate!
-
- init() {
- super.init(nibName: "KMBatesPropertyInfoController", bundle: nil)
- }
-
- required init?(coder: NSCoder) {
- super.init(nibName: "KMBatesPropertyInfoController", bundle: nil)
- }
-
- private var myModel: KMHeaderFooterObject!
- var model: KMHeaderFooterObject {
- get {
- return myModel
- }
- set {
- myModel = newValue
-
- self.fontView.model = newValue
- self.marginView.model = newValue
- self.preFixView.model = newValue
-
- self.pageView.leftStepper.value = Double(newValue.batesDigits)
- self.pageView.rightComboBox.removeAllItems()
- for i in 0 ..< newValue.pageCount {
- self.pageView.rightComboBox.addItem(withObjectValue: i+1)
- }
- self.pageView.rightComboBox.stringValue = newValue.startString
-
- self.contentView.model = newValue
- }
- }
-
- override func viewWillAppear() {
- super.viewWillAppear()
-
- /// 滚动到顶部
- DispatchQueue.main.async {
- let contentView = self.scrollView.contentView;
- contentView.scroll(to: NSMakePoint(0, NSHeight(self.scrollView.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 = { [unowned self] (index: Int, value: Any) in
- if (index == 1) { /// top
- self.model.topMargin = value as! Int
- } else if (index == 2) { /// bottom
- self.model.bottomMargin = value as! Int
- } else if (index == 3) { /// left
- self.model.leftMargin = value as! Int
- } else if (index == 4) { /// right
- self.model.rightMargin = value as! Int
- }
-
- if (self.delegate != nil) {
- self.delegate.propertyInfoDidChange(model: self.model)
- }
- }
-
- let preFixView = self.preFixView
- preFixView.frame = self.preSufFixBox.contentView!.bounds
- preFixView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
- self.preSufFixBox.contentView?.addSubview(preFixView)
- preFixView.itemClick = { [weak self] (index: Int, value: Any) in
- if (index == 1) {
- self!.model.batesPrefixString = value as! String
- } else if (index == 2) {
- self!.model.batesSuffixString = 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.itemClick = { [weak self] (index: Int, value: Any) in
- if (index == 1) { /// 位数
- self!.model.batesDigits = Int(value as! Double)
- } 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 = { [self]
- (index: Int, sender: NSButton) in
-
- var string: String = "<<#\(self.model.batesDigits)"
- string.append("#\(self.model.startString)")
-
- if (!self.model.batesPrefixString.isEmpty) {
- string.append("#\(self.model.batesPrefixString)")
- } else if (!self.model.batesSuffixString.isEmpty) {
- string.append("#")
- }
-
- if (!self.model.batesSuffixString.isEmpty) {
- string.append("#\(self.model.batesSuffixString)")
- }
- string.append(">>")
-
- self.contentView.textView.string = "\(self.contentView.textView.string)".appending(string)
-
- self.updateModelForContentInfo(index: index, value: self.contentView.textView.string)
- }
- 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 = KMWatermarkeModelPageRangeType(rawValue: 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)
- }
- }
- }
-
- @objc private 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)
- }
- }
-
- 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)
- }
- }
-
- }
|