SectionItem.qml 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import Felgo 4.0
  2. import QtQuick 2.0
  3. Rectangle {
  4. id: root
  5. readonly property real calculatedHeight: titleSection.height + contentColumn.height + 3 * verticalMargin
  6. property real horizontalMargin: dp(Theme.navigationBar.defaultBarItemPadding)
  7. property real verticalMargin: dp(Theme.navigationBar.defaultBarItemPadding)
  8. property real spacing: dp(1)
  9. default property alias content: contentColumn.children
  10. property alias title: titleSection.text
  11. // width will be set via binding in Component.onCompleted
  12. height: calculatedHeight
  13. color: Theme.backgroundColor
  14. AppText {
  15. id: titleSection
  16. anchors {
  17. top: parent.top
  18. topMargin: root.verticalMargin
  19. horizontalCenter: parent.horizontalCenter
  20. }
  21. font {
  22. bold: true
  23. pixelSize: sp(16)
  24. }
  25. }
  26. Column {
  27. id: contentColumn
  28. anchors {
  29. horizontalCenter: parent.horizontalCenter
  30. top: titleSection.bottom
  31. topMargin: root.verticalMargin
  32. }
  33. width: parent.width - 2 * horizontalMargin
  34. spacing: root.spacing
  35. onHeightChanged: {
  36. if (!root.parent.columns) {
  37. internal.adjustColorAndHeight()
  38. }
  39. }
  40. }
  41. Item {
  42. anchors.top: contentColumn.bottom
  43. height: root.verticalMargin
  44. width: parent.width
  45. }
  46. QtObject {
  47. id: internal
  48. function resetHeight() {
  49. root.height = root.calculatedHeight
  50. root.height = Qt.binding(function() { return root.calculatedHeight})
  51. }
  52. function adjustColorAndHeight() {
  53. for (var i = 0; i < root.parent.children.length; i++) {
  54. if (root.parent.children[i] === root) {
  55. if (root.parent.columns > 1) {
  56. if (i % 2 == 1) {
  57. var left = root.parent.children[i - 1]
  58. var right = root.parent.children[i]
  59. left.height = Qt.binding(function() {return (right.calculatedHeight > left.calculatedHeight) ? right.calculatedHeight : left.calculatedHeight})
  60. right.height = Qt.binding(function() {return (right.calculatedHeight > left.calculatedHeight) ? right.calculatedHeight : left.calculatedHeight})
  61. }
  62. if (i % 4 >= 2) {
  63. root.color = "#f8f9fa"
  64. } else {
  65. root.color = Theme.backgroundColor
  66. }
  67. } else {
  68. if (i % 2 == 1) {
  69. root.color = "#f8f9fa"
  70. } else {
  71. root.color = Theme.backgroundColor
  72. }
  73. resetHeight()
  74. }
  75. }
  76. }
  77. }
  78. }
  79. onWidthChanged: {
  80. internal.adjustColorAndHeight()
  81. }
  82. Component.onCompleted: {
  83. internal.adjustColorAndHeight()
  84. width = Qt.binding(function() { return app.tablet ? parent.width / 2 : parent.width })
  85. }
  86. }