123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- //
- // KMBackgroundObject.swift
- // PDF Reader Pro
- //
- // Created by tangchao on 2023/11/3.
- //
- import Cocoa
- @objcMembers class KMBackgroundObject: NSObject {
- var type: KMBackgroundType = .color
- var color: NSColor = .clear
- var imagePath: String?
-
- var rotation: Int = 0
- var opacity: Double = 0
-
- var scale: Double = 0
-
- // (Top:0 Middle:1 Bottom:2)
- var verticalMode: Int = 0
- var verticalSpace: Int = 0
- // (Left:0 Middle:1 Right:2)
- var horizontalMode: Int = 0
- var horizontalSpace: Int = 0
-
- var pagesString: String = ""
- var pageRangeType: KMBatchOperatePageChoice = .All
-
- var backgroundID: String?
- private var _image: NSImage?
- var image: NSImage? {
- set {
- self._image = newValue
- }
- get {
- if let data = self.imagePath, FileManager.default.fileExists(atPath: data) {
- if let img = NSImage(contentsOfFile: data) {
- return img
- }
- }
- return self._image
- }
- }
-
- override init() {
- super.init()
- }
-
- required init?(coder: NSCoder) {
- // super.init()
-
- self.type = KMBackgroundType(rawValue: coder.decodeInteger(forKey: "type")) ?? .color
- self.color = coder.decodeObject(forKey: "color") as? NSColor ?? .clear
- self.imagePath = coder.decodeObject(forKey: "imagePath") as? String ?? ""
- self.rotation = coder.decodeInteger(forKey: "rotation")
- self.opacity = coder.decodeDouble(forKey: "opacity")
- self.scale = coder.decodeDouble(forKey: "scale")
- self.verticalMode = coder.decodeInteger(forKey: "verticalMode")
- self.verticalSpace = coder.decodeInteger(forKey: "verticalSpace")
- self.horizontalMode = coder.decodeInteger(forKey: "horizontalMode")
- self.horizontalSpace = coder.decodeInteger(forKey: "horizontalSpace")
- self.pagesString = coder.decodeObject(forKey: "pagesString") as? String ?? ""
- self.pageRangeType = KMBatchOperatePageChoice(rawValue: coder.decodeInteger(forKey: "pageRangeType")) ?? .All
- self.backgroundID = coder.decodeObject(forKey: "backgroundID") as? String ?? ""
- self._image = coder.decodeObject(forKey: "image") as? NSImage
- }
-
- //拷贝其他数据的值
- func copyData(_ dataObj: KMBackgroundObject?) {
- if (dataObj != nil) {
- self.type = dataObj!.type
- self.color = dataObj!.color
- self.imagePath = dataObj!.imagePath
- self.rotation = dataObj!.rotation
- self.opacity = dataObj!.opacity
- self.scale = dataObj!.scale
- self.verticalMode = dataObj!.verticalMode
- self.verticalSpace = dataObj!.verticalSpace
- self.horizontalMode = dataObj!.horizontalMode
- self.horizontalSpace = dataObj!.horizontalSpace
- self.pagesString = dataObj!.pagesString
- self.pageRangeType = dataObj!.pageRangeType
- self.backgroundID = dataObj!.backgroundID
- self.image = dataObj!.image
- }
- }
- }
- extension KMBackgroundObject: NSCoding {
- func encode(with coder: NSCoder) {
- coder.encode(self.type.rawValue, forKey: "type")
- coder.encode(self.color, forKey: "color")
- coder.encode(self.imagePath, forKey: "imagePath")
- coder.encode(self.rotation, forKey: "rotation")
- coder.encode(self.opacity, forKey: "opacity")
- coder.encode(self.scale, forKey: "scale")
- coder.encode(self.verticalMode, forKey: "verticalMode")
- coder.encode(self.verticalSpace, forKey: "verticalSpace")
- coder.encode(self.horizontalMode, forKey: "horizontalMode")
- coder.encode(self.horizontalSpace, forKey: "horizontalSpace")
- coder.encode(self.pagesString, forKey: "pagesString")
- coder.encode(self.pageRangeType.rawValue, forKey: "pageRangeType")
- coder.encode(self.backgroundID, forKey: "backgroundID")
- coder.encode(self.image, forKey: "image")
- }
- }
- extension KMBackgroundObject: NSCopying {
- func copy(with zone: NSZone? = nil) -> Any {
- let obj = KMBackgroundObject()
- obj.type = self.type
- obj.color = self.color
- obj.imagePath = self.imagePath
- obj.rotation = self.rotation
- obj.opacity = self.opacity
- obj.scale = self.scale
- obj.verticalMode = self.verticalMode
- obj.verticalSpace = self.verticalSpace
- obj.horizontalMode = self.horizontalMode
- obj.horizontalSpace = self.horizontalSpace
- obj.pagesString = self.pagesString
- obj.pageRangeType = self.pageRangeType
- obj.backgroundID = self.backgroundID
- obj.image = self.image
- return obj
- }
- }
|