RatingWidget.qml 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import QtQuick 2.7
  2. import QtQuick.Controls 2.1
  3. import UM 1.0 as UM
  4. import Cura 1.1 as Cura
  5. Item
  6. {
  7. id: ratingWidget
  8. property real rating: 0
  9. property int indexHovered: -1
  10. property string packageId: ""
  11. property int userRating: 0
  12. property bool canRate: false
  13. signal rated(int rating)
  14. width: contentRow.width
  15. height: contentRow.height
  16. MouseArea
  17. {
  18. id: mouseArea
  19. anchors.fill: parent
  20. hoverEnabled: ratingWidget.canRate
  21. acceptedButtons: Qt.NoButton
  22. onExited:
  23. {
  24. if(ratingWidget.canRate)
  25. {
  26. ratingWidget.indexHovered = -1
  27. }
  28. }
  29. Row
  30. {
  31. id: contentRow
  32. height: childrenRect.height
  33. Repeater
  34. {
  35. model: 5 // We need to get 5 stars
  36. Button
  37. {
  38. id: control
  39. hoverEnabled: true
  40. onHoveredChanged:
  41. {
  42. if(hovered && ratingWidget.canRate)
  43. {
  44. indexHovered = index
  45. }
  46. }
  47. ToolTip.visible: control.hovered && !ratingWidget.canRate
  48. ToolTip.text: !Cura.API.account.isLoggedIn ? catalog.i18nc("@label", "You need to login first before you can rate"): catalog.i18nc("@label", "You need to install the package before you can rate")
  49. property bool isStarFilled:
  50. {
  51. // If the entire widget is hovered, override the actual rating.
  52. if(ratingWidget.indexHovered >= 0)
  53. {
  54. return indexHovered >= index
  55. }
  56. if(ratingWidget.userRating > 0)
  57. {
  58. return userRating >= index +1
  59. }
  60. return rating >= index + 1
  61. }
  62. contentItem: Item {}
  63. height: UM.Theme.getSize("rating_star").height
  64. width: UM.Theme.getSize("rating_star").width
  65. background: UM.RecolorImage
  66. {
  67. source: UM.Theme.getIcon(control.isStarFilled ? "star_filled" : "star_empty")
  68. sourceSize.width: width
  69. sourceSize.height: height
  70. // Unfilled stars should always have the default color. Only filled stars should change on hover
  71. color:
  72. {
  73. if(!ratingWidget.canRate)
  74. {
  75. return UM.Theme.getColor("rating_star")
  76. }
  77. if((ratingWidget.indexHovered >= 0 || ratingWidget.userRating > 0) && isStarFilled)
  78. {
  79. return UM.Theme.getColor("primary")
  80. }
  81. return UM.Theme.getColor("rating_star")
  82. }
  83. }
  84. onClicked:
  85. {
  86. if(ratingWidget.canRate)
  87. {
  88. rated(index + 1) // Notify anyone who cares about this.
  89. }
  90. }
  91. }
  92. }
  93. }
  94. }
  95. }