KMEditImagePropertyViewController.swift 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. //
  2. // KMEditImagePropertyViewController.swift
  3. // PDF Master
  4. //
  5. // Created by lxy on 2022/12/28.
  6. //
  7. import Cocoa
  8. public enum KMOperationState {
  9. case none
  10. case began
  11. case changed
  12. case ended
  13. }
  14. enum KMEditImagePropertyViewControllerChangeType {
  15. case rotate
  16. case flip
  17. case add
  18. case move
  19. }
  20. protocol KMEditImagePropertyViewControllerDelegate: NSObject {
  21. func editImagePropertyViewControllerDidChanged(controller: KMEditImagePropertyViewController, type: KMEditImagePropertyViewControllerChangeType)
  22. }
  23. class KMEditImagePropertyViewController: NSViewController {
  24. @IBOutlet weak var titleLabel: NSTextField!
  25. @IBOutlet weak var editImageView: NSImageView!
  26. @IBOutlet weak var headerBox: NSBox!
  27. @IBOutlet weak var headerBoxHeight: NSLayoutConstraint!
  28. @IBOutlet weak var headerBoxMaginWidthConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var opacityTitleLabel: NSTextField!
  30. @IBOutlet weak var opacitySlider: NSSlider!
  31. @IBOutlet weak var opacityBox: NSBox!
  32. @IBOutlet weak var opacityBoxBottomMaginConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var opacityBoxTopConstraint: NSLayoutConstraint!
  34. @IBOutlet weak var cropBox: NSBox!
  35. @IBOutlet weak var confirmBox: NSBox!
  36. @IBOutlet weak var cancelBox: NSBox!
  37. @IBOutlet weak var replaceBox: NSBox!
  38. @IBOutlet weak var exportBox: NSBox!
  39. @IBOutlet weak var imageBox: NSBox!
  40. @IBOutlet weak var imageBoxHeight: NSLayoutConstraint!
  41. @IBOutlet weak var opacityBoxHeight: NSLayoutConstraint!
  42. @IBOutlet weak var buttonBoxHeight: NSLayoutConstraint!
  43. @IBOutlet weak var buttonBox: NSBox!
  44. @IBOutlet weak var rotateLeftBox: NSBox!
  45. @IBOutlet weak var rotateRightBox: NSBox!
  46. @IBOutlet weak var flipHorizontalBox: NSBox!
  47. @IBOutlet weak var flipVerticalBox: NSBox!
  48. @IBOutlet weak var alignmentView: KMEditPropertyAlignmentView!
  49. @IBOutlet weak var alignmentViewTopConstraint: NSLayoutConstraint!
  50. //NO Image
  51. @IBOutlet weak var noImageView: NSImageView!
  52. @IBOutlet weak var noTitleLabel: NSTextField!
  53. @IBOutlet weak var noSubtitleLabel: NSTextField!
  54. @IBOutlet weak var noBackgroundView: NSView!
  55. var rotateLeftVC: KMDesignPropertySelector?
  56. var rotateRightVC: KMDesignPropertySelector?
  57. var flipHorizontalVC: KMDesignPropertySelector?
  58. var flipVerticalVC: KMDesignPropertySelector?
  59. var opacityVC: KMDesignSelect?
  60. var cropVC: KMDesignButton?
  61. var confirmVC: KMDesignButton?
  62. var cancelVC: KMDesignButton?
  63. var replaceVC: KMDesignButton?
  64. var exportVC: KMDesignButton?
  65. var imagesAreas : [CPDFEditImageArea] = []
  66. var listView : CPDFListView!
  67. var editingAreas: [Any] {
  68. get {
  69. return self.listView.editingAreas() ?? []
  70. }
  71. }
  72. weak var delegate: KMEditImagePropertyViewControllerDelegate?
  73. override func viewDidLoad() {
  74. super.viewDidLoad()
  75. self.setup()
  76. self.initData()
  77. self.updateLanguage()
  78. self.updateButtonState(hidden: true)
  79. self.reloadData()
  80. }
  81. func initData() {
  82. self.updateButtonState(hidden: true)
  83. }
  84. func setup() {
  85. self.titleLabel.font = NSFont.SFProTextSemiboldFont(14.0)
  86. self.titleLabel.textColor = NSColor.km_init(hex: "#252629")
  87. self.opacityTitleLabel.font = NSFont.SFProTextSemiboldFont(12.0)
  88. self.opacityTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  89. self.imageBox.borderWidth = 1
  90. self.imageBox.borderColor = NSColor.km_init(hex: "#DFE1E5")
  91. self.imageBox.cornerRadius = 4
  92. self.imageBox.fillColor = NSColor.km_init(hex: "#FFFFFF")
  93. self.alignmentView.didChange = {[unowned self] view, areasArray, boundsArray in
  94. self.changeAreasAlign(areas: areasArray, newBounds: boundsArray)
  95. }
  96. imageBox.backgroundColor(NSColor.km_init(hex: "#FFFFFF"))
  97. opacityVC = KMDesignSelect.init(withType: .PopButton)
  98. opacityBox.contentView = opacityVC?.view
  99. opacityBox.fillColor = NSColor.clear
  100. opacityVC?.addItems(withObjectValues: ["25%","50%","75%","100%"])
  101. opacityVC?.selectItem(at: 0)
  102. opacityVC?.delete = self
  103. //alignment
  104. rotateLeftVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  105. rotateLeftBox.contentView = rotateLeftVC?.view
  106. rotateLeftBox.fillColor = NSColor.clear
  107. rotateLeftVC?.target = self
  108. rotateLeftVC?.action = #selector(leftRotationImageAction)
  109. rotateLeftVC?.image = NSImage(named: "KMImageNameEditPDFRotationLeft")!
  110. rotateLeftVC?._image_disabled = NSImage(named: "KMImageNameEditPDFRotationLeft")!
  111. rotateLeftVC?.updateUI()
  112. rotateRightVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  113. rotateRightBox.contentView = rotateRightVC?.view
  114. rotateRightBox.fillColor = NSColor.clear
  115. rotateRightVC?.target = self
  116. rotateRightVC?.action = #selector(rightRotationImageAction)
  117. rotateRightVC?.image = NSImage(named: "KMImageNameEditPDFRotationRight")!
  118. rotateRightVC?.updateUI()
  119. flipHorizontalVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  120. flipHorizontalBox.contentView = flipHorizontalVC?.view
  121. flipHorizontalBox.fillColor = NSColor.clear
  122. flipHorizontalVC?.target = self
  123. flipHorizontalVC?.action = #selector(flipHorizontalImageAction)
  124. flipHorizontalVC?.image = NSImage(named: "KMImageNameEditPDFFlipHorizontal")!
  125. flipHorizontalVC?.updateUI()
  126. flipVerticalVC = KMDesignPropertySelector.init(withType: .Icon_Btn)
  127. flipVerticalBox.contentView = flipVerticalVC?.view
  128. flipVerticalBox.fillColor = NSColor.clear
  129. flipVerticalVC?.target = self
  130. flipVerticalVC?.action = #selector(flipVerticalImageAction)
  131. flipVerticalVC?.image = NSImage(named: "KMImageNameEditPDFFlipVertical")!
  132. flipVerticalVC?.updateUI()
  133. // 设置默认状态下的滑块图像
  134. let customCell = CustomSliderCell()
  135. // 设置滑块图像
  136. customCell.knobImage = NSImage(named: "KMImageNameEditPDFSlider")
  137. // 应用自定义的NSSliderCell到NSSlider
  138. opacitySlider.cell = customCell
  139. // 添加滑动事件处理程序
  140. opacitySlider.target = self
  141. opacitySlider.action = #selector(sliderValueChanged(_:))
  142. opacitySlider.isContinuous = false
  143. cropVC = KMDesignButton.init(withType: .Text)
  144. cropBox.contentView = cropVC?.view
  145. cropBox.fillColor = NSColor.clear
  146. cropVC?.target = self
  147. cropVC?.action = #selector(cutImageAction)
  148. cropVC?.button(type: .Sec, size: .m)
  149. cropVC?.stringValue = NSLocalizedString("Crop", comment: "")
  150. cropVC?.updateUI()
  151. confirmVC = KMDesignButton.init(withType: .Text)
  152. confirmBox.contentView = confirmVC?.view
  153. confirmBox.fillColor = NSColor.clear
  154. confirmVC?.target = self
  155. confirmVC?.action = #selector(confirmVCImageAction)
  156. confirmVC?.button(type: .Cta, size: .m)
  157. confirmVC?.stringValue = NSLocalizedString("Confirm Cut", comment: "")
  158. confirmVC?.updateUI()
  159. confirmVC?.button.keyEquivalent = KMKeyEquivalent.enter
  160. cancelVC = KMDesignButton.init(withType: .Text)
  161. cancelBox.contentView = cancelVC?.view
  162. cancelBox.fillColor = NSColor.clear
  163. cancelVC?.target = self
  164. cancelVC?.action = #selector(cancelCutImageAction)
  165. cancelVC?.button(type: .Sec, size: .m)
  166. cancelVC?.stringValue = NSLocalizedString("Cancel", comment: "")
  167. cancelVC?.updateUI()
  168. replaceVC = KMDesignButton.init(withType: .Text)
  169. replaceBox.contentView = replaceVC?.view
  170. replaceBox.fillColor = NSColor.clear
  171. replaceVC?.target = self
  172. replaceVC?.action = #selector(replaceImageAction)
  173. replaceVC?.button(type: .Sec, size: .m)
  174. replaceVC?.stringValue = NSLocalizedString("Replace", comment: "")
  175. replaceVC?.updateUI()
  176. exportVC = KMDesignButton.init(withType: .Text)
  177. exportBox.contentView = exportVC?.view
  178. exportBox.fillColor = NSColor.clear
  179. exportVC?.target = self
  180. exportVC?.action = #selector(exportImageAction)
  181. exportVC?.button(type: .Sec, size: .m)
  182. exportVC?.stringValue = NSLocalizedString("Export", comment: "")
  183. exportVC?.updateUI()
  184. self.noTitleLabel.textColor = NSColor.km_init(hex: "#616469")
  185. self.noTitleLabel.font = NSFont.SFProTextRegularFont(14)
  186. self.noImageView.image = NSImage(named: "No_addimage")
  187. let string = self.noSubtitleLabel.stringValue
  188. let paragraphStyle = NSMutableParagraphStyle()
  189. paragraphStyle.lineHeightMultiple = 1.32
  190. paragraphStyle.alignment = .center
  191. self.noSubtitleLabel.attributedStringValue = NSMutableAttributedString(string: NSLocalizedString(string, comment: ""), attributes: [NSAttributedString.Key.paragraphStyle: paragraphStyle, .foregroundColor : NSColor.km_init(hex: "#94989C"), NSAttributedString.Key.font: NSFont.SFProTextRegularFont(12.0)])
  192. self.noBackgroundView.backgroundColor(NSColor.km_init(hex: "#F7F8FA"))
  193. }
  194. func updateLanguage() {
  195. let areas = self.editingAreas
  196. if imagesAreas.count > 0 && imagesAreas.count != areas.count { //多选图片跟文字
  197. self.titleLabel.stringValue = NSLocalizedString("General Properties", comment: "")
  198. } else {
  199. self.titleLabel.stringValue = NSLocalizedString("Image", comment: "")
  200. }
  201. self.opacityTitleLabel.stringValue = NSLocalizedString("Opacity", comment: "")
  202. self.cropVC?.stringValue = NSLocalizedString("Crop", comment: "")
  203. self.confirmVC?.stringValue = NSLocalizedString("Confirm Crop", comment: "")
  204. self.cancelVC?.stringValue = NSLocalizedString("Cancel", comment: "")
  205. self.replaceVC?.stringValue = NSLocalizedString("Replace", comment: "")
  206. self.exportVC?.stringValue = NSLocalizedString("Export", comment: "")
  207. }
  208. func reloadData() {
  209. imagesAreas = []
  210. if self.editingAreas.count > 0 {
  211. self.noBackgroundView.isHidden = true
  212. let areas = self.editingAreas
  213. self.alignmentView.editingAreas = areas
  214. for i in 0 ... areas.count-1 {
  215. if areas[i] is CPDFEditImageArea {
  216. imagesAreas.append(areas[i] as! CPDFEditImageArea)
  217. }
  218. }
  219. if imagesAreas.count == 1 && imagesAreas.count == areas.count{ //单个图片
  220. self.listView.selectImageAreas = imagesAreas.first
  221. self.headerBox.isHidden = false
  222. self.headerBoxHeight.constant = 176
  223. self.headerBoxMaginWidthConstraint.constant = 8
  224. self.imageBox.isHidden = false
  225. self.imageBoxHeight.constant = 88
  226. self.opacityBox.isHidden = false
  227. self.opacityBoxHeight.constant = 56
  228. self.opacityBoxBottomMaginConstraint.constant = 16
  229. self.opacityBoxTopConstraint.constant = 16
  230. self.buttonBox.isHidden = false
  231. // self.cropBox.isHidden = false
  232. // self.cancelBox.isHidden = false
  233. // self.replaceBox.isHidden = false
  234. self.buttonBoxHeight.constant = 112
  235. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  236. self.alignmentViewTopConstraint.constant = 16
  237. let opacity: CGFloat = self.listView.opacity(for: imagesAreas.first)
  238. self.updateImageAreasOpacity(opacity: opacity, state: .ended, needListView: false)
  239. } else if imagesAreas.count > 1 && imagesAreas.count == areas.count { //多选图片
  240. self.headerBox.isHidden = false
  241. self.headerBoxHeight.constant = 82
  242. self.headerBoxMaginWidthConstraint.constant = 0
  243. self.imageBox.isHidden = true
  244. self.imageBoxHeight.constant = 0
  245. self.opacityBox.isHidden = false
  246. self.opacityBoxHeight.constant = 56
  247. self.opacityBoxBottomMaginConstraint.constant = 0
  248. self.opacityBoxTopConstraint.constant = 0
  249. self.buttonBox.isHidden = false
  250. // self.cropBox.isHidden = true
  251. // self.cancelBox.isHidden = true
  252. // self.replaceBox.isHidden = true
  253. self.buttonBoxHeight.constant = 0
  254. self.alignmentViewTopConstraint.constant = 16
  255. var opacity = self.listView.opacity(for: imagesAreas.first)
  256. for area in imagesAreas {
  257. let newOpacity = self.listView.opacity(for: area)
  258. if (opacity < newOpacity) {
  259. opacity = newOpacity
  260. }
  261. }
  262. self.updateImageAreasOpacity(opacity: opacity, state: .ended, needListView: false)
  263. } else if imagesAreas.count > 0 && imagesAreas.count != areas.count { //多选图片跟文字
  264. self.opacityBoxBottomMaginConstraint.constant = 0
  265. self.headerBoxMaginWidthConstraint.constant = 0
  266. self.alignmentViewTopConstraint.constant = 0
  267. self.opacityBoxTopConstraint.constant = 0
  268. // self.headerBox.isHidden = true
  269. self.headerBoxHeight.constant = 48
  270. self.imageBox.isHidden = true
  271. self.imageBoxHeight.constant = 0
  272. self.opacityBox.isHidden = true
  273. self.opacityBoxHeight.constant = 0
  274. self.buttonBox.isHidden = true
  275. self.buttonBoxHeight.constant = 0
  276. }
  277. self.updateLanguage()
  278. } else {
  279. // self.updateButtonState(hidden: true)
  280. // self.editImageView.image = NSImage()
  281. self.noBackgroundView.isHidden = false
  282. }
  283. }
  284. func updateButtonState(hidden: Bool) {
  285. if hidden {
  286. self.cancelBox.isHidden = true
  287. self.confirmBox.isHidden = true
  288. self.cropBox.isHidden = false
  289. self.replaceVC?.enabled = true
  290. self.exportVC?.enabled = true
  291. self.opacitySlider.isEnabled = true
  292. self.opacityVC?.enabled = true
  293. self.rotateLeftVC?.button.isEnabled = true
  294. self.rotateLeftVC?.view.alphaValue = 1.0
  295. self.rotateRightVC?.button.isEnabled = true
  296. self.rotateRightVC?.view.alphaValue = 1.0
  297. self.flipVerticalVC?.button.isEnabled = true
  298. self.flipVerticalVC?.view.alphaValue = 1.0
  299. self.flipHorizontalVC?.button.isEnabled = true
  300. self.flipHorizontalVC?.view.alphaValue = 1.0
  301. } else {
  302. self.cancelBox.isHidden = false
  303. self.confirmBox.isHidden = false
  304. self.cropBox.isHidden = true
  305. self.replaceVC?.enabled = false
  306. self.exportVC?.enabled = false
  307. self.opacitySlider.isEnabled = false
  308. self.opacityVC?.enabled = false
  309. self.rotateLeftVC?.button.isEnabled = false
  310. self.rotateLeftVC?.view.alphaValue = 0.5
  311. self.rotateRightVC?.button.isEnabled = false
  312. self.rotateRightVC?.view.alphaValue = 0.5
  313. self.flipVerticalVC?.button.isEnabled = false
  314. self.flipVerticalVC?.view.alphaValue = 0.5
  315. self.flipHorizontalVC?.button.isEnabled = false
  316. self.flipHorizontalVC?.view.alphaValue = 0.5
  317. }
  318. self.reloadData()
  319. }
  320. func updateImageAreasOpacity(opacity: CGFloat, state: KMOperationState, needListView: Bool = true) {
  321. if self.editingAreas.count >= 1 {
  322. if needListView {
  323. for area in imagesAreas {
  324. self.listView.setOpacityEditArea(area, opacity: opacity)
  325. }
  326. }
  327. // self.listView.editingAreas()!.count == 1 &&
  328. if (self.listView.editingAreas()?.first is CPDFEditImageArea) {
  329. self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  330. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  331. self.opacitySlider.objectValue = self.listView.opacity(for: self.listView.selectImageAreas)
  332. self.opacityVC?.stringValue = Int(opacity * 100).description + "%"
  333. self.editImageView.alphaValue = opacity
  334. }
  335. }
  336. }
  337. private func changeAreasAlign(areas:[Any],newBounds:[String]) {
  338. var oldBounds : [String] = []
  339. for i in 0 ... areas.count-1 {
  340. let area : CPDFEditArea = areas[i] as! CPDFEditArea
  341. oldBounds.append(NSStringFromRect(area.bounds))
  342. self.listView.setBoundsEditArea(area, withBounds: NSRectFromString(newBounds[i]))
  343. }
  344. self.listView.setNeedsDisplayForVisiblePages()
  345. }
  346. }
  347. extension KMEditImagePropertyViewController: KMSelectPopButtonDelegate {
  348. func km_comboBoxSelectionDidChange(_ obj: KMDesignSelect) {
  349. if obj == opacityVC {
  350. KMPrint("km_comboBoxSelectionDidChange")
  351. let index = obj.indexOfSelectedItem
  352. var string = obj.items[index]
  353. string = string.replacingOccurrences(of: "%", with: "")
  354. let value = CGFloat(Float(string)! * 0.01)
  355. self.updateImageAreasOpacity(opacity: value, state: .changed)
  356. }
  357. }
  358. func km_controlTextDidEndEditing(_ obj: KMDesignSelect) {
  359. if obj == opacityVC {
  360. KMPrint("km_comboBoxSelectionDidChange")
  361. let index = obj.indexOfSelectedItem
  362. var string = obj.items[index]
  363. string = string.replacingOccurrences(of: "%", with: "")
  364. let value = CGFloat(Float(string)! * 0.01)
  365. self.updateImageAreasOpacity(opacity: value, state: .ended)
  366. }
  367. }
  368. }
  369. //MARK: - Action
  370. extension KMEditImagePropertyViewController {
  371. @objc func sliderValueChanged(_ sender: NSSlider) {
  372. let sliderValue = sender.floatValue
  373. self.updateImageAreasOpacity(opacity:CGFloat(sliderValue), state: .changed)
  374. }
  375. @IBAction func rightRotationImageAction(_ sender: Any) {
  376. if self.listView.editingAreas()?.count ?? 0 < 1 {
  377. return
  378. }
  379. let areas = self.listView.editingAreas()
  380. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  381. let imageArea = areas!.first as! CPDFEditImageArea
  382. self.listView.rotate(with: imageArea, rotate: 90)
  383. self.listView.selectImageAreas = imageArea
  384. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  385. } else if areas!.count > 1 {
  386. for imagesArea in imagesAreas {
  387. self.listView.rotate(with: imagesArea, rotate: 90)
  388. }
  389. }
  390. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  391. }
  392. @IBAction func leftRotationImageAction(_ sender: Any) {
  393. if self.listView.editingAreas()?.count ?? 0 < 1 {
  394. return
  395. }
  396. let areas = self.listView.editingAreas()
  397. if areas!.count == 1 && (areas!.first is CPDFEditImageArea) {
  398. self.listView.rotate(with: self.listView.selectImageAreas, rotate: -90)
  399. if self.listView.editingAreas()!.count == 1 && (self.listView.editingAreas()!.first is CPDFEditImageArea) {
  400. self.listView.selectImageAreas = self.listView.editingAreas()!.first as? CPDFEditImageArea
  401. }
  402. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  403. } else {
  404. for imagesArea in imagesAreas {
  405. self.listView.rotate(with: imagesArea, rotate: -90)
  406. }
  407. }
  408. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .rotate)
  409. }
  410. @IBAction func flipHorizontalImageAction(_ sender: Any) {
  411. if self.listView.editingAreas()?.count ?? 0 < 1 {
  412. return
  413. }
  414. let areas = self.listView.editingAreas()
  415. if areas != nil {
  416. for item in areas! {
  417. if item is CPDFEditImageArea {
  418. self.listView.horizontalMirror(with: item as? CPDFEditImageArea)
  419. if areas?.count == 1 {
  420. self.editImageView.image = (item as AnyObject).thumbnailImage
  421. }
  422. } else {
  423. for imagesArea in imagesAreas {
  424. self.listView.horizontalMirror(with: imagesArea)
  425. }
  426. }
  427. }
  428. }
  429. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .flip)
  430. }
  431. @IBAction func flipVerticalImageAction(_ sender: Any) {
  432. if self.listView.editingAreas()?.count ?? 0 < 1 {
  433. return
  434. }
  435. let areas = self.listView.editingAreas()
  436. if areas != nil {
  437. for item in areas! {
  438. if item is CPDFEditImageArea {
  439. self.listView.verticalMirror(with: item as? CPDFEditImageArea)
  440. self.editImageView.image = self.listView.selectImageAreas.thumbnailImage
  441. } else {
  442. for imagesArea in imagesAreas {
  443. self.listView.verticalMirror(with: imagesArea)
  444. }
  445. }
  446. }
  447. }
  448. self.delegate?.editImagePropertyViewControllerDidChanged(controller: self, type: .flip)
  449. }
  450. @IBAction func cutImageAction(_ sender: Any) {
  451. if self.listView.selectImageAreas == nil {
  452. return
  453. }
  454. self.listView.isEditImage = true
  455. self.listView.enterCrop(with: self.listView.selectImageAreas)
  456. self.updateButtonState(hidden: false)
  457. }
  458. @IBAction func confirmVCImageAction(_ sender: Any) {
  459. if self.listView.selectImageAreas == nil || self.listView.cropAreas == nil {
  460. return
  461. }
  462. self.listView.cropEditImageArea(self.listView.selectImageAreas, withBounds: self.listView.cropAreas.cropRect)
  463. self.cancelCutImageAction(cancelVC?.button as Any)
  464. }
  465. @IBAction func cancelCutImageAction(_ sender: Any) {
  466. if self.listView.selectImageAreas == nil {
  467. return
  468. }
  469. self.listView.exitCrop(with: self.listView.selectImageAreas)
  470. self.listView.cropAreas = nil
  471. self.listView.isEditImage = false
  472. self.updateButtonState(hidden: true)
  473. }
  474. @IBAction func restoreCutImageAction(_ sender: Any) {
  475. if self.listView.selectImageAreas == nil {
  476. return
  477. }
  478. self.listView.resetCrop(with: self.listView.selectImageAreas)
  479. }
  480. @IBAction func replaceImageAction(_ sender: NSButton) {
  481. if self.listView.selectImageAreas == nil {
  482. return
  483. }
  484. let panel = NSOpenPanel()
  485. panel.allowsMultipleSelection = false
  486. panel.allowedFileTypes = ["png","jpg"]
  487. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  488. if response == .OK {
  489. let openPath = panel.url?.path
  490. // let s = self.listView.extractImage(with: self.selectImageAreas, toImagePath: openPath!)
  491. let s = self.listView.replace(self.listView.selectImageAreas, imagePath: openPath!)
  492. if s {
  493. }
  494. }
  495. }
  496. }
  497. // @available(macOS 13.0, *)
  498. @IBAction func exportImageAction(_ sender: Any) {
  499. if self.listView.selectImageAreas == nil {
  500. return
  501. }
  502. if imagesAreas.count == 1 {
  503. let panel = NSSavePanel()
  504. panel.nameFieldStringValue = "\(NSLocalizedString("Untitled", comment: "")).jpg"
  505. // let button = NSButton.init(checkboxWithTitle: NSLocalizedString("Open the document after saving", comment: ""), target: nil, action: nil)
  506. // button.state = .on
  507. // panel.accessoryView = button
  508. panel.isExtensionHidden = true
  509. let response = panel.runModal()
  510. if response == .OK {
  511. let url = panel.url
  512. if FileManager.default.fileExists(atPath: url!.path) {
  513. try?FileManager.default.removeItem(atPath: url!.path)
  514. }
  515. let result = self.listView.extractImage(with: self.listView.selectImageAreas, toImagePath: url!.path)
  516. if result {
  517. // if button.state == .on { /// 开启文档
  518. // NSWorkspace.shared.openFile(url!.path)
  519. NSWorkspace.shared.activateFileViewerSelecting([url!])
  520. // } else {
  521. //
  522. // }
  523. }
  524. }
  525. } else if imagesAreas.count > 1 {
  526. let panel = NSOpenPanel()
  527. panel.canChooseFiles = false
  528. panel.canChooseDirectories = true
  529. panel.canCreateDirectories = true
  530. panel.allowsMultipleSelection = false
  531. panel.beginSheetModal(for: NSApp.mainWindow!) { response in
  532. if response == .OK {
  533. let outputURL = panel.url
  534. let s = self.listView.document.documentURL.lastPathComponent
  535. let folderPath = self.listView.document.documentURL.deletingPathExtension().lastPathComponent + "_extract"
  536. var filePath = outputURL?.path.stringByAppendingPathComponent(folderPath)
  537. var i = 1
  538. let testFilePath = filePath
  539. while FileManager.default.fileExists(atPath: filePath!) {
  540. filePath = testFilePath! + "\(i)"
  541. i = i + 1
  542. }
  543. try? FileManager.default.createDirectory(atPath: filePath!, withIntermediateDirectories: false, attributes: nil)
  544. var saveURLs : [URL] = []
  545. for j in 0 ... self.imagesAreas.count-1 {
  546. let documentFileName = self.listView.document.documentURL.deletingPathExtension().lastPathComponent
  547. var outPath = filePath!
  548. outPath = outPath.stringByAppendingPathComponent(documentFileName)
  549. outPath = outPath + "page \(j+1)"
  550. outPath = outPath.stringByAppendingPathExtension("jpg")
  551. let result = self.listView.extractImage(with: self.imagesAreas[j], toImagePath: outPath)
  552. if result {
  553. saveURLs.append(URL(fileURLWithPath: outPath))
  554. }
  555. }
  556. NSWorkspace.shared.activateFileViewerSelecting(saveURLs)
  557. }
  558. }
  559. }
  560. }
  561. }
  562. class CustomSliderCell: NSSliderCell {
  563. var knobImage: NSImage?
  564. override func drawKnob(_ knobRect: NSRect) {
  565. if let image = knobImage {
  566. let imageSize = NSSize(width: knobRect.size.height, height: knobRect.size.height)
  567. let imageRect = NSRect(origin: knobRect.origin, size: imageSize)
  568. image.draw(in: imageRect)
  569. } else {
  570. super.drawKnob(knobRect)
  571. }
  572. }
  573. }