-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgcode_help.py
More file actions
142 lines (123 loc) · 4.19 KB
/
Copy pathgcode_help.py
File metadata and controls
142 lines (123 loc) · 4.19 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
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.logger import Logger
from kivy.properties import NumericProperty
import kivy.core.text
from kivy.core.window import Window
Builder.load_string('''
<GCHRow@Label>:
canvas.before:
Color:
rgba: 0.5, 0.5, 0.5, 1
Rectangle:
size: self.size
pos: self.pos
text_size: self.size
#padding_x: dp(4)
<GcodeHelp>:
rv: rv
on_enter: root.populate('G')
BoxLayout:
canvas:
Color:
rgba: 0.3, 0.3, 0.3, 1
Rectangle:
size: self.size
pos: self.pos
rv: rv
orientation: 'vertical'
BoxLayout:
size_hint_y: None
height: 40
Button:
text: 'GCodes'
on_press: root.populate('G')
Button:
text: 'MCodes'
on_press: root.populate('M')
Button:
text: 'Commands'
on_press: root.populate(' ')
Button:
text: '$Codes'
on_press: root.populate('$')
Button:
text: 'Pins'
on_press: root.populate('P')
Button:
text: 'Headers'
on_press: root.populate('H')
Button:
text: 'Fnc'
on_press: root.populate('F')
Button:
text: 'Back'
on_press: root.close()
RecycleView:
id: rv
scroll_type: ['bars', 'content']
scroll_wheel_distance: dp(114)
bar_width: dp(10)
viewclass: 'GCHRow'
RecycleGridLayout:
cols: 2
cols_minimum: {0: root.max_width, 1: 1280}
default_size: None, dp(20)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
spacing: dp(2)
''')
class GcodeHelp(Screen):
max_width = NumericProperty(0)
def _get_str_pixel_width(self, string, **kwargs):
return kivy.core.text.Label(**kwargs).get_extents(string)[0]
def populate(self, type):
self.rv.data = []
self.max_width = 0
fn = '{}/gcodes.txt'.format(App.get_running_app().running_directory)
try:
with open(fn, encoding="utf-8") as f:
for line in f:
if line[0] == type or (type == ' ' and line[0].islower()):
c = line.split(' | ')
if len(c) < 2:
continue
g = c[0].strip()
d = c[1].strip()
w = self._get_str_pixel_width(g, font_name="RobotoMono-Regular.ttf", font_size=14) + kivy.metrics.dp(8)
if w > self.max_width:
self.max_width = w
self.rv.data.append({'text': g, 'font_name': "RobotoMono-Regular.ttf", 'font_size': 14})
self.rv.data.append({'text': d, 'font_name': "Roboto-Regular.ttf", 'font_size': 14})
except Exception as ex:
Logger.error("GcodeHelp: Can't open {} - {}".format(fn, ex))
def close(self):
self.rv.data = []
self.manager.current = 'main'
if __name__ == '__main__':
import os
Builder.load_string('''
<ExitScreen>:
on_enter: app.stop()
''')
class ExitScreen(Screen):
pass
class MainWindow:
def display(self, x):
print(x)
class GCodeHelpApp(App):
def __init__(self, **kwargs):
super(GCodeHelpApp, self).__init__(**kwargs)
self.main_window = MainWindow()
self.running_directory = os.path.dirname(os.path.realpath(__file__))
def build(self):
Window.size = (800, 600)
self.sm = ScreenManager()
self.sm.add_widget(GcodeHelp(name='gcodehelp'))
self.sm.add_widget(ExitScreen(name='main'))
self.sm.current = 'gcodehelp'
return self.sm
GCodeHelpApp().run()