-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
235 lines (188 loc) Β· 5.61 KB
/
main.py
File metadata and controls
235 lines (188 loc) Β· 5.61 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
"""
Main script to launch Tramway Collector
"""
###############
### Imports ###
###############
### Python import ###
import os
### Kivy imports ###
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, NoTransition, Screen
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.app import App
### Module imports ###
from tools.tools import (
PATH_RESOURCES_FOLDER,
PATH_KIVY_FOLDER,
MOBILE_MODE
)
from tools.tools_collection import clean_unused_images
from tools.tools_kivy import (
highlight_text_color,
pink_color,
blue_color,
color_label,
Window,
background_color,
window_size,
ImprovedPopup
)
from screens import (
MenuWindow,
CollectionWindow,
GalleryWindow,
ImageEditionWindow,
SettingsWindow
)
if MOBILE_MODE:
from androidstorage4kivy import Chooser # type: ignore
######################
### Global classes ###
######################
class TopMenuLayout(FloatLayout):
"""
Class displaying the top of each menu
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
path_resources = PATH_RESOURCES_FOLDER
def back_to_general(self):
"""
Function to reopen the main menu.
Used when clicking on the tramway icon at the top.
Parameters
----------
None
Returns
-------
None
"""
self.parent.manager.init_screen("menu")
class MyScrollViewLayout(GridLayout):
"""
Class corresponding to the layout inside the scroll view
"""
def __init__(self, **kwargs):
super(MyScrollViewLayout, self).__init__(**kwargs)
self.size_hint_y = None
self.bind(minimum_height=self.setter('height'))
def reset_screen(self):
"""
Removes all widgets from the scrollview.
Parameters
----------
None
Returns
-------
None
"""
list_widgets = self.children[:]
for element in list_widgets:
self.remove_widget(element)
###############
### General ###
###############
class WindowManager(ScreenManager):
"""
Screen manager, which allows the navigations between the different menus.
"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.highlight_text_color = highlight_text_color
self.button_disabled_color = (
382 / 255, 382 / 255, 382 / 255, 1)
self.pink_color = pink_color
self.blue_color = blue_color
self.color_label = color_label
self.transition = NoTransition()
self.add_widget(Screen(name="opening"))
self.current = "opening"
self.list_former_screens = []
def init_screen(self, screen_name, gallery=None, tramway_image=None):
"""
Init the selected screen with the given informations and change the screen.
Parameters
----------
screen_name : str
Name of the screen.
gallery : Gallery | None, optional (default is None)
Gallery to use to load the screen if one is needed.
tramway_image : TramwayImage | None, optional (default is None)
TramwayImage instance to use to load the screen if one is needed.
Returns
-------
None
"""
self.list_former_screens.append(self.current)
self.current = screen_name
if screen_name in ["collection", "settings", "menu"]:
self.get_screen(screen_name).init_screen()
if screen_name == "image_edition":
self.get_screen("image_edition").init_screen(
gallery=gallery,
tramway_image=tramway_image
)
if screen_name == "gallery":
self.get_screen("gallery").init_screen(
gallery=gallery
)
class MainApp(App, Widget):
"""
Main class of the application.
"""
def build(self):
"""
Build the application.
Parameters
----------
None
Returns
-------
None
"""
Window.clearcolor = background_color
Window.size = window_size
self.icon = PATH_RESOURCES_FOLDER + "logo_collector_1024.png"
Window.bind(on_keyboard=self.key_input)
def on_start(self):
self.root_window.children[0].init_screen("menu")
return super().on_start()
def key_input(self, window, key, scancode, codepoint, modifier):
"""
Take into account the back arrow in Android, corresponding to the key 27 (escape).
Parameters
----------
window
key
scancode
codepoint
modifier
Returns
-------
None
"""
if key == 27:
widget = self._app_window.children[0]
if type(widget) == WindowManager:
if len(widget.list_former_screens) > 1:
self._app_window.children[0].current = widget.list_former_screens.pop(
)
return True
elif type(widget) == ImprovedPopup:
widget.dismiss()
return True
elif MOBILE_MODE and type(widget) == Chooser:
return True
return False
# Run the application
if __name__ == "__main__":
for file_name in os.listdir(PATH_KIVY_FOLDER):
if file_name.endswith(".kv"):
Builder.load_file(PATH_KIVY_FOLDER + file_name, encoding="utf-8")
MainApp().run()
clean_unused_images()