main.qml 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import QtQuick 2.5
  2. import QtQuick.Controls 1.4
  3. import QtWebSockets 1.0
  4. import "./simplify.js" as Simplify
  5. ApplicationWindow {
  6. id: aw
  7. visible: true
  8. width: 800
  9. height: 480
  10. title: qsTr("Hello World")
  11. color: "#fffffe"
  12. WebSocket {
  13. property int total_slides: 5
  14. id: socket
  15. url: "wss://wscasa.especificosba.com.ar"
  16. onTextMessageReceived: {
  17. console.log("\nReceived message: " + message)
  18. var obj = JSON.parse(message);
  19. switch(obj.type){
  20. case "diapo":
  21. canvas.clean_path();
  22. item1.curPDF=obj.PDF;
  23. item1.curImage=obj.slide; //automagically change_image
  24. total_slides=obj.total_slides;
  25. }
  26. }
  27. function send_obj(obj){
  28. sendTextMessage(JSON.stringify(obj));
  29. }
  30. function prev_diapo(){
  31. var curImage= Math.max(item1.curImage - 1, 1)
  32. send_obj({type:"command",commandtype:"diapo", PDF: item1.curPDF, slide: curImage});
  33. }
  34. function next_diapo(){
  35. var curImage= Math.min(item1.curImage + 1, total_slides)
  36. send_obj({type:"command",commandtype:"diapo", PDF: item1.curPDF, slide: curImage});
  37. }
  38. function clear_path(){
  39. send_obj({type:"clean"})
  40. canvas.clean_path()
  41. }
  42. onStatusChanged: if (socket.status == WebSocket.Error) {
  43. console.log("Error: " + socket.errorString)
  44. } else if (socket.status == WebSocket.Open) {
  45. socket.sendTextMessage("Hello World")
  46. } else if (socket.status == WebSocket.Closed) {
  47. console.log("\nSocket closed")
  48. }
  49. active: true
  50. }
  51. Item {
  52. id: item1
  53. width: parent.width
  54. height: parent.height
  55. //property alias gridLayout1: gridLayout1
  56. property alias button4: button4
  57. property alias mouseArea1: mouseArea1
  58. property alias button2: button2
  59. property alias button3: button3
  60. property alias button1: button1
  61. property int curImage: 1
  62. property string curPDF: "EBA"
  63. anchors.fill: parent
  64. onCurPDFChanged: {
  65. canvas.change_image(curPDF, curImage)
  66. }
  67. onCurImageChanged: {
  68. canvas.change_image(curPDF, curImage)
  69. }
  70. Grid {
  71. id: grid1
  72. width: parent.width
  73. height: parent.height
  74. anchors.right: parent.right
  75. anchors.bottom: parent.bottom
  76. anchors.top: parent.top
  77. anchors.left: parent.left
  78. columns: 4
  79. spacing: 4
  80. Rectangle {
  81. id: canvasCol
  82. height: parent.height
  83. width: height*4/3
  84. color: aw.color
  85. Canvas {
  86. id: canvas
  87. antialiasing: true
  88. renderTarget: Canvas.Image
  89. property color strokeStyle: Qt.darker(fillStyle, 1.2)
  90. property color fillStyle: "#6400aa"
  91. property int lineWidth: 3
  92. property real px: width / 2
  93. property real py: height / 2 + 10
  94. property real alpha: 1.0
  95. property variant points_array: []
  96. property string curImageUrl: "https://plataforma.especificosba.com.ar/diapo/EBA/1.png"
  97. width: parent.width
  98. height: parent.height
  99. onPaint: paint_canvas()
  100. onLineWidthChanged: requestPaint()
  101. onImageLoaded: {
  102. clean_path()
  103. requestPaint()
  104. }
  105. function clean_path() {
  106. points_array = []
  107. requestPaint()
  108. //ws.send()..
  109. }
  110. function start_path() {
  111. points_array.push([])
  112. }
  113. function finish_path() {
  114. points_array[points_array.length - 1] = Simplify.simplify(
  115. points_array[points_array.length - 1], 3)
  116. requestPaint()
  117. //ws.send()..
  118. }
  119. function change_image(pdf, number) {
  120. curImageUrl = "https://plataforma.especificosba.com.ar/diapo/"
  121. + pdf + "/" + number + ".png"
  122. requestPaint()
  123. }
  124. function add_point(p) {
  125. points_array[points_array.length - 1].push(p)
  126. requestPaint()
  127. }
  128. function paint_canvas() {
  129. var curColor = '#ff0000'
  130. var ctx = canvas.getContext("2d")
  131. canvas.loadImage(curImageUrl)
  132. if (canvas.isImageLoaded(curImageUrl)) {
  133. ctx.drawImage(curImageUrl, 0, 0, canvas.width,
  134. canvas.height)
  135. }
  136. ctx.lineCap = 'square'
  137. ctx.lineWidth = 2
  138. ctx.strokeStyle = curColor
  139. ctx.beginPath()
  140. var i = 0, j = 0
  141. for (j = 0; j < points_array.length; j++) {
  142. var points = points_array[j]
  143. ctx.moveTo(points[0].x, points[0].y)
  144. for (i = 0; i < points.length - 1; i++) {
  145. ctx.moveTo(points[i].x, points[i].y)
  146. ctx.lineTo(points[i + 1].x, points[i + 1].y)
  147. }
  148. }
  149. ctx.stroke()
  150. }
  151. }
  152. MouseArea {
  153. id: mouseArea1
  154. x: 0
  155. y: 0
  156. width: canvas.width
  157. height: canvas.height
  158. onPressed: {
  159. if (!socket.active)
  160. return
  161. canvas.start_path()
  162. }
  163. onPositionChanged: {
  164. if (!socket.active)
  165. return
  166. canvas.add_point({
  167. x: mouse.x,
  168. y: mouse.y
  169. })
  170. }
  171. onReleased: {
  172. if (!socket.active)
  173. return
  174. canvas.finish_path()
  175. }
  176. }
  177. }
  178. Column {
  179. id: columnLayout1
  180. spacing: 15
  181. width: parent.width - canvasCol.width - parent.spacing*2
  182. Button {
  183. width:parent.width
  184. height:80
  185. id: button2
  186. text: qsTr("Anterior")
  187. enabled: socket.active
  188. onClicked: socket.prev_diapo()
  189. }
  190. Button {
  191. height:80
  192. width:parent.width
  193. id: button1
  194. text: qsTr("Siguiente")
  195. enabled: socket.active
  196. onClicked: socket.next_diapo()
  197. }
  198. Button {
  199. height:80
  200. width:parent.width
  201. id: button4
  202. text: qsTr("Limpiar")
  203. enabled: socket.active
  204. onClicked: socket.clear_path()
  205. }
  206. Button {
  207. height:80
  208. width:parent.width
  209. id: button3
  210. text: qsTr("Break")
  211. isDefault: false
  212. checkable: false
  213. onClicked: canvas.paint_canvas()
  214. }
  215. }
  216. }
  217. }
  218. }