KMWatermarkPropertyInfoController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // KMWatermarkPropertyInfoController.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2022/12/16.
  6. //
  7. import Cocoa
  8. enum KMWatermarkPropertyType: Int {
  9. case text = 0
  10. case file = 1
  11. }
  12. class KMWatermarkPropertyInfoController: NSViewController {
  13. @IBOutlet weak var scrollView: NSScrollView!
  14. @IBOutlet weak var infoBox: NSBox!
  15. @IBOutlet weak var infoBoxHeightConst: NSLayoutConstraint!
  16. @IBOutlet weak var outsideBox: NSBox!
  17. @IBOutlet weak var outsideHeightConst: NSLayoutConstraint!
  18. @IBOutlet weak var positionBox: NSBox!
  19. @IBOutlet weak var plainBox: NSBox!
  20. @IBOutlet weak var pageRangeBox: NSBox!
  21. var model: KMWatermarkModel!
  22. var type: KMWatermarkPropertyType = .text
  23. var fileInfoView: KMWatermarkFileView!
  24. var textInfoView: KMWatermarkTextView!
  25. var outsideView: KMWatermarkAdjectiveOutsideView!
  26. var fileOutSideView: KMWatermarkFileOutsideView!
  27. var positiomView: KMWatermarkAdjectivePositionView!
  28. var plainView: KMWatermarkAdjectivePlainView!
  29. var pageRangeView: KMConvertPageRangeSettingItemView!
  30. weak var delegate: KMWatermarkAdjectivePropertyDelegate!
  31. override func viewDidLoad() {
  32. super.viewDidLoad()
  33. // Do view setup here.
  34. self.scrollView.hasVerticalScroller = false
  35. self.scrollView.hasHorizontalScroller = false
  36. if (self.type == .text) {
  37. self.infoBoxHeightConst.constant = 178
  38. var infoView = KMWatermarkTextView.createFromNib()
  39. infoView?.setModel(model: self.model)
  40. infoView?.frame = self.infoBox.contentView!.bounds
  41. infoView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  42. self.infoBox.contentView?.addSubview(infoView!)
  43. self.textInfoView = infoView
  44. infoView?.itemClick = {
  45. (itemID: Int, itemValue: Any) in
  46. if (itemID == 1) {
  47. self.model.text = itemValue as! String
  48. } else if (itemID == 2) {
  49. self.model.textFont = .font(name: itemValue as! String, size: self.model.getTextFontSize())
  50. } else if (itemID == 3) {
  51. let size: Int = itemValue as! Int
  52. self.model.textFont = .font(name: self.model.getTextFontName(), size: CGFloat(size))
  53. } else if (itemID == 4) {
  54. let aligement: Int = itemValue as! Int
  55. if (aligement == 0) {
  56. self.model.textAligement = .left
  57. } else if (aligement == 1) {
  58. self.model.textAligement = .center
  59. } else if (aligement == 2) {
  60. self.model.textAligement = .right
  61. }
  62. } else if (itemID == 5) {
  63. let color = KMWatermarkAdjectiveTools.parseColor(color: itemValue as! NSColor)
  64. self.model.textColor = .color(red: color.red, green: color.green, blue: color.blue, alpha: color.alpha)
  65. }
  66. self.updateModel(model: self.model)
  67. }
  68. } else {
  69. self.infoBoxHeightConst.constant = 50
  70. var infoView = KMWatermarkFileView.createFromNib()
  71. infoView?.frame = self.infoBox.contentView!.bounds
  72. infoView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  73. self.infoBox.contentView?.addSubview(infoView!)
  74. self.fileInfoView = infoView
  75. self.outsideBox.isHidden = true
  76. self.positionBox.isHidden = true
  77. self.plainBox.isHidden = true
  78. self.pageRangeBox.isHidden = true
  79. infoView?.itemClick = {
  80. (itemID: Int, itemValue: Any) in
  81. let panel = NSOpenPanel()
  82. panel.allowsMultipleSelection = false
  83. panel.allowedFileTypes = ["pdf", "png", "jpeg"]
  84. panel.beginSheetModal(for: self.view.window!) {
  85. response in
  86. if response == .cancel {
  87. return
  88. }
  89. self.fileInfoView.tipLabel.stringValue = panel.url!.lastPathComponent
  90. self.outsideBox.isHidden = false
  91. self.positionBox.isHidden = false
  92. self.plainBox.isHidden = false
  93. self.pageRangeBox.isHidden = false
  94. if (panel.url?.pathExtension.lowercased() == "pdf") {
  95. let document = CPDFDocument(url: panel.url)
  96. if ((document?.isEncrypted)!) {
  97. return
  98. }
  99. let convert = KMPDFConvert()
  100. convert.convertType = .png
  101. convert.filePath = panel.url!.path
  102. convert.pages = [1]
  103. convert.outputFolderPath = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.applicationSupportDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last?.stringByAppendingPathComponent(Bundle.main.bundleIdentifier!))!
  104. convert.outputFileName = "pdfConvertPng"
  105. KMPDFConvertManager.defaultManager.convert(convert: convert) { finished, error in
  106. if (finished) {
  107. if (FileManager.default.fileExists(atPath: convert.outputFilePath)) {
  108. let tagString = self.tagString()
  109. let imagePath: String = (FileManager.default.subpaths(atPath: convert.outputFilePath)?.first)!
  110. var pdfPath: String = "\(convert.outputFilePath)/\(imagePath)"
  111. let newPath: String = "\(convert.outputFilePath)/\(tagString))/\(tagString).png"
  112. let result = try?FileManager.default.copyItem(atPath: pdfPath, toPath: newPath)
  113. if ((result) != nil) {
  114. pdfPath = newPath
  115. try?FileManager.default.removeItem(atPath: convert.outputFilePath)
  116. }
  117. let image = NSImage(contentsOfFile: pdfPath)
  118. if (image!.size.width <= 0 || image!.size.height <= 0 ) {
  119. let alert = NSAlert()
  120. alert.alertStyle = .critical
  121. alert.messageText = "文件\(panel.url?.lastPathComponent)已损坏"
  122. alert.runModal()
  123. return
  124. }
  125. self.model.imagePath = panel.url?.path
  126. self.model.image = image
  127. self.model.text = ""
  128. self.model.watermarkID = panel.url?.lastPathComponent.components(separatedBy: ".").first
  129. self.modelDidChange(self.model)
  130. }
  131. }
  132. }
  133. } else {
  134. let image = NSImage(contentsOf: panel.url!)
  135. if (image!.size.width <= 0 || image!.size.height <= 0 ) {
  136. let alert = NSAlert()
  137. alert.alertStyle = .critical
  138. alert.messageText = "文件\(panel.url?.lastPathComponent)已损坏"
  139. alert.runModal()
  140. return
  141. }
  142. self.model.imagePath = panel.url?.path
  143. self.model.image = image
  144. self.model.text = ""
  145. self.model.watermarkID = panel.url?.lastPathComponent.components(separatedBy: ".").first
  146. self.modelDidChange(self.model)
  147. }
  148. }
  149. }
  150. }
  151. /// 外观
  152. self.outsideHeightConst.constant = 150
  153. var outsideView = KMWatermarkAdjectiveOutsideView()
  154. outsideView.frame = self.outsideBox.contentView!.frame
  155. outsideView.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  156. outsideView.setModel(model: model)
  157. self.outsideBox.contentView?.addSubview(outsideView)
  158. self.outsideView = outsideView
  159. outsideView.itemClick = {
  160. (itemID: Int, itemValue: Any) in
  161. if (itemID == 1) {
  162. self.model.rotation = -CGFloat(itemValue as! Int)
  163. } else if (itemID == 2) {
  164. self.model.opacity = CGFloat(itemValue as! Int)/100.0
  165. } else if (itemID == 3) {
  166. let value: Int = itemValue as! Int
  167. if (value == 0) {
  168. self.model.isFront = true
  169. } else {
  170. self.model.isFront = false
  171. }
  172. } else if (itemID == 4) {
  173. self.model.scale = CGFloat((itemValue as! Int))/100.0
  174. }
  175. self.modelDidChange(self.model)
  176. }
  177. /// 位置
  178. var positionView = KMWatermarkAdjectivePositionView.createFromNib()
  179. positionView?.frame = self.positionBox.contentView!.bounds
  180. positionView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  181. self.positionBox.contentView?.addSubview(positionView!)
  182. self.positiomView = positionView
  183. positionView?.itemClick = {
  184. (itemID: Int, itemValue: Any) in
  185. if (itemID == 1) {
  186. let position: KMWatermarkPosition = itemValue as! KMWatermarkPosition
  187. if (position == .leftTop || position == .top || position == .rightTop) {
  188. self.model.verticalMode = 0
  189. } else if (position == .leftMiddle || position == .middle || position == .rightMiddle) {
  190. self.model.verticalMode = 1
  191. } else if (position == .leftBottom || position == .bottom || position == .rightBottom) {
  192. self.model.verticalMode = 2
  193. }
  194. if (position == .leftTop || position == .leftMiddle || position == .leftBottom) {
  195. self.model.horizontalMode = 0
  196. } else if (position == .top || position == .middle || position == .bottom) {
  197. self.model.horizontalMode = 1
  198. } else if (position == .rightTop || position == .rightMiddle || position == .rightBottom) {
  199. self.model.horizontalMode = 2
  200. }
  201. } else if (itemID == 2) {
  202. self.model.horizontalSpace = CGFloat(itemValue as! Double)
  203. } else if (itemID == 3) {
  204. self.model.verticalSpace = CGFloat(itemValue as! Double)
  205. }
  206. self.modelDidChange(self.model)
  207. }
  208. /// 平铺
  209. var plainView = KMWatermarkAdjectivePlainView.createFromNib()
  210. plainView?.frame = self.plainBox.contentView!.bounds
  211. plainView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  212. self.plainBox.contentView?.addSubview(plainView!)
  213. self.plainView = plainView
  214. plainView?.setModel(model: self.model)
  215. plainView?.itemClick = {
  216. (itemID: Int, itemValue: Any) in
  217. if (itemID == 1) {
  218. self.model.isTilePage = itemValue as! Bool
  219. } else if (itemID == 2) {
  220. self.model.tileHorizontalSpace = CGFloat(itemValue as! Double)
  221. } else if (itemID == 3) {
  222. self.model.tileVerticalSpace = CGFloat(itemValue as! Double)
  223. }
  224. self.modelDidChange(self.model)
  225. }
  226. var pageRangeView = KMConvertPageRangeSettingItemView.createFromNib()
  227. pageRangeView?.frame = pageRangeBox.contentView!.bounds
  228. pageRangeView?.autoresizingMask = NSView.AutoresizingMask(rawValue: 18)
  229. pageRangeBox.contentView?.addSubview(pageRangeView!)
  230. pageRangeView!.pageSizeComboBox.removeAllItems()
  231. pageRangeView!.pageSizeComboBox.addItems(withObjectValues: [NSLocalizedString("All Pages", comment: ""), NSLocalizedString("Odd Pages", comment: ""), NSLocalizedString("Even Pages", comment: ""), NSLocalizedString("Customized", comment: "")])
  232. pageRangeView!.pageSizeComboBox.selectItem(at: 0)
  233. self.pageRangeView = pageRangeView
  234. pageRangeView?.isTextDidEndCallback = true
  235. pageRangeView?.itemClick = { [unowned self] index, _ in
  236. self.model.pageRangeType = index
  237. self.modelDidChange(self.model)
  238. }
  239. pageRangeView?.textDidEndEditingCallback = {
  240. [unowned self] (textString: String) in
  241. let array = KMPageRangeTools.findSelectPage(pageRangeString: textString, pageCount: Int(self.model.pageCount))
  242. if (array.count <= 0) {
  243. let alert = NSAlert()
  244. alert.alertStyle = .warning
  245. alert.messageText = NSLocalizedString("Invalid page range or the page number is out of range. Please try again.", comment: "")
  246. alert.runModal()
  247. return
  248. }
  249. var pageString: String = ""
  250. for i in 0 ..< array.count {
  251. pageString.append("\(array[i]-1)")
  252. if (i != array.count-1) {
  253. pageString.append(",")
  254. }
  255. }
  256. self.model.pagesString = pageString
  257. self.modelDidChange(self.model)
  258. }
  259. }
  260. override func viewDidAppear() {
  261. super.viewDidAppear()
  262. /// 滚动到顶部
  263. let contentView = self.scrollView.contentView;
  264. contentView.scroll(to: NSMakePoint(0, NSHeight(self.scrollView.documentView!.frame)-NSHeight(contentView.frame)))
  265. }
  266. private func tagString() -> String {
  267. var result: String = ""
  268. let dateFormatter = DateFormatter()
  269. dateFormatter.dateFormat = "yyMMddHHmmss"
  270. result.append(dateFormatter.string(from: Date()))
  271. result = result.appendingFormat("%04d", arc4random()%10000)
  272. return result
  273. }
  274. private func modelDidChange(_ model: KMWatermarkModel) {
  275. self.delegate.propertyInfoDidChange(model: model)
  276. }
  277. func updateModel(model: KMWatermarkModel) {
  278. self.model = model
  279. self.outsideBox.isHidden = false
  280. self.plainBox.isHidden = false
  281. self.positionBox.isHidden = false
  282. self.pageRangeBox.isHidden = false
  283. if (self.textInfoView != nil) {
  284. self.textInfoView.setModel(model: model)
  285. }
  286. if (self.fileInfoView != nil) {
  287. self.fileInfoView.setModel(model: model)
  288. self.fileInfoView.tipLabel.stringValue = model.imagePath.lastPathComponent
  289. }
  290. if (self.outsideView != nil) {
  291. self.outsideView.setModel(model: model)
  292. }
  293. if (self.fileOutSideView != nil) {
  294. self.fileOutSideView.setModel(model: model)
  295. }
  296. if (self.positiomView != nil) {
  297. self.positiomView.setModel(model: model)
  298. }
  299. if (self.plainView != nil) {
  300. self.plainView.setModel(model: model)
  301. }
  302. }
  303. }