Warning, /plasma/latte-dock/declarativeimports/components/BadgeText.qml is written in an unsupported language. File is not indexed.

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 import QtQuick 2.2
0007 import QtGraphicalEffects 1.0
0008 
0009 import org.kde.plasma.plasmoid 2.0
0010 
0011 Rectangle {
0012     property double proportion: 0
0013 
0014     property double previousProportion: 0
0015 
0016     property bool style3d: true
0017 
0018     property int numberValue
0019     property string textValue
0020 
0021     property bool fullCircle: true
0022     property bool showNumber: true
0023     property bool showText: false
0024     property bool textWithBackgroundColor: false
0025 
0026     property int radiusPerCentage: 100
0027     property int minimumWidth: 0
0028     property int maximumWidth: 9999
0029 
0030     property double circleOpacity: 1
0031     property double fontPixelSize: partSize // * 0.55
0032 
0033     property double stdThickness: partSize < 0 ? 0 : partSize  //  "/2.1"
0034     property double circleThicknessAttr: fullCircle ? 0 : stdThickness * 0.9
0035     property double partSize: height / 2
0036     property double pi2: Math.PI * 2
0037 
0038     width: Math.max(minimumWidth, valueText.width + 4*units.smallSpacing)
0039 
0040     color: theme.backgroundColor
0041     radius: (radiusPerCentage / 100) * (height / 2)
0042     border.width: 0 //Math.max(1,width/64)
0043 
0044     property int borderWidth: 1
0045     property real borderOpacity: 1
0046     property color borderColor: theme.textColor
0047     property color textColor: theme.textColor
0048     property color highlightedColor: theme.buttonFocusColor
0049 
0050     readonly property bool singleCharacter: (showNumber && numberValue<=9 && numberValue>=0)|| (showText && textValue.length===1)
0051 
0052     onProportionChanged: {
0053         if (proportion<0.03) {
0054             previousProportion = 0;
0055         }
0056 
0057         //console.log(previousProportion + " - "+proportion);
0058         var currentStep = (proportion - previousProportion);
0059         if ((currentStep >= 0.01) || (proportion>=1 && previousProportion !==1)) {
0060             //   console.log("request repaint...");
0061             previousProportion = proportion;
0062             repaint();
0063         }
0064     }
0065 
0066     function repaint() {
0067         canvas.requestPaint()
0068     }
0069 
0070     Canvas {
0071         id: canvas
0072 
0073         property int lineWidth: 1
0074         property bool fill: true
0075         property bool stroke: true
0076         property real alpha: 1.0
0077 
0078         // edge bleeding fix
0079         readonly property double filler: 0.01
0080 
0081         width: parent.width - 2 * parent.borderWidth
0082         height: parent.height - 2 * parent.borderWidth
0083         opacity: proportion > 0 ? 1 : 0
0084 
0085         anchors.centerIn: parent
0086 
0087         property color drawColor: highlightedColor
0088 
0089         onDrawColorChanged: requestPaint();
0090 
0091         onPaint: {
0092             var ctx = getContext('2d');
0093             ctx.clearRect(0, 0, canvas.width, canvas.height);
0094             ctx.fillStyle = drawColor;
0095 
0096             var startRadian = - Math.PI / 2;
0097 
0098             var radians = pi2 * proportion;
0099 
0100             ctx.beginPath();
0101             ctx.arc(width/2, height/2, stdThickness, startRadian, startRadian + radians + filler, false);
0102             ctx.arc(width/2, height/2, circleThicknessAttr, startRadian + radians + filler, startRadian, true);
0103 
0104             ctx.closePath();
0105             ctx.fill();
0106         }
0107     }
0108 
0109     Rectangle {
0110         id: badgerBackground
0111         anchors.fill: canvas
0112         color: canvas.drawColor
0113 
0114         visible: proportion === 1 && showNumber
0115         radius: parent.radius
0116     }
0117 
0118     Text {
0119         id: valueText
0120         anchors.centerIn: canvas
0121 
0122         width: Math.min(maximumWidth - 4*units.smallSpacing, implicitWidth)
0123         horizontalAlignment: Text.AlignHCenter
0124         verticalAlignment: Text.AlignVCenter
0125 
0126         elide: Text.ElideRight
0127 
0128         text: {
0129             if (showNumber) {
0130                 if (numberValue > 9999) {
0131                     return i18nc("Over 9999 new messages, overlay, keep short", "9,999+");
0132                 } else if (numberValue > 0) {
0133                     return numberValue.toLocaleString(Qt.locale(), 'f', 0);
0134                 }
0135             }
0136 
0137             if (showText) {
0138                 return textValue;
0139             }
0140 
0141             return "";
0142         }
0143         font.pixelSize: 0.62 * parent.height
0144         font.bold: true
0145         color: textWithBackgroundColor ? parent.color : parent.textColor
0146         visible: showNumber || showText
0147     }
0148 
0149     Rectangle{
0150         anchors.fill: parent
0151         anchors.topMargin: parent.borderWidth
0152         anchors.bottomMargin: parent.borderWidth
0153         anchors.leftMargin: parent.borderWidth
0154         anchors.rightMargin: parent.borderWidth
0155         color: "transparent"
0156         border.width: parent.borderWidth > 0 ? parent.borderWidth+1 : 0
0157         border.color: "black"
0158         radius: parent.radius
0159         opacity: 0.4
0160 
0161         visible: style3d
0162     }
0163 
0164     Rectangle{
0165         anchors.fill: parent
0166         border.width: parent.borderWidth
0167         border.color: {
0168             if (style3d) {
0169                 return parent.borderColor
0170             }
0171 
0172             return proportion === 1 ? parent.highlightedColor : parent.color
0173         }
0174         color: "transparent"
0175         radius: parent.radius
0176         opacity: parent.borderOpacity
0177     }
0178 }
0179