KMBackgroundPropertyInfoController.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. //
  2. // KMBackgroundPropertyInfoController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/23.
  6. //
  7. import Cocoa
  8. protocol KMBackgroundPropertyInfoDelegate: NSObjectProtocol {
  9. func propertyInfoDidChange(model: KMBackgroundModel)
  10. }
  11. class KMBackgroundPropertyInfoController: NSViewController {
  12. @IBOutlet weak var colorBox: NSBox!
  13. var colorView = KMBackgroundColorView()
  14. @IBOutlet weak var outsideBox: NSBox!
  15. var outsideView = KMBackgroundOutsideView()
  16. @IBOutlet weak var positionBox: NSBox!
  17. var positionView = KMWatermarkAdjectivePositionView.createFromNib()
  18. @IBOutlet weak var pageRangeBox: NSBox!
  19. var pagePageView = KMConvertPageRangeSettingItemView.createFromNib()
  20. weak var delegate: KMBackgroundPropertyInfoDelegate!
  21. private var myModel: KMBackgroundModel!
  22. var model: KMBackgroundModel {
  23. get {
  24. return myModel
  25. }
  26. set {
  27. myModel = newValue
  28. self.colorView.color = newValue.color!
  29. self.outsideView.rComboBox.stringValue = "\(abs(Int(-newValue.rotation)))"
  30. self.outsideView.alphaComboBox.stringValue = "\(Int(newValue.opacity*100))%"
  31. self.outsideView.pageTargetScaleComboBox.stringValue = "\(Int(newValue.scale*100))"
  32. if (newValue.horizontalMode == 0) {
  33. if (model.verticalMode == 0) {
  34. self.positionView?.myPositionView.selectPosition(positon: .leftTop)
  35. } else if (model.verticalMode == 1) {
  36. self.positionView?.myPositionView.selectPosition(positon: .top)
  37. } else if (model.verticalMode == 2) {
  38. self.positionView?.myPositionView.selectPosition(positon: .rightTop)
  39. }
  40. }
  41. if (newValue.horizontalMode == 1) {
  42. if (model.verticalMode == 0) {
  43. self.positionView?.myPositionView.selectPosition(positon: .leftMiddle)
  44. } else if (model.verticalMode == 1) {
  45. self.positionView?.myPositionView.selectPosition(positon: .middle)
  46. } else if (model.verticalMode == 2) {
  47. self.positionView?.myPositionView.selectPosition(positon: .rightMiddle)
  48. }
  49. }
  50. if (newValue.horizontalMode == 2) {
  51. if (model.verticalMode == 0) {
  52. self.positionView?.myPositionView.selectPosition(positon: .leftBottom)
  53. } else if (model.verticalMode == 1) {
  54. self.positionView?.myPositionView.selectPosition(positon: .bottom)
  55. } else if (model.verticalMode == 2) {
  56. self.positionView?.myPositionView.selectPosition(positon: .rightBottom)
  57. }
  58. }
  59. self.positionView!.xOffsetStepper.value = newValue.horizontalSpace
  60. self.positionView!.yOffsetStepper.value = newValue.verticalSpace
  61. }
  62. }
  63. override func viewDidLoad() {
  64. super.viewDidLoad()
  65. self.outsideBox.enclosingScrollView?.drawsBackground = false
  66. let colorView = self.colorView
  67. colorView.frame = self.colorBox.contentView!.bounds
  68. colorView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  69. self.colorBox.contentView?.addSubview(colorView)
  70. self.colorView = colorView
  71. colorView.itemClick = { [weak self] (index: Int, color: NSColor) in
  72. self!.model.color = color
  73. if (self!.delegate != nil) {
  74. self!.delegate.propertyInfoDidChange(model: self!.model)
  75. }
  76. }
  77. colorView.colorPlateClick = { [weak self] in
  78. let panel = NSColorPanel.shared
  79. panel.setTarget(self!)
  80. panel.setAction(#selector(self!.colorPanelAction))
  81. panel.orderFront(nil)
  82. }
  83. let outsideView = self.outsideView
  84. outsideView.frame = self.outsideBox.contentView!.bounds
  85. outsideView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  86. self.outsideBox.contentView?.addSubview(outsideView)
  87. outsideView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in
  88. if (itemID == 1) { /// 旋转
  89. self!.model.rotation = -(itemValue as! Int)
  90. } else if (itemID == 2) { /// 透明度
  91. self!.model.opacity = CGFloat(itemValue as! Int)/100.0
  92. } else if (itemID == 4) { /// 缩放
  93. self!.model.scale = CGFloat((itemValue as! Int))/100.0
  94. }
  95. if (self!.delegate != nil) {
  96. self!.delegate.propertyInfoDidChange(model: self!.model)
  97. }
  98. }
  99. let positionView = self.positionView
  100. positionView?.frame = self.positionBox.contentView!.bounds
  101. positionView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  102. self.positionBox.contentView?.addSubview(positionView!)
  103. positionView?.itemClick = { [weak self] (itemID: Int, itemValue: Any) in
  104. if (itemID == 1) {
  105. let position: KMWatermarkPosition = itemValue as! KMWatermarkPosition
  106. if (position == .leftTop || position == .top || position == .rightTop) {
  107. self!.model.verticalMode = 0
  108. } else if (position == .leftMiddle || position == .middle || position == .rightMiddle) {
  109. self!.model.verticalMode = 1
  110. } else if (position == .leftBottom || position == .bottom || position == .rightBottom) {
  111. self!.model.verticalMode = 2
  112. }
  113. if (position == .leftTop || position == .leftMiddle || position == .leftBottom) {
  114. self!.model.horizontalMode = 0
  115. } else if (position == .top || position == .middle || position == .bottom) {
  116. self!.model.horizontalMode = 1
  117. } else if (position == .rightTop || position == .rightMiddle || position == .rightBottom) {
  118. self!.model.horizontalMode = 2
  119. }
  120. } else if (itemID == 2) {
  121. self!.model.horizontalSpace = CGFloat(itemValue as! Double)
  122. } else if (itemID == 3) {
  123. self!.model.verticalSpace = CGFloat(itemValue as! Double)
  124. }
  125. if (self!.delegate != nil) {
  126. self!.delegate.propertyInfoDidChange(model: self!.model)
  127. }
  128. }
  129. let pageRangeView = self.pagePageView
  130. pageRangeView?.frame = self.pageRangeBox.contentView!.bounds
  131. pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  132. self.pageRangeBox.contentView?.addSubview(pageRangeView!)
  133. pageRangeView!.pageSizeComboBox.removeAllItems()
  134. pageRangeView!.pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")])
  135. pageRangeView!.pageSizeComboBox.selectItem(at: 0)
  136. pageRangeView?.isTextDidEndCallback = true
  137. pageRangeView?.itemClick = { [unowned self] index, _ in
  138. self.model.pageRangeType = index
  139. if (self.delegate != nil) {
  140. self.delegate.propertyInfoDidChange(model: self.model)
  141. }
  142. }
  143. pageRangeView?.textDidEndEditingCallback = { [weak self] (textString: String) in
  144. let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: self!.model.pageCount)
  145. if (array.count <= 0) {
  146. let alert = NSAlert()
  147. alert.alertStyle = .warning
  148. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  149. alert.runModal()
  150. return
  151. }
  152. var pageString: String = ""
  153. for i in 0 ..< array.count {
  154. pageString.append("\(array[i]-1)")
  155. if (i != array.count-1) {
  156. pageString.append(",")
  157. }
  158. }
  159. self!.model.pageRangeString = pageString
  160. if (self!.delegate != nil) {
  161. self!.delegate.propertyInfoDidChange(model: self!.model)
  162. }
  163. }
  164. }
  165. @objc func colorPanelAction(sender: NSColorPanel) {
  166. self.model.color = sender.color
  167. self.colorView.colorView.layer?.backgroundColor = sender.color.cgColor
  168. self.colorView.dealItemViewsSelected(itemID: -1)
  169. if (self.delegate != nil) {
  170. self.delegate.propertyInfoDidChange(model: self.model)
  171. }
  172. }
  173. }