KMBackgroundObject.swift 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // KMBackgroundObject.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by tangchao on 2023/11/3.
  6. //
  7. import Cocoa
  8. @objcMembers class KMBackgroundObject: NSObject {
  9. var type: KMBackgroundType = .color
  10. var color: NSColor = .clear
  11. var imagePath: String?
  12. var rotation: Int = 0
  13. var opacity: Double = 0
  14. var scale: Double = 0
  15. // (Top:0 Middle:1 Bottom:2)
  16. var verticalMode: Int = 0
  17. var verticalSpace: Int = 0
  18. // (Left:0 Middle:1 Right:2)
  19. var horizontalMode: Int = 0
  20. var horizontalSpace: Int = 0
  21. var pagesString: String = ""
  22. var pageRangeType: KMBatchOperatePageChoice = .All
  23. var backgroundID: String?
  24. private var _image: NSImage?
  25. var image: NSImage? {
  26. set {
  27. self._image = newValue
  28. }
  29. get {
  30. if let data = self.imagePath, FileManager.default.fileExists(atPath: data) {
  31. if let img = NSImage(contentsOfFile: data) {
  32. return img
  33. }
  34. }
  35. return self._image
  36. }
  37. }
  38. override init() {
  39. super.init()
  40. }
  41. required init?(coder: NSCoder) {
  42. // super.init()
  43. self.type = KMBackgroundType(rawValue: coder.decodeInteger(forKey: "type")) ?? .color
  44. self.color = coder.decodeObject(forKey: "color") as? NSColor ?? .clear
  45. self.imagePath = coder.decodeObject(forKey: "imagePath") as? String ?? ""
  46. self.rotation = coder.decodeInteger(forKey: "rotation")
  47. self.opacity = coder.decodeDouble(forKey: "opacity")
  48. self.scale = coder.decodeDouble(forKey: "scale")
  49. self.verticalMode = coder.decodeInteger(forKey: "verticalMode")
  50. self.verticalSpace = coder.decodeInteger(forKey: "verticalSpace")
  51. self.horizontalMode = coder.decodeInteger(forKey: "horizontalMode")
  52. self.horizontalSpace = coder.decodeInteger(forKey: "horizontalSpace")
  53. self.pagesString = coder.decodeObject(forKey: "pagesString") as? String ?? ""
  54. self.pageRangeType = KMBatchOperatePageChoice(rawValue: coder.decodeInteger(forKey: "pageRangeType")) ?? .All
  55. self.backgroundID = coder.decodeObject(forKey: "backgroundID") as? String ?? ""
  56. self._image = coder.decodeObject(forKey: "image") as? NSImage
  57. }
  58. //拷贝其他数据的值
  59. func copyData(_ dataObj: KMBackgroundObject?) {
  60. if (dataObj != nil) {
  61. self.type = dataObj!.type
  62. self.color = dataObj!.color
  63. self.imagePath = dataObj!.imagePath
  64. self.rotation = dataObj!.rotation
  65. self.opacity = dataObj!.opacity
  66. self.scale = dataObj!.scale
  67. self.verticalMode = dataObj!.verticalMode
  68. self.verticalSpace = dataObj!.verticalSpace
  69. self.horizontalMode = dataObj!.horizontalMode
  70. self.horizontalSpace = dataObj!.horizontalSpace
  71. self.pagesString = dataObj!.pagesString
  72. self.pageRangeType = dataObj!.pageRangeType
  73. self.backgroundID = dataObj!.backgroundID
  74. self.image = dataObj!.image
  75. }
  76. }
  77. }
  78. extension KMBackgroundObject: NSCoding {
  79. func encode(with coder: NSCoder) {
  80. coder.encode(self.type.rawValue, forKey: "type")
  81. coder.encode(self.color, forKey: "color")
  82. coder.encode(self.imagePath, forKey: "imagePath")
  83. coder.encode(self.rotation, forKey: "rotation")
  84. coder.encode(self.opacity, forKey: "opacity")
  85. coder.encode(self.scale, forKey: "scale")
  86. coder.encode(self.verticalMode, forKey: "verticalMode")
  87. coder.encode(self.verticalSpace, forKey: "verticalSpace")
  88. coder.encode(self.horizontalMode, forKey: "horizontalMode")
  89. coder.encode(self.horizontalSpace, forKey: "horizontalSpace")
  90. coder.encode(self.pagesString, forKey: "pagesString")
  91. coder.encode(self.pageRangeType.rawValue, forKey: "pageRangeType")
  92. coder.encode(self.backgroundID, forKey: "backgroundID")
  93. coder.encode(self.image, forKey: "image")
  94. }
  95. }
  96. extension KMBackgroundObject: NSCopying {
  97. func copy(with zone: NSZone? = nil) -> Any {
  98. let obj = KMBackgroundObject()
  99. obj.type = self.type
  100. obj.color = self.color
  101. obj.imagePath = self.imagePath
  102. obj.rotation = self.rotation
  103. obj.opacity = self.opacity
  104. obj.scale = self.scale
  105. obj.verticalMode = self.verticalMode
  106. obj.verticalSpace = self.verticalSpace
  107. obj.horizontalMode = self.horizontalMode
  108. obj.horizontalSpace = self.horizontalSpace
  109. obj.pagesString = self.pagesString
  110. obj.pageRangeType = self.pageRangeType
  111. obj.backgroundID = self.backgroundID
  112. obj.image = self.image
  113. return obj
  114. }
  115. }