b6bb57ea4b3598d94ac5318c894a95af9859cc80
[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                 var alreadyRunning = snowFalling.running;
125                 
126                 destroySnow();
127                 plasmoid.configuration.userCount = userCount.textAt(userCount.currentIndex);
128                 initializeSnow();
129
130                 if (alreadyRunning)
131                         startSnow();
132
133         }
134
135          GridLayout {
136                 id: gridLayout
137                 rows: 5
138                 flow: GridLayout.TopToBottom
139                 anchors.fill: parent
140
141                 Label { text: "Speed"; color: theme.textColor; }
142                 Label { text: "Size"; color: theme.textColor; }
143                 Label { text: "Style"; color: theme.textColor;  }
144                 Label { text: "Count"; color: theme.textColor;  }
145                 Button { id: letitsnow; text: "Let it snow!"; onClicked: { initializeSnow(); startSnow(); } }
146
147                 ComboBox { id: userSpeed; width: 100; model: ["Slow", "Normal", "Fast"]; currentIndex: 1; onCurrentIndexChanged: updateSnow(); }        
148                 ComboBox { id: userSize; width: 100; model: ["Tiny", "Small", "Big"]; currentIndex: 2; onCurrentIndexChanged: updateSnow(); }   
149                 ComboBox { id: userStyle; width: 100; model: ["Classic", "Plain", "Romantic"]; currentIndex: 0; onCurrentIndexChanged: updateSnow(); }  
150                 ComboBox { id: userCount; width: 100; model: ["Few", "Medium", "Many"]; currentIndex: 1; onCurrentIndexChanged: updateCount(); }        
151                         
152                 Button { width: 100; text: "Stop"; onClicked: destroySnow(); }
153
154         }
155
156         Timer {
157
158                 id: snowFalling
159
160                 interval: 50; running: false; repeat: true
161                 
162                 onTriggered: {
163                         
164                         for (var i = 0; i < snowCount; i++) {
165                                 if(snow[i].dir == 0) {
166                                         snow[i].x += 2;
167                                 } else {
168                                         snow[i].x -= 2;
169                                 }
170                                 
171                                 if (snow[i].disposition++ % snow[i].swirl == 0) {
172                                         snow[i].dir = !snow[i].dir;
173                                 }
174
175                                 if(snow[i].x > screenWidth) {
176                                         snow[i].x = 0;
177                                 } else if (snow[i].x < 0) {
178                                         snow[i].x = 1440;
179                                 }
180
181                                 snow[i].y += snow[i].fallingSpeed;
182
183                                 if (snow[i].y > screenHeight) {
184                                         endSnowFlake(i);        
185                                 }
186
187                                 if(snow[i].rotationDirection == 0) {
188                                         snow[i].snowFlakeRotation += snow[i].rotationSpeed;
189                                 } else {
190                                         snow[i].snowFlakeRotation -= snow[i].rotationSpeed;
191                                 }
192
193                         }
194                 }
195         }
196
197 }