8f59156d3158408215d24bb59b9f332d497129a1
[qt-letitsnow] / contents / ui / main.qml
1 /*
2  *   Copyright 2020 Robin Krens <robin@robinkrens.nl>
3  *
4  *   This program is free software; you can redistribute it and/or modify
5  *   it under the terms of the GNU Library General Public License as
6  *   published by the Free Software Foundation; either version 2 or
7  *   (at your option) any later version.
8  *
9  *   This program is distributed in the hope that it will be useful,
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *   GNU General Public License for more details
13  *
14  *   You should have received a copy of the GNU Library General Public
15  *   License along with this program; if not, write to the
16  *   Free Software Foundation, Inc.,
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 import QtQuick 2.3
21 import QtQuick.Controls 1.2
22 import QtQuick.Layouts 1.0
23
24 import org.kde.plasma.plasmoid 2.0 as Plasmoid
25 import org.kde.plasma.core 2.0 as PlasmaCore
26 import org.kde.plasma.components 2.0 as PlasmaComponents
27
28 import "../code/utils.js" as Utils
29
30 Item {
31         id: root;
32         
33         property var snow: [];
34         property int snowCount: Utils.getSnowCount(plasmoid.configuration.userCount);
35         readonly property int screenWidth: Qt.application.screens[0].width;
36         readonly property int screenHeight: Qt.application.screens[0].height;
37
38         width: 250;
39         height: 180;
40
41         function initializeSnow() {
42
43                 if (snowFalling.running)
44                         return;
45
46                 for (var i = 0; i < snowCount; i++) {
47                                 
48                         var component = Qt.createComponent("snowWindow.qml");
49                         
50                         snow[i] = component.createObject(root);
51                                 
52                         snow[i].x = Math.random() * screenWidth;
53                         snow[i].y = Math.random() * screenHeight;
54                         snow[i].width = (Math.random() * 100) % snow[i].radius;
55                         snow[i].height = snow[i].width;
56                 }
57         }
58
59         function startSnow() {
60
61                 for (var i = 0; i < snowCount; i++) {
62                         snow[i].showMaximized();
63                 }
64                 snowFalling.running = true;
65         }
66
67         /* End snow flake and start a 'random' new one */
68         function endSnowFlake(i) { 
69                 
70                 snow[i].close();
71                 snow[i].destroy();
72                 
73                 var component = Qt.createComponent("snowWindow.qml");
74                 snow[i] = component.createObject(root);
75         
76                 snow[i].x = Math.random() * screenWidth;
77                 snow[i].y = -10;
78                 snow[i].width = (Math.random() * 100) % snow[i].radius;
79                 snow[i].height = snow[i].width;
80
81                 snow[i].showMaximized();
82
83         }
84
85         function destroySnow() {
86
87                 if (!snowFalling.running)
88                         return;
89
90                 snowFalling.running = false;
91                 
92                 for (var i = 0; i < snowCount; i++) {
93                         snow[i].close();
94                         snow[i].destroy();
95                 }
96         }
97
98         function updateSnow() {
99
100                 if (userSpeed.currentText == '') { // hack
101                         //console.log('not initialized yet');   
102                         return;
103                 }
104
105                 /* set config for new snow flakes */
106                 plasmoid.configuration.userStyle = userStyle.textAt(userStyle.currentIndex);
107                 plasmoid.configuration.userSize = userSize.textAt(userSize.currentIndex);
108                 plasmoid.configuration.userSpeed = userSpeed.textAt(userSpeed.currentIndex);
109         
110                 if (!snowFalling.running || plasmoid.configuration.userSpeed == userSpeed.currentText) // don't update random speed if same
111                         return;
112
113                 /* update current snow flakes*/
114                 for (var i = 0; i < snowCount; i++) {
115                         snow[i].fallingSpeed = Utils.getFallingSpeed(userSpeed.currentText);
116                 }
117         }
118         
119         /* Update snow count works a bit differently; 
120          * can either kill all (implemented now) and start over;
121          * or slowly add/delete new snow flakes  */
122         function updateCount() {
123
124                 if (snowFalling == null) // quickhack
125                         return; 
126
127                 var alreadyRunning = snowFalling.running;
128                 
129                 destroySnow();
130                 plasmoid.configuration.userCount = userCount.textAt(userCount.currentIndex);
131                 initializeSnow();
132
133                 if (alreadyRunning)
134                         startSnow();
135
136         }
137
138         //Plasmoid.Plasmoid.preferredRepresentation: Plasmoid.fullRepresentation
139         Layout.minimumWidth: units.gridUnit * 13;
140         Layout.minimumHeight: units.gridUnit * 10;
141
142         GridLayout {
143                 id: gridLayout
144                 rows: 5
145                 flow: GridLayout.TopToBottom
146                 anchors.fill: parent
147
148                 Label { text: "Speed"; color: theme.textColor; }
149                 Label { text: "Size"; color: theme.textColor; }
150                 Label { text: "Style"; color: theme.textColor;  }
151                 Label { text: "Count"; color: theme.textColor;  }
152                 Button { id: letitsnow; text: "Let it snow!"; onClicked: { initializeSnow(); startSnow(); } }
153
154                 ComboBox { id: userSpeed; width: 100; model: ["Slow", "Normal", "Fast"]; currentIndex: 1; onCurrentIndexChanged: updateSnow(); }        
155                 ComboBox { id: userSize; width: 100; model: ["Tiny", "Small", "Big"]; currentIndex: 2; onCurrentIndexChanged: updateSnow(); }   
156                 ComboBox { id: userStyle; width: 100; model: ["Classic", "Plain", "Romantic"]; currentIndex: 0; onCurrentIndexChanged: updateSnow(); }  
157                 ComboBox { id: userCount; width: 100; model: ["Few", "Medium", "Many"]; currentIndex: 1; onCurrentIndexChanged: updateCount(); }        
158                         
159                 Button { width: 100; text: "Stop"; onClicked: destroySnow(); }
160
161         }
162 //}
163
164         Timer {
165
166                 id: snowFalling
167
168                 interval: 50; running: false; repeat: true
169                 
170                 onTriggered: {
171                         
172                         for (var i = 0; i < snowCount; i++) {
173                                 if(snow[i].dir == 0) {
174                                         snow[i].x += 2;
175                                 } else {
176                                         snow[i].x -= 2;
177                                 }
178                                 
179                                 if (snow[i].disposition++ % snow[i].swirl == 0) {
180                                         snow[i].dir = !snow[i].dir;
181                                 }
182
183                                 if(snow[i].x > screenWidth) {
184                                         snow[i].x = 0;
185                                 } else if (snow[i].x < 0) {
186                                         snow[i].x = 1440;
187                                 }
188
189                                 snow[i].y += snow[i].fallingSpeed;
190
191                                 if (snow[i].y > screenHeight) {
192                                         endSnowFlake(i);        
193                                 }
194
195                                 if(snow[i].rotationDirection == 0) {
196                                         snow[i].snowFlakeRotation += snow[i].rotationSpeed;
197                                 } else {
198                                         snow[i].snowFlakeRotation -= snow[i].rotationSpeed;
199                                 }
200
201                         }
202                 }
203         }
204
205 }