main.qml 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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: false
  65. renderTarget: Canvas.Image
  66. renderStrategy: Canvas.Immediate
  67. //defaults
  68. //renderStrategy: Canvas.Cooperative
  69. //renderTarget: Canvas.FramebufferObject
  70. //renderStrategy
  71. property color strokeStyle: Qt.darker(fillStyle, 1.2)
  72. property color fillStyle: "#6400aa"
  73. property string curColor: "#ff0000"
  74. property real px: width / 2
  75. property real py: height / 2 + 10
  76. property real alpha: 1.0
  77. property variant points_array: []
  78. property string curImageUrl: "https://plataforma.especificosba.com.ar/diapo/EBA/1.png"
  79. property var ctx: canvas.getContext("2d")
  80. width: parent.width
  81. height: parent.height
  82. onPaint: paint_canvas()
  83. onImageLoaded: {
  84. clean_path()
  85. ctx.lineCap = 'square'
  86. ctx.lineWidth = 4
  87. ctx.strokeStyle = "#000000"
  88. full_repaint()
  89. }
  90. function clean_path() {
  91. //points_array = []
  92. points_array.length=0
  93. console.log("clean")
  94. full_repaint()
  95. }
  96. function start_path() {
  97. points_array.push({
  98. points: [],
  99. color: curColor
  100. })
  101. }
  102. function finish_path() {
  103. points_array[points_array.length - 1].points = Simplify.simplify(
  104. points_array[points_array.length - 1].points,
  105. 2.2)
  106. if (points_array[points_array.length - 1].points.length <=1)
  107. return;
  108. //normalized to 800x600
  109. var ar = points_array[points_array.length - 1].points.map(
  110. function (el) {
  111. return {
  112. x: Math.round(
  113. el.x * 800 / canvas.width),
  114. y: Math.round(
  115. el.y * 600 / canvas.height)
  116. }
  117. })
  118. var MAX_ELEMS=65;
  119. while (ar.length > MAX_ELEMS) {
  120. socket.send_drawing(ar.splice(0, MAX_ELEMS-1));
  121. }
  122. socket.send_drawing(ar);
  123. }
  124. function change_image(pdf, number) {
  125. curImageUrl = "https://plataforma.especificosba.com.ar/diapo/"
  126. + pdf + "/" + number + ".png"
  127. }
  128. function add_point(p) {
  129. var pts = points_array[points_array.length - 1].points
  130. var lastpoint;
  131. if (pts.length>1)
  132. lastpoint = pts[pts.length-1];
  133. pts.push(p)
  134. if (pts.length > 150) {
  135. //FIXME
  136. finish_path()
  137. start_path()
  138. pts.push(p)
  139. }
  140. if (lastpoint)
  141. markDirty(Qt.rect(Math.min(p.x,lastpoint.x),Math.min(p.y,lastpoint.y), Math.abs(p.x-lastpoint.x)+4, Math.abs(p.y-lastpoint.y)+1))
  142. }
  143. function stroke(points, color) {
  144. if (points.length===0)
  145. return;
  146. if (ctx.strokeStyle !== color){
  147. ctx.strokeStyle = color;
  148. console.log("Changing colors");
  149. }
  150. console.log(points.length)
  151. ctx.beginPath()
  152. ctx.moveTo(points[0].x, points[0].y)
  153. var i = 0;
  154. for (i = 0; i < points.length - 1; i++) {
  155. ctx.moveTo(points[i].x, points[i].y)
  156. ctx.lineTo(points[i + 1].x, points[i + 1].y)
  157. }
  158. ctx.stroke()
  159. }
  160. function full_repaint(){
  161. //console.log("Fullpaint")
  162. canvas.loadImage(curImageUrl)
  163. if (canvas.isImageLoaded(curImageUrl)) {
  164. ctx.drawImage(curImageUrl, 0, 0, canvas.width,
  165. canvas.height)
  166. }
  167. var j = 0
  168. for (j = 0; j < points_array.length; j++) {
  169. if (points_array[j].points.length <= 1)
  170. continue
  171. stroke(points_array[j].points,points_array[j].color);
  172. }
  173. requestPaint()
  174. }
  175. function paint_canvas() {
  176. //full_repaint();
  177. //return;
  178. if (points_array.length==0)
  179. return;
  180. var ar = points_array[points_array.length -1];
  181. stroke(ar.points,ar.color);
  182. }
  183. }
  184. MouseArea {
  185. id: mouseArea1
  186. x: 0
  187. y:0
  188. width: canvas.width
  189. height: canvas.height
  190. property var last_point;
  191. onPressed: {
  192. if (!socket.isOpen)
  193. return
  194. canvas.start_path()
  195. last_point = { x: mouse.x, y: mouse.y }
  196. canvas.add_point(last_point)
  197. }
  198. onPositionChanged: {
  199. if (!socket.isOpen)
  200. return
  201. var point = { x: mouse.x, y: mouse.y }
  202. if (distance(last_point, point) <= 81) //distance returns squared dsitance
  203. return;
  204. last_point = point
  205. canvas.add_point(point)
  206. }
  207. onReleased: {
  208. if (!socket.isOpen)
  209. return
  210. canvas.finish_path()
  211. }
  212. function distance(p1,p2){
  213. return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)
  214. }
  215. }
  216. }
  217. Column {
  218. id: columnLayout1
  219. spacing: 10
  220. width: parent.width - canvasCol.width - parent.spacing * 2
  221. height: parent.height
  222. Column {
  223. width: parent.width
  224. height: parent.height / 2 - 60
  225. Row {
  226. id: row2
  227. width: parent.width
  228. height: 70
  229. MouseArea {
  230. id: image1
  231. width: parent.width/2
  232. height: 70
  233. Image {
  234. anchors.centerIn: parent.Center
  235. source: "qrc:///images/back.png"
  236. height: parent.height
  237. fillMode: Image.PreserveAspectFit
  238. }
  239. enabled: socket.isOpen && (item1.curImage > 1)
  240. onClicked : socket.prev_diapo()
  241. }
  242. MouseArea {
  243. id: image2
  244. width: parent.width/2
  245. height: 70
  246. Image {
  247. anchors.centerIn: parent.Center
  248. height: parent.height
  249. source: "qrc:///images/fwd.png"
  250. fillMode: Image.PreserveAspectFit
  251. }
  252. enabled: socket.isOpen && (item1.curImage < t_slides)
  253. onClicked: socket.next_diapo()
  254. }
  255. }
  256. Row {
  257. id: row1
  258. width: parent.width
  259. height: 70
  260. Button {
  261. height: 70
  262. width: parent.width / 2
  263. id: preg
  264. text: qsTr("Preg")
  265. onClicked: {
  266. socket.set_preg()
  267. }
  268. }
  269. Button {
  270. height: 70
  271. width: parent.width / 2
  272. id: nopreg
  273. text: qsTr("No Preg")
  274. onClicked: {
  275. socket.unset_preg()
  276. }
  277. }
  278. }
  279. }
  280. Column {
  281. id: column1
  282. width: parent.width
  283. height: parent.height / 2 - 60
  284. Row {
  285. id: row3
  286. width: parent.width
  287. height: 70
  288. Button {
  289. height: 70
  290. width: parent.width/3
  291. id: color3
  292. checkable: true
  293. onClicked: {
  294. color1.checked = false
  295. color2.checked = false
  296. checked = true
  297. canvas.curColor = "#0000ff"
  298. }
  299. style: ButtonStyle {
  300. background: Rectangle {
  301. implicitWidth: 100
  302. implicitHeight: 25
  303. border.width: 3
  304. border.color: color3.checked?"#000":"#FFF"
  305. radius: 8
  306. gradient: Gradient {
  307. GradientStop { position: 0 ; color: "#00f" }
  308. GradientStop { position: 1 ; color: "#00f" }
  309. }
  310. }
  311. }
  312. }
  313. Button {
  314. height: 70
  315. width: parent.width/3
  316. id: color2
  317. checkable: true
  318. onClicked: {
  319. color1.checked = false
  320. color3.checked = false
  321. checked = true
  322. canvas.curColor = "#00ff00"
  323. }
  324. style: ButtonStyle {
  325. background: Rectangle {
  326. implicitWidth: 100
  327. implicitHeight: 25
  328. border.width: 3
  329. border.color: color2.checked?"#000":"#FFF"
  330. radius: 8
  331. gradient: Gradient {
  332. GradientStop { position: 0 ; color: "#0f0" }
  333. GradientStop { position: 1 ; color: "#0f0" }
  334. }
  335. }
  336. }
  337. }
  338. Button {
  339. width: parent.width/3
  340. height: 70
  341. id: color1
  342. checked: true
  343. checkable: true
  344. onClicked: {
  345. color2.checked = false
  346. color3.checked = false
  347. checked = true
  348. canvas.curColor = "#ff0000"
  349. }
  350. style: ButtonStyle {
  351. background: Rectangle {
  352. implicitWidth: 100
  353. implicitHeight: 25
  354. border.width: 3
  355. border.color: color1.checked?"#000":"#FFF"
  356. radius: 8
  357. gradient: Gradient {
  358. GradientStop { position: 0 ; color: "#f00" }
  359. GradientStop { position: 1 ; color: "#f00" }
  360. }
  361. }
  362. }
  363. }
  364. }
  365. Button {
  366. height: 70
  367. width: parent.width
  368. id: button3
  369. text: qsTr("Limpiar")
  370. enabled: socket.isOpen
  371. onClicked: {
  372. canvas.clean_path();
  373. socket.clear_path();
  374. }
  375. }
  376. Text {
  377. id: wsStatus
  378. text: qsTr("WS Status")
  379. }
  380. Text {
  381. id: diapoStatus
  382. text: qsTr("Diapo status")
  383. }
  384. }
  385. }
  386. }
  387. }
  388. }