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 2.11
8import QtQuick.Layouts 1.11
9import QtQuick.Controls 2.11 as QtControls
10
11import org.kde.kirigami 2.0 as Kirigami
12
13RowLayout
14{
15 id: view
16 property bool editable: false
17 property int max: 100
18 property int rating: 0
19 property real starSize: Kirigami.Units.gridUnit
20 property bool reverseLayout: false
21
22 clip: true
23 spacing: 0
24
25 readonly property var ratingIndex: Math.floor((theRepeater.count*view.rating)/view.max)
26 readonly property var ratingHalf: (theRepeater.count*view.rating)%view.max >= view.max / 2
27
28 QtControls.Label {
29 Layout.minimumWidth: view.starSize
30 Layout.minimumHeight: view.starSize
31 visible: view.reverseLayout
32 text: ratingAsText.text
33 }
34 Item {
35 visible: view.reverseLayout
36 Layout.minimumHeight: view.starSize;
37 Layout.minimumWidth: Kirigami.Units.smallSpacing;
38 Layout.maximumWidth: Kirigami.Units.smallSpacing;
39 }
40 Repeater {
41 id: theRepeater
42 model: 5
43 delegate: Kirigami.Icon {
44 Layout.minimumWidth: view.starSize
45 Layout.minimumHeight: view.starSize
46 Layout.preferredWidth: view.starSize
47 Layout.preferredHeight: view.starSize
48
49 source: index < view.ratingIndex
50 ? "rating"
51 : (view.ratingHalf && index == view.ratingIndex
52 ? (view.LayoutMirroring.enabled ? "rating-half-rtl" : "rating-half")
53 : "rating-unrated")
54 opacity: (view.editable && mouse.item.containsMouse) ? 0.7 : 1
55
56 ConditionalLoader {
57 id: mouse
58
59 anchors.fill: parent
60 condition: view.editable
61 componentTrue: MouseArea {
62 hoverEnabled: true
63 onClicked: rating = (max/theRepeater.model*(index+1))
64 }
65 componentFalse: null
66 }
67 }
68 }
69 Item {
70 visible: !view.reverseLayout
71 Layout.minimumHeight: view.starSize;
72 Layout.minimumWidth: Kirigami.Units.smallSpacing;
73 Layout.maximumWidth: Kirigami.Units.smallSpacing;
74 }
75 QtControls.Label {
76 id: ratingAsText
77 Layout.minimumWidth: view.starSize
78 Layout.minimumHeight: view.starSize
79 visible: !view.reverseLayout
80 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)
81 }
82}
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 Tue Mar 26 2024 11:21:35 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.