// // KMBatesPropertyInfoController.swift // PDF Master // // 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: KMBatesModel! var model: KMBatesModel { get { return myModel } set { myModel = newValue self.fontView.model = newValue self.marginView.model = newValue self.preFixView.model = newValue self.pageView.leftStepper.value = Double(newValue.digits) 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 = { [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 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.prefixString = value as! String } else if (index == 2) { self!.model.suffixString = 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.digits = 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.digits)" string.append("#\(self.model.startString)") if (!self.model.prefixString.isEmpty) { string.append("#\(self.model.prefixString)") } else if (!self.model.suffixString.isEmpty) { string.append("#") } if (!self.model.suffixString.isEmpty) { string.append("#\(self.model.suffixString)") } 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 = 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) } } }