KMBatchOperateConvertViewController.swift 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  1. //
  2. // KMBatchOperateConvertViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by kdanmobile on 2023/11/1.
  6. //
  7. import Cocoa
  8. typealias detailInfoViewMouseDownCallback = (_ mouseDown: Bool) -> ()
  9. class KMDetailInfoView: NSView{
  10. var mouseDownCallback: detailInfoViewMouseDownCallback?
  11. override func mouseDown(with event: NSEvent) {
  12. super.mouseDown(with: event)
  13. guard let callBack = mouseDownCallback else { return }
  14. callBack(true)
  15. }
  16. }
  17. class KMBatchOperateConvertViewController: KMBatchOperateBaseViewController,NSCollectionViewDelegate,NSCollectionViewDataSource{
  18. var dataSourcesArray: [KMConvertWithPDFType]?
  19. @IBOutlet var collectionView: NSCollectionView!
  20. @IBOutlet var bottomView: NSView!
  21. @IBOutlet var convertEveryPageButton: NSButton!
  22. @IBOutlet var convertButton: NSButton!
  23. @IBOutlet var detailInfoView: KMDetailInfoView!
  24. @IBOutlet var detailInfoLabel: NSTextField!
  25. @IBOutlet var checkBoxTopConstraint: NSLayoutConstraint!
  26. @IBOutlet var containerViewTopConstraint: NSLayoutConstraint!
  27. @IBOutlet var checkBoxBottomConstaint: NSLayoutConstraint!
  28. @IBOutlet var convertEveryPageButtonLabel: NSTextField!
  29. @IBOutlet var extractButton: NSButton!
  30. @IBOutlet var wordButtonOne: NSButton!
  31. @IBOutlet var wordButtonTwo: NSButton!
  32. @IBOutlet var csvExtractButton: NSButton!
  33. @IBOutlet var onlyTextBtn: NSButton!
  34. @IBOutlet var onlyTableBtn: NSButton!
  35. @IBOutlet var allContentBtn: NSButton!
  36. @IBOutlet var tableMenu1: NSMenuItem!
  37. @IBOutlet var tableMenu2: NSMenuItem!
  38. @IBOutlet var tableMenu3: NSMenuItem!
  39. @IBOutlet var allContentMenu2: NSMenuItem!
  40. @IBOutlet var allContentMenu3: NSMenuItem!
  41. @IBOutlet var tableMenu: NSPopUpButton!
  42. @IBOutlet var allContentMenu: NSPopUpButton!
  43. var excelContentOption: CPDFConvertExcelContentOptions?
  44. var excelWorksheetOption: CPDFConvertExcelWorksheetOptions?
  45. var haveFiles: Bool = false
  46. deinit {
  47. NotificationCenter.default.removeObserver(self)
  48. }
  49. override var interfaceStatus: KMBatchOperateInterfaceStatus?{
  50. set{
  51. super.interfaceStatus = newValue
  52. self.convertButton.isEnabled = true
  53. if newValue == .Processing {
  54. self.collectionView.isSelectable = false
  55. self.convertButton.title = NSLocalizedString("Cancel", comment: "")
  56. self.convertButton.tag = 2
  57. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  58. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  59. if self.convertType == .WordStandard {
  60. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  61. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  62. self.convertButton.isEnabled = false
  63. }
  64. } else {
  65. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  66. var files: [URL] = []
  67. for url in self.successFilePathURLArray! {
  68. if FileManager.default.fileExists(atPath: url.path) {
  69. files.append(url)
  70. }
  71. }
  72. if files.count > 0 {
  73. let workspace = NSWorkspace.shared
  74. workspace.activateFileViewerSelecting(files)
  75. }
  76. }
  77. self.collectionView.isSelectable = true
  78. self.convertButton.title = NSLocalizedString("Convert", comment: "")
  79. self.convertButton.tag = 1
  80. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color())
  81. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  82. }
  83. }
  84. get{
  85. return super.interfaceStatus
  86. }
  87. }
  88. override var convertType: KMConvertWithPDFType?{
  89. set{
  90. super.convertType = newValue
  91. changeAllFilesToConvertType(convertType!)
  92. fetchFileListViewController()?.reloadConvertInterface(withType: convertType!)
  93. }
  94. get{
  95. return super.convertType
  96. }
  97. }
  98. func changeAllFilesToConvertType(_ type: KMConvertWithPDFType) {
  99. for i in 0..<self.files!.count {
  100. let file = self.files![i]
  101. file.convertType = type
  102. }
  103. }
  104. func switchToConvertType(_ convertType: KMConvertWithPDFType) {
  105. self.convertType = convertType
  106. self.manualSelectRow(convertType)
  107. self.updateBottomView()
  108. }
  109. func manualSelectRow(_ convertType: KMConvertWithPDFType) {
  110. self.collectionView.deselectAll(nil)
  111. self.collectionView.reloadData()
  112. var set = Set<IndexPath>()
  113. var index = -1
  114. for i in 0..<self.dataSourcesArray!.count {
  115. let number = self.dataSourcesArray![i]
  116. if number.rawValue == convertType.rawValue {
  117. index = i
  118. break
  119. }
  120. }
  121. if index != -1 {
  122. let indexPath = IndexPath(item: index, section: 0)
  123. set.insert(indexPath)
  124. self.collectionView.selectItems(at: set, scrollPosition: [])
  125. let selectionRect = self.collectionView.frameForItem(at: self.collectionView.selectionIndexes.first ?? 0)
  126. self.collectionView.scrollToVisible(selectionRect)
  127. }
  128. }
  129. func updateBottomView() {
  130. var moreLabelString = ""
  131. self.onlyTextBtn.isHidden = true
  132. self.onlyTableBtn.isHidden = true
  133. self.allContentBtn.isHidden = true
  134. self.tableMenu.isHidden = true
  135. self.allContentMenu.isHidden = true
  136. #if VERSION_FREE
  137. if .WordStandard == self.convertType {
  138. moreLabelString = NSLocalizedString("Export PDF to Word and other Microsoft Office formats", comment: "")
  139. } else if .WordAdvance == self.convertType ||
  140. .Excel == self.convertType ||
  141. .PowerPoint == self.convertType ||
  142. .CSV == self.convertType ||
  143. .RTF == self.convertType ||
  144. .GIF == self.convertType ||
  145. .TIFF == self.convertType ||
  146. .JPEG2000 == self.convertType ||
  147. .BMP == self.convertType ||
  148. .TGA == self.convertType {
  149. moreLabelString = NSLocalizedString("The first 10 pages for free. More precise one.", comment: "")
  150. }
  151. #else
  152. // 付费版
  153. if .WordStandard == self.convertType {
  154. moreLabelString = NSLocalizedString("Export PDF to Word and other Microsoft Office formats", comment: "")
  155. } else if .WordAdvance == self.convertType ||
  156. .Excel == self.convertType ||
  157. .PowerPoint == self.convertType ||
  158. .CSV == self.convertType ||
  159. .RTF == self.convertType ||
  160. .GIF == self.convertType ||
  161. .TIFF == self.convertType ||
  162. .JPEG2000 == self.convertType ||
  163. .BMP == self.convertType ||
  164. .TGA == self.convertType {
  165. moreLabelString = NSLocalizedString("The first 10 pages for free. More precise one.", comment: "")
  166. }
  167. #endif
  168. var hasInfo = false
  169. var isExcel = false
  170. self.detailInfoLabel.stringValue = moreLabelString
  171. if moreLabelString.count > 0 {
  172. self.detailInfoView.isHidden = false
  173. self.containerViewTopConstraint.constant = 16
  174. hasInfo = true
  175. } else {
  176. self.detailInfoView.isHidden = true
  177. self.containerViewTopConstraint.constant = 11
  178. }
  179. if self.convertType == .WordAdvance {
  180. isExcel = true
  181. self.convertEveryPageButton.isHidden = true
  182. self.wordButtonOne.isHidden = false
  183. self.wordButtonOne.title = NSLocalizedString("Retain Flowing Text", comment: "")
  184. self.convertEveryPageButtonLabel.isHidden = true
  185. self.extractButton.isHidden = true
  186. self.csvExtractButton.isHidden = true
  187. self.wordButtonTwo.isHidden = false
  188. self.wordButtonTwo.title = NSLocalizedString("Retain Page Layout", comment: "")
  189. self.checkBoxBottomConstaint.constant = 15 + self.wordButtonTwo.frame.size.height + self.wordButtonOne.frame.size.height
  190. self.checkBoxTopConstraint.constant = 5
  191. } else if self.convertType == .Excel {
  192. isExcel = true
  193. self.convertEveryPageButtonLabel.stringValue = NSLocalizedString("Convert each page to a separate worksheet", comment: "")
  194. self.onlyTextBtn.isHidden = false
  195. self.onlyTableBtn.isHidden = false
  196. self.allContentBtn.isHidden = false
  197. self.tableMenu.isHidden = false
  198. self.allContentMenu.isHidden = false
  199. self.convertEveryPageButtonLabel.isHidden = true
  200. self.convertEveryPageButton.isHidden = true
  201. self.wordButtonOne.isHidden = true
  202. self.extractButton.isHidden = true
  203. self.wordButtonTwo.isHidden = true
  204. self.csvExtractButton.isHidden = true
  205. self.extractButton.title = NSLocalizedString("Extract Tables Only", comment: "")
  206. var oneHeight = self.convertEveryPageButtonLabel.frame.size.height
  207. let language = Bundle.main.preferredLocalizations[0]
  208. if language != "zh_CN" && language != "zh_TW" {
  209. oneHeight = 34.0
  210. }
  211. self.checkBoxBottomConstaint.constant = 83
  212. self.checkBoxTopConstraint.constant = 5
  213. } else if self.convertType == .CSV {
  214. isExcel = true
  215. self.convertEveryPageButtonLabel.isHidden = true
  216. self.convertEveryPageButton.isHidden = true
  217. self.extractButton.isHidden = true
  218. self.wordButtonTwo.isHidden = true
  219. self.wordButtonOne.isHidden = true
  220. self.csvExtractButton.isHidden = false
  221. self.csvExtractButton.title = NSLocalizedString("Extract Tables Only", comment: "")
  222. self.checkBoxBottomConstaint.constant = 15 + self.csvExtractButton.frame.size.height
  223. self.checkBoxTopConstraint.constant = 5
  224. } else {
  225. self.convertEveryPageButton.isHidden = true
  226. self.extractButton.isHidden = true
  227. self.wordButtonTwo.isHidden = true
  228. self.wordButtonOne.isHidden = true
  229. self.csvExtractButton.isHidden = true
  230. self.convertEveryPageButtonLabel.stringValue = NSLocalizedString("", comment: "")
  231. self.checkBoxTopConstraint.constant = -10
  232. self.checkBoxBottomConstaint.constant = 5
  233. }
  234. if !hasInfo && !isExcel {
  235. self.containerViewTopConstraint.constant = 0
  236. self.checkBoxTopConstraint.constant = 0
  237. self.checkBoxBottomConstaint.constant = 1
  238. }
  239. }
  240. override func viewDidLoad() {
  241. super.viewDidLoad()
  242. self.prepareData()
  243. self.view.wantsLayer = true
  244. collectionView.allowsMultipleSelection = false
  245. collectionView.enclosingScrollView?.borderType = .noBorder
  246. collectionView.allowsEmptySelection = false
  247. convertButton.wantsLayer = true
  248. convertButton.font = NSFont.systemFont(ofSize: 13)
  249. convertButton.layer?.cornerRadius = 1.0
  250. interfaceStatus = .PrepareProcess
  251. convertEveryPageButton.title = NSLocalizedString("", comment: "")
  252. // Convert each page to a separate worksheet
  253. convertEveryPageButtonLabel.stringValue = NSLocalizedString("Convert each page to a separate worksheet", comment: "")
  254. self.collectionView.register(KMConvertCollectionViewHeader.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMAdvertisementCollectionViewItem"))
  255. self.collectionView.register(KMBatchoperateConvertCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchoperateConvertCollectionViewItem"))
  256. self.collectionView.register(KMConvertCollectionViewHeader.self, forSupplementaryViewOfKind: NSCollectionView.elementKindSectionHeader, withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "convertHeader"))
  257. collectionView.enclosingScrollView?.drawsBackground = false
  258. let v = NSView(frame: NSRect(x: 0, y: 0, width: 100, height: 100))
  259. v.wantsLayer = true
  260. v.layer?.backgroundColor = KMAppearance.Layout.l0Color().cgColor
  261. collectionView.backgroundView = v
  262. collectionView.enclosingScrollView?.horizontalScrollElasticity = .none
  263. collectionView.enclosingScrollView?.verticalScrollElasticity = .none
  264. allContentBtn.state = .on
  265. allContentMenu.isEnabled = true
  266. tableMenu.isEnabled = false
  267. excelContentOption = .allContent
  268. excelWorksheetOption = .forEachPage
  269. onlyTextBtn.title = NSLocalizedString("Only Text", comment: "")
  270. onlyTableBtn.title = NSLocalizedString("Only Table", comment: "")
  271. allContentBtn.title = NSLocalizedString("All Content", comment: "")
  272. onlyTextBtn.toolTip = NSLocalizedString("Only Text", comment: "")
  273. onlyTableBtn.toolTip = NSLocalizedString("Only Table", comment: "")
  274. allContentBtn.toolTip = NSLocalizedString("All Content", comment: "")
  275. tableMenu1.title = NSLocalizedString("Create Sheet for each Table", comment: "")
  276. tableMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  277. allContentMenu2.title = NSLocalizedString("Create Sheet for each Page", comment: "")
  278. tableMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  279. allContentMenu3.title = NSLocalizedString("Create single Sheet for File", comment: "")
  280. self.detailInfoView.mouseDownCallback = { [weak self] (downEntered: Bool) in
  281. if downEntered {
  282. // if (![IAPProductsManager defaultManager].isAvailableAdvancedPDFToOffice) {
  283. self?.convertActionVC()
  284. // }
  285. }
  286. }
  287. self.updateViewColor()
  288. NotificationCenter.default.addObserver(self, selector: #selector(batchFilesCountNotification(notification:)), name: Notification.Name(rawValue: "KMBatchFilesCountNotification"), object: nil)
  289. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductPurchasedNotification(notification:)), name: NSNotification.Name("KMIAPProductPurchasedNotification"), object: nil)
  290. NotificationCenter.default.addObserver(self, selector: #selector(IAPProductRestoreFinishedNotification(notification:)), name: NSNotification.Name("KMIAPProductRestoreFinishedNotification"), object: nil)
  291. // NotificationCenter.default.addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  292. DistributedNotificationCenter.default().addObserver(self, selector: #selector(themeChanged(notification:)), name: NSNotification.Name("AppleInterfaceThemeChangedNotification"), object: nil)
  293. }
  294. @objc func batchFilesCountNotification(notification: NSNotification) {
  295. let arr: Array? = notification.object as? [KMBatchOperateFile]
  296. self.files? = arr ?? []
  297. if files!.count > 0 {
  298. self.convertButton.setTitleColor(NSColor.white)
  299. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().cgColor
  300. self.haveFiles = true
  301. } else {
  302. self.convertButton.setTitleColor(KMAppearance.Layout.w0Color().withAlphaComponent(0.6))
  303. self.convertButton.layer?.backgroundColor = KMAppearance.Interactive.m0Color().withAlphaComponent(0.4).cgColor
  304. self.haveFiles = false
  305. }
  306. }
  307. @objc func themeChanged(notification: NSNotification) {
  308. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  309. self.updateViewColor()
  310. }
  311. }
  312. @objc func IAPProductRestoreFinishedNotification(notification: NSNotification) {
  313. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  314. self.updateBottomView()
  315. self.prepareData()
  316. self.collectionView.reloadData()
  317. }
  318. }
  319. @objc func IAPProductPurchasedNotification(notification: NSNotification) {
  320. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  321. self.updateBottomView()
  322. self.prepareData()
  323. self.collectionView.reloadData()
  324. }
  325. }
  326. func updateViewColor() {
  327. self.view.wantsLayer = true
  328. if KMAppearance.isDarkMode() {
  329. self.view.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  330. self.collectionView.backgroundView?.layer?.backgroundColor = NSColor(red: 0.055, green: 0.067, blue: 0.078, alpha: 1).cgColor
  331. } else {
  332. self.view.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  333. self.collectionView.backgroundView?.layer?.backgroundColor = NSColor(red: 0.922, green: 0.925, blue: 0.941, alpha: 1).cgColor
  334. }
  335. }
  336. func convertActionVC() {
  337. var vc: KMToolCompareWindowController? = nil
  338. if(.WordStandard == self.convertType ||
  339. .WordAdvance == self.convertType) {
  340. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 1)
  341. } else if (.Excel == self.convertType) {
  342. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 2)
  343. } else if (.PowerPoint == self.convertType) {
  344. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 3)
  345. } else if (self.convertType == .JPG || self.convertType == .PNG || self.convertType == .GIF || self.convertType == .TIFF || self.convertType == .TGA || self.convertType == .BMP){
  346. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 4)
  347. } else {
  348. vc = KMToolCompareWindowController(toolType: .Convert, selectNum: 0)
  349. }
  350. vc?.showWindow(nil)
  351. }
  352. func prepareData() {
  353. let arr: [KMConvertWithPDFType] = [.Excel, .PowerPoint, .RTF, .CSV, .HTML, .Text, .JPEG, .JPG, .PNG, .GIF, .TIFF, .TGA, .BMP, .JPEG2000]
  354. self.dataSourcesArray = arr
  355. var needShowAdvance = true
  356. var needShowDefault = true
  357. #if VERSION_FREE
  358. // 桌机版
  359. // if IAPProductsManager.defaultManager().isAvailableAllFunction {
  360. // if IAPProductsManager.defaultManager().isAvailableAdvancedPDFToOffice {
  361. // needShowAdvance = true
  362. // needShowDefault = false
  363. // }
  364. // }
  365. #else
  366. // if IAPProductsManager.defaultManager().isAvailableAdvancedPDFToOffice {
  367. // needShowAdvance = true
  368. // needShowDefault = false
  369. // }
  370. #endif
  371. if needShowDefault {
  372. var wordType: KMConvertWithPDFType = .WordStandard
  373. self.dataSourcesArray?.insert(wordType, at: 0)
  374. }
  375. if needShowAdvance {
  376. var wordType: KMConvertWithPDFType = .WordAdvance
  377. self.dataSourcesArray?.insert(wordType, at: 0)
  378. }
  379. }
  380. @IBAction func buttonClicked_Convert(_ sender: NSButton) {
  381. if !self.haveFiles { return }
  382. self.view.window?.makeFirstResponder(nil)
  383. for i in 0..<self.files!.count {
  384. let file = self.files?[i]
  385. file?.excelParameter.allInOneSheet = (self.convertEveryPageButton.state == .on) ? false : true
  386. file?.advanceWordParameter.isRetainLayout = (self.wordButtonTwo.state == .on) ? true : false
  387. file?.excelParameter.isExtreactTabel = (self.extractButton.state == .on) ? true : false
  388. file?.CSVParameter.isExtreactTabel = (self.csvExtractButton.state == .on) ? true : false
  389. file?.excelParameter.excelContentOption = self.excelContentOption
  390. file?.excelParameter.excelWorksheetOption = self.excelWorksheetOption
  391. }
  392. if sender.tag == 1 {
  393. self.beginBatchOperation()
  394. } else {
  395. self.cancelBatchOperation()
  396. }
  397. }
  398. @IBAction func moreButtonAction(_ sender: NSButton) {
  399. self.convertActionVC()
  400. self.buttonClicked_Cancel("")
  401. }
  402. @IBAction func layoutButtonAction(_ sender: Any) {
  403. }
  404. @IBAction func exctractTableButtonAction(_ sender: Any) {
  405. if extractButton.state == .on {
  406. convertEveryPageButton.state = .off
  407. convertEveryPageButtonLabel.isEnabled = false
  408. convertEveryPageButton.isEnabled = false
  409. convertEveryPageButtonLabel.textColor = KMAppearance.Layout.b15_1Color()
  410. } else {
  411. convertEveryPageButton.state = .on
  412. convertEveryPageButtonLabel.isEnabled = true
  413. convertEveryPageButton.isEnabled = true
  414. convertEveryPageButtonLabel.textColor = NSColor.labelColor
  415. }
  416. }
  417. @IBAction func buttonClicked_Cancel(_ sender: Any) {
  418. let basePath = NSSearchPathForDirectoriesInDomains(.applicationSupportDirectory, .userDomainMask, true).last ?? ""
  419. let newPath = (basePath as NSString).appendingPathComponent(Bundle.main.bundleIdentifier ?? "")
  420. let filePath = newPath.stringByAppendingPathComponent("convert.pdf")
  421. if FileManager.default.fileExists(atPath: filePath) {
  422. try? FileManager.default.removeItem(atPath: filePath)
  423. }
  424. }
  425. func transform(withString string: String) {
  426. if string == NSLocalizedString("Create Sheet for each Table", comment: "") {
  427. self.excelWorksheetOption = .forEachTable
  428. } else if string == NSLocalizedString("Create Sheet for each Page", comment: "") {
  429. self.excelWorksheetOption = .forEachPage
  430. } else if string == NSLocalizedString("Create single Sheet for File", comment: "") {
  431. self.excelWorksheetOption = .forTheDocument
  432. }
  433. }
  434. @IBAction func buttonClicked_excelStyle(_ sender: NSButton) {
  435. if sender == onlyTextBtn {
  436. onlyTextBtn.state = NSControl.StateValue.on
  437. onlyTableBtn.state = NSControl.StateValue.off
  438. allContentBtn.state = NSControl.StateValue.off
  439. tableMenu.isEnabled = false
  440. allContentMenu.isEnabled = false
  441. excelContentOption = .onlyText
  442. excelWorksheetOption = .forEachTable
  443. } else if sender == onlyTableBtn {
  444. onlyTableBtn.state = NSControl.StateValue.on
  445. onlyTextBtn.state = NSControl.StateValue.off
  446. allContentBtn.state = NSControl.StateValue.off
  447. tableMenu.isEnabled = true
  448. allContentMenu.isEnabled = false
  449. excelContentOption = .onlyTable
  450. transform(withString: tableMenu.selectedItem?.title ?? "")
  451. } else if sender == allContentBtn {
  452. allContentBtn.state = NSControl.StateValue.on
  453. onlyTextBtn.state = NSControl.StateValue.off
  454. onlyTableBtn.state = NSControl.StateValue.off
  455. tableMenu.isEnabled = false
  456. allContentMenu.isEnabled = true
  457. excelContentOption = .allContent
  458. transform(withString: allContentMenu.selectedItem?.title ?? "")
  459. }
  460. }
  461. @IBAction func buttonClicked_tableMenu(_ sender: NSPopUpButton) {
  462. transform(withString: self.tableMenu.selectedItem!.title)
  463. }
  464. @IBAction func buttonClickec_allContentMenu(_ sender: NSButton) {
  465. transform(withString: self.allContentMenu.selectedItem!.title)
  466. }
  467. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  468. return dataSourcesArray!.count
  469. }
  470. func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  471. if let indexPath = indexPaths.first {
  472. convertType = dataSourcesArray![indexPath.item]
  473. updateBottomView()
  474. }
  475. }
  476. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> NSSize {
  477. return NSSize(width: 10000, height: 50)
  478. }
  479. func collectionView(_ collectionView: NSCollectionView, viewForSupplementaryElementOfKind kind: NSCollectionView.SupplementaryElementKind, at indexPath: IndexPath) -> NSView {
  480. let view = collectionView.makeSupplementaryView(ofKind: kind, withIdentifier:NSUserInterfaceItemIdentifier(rawValue: "convertHeader") , for: indexPath) as? KMConvertCollectionViewHeader
  481. view?.convertHeaderClickedCallBack = {
  482. self.convertActionVC()
  483. }
  484. return view ?? NSView()
  485. }
  486. func intConvertType(num: Int) -> KMConvertWithPDFType {
  487. var type: KMConvertWithPDFType?
  488. switch num {
  489. case 0:
  490. type = .WordAdvance
  491. case 1:
  492. type = .WordStandard
  493. case 2:
  494. type = .Excel
  495. case 3:
  496. type = .PowerPoint
  497. case 4:
  498. type = .RTF
  499. case 5:
  500. type = .CSV
  501. case 6:
  502. type = .HTML
  503. case 7:
  504. type = .Text
  505. case 8:
  506. type = .JPEG
  507. case 9:
  508. type = .JPG
  509. case 10:
  510. type = .PNG
  511. case 11:
  512. type = .GIF
  513. case 12:
  514. type = .TIFF
  515. case 13:
  516. type = .TGA
  517. case 14:
  518. type = .BMP
  519. case 15:
  520. type = .JPEG2000
  521. default:
  522. type = .WordAdvance
  523. }
  524. return type!
  525. }
  526. }
  527. extension KMBatchOperateConvertViewController: NSCollectionViewDelegateFlowLayout {
  528. func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  529. let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchoperateConvertCollectionViewItem"), for: indexPath) as! KMBatchoperateConvertCollectionViewItem
  530. item.updateInterface(dataSourcesArray![indexPath.item])
  531. return item
  532. }
  533. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  534. return NSSize(width: 74, height: 80)
  535. }
  536. func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, insetForSectionAt section: Int) -> NSEdgeInsets {
  537. return NSEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
  538. }
  539. }