KMBatesPropertyInfoController.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // KMBatesPropertyInfoController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2022/12/28.
  6. //
  7. import Cocoa
  8. /// Bates 属性控制器
  9. class KMBatesPropertyInfoController: NSViewController {
  10. @IBOutlet weak var scrollView: NSScrollView!
  11. @IBOutlet weak var fontBox: NSBox!
  12. @IBOutlet weak var marginBox: NSBox!
  13. @IBOutlet weak var preSufFixBox: NSBox!
  14. @IBOutlet weak var pageBox: NSBox!
  15. @IBOutlet weak var contentBox: NSBox!
  16. @IBOutlet weak var pageRangeBox: NSBox!
  17. var fontView = KMHeaderFooterFontInfoView()
  18. var marginView = KMHeaderFooterMarginInfoView()
  19. var preFixView = KMBatesPrefixInfoView()
  20. var pageView = KMBatesPageInfoView()
  21. var contentView = KMHeaderFooterContentInfoView()
  22. var pagePageView = KMConvertPageRangeSettingItemView.createFromNib()
  23. weak var delegate: KMWatermarkAdjectivePropertyDelegate!
  24. init() {
  25. super.init(nibName: "KMBatesPropertyInfoController", bundle: nil)
  26. }
  27. required init?(coder: NSCoder) {
  28. super.init(nibName: "KMBatesPropertyInfoController", bundle: nil)
  29. }
  30. private var myModel: KMHeaderFooterObject!
  31. var model: KMHeaderFooterObject {
  32. get {
  33. return myModel
  34. }
  35. set {
  36. myModel = newValue
  37. self.fontView.model = newValue
  38. self.marginView.model = newValue
  39. self.preFixView.model = newValue
  40. self.pageView.leftStepper.value = Double(newValue.batesDigits)
  41. self.pageView.rightComboBox.removeAllItems()
  42. for i in 0 ..< newValue.pageCount {
  43. self.pageView.rightComboBox.addItem(withObjectValue: i+1)
  44. }
  45. self.pageView.rightComboBox.stringValue = newValue.startString
  46. self.contentView.model = newValue
  47. }
  48. }
  49. override func viewWillAppear() {
  50. super.viewWillAppear()
  51. /// 滚动到顶部
  52. DispatchQueue.main.async {
  53. let contentView = self.scrollView.contentView;
  54. contentView.scroll(to: NSMakePoint(0, NSHeight(self.scrollView.documentView!.frame)-NSHeight(contentView.frame)))
  55. }
  56. }
  57. override func viewDidLoad() {
  58. super.viewDidLoad()
  59. let fontView = self.fontView
  60. fontView.frame = self.fontBox.contentView!.bounds
  61. fontView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  62. self.fontBox.contentView?.addSubview(fontView)
  63. fontView.itemClick = { [weak self] (index: Int, value: Any) in
  64. if (index == 1) { /// 字体名称
  65. switch self!.model.textFont {
  66. case .font(name: _, size: let size):
  67. self!.model.textFont = .font(name: value as! String, size: size)
  68. default:
  69. break
  70. }
  71. if (self!.delegate != nil) {
  72. self!.delegate.propertyInfoDidChange(model: self!.model)
  73. }
  74. } else if (index == 2) { /// 字体大小
  75. switch self!.model.textFont {
  76. case .font(name: let name, size: _):
  77. self!.model.textFont = .font(name: name, size: CGFloat(value as! Int))
  78. default:
  79. break
  80. }
  81. if (self!.delegate != nil) {
  82. self!.delegate.propertyInfoDidChange(model: self!.model)
  83. }
  84. } else if (index == 3) { /// 字体颜色
  85. let panel = NSColorPanel.shared
  86. panel.setTarget(self)
  87. panel.setAction(#selector(self!.colorPanelAction))
  88. panel.orderFront(nil)
  89. }
  90. }
  91. let marginView = self.marginView
  92. marginView.frame = self.marginBox.contentView!.bounds
  93. marginView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  94. self.marginBox.contentView?.addSubview(marginView)
  95. marginView.itemClick = { [unowned self] (index: Int, value: Any) in
  96. if (index == 1) { /// top
  97. self.model.topMargin = value as! Int
  98. } else if (index == 2) { /// bottom
  99. self.model.bottomMargin = value as! Int
  100. } else if (index == 3) { /// left
  101. self.model.leftMargin = value as! Int
  102. } else if (index == 4) { /// right
  103. self.model.rightMargin = value as! Int
  104. }
  105. if (self.delegate != nil) {
  106. self.delegate.propertyInfoDidChange(model: self.model)
  107. }
  108. }
  109. let preFixView = self.preFixView
  110. preFixView.frame = self.preSufFixBox.contentView!.bounds
  111. preFixView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  112. self.preSufFixBox.contentView?.addSubview(preFixView)
  113. preFixView.itemClick = { [weak self] (index: Int, value: Any) in
  114. if (index == 1) {
  115. self!.model.batesPrefixString = value as! String
  116. } else if (index == 2) {
  117. self!.model.batesSuffixString = value as! String
  118. }
  119. if (self!.delegate != nil) {
  120. self!.delegate.propertyInfoDidChange(model: self!.model)
  121. }
  122. }
  123. let pageView = self.pageView
  124. pageView.frame = self.pageBox.contentView!.bounds
  125. pageView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  126. self.pageBox.contentView?.addSubview(pageView)
  127. pageView.itemClick = { [weak self] (index: Int, value: Any) in
  128. if (index == 1) { /// 位数
  129. self!.model.batesDigits = Int(value as! Double)
  130. } else if (index == 2) { /// 起始页
  131. self!.model.startString = "\(value as! Int+1)"
  132. }
  133. if (self!.delegate != nil) {
  134. self!.delegate.propertyInfoDidChange(model: self!.model)
  135. }
  136. }
  137. let contentView = self.contentView
  138. contentView.frame = self.contentBox.contentView!.bounds
  139. contentView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  140. self.contentBox.contentView?.addSubview(contentView)
  141. contentView.addAction = { [self]
  142. (index: Int, sender: NSButton) in
  143. var string: String = "<<#\(self.model.batesDigits)"
  144. string.append("#\(self.model.startString)")
  145. if (!self.model.batesPrefixString.isEmpty) {
  146. string.append("#\(self.model.batesPrefixString)")
  147. } else if (!self.model.batesSuffixString.isEmpty) {
  148. string.append("#")
  149. }
  150. if (!self.model.batesSuffixString.isEmpty) {
  151. string.append("#\(self.model.batesSuffixString)")
  152. }
  153. string.append(">>")
  154. self.contentView.textView.string = "\(self.contentView.textView.string)".appending(string)
  155. self.updateModelForContentInfo(index: index, value: self.contentView.textView.string)
  156. }
  157. contentView.itemClick = { [weak self] (index: Int, value: Any) in
  158. self!.updateModelForContentInfo(index: index, value: value as! String)
  159. }
  160. let pageRangeView = self.pagePageView
  161. pageRangeView?.frame = self.pageRangeBox.contentView!.bounds
  162. pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  163. self.pageRangeBox.contentView?.addSubview(pageRangeView!)
  164. pageRangeView!.pageSizeComboBox.removeAllItems()
  165. pageRangeView!.pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")])
  166. pageRangeView!.pageSizeComboBox.selectItem(at: 0)
  167. pageRangeView?.isTextDidEndCallback = true
  168. pageRangeView?.itemClick = { [unowned self] index, _ in
  169. self.model.pageRangeType = KMWatermarkeModelPageRangeType(rawValue: index)!
  170. if (self.delegate != nil) {
  171. self.delegate.propertyInfoDidChange(model: self.model)
  172. }
  173. }
  174. pageRangeView?.textDidEndEditingCallback = { [weak self] (textString: String) in
  175. let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: self!.model.pageCount)
  176. if (array.count <= 0) {
  177. let alert = NSAlert()
  178. alert.alertStyle = .warning
  179. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  180. alert.runModal()
  181. return
  182. }
  183. var pageString: String = ""
  184. for i in 0 ..< array.count {
  185. pageString.append("\(array[i]-1)")
  186. if (i != array.count-1) {
  187. pageString.append(",")
  188. }
  189. }
  190. self!.model.pageRangeString = pageString
  191. if (self!.delegate != nil) {
  192. self!.delegate.propertyInfoDidChange(model: self!.model)
  193. }
  194. }
  195. }
  196. @objc private func colorPanelAction(sender: NSColorPanel) {
  197. let color = KMWatermarkAdjectiveTools.parseColor(color: sender.color)
  198. self.model.textColor = .color(red: color.red, green: color.green, blue: color.blue, alpha: color.alpha)
  199. self.fontView.colorView.layer?.backgroundColor = sender.color.cgColor
  200. if (self.delegate != nil) {
  201. self.delegate.propertyInfoDidChange(model: self.model)
  202. }
  203. }
  204. private func updateModelForContentInfo(index: Int, value: String) {
  205. if (index == 1) {
  206. self.model.topLeftString = value
  207. } else if (index == 2) {
  208. self.model.topCenterString = value
  209. } else if (index == 3) {
  210. self.model.topRightString = value
  211. } else if (index == 4) {
  212. self.model.bottomLeftString = value
  213. } else if (index == 5) {
  214. self.model.bottomCenterString = value
  215. } else if (index == 6) {
  216. self.model.bottomRightString = value
  217. }
  218. if (self.delegate != nil) {
  219. self.delegate.propertyInfoDidChange(model: self.model)
  220. }
  221. }
  222. }