AdvanceSettingViewController.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. //
  2. // AdvanceSettingViewController.swift
  3. // KdanAuto
  4. //
  5. // Created by 朱东勇 on 2023/4/17.
  6. //
  7. import Cocoa
  8. class AdvanceSettingViewController: NSViewController, NSPopoverDelegate {
  9. @IBOutlet var m_titleBarView:NSView! // Default is NO
  10. @IBOutlet var m_cancelBtn:NSButton!
  11. @IBOutlet var m_doneBtn:NSButton!
  12. @IBOutlet var m_titleLbl:NSTextField!
  13. @IBOutlet var m_nameLbl:NSTextField!
  14. @IBOutlet var m_pathLbl:NSTextField!
  15. @IBOutlet var m_sepLine:NSView! // Default is NO
  16. let m_useOldLibCell = ASBOOLPropertyCell.shared()
  17. /**
  18. {
  19. "Extention" : "xlsx",
  20. "Class" : "AutoTest",
  21. "Params" : [
  22. {
  23. "ValueType" : "NSInteger",
  24. "Property" : "contentOptions",
  25. "Name" : "Content Options",
  26. "DefaultValue": @(2)
  27. "Values" : [{"Value":@(0), "Name":"Only Text"},
  28. {"Value":@(1), "Name":"Only Table"},...
  29. ]
  30. },
  31. {
  32. "ValueType" : "BOOL",
  33. "Property" : "isAllowOCR",
  34. "Name" : "Allow OCR",
  35. "DefaultValue": @(YES)
  36. },
  37. ...
  38. ]
  39. }
  40. */
  41. var m_baseInfo:NSDictionary!
  42. /**
  43. {
  44. "Type":"Others(Old)", // The Folder Name
  45. "ID" : "1001", // The ID for Search, private,
  46. "Extentiion" : "txt",
  47. "Name" : "快照对比(老库)", // Name
  48. "Class" : "AutoTest",
  49. "Params" : {
  50. "useOldLib" : @(true),
  51. "Options" : [
  52. {
  53. "Property" : "contentOptions",
  54. "ValueType" : "NSInteger",
  55. "Value": @(2)
  56. },
  57. ...
  58. ]
  59. }
  60. }
  61. */
  62. var m_testCaseInfo:NSMutableDictionary? = nil
  63. /** such as CSV/PDF/PNG/JPG etc.
  64. */
  65. var m_fileType:String? = nil
  66. var m_testObj:AutoTest? = nil
  67. var m_callBack:(_ testObj:AutoTest?, _ property:NSDictionary) -> () = {(obj, property) in
  68. }
  69. @IBOutlet var m_propertyCV:NSView!
  70. var m_propertyViews = NSMutableArray()
  71. var _popover: NSPopover!
  72. ///
  73. static var sharedASViewController : AdvanceSettingViewController? = nil
  74. class func shared() -> AdvanceSettingViewController {
  75. if nil == sharedASViewController {
  76. var objects : NSArray!
  77. Bundle.main.loadNibNamed("AdvanceSettingViewController", owner: nil, topLevelObjects: &objects)
  78. for tView in objects {
  79. if let tv = tView as? AdvanceSettingViewController {
  80. sharedASViewController = tv
  81. }
  82. }
  83. }
  84. return sharedASViewController!
  85. }
  86. override func viewDidLoad() {
  87. super.viewDidLoad()
  88. // Do view setup here.
  89. }
  90. override func awakeFromNib() {
  91. self.view.wantsLayer = true;
  92. self.view.layer?.backgroundColor = .init(gray: 0.98, alpha: 1)
  93. m_titleBarView.wantsLayer = true;
  94. m_titleBarView.layer?.backgroundColor = .init(gray: 0.9, alpha: 0.8)
  95. m_sepLine.wantsLayer = true;
  96. m_sepLine.layer?.backgroundColor = .init(gray: 0.5, alpha: 0.5);
  97. m_useOldLibCell?.setPropertyInfo(["ValueType":"BOOL",
  98. "DefaultValue":NSNumber(booleanLiteral: true),
  99. "Name":"Use Old Lib"
  100. ])
  101. m_useOldLibCell?.setValueChangedCallBack({ value, object in
  102. let boolValue = value as! NSNumber
  103. for pv in self.m_propertyViews {
  104. let propView = pv as! ASPropertyCell?
  105. propView?.setEnabled(!boolValue.boolValue)
  106. }
  107. });
  108. }
  109. /// Setter
  110. func setBaseInfo(_ info:NSDictionary) {
  111. /**
  112. {
  113. class : AutoTest
  114. Extention : pdf
  115. Params: Aray
  116. }
  117. */
  118. m_baseInfo = info;
  119. updateViews()
  120. }
  121. func setTestCase(_ info:NSMutableDictionary?) {
  122. m_testCaseInfo = info;
  123. m_doneBtn.title = (m_testCaseInfo == nil) ? "新建" : "保存"
  124. m_nameLbl.stringValue = (m_testCaseInfo?.value(forKey: "Name") ?? "") as! String
  125. m_pathLbl.isEditable = m_testCaseInfo == nil;
  126. m_pathLbl.stringValue = (m_testCaseInfo?.value(forKey: "Type") ?? "") as! String
  127. m_useOldLibCell?.setValue(NSNumber(booleanLiteral: false))
  128. let params = m_testCaseInfo?.value(forKey: "Params") as? NSDictionary
  129. if (params != nil) {
  130. let useOldLib = NSDictionary(dictionary: params!).value(forKey: "useOldLib") as? NSNumber
  131. if (nil != useOldLib && useOldLib!.boolValue) {
  132. m_useOldLibCell?.setValue(useOldLib as AnyObject)
  133. }
  134. }
  135. let boolValue = m_useOldLibCell?.value() as! NSNumber
  136. for pv in self.m_propertyViews {
  137. let propView = pv as! ASPropertyCell?
  138. propView?.setEnabled(!boolValue.boolValue)
  139. }
  140. updateValues()
  141. }
  142. func setCallback(_ callback:@escaping (_ testObj:AutoTest?, _ property:NSDictionary) -> ()) {
  143. m_callBack = callback
  144. }
  145. /// Load Views
  146. func updateViews() {
  147. for view in m_propertyViews {
  148. (view as! ASPropertyCell?)!.removeFromSuperview()
  149. }
  150. m_propertyViews.removeAllObjects()
  151. if (m_baseInfo.value(forKey: "Params") != nil) {
  152. let propertys = m_baseInfo.value(forKey: "Params") as! NSArray
  153. for property in propertys {
  154. let cell = ASPropertyCell.propertyCellFor(property as! NSDictionary)
  155. if (cell != nil) {
  156. m_propertyViews.add(cell!);
  157. m_propertyCV.addSubview(cell!);
  158. }
  159. }
  160. }
  161. m_propertyCV.addSubview(m_useOldLibCell!)
  162. let cellCount = m_propertyViews.count + 1
  163. self.view.frame = CGRectMake(self.view.frame.origin.x,
  164. self.view.frame.origin.y,
  165. self.view.frame.size.width,
  166. self.view.frame.size.height - self.m_propertyCV.frame.size.height + CGFloat(cellCount * 35)+5)
  167. m_useOldLibCell?.frame = CGRectMake(0, 5+CGFloat((cellCount - 1) * 35), self.m_propertyCV.frame.size.width, 35)
  168. for pv in m_propertyViews {
  169. let propView = pv as! ASPropertyCell?
  170. let index = cellCount - 2 - m_propertyViews.index(of: pv)
  171. propView?.frame = CGRectMake(0, 5+CGFloat(index * 35), self.m_propertyCV.frame.size.width, 35)
  172. }
  173. }
  174. func updateValues() {
  175. let params = m_testCaseInfo?.value(forKey: "Params") as? NSDictionary
  176. if (params != nil) {
  177. let options = params?.value(forKey: "Options") as? NSArray
  178. if (options != nil) {
  179. for option in options! {
  180. let par = option as! NSDictionary
  181. let propertyName = par.value(forKey: "Property") as! String
  182. for pv in m_propertyViews {
  183. let propView = pv as! ASPropertyCell?
  184. let pv_propertyName = propView?.m_propertyInfo?.value(forKey: "Property") as! String
  185. if (NSString(string: propertyName).isEqual(to: pv_propertyName)) {
  186. if (par.value(forKey: "Value") != nil) {
  187. propView?.setValue(par.value(forKey: "Value")! as AnyObject)
  188. }
  189. break
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. /// Show
  197. func showIn(_ view:NSView, rect:NSRect) {
  198. if _popover == nil {
  199. _popover = NSPopover.init()
  200. _popover.contentViewController = self;
  201. _popover.delegate = self
  202. }
  203. _popover.contentSize = self.view.frame.size
  204. _popover.show(relativeTo: rect, of: view, preferredEdge: NSRectEdge.maxX)
  205. }
  206. /// IBAction
  207. @IBAction func cancelAction(_ sender:NSButton) {
  208. _popover.close()
  209. }
  210. @IBAction func doneAction(_ sender:NSButton) {
  211. let type = self.m_pathLbl.stringValue
  212. let name = self.m_nameLbl.stringValue
  213. let property = NSMutableDictionary()
  214. property.setValue(type, forKey: "Type")
  215. property.setValue(name, forKey: "Name")
  216. if (m_testCaseInfo != nil) {
  217. property.setValue(m_testCaseInfo?.value(forKey: "ID"), forKey: "ID")
  218. }
  219. property.setValue(m_baseInfo.value(forKey: "Extention"), forKey: "Extention")
  220. property.setValue(m_baseInfo.value(forKey: "Class"), forKey: "Class")
  221. let params = NSMutableDictionary()
  222. property.setValue(params, forKey: "Params")
  223. let useOldLib = m_useOldLibCell?.value() as! NSNumber
  224. if (useOldLib.boolValue) {
  225. params.setValue(useOldLib, forKey: "useOldLib")
  226. }else {
  227. let options = NSMutableArray()
  228. params.setValue(options, forKey: "Options")
  229. for pv in m_propertyViews {
  230. let propView = pv as! ASPropertyCell?
  231. let pv_propertyName = propView?.m_propertyInfo?.value(forKey: "Property") as! String
  232. let pv_valueType = propView?.m_propertyInfo?.value(forKey: "ValueType") as! String
  233. options.add(["Property": pv_propertyName,
  234. "ValueType":pv_valueType,
  235. "Value":propView!.value()])
  236. }
  237. }
  238. NSLog("property:\(property)")
  239. m_callBack(m_testObj, property)
  240. _popover.close()
  241. }
  242. // NSPopoverDelegate
  243. func popoverShouldClose(_ popover: NSPopover) -> Bool {
  244. return true
  245. }
  246. }
  247. extension AdvanceSettingViewController {
  248. class func baseInfoFor(_ type:String) -> NSDictionary? {
  249. let path = Bundle.main.path(forResource: "AdvanceSettingProperty", ofType: "plist")
  250. let asPropertys = try? NSDictionary.init(contentsOfFile: path!)
  251. if asPropertys != nil {
  252. return asPropertys?.value(forKey: type) as? NSDictionary
  253. }
  254. return nil
  255. }
  256. }