main.qml 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. color: "#fffffe"
  11. WebSocket {
  12. property int total_slides: 5
  13. id: socket
  14. url: "wss://wscasa.especificosba.com.ar"
  15. onTextMessageReceived: {
  16. // console.log("\nReceived message: " + message)
  17. var obj = JSON.parse(message)
  18. switch (obj.type) {
  19. case "diapo":
  20. canvas.clean_path()
  21. item1.curPDF = obj.PDF
  22. item1.curImage = obj.slide //automagically change_image
  23. total_slides = obj.total_slides
  24. }
  25. }
  26. function send_obj(obj) {
  27. sendTextMessage(JSON.stringify(obj))
  28. }
  29. function prev_diapo() {
  30. var curImage = Math.max(item1.curImage - 1, 1)
  31. send_obj({
  32. type: "command",
  33. commandtype: "diapo",
  34. PDF: item1.curPDF,
  35. slide: curImage
  36. })
  37. }
  38. function next_diapo() {
  39. var curImage = Math.min(item1.curImage + 1, total_slides)
  40. send_obj({
  41. type: "command",
  42. commandtype: "diapo",
  43. PDF: item1.curPDF,
  44. slide: curImage
  45. })
  46. }
  47. function clear_path() {
  48. send_obj({
  49. type: "command",
  50. commandtype: "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: {
  63. //FIXME implement
  64. if (socket.status == WebSocket.Error) {
  65. console.log("Error: " + socket.errorString)
  66. } else if (socket.status == WebSocket.Open) {
  67. /*?*/
  68. } else if (socket.status == WebSocket.Closed) {
  69. console.log("\nSocket closed")
  70. }
  71. }
  72. active: true
  73. }
  74. Item {
  75. id: item1
  76. width: parent.width
  77. height: parent.height
  78. property alias mouseArea1: mouseArea1
  79. property alias button2: button2
  80. property alias button3: button3
  81. property alias button1: button1
  82. property int curImage: 1
  83. property string curPDF: "EBA"
  84. anchors.fill: parent
  85. onCurPDFChanged: {
  86. canvas.change_image(curPDF, curImage)
  87. }
  88. onCurImageChanged: {
  89. canvas.change_image(curPDF, curImage)
  90. }
  91. Grid {
  92. id: grid1
  93. width: parent.width
  94. height: parent.height
  95. anchors.right: parent.right
  96. anchors.bottom: parent.bottom
  97. anchors.top: parent.top
  98. anchors.left: parent.left
  99. columns: 4
  100. spacing: 4
  101. Rectangle {
  102. id: canvasCol
  103. height: parent.height
  104. width: height * 4 / 3
  105. color: aw.color
  106. Canvas {
  107. id: canvas
  108. antialiasing: true
  109. renderTarget: Canvas.Image
  110. property color strokeStyle: Qt.darker(fillStyle, 1.2)
  111. property color fillStyle: "#6400aa"
  112. property string curColor: "#ff0000"
  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. onImageLoaded: {
  122. clean_path()
  123. requestPaint()
  124. }
  125. function clean_path() {
  126. points_array = []
  127. requestPaint()
  128. }
  129. function start_path() {
  130. points_array.push({points:[],color:curColor})
  131. }
  132. function finish_path() {
  133. points_array[points_array.length - 1].points = Simplify.simplify(
  134. points_array[points_array.length - 1].points, 2.2)
  135. requestPaint()
  136. //normalized to 800x600
  137. var ar = points_array[points_array.length - 1].points.map(
  138. function (el) {
  139. return {
  140. x: el.x * 800 / canvas.width,
  141. y: el.y * 600 / canvas.height
  142. }
  143. })
  144. while (ar.length > 30) {
  145. socket.send_drawing(ar.splice(0,29));
  146. }
  147. socket.send_drawing(ar);
  148. }
  149. function change_image(pdf, number) {
  150. curImageUrl = "https://plataforma.especificosba.com.ar/diapo/"
  151. + pdf + "/" + number + ".png"
  152. requestPaint()
  153. }
  154. function add_point(p) {
  155. points_array[points_array.length - 1].points.push(p);
  156. if (points_array[points_array.length - 1].length/ 3 > 40){ //FIXME
  157. finish_path()
  158. start_path()
  159. points_array[points_array.length - 1].points.push(p)
  160. } else {
  161. requestPaint()
  162. }
  163. }
  164. function paint_canvas() {
  165. var ctx = canvas.getContext("2d")
  166. canvas.loadImage(curImageUrl)
  167. if (canvas.isImageLoaded(curImageUrl)) {
  168. ctx.drawImage(curImageUrl, 0, 0, canvas.width,
  169. canvas.height)
  170. }
  171. ctx.lineCap = 'square'
  172. ctx.lineWidth = 3
  173. ctx.strokeStyle = "#000000"
  174. var i = 0, j = 0
  175. for (j = 0; j < points_array.length; j++) {
  176. var points = points_array[j].points;
  177. ctx.strokeStyle= points_array[j].color;
  178. ctx.beginPath()
  179. ctx.moveTo(points[0].x, points[0].y)
  180. for (i = 0; i < points.length - 1; i++) {
  181. ctx.moveTo(points[i].x, points[i].y)
  182. ctx.lineTo(points[i + 1].x, points[i + 1].y)
  183. }
  184. ctx.stroke()
  185. }
  186. }
  187. }
  188. MouseArea {
  189. id: mouseArea1
  190. x: 0
  191. y: 0
  192. width: canvas.width
  193. height: canvas.height
  194. onPressed: {
  195. if (!socket.active)
  196. return
  197. canvas.start_path()
  198. }
  199. onPositionChanged: {
  200. if (!socket.active)
  201. return
  202. canvas.add_point({
  203. x: mouse.x,
  204. y: mouse.y
  205. })
  206. }
  207. onReleased: {
  208. if (!socket.active)
  209. return
  210. canvas.finish_path()
  211. }
  212. }
  213. }
  214. Column {
  215. id: columnLayout1
  216. spacing: 10
  217. width: parent.width - canvasCol.width - parent.spacing * 2
  218. height: parent.height
  219. Column {
  220. width: parent.width
  221. height: parent.height/2
  222. Button {
  223. width: parent.width
  224. height: 70
  225. id: button1
  226. text: qsTr("Anterior")
  227. enabled: socket.active
  228. onClicked: socket.prev_diapo()
  229. }
  230. Button {
  231. height: 70
  232. width: parent.width
  233. id: button2
  234. text: qsTr("Siguiente")
  235. enabled: socket.active
  236. onClicked: socket.next_diapo()
  237. }
  238. Button {
  239. height: 70
  240. width: parent.width
  241. id: button3
  242. text: qsTr("Limpiar")
  243. enabled: socket.active
  244. onClicked: socket.clear_path()
  245. }
  246. }
  247. Column {
  248. width: parent.width
  249. height: parent.height/2
  250. Button {
  251. width: parent.width
  252. height: 70
  253. id: color1
  254. text: qsTr("Rojo")
  255. onClicked: canvas.curColor="#ff0000"
  256. }
  257. Button {
  258. height: 70
  259. width: parent.width
  260. id: color2
  261. text: qsTr("Verde")
  262. onClicked: canvas.curColor="#00ff00"
  263. }
  264. Button {
  265. height: 70
  266. width: parent.width
  267. id: color3
  268. text: qsTr("Azul")
  269. onClicked: canvas.curColor="#0000ff"
  270. }
  271. }
  272. }
  273. }
  274. }
  275. }