KMFromViewController.swift 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. //
  2. // KMFromViewController.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by lxy on 2022/10/10.
  6. //
  7. import Cocoa
  8. class KMFromViewController: NSViewController {
  9. @IBOutlet weak var titleLabel: NSTextField!
  10. @IBOutlet weak var formOutilneView: NSOutlineView!
  11. @IBOutlet weak var topBackView: NSView!
  12. var listView : CPDFListView = CPDFListView()
  13. var widgetAnnotations : [KMFormModel] = []
  14. var renameTextField : NSTextField!
  15. var renameFormModel : KMFormModel!
  16. override func viewDidLoad() {
  17. super.viewDidLoad()
  18. self.view.wantsLayer = true
  19. self.view.layer?.backgroundColor = NSColor.gray.cgColor
  20. self.topBackView.wantsLayer = true
  21. self.topBackView.layer?.backgroundColor = NSColor.white.cgColor
  22. self.titleLabel.stringValue = NSLocalizedString("域", comment: "")
  23. self.formOutilneView.doubleAction = #selector(renameWidgetAction)
  24. self.addoutlineMenu()
  25. self.reloadData()
  26. }
  27. public func reloadData() {
  28. self.widgetAnnotations.removeAll()
  29. if self.listView.document == nil {
  30. return
  31. }
  32. self.widgetAnnotations = KMFormModel.getFormAnnotations(document: self.listView.document)
  33. self.formOutilneView.reloadData()
  34. }
  35. func addoutlineMenu() {
  36. let menus : NSMenu = NSMenu(title: "")
  37. let deleteItem = self.menuItemWithTitle(title: NSLocalizedString("Delete", comment: ""), action: #selector(deleteWidgetAction))
  38. let renameItem = self.menuItemWithTitle(title: NSLocalizedString("Rename", comment: ""), action: #selector(renameWidgetAction))
  39. menus.addItem(renameItem)
  40. menus.addItem(deleteItem)
  41. self.formOutilneView.menu = menus
  42. }
  43. @objc func deleteWidgetAction() {
  44. let selectedRowIndexes : IndexSet! = self.selectedRowIndexs()
  45. if selectedRowIndexes.count == 0 {
  46. __NSBeep()
  47. } else {
  48. var selectedPDFOutlineArr = [KMFormModel]();
  49. for index in selectedRowIndexes {
  50. var outline : KMFormModel = self.formOutilneView.item(atRow: index) as! KMFormModel
  51. selectedPDFOutlineArr.append(outline)
  52. }
  53. for form in selectedPDFOutlineArr {
  54. if form.datas.count > 0 {
  55. }
  56. if (form.annotation.fieldName() != nil) {
  57. form.annotation.page.removeAnnotation(form.annotation)
  58. }
  59. }
  60. self.listView.setNeedsDisplayForVisiblePages()
  61. self.reloadData()
  62. }
  63. }
  64. @objc func renameWidgetAction() {
  65. if self.formOutilneView.clickedRow >= 0 {
  66. self.renameFormModel = self.formOutilneView.item(atRow: self.formOutilneView.clickedRow) as? KMFormModel
  67. if self.renameFormModel.annotation.fieldName() == nil {
  68. return
  69. }
  70. let cell : KMFormCellView = self.formOutilneView.view(atColumn: 0, row: self.formOutilneView.clickedRow, makeIfNecessary: true) as! KMFormCellView
  71. self.renameTextField = cell.widgetTitleLabel
  72. self.renameTextField.delegate = self
  73. self.renameTextField.isEditable = true
  74. self.renameTextField.becomeFirstResponder()
  75. } else {
  76. __NSBeep()
  77. }
  78. }
  79. private func renameWidgetAnnotation(model:KMFormModel,label:String) {
  80. if model.annotation.fieldName() != nil {
  81. if model.annotation.fieldName() == label {
  82. return
  83. }
  84. model.annotation.setFieldName(label)
  85. self.formOutilneView.reloadData()
  86. var indexSet = IndexSet()
  87. indexSet.insert(self.formOutilneView.row(forItem: model))
  88. self.formOutilneView.selectRowIndexes(indexSet, byExtendingSelection: false)
  89. }
  90. }
  91. private func selectedRowIndexs() -> IndexSet {
  92. let clickRow = self.formOutilneView.clickedRow
  93. var selectedRowIndexs = self.formOutilneView.selectedRowIndexes
  94. if(clickRow != -1 && !selectedRowIndexs.contains(clickRow)) {
  95. selectedRowIndexs = [clickRow]
  96. }
  97. return selectedRowIndexs
  98. }
  99. private func menuItemWithTitle(title:String, action:Selector?) -> NSMenuItem {
  100. let menuItem = NSMenuItem.init(title: title as String, action: action, keyEquivalent: "")
  101. menuItem.target = self
  102. return menuItem
  103. }
  104. }
  105. // MARK - NSOutlineViewDataSource,NSOutlineViewDelegate
  106. extension KMFromViewController : NSOutlineViewDataSource,NSOutlineViewDelegate {
  107. func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
  108. let indexModel = item as? KMFormModel
  109. var count = 0
  110. if indexModel == nil {
  111. count = self.widgetAnnotations.count;
  112. } else {
  113. count = indexModel?.datas.count ?? 0
  114. }
  115. if(count == 0) { //无数据时的图
  116. } else {
  117. }
  118. return count
  119. }
  120. func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
  121. let indexModel = item as? KMFormModel
  122. var child = KMFormModel()
  123. if indexModel == nil {
  124. child = self.widgetAnnotations[index];
  125. } else {
  126. child = indexModel?.datas[index] ?? KMFormModel()
  127. }
  128. return child
  129. }
  130. func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
  131. let newitem = item as? KMFormModel
  132. return newitem?.datas.count ?? 0 > 0
  133. }
  134. func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item:Any) -> NSView? {
  135. let cell : KMFormCellView = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFormCellView"), owner: self) as! KMFormCellView
  136. let model : KMFormModel = item as! KMFormModel
  137. if (model.annotation.fieldName() != nil) {
  138. if(model.datas.count > 0 && model.groupName != ""){
  139. cell.widgetTitleLabel.stringValue = model.annotation.fieldName()
  140. cell.widgetImageView.isHidden = false
  141. cell.leftOffset.constant = 36
  142. } else if (model.datas.count == 0 && model.groupName == "") {
  143. cell.widgetTitleLabel.stringValue = model.annotation.fieldName()
  144. cell.widgetImageView.isHidden = false
  145. cell.leftOffset.constant = 36
  146. }
  147. } else {
  148. cell.widgetTitleLabel.stringValue = "\(model.index+1) page"
  149. cell.widgetImageView.isHidden = true
  150. cell.leftOffset.constant = 10
  151. }
  152. let font : NSFont = NSFont.systemFont(ofSize: CGFloat(UserDefaults.standard.float(forKey: "SKOfficeTableFontSize")))
  153. if (font != nil) {
  154. cell.widgetTitleLabel.font = NSFont.init(name: cell.widgetTitleLabel.font!.fontName, size: font.pointSize)
  155. }
  156. return cell
  157. }
  158. func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
  159. let rowView = KMCustomTableRowView()
  160. return rowView
  161. }
  162. func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
  163. let font : NSFont = NSFont.systemFont(ofSize: CGFloat(UserDefaults.standard.float(forKey: "SKOfficeTableFontSize")))
  164. return 27 + font.pointSize
  165. }
  166. func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
  167. if self.formOutilneView.selectedRowIndexes.count > 1 {
  168. return true
  169. }
  170. let model : KMFormModel = item as! KMFormModel
  171. if model.datas.count == 0 && model.annotation.fieldName() != nil {
  172. self.listView.go(to: model.annotation.bounds, on: model.annotation.page, animated: true)
  173. }
  174. return true
  175. }
  176. }
  177. // MARK - NSTextFieldDelegate
  178. extension KMFromViewController : NSTextFieldDelegate ,NSMenuItemValidation {
  179. func controlTextDidEndEditing(_ obj: Notification) {
  180. if (self.renameTextField.isEqual(obj.object)) {
  181. let textField : NSTextField = obj.object as! NSTextField
  182. self.renameWidgetAnnotation(model: self.renameFormModel, label: textField.stringValue)
  183. }
  184. }
  185. func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
  186. let action = menuItem.action
  187. if action == #selector(deleteWidgetAction) ||
  188. action == #selector(renameWidgetAction){
  189. if self.formOutilneView.selectedRowIndexes.count > 0 {
  190. let model : KMFormModel = self.formOutilneView.item(atRow: self.formOutilneView.clickedRow) as! KMFormModel
  191. if(model.annotation.fieldName() == nil) {
  192. return false
  193. }
  194. return true
  195. } else {
  196. return false
  197. }
  198. }
  199. return true
  200. }
  201. }