RuleFile.qml 6.6 KB

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