AdvanceSettingViewController.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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_typeBox:NSComboBox!
  16. @IBOutlet var m_sepLine:NSView! // Default is NO
  17. let m_useOldLibCell = ASBOOLPropertyCell.shared()
  18. /**
  19. {
  20. "Extention" : "xlsx",
  21. "Classs" : [
  22. {
  23. "Name" : "对照测试",
  24. "Class": "AutoTest"
  25. }
  26. ],
  27. "Params" : [
  28. {
  29. "ValueType" : "NSInteger",
  30. "Property" : "contentOptions",
  31. "Name" : "Content Options",
  32. "DefaultValue": @(2)
  33. "Values" : [{"Value":@(0), "Name":"Only Text"},
  34. {"Value":@(1), "Name":"Only Table"},...
  35. ]
  36. },
  37. {
  38. "ValueType" : "BOOL",
  39. "Property" : "isAllowOCR",
  40. "Name" : "Allow OCR",
  41. "DefaultValue": @(YES)
  42. },
  43. ...
  44. ]
  45. }
  46. */
  47. var m_baseInfo:NSDictionary!
  48. /**
  49. {
  50. "Type":"Others(Old)", // The Folder Name
  51. "ID" : "1001", // The ID for Search, private,
  52. "Extentiion" : "txt",
  53. "Name" : "快照对比(老库)", // Name
  54. "Class" : "AutoTest",
  55. "Params" : {
  56. "useOldLib" : @(true),
  57. "Options" : [
  58. {
  59. "Property" : "contentOptions",
  60. "ValueType" : "NSInteger",
  61. "Value": @(2)
  62. },
  63. ...
  64. ]
  65. }
  66. }
  67. */
  68. var m_testCaseInfo:NSMutableDictionary? = nil
  69. /** such as CSV/PDF/PNG/JPG etc.
  70. */
  71. var m_fileType:String? = nil
  72. var m_testObj:AutoTest? = nil
  73. var m_callBack:(_ testObj:AutoTest?, _ property:NSDictionary) -> () = {(obj, property) in
  74. }
  75. @IBOutlet var m_propertyCV:NSView!
  76. var m_propertyViews = NSMutableArray()
  77. var _popover: NSPopover!
  78. ///
  79. static var sharedASViewController : AdvanceSettingViewController? = nil
  80. class func shared() -> AdvanceSettingViewController {
  81. if nil == sharedASViewController {
  82. var objects : NSArray!
  83. Bundle.main.loadNibNamed("AdvanceSettingViewController", owner: nil, topLevelObjects: &objects)
  84. for tView in objects {
  85. if let tv = tView as? AdvanceSettingViewController {
  86. sharedASViewController = tv
  87. }
  88. }
  89. }
  90. return sharedASViewController!
  91. }
  92. override func viewDidLoad() {
  93. super.viewDidLoad()
  94. // Do view setup here.
  95. }
  96. override func awakeFromNib() {
  97. self.view.wantsLayer = true;
  98. self.view.layer?.backgroundColor = .init(gray: 0.98, alpha: 1)
  99. m_titleBarView.wantsLayer = true;
  100. m_titleBarView.layer?.backgroundColor = .init(gray: 0.9, alpha: 0.8)
  101. m_sepLine.wantsLayer = true;
  102. m_sepLine.layer?.backgroundColor = .init(gray: 0.5, alpha: 0.5);
  103. m_useOldLibCell?.setPropertyInfo(["ValueType":"BOOL",
  104. "DefaultValue":NSNumber(booleanLiteral: true),
  105. "Name":"Use Old Lib"
  106. ])
  107. m_useOldLibCell?.setValueChangedCallBack({ value, object in
  108. let boolValue = value as! NSNumber
  109. for pv in self.m_propertyViews {
  110. let propView = pv as! ASPropertyCell?
  111. propView?.setEnabled(!boolValue.boolValue)
  112. }
  113. });
  114. }
  115. /// Setter
  116. func setBaseInfo(_ info:NSDictionary) {
  117. /**
  118. {
  119. class : AutoTest
  120. Extention : pdf
  121. Params: Aray
  122. }
  123. */
  124. m_baseInfo = info;
  125. updateViews()
  126. }
  127. func setTestCase(_ info:NSDictionary?) {
  128. m_testCaseInfo = info != nil ? NSMutableDictionary(dictionary: info!) : nil
  129. m_doneBtn.title = (m_testCaseInfo == nil) ? "新建" : "保存"
  130. m_nameLbl.stringValue = (m_testCaseInfo?.value(forKey: "Name") ?? "") as! String
  131. m_pathLbl.isEditable = m_testCaseInfo == nil;
  132. m_pathLbl.stringValue = (m_testCaseInfo?.value(forKey: "Type") ?? "") as! String
  133. m_useOldLibCell?.setValue(NSNumber(booleanLiteral: false))
  134. let params = m_testCaseInfo?.value(forKey: "Params") as? NSDictionary
  135. if (params != nil) {
  136. let useOldLib = NSDictionary(dictionary: params!).value(forKey: "useOldLib") as? NSNumber
  137. if (nil != useOldLib && useOldLib!.boolValue) {
  138. m_useOldLibCell?.setValue(useOldLib as AnyObject)
  139. }
  140. }
  141. let boolValue = m_useOldLibCell?.value() as! NSNumber
  142. for pv in self.m_propertyViews {
  143. let propView = pv as! ASPropertyCell?
  144. propView?.setEnabled(!boolValue.boolValue)
  145. }
  146. updateValues()
  147. }
  148. func setCallback(_ callback:@escaping (_ testObj:AutoTest?, _ property:NSDictionary) -> ()) {
  149. m_callBack = callback
  150. }
  151. /// Load Views
  152. func updateViews() {
  153. for view in m_propertyViews {
  154. (view as! ASPropertyCell?)!.removeFromSuperview()
  155. }
  156. m_propertyViews.removeAllObjects()
  157. let classs = m_baseInfo.value(forKey: "Classs") as! NSArray
  158. m_typeBox.removeAllItems()
  159. for cl in classs {
  160. let classInfo = cl as! NSDictionary
  161. m_typeBox.addItem(withObjectValue: classInfo.value(forKey: "Name") as! String);
  162. }
  163. m_typeBox.isEnabled = classs.count > 1
  164. if (m_baseInfo.value(forKey: "Params") != nil) {
  165. let propertys = m_baseInfo.value(forKey: "Params") as! NSArray
  166. for property in propertys {
  167. let cell = ASPropertyCell.propertyCellFor(property as! NSDictionary)
  168. if (cell != nil) {
  169. m_propertyViews.add(cell!);
  170. m_propertyCV.addSubview(cell!);
  171. }
  172. }
  173. }
  174. m_propertyCV.addSubview(m_useOldLibCell!)
  175. let cellCount = m_propertyViews.count + 1
  176. self.view.frame = CGRectMake(self.view.frame.origin.x,
  177. self.view.frame.origin.y,
  178. self.view.frame.size.width,
  179. self.view.frame.size.height - self.m_propertyCV.frame.size.height + CGFloat(cellCount * 35)+5)
  180. m_useOldLibCell?.frame = CGRectMake(0, 5+CGFloat((cellCount - 1) * 35), self.m_propertyCV.frame.size.width, 35)
  181. for pv in m_propertyViews {
  182. let propView = pv as! ASPropertyCell?
  183. let index = cellCount - 2 - m_propertyViews.index(of: pv)
  184. propView?.frame = CGRectMake(0, 5+CGFloat(index * 35), self.m_propertyCV.frame.size.width, 35)
  185. }
  186. }
  187. func updateValues() {
  188. let params = m_testCaseInfo?.value(forKey: "Params") as? NSDictionary
  189. if (params != nil) {
  190. let options = params?.value(forKey: "Options") as? NSArray
  191. if (options != nil) {
  192. for option in options! {
  193. let par = option as! NSDictionary
  194. let propertyName = par.value(forKey: "Property") as! String
  195. for pv in m_propertyViews {
  196. let propView = pv as! ASPropertyCell?
  197. let pv_property = propView?.m_propertyInfo?.value(forKey: "Property") as! String
  198. if (NSString(string: propertyName).isEqual(to: pv_property)) {
  199. if (par.value(forKey: "Value") != nil) {
  200. propView?.setValue(par.value(forKey: "Value")! as AnyObject)
  201. }
  202. break
  203. }
  204. }
  205. }
  206. }
  207. }
  208. if (m_testCaseInfo != nil) {
  209. let cClass = m_testCaseInfo!.value(forKey: "Class") as! NSString
  210. let classs = m_baseInfo.value(forKey: "Classs") as! NSArray
  211. for cl in classs {
  212. let classInfo = cl as! NSDictionary
  213. if (cClass.isEqual(to: classInfo.value(forKey: "Class"))) {
  214. m_typeBox.selectItem(at: classs.index(of: cl))
  215. break
  216. }
  217. }
  218. }else {
  219. m_typeBox.selectItem(at: 0)
  220. }
  221. }
  222. /// Show
  223. func showIn(_ view:NSView, rect:NSRect) {
  224. if _popover == nil {
  225. _popover = NSPopover.init()
  226. _popover.contentViewController = self;
  227. _popover.delegate = self
  228. }
  229. _popover.contentSize = self.view.frame.size
  230. _popover.show(relativeTo: rect, of: view, preferredEdge: NSRectEdge.maxX)
  231. }
  232. /// IBAction
  233. @IBAction func cancelAction(_ sender:NSButton) {
  234. _popover.close()
  235. }
  236. @IBAction func doneAction(_ sender:NSButton) {
  237. let type = self.m_pathLbl.stringValue as NSString
  238. let name = self.m_nameLbl.stringValue as NSString
  239. if (name.length == 0) {
  240. self.showAlert("测试项名称不能为空!")
  241. return
  242. }
  243. if (type.length == 0) {
  244. self.showAlert("测试项文件夹不能为空!")
  245. return
  246. }
  247. let path = DataModel.shared.directoryPath().appendingFormat("/\(m_fileType ?? "")/\(type)")
  248. let currentType = m_testCaseInfo?.value(forKey: "Type")
  249. if (!type.isEqual(to: currentType) && FileManager.default.fileExists(atPath: path)) {
  250. self.showAlert("【测试项文件夹】(\(type))已存在,请换一个【测试项文件夹】名称!")
  251. return
  252. }
  253. let property = NSMutableDictionary()
  254. property.setValue(type, forKey: "Type")
  255. property.setValue(name, forKey: "Name")
  256. if (m_testCaseInfo != nil) {
  257. property.setValue(m_testCaseInfo?.value(forKey: "ID"), forKey: "ID")
  258. }
  259. property.setValue(m_baseInfo.value(forKey: "Extention"), forKey: "Extention")
  260. let classs = m_baseInfo.value(forKey: "Classs") as! NSArray
  261. let classInfo = classs.object(at: m_typeBox.indexOfSelectedItem) as! NSDictionary
  262. property.setValue(classInfo.value(forKey: "Class") as! String, forKey: "Class")
  263. let params = NSMutableDictionary()
  264. property.setValue(params, forKey: "Params")
  265. let useOldLib = m_useOldLibCell?.value() as! NSNumber
  266. if (useOldLib.boolValue) {
  267. params.setValue(useOldLib, forKey: "useOldLib")
  268. }else {
  269. let options = NSMutableArray()
  270. params.setValue(options, forKey: "Options")
  271. for pv in m_propertyViews {
  272. let propView = pv as! ASPropertyCell?
  273. let pv_property = propView?.m_propertyInfo?.value(forKey: "Property") as! String
  274. let pv_valueType = propView?.m_propertyInfo?.value(forKey: "ValueType") as! String
  275. options.add(["Property": pv_property,
  276. "ValueType":pv_valueType,
  277. "Value":propView!.value()])
  278. }
  279. }
  280. m_callBack(m_testObj, property)
  281. if (LogViewController.shared().isVisable()) {
  282. if (m_testCaseInfo != nil) {
  283. LogViewController.shared().appendLog("\n*********修改参数*******\n")
  284. }else {
  285. LogViewController.shared().appendLog("\n*********新建测试项*******\n")
  286. }
  287. LogViewController.shared().appendLog("- 名称: \(name)\n")
  288. LogViewController.shared().appendLog("- 文件夹: \(type)\n")
  289. property.setValue(type, forKey: "Type")
  290. property.setValue(name, forKey: "Name")
  291. if (useOldLib.boolValue) {
  292. LogViewController.shared().appendLog("- 库版本: 老库\n")
  293. }else {
  294. LogViewController.shared().appendLog("- 库版本: 新库\n")
  295. for pv in m_propertyViews {
  296. let propView = pv as! ASPropertyCell?
  297. let pv_propertyName = propView?.m_propertyInfo?.value(forKey: "Name") as! String
  298. let pv_ValueType = propView?.m_propertyInfo?.value(forKey: "ValueType") as! NSString
  299. var value = propView?.value()
  300. let pv_Values = propView?.m_propertyInfo?.value(forKey: "Values") as? NSArray
  301. if (pv_ValueType.isEqual(to: "BOOL") || pv_ValueType.isEqual(to: "Bool") || pv_ValueType.isEqual(to: "bool")) {
  302. LogViewController.shared().appendLog("- \(pv_propertyName): \((value as! NSNumber).boolValue ? "YES" : "NO")\n")
  303. }else if ((["int", "int16_t", "int32_t", "int64_t", "NSInteger",
  304. "size_t", "uint16_t", "uint32_t", "uint64_t", "NSUInteger",
  305. "uint8_t", "int8_t"] as NSArray).contains(pv_ValueType)) {
  306. if (pv_Values != nil) {
  307. for vi in pv_Values! {
  308. let valueInfo = vi as! NSDictionary
  309. if ((valueInfo["Value"] as! NSNumber).intValue == (value as! NSNumber).intValue) {
  310. LogViewController.shared().appendLog("- \(pv_propertyName): \(valueInfo["Name"] as! String)\n")
  311. break;
  312. }
  313. }
  314. }else {
  315. LogViewController.shared().appendLog("- \(pv_propertyName): \((value as! NSNumber).intValue)\n")
  316. }
  317. }else if ((["float", "double"] as NSArray).contains(pv_ValueType)) {
  318. if (pv_Values != nil) {
  319. for vi in pv_Values! {
  320. let valueInfo = vi as! NSDictionary
  321. if (fabs((valueInfo["Value"] as! NSNumber).floatValue - (value as! NSNumber).floatValue) < 0.001) {
  322. LogViewController.shared().appendLog("- \(pv_propertyName): \(valueInfo["Name"] as! String)\n")
  323. break;
  324. }
  325. }
  326. }else {
  327. LogViewController.shared().appendLog("- \(pv_propertyName): \((value as! NSNumber).floatValue)\n")
  328. }
  329. }else if ((["CGRect", "NSRect"] as NSArray).contains(pv_ValueType)) {
  330. if (pv_Values != nil) {
  331. for vi in pv_Values! {
  332. let valueInfo = vi as! NSDictionary
  333. if (valueInfo["Value"] as! NSString).isEqual(to: (value as! String)) {
  334. LogViewController.shared().appendLog("- \(pv_propertyName): \(valueInfo["Name"] as! String)\n")
  335. break;
  336. }
  337. }
  338. }else {
  339. LogViewController.shared().appendLog("- \(pv_propertyName): \(value as! String)\n")
  340. }
  341. }else if ((["CGSize", "NSSize"] as NSArray).contains(pv_ValueType)) {
  342. if (pv_Values != nil) {
  343. for vi in pv_Values! {
  344. let valueInfo = vi as! NSDictionary
  345. if (valueInfo["Value"] as! NSString).isEqual(to: (value as! String)) {
  346. LogViewController.shared().appendLog("- \(pv_propertyName): \(valueInfo["Name"] as! String)\n")
  347. break;
  348. }
  349. }
  350. }else {
  351. LogViewController.shared().appendLog("- \(pv_propertyName): \(value as! String)\n")
  352. }
  353. }else {
  354. LogViewController.shared().appendLog("- \(pv_propertyName): \((value as? String) ?? "")\n")
  355. }
  356. }
  357. }
  358. }
  359. _popover.close()
  360. }
  361. // NSPopoverDelegate
  362. func popoverShouldClose(_ popover: NSPopover) -> Bool {
  363. return true
  364. }
  365. //Show Alert
  366. func showAlert(_ msg:String) {
  367. let alert = NSAlert()
  368. alert.messageText = msg
  369. alert.runModal()
  370. }
  371. }
  372. extension AdvanceSettingViewController {
  373. class func baseInfoFor(_ type:String) -> NSDictionary? {
  374. let path = Bundle.main.path(forResource: "AdvanceSettingPropertyRules", ofType: "plist")
  375. let asPropertys = try? NSDictionary.init(contentsOfFile: path!)
  376. if asPropertys != nil {
  377. return asPropertys?.value(forKey: type) as? NSDictionary
  378. }
  379. return nil
  380. }
  381. }