main.qml 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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: "#f00"
  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.length=0
  92. full_repaint()
  93. }
  94. function start_path() {
  95. points_array.push({
  96. points: [],
  97. color: curColor
  98. })
  99. }
  100. function finish_path() {
  101. points_array[points_array.length - 1].points = Simplify.simplify(
  102. points_array[points_array.length - 1].points,
  103. 2.2)
  104. if (points_array[points_array.length - 1].points.length <=1)
  105. return;
  106. //normalized to 800x600
  107. var ar = points_array[points_array.length - 1].points.map(
  108. function (el) {
  109. return {
  110. x: Math.round(
  111. el.x * 800 / canvas.width),
  112. y: Math.round(
  113. el.y * 600 / canvas.height)
  114. }
  115. })
  116. var MAX_ELEMS=65;
  117. while (ar.length > MAX_ELEMS) {
  118. socket.send_drawing(ar.splice(0, MAX_ELEMS-1));
  119. }
  120. socket.send_drawing(ar);
  121. }
  122. function change_image(pdf, number) {
  123. curImageUrl = "https://plataforma.especificosba.com.ar/diapo/"
  124. + pdf + "/" + number + ".png"
  125. }
  126. function add_point(p) {
  127. var pts = points_array[points_array.length - 1].points
  128. var lastpoint;
  129. if (pts.length>1)
  130. lastpoint = pts[pts.length-1];
  131. pts.push(p)
  132. if (pts.length > 150) {
  133. //FIXME
  134. finish_path()
  135. start_path()
  136. pts.push(p)
  137. }
  138. if (lastpoint)
  139. 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))
  140. }
  141. function stroke(points, color) {
  142. if (points.length===0)
  143. return;
  144. if (ctx.strokeStyle !== color){
  145. ctx.strokeStyle = color;
  146. console.log("Changing colors");
  147. }
  148. ctx.beginPath()
  149. ctx.moveTo(points[0].x, points[0].y)
  150. var i = 0;
  151. for (i = 0; i < points.length - 1; i++) {
  152. ctx.moveTo(points[i].x, points[i].y)
  153. ctx.lineTo(points[i + 1].x, points[i + 1].y)
  154. }
  155. ctx.stroke()
  156. }
  157. function full_repaint(){
  158. //console.log("Fullpaint")
  159. canvas.loadImage(curImageUrl)
  160. if (canvas.isImageLoaded(curImageUrl)) {
  161. ctx.drawImage(curImageUrl, 0, 0, canvas.width,
  162. canvas.height)
  163. }
  164. var j = 0
  165. for (j = 0; j < points_array.length; j++) {
  166. if (points_array[j].points.length <= 1)
  167. continue
  168. stroke(points_array[j].points,points_array[j].color);
  169. }
  170. requestPaint()
  171. }
  172. function paint_canvas() {
  173. //full_repaint();
  174. //return;
  175. if (points_array.length==0)
  176. return;
  177. var ar = points_array[points_array.length -1];
  178. stroke(ar.points,ar.color);
  179. }
  180. }
  181. MouseArea {
  182. id: mouseArea1
  183. x: 0
  184. y:0
  185. width: canvas.width
  186. height: canvas.height
  187. property var last_point;
  188. onPressed: {
  189. if (!socket.isOpen)
  190. return
  191. canvas.start_path()
  192. last_point = { x: mouse.x, y: mouse.y }
  193. canvas.add_point(last_point)
  194. }
  195. onPositionChanged: {
  196. if (!socket.isOpen)
  197. return
  198. var point = { x: mouse.x, y: mouse.y }
  199. if (distance(last_point, point) <= 81) //distance returns squared dsitance
  200. return;
  201. last_point = point
  202. canvas.add_point(point)
  203. }
  204. onReleased: {
  205. if (!socket.isOpen)
  206. return
  207. canvas.finish_path()
  208. }
  209. function distance(p1,p2){
  210. return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)
  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 - 60
  222. Row {
  223. id: row2
  224. width: parent.width
  225. height: 70
  226. MouseArea {
  227. id: image1
  228. width: parent.width/2
  229. height: 70
  230. Image {
  231. anchors.centerIn: parent.Center
  232. source: "qrc:///images/back.png"
  233. height: parent.height
  234. fillMode: Image.PreserveAspectFit
  235. }
  236. enabled: socket.isOpen && (item1.curImage > 1)
  237. onClicked : socket.prev_diapo()
  238. }
  239. MouseArea {
  240. id: image2
  241. width: parent.width/2
  242. height: 70
  243. Image {
  244. anchors.centerIn: parent.Center
  245. height: parent.height
  246. source: "qrc:///images/fwd.png"
  247. fillMode: Image.PreserveAspectFit
  248. }
  249. enabled: socket.isOpen && (item1.curImage < t_slides)
  250. onClicked: socket.next_diapo()
  251. }
  252. }
  253. Row {
  254. id: row1
  255. width: parent.width
  256. height: 70
  257. Button {
  258. height: 70
  259. width: parent.width / 2
  260. id: preg
  261. text: qsTr("Preg")
  262. onClicked: {
  263. socket.set_preg()
  264. }
  265. }
  266. Button {
  267. height: 70
  268. width: parent.width / 2
  269. id: nopreg
  270. text: qsTr("No Preg")
  271. onClicked: {
  272. socket.unset_preg()
  273. }
  274. }
  275. }
  276. }
  277. Column {
  278. id: column1
  279. width: parent.width
  280. height: parent.height / 2 - 60
  281. Repeater{
  282. model: [ ["#00f", "#0f0", "#f00"], ["#ff0", "#0ff", "#000"] ]
  283. Row {
  284. width: parent.width
  285. height: 70
  286. Repeater {
  287. model: modelData
  288. Button {
  289. height: 70
  290. width: parent.width/3
  291. checkable: true
  292. checked: canvas.curColor === modelData
  293. onClicked: canvas.curColor = modelData
  294. style: ButtonStyle {
  295. background: Rectangle {
  296. implicitWidth: 100
  297. implicitHeight: 25
  298. border.width: 3
  299. border.color: canvas.curColor === modelData ?"#000":"#FFF"
  300. radius: 8
  301. gradient: Gradient {
  302. GradientStop { position: 0 ; color: modelData }
  303. GradientStop { position: 1 ; color: modelData }
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. Button {
  312. height: 70
  313. width: parent.width
  314. id: button3
  315. text: qsTr("Limpiar")
  316. enabled: socket.isOpen
  317. onClicked: {
  318. canvas.clean_path();
  319. socket.clear_path();
  320. }
  321. }
  322. Text {
  323. id: wsStatus
  324. text: qsTr("WS Status")
  325. }
  326. Text {
  327. id: diapoStatus
  328. text: qsTr("Diapo status")
  329. }
  330. }
  331. }
  332. }
  333. }
  334. }