main.qml 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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({
  33. type: "command",
  34. commandtype: "diapo",
  35. PDF: item1.curPDF,
  36. slide: curImage
  37. })
  38. }
  39. function next_diapo() {
  40. var curImage = Math.min(item1.curImage + 1, total_slides)
  41. send_obj({
  42. type: "command",
  43. commandtype: "diapo",
  44. PDF: item1.curPDF,
  45. slide: curImage
  46. })
  47. }
  48. function clear_path() {
  49. send_obj({
  50. type: "clean"
  51. })
  52. canvas.clean_path()
  53. }
  54. function send_drawing(d) {
  55. send_obj({
  56. type: "command",
  57. commandtype: "draw",
  58. points: d,
  59. color: canvas.curColor
  60. })
  61. }
  62. onStatusChanged: if (socket.status == WebSocket.Error) {
  63. console.log("Error: " + socket.errorString)
  64. } else if (socket.status == WebSocket.Open) {
  65. socket.sendTextMessage("Hello World")
  66. } else if (socket.status == WebSocket.Closed) {
  67. console.log("\nSocket closed")
  68. }
  69. active: true
  70. }
  71. Item {
  72. id: item1
  73. width: parent.width
  74. height: parent.height
  75. //property alias gridLayout1: gridLayout1
  76. property alias button4: button4
  77. property alias mouseArea1: mouseArea1
  78. property alias button2: button2
  79. property alias button3: button3
  80. property alias button1: button1
  81. property int curImage: 1
  82. property string curPDF: "EBA"
  83. anchors.fill: parent
  84. onCurPDFChanged: {
  85. canvas.change_image(curPDF, curImage)
  86. }
  87. onCurImageChanged: {
  88. canvas.change_image(curPDF, curImage)
  89. }
  90. Grid {
  91. id: grid1
  92. width: parent.width
  93. height: parent.height
  94. anchors.right: parent.right
  95. anchors.bottom: parent.bottom
  96. anchors.top: parent.top
  97. anchors.left: parent.left
  98. columns: 4
  99. spacing: 4
  100. Rectangle {
  101. id: canvasCol
  102. height: parent.height
  103. width: height * 4 / 3
  104. color: aw.color
  105. Canvas {
  106. id: canvas
  107. antialiasing: true
  108. renderTarget: Canvas.Image
  109. property color strokeStyle: Qt.darker(fillStyle, 1.2)
  110. property color fillStyle: "#6400aa"
  111. property string curColor: "#ff0000"
  112. property int lineWidth: 3
  113. property real px: width / 2
  114. property real py: height / 2 + 10
  115. property real alpha: 1.0
  116. property variant points_array: []
  117. property string curImageUrl: "https://plataforma.especificosba.com.ar/diapo/EBA/1.png"
  118. width: parent.width
  119. height: parent.height
  120. onPaint: paint_canvas()
  121. onLineWidthChanged: requestPaint()
  122. onImageLoaded: {
  123. clean_path()
  124. requestPaint()
  125. }
  126. function clean_path() {
  127. points_array = []
  128. requestPaint()
  129. //ws.send()..
  130. }
  131. function start_path() {
  132. points_array.push([])
  133. }
  134. function finish_path() {
  135. points_array[points_array.length - 1] = Simplify.simplify(
  136. points_array[points_array.length - 1], 2.2)
  137. requestPaint()
  138. //normalized to 800x600
  139. var ar = points_array[points_array.length - 1].map(
  140. function(el){
  141. return {x: el.x*800/canvas.width, y:el.y*600/canvas.height}
  142. }
  143. );
  144. socket.send_drawing(ar)
  145. }
  146. function change_image(pdf, number) {
  147. curImageUrl = "https://plataforma.especificosba.com.ar/diapo/"
  148. + pdf + "/" + number + ".png"
  149. requestPaint()
  150. }
  151. function add_point(p) {
  152. points_array[points_array.length - 1].push(p)
  153. requestPaint()
  154. }
  155. function paint_canvas() {
  156. var curColor = '#ff0000'
  157. var ctx = canvas.getContext("2d")
  158. canvas.loadImage(curImageUrl)
  159. if (canvas.isImageLoaded(curImageUrl)) {
  160. ctx.drawImage(curImageUrl, 0, 0, canvas.width,
  161. canvas.height)
  162. }
  163. ctx.lineCap = 'square'
  164. ctx.lineWidth = 2
  165. ctx.strokeStyle = curColor
  166. ctx.beginPath()
  167. var i = 0, j = 0
  168. for (j = 0; j < points_array.length; j++) {
  169. var points = points_array[j]
  170. ctx.moveTo(points[0].x, points[0].y)
  171. for (i = 0; i < points.length - 1; i++) {
  172. ctx.moveTo(points[i].x, points[i].y)
  173. ctx.lineTo(points[i + 1].x, points[i + 1].y)
  174. }
  175. }
  176. ctx.stroke()
  177. }
  178. }
  179. MouseArea {
  180. id: mouseArea1
  181. x: 0
  182. y: 0
  183. width: canvas.width
  184. height: canvas.height
  185. onPressed: {
  186. if (!socket.active)
  187. return
  188. canvas.start_path()
  189. }
  190. onPositionChanged: {
  191. if (!socket.active)
  192. return
  193. canvas.add_point({
  194. x: mouse.x,
  195. y: mouse.y
  196. })
  197. }
  198. onReleased: {
  199. if (!socket.active)
  200. return
  201. canvas.finish_path()
  202. }
  203. }
  204. }
  205. Column {
  206. id: columnLayout1
  207. spacing: 15
  208. width: parent.width - canvasCol.width - parent.spacing * 2
  209. Button {
  210. width: parent.width
  211. height: 80
  212. id: button2
  213. text: qsTr("Anterior")
  214. enabled: socket.active
  215. onClicked: socket.prev_diapo()
  216. }
  217. Button {
  218. height: 80
  219. width: parent.width
  220. id: button1
  221. text: qsTr("Siguiente")
  222. enabled: socket.active
  223. onClicked: socket.next_diapo()
  224. }
  225. Button {
  226. height: 80
  227. width: parent.width
  228. id: button4
  229. text: qsTr("Limpiar")
  230. enabled: socket.active
  231. onClicked: socket.clear_path()
  232. }
  233. Button {
  234. height: 80
  235. width: parent.width
  236. id: button3
  237. text: qsTr("Break")
  238. isDefault: false
  239. checkable: false
  240. onClicked: canvas.paint_canvas()
  241. }
  242. }
  243. }
  244. }
  245. }