main.qml 15 KB

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