Graph.qml 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import QtQuick 2.0
  2. Canvas {
  3. id: root
  4. property int elementWidth: 20
  5. property int elementHeight: 20
  6. property int spacingH: 12
  7. property int spacingV: 20
  8. property int lineWidth: 1
  9. property QtObject model: null
  10. height: model.points.count*(elementWidth + spacingV)
  11. width: (elementWidth + spacingH)*model.branchesCount
  12. QtObject {
  13. id: d
  14. property int halfElementWidth: elementWidth/2
  15. property int halfElementHeight: elementHeight/2
  16. }
  17. onPaint: {
  18. var ctx = getContext("2d")
  19. for(var i = 0; i < model.points.count; i++) {
  20. var point = model.points.at(i)
  21. var pointAbsX = point.x*(elementWidth + spacingH)
  22. var pointAbsY = point.y*(elementHeight + spacingV)
  23. var childPoints = point.childPoints
  24. ctx.beginPath()
  25. ctx.lineWidth = root.lineWidth
  26. ctx.strokeStyle = "#"+point.color
  27. ctx.roundedRect(pointAbsX, pointAbsY, elementWidth, elementHeight, elementWidth, elementHeight)
  28. ctx.stroke()
  29. ctx.closePath()
  30. for(var j = 0; j < childPoints.length; j++) {
  31. var childPoint = childPoints[j]
  32. var childPointAbsX = childPoint.x*(elementWidth + spacingH)
  33. var childPointAbsY = childPoint.y*(elementHeight + spacingV)
  34. ctx.beginPath()
  35. ctx.strokeStyle = "#" + childPoint.color
  36. ctx.lineWidth = root.lineWidth
  37. if(point.x !== childPoint.x) {
  38. if(point.x < childPoint.x) {
  39. ctx.moveTo(pointAbsX + d.halfElementWidth, childPointAbsY + elementHeight + spacingV)
  40. ctx.bezierCurveTo(
  41. pointAbsX + d.halfElementWidth, childPointAbsY + elementHeight,
  42. childPointAbsX + d.halfElementWidth, childPointAbsY + elementHeight + spacingV/* + d.halfElementHeight*/,
  43. childPointAbsX + d.halfElementWidth, childPointAbsY + elementHeight/*d.halfElementHeight*/)
  44. } else {
  45. ctx.moveTo(pointAbsX + d.halfElementWidth, pointAbsY/* + d.halfElementHeight*/)
  46. ctx.lineTo(pointAbsX + d.halfElementWidth, childPointAbsY + elementHeight + spacingV + elementHeight)
  47. ctx.bezierCurveTo(
  48. pointAbsX + d.halfElementWidth, childPointAbsY + d.halfElementHeight,
  49. childPointAbsX + d.halfElementWidth, childPointAbsY + elementHeight + spacingV/* + d.halfElementHeight*/,
  50. childPointAbsX + d.halfElementWidth, childPointAbsY + elementHeight/*d.halfElementHeight*/)
  51. //For debuggin bazier curve migh be replaced buy straight line
  52. // ctx.lineTo(childPointAbsX + d.halfElementWidth, childPointAbsY + d.halfElementHeight)
  53. }
  54. } else {
  55. ctx.moveTo(pointAbsX + d.halfElementWidth, pointAbsY/* + d.halfElementHeight*/)
  56. ctx.lineTo(childPointAbsX + d.halfElementWidth, childPointAbsY + elementHeight)
  57. }
  58. ctx.stroke()
  59. ctx.closePath()
  60. }
  61. }
  62. }
  63. }