KNewStuff

Rating.qml
1/*
2 SPDX-FileCopyrightText: 2012 Aleix Pol Gonzalez <aleixpol@blue-systems.com>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7import QtQuick
8import QtQuick.Layouts
9import QtQuick.Controls as QQC2
10
11import org.kde.kirigami as Kirigami
12
13RowLayout {
14 id: view
15 property bool editable: false
16 property int max: 100
17 property int rating: 0
18 property real starSize: Kirigami.Units.gridUnit
19 property bool reverseLayout: false
20
21 clip: true
22 spacing: 0
23
24 readonly property int ratingIndex: Math.floor((theRepeater.count * view.rating) / view.max)
25 readonly property bool ratingHalf: (theRepeater.count * view.rating) % view.max >= view.max / 2
26
27 QQC2.Label {
28 Layout.minimumWidth: view.starSize
29 Layout.minimumHeight: view.starSize
30 visible: view.reverseLayout
31 text: ratingAsText.text
32 }
33 Item {
34 visible: view.reverseLayout
35 Layout.minimumHeight: view.starSize;
36 Layout.minimumWidth: Kirigami.Units.smallSpacing;
37 Layout.maximumWidth: Kirigami.Units.smallSpacing;
38 }
39 Repeater {
40 id: theRepeater
41 model: 5
42 delegate: Kirigami.Icon {
43 Layout.minimumWidth: view.starSize
44 Layout.minimumHeight: view.starSize
45 Layout.preferredWidth: view.starSize
46 Layout.preferredHeight: view.starSize
47
48 source: index < view.ratingIndex
49 ? "rating"
50 : (view.ratingHalf && index === view.ratingIndex
51 ? (view.LayoutMirroring.enabled ? "rating-half-rtl" : "rating-half")
52 : "rating-unrated")
53 opacity: (view.editable && mouse.item.containsMouse) ? 0.7 : 1
54
55 ConditionalLoader {
56 id: mouse
57
58 anchors.fill: parent
59 condition: view.editable
60 componentTrue: MouseArea {
61 hoverEnabled: true
62 onClicked: mouse => {
63 rating = (max/theRepeater.model*(index+1));
64 }
65 }
66 componentFalse: null
67 }
68 }
69 }
70 Item {
71 visible: !view.reverseLayout
72 Layout.minimumHeight: view.starSize;
73 Layout.minimumWidth: Kirigami.Units.smallSpacing;
74 Layout.maximumWidth: Kirigami.Units.smallSpacing;
75 }
76 QQC2.Label {
77 id: ratingAsText
78 Layout.minimumWidth: view.starSize
79 Layout.minimumHeight: view.starSize
80 visible: !view.reverseLayout
81 text: i18ndc("knewstuff6", "A text representation of the rating, shown as a fraction of the max value", "(%1/%2)", view.rating / 10, view.max / 10)
82 }
83}
QString i18ndc(const char *domain, const char *context, const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:20:03 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.