123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- //
- // 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)
- }
- }
- }
|