123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235 |
- //
- // KMFromViewController.swift
- // PDF Reader Pro
- //
- // Created by lxy on 2022/10/10.
- //
- import Cocoa
- class KMFromViewController: NSViewController {
- @IBOutlet weak var titleLabel: NSTextField!
- @IBOutlet weak var formOutilneView: NSOutlineView!
- @IBOutlet weak var topBackView: NSView!
-
- var listView : CPDFListView = CPDFListView()
- var widgetAnnotations : [KMFormModel] = []
- var renameTextField : NSTextField!
- var renameFormModel : KMFormModel!
- override func viewDidLoad() {
- super.viewDidLoad()
- self.view.wantsLayer = true
- self.view.layer?.backgroundColor = NSColor.gray.cgColor
- self.topBackView.wantsLayer = true
- self.topBackView.layer?.backgroundColor = NSColor.white.cgColor
- self.titleLabel.stringValue = NSLocalizedString("域", comment: "")
- self.formOutilneView.doubleAction = #selector(renameWidgetAction)
- self.addoutlineMenu()
- self.reloadData()
- }
-
- public func reloadData() {
- self.widgetAnnotations.removeAll()
- if self.listView.document == nil {
- return
- }
- self.widgetAnnotations = KMFormModel.getFormAnnotations(document: self.listView.document)
- self.formOutilneView.reloadData()
- }
-
- func addoutlineMenu() {
- let menus : NSMenu = NSMenu(title: "")
- let deleteItem = self.menuItemWithTitle(title: NSLocalizedString("Delete", comment: ""), action: #selector(deleteWidgetAction))
- let renameItem = self.menuItemWithTitle(title: NSLocalizedString("Rename", comment: ""), action: #selector(renameWidgetAction))
- menus.addItem(renameItem)
- menus.addItem(deleteItem)
- self.formOutilneView.menu = menus
- }
-
- @objc func deleteWidgetAction() {
- let selectedRowIndexes : IndexSet! = self.selectedRowIndexs()
- if selectedRowIndexes.count == 0 {
- __NSBeep()
- } else {
- var selectedPDFOutlineArr = [KMFormModel]();
- for index in selectedRowIndexes {
- var outline : KMFormModel = self.formOutilneView.item(atRow: index) as! KMFormModel
- selectedPDFOutlineArr.append(outline)
- }
- for form in selectedPDFOutlineArr {
- if form.datas.count > 0 {
-
- }
- if (form.annotation.fieldName() != nil) {
- form.annotation.page.removeAnnotation(form.annotation)
- }
- }
- self.listView.setNeedsDisplayForVisiblePages()
- self.reloadData()
- }
- }
-
- @objc func renameWidgetAction() {
- if self.formOutilneView.clickedRow >= 0 {
- self.renameFormModel = self.formOutilneView.item(atRow: self.formOutilneView.clickedRow) as? KMFormModel
- if self.renameFormModel.annotation.fieldName() == nil {
- return
- }
- let cell : KMFormCellView = self.formOutilneView.view(atColumn: 0, row: self.formOutilneView.clickedRow, makeIfNecessary: true) as! KMFormCellView
- self.renameTextField = cell.widgetTitleLabel
- self.renameTextField.delegate = self
- self.renameTextField.isEditable = true
- self.renameTextField.becomeFirstResponder()
- } else {
- __NSBeep()
- }
- }
-
- private func renameWidgetAnnotation(model:KMFormModel,label:String) {
- if model.annotation.fieldName() != nil {
- if model.annotation.fieldName() == label {
- return
- }
- model.annotation.setFieldName(label)
- self.formOutilneView.reloadData()
-
- var indexSet = IndexSet()
- indexSet.insert(self.formOutilneView.row(forItem: model))
- self.formOutilneView.selectRowIndexes(indexSet, byExtendingSelection: false)
- }
- }
-
- private func selectedRowIndexs() -> IndexSet {
- let clickRow = self.formOutilneView.clickedRow
- var selectedRowIndexs = self.formOutilneView.selectedRowIndexes
- if(clickRow != -1 && !selectedRowIndexs.contains(clickRow)) {
- selectedRowIndexs = [clickRow]
- }
- return selectedRowIndexs
- }
-
-
- private func menuItemWithTitle(title:String, action:Selector?) -> NSMenuItem {
- let menuItem = NSMenuItem.init(title: title as String, action: action, keyEquivalent: "")
- menuItem.target = self
- return menuItem
- }
-
-
- }
- // MARK - NSOutlineViewDataSource,NSOutlineViewDelegate
- extension KMFromViewController : NSOutlineViewDataSource,NSOutlineViewDelegate {
- func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int {
- let indexModel = item as? KMFormModel
- var count = 0
- if indexModel == nil {
- count = self.widgetAnnotations.count;
- } else {
- count = indexModel?.datas.count ?? 0
- }
- if(count == 0) { //无数据时的图
-
- } else {
-
- }
- return count
- }
-
- func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any {
- let indexModel = item as? KMFormModel
- var child = KMFormModel()
- if indexModel == nil {
- child = self.widgetAnnotations[index];
- } else {
- child = indexModel?.datas[index] ?? KMFormModel()
- }
- return child
- }
-
- func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool {
- let newitem = item as? KMFormModel
- return newitem?.datas.count ?? 0 > 0
- }
-
- func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item:Any) -> NSView? {
- let cell : KMFormCellView = outlineView.makeView(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMFormCellView"), owner: self) as! KMFormCellView
- let model : KMFormModel = item as! KMFormModel
- if (model.annotation.fieldName() != nil) {
- if(model.datas.count > 0 && model.groupName != ""){
- cell.widgetTitleLabel.stringValue = model.annotation.fieldName()
- cell.widgetImageView.isHidden = false
- cell.leftOffset.constant = 36
- } else if (model.datas.count == 0 && model.groupName == "") {
- cell.widgetTitleLabel.stringValue = model.annotation.fieldName()
- cell.widgetImageView.isHidden = false
- cell.leftOffset.constant = 36
- }
- } else {
- cell.widgetTitleLabel.stringValue = "\(model.index+1) page"
- cell.widgetImageView.isHidden = true
- cell.leftOffset.constant = 10
- }
- let font : NSFont = NSFont.systemFont(ofSize: CGFloat(UserDefaults.standard.float(forKey: "SKOfficeTableFontSize")))
- if (font != nil) {
- cell.widgetTitleLabel.font = NSFont.init(name: cell.widgetTitleLabel.font!.fontName, size: font.pointSize)
- }
-
- return cell
- }
-
- func outlineView(_ outlineView: NSOutlineView, rowViewForItem item: Any) -> NSTableRowView? {
- let rowView = KMCustomTableRowView()
- return rowView
- }
-
- func outlineView(_ outlineView: NSOutlineView, heightOfRowByItem item: Any) -> CGFloat {
- let font : NSFont = NSFont.systemFont(ofSize: CGFloat(UserDefaults.standard.float(forKey: "SKOfficeTableFontSize")))
- return 27 + font.pointSize
- }
-
- func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool {
- if self.formOutilneView.selectedRowIndexes.count > 1 {
- return true
- }
- let model : KMFormModel = item as! KMFormModel
- if model.datas.count == 0 && model.annotation.fieldName() != nil {
- self.listView.go(to: model.annotation.bounds, on: model.annotation.page, animated: true)
- }
- return true
- }
-
- }
- // MARK - NSTextFieldDelegate
- extension KMFromViewController : NSTextFieldDelegate ,NSMenuItemValidation {
- func controlTextDidEndEditing(_ obj: Notification) {
- if (self.renameTextField.isEqual(obj.object)) {
- let textField : NSTextField = obj.object as! NSTextField
- self.renameWidgetAnnotation(model: self.renameFormModel, label: textField.stringValue)
- }
- }
-
- func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
- let action = menuItem.action
- if action == #selector(deleteWidgetAction) ||
- action == #selector(renameWidgetAction){
- if self.formOutilneView.selectedRowIndexes.count > 0 {
- let model : KMFormModel = self.formOutilneView.item(atRow: self.formOutilneView.clickedRow) as! KMFormModel
- if(model.annotation.fieldName() == nil) {
- return false
- }
- return true
- } else {
- return false
- }
- }
- return true
- }
-
- }
|