main.qml 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. import QtQuick 2.5
  2. import QtQuick.Controls 1.4
  3. import QtQuick.Controls.Styles 1.4
  4. import QtWebSockets 1.0
  5. import "./simplify.js" as Simplify
  6. ApplicationWindow {
  7. id: aw
  8. visible: true
  9. width: 960
  10. height: 540
  11. color: "#fffffe"
  12. property alias canvas: canvas
  13. Timer {
  14. interval: 800; running: true; repeat: true
  15. onTriggered: {
  16. if (socket.status != WebSocket.Open){//Reconnect
  17. socket.active = false
  18. socket.active = true
  19. console.log(Date().toString())
  20. }
  21. }
  22. }
  23. WebSocket {
  24. property int total_slides: 5
  25. id: socket
  26. url: "wss://wscasa.especificosba.com.ar"
  27. onTextMessageReceived: {
  28. var obj = JSON.parse(message)
  29. switch (obj.type) {
  30. case "diapo":
  31. canvas.clean_path()
  32. item1.curPDF = obj.PDF
  33. item1.curImage = obj.slide //automagically change_image
  34. if (obj.total_slides === undefined)
  35. total_slides = 3
  36. else
  37. total_slides = obj.total_slides
  38. break
  39. case "preg":
  40. item1.addPreg(obj.text)
  41. break
  42. case "deletepreg":
  43. item1.pregs = []
  44. break
  45. case "draw":
  46. break
  47. default:
  48. console.log(JSON.stringify(obj))
  49. }
  50. }
  51. function send_obj(obj) {
  52. sendTextMessage(JSON.stringify(obj))
  53. }
  54. function set_preg() {
  55. send_obj({
  56. type: "initpreg"
  57. })
  58. }
  59. function unset_preg() {
  60. send_obj({
  61. type: "stoppreg"
  62. })
  63. }
  64. function prev_diapo() {
  65. var curImage = Math.max(item1.curImage - 1, 1)
  66. send_obj({
  67. type: "command",
  68. commandtype: "diapo",
  69. PDF: item1.curPDF,
  70. slide: curImage
  71. })
  72. }
  73. function next_diapo() {
  74. var curImage = Math.min(item1.curImage + 1, total_slides)
  75. send_obj({
  76. type: "command",
  77. commandtype: "diapo",
  78. PDF: item1.curPDF,
  79. slide: curImage
  80. })
  81. }
  82. function clear_path() {
  83. send_obj({
  84. type: "command",
  85. commandtype: "clean"
  86. })
  87. canvas.clean_path()
  88. }
  89. function send_drawing(d) {
  90. send_obj({
  91. type: "command",
  92. commandtype: "draw",
  93. points: d,
  94. color: canvas.curColor
  95. })
  96. }
  97. onStatusChanged: {
  98. //FIXME implement
  99. if (socket.status == WebSocket.Open) {
  100. wsStatus.text = qsTr("Conectado")
  101. } else {
  102. wsStatus.text = qsTr("Desconectado\nReconectando..")
  103. }
  104. }
  105. active: true
  106. }
  107. Item {
  108. id: item1
  109. width: parent.width
  110. height: parent.height
  111. property alias mouseArea1: mouseArea1
  112. property alias button3: button3
  113. property int curImage: 1
  114. property variant pregs: []
  115. property string curPDF: "EBA"
  116. anchors.fill: parent
  117. onCurPDFChanged: {
  118. canvas.change_image(curPDF, curImage)
  119. diapoStatus.text = qsTr("%1/%2 (%3 preg)").arg(curImage).arg(
  120. socket.total_slides).arg(pregs.length)
  121. }
  122. onPregsChanged: {
  123. diapoStatus.text = qsTr("%1/%2 (%3 preg)").arg(curImage).arg(
  124. socket.total_slides).arg(pregs.length)
  125. }
  126. onCurImageChanged: {
  127. canvas.change_image(curPDF, curImage)
  128. diapoStatus.text = qsTr("%1/%2 (%3 preg)").arg(curImage).arg(
  129. socket.total_slides).arg(pregs.length)
  130. }
  131. function addPreg(p) {
  132. if (pregs.indexOf(p) !== -1) {
  133. return
  134. }
  135. pregs.push(p)
  136. }
  137. Grid {
  138. id: grid1
  139. width: parent.width
  140. height: parent.height
  141. anchors.right: parent.right
  142. anchors.bottom: parent.bottom
  143. anchors.top: parent.top
  144. anchors.left: parent.left
  145. columns: 4
  146. spacing: 4
  147. Rectangle {
  148. id: canvasCol
  149. height: parent.height
  150. width: height * 4 / 3
  151. color: aw.color
  152. Canvas {
  153. id: canvas
  154. antialiasing: true
  155. renderTarget: Canvas.Image
  156. property color strokeStyle: Qt.darker(fillStyle, 1.2)
  157. property color fillStyle: "#6400aa"
  158. property string curColor: "#ff0000"
  159. property real px: width / 2
  160. property real py: height / 2 + 10
  161. property real alpha: 1.0
  162. property variant points_array: []
  163. property string curImageUrl: "https://plataforma.especificosba.com.ar/diapo/EBA/1.png"
  164. width: parent.width
  165. height: parent.height
  166. onPaint: paint_canvas()
  167. onImageLoaded: {
  168. clean_path()
  169. requestPaint()
  170. }
  171. function clean_path() {
  172. points_array = []
  173. requestPaint()
  174. }
  175. function start_path() {
  176. points_array.push({
  177. points: [],
  178. color: curColor
  179. })
  180. }
  181. function finish_path() {
  182. points_array[points_array.length - 1].points = Simplify.simplify(
  183. points_array[points_array.length - 1].points,
  184. 2.2)
  185. requestPaint()
  186. //normalized to 800x600
  187. var ar = points_array[points_array.length - 1].points.map(
  188. function (el) {
  189. return {
  190. x: Math.round(
  191. el.x * 800 / canvas.width),
  192. y: Math.round(
  193. el.y * 600 / canvas.height)
  194. }
  195. })
  196. while (ar.length > 30) {
  197. socket.send_drawing(ar.splice(0, 29))
  198. }
  199. socket.send_drawing(ar)
  200. }
  201. function change_image(pdf, number) {
  202. curImageUrl = "https://plataforma.especificosba.com.ar/diapo/"
  203. + pdf + "/" + number + ".png"
  204. requestPaint()
  205. }
  206. function add_point(p) {
  207. var pts = points_array[points_array.length - 1].points
  208. pts.push(p)
  209. if (pts.length > 60) {
  210. //FIXME
  211. finish_path()
  212. start_path()
  213. pts.push(p)
  214. } else {
  215. requestPaint()
  216. }
  217. }
  218. function paint_canvas() {
  219. var ctx = canvas.getContext("2d")
  220. canvas.loadImage(curImageUrl)
  221. if (canvas.isImageLoaded(curImageUrl)) {
  222. ctx.drawImage(curImageUrl, 0, 0, canvas.width,
  223. canvas.height)
  224. }
  225. ctx.lineCap = 'square'
  226. ctx.lineWidth = 4
  227. ctx.strokeStyle = "#000000"
  228. var i = 0, j = 0
  229. for (j = 0; j < points_array.length; j++) {
  230. var points = points_array[j].points
  231. if (points.length === 0)
  232. continue
  233. ctx.strokeStyle = points_array[j].color
  234. ctx.beginPath()
  235. ctx.moveTo(points[0].x, points[0].y)
  236. for (i = 0; i < points.length - 1; i++) {
  237. ctx.moveTo(points[i].x, points[i].y)
  238. ctx.lineTo(points[i + 1].x, points[i + 1].y)
  239. }
  240. ctx.stroke()
  241. }
  242. }
  243. }
  244. MouseArea {
  245. id: mouseArea1
  246. x: 0
  247. y: 0
  248. width: canvas.width
  249. height: canvas.height
  250. onPressed: {
  251. if (socket.status !== WebSocket.Open)
  252. return
  253. canvas.start_path()
  254. }
  255. onPositionChanged: {
  256. if (socket.status !== WebSocket.Open)
  257. return
  258. canvas.add_point({
  259. x: mouse.x,
  260. y: mouse.y
  261. })
  262. }
  263. onReleased: {
  264. if (socket.status !== WebSocket.Open)
  265. return
  266. canvas.finish_path()
  267. }
  268. }
  269. }
  270. Column {
  271. id: columnLayout1
  272. spacing: 10
  273. width: parent.width - canvasCol.width - parent.spacing * 2
  274. height: parent.height
  275. Column {
  276. width: parent.width
  277. height: parent.height / 2 - 60
  278. Row {
  279. id: row2
  280. width: parent.width
  281. height: 70
  282. MouseArea {
  283. id: image1
  284. width: parent.width/2
  285. height: 70
  286. Image {
  287. source: "qrc:///images/back.png"
  288. height: parent.height
  289. fillMode: Image.PreserveAspectFit
  290. }
  291. enabled: socket.status == WebSocket.Open && (item1.curImage > 1)
  292. onClicked : socket.prev_diapo()
  293. }
  294. MouseArea {
  295. id: image2
  296. width: parent.width/2
  297. height: 70
  298. Image {
  299. height: parent.height
  300. source: "qrc:///images/fwd.png"
  301. fillMode: Image.PreserveAspectFit
  302. }
  303. enabled: socket.status == WebSocket.Open
  304. && (item1.curImage < socket.total_slides)
  305. onClicked: socket.next_diapo()
  306. }
  307. }
  308. Row {
  309. id: row1
  310. width: parent.width
  311. height: 70
  312. Button {
  313. height: 70
  314. width: parent.width / 2
  315. id: preg
  316. text: qsTr("Preg")
  317. onClicked: {
  318. socket.set_preg()
  319. }
  320. }
  321. Button {
  322. height: 70
  323. width: parent.width / 2
  324. id: nopreg
  325. text: qsTr("No Preg")
  326. onClicked: {
  327. socket.unset_preg()
  328. }
  329. }
  330. }
  331. }
  332. Column {
  333. id: column1
  334. width: parent.width
  335. height: parent.height / 2 - 60
  336. Row {
  337. id: row3
  338. width: parent.width
  339. height: 70
  340. Button {
  341. height: 70
  342. width: parent.width/3
  343. id: color3
  344. checkable: true
  345. onClicked: {
  346. color1.checked = false
  347. color2.checked = false
  348. checked = true
  349. canvas.curColor = "#0000ff"
  350. }
  351. style: ButtonStyle {
  352. background: Rectangle {
  353. implicitWidth: 100
  354. implicitHeight: 25
  355. border.width: 2
  356. border.color: color3.checked?"#000":"#FFF"
  357. radius: 8
  358. gradient: Gradient {
  359. GradientStop { position: 0 ; color: "#00f" }
  360. GradientStop { position: 1 ; color: "#00f" }
  361. }
  362. }
  363. }
  364. }
  365. Button {
  366. height: 70
  367. width: parent.width/3
  368. id: color2
  369. checkable: true
  370. onClicked: {
  371. color1.checked = false
  372. color3.checked = false
  373. checked = true
  374. canvas.curColor = "#00ff00"
  375. }
  376. style: ButtonStyle {
  377. background: Rectangle {
  378. implicitWidth: 100
  379. implicitHeight: 25
  380. border.width: 2
  381. border.color: color2.checked?"#000":"#FFF"
  382. radius: 8
  383. gradient: Gradient {
  384. GradientStop { position: 0 ; color: "#0f0" }
  385. GradientStop { position: 1 ; color: "#0f0" }
  386. }
  387. }
  388. }
  389. }
  390. Button {
  391. width: parent.width/3
  392. height: 70
  393. id: color1
  394. checked: true
  395. checkable: true
  396. onClicked: {
  397. color2.checked = false
  398. color3.checked = false
  399. checked = true
  400. canvas.curColor = "#ff0000"
  401. }
  402. style: ButtonStyle {
  403. background: Rectangle {
  404. implicitWidth: 100
  405. implicitHeight: 25
  406. border.width: 2
  407. border.color: color1.checked?"#000":"#FFF"
  408. radius: 8
  409. gradient: Gradient {
  410. GradientStop { position: 0 ; color: "#f00" }
  411. GradientStop { position: 1 ; color: "#f00" }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. Button {
  418. height: 70
  419. width: parent.width
  420. id: button3
  421. text: qsTr("Limpiar")
  422. enabled: socket.status == WebSocket.Open
  423. onClicked: socket.clear_path()
  424. }
  425. Text {
  426. id: wsStatus
  427. text: qsTr("WS")
  428. }
  429. Text {
  430. id: diapoStatus
  431. text: qsTr("test123")
  432. }
  433. }
  434. }
  435. }
  436. }
  437. }