AutoTestAdvanceSettingView.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. //
  2. // AutoTestAdvanceSettingView.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2022/11/25.
  6. //
  7. import Foundation
  8. import AppKit
  9. let kItemCountPerRow = 3
  10. public protocol AutoTestAdvanceSettingViewDelegate : NSObjectProtocol {
  11. func advanceSettingDidUpdate(_ settingView:NSView?)
  12. }
  13. class AutoTestAdvanceSettingView : NSView, NSTableViewDataSource, NSTableViewDelegate, TestFileCellViewDelegate {
  14. @IBOutlet var _titleLbl : NSTextField!
  15. @IBOutlet var _keyScrollView : NSScrollView!
  16. @IBOutlet var _keyContentView : NSView!
  17. @IBOutlet var _fileList : NSTableView!
  18. @IBOutlet var _replaceAllBtn : NSButton!
  19. @IBOutlet var _addBtn : NSButton!
  20. var _keyViews : NSMutableArray!
  21. var _autoTestObj : AutoTest?
  22. var _files : [String]! = []
  23. var delegate : AutoTestAdvanceSettingViewDelegate?
  24. override func awakeFromNib() {
  25. self.wantsLayer = true;
  26. self.layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
  27. _addBtn.isHidden = true;
  28. }
  29. // Setter
  30. let kCheckBtnDefaultHeight = 25.0
  31. public func setAutoTestObj(_ obj:AutoTest?) {
  32. _autoTestObj = obj;
  33. // Set Title
  34. // self.setAccessibilityEnabled((_autoTestObj?.isOriginFileExist() == true && _autoTestObj?.isCheckFileExist() == true))
  35. //
  36. _addBtn.isHidden = nil == _autoTestObj;
  37. if nil != _autoTestObj {
  38. _titleLbl.stringValue = String("[\(_autoTestObj?.fileType() as! String)]\(_autoTestObj?.name() as! String)")
  39. let checkKeys = _autoTestObj?.keys() as! NSArray
  40. let selectKeys = _autoTestObj?.selectedKeys() as! NSArray
  41. if _keyViews == nil {
  42. _keyViews = NSMutableArray()
  43. }
  44. while (_keyViews.count != checkKeys.count) {
  45. if (_keyViews.count > checkKeys.count) {
  46. (_keyViews.lastObject as! NSButton).removeFromSuperview()
  47. _keyViews.removeLastObject()
  48. }else {
  49. let checkBtn = NSButton(checkboxWithTitle: "", target: self, action: #selector(keyChecked))
  50. _keyContentView.addSubview(checkBtn);
  51. _keyViews.add(checkBtn)
  52. }
  53. }
  54. var height = CGFloat((_keyViews.count + 1)/kItemCountPerRow * (Int(kCheckBtnDefaultHeight) + 5));
  55. // _keyContentView.frame = NSRect(x: 0, y: 0, width: width, height: kTFDefaultHeight)
  56. if (height > _keyScrollView.frame.height) {
  57. _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 20, height: height))
  58. }else {
  59. height = _keyScrollView.frame.height
  60. _keyContentView.setFrameSize(NSSize.init(width: _keyScrollView.frame.width - 20, height: _keyScrollView.frame.height))
  61. }
  62. for btn in _keyViews {
  63. let i = _keyViews.index(of: btn)
  64. let checkBtn = btn as! NSButton
  65. checkBtn.title = checkKeys[i] as! String;
  66. // Update Check Status
  67. if selectKeys.contains(checkKeys[i]) {
  68. checkBtn.state = .on
  69. }else {
  70. checkBtn.state = .off
  71. }
  72. checkBtn.sizeToFit()
  73. checkBtn.frame = CGRect.init(x: CGFloat(i % kItemCountPerRow) * _keyContentView.frame.size.width/CGFloat(kItemCountPerRow), y: CGFloat(height - kCheckBtnDefaultHeight), width: checkBtn.frame.width, height: kCheckBtnDefaultHeight)
  74. checkBtn.autoresizingMask = .maxXMargin.union(.minYMargin)
  75. if i % kItemCountPerRow == (kItemCountPerRow - 1) {
  76. height = height - checkBtn.frame.height - 5
  77. }
  78. }
  79. _keyScrollView.documentView = _keyContentView;
  80. _files = DataModel.shared.originFilesFor(String(_autoTestObj?.fileType() ?? ""),
  81. type: String(_autoTestObj?.type() ?? ""))
  82. _fileList.reloadData()
  83. _replaceAllBtn.isEnabled = _autoTestObj!.canUpdateRefImage()
  84. }else {
  85. _titleLbl.stringValue = ""
  86. if _keyViews == nil {
  87. _keyViews = NSMutableArray()
  88. }
  89. while (_keyViews.count != 0) {
  90. (_keyViews.lastObject as! NSView).removeFromSuperview()
  91. _keyViews.removeLastObject()
  92. }
  93. _files = []
  94. _fileList.reloadData()
  95. _replaceAllBtn.isEnabled = false
  96. }
  97. }
  98. /// IBActionn
  99. @IBAction func keyChecked(_ sender:NSButton) {
  100. var currentSelectedKeys = NSMutableArray()
  101. let checkKeys = _autoTestObj?.keys() as! NSArray
  102. for btn in _keyViews {
  103. let cBtn = btn as! NSButton
  104. if cBtn.state == .on {
  105. let index = _keyViews.index(of: btn)
  106. currentSelectedKeys.add(checkKeys[index])
  107. }
  108. }
  109. _autoTestObj?.setSelectedKeys(currentSelectedKeys)
  110. if delegate != nil {
  111. delegate?.advanceSettingDidUpdate(self)
  112. }
  113. }
  114. @IBAction func replaceAllAction(_ sender:NSButton) {
  115. if nil != _autoTestObj {
  116. _autoTestObj?.updateRefImage()
  117. }
  118. }
  119. @IBAction func addFileAction(_ sender:NSButton) {
  120. if nil == _autoTestObj {
  121. return
  122. }
  123. let originDirectory = _autoTestObj?.originFileDirectory()
  124. let openPanel = NSOpenPanel()
  125. openPanel.canChooseFiles = true
  126. openPanel.allowedContentTypes = [.pdf]
  127. openPanel.canChooseDirectories = false
  128. let comboBox = NSComboBox.init(frame: NSRect.init(x: 0, y: 0, width: 100, height: 25))
  129. comboBox.removeAllItems()
  130. comboBox.addItems(withObjectValues: ["添加超链接", "导入文件"])
  131. comboBox.selectItem(at: 0)
  132. openPanel.accessoryView = comboBox;
  133. if openPanel.runModal() == NSApplication.ModalResponse.OK {
  134. let path = openPanel.url?.relativePath
  135. let resultPath = NSString(string: originDirectory!).appendingPathComponent(openPanel.url!.lastPathComponent)
  136. if FileManager.default.fileExists(atPath: path!) {
  137. if comboBox.indexOfSelectedItem == 0 {
  138. try! FileManager.default.createSymbolicLink(atPath: resultPath, withDestinationPath: path!)
  139. }else {
  140. try! FileManager.default.copyItem(atPath: path!, toPath: resultPath)
  141. }
  142. _files = DataModel.shared.originFilesFor(String(_autoTestObj?.fileType() ?? ""),
  143. type: String(_autoTestObj?.type() ?? ""))
  144. _fileList.reloadData()
  145. }
  146. }
  147. }
  148. // TableView Delegate
  149. func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
  150. let cellView = TestFileCellView.shared()
  151. if (_files.count >= row) {
  152. let title = _files[row]
  153. cellView?.setTitle(title)
  154. cellView?._delegate = self
  155. if _autoTestObj != nil {
  156. cellView?.setNeedReplaceBtn(_autoTestObj!.canUpdateRefImage(title))
  157. }else {
  158. cellView?.setNeedReplaceBtn(false)
  159. }
  160. }
  161. return cellView
  162. }
  163. func selectionShouldChange(in tableView: NSTableView) -> Bool {
  164. return true
  165. }
  166. func tableView(_ tableView: NSTableView, shouldSelectRow row: Int) -> Bool {
  167. return false
  168. }
  169. func tableView(_ tableView: NSTableView, shouldSelect tableColumn: NSTableColumn?) -> Bool {
  170. return false
  171. }
  172. func tableView(_ tableView: NSTableView, mouseDownInHeaderOf tableColumn: NSTableColumn) {
  173. }
  174. func tableView(_ tableView: NSTableView, didClick tableColumn: NSTableColumn) {
  175. }
  176. func tableView(_ tableView: NSTableView, didDrag tableColumn: NSTableColumn) {
  177. }
  178. func tableView(_ tableView: NSTableView, heightOfRow row: Int) -> CGFloat {
  179. return 30
  180. }
  181. func tableView(_ tableView: NSTableView, isGroupRow row: Int) -> Bool {
  182. return false
  183. }
  184. func tableView(_ tableView: NSTableView, sizeToFitWidthOfColumn column: Int) -> CGFloat {
  185. return tableView.frame.width
  186. }
  187. func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
  188. return []
  189. }
  190. func tableViewSelectionDidChange(_ notification: Notification) {
  191. }
  192. // TableView Data Source
  193. func numberOfRows(in tableView: NSTableView) -> Int {
  194. return _files.count
  195. }
  196. // TestFileCellViewDelegate
  197. func fileCellNeedReplace(_ cell: TestFileCellView, fileName: String) {
  198. if (_autoTestObj != nil) {
  199. _autoTestObj?.updateRefImage(fileName)
  200. }
  201. _fileList.reloadData()
  202. }
  203. }