RoundedRectangle.qml 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import QtQuick 2.7
  2. import UM 1.2 as UM
  3. // The rounded rectangle works mostly like a regular rectangle, but provides the option to have rounded corners on only one side of the rectangle.
  4. Item
  5. {
  6. id: roundedRectangle
  7. // As per the regular rectangle
  8. property color color: "transparent"
  9. // As per regular rectangle
  10. property int radius: UM.Theme.getSize("default_radius").width
  11. // On what side should the corners be shown 5 can be used if no radius is needed.
  12. // 1 is down, 2 is left, 3 is up and 4 is right.
  13. property int cornerSide: RoundedRectangle.Direction.None
  14. // Simple object to ensure that border.width and border.color work
  15. property BorderGroup border: BorderGroup {}
  16. enum Direction
  17. {
  18. None = 0,
  19. Down = 1,
  20. Left = 2,
  21. Up = 3,
  22. Right = 4,
  23. All = 5
  24. }
  25. Rectangle
  26. {
  27. id: background
  28. anchors.fill: parent
  29. radius: cornerSide != RoundedRectangle.Direction.None ? parent.radius : 0
  30. color: parent.color
  31. border.width: parent.border.width
  32. border.color: parent.border.color
  33. }
  34. // The item that covers 2 of the corners to make them not rounded.
  35. Rectangle
  36. {
  37. visible: cornerSide != RoundedRectangle.Direction.None && cornerSide != RoundedRectangle.Direction.All
  38. height: cornerSide % 2 ? parent.radius: parent.height
  39. width: cornerSide % 2 ? parent.width : parent.radius
  40. color: parent.color
  41. anchors
  42. {
  43. right: cornerSide == RoundedRectangle.Direction.Left ? parent.right: undefined
  44. bottom: cornerSide == RoundedRectangle.Direction.Up ? parent.bottom: undefined
  45. }
  46. border.width: parent.border.width
  47. border.color: parent.border.color
  48. Rectangle
  49. {
  50. color: roundedRectangle.color
  51. height: cornerSide % 2 ? roundedRectangle.border.width: roundedRectangle.height - 2 * roundedRectangle.border.width
  52. width: cornerSide % 2 ? roundedRectangle.width - 2 * roundedRectangle.border.width: roundedRectangle.border.width
  53. anchors
  54. {
  55. right: cornerSide == RoundedRectangle.Direction.Right ? parent.right : undefined
  56. bottom: cornerSide == RoundedRectangle.Direction.Down ? parent.bottom: undefined
  57. horizontalCenter: cornerSide % 2 ? parent.horizontalCenter: undefined
  58. verticalCenter: cornerSide % 2 ? undefined: parent.verticalCenter
  59. }
  60. }
  61. }
  62. }