KMBackgroundFilePropertyInfoController.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // KMBackgroundFilePropertyInfoController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/25.
  6. //
  7. import Cocoa
  8. class KMBackgroundFilePropertyInfoController: NSViewController {
  9. @IBOutlet weak var selectFileBox: NSBox!
  10. var fileInfoView: KMWatermarkFileView = KMWatermarkFileView.createFromNib()!
  11. @IBOutlet weak var outsideBox: NSBox!
  12. var outsideView: KMWatermarkAdjectiveOutsideView = KMBackgroundOutsideView()
  13. @IBOutlet weak var positionBox: NSBox!
  14. var positionView: KMWatermarkAdjectivePositionView = KMWatermarkAdjectivePositionView.createFromNib()!
  15. @IBOutlet weak var pageRangeBox: NSBox!
  16. var pagePageView = KMConvertPageRangeSettingItemView.createFromNib()
  17. weak var delegate: KMBackgroundPropertyInfoDelegate!
  18. private var myModel: KMBackgroundModel!
  19. var model: KMBackgroundModel {
  20. get {
  21. return myModel
  22. }
  23. set {
  24. myModel = newValue
  25. self.outsideView.rComboBox.stringValue = "\(abs(Int(-newValue.rotation)))"
  26. self.outsideView.alphaComboBox.stringValue = "\(Int(newValue.opacity*100))%"
  27. self.outsideView.pageTargetScaleComboBox.stringValue = "\(Int(newValue.scale*100))"
  28. if (newValue.horizontalMode == 0) {
  29. if (model.verticalMode == 0) {
  30. self.positionView.myPositionView.selectPosition(positon: .leftTop)
  31. } else if (model.verticalMode == 1) {
  32. self.positionView.myPositionView.selectPosition(positon: .top)
  33. } else if (model.verticalMode == 2) {
  34. self.positionView.myPositionView.selectPosition(positon: .rightTop)
  35. }
  36. }
  37. if (newValue.horizontalMode == 1) {
  38. if (model.verticalMode == 0) {
  39. self.positionView.myPositionView.selectPosition(positon: .leftMiddle)
  40. } else if (model.verticalMode == 1) {
  41. self.positionView.myPositionView.selectPosition(positon: .middle)
  42. } else if (model.verticalMode == 2) {
  43. self.positionView.myPositionView.selectPosition(positon: .rightMiddle)
  44. }
  45. }
  46. if (newValue.horizontalMode == 2) {
  47. if (model.verticalMode == 0) {
  48. self.positionView.myPositionView.selectPosition(positon: .leftBottom)
  49. } else if (model.verticalMode == 1) {
  50. self.positionView.myPositionView.selectPosition(positon: .bottom)
  51. } else if (model.verticalMode == 2) {
  52. self.positionView.myPositionView.selectPosition(positon: .rightBottom)
  53. }
  54. }
  55. self.positionView.xOffsetStepper.value = newValue.horizontalSpace
  56. self.positionView.yOffsetStepper.value = newValue.verticalSpace
  57. }
  58. }
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. self.outsideBox.enclosingScrollView?.drawsBackground = false
  62. var infoView = self.fileInfoView
  63. infoView.frame = self.selectFileBox.contentView!.bounds
  64. infoView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  65. self.selectFileBox.contentView?.addSubview(infoView)
  66. self.outsideBox.isHidden = true
  67. self.positionBox.isHidden = true
  68. self.pageRangeBox.isHidden = true
  69. infoView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in
  70. let panel = NSOpenPanel()
  71. panel.allowsMultipleSelection = false
  72. panel.allowedFileTypes = ["pdf", "png", "jpeg"]
  73. panel.beginSheetModal(for: self!.view.window!) {
  74. response in
  75. if response == .cancel {
  76. return
  77. }
  78. self!.fileInfoView.tipLabel.stringValue = panel.url!.lastPathComponent
  79. self!.outsideBox.isHidden = false
  80. self!.positionBox.isHidden = false
  81. self!.pageRangeBox.isHidden = false
  82. if (panel.url?.pathExtension.lowercased() == "pdf") {
  83. let document = CPDFDocument(url: panel.url)
  84. if ((document?.isEncrypted)!) {
  85. return
  86. }
  87. let convert = KMPDFConvert()
  88. convert.convertType = .png
  89. convert.filePath = panel.url!.path
  90. convert.pages = [1]
  91. convert.outputFolderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!))!
  92. convert.outputFileName = "pdfConvertPng"
  93. KMPDFConvertManager.defaultManager.convert(convert: convert) { finished, error in
  94. if (finished) {
  95. if (FileManager.default.fileExists(atPath: convert.outputFilePath)) {
  96. let tagString = self!.tagString()
  97. let imagePath: String = (FileManager.default.subpaths(atPath: convert.outputFilePath)?.first)!
  98. var pdfPath: String = "\(convert.outputFilePath)/\(imagePath)"
  99. let newPath: String = "\(convert.outputFilePath)/\(tagString))/\(tagString).png"
  100. let result = try?FileManager.default.copyItem(atPath: pdfPath, toPath: newPath)
  101. if ((result) != nil) {
  102. pdfPath = newPath
  103. try?FileManager.default.removeItem(atPath: convert.outputFilePath)
  104. }
  105. let image = NSImage(contentsOfFile: pdfPath)
  106. if (image!.size.width <= 0 || image!.size.height <= 0 ) {
  107. let alert = NSAlert()
  108. alert.alertStyle = .critical
  109. alert.messageText = "文件\(panel.url?.lastPathComponent)已损坏"
  110. alert.runModal()
  111. return
  112. }
  113. self!.model.color = nil
  114. self!.model.imagePath = panel.url!.path
  115. self!.model.image = image
  116. if (self!.delegate != nil) {
  117. self!.delegate.propertyInfoDidChange(model: self!.model)
  118. }
  119. }
  120. }
  121. }
  122. } else {
  123. let image = NSImage(contentsOf: panel.url!)
  124. if (image!.size.width <= 0 || image!.size.height <= 0 ) {
  125. let alert = NSAlert()
  126. alert.alertStyle = .critical
  127. alert.messageText = "文件\(panel.url?.lastPathComponent)已损坏"
  128. alert.runModal()
  129. return
  130. }
  131. self!.model.imagePath = panel.url!.path
  132. self!.model.image = image
  133. if (self!.delegate != nil) {
  134. self!.delegate.propertyInfoDidChange(model: self!.model)
  135. }
  136. }
  137. }
  138. }
  139. let outsideView = self.outsideView
  140. outsideView.frame = self.outsideBox.contentView!.bounds
  141. outsideView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  142. self.outsideBox.contentView?.addSubview(outsideView)
  143. outsideView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in
  144. if (itemID == 1) { /// 旋转
  145. self!.model.rotation = -(itemValue as! Int)
  146. } else if (itemID == 2) { /// 透明度
  147. self!.model.opacity = CGFloat(itemValue as! Int)/100.0
  148. } else if (itemID == 4) { /// 缩放
  149. self!.model.scale = CGFloat((itemValue as! Int))/100.0
  150. }
  151. if (self!.delegate != nil) {
  152. self!.delegate.propertyInfoDidChange(model: self!.model)
  153. }
  154. }
  155. let positionView = self.positionView
  156. positionView.frame = self.positionBox.contentView!.bounds
  157. positionView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  158. self.positionBox.contentView?.addSubview(positionView)
  159. positionView.itemClick = { [weak self] (itemID: Int, itemValue: Any) in
  160. if (itemID == 1) {
  161. let position: KMWatermarkPosition = itemValue as! KMWatermarkPosition
  162. if (position == .leftTop || position == .top || position == .rightTop) {
  163. self!.model.verticalMode = 0
  164. } else if (position == .leftMiddle || position == .middle || position == .rightMiddle) {
  165. self!.model.verticalMode = 1
  166. } else if (position == .leftBottom || position == .bottom || position == .rightBottom) {
  167. self!.model.verticalMode = 2
  168. }
  169. if (position == .leftTop || position == .leftMiddle || position == .leftBottom) {
  170. self!.model.horizontalMode = 0
  171. } else if (position == .top || position == .middle || position == .bottom) {
  172. self!.model.horizontalMode = 1
  173. } else if (position == .rightTop || position == .rightMiddle || position == .rightBottom) {
  174. self!.model.horizontalMode = 2
  175. }
  176. } else if (itemID == 2) {
  177. self!.model.horizontalSpace = CGFloat(itemValue as! Double)
  178. } else if (itemID == 3) {
  179. self!.model.verticalSpace = CGFloat(itemValue as! Double)
  180. }
  181. if (self!.delegate != nil) {
  182. self!.delegate.propertyInfoDidChange(model: self!.model)
  183. }
  184. }
  185. let pageRangeView = self.pagePageView
  186. pageRangeView?.frame = self.pageRangeBox.contentView!.bounds
  187. pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  188. self.pageRangeBox.contentView?.addSubview(pageRangeView!)
  189. pageRangeView!.pageSizeComboBox.removeAllItems()
  190. pageRangeView!.pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")])
  191. pageRangeView!.pageSizeComboBox.selectItem(at: 0)
  192. pageRangeView?.isTextDidEndCallback = true
  193. pageRangeView?.itemClick = { [unowned self] index, _ in
  194. self.model.pageRangeType = index
  195. if (self.delegate != nil) {
  196. self.delegate.propertyInfoDidChange(model: self.model)
  197. }
  198. }
  199. pageRangeView?.textDidEndEditingCallback = { [weak self] (textString: String) in
  200. let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: self!.model.pageCount)
  201. if (array.count <= 0) {
  202. let alert = NSAlert()
  203. alert.alertStyle = .warning
  204. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  205. alert.runModal()
  206. return
  207. }
  208. var pageString: String = ""
  209. for i in 0 ..< array.count {
  210. pageString.append("\(array[i]-1)")
  211. if (i != array.count-1) {
  212. pageString.append(",")
  213. }
  214. }
  215. self!.model.pageRangeString = pageString
  216. if (self!.delegate != nil) {
  217. self!.delegate.propertyInfoDidChange(model: self!.model)
  218. }
  219. }
  220. }
  221. private func tagString() -> String {
  222. var result: String = ""
  223. let dateFormatter = DateFormatter()
  224. dateFormatter.dateFormat = "yyMMddHHmmss"
  225. result.append(dateFormatter.string(from: Date()))
  226. result = result.appendingFormat("%04d", arc4random()%10000)
  227. return result
  228. }
  229. }