File: CompactRepresentation.qml

package info (click to toggle)
plasma-applet-redshift-control 1.0.18-2+deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 356 kB
  • sloc: xml: 79; sh: 14; makefile: 2
file content (193 lines) | stat: -rw-r--r-- 6,981 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright 2015  Martin Kotelnik <clearmartin@seznam.cz>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http: //www.gnu.org/licenses/>.
 */
import QtQuick 2.2
import QtQuick.Layouts 1.1
import QtGraphicalEffects 1.0
import org.kde.plasma.plasmoid 2.0
import org.kde.plasma.core 2.0 as PlasmaCore
import org.kde.plasma.components 2.0 as PlasmaComponents

Item {
    id: compactRepresentation

    property double itemWidth:  parent === null ? 0 : vertical ? parent.width : parent.height
    property double itemHeight: itemWidth

    Layout.preferredWidth: itemWidth
    Layout.preferredHeight: itemHeight

    property double fontPixelSize: itemWidth * 0.72
    property int temperatureIncrement: plasmoid.configuration.manualTemperatureStep
    property int temperatureMin: 1000
    property int temperatureMax: 25000

    // x100 for better counting
    property int brightnessIncrement: plasmoid.configuration.manualBrightnessStep * 100
    property int brightnessMin: 10
    property int brightnessMax: 100

    property bool textColorLight: ((theme.textColor.r + theme.textColor.g + theme.textColor.b) / 3) > 0.5
    property color bulbIconColourActive: theme.textColor
    property color bulbIconColourInactive: textColorLight ? Qt.tint(theme.textColor, '#80000000') : Qt.tint(theme.textColor, '#80FFFFFF')
    property color bulbIconColourCurrent: active ? bulbIconColourActive : bulbIconColourInactive
    property string customIconSource: active ? plasmoid.configuration.iconActive : plasmoid.configuration.iconInactive
    property color redshiftColour: '#ff3c0b'
    property color brightnessColour: '#39a2ee'

    property string versionString: 'N/A'

    PlasmaCore.IconItem {
        id: customIcon
        anchors.fill: parent
        visible: !plasmoid.configuration.useDefaultIcons
        source: customIconSource
    }

    PlasmaComponents.Label {
        id: bulbIcon
        anchors.centerIn: parent
        visible: plasmoid.configuration.useDefaultIcons

        font.family: 'FontAwesome'
        text: '\uf0eb'

        color: bulbIconColourCurrent
        font.pixelSize: fontPixelSize
        font.pointSize: -1

        ColorAnimation on color { id: animWheelTemperature; running: false; from: redshiftColour; to: bulbIconColourCurrent; duration: 1000 }
        ColorAnimation on color { id: animWheelBrighness;   running: false; from: brightnessColour; to: bulbIconColourCurrent; duration: 1000 }
    }

    PlasmaComponents.Label {
        id: manualIcon
        anchors.right: parent.right
        anchors.rightMargin: parent.width * 0.2
        anchors.bottom: parent.bottom
        anchors.bottomMargin: parent.height * 0.1

        font.family: 'FontAwesome'
        text: '\uf04c'

        color: textColorLight ? Qt.tint(theme.textColor, '#80FFFF00') : Qt.tint(theme.textColor, '#80FF3300')
        font.pixelSize: fontPixelSize * 0.3
        font.pointSize: -1
        verticalAlignment: Text.AlignBottom

        visible: manualEnabled
    }

    MouseArea {
        id: mouseArea
        anchors.fill: parent

        acceptedButtons: Qt.LeftButton | Qt.MiddleButton

        onWheel: {
            if (!manualEnabled) {
                manualTemperature = currentTemperature
                manualBrightness = currentBrightness
                redshiftDS.connectedSources.length = 0
                manualEnabled = true
                previouslyActive = active
                active = false
            }
            if (redshiftDS.connectedSources.length > 0) {
                return
            }
            if (wheel.angleDelta.y > 0) {
                // wheel up
                if (manualEnabledBrightness) {
                    manualBrightness += brightnessIncrement
                    if (manualBrightness > brightnessMax) {
                        manualBrightness = brightnessMax
                    }
                    currentBrightness = manualBrightness
                } else {
                    manualTemperature += temperatureIncrement
                    if (manualTemperature > temperatureMax) {
                        manualTemperature = temperatureMax
                    }
                }
            } else {
                // wheel down
                if (manualEnabledBrightness) {
                    manualBrightness -= brightnessIncrement
                    if (manualBrightness < brightnessMin) {
                        manualBrightness = brightnessMin
                    }
                    currentBrightness = manualBrightness
                } else {
                    manualTemperature -= temperatureIncrement
                    if (manualTemperature < temperatureMin) {
                        manualTemperature = temperatureMin
                    }
                }
            }
            if (parseFloat(versionString) >= 1.12) {
                redshiftDS.connectedSources.push(redshiftOneTimeCommand + " -P")
            } else {
                redshiftDS.connectedSources.push(redshiftOneTimeCommand)
            }
        }

        onClicked: {
            if (mouse.button === Qt.MiddleButton) {
                manualEnabledBrightness = !manualEnabledBrightness
                updateTooltip()
                if (manualEnabledBrightness) {
                    animWheelBrighness.running = false
                    animWheelTemperature.running = false
                    animWheelBrighness.running = true
                } else {
                    animWheelBrighness.running = false
                    animWheelTemperature.running = false
                    animWheelTemperature.running = true
                }
                return;
            }

            if (!manualEnabled) {
                toggleRedshift()
                return
            }

            manualEnabled = false
            if (previouslyActive) {
                toggleRedshift()
            } else {
                stopRedshift()
            }
        }
    }

    PlasmaCore.DataSource {
        id: getOptionsDS
        engine: 'executable'

        connectedSources: ['redshift -V']

        onNewData: {
            connectedSources.length = 0
            if (data['exit code'] > 0) {
                print('Error running redshift with command: ' + sourceName + '   ...stderr: ' + data.stderr)
                return
            }
            versionString = data.stdout.split(' ')[1]
        }
    }
}