-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
dialogs.py
256 lines (233 loc) · 10.3 KB
/
dialogs.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python3
'''
This class is used to handle the dialogs from gmoccapy,
it is just a copy of a class from gscreen and has been slightly modified
Copyright 2014 Norbert Schechner
nieson@web.de
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
'''
import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk
from gi.repository import GObject
from gi.repository import GLib
from gi.repository import Pango
import gladevcp
class Dialogs(GObject.GObject):
__gtype_name__ = 'Dialogs'
__gsignals__ = {
'play_sound': (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)),
}
def __init__(self):
GObject.GObject.__init__(self)
# This dialog is for unlocking the system tab
# The unlock code number is defined at the top of the page
def system_dialog(self, caller):
dialog = Gtk.Dialog(_("Enter System Unlock Code"),
caller.widgets.window1,
Gtk.DialogFlags.DESTROY_WITH_PARENT)
label = Gtk.Label(_("Enter System Unlock Code"))
label.modify_font(Pango.FontDescription("sans 20"))
calc = gladevcp.Calculator()
dialog.vbox.pack_start(label, False, False, 0)
dialog.vbox.add(calc)
calc.set_value("")
calc.set_property("font", "sans 20")
calc.set_editable(True)
calc.integer_entry_only(True)
calc.num_pad_only(True)
calc.entry.connect("activate", lambda w : dialog.emit("response", Gtk.ResponseType.ACCEPT))
dialog.parse_geometry("360x400")
dialog.set_decorated(True)
dialog.show_all()
self.emit("play_sound", "alert")
response = dialog.run()
code = calc.get_value()
dialog.destroy()
if response == Gtk.ResponseType.ACCEPT:
if code == int(caller.unlock_code):
return True
return False
def entry_dialog(self, caller, data = None, header = _("Enter value") , label = _("Enter the value to set"), integer = False):
dialog = Gtk.Dialog(header,
caller.widgets.window1,
Gtk.DialogFlags.DESTROY_WITH_PARENT)
label = Gtk.Label(label)
label.modify_font(Pango.FontDescription("sans 20"))
label.set_margin_top(15)
calc = gladevcp.Calculator()
content_area = dialog.get_content_area()
content_area.pack_start(child=label, expand=False, fill=False, padding=0)
content_area.add(calc)
if data != None:
calc.set_value(data)
else:
calc.set_value("")
calc.set_property("font", "sans 20")
calc.set_editable(True)
calc.entry.connect("activate", lambda w : dialog.emit("response", Gtk.ResponseType.ACCEPT))
dialog.parse_geometry("460x400")
dialog.set_decorated(True)
if integer: # The user is only allowed to enter integer values, we hide some button
calc.integer_entry_only(True)
calc.num_pad_only(True)
dialog.show_all()
response = dialog.run()
value = calc.get_value()
dialog.destroy()
if response == Gtk.ResponseType.ACCEPT:
if value != None:
if integer:
return int(value)
else:
return float(value)
else:
return "ERROR"
return "CANCEL"
# display warning dialog
def warning_dialog(self, caller, message, secondary = None, title = _("Operator Message"),\
sound = True, confirm_pin = 'warning-confirm', active_pin = None):
dialog = Gtk.MessageDialog(caller.widgets.window1,
Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.INFO, Gtk.ButtonsType.NONE, message)
# if there is a secondary message then the first message text is bold
if secondary:
dialog.format_secondary_text(secondary)
ok_button = Gtk.Button.new_with_mnemonic("_Ok")
ok_button.set_size_request(-1, 56)
ok_button.connect("clicked",lambda w:dialog.response(Gtk.ResponseType.OK))
box = Gtk.HButtonBox()
box.add(ok_button)
dialog.action_area.add(box)
dialog.set_border_width(5)
dialog.show_all()
if sound:
self.emit("play_sound", "alert")
dialog.set_title(title)
def periodic():
if caller.halcomp[confirm_pin]:
dialog.response(Gtk.ResponseType.OK)
return False
if active_pin is not None:
if not caller.halcomp[active_pin]:
dialog.response(Gtk.ResponseType.CANCEL)
return False
return True
GLib.timeout_add(100, periodic)
response = dialog.run()
dialog.destroy()
return response == Gtk.ResponseType.OK
def yesno_dialog(self, caller, message, title = _("Operator Message")):
dialog = Gtk.MessageDialog(caller.widgets.window1,
Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.QUESTION,
Gtk.ButtonsType.NONE)
if title:
dialog.set_title(str(title))
dialog.set_markup(message)
yes_button = Gtk.Button.new_with_mnemonic(_("_Yes"))
no_button = Gtk.Button.new_with_mnemonic(_("_No"))
yes_button.set_size_request(-1, 56)
no_button.set_size_request(-1, 56)
yes_button.connect("clicked",lambda w:dialog.response(Gtk.ResponseType.YES))
no_button.connect("clicked",lambda w:dialog.response(Gtk.ResponseType.NO))
box = Gtk.HButtonBox()
box.add(no_button)
box.add(yes_button)
box.set_spacing(10)
box.set_layout(Gtk.ButtonBoxStyle.CENTER)
dialog.action_area.add(box)
dialog.set_border_width(5)
dialog.show_all()
self.emit("play_sound", "alert")
response = dialog.run()
dialog.destroy()
return response == Gtk.ResponseType.YES
def show_user_message(self, caller, message, title = _("Operator Message")):
dialog = Gtk.MessageDialog(caller.widgets.window1,
Gtk.DialogFlags.DESTROY_WITH_PARENT,
Gtk.MessageType.INFO,
Gtk.ButtonsType.NONE)
if title:
dialog.set_title(str(title))
dialog.set_markup(message)
ok_button = Gtk.Button.new_with_mnemonic(_("_Ok"))
ok_button.set_size_request(-1, 56)
ok_button.connect("clicked",lambda w:dialog.response(Gtk.ResponseType.OK))
box = Gtk.HButtonBox()
box.add(ok_button)
dialog.action_area.add(box)
dialog.set_border_width(5)
dialog.show_all()
self.emit("play_sound", "alert")
response = dialog.run()
dialog.destroy()
return response == Gtk.ResponseType.OK
# dialog for run from line
def restart_dialog(self, caller):
# highlight the gcode down one line lower
# used for run-at-line restart
def restart_down(widget, obj, calc):
obj.widgets.gcode_view.line_down()
line = int(obj.widgets.gcode_view.get_line_number())
calc.set_value(line)
# highlight the gcode down one line higher
# used for run-at-line restart
def restart_up(widget, obj, calc):
obj.widgets.gcode_view.line_up()
line = int(obj.widgets.gcode_view.get_line_number())
calc.set_value(line)
# highlight the gcode of the entered line
# used for run-at-line restart
def on_enter_button(widget, obj, calc):
line = int(calc.get_value())
obj.start_line = line
obj.widgets.gcode_view.set_line_number(line)
restart_dialog = Gtk.Dialog(_("Restart Entry"),
caller.widgets.window1, Gtk.DialogFlags.DESTROY_WITH_PARENT)
label = Gtk.Label(_("Restart Entry"))
label.modify_font(Pango.FontDescription("sans 20"))
restart_dialog.vbox.pack_start(label, False, False, 0)
calc = gladevcp.Calculator()
restart_dialog.vbox.add(calc)
calc.set_value("%d" % caller.widgets.gcode_view.get_line_number())
calc.set_property("font", "sans 20")
calc.set_editable(True)
calc.entry.connect("activate", on_enter_button, caller, calc)
calc.integer_entry_only(True)
calc.num_pad_only(True)
# add additional buttons
upbutton = Gtk.Button.new_with_mnemonic(_("_Up "))
upbutton.set_image(Gtk.Image.new_from_icon_name("go-up", Gtk.IconSize.BUTTON))
downbutton = Gtk.Button.new_with_mnemonic(_("_Down"))
downbutton.set_image(Gtk.Image.new_from_icon_name("go-down", Gtk.IconSize.BUTTON))
enterbutton = Gtk.Button.new_with_mnemonic(_("_Jump to"))
enterbutton.set_image(Gtk.Image.new_from_icon_name("go-jump", Gtk.IconSize.BUTTON))
calc.table.attach(upbutton,3,1,1,1)
calc.table.attach(downbutton,3,2,1,1)
calc.table.attach(enterbutton,3,3,1,1)
upbutton.connect("clicked", restart_up, caller, calc)
downbutton.connect("clicked", restart_down, caller, calc)
enterbutton.connect("clicked", on_enter_button, caller, calc)
restart_dialog.parse_geometry("410x400+0+0")
restart_dialog.show_all()
result = restart_dialog.run()
restart_dialog.destroy()
if result == Gtk.ResponseType.REJECT:
line = 0
else:
line = int(calc.get_value())
if line == None:
line = 0
caller.widgets.gcode_view.set_line_number(line)
caller.start_line = line