LabelBar.qml 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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.2 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. Label
  33. {
  34. id: label
  35. text: model[modelKey]
  36. color: UM.Theme.getColor("text")
  37. font: UM.Theme.getFont("default")
  38. renderType: Text.NativeRendering
  39. height: contentHeight
  40. anchors
  41. {
  42. // Some magic to ensure that the items are aligned properly.
  43. // We want the following:
  44. // First item should be aligned to the left, no margin.
  45. // Last item should be aligned to the right, no margin.
  46. // The middle item(s) should be aligned to the center of the "item" it's showing (hence half the itemsize as offset).
  47. // We want the center of the label to align with the center of the item, so we negatively offset by half the contentWidth
  48. right: index + 1 === repeater.count ? parent.right: undefined
  49. left: index + 1 === repeater.count || index === 0 ? undefined: parent.left
  50. leftMargin: Math.round((itemSize - contentWidth) * 0.5)
  51. // For some reason, the last label in the row gets misaligned with Qt 5.10. This lines seems to
  52. // fix it.
  53. verticalCenter: parent.verticalCenter
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }