more cleanup
[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                         return;
102                 }
103
104                 /* set config for new snow flakes */
105                 plasmoid.configuration.userStyle = userStyle.textAt(userStyle.currentIndex);
106                 plasmoid.configuration.userSize = userSize.textAt(userSize.currentIndex);
107                 plasmoid.configuration.userSpeed = userSpeed.textAt(userSpeed.currentIndex);
108         
109                 if (!snowFalling.running || plasmoid.configuration.userSpeed == userSpeed.currentText) // don't update random speed if same
110                         return;
111
112                 /* update current snow flakes*/
113                 for (var i = 0; i < snowCount; i++) {
114                         snow[i].fallingSpeed = Utils.getFallingSpeed(userSpeed.currentText);
115                 }
116         }
117         
118         /* Update snow count works a bit differently; 
119          * can either kill all (implemented now) and start over;
120          * or slowly add/delete new snow flakes  */
121         function updateCount() {
122
123                 if (snowFalling == null) // quickhack
124                         return; 
125
126                 var alreadyRunning = snowFalling.running;
127                 
128                 destroySnow();
129                 plasmoid.configuration.userCount = userCount.textAt(userCount.currentIndex);
130                 initializeSnow();
131
132                 if (alreadyRunning)
133                         startSnow();
134
135         }
136
137         Layout.minimumWidth: units.gridUnit * 14;
138         Layout.minimumHeight: units.gridUnit * 10;
139
140         GridLayout {
141                 id: gridLayout
142                 rows: 5
143                 flow: GridLayout.TopToBottom
144                 anchors.fill: parent
145
146                 Label { text: "Speed"; color: theme.textColor; }
147                 Label { text: "Size"; color: theme.textColor; }
148                 Label { text: "Style"; color: theme.textColor;  }
149                 Label { text: "Count"; color: theme.textColor;  }
150                 Button { id: letitsnow; text: "Let it snow!"; onClicked: { initializeSnow(); startSnow(); } }
151
152                 ComboBox { id: userSpeed; width: 100; model: ["Slow", "Normal", "Fast"]; currentIndex: 1; onCurrentIndexChanged: updateSnow(); }        
153                 ComboBox { id: userSize; width: 100; model: ["Tiny", "Small", "Big"]; currentIndex: 2; onCurrentIndexChanged: updateSnow(); }   
154                 ComboBox { id: userStyle; width: 100; model: ["Classic", "Plain", "Romantic"]; currentIndex: 0; onCurrentIndexChanged: updateSnow(); }  
155                 ComboBox { id: userCount; width: 100; model: ["Few", "Medium", "Many"]; currentIndex: 1; onCurrentIndexChanged: updateCount(); }        
156                         
157                 Button { width: 100; text: "Stop"; onClicked: destroySnow(); }
158
159         }
160
161         Timer {
162
163                 id: snowFalling
164
165                 interval: 50; running: false; repeat: true
166                 
167                 onTriggered: {
168                         
169                         for (var i = 0; i < snowCount; i++) {
170                                 if(snow[i].dir == 0) {
171                                         snow[i].x += 2;
172                                 } else {
173                                         snow[i].x -= 2;
174                                 }
175                                 
176                                 if (snow[i].disposition++ % snow[i].swirl == 0) {
177                                         snow[i].dir = !snow[i].dir;
178                                 }
179
180                                 if(snow[i].x > screenWidth) {
181                                         snow[i].x = 0;
182                                 } else if (snow[i].x < 0) {
183                                         snow[i].x = screenWidth;
184                                 }
185
186                                 snow[i].y += snow[i].fallingSpeed;
187
188                                 if (snow[i].y > screenHeight) {
189                                         endSnowFlake(i);        
190                                 }
191
192                                 if(snow[i].rotationDirection == 0) {
193                                         snow[i].snowFlakeRotation += snow[i].rotationSpeed;
194                                 } else {
195                                         snow[i].snowFlakeRotation -= snow[i].rotationSpeed;
196                                 }
197
198                         }
199                 }
200         }
201
202 }