-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.py
123 lines (96 loc) · 3.98 KB
/
cookie.py
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
import os
from kivy import Config
from kivy.app import App
from kivy.clock import Clock
from kivy.graphics.context_instructions import Color
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
from shapebutton import ShapeButton
from picture import Picture
from pictureview import PictureView
from listview import ListView
from loadfiledialog import LoadFileDialog
from shapelabel import ShapeLabel
from dataconverter import convert
from savefiledialog import SaveFileDialog
import win32timezone
class Cookie(FloatLayout):
def __init__(self, **kwargs):
super(Cookie, self).__init__(**kwargs)
self.ids.picture.bind(last_drawing=self.drawing_callback)
self.ids.list_view.layout_manager.bind(selected_nodes=self.drawing_selection_callback)
self.drawings = []
self.last_selected = -1
self.popup = None
def enable_draw_mode(self, mode):
self.ids.picture.draw_mode = mode
self.ids.picture_view.scroll_timeout = 1
self.ids.picture_view.scroll_distance = 100000
def disable_draw_mode(self):
self.ids.picture_view.scroll_timeout = 200
self.ids.picture_view.scroll_distance = 20
self.ids.picture.draw_mode = ""
def shape_button_click(self, button):
next_state = not button.selected
self.deactivate_buttons()
button.selected = next_state
if next_state:
self.enable_draw_mode(button.name)
def deactivate_buttons(self):
self.disable_draw_mode()
for key in self.ids:
if isinstance(self.ids[key], ShapeButton):
self.ids[key].selected = False
def drawing_callback(self, instance, value):
Clock.schedule_once(lambda dt: self.ids.list_view.data.append(convert(self.ids.picture.draw_mode, value)))
self.ids.list_view.layout_manager.clear_selection()
self.drawings.append(value)
def drawing_selection_callback(self, instance, value):
if len(value) == 0:
return
self.paint_object(self.drawings[self.last_selected], Color(1, 1, 0))
self.last_selected = value[0]
self.paint_object(self.drawings[self.last_selected], Color(1, 0, 0))
def paint_object(self, instance, color):
self.ids.picture.canvas.remove(instance)
instance.remove(instance.children[0])
instance.insert(0, color)
self.ids.picture.canvas.add(instance)
self.ids.picture.canvas.ask_update()
def delete_drawing(self):
if len(self.drawings) == 0:
return
self.ids.picture.canvas.remove(self.drawings[self.last_selected])
del self.drawings[self.last_selected]
del self.ids.list_view.data[self.last_selected]
self.last_selected = -1
self.ids.list_view.layout_manager.clear_selection()
def open_file(self):
self.popup = Popup(title="Open File", content=LoadFileDialog(load=self.load_file, cancel=self.dismiss_popup),
size_hint=(0.9, 0.9))
self.popup.open()
def load_file(self, filename):
self.ids.picture.source = filename
self.dismiss_popup()
def dismiss_popup(self):
self.popup.dismiss()
def save_file(self):
self.popup = Popup(title="Save File", content=SaveFileDialog(save_file=self.save, cancel=self.dismiss_popup),
size_hint=(0.9, 0.9))
self.popup.open()
def save(self, path, filename):
self.dismiss_popup()
full_path = os.path.join(path, filename)
self.write_data(full_path)
def write_data(self, path):
with open(path, "w") as f:
f.write("Type,Length,Area,Volume\n")
for item in self.ids.list_view.data:
line = item['type'] + ',' + str(item['length']) + ',' + str(item['area']) + ',' + str(item['volume'])
f.write(line + '\n')
class CookieApp(App):
def build(self):
return Cookie()
if __name__ == '__main__':
Config.read('config.ini')
CookieApp().run()