// // KMBackgroundPropertyInfoController.swift // PDF Master // // Created by tangchao on 2022/12/23. // import Cocoa protocol KMBackgroundPropertyInfoDelegate: NSObjectProtocol { func propertyInfoDidChange(model: KMBackgroundModel) } class KMBackgroundPropertyInfoController: NSViewController { @IBOutlet weak var colorBox: NSBox! var colorView = KMBackgroundColorView() @IBOutlet weak var outsideBox: NSBox! var outsideView = KMBackgroundOutsideView() @IBOutlet weak var positionBox: NSBox! var positionView = KMWatermarkAdjectivePositionView.createFromNib() @IBOutlet weak var pageRangeBox: NSBox! var pagePageView = KMConvertPageRangeSettingItemView.createFromNib() weak var delegate: KMBackgroundPropertyInfoDelegate! private var myModel: KMBackgroundModel! var model: KMBackgroundModel { get { return myModel } set { myModel = newValue self.colorView.color = newValue.color! self.outsideView.rComboBox.stringValue = "\(abs(Int(-newValue.rotation)))" self.outsideView.alphaComboBox.stringValue = "\(Int(newValue.opacity*100))%" self.outsideView.pageTargetScaleComboBox.stringValue = "\(Int(newValue.scale*100))" if (newValue.horizontalMode == 0) { if (model.verticalMode == 0) { self.positionView?.myPositionView.selectPosition(positon: .leftTop) } else if (model.verticalMode == 1) { self.positionView?.myPositionView.selectPosition(positon: .top) } else if (model.verticalMode == 2) { self.positionView?.myPositionView.selectPosition(positon: .rightTop) } } if (newValue.horizontalMode == 1) { if (model.verticalMode == 0) { self.positionView?.myPositionView.selectPosition(positon: .leftMiddle) } else if (model.verticalMode == 1) { self.positionView?.myPositionView.selectPosition(positon: .middle) } else if (model.verticalMode == 2) { self.positionView?.myPositionView.selectPosition(positon: .rightMiddle) } } if (newValue.horizontalMode == 2) { if (model.verticalMode == 0) { self.positionView?.myPositionView.selectPosition(positon: .leftBottom) } else if (model.verticalMode == 1) { self.positionView?.myPositionView.selectPosition(positon: .bottom) } else if (model.verticalMode == 2) { self.positionView?.myPositionView.selectPosition(positon: .rightBottom) } } self.positionView!.xOffsetStepper.value = newValue.horizontalSpace self.positionView!.yOffsetStepper.value = newValue.verticalSpace } } override func viewDidLoad() { super.viewDidLoad() self.outsideBox.enclosingScrollView?.drawsBackground = false let colorView = self.colorView colorView.frame = self.colorBox.contentView!.bounds colorView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.colorBox.contentView?.addSubview(colorView) self.colorView = colorView colorView.itemClick = { [weak self] (index: Int, color: NSColor) in self!.model.color = color if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } colorView.colorPlateClick = { [weak self] in let panel = NSColorPanel.shared panel.setTarget(self!) panel.setAction(#selector(self!.colorPanelAction)) panel.orderFront(nil) } let outsideView = self.outsideView outsideView.frame = self.outsideBox.contentView!.bounds outsideView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.outsideBox.contentView?.addSubview(outsideView) outsideView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in if (itemID == 1) { /// 旋转 self!.model.rotation = -(itemValue as! Int) } else if (itemID == 2) { /// 透明度 self!.model.opacity = CGFloat(itemValue as! Int)/100.0 } else if (itemID == 4) { /// 缩放 self!.model.scale = CGFloat((itemValue as! Int))/100.0 } if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } let positionView = self.positionView positionView?.frame = self.positionBox.contentView!.bounds positionView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18) self.positionBox.contentView?.addSubview(positionView!) positionView?.itemClick = { [weak self] (itemID: Int, itemValue: Any) in if (itemID == 1) { let position: KMWatermarkPosition = itemValue as! KMWatermarkPosition if (position == .leftTop || position == .top || position == .rightTop) { self!.model.verticalMode = 0 } else if (position == .leftMiddle || position == .middle || position == .rightMiddle) { self!.model.verticalMode = 1 } else if (position == .leftBottom || position == .bottom || position == .rightBottom) { self!.model.verticalMode = 2 } if (position == .leftTop || position == .leftMiddle || position == .leftBottom) { self!.model.horizontalMode = 0 } else if (position == .top || position == .middle || position == .bottom) { self!.model.horizontalMode = 1 } else if (position == .rightTop || position == .rightMiddle || position == .rightBottom) { self!.model.horizontalMode = 2 } } else if (itemID == 2) { self!.model.horizontalSpace = CGFloat(itemValue as! Double) } else if (itemID == 3) { self!.model.verticalSpace = CGFloat(itemValue as! Double) } if (self!.delegate != nil) { self!.delegate.propertyInfoDidChange(model: self!.model) } } 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 func colorPanelAction(sender: NSColorPanel) { self.model.color = sender.color self.colorView.colorView.layer?.backgroundColor = sender.color.cgColor self.colorView.dealItemViewsSelected(itemID: -1) if (self.delegate != nil) { self.delegate.propertyInfoDidChange(model: self.model) } } }