RuleFile2.qml 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import QtQuick
  2. import QtQuick.Controls
  3. import FluentUI
  4. import QtQuick.Dialogs
  5. import Oas
  6. FluArea {
  7. id: root
  8. property string file: "" //完整的文件路径带后缀
  9. property alias folder: folderButton.text
  10. property alias list_model: listModel
  11. property alias rule_file: ruleFile
  12. property var currentItem: listModel.get(0)
  13. property var addFunc: function(){}
  14. property var editFunc: function(){}
  15. property var saveFunc: function(){}
  16. // 这些是针对ruleList的
  17. property string name: ""
  18. property string direction: ""
  19. property string type: ""
  20. property string roiBack: ""
  21. property string description: ""
  22. ListModel{
  23. id: listModel
  24. }
  25. RuleFile{
  26. id: ruleFile
  27. }
  28. Column{
  29. id: coi
  30. anchors{
  31. top: parent.top
  32. // topMargin: 10
  33. bottom: parent.bottom
  34. left: parent.left
  35. leftMargin: 20
  36. right: parent.right
  37. rightMargin: 20
  38. }
  39. spacing: 5
  40. // 头
  41. Item{
  42. width: coi.width
  43. height: 40
  44. FluText{
  45. anchors.left: parent.left
  46. anchors.verticalCenter: parent.verticalCenter
  47. text: "Select rule"
  48. font: FluTextStyle.BodyStrong
  49. }
  50. }
  51. //路径
  52. Item{
  53. width: coi.width
  54. height: 40
  55. FluText{
  56. anchors.left: parent.left
  57. anchors.verticalCenter: parent.verticalCenter
  58. text: "Folder"
  59. font: FluTextStyle.BodyStrong
  60. }
  61. FluButton{
  62. id: folderButton
  63. anchors{
  64. right: parent.right
  65. verticalCenter: parent.verticalCenter
  66. }
  67. disabled: true
  68. text:"Select folder"
  69. width: 200
  70. onClicked: {
  71. }
  72. }
  73. }
  74. // 文件名
  75. Item{
  76. width: coi.width
  77. height: 40
  78. FluText{
  79. anchors.left: parent.left
  80. anchors.verticalCenter: parent.verticalCenter
  81. text: "File"
  82. font: FluTextStyle.BodyStrong
  83. }
  84. FluButton{
  85. id: fileButton
  86. anchors{
  87. right: parent.right
  88. verticalCenter: parent.verticalCenter
  89. }
  90. text:"Select file"
  91. width: 200
  92. onClicked: {
  93. jsonFileDialog.open()
  94. }
  95. }
  96. FileDialog{
  97. id: jsonFileDialog
  98. title: "select your json file"
  99. nameFilters: ["(*.json)"]
  100. onAccepted: {
  101. const selectedUrl = jsonFileDialog.selectedFile.toString()
  102. if(!selectedUrl.startsWith("file:///")){
  103. console.error("请你选择正确的文件路径")
  104. return
  105. }
  106. const filePath = selectedUrl.substring("file:///".length)
  107. const lastSlashIndex = filePath.lastIndexOf("/"); // 获取最后一个斜杠的索引
  108. const secondLastSlashIndex = filePath.lastIndexOf("/", lastSlashIndex - 1); // 获取倒数第二个斜杠的索引
  109. const filename = filePath.substring(lastSlashIndex + 1); // 获取文件名(带后缀)
  110. const directory = filePath.substring(secondLastSlashIndex+1, lastSlashIndex); // 获取文件所在的目录
  111. root.file = filePath
  112. folderButton.text = directory
  113. fileButton.text = filename
  114. listModel.clear()
  115. loadFile()
  116. }
  117. }
  118. }
  119. // new
  120. Item{
  121. width: coi.width
  122. height: 40
  123. FluFilledButton{
  124. id: saveItemButton
  125. anchors{
  126. right: parent.right
  127. verticalCenter: parent.verticalCenter
  128. }
  129. text:"New"
  130. disabled: root.file === ""? true : false
  131. width: parent.width
  132. onClicked: {
  133. listModel.append(addFunc())
  134. }
  135. }
  136. }
  137. // 保存 文件
  138. Item{
  139. width: coi.width
  140. height: 40
  141. FluFilledButton{
  142. id: saveFileButton
  143. anchors{
  144. right: parent.right
  145. verticalCenter: parent.verticalCenter
  146. }
  147. text:"Save file"
  148. width: parent.width
  149. disabled: root.file === ""? true : false
  150. onClicked: {
  151. root.saveFunc()
  152. }
  153. }
  154. }
  155. //表格视图
  156. Item{
  157. width: coi.width
  158. height: 370
  159. ListView{
  160. id: listView
  161. width: 200
  162. height: parent.height
  163. anchors{
  164. right: parent.right
  165. }
  166. model: listModel
  167. delegate: listDelegate
  168. }
  169. Component{
  170. id: listDelegate
  171. Item{
  172. width: 200
  173. height: 40
  174. FluTextBox{
  175. id: itemName
  176. width: parent.width
  177. anchors{
  178. verticalCenter: parent.verticalCenter
  179. }
  180. placeholderText:"item name"
  181. text: model.itemName
  182. // validator: RegularExpressionValidator { regularExpression: /^[a-z0-9_]*$/ }
  183. onActiveFocusChanged: {
  184. if(activeFocus){
  185. currentItem = listModel.get(index)
  186. }
  187. }
  188. onEditingFinished: {
  189. model.itemName = text
  190. root.editFunc(model)
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. // 加载文件,前提是文件的路径是加载好的
  198. function loadFile(){
  199. const getData = ruleFile.read_file(file)
  200. var data = {}
  201. if (getData === "" || typeof getData === "undefined") {
  202. console.log("String is empty or undefined.")
  203. // 如果是空的表示这个是一个刚刚建立的rule文件
  204. data["name"] = "name"
  205. data["direction"] = "vertical"
  206. data["type"] = "image"
  207. data["roiBack"] = "0,0,100,100"
  208. data["description"] = "description"
  209. data["list"] = []
  210. }else{
  211. data = JSON.parse(getData)
  212. }
  213. if (!'name' in data){
  214. return
  215. }
  216. if(!'list' in data){
  217. return
  218. }
  219. root.name = data["name"]
  220. root.direction = data["direction"]
  221. root.type = data["type"]
  222. root.roiBack = data["roiBack"]
  223. root.description = data["description"]
  224. for(let item of data["list"]){
  225. listModel.append(item)
  226. }
  227. }
  228. }