LabelBar.qml 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright (c) 2019 Ultimaker B.V.
  2. // Cura is released under the terms of the LGPLv3 or higher.
  3. import QtQuick 2.10
  4. import QtQuick.Controls 2.3
  5. import QtQuick.Layouts 1.3
  6. import UM 1.5 as UM
  7. // The labelBar shows a set of labels that are evenly spaced from one another.
  8. // The first item is aligned to the left, the last is aligned to the right.
  9. // It's intended to be used together with RadioCheckBar. As such, it needs
  10. // to know what the used itemSize is, so it can ensure the labels are aligned correctly.
  11. Item
  12. {
  13. id: base
  14. property var model: null
  15. property string modelKey: ""
  16. property int itemSize: 14
  17. height: childrenRect.height
  18. RowLayout
  19. {
  20. anchors.left: parent.left
  21. anchors.right: parent.right
  22. spacing: 0
  23. Repeater
  24. {
  25. id: repeater
  26. model: base.model
  27. Item
  28. {
  29. Layout.fillWidth: true
  30. Layout.maximumWidth: Math.round(index + 1 === repeater.count || repeater.count <= 1 ? itemSize : base.width / (repeater.count - 1))
  31. height: label.height
  32. UM.Label
  33. {
  34. id: label
  35. text: model[modelKey]
  36. height: contentHeight
  37. anchors
  38. {
  39. // Some magic to ensure that the items are aligned properly.
  40. // We want the following:
  41. // First item should be aligned to the left, no margin.
  42. // Last item should be aligned to the right, no margin.
  43. // The middle item(s) should be aligned to the center of the "item" it's showing (hence half the itemsize as offset).
  44. // We want the center of the label to align with the center of the item, so we negatively offset by half the contentWidth
  45. right: index + 1 === repeater.count ? parent.right: undefined
  46. left: index + 1 === repeater.count || index === 0 ? undefined: parent.left
  47. leftMargin: Math.round((itemSize - contentWidth) * 0.5)
  48. // For some reason, the last label in the row gets misaligned with Qt 5.10. This lines seems to
  49. // fix it.
  50. verticalCenter: parent.verticalCenter
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }