main.qml 17 KB

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