This repository was archived by the owner on May 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
185 lines (159 loc) · 6.48 KB
/
main.py
File metadata and controls
185 lines (159 loc) · 6.48 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
from platform import system
import os
if system() == 'Windows':
## Needed to run the application if user have OpenGL < v2.0
os.environ['KIVY_GL_BACKEND'] = 'angle_sdl2'
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0') ## Disable closing app on escape
Config.set('graphics', 'window_state', 'maximized') ## Set screen to Maximized
Config.set('input', 'mouse', 'mouse,multitouch_on_demand') ## Turns off red circles on left click
try:
from kivy.resources import resource_add_path
from pathlib import Path, PurePath
import sys
if hasattr(sys, "_MEIPASS"): ## App is frozen
## Bundle with PyInstaller
os.environ["ELPCD_ROOT"] = sys._MEIPASS
else:
## If app is NOT frozen, the path is set to the parent of this file
os.environ["ELPCD_ROOT"] = str(PurePath(Path(__file__).resolve()).parent)
resource_add_path(os.environ["ELPCD_ROOT"]) ## Add path to kivy resources
except:
quit()
else:
import lib.paths
Config.set('kivy', 'window_icon', lib.paths.ICON)
from kivy.uix.screenmanager import ScreenManager, FadeTransition
from kivymd.uix.button import MDFlatButton, MDRaisedButton
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.snackbar import Snackbar
from kivymd.uix.dialog import MDDialog
from kivymd.toast import toast
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.clock import Clock
import kivy.properties as prop
from lib.py.data_management import DataManagement
from lib.csv_export import ExportCSV
from lib.py.pcd_tree import PCDTree
import lib.data_cls
class MainFrame(MDBoxLayout):
"""Main Frame of the application"""
app = None
export_dialog = prop.ObjectProperty()
def __init__(self, **kwargs):
super().__init__(**kwargs)
## Set up var to interact with main application \/
self.app = MDApp.get_running_app()
## Add standard widgets to main frame \/
self.ids.tree_frame.add_widget(self.app.pcd_tree)
def switch_to_screen(self, screen, duration=0.2):
"""Switch to screen
:param screen: str
:param duration: float
"""
screen = self.ids.screen_manager.get_screen(screen)
self.ids.screen_manager.switch_to(screen, duration=duration)
def _export_csv(self, *args):
"""Callback for export dialog popup button"""
try:
## try exporting data into .csv file
self.export.export_data()
except lib.utils.NotAbleToWriteFile:
toast('Não foi possível criar o arquivo!')
else:
## shows snackbar with path to new .csv file
self.export_dialog.dismiss()
snackbar = Snackbar(
text=f'PCD exportado para {self.export.path_to_file}',
duration=10
)
snackbar.show()
def confirm_export_dialog(self):
"""Create and open export dialog popup"""
btn_cancel = MDFlatButton(
text = 'Cancelar',
theme_text_color = 'Custom',
text_color = self.app.theme_cls.primary_color
)
btn_confirm = MDRaisedButton(
text= 'Exportar .CSV',
elevation= 11,
on_release= self._export_csv
)
self.export_dialog = MDDialog(
title= 'Deseja exportar seu PCD?',
text= f'O arquivo será salvo em\n{self.export.get_path()}',
auto_dismiss= False,
buttons= [btn_cancel,btn_confirm])
self.export_dialog.buttons[0].bind(on_release= self.export_dialog.dismiss)
self.export_dialog.open()
def open_export_dialog(self, *args):
"""Callback for Export button"""
self.export = ExportCSV(name='PCD') ## Prepares for file export
self.confirm_export_dialog() ## Opens dialog
class Manager(ScreenManager):
"""Screen Manager Class"""
def __init__(self, **kwargs):
super().__init__(**kwargs)
## Sets transition of Screen Manager \/
self.transition = FadeTransition()
class ElPCD(MDApp):
"""Main App Class"""
## Repository name, user will be able to change in the future \/
REPOSITORY = prop.StringProperty('ElPCD')
LOGO = prop.StringProperty(lib.paths.LOGO)
## Place holders for APP objects \/
cursor = prop.ObjectProperty() ## Sqlite cursor
pcd_tree = prop.ObjectProperty() ## PCD TreeView + widgets object
connection = prop.ObjectProperty() ## Sqlite connection
data_management = prop.ObjectProperty() ## Data Management object, *text fields*
def __init__(self, **kwargs):
super().__init__(**kwargs)
## Theming \/
self.theme_cls.primary_palette = "Blue"
self.theme_cls.accent_palette = "Gray"
self.theme_cls.primary_hue = '900'
## Tries to set Sqlite connection and cursor \/
try:
import sqlite3
db_path = Path(self.user_data_dir) / 'database.db'
self.connection = sqlite3.connect(str(db_path.resolve()))
self.cursor = self.connection.cursor()
## Sets up table PCD in sqlite \/
lib.data_cls.create_tables()
except:
self.stop()
## Instantiation of TreeView objects \/
self.pcd_tree = PCDTree()
def set_data_management_widget(self, view_only=True, item_data=None, new_cls=False):
"""Clear and add new Data Management object,
`view_only` locks the text fields,
`item_data` stores database row,
`new_cls` creates an item without any parents.
:param view_only: bool
:param item_data: dict
:param new_cls: bool
"""
self.root.ids.data_frame.clear_widgets()
self.data_management = DataManagement(view_only=view_only,item_data=item_data,new_cls=new_cls)
self.root.ids.data_frame.add_widget(self.data_management)
def on_start(self):
## Switch off of welcome screen \/
switch = lambda *_: self.root.switch_to_screen('pcd', duration = 1)
self.welcome_event = Clock.schedule_once(switch, 1.5)
def on_stop(self):
## Close Sqlite connection \/
self.connection.close()
def open_settings(self, *args):
"""Override to disable settings panel opening"""
return False
def build(self):
"""Main APP builder"""
return MainFrame() ## Main Frame object
if __name__ == "__main__":
## Load .kv files
for item in lib.paths.KV.iterdir():
Builder.load_file(str((lib.paths.KV / item).resolve()))
## Execute app
ElPCD().run()