Warning, /multimedia/elisa/src/qml/RatingStar.qml is written in an unsupported language. File is not indexed.

0001 /*
0002    SPDX-FileCopyrightText: 2016 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
0003 
0004    SPDX-License-Identifier: LGPL-3.0-or-later
0005  */
0006 
0007 import QtQuick 2.7
0008 import QtQuick.Controls 2.15 as QQC2
0009 import QtQuick.Layouts 1.2
0010 import Qt5Compat.GraphicalEffects
0011 import org.kde.kirigami 2.5 as Kirigami
0012 
0013 QQC2.Control {
0014     id: control
0015 
0016     property int starRating
0017     property bool readOnly: true
0018 
0019     property double hoverBrightness: 0.5
0020     property double hoverContrast: 0.5
0021 
0022     readonly property int hoveredStar: mouseArea.containsMouse ? Math.ceil(5 * mouseArea.mouseX / mouseArea.width) : 0
0023     readonly property int hoveredRating: 2 * hoveredStar
0024 
0025     signal ratingEdited()
0026 
0027     function decreaseRating() {
0028         if (!readOnly) {
0029             starRating = Math.max(0, starRating - 2);
0030             ratingEdited();
0031         }
0032     }
0033 
0034     function increaseRating() {
0035         if (!readOnly) {
0036             starRating = Math.min(10, starRating + 2);
0037             ratingEdited();
0038         }
0039     }
0040 
0041     Keys.onLeftPressed: event => {
0042         if (readOnly) {
0043             event.accepted = false;
0044         } else {
0045             event.accepted = true;
0046             decreaseRating();
0047         }
0048     }
0049 
0050     Keys.onRightPressed: event => {
0051         if (readOnly) {
0052             event.accepted = false;
0053         } else {
0054             event.accepted = true;
0055             increaseRating();
0056         }
0057     }
0058 
0059     focusPolicy: readOnly ? Qt.NoFocus : Qt.StrongFocus
0060 
0061     padding: Kirigami.Units.smallSpacing
0062     // Reset paddings after qqc2-desktop-style Control
0063     topPadding: undefined
0064     leftPadding: undefined
0065     rightPadding: undefined
0066     bottomPadding: undefined
0067 
0068     contentItem: Row {
0069 
0070         spacing: 0
0071 
0072         Repeater {
0073             model: 5
0074 
0075             Item {
0076                 id: delegate
0077 
0078                 required property int index
0079 
0080                 readonly property int ratingThreshold: 2 + index * 2
0081 
0082                 width: Kirigami.Units.iconSizes.small
0083                 height: Kirigami.Units.iconSizes.small
0084 
0085                 Kirigami.Icon {
0086                     width: Kirigami.Units.iconSizes.small
0087                     height: Kirigami.Units.iconSizes.small
0088                     anchors.centerIn: parent
0089 
0090                     layer.enabled: control.hoveredRating >= delegate.ratingThreshold
0091 
0092                     layer.effect: BrightnessContrast {
0093                         brightness: control.hoverBrightness
0094                         contrast: control.hoverContrast
0095                     }
0096 
0097                     animated: false
0098                     source: (control.starRating >= delegate.ratingThreshold || control.hoveredRating >= delegate.ratingThreshold)
0099                         ? Qt.resolvedUrl(elisaTheme.ratingIcon)
0100                         : Qt.resolvedUrl(elisaTheme.ratingUnratedIcon)
0101 
0102                     opacity: (control.starRating >= delegate.ratingThreshold || control.hoveredRating >= delegate.ratingThreshold)
0103                         ? 1 : 0.7
0104                 }
0105             }
0106         }
0107     }
0108 
0109     MouseArea {
0110         id: mouseArea
0111 
0112         anchors.fill: parent
0113 
0114         enabled: !control.readOnly
0115 
0116         acceptedButtons: Qt.LeftButton
0117         hoverEnabled: true
0118 
0119         onClicked: {
0120             if (control.starRating !== control.hoveredRating) {
0121                 control.starRating = control.hoveredRating
0122             } else {
0123                 control.starRating = 0
0124             }
0125             control.ratingEdited()
0126         }
0127     }
0128 
0129     background: Rectangle {
0130         color: "transparent"
0131         border.color: control.palette.highlight
0132         border.width: 1
0133         radius: Kirigami.Units.smallSpacing
0134 
0135         opacity: control.activeFocus && [Qt.TabFocusReason, Qt.BacktabFocusReason].includes(control.focusReason)
0136             ? 1 : 0
0137 
0138         Behavior on opacity {
0139             OpacityAnimator {
0140                 duration: Kirigami.Units.shortDuration
0141                 easing.type: Easing.InOutCubic
0142             }
0143         }
0144     }
0145 }