ImageRule.qml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import QtQuick
  2. import QtQuick.Controls
  3. import FluentUI
  4. import "../Component"
  5. Item {
  6. property alias aItemName: itemName.text
  7. property alias aImageName: imageName.text
  8. property alias aRoiFront: roiFront.text
  9. property alias aRoiBack: roiBack.text
  10. property alias aMethod: method.currentText
  11. property alias aThreshold: threshold.text
  12. property alias aDescription: description.text
  13. MirrorImage{
  14. id: mirrorImage
  15. anchors{
  16. left: parent.left
  17. top: parent.top
  18. }
  19. width: 1280
  20. height: 720 + 50
  21. imageWidth: 1280
  22. imageHeight: 720
  23. onRoi_red_changed: function(roi){
  24. if(typeof ruleFile.currentItem === "undefined"){
  25. return
  26. }
  27. ruleFile.currentItem.roiFront = roi
  28. }
  29. onRoi_green_changed: function(roi){
  30. if(typeof ruleFile.currentItem === "undefined"){
  31. return
  32. }
  33. ruleFile.currentItem.roiBack = roi
  34. }
  35. }
  36. RuleFile{
  37. id: ruleFile
  38. anchors{
  39. left: mirrorImage.right
  40. leftMargin: 10
  41. right: parent.right
  42. top: parent.top
  43. }
  44. height: 600
  45. addFunc: add
  46. editFunc: edit
  47. saveFunc: save
  48. onCurrentItemChanged: {
  49. if(currentItem === "" || typeof currentItem === "undefined"){
  50. return
  51. }
  52. var [x, y, width, height] = currentItem.roiFront.split(',')
  53. mirrorImage.roiRed_x = parseInt(x)
  54. mirrorImage.roiRed_y = parseInt(y)
  55. mirrorImage.roiRed_width = parseInt(width)
  56. mirrorImage.roiRed_height = parseInt(height)
  57. var [x, y, width, height] = currentItem.roiBack.split(',')
  58. mirrorImage.roiGreen_x = parseInt(x)
  59. mirrorImage.roiGreen_y = parseInt(y)
  60. mirrorImage.roiGreen_width = parseInt(width)
  61. mirrorImage.roiGreen_height = parseInt(height)
  62. }
  63. }
  64. FluArea {
  65. id: ruleArea
  66. anchors{
  67. left: mirrorImage.right
  68. leftMargin: 10
  69. right: parent.right
  70. top: ruleFile.bottom
  71. topMargin: 12
  72. bottom: parent.bottom
  73. }
  74. Column{
  75. id: coi
  76. anchors{
  77. top: parent.top
  78. bottom: parent.bottom
  79. left: parent.left
  80. leftMargin: 20
  81. right: parent.right
  82. rightMargin: 20
  83. }
  84. spacing: 5
  85. //头
  86. Item{
  87. width: coi.width
  88. height: 40
  89. FluText{
  90. anchors.left: parent.left
  91. anchors.verticalCenter: parent.verticalCenter
  92. text: "Editing rule"
  93. font: FluTextStyle.Subtitle
  94. }
  95. }
  96. //item name 这个参数项的名称
  97. Item{
  98. width: coi.width
  99. height: 40
  100. FluText{
  101. anchors.left: parent.left
  102. anchors.verticalCenter: parent.verticalCenter
  103. text: "Item name"
  104. font: FluTextStyle.BodyStrong
  105. }
  106. FluTextBox{
  107. id: itemName
  108. anchors{
  109. right: parent.right
  110. verticalCenter: parent.verticalCenter
  111. }
  112. placeholderText:"单行输入框"
  113. disabled: true
  114. text: (typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.itemName
  115. width: 200
  116. }
  117. }
  118. // 文件名
  119. Item{
  120. width: coi.width
  121. height: 40
  122. FluText{
  123. anchors.left: parent.left
  124. anchors.verticalCenter: parent.verticalCenter
  125. text: "Image name"
  126. font: FluTextStyle.BodyStrong
  127. }
  128. FluTextBox{
  129. id: imageName
  130. anchors{
  131. right: parent.right
  132. verticalCenter: parent.verticalCenter
  133. }
  134. disabled: true
  135. text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.imageName
  136. placeholderText:"Save image name"
  137. width: 200
  138. }
  139. }
  140. // roi front
  141. Item{
  142. width: coi.width
  143. height: 40
  144. FluText{
  145. anchors.left: parent.left
  146. anchors.verticalCenter: parent.verticalCenter
  147. text: "Roi front"
  148. font: FluTextStyle.BodyStrong
  149. }
  150. FluTextBox{
  151. id: roiFront
  152. anchors{
  153. right: parent.right
  154. verticalCenter: parent.verticalCenter
  155. }
  156. disabled: true
  157. text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.roiFront
  158. placeholderText:"0,0,100,100"
  159. width: 200
  160. }
  161. }
  162. //roi back
  163. Item{
  164. width: coi.width
  165. height: 40
  166. FluText{
  167. anchors.left: parent.left
  168. anchors.verticalCenter: parent.verticalCenter
  169. text: "Roi Back"
  170. font: FluTextStyle.BodyStrong
  171. }
  172. FluTextBox{
  173. id: roiBack
  174. anchors{
  175. right: parent.right
  176. verticalCenter: parent.verticalCenter
  177. }
  178. disabled: true
  179. text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.roiBack
  180. placeholderText:"0,0,100,100"
  181. width: 200
  182. }
  183. }
  184. //找图的方法
  185. Item{
  186. width: coi.width
  187. height: 40
  188. FluText{
  189. anchors.left: parent.left
  190. anchors.verticalCenter: parent.verticalCenter
  191. text: "Find method"
  192. font: FluTextStyle.BodyStrong
  193. }
  194. FComboBox{
  195. id: method
  196. anchors{
  197. right: parent.right
  198. verticalCenter: parent.verticalCenter
  199. }
  200. width: 200
  201. currentIndex: 0
  202. model: ["Template matching"]
  203. onActivated: {
  204. }
  205. }
  206. }
  207. //阈值
  208. Item{
  209. width: coi.width
  210. height: 40
  211. FluText{
  212. anchors.left: parent.left
  213. anchors.verticalCenter: parent.verticalCenter
  214. text: "Threshod"
  215. font: FluTextStyle.BodyStrong
  216. }
  217. FluTextBox{
  218. id: threshold
  219. anchors{
  220. right: parent.right
  221. verticalCenter: parent.verticalCenter
  222. }
  223. width: 200
  224. text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.threshold
  225. placeholderText:"0.8"
  226. onEditingFinished: {
  227. if(typeof ruleFile.currentItem === "undefined"){
  228. return
  229. }
  230. ruleFile.currentItem.threshold = parseFloat(text)
  231. }
  232. }
  233. }
  234. //描述 description
  235. Item{
  236. width: coi.width
  237. height: 40
  238. FluText{
  239. anchors.left: parent.left
  240. anchors.verticalCenter: parent.verticalCenter
  241. text: "Description"
  242. font: FluTextStyle.BodyStrong
  243. }
  244. FluTextBox{
  245. id: description
  246. anchors{
  247. right: parent.right
  248. verticalCenter: parent.verticalCenter
  249. }
  250. width: 200
  251. text:(typeof ruleFile.currentItem === "undefined")? "" : ruleFile.currentItem.description
  252. placeholderText:"input your description"
  253. onEditingFinished: {
  254. if(typeof ruleFile.currentItem === "undefined"){
  255. return
  256. }
  257. ruleFile.currentItem.description = text
  258. }
  259. }
  260. }
  261. }
  262. }
  263. // 帮助
  264. FluArea{
  265. id: ruleHelp
  266. anchors{
  267. left: parent.left
  268. right: ruleArea.left
  269. rightMargin: 10
  270. top: mirrorImage.bottom
  271. topMargin: 10
  272. bottom: parent.bottom
  273. }
  274. FluText{
  275. leftPadding: 12
  276. topPadding: 12
  277. text: "红色的框(Roi front)表示这一个项要匹配的图片,绿色的框(Roi back)表示匹配的范围。
  278. 在移动边框时右边的roi并不会实时显示,当切换软件的焦点时更新显示。但是具体的数值是更新的,可以放心的保存截图"
  279. }
  280. FluFilledButton{
  281. anchors{
  282. right: parent.right
  283. rightMargin: 10
  284. top: parent.top
  285. topMargin: 10
  286. }
  287. text:"Save image"
  288. onClicked: {
  289. const i = ruleFile.file.lastIndexOf("/")
  290. const imagepath = ruleFile.file.substring(0, i)
  291. mirrorImage.save_target_image(ruleFile.currentItem.roiFront, imagepath +"/"+ruleFile.currentItem.imageName)
  292. showSuccess("Save target image")
  293. }
  294. }
  295. }
  296. function add(){
  297. const item ={}
  298. item["itemName"] = "new"
  299. item["imageName"] = "image_name.png"
  300. item["roiFront"] = "0,0,100,100"
  301. item["roiBack"] = "0,0,100,100"
  302. item["method"] = "Template matching"
  303. item["threshold"] = 0.8
  304. item["description"] = "description"
  305. showSuccess("Add new item")
  306. return item
  307. }
  308. function edit(model){
  309. model.imageName = ruleFile.folder +"_"+ model.itemName + ".png"
  310. }
  311. function save(){
  312. const data = []
  313. for(var i=0; i<ruleFile.list_model.count; i++){
  314. const item = ruleFile.list_model.get(i)
  315. const itemData = {}
  316. itemData["itemName"] = item["itemName"]
  317. itemData["imageName"] = item["imageName"]
  318. itemData["roiFront"] = item["roiFront"]
  319. itemData["roiBack"] = item["roiBack"]
  320. itemData["method"] = item["method"]
  321. itemData["threshold"] = item["threshold"]
  322. itemData["description"] = item["description"]
  323. data.push(itemData)
  324. }
  325. ruleFile.rule_file.write_file(ruleFile.file, JSON.stringify(data))
  326. showSuccess("Save file")
  327. }
  328. }