Skip to content

Commit 0f4cd20

Browse files
committed
gmoccapy -more dialog work
wait but don't block code added. entry dialogs now can use halui yes and no responses
1 parent db07465 commit 0f4cd20

File tree

2 files changed

+192
-167
lines changed

2 files changed

+192
-167
lines changed

src/emc/usr_intf/gmoccapy/dialogs.py

Lines changed: 114 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,27 @@ class Dialogs(GObject.GObject):
3636

3737
__gsignals__ = {
3838
'play_sound': (GObject.SignalFlags.RUN_FIRST, GObject.TYPE_NONE, (GObject.TYPE_STRING,)),
39-
'system-dialog-result': (GObject.SignalFlags.RUN_FIRST , GObject.TYPE_NONE, (GObject.TYPE_INT,)),
40-
'warning-dialog-result': (GObject.SignalFlags.RUN_FIRST , GObject.TYPE_NONE, (GObject.TYPE_INT, GObject.TYPE_STRING))
4139
}
4240

4341
def __init__(self, caller):
4442
GObject.GObject.__init__(self)
4543
self.sys_dialog = self.system_dialog(caller)
4644
self.warn_dialog = self.warning_dialog(caller)
45+
self.ent_dialog = self.entry_dialog(caller)
46+
self.yn_dialog = self.yesno_dialog(caller)
4747

48+
# sent from Gstat messages
49+
# first one found visibly gets the answer
50+
# maybe we should check focus?
4851
def dialog_ext_control(self, answer):
4952
if self.sys_dialog.get_visible():
5053
self.sys_dialog.response(answer)
5154
elif self.warn_dialog.get_visible():
5255
self.warn_dialog.response(answer)
56+
elif self.ent_dialog.get_visible():
57+
self.ent_dialog.response(answer)
58+
elif self.yn_dialog.get_visible():
59+
self.yn_dialog.response(answer)
5360

5461
# This dialog is for unlocking the system tab
5562
# The unlock code number is defined at the top of the page
@@ -77,66 +84,94 @@ def system_dialog(self, caller):
7784
return dialog
7885

7986
def show_system_dialog(self):
80-
self.sys_dialog._calc.set_value("")
81-
self.sys_dialog.show_all()
87+
dialog = self.sys_dialog
88+
dialog._calc.set_value("")
89+
dialog.show_all()
8290
self.emit("play_sound", "alert")
8391

84-
def on_system_response(self, dialog, result):
92+
# wait but don't block event loop
93+
dialog.RESPONSE = None
94+
while dialog.RESPONSE is None:
95+
while Gtk.events_pending():
96+
Gtk.main_iteration()
97+
98+
dialog.hide()
99+
85100
code = dialog._calc.get_value()
86-
print('Code:',code)
87101
rtn = -1
88-
if result == Gtk.ResponseType.ACCEPT:
102+
if dialog.RESPONSE == Gtk.ResponseType.ACCEPT:
89103
if code == int(dialog._caller.unlock_code):
90-
print('Yes')
91104
rtn = 1
92105
else:
93-
print('No')
94106
rtn = 0
95-
else:
96-
print('Cancelled')
97-
self.emit('system-dialog-result',rtn)
98-
dialog.hide()
99107

100-
def entry_dialog(self, caller, data = None, header = _("Enter value") , label = _("Enter the value to set"), integer = False):
101-
dialog = Gtk.Dialog(header,
108+
return rtn
109+
110+
def on_system_response(self, dialog, rtn):
111+
dialog.RESPONSE = rtn
112+
113+
def entry_dialog(self, caller):
114+
dialog = Gtk.Dialog('',
102115
caller.widgets.window1,
103116
Gtk.DialogFlags.DESTROY_WITH_PARENT)
104-
label = Gtk.Label(label)
105-
label.modify_font(Pango.FontDescription("sans 20"))
106-
label.set_margin_top(15)
107-
calc = gladevcp.Calculator()
117+
dialog.label = Gtk.Label('')
118+
dialog.label.modify_font(Pango.FontDescription("sans 20"))
119+
dialog.label.set_margin_top(15)
120+
dialog.calc = gladevcp.Calculator()
108121
content_area = dialog.get_content_area()
109-
content_area.pack_start(child=label, expand=False, fill=False, padding=0)
110-
content_area.add(calc)
111-
if data != None:
112-
calc.set_value(data)
113-
else:
114-
calc.set_value("")
115-
calc.set_property("font", "sans 20")
116-
calc.set_editable(True)
117-
calc.entry.connect("activate", lambda w : dialog.emit("response", Gtk.ResponseType.ACCEPT))
122+
content_area.pack_start(child=dialog.label, expand=False, fill=False, padding=0)
123+
content_area.add(dialog.calc)
124+
dialog.calc.set_property("font", "sans 20")
125+
dialog.calc.set_editable(True)
126+
dialog.calc.entry.connect("activate", lambda w : self.on_entry_response(dialog, Gtk.ResponseType.ACCEPT))
118127
dialog.parse_geometry("460x400")
119128
dialog.set_decorated(True)
129+
dialog.connect("response", self.on_entry_response)
130+
return dialog
131+
132+
def show_entry_dialog(self, data = None, header = _("Enter value") ,
133+
label = _("Enter the value to set"), integer = False):
134+
135+
dialog = self.ent_dialog
136+
if data != None:
137+
dialog.calc.set_value(data)
138+
else:
139+
dialog.calc.set_value("")
120140
if integer: # The user is only allowed to enter integer values, we hide some button
121-
calc.integer_entry_only(True)
122-
calc.num_pad_only(True)
141+
dialog.calc.integer_entry_only(True)
142+
dialog.calc.num_pad_only(True)
143+
dialog.label.set_text(label)
144+
dialog.set_title(header)
123145
dialog.show_all()
124-
response = dialog.run()
125-
value = calc.get_value()
126-
dialog.destroy()
127-
if response == Gtk.ResponseType.ACCEPT:
146+
147+
# wait but don't block event loop
148+
dialog.RESPONSE = None
149+
while dialog.RESPONSE is None:
150+
while Gtk.events_pending():
151+
Gtk.main_iteration()
152+
153+
dialog.hide()
154+
155+
value = dialog.calc.get_value()
156+
if dialog.RESPONSE == Gtk.ResponseType.ACCEPT:
128157
if value != None:
129-
if integer:
130-
return int(value)
158+
if dialog.calc.integer_only:
159+
qv = int(value)
131160
else:
132-
return float(value)
161+
qv = float(value)
133162
else:
134-
return "ERROR"
135-
return "CANCEL"
163+
qv = "ERROR"
164+
else:
165+
qv = "CANCEL"
166+
return qv
167+
168+
def on_entry_response(self, dialog, rtn):
169+
dialog.RESPONSE = rtn
136170

137171
# display warning dialog
138172
def warning_dialog(self, caller, message = '', secondary = None, title = _("Operator Message"),\
139173
sound = True, confirm_pin = 'warning-confirm', active_pin = None):
174+
140175
dialog = Gtk.MessageDialog(caller.widgets.window1,
141176
Gtk.DialogFlags.DESTROY_WITH_PARENT,
142177
Gtk.MessageType.INFO, Gtk.ButtonsType.NONE, message)
@@ -167,32 +202,34 @@ def periodic():
167202
dialog.connect("response", self.on_warning_response)
168203
return dialog
169204

170-
def show_warning_dialog(self, title, message, context=None, sound=True,\
205+
def show_warning_dialog(self, title, message, sound=True,
171206
confirm_pin = 'warning-confirm', active_pin = None):
172-
print(message,context)
173-
self.warn_dialog.context.append(context)
174-
self.warn_dialog.set_title(title)
175-
self.warn_dialog.format_secondary_text(message)
176-
self.warn_dialog.set_markup(message)
177-
self.warn_dialog.show_all()
207+
dialog = self.warn_dialog
208+
dialog.set_title(title)
209+
dialog.format_secondary_text(message)
210+
dialog.set_markup(message)
211+
dialog.show_all()
178212
if sound:
179213
self.emit("play_sound", "alert")
180-
print(self.warn_dialog.context)
181214

182-
def on_warning_response(self, dialog, rtn):
183-
context = dialog.context.pop()
184-
print(context)
185-
self.emit('warning-dialog-result', rtn, context)
215+
# wait but don't block event loop
216+
dialog.RESPONSE = None
217+
while dialog.RESPONSE is None:
218+
while Gtk.events_pending():
219+
Gtk.main_iteration()
220+
186221
dialog.hide()
187222

188-
def yesno_dialog(self, caller, message, title = _("Operator Message")):
223+
return dialog.RESPONSE
224+
225+
def on_warning_response(self, dialog, rtn):
226+
dialog.RESPONSE = rtn
227+
228+
def yesno_dialog(self, caller):
189229
dialog = Gtk.MessageDialog(caller.widgets.window1,
190230
Gtk.DialogFlags.DESTROY_WITH_PARENT,
191231
Gtk.MessageType.QUESTION,
192232
Gtk.ButtonsType.NONE)
193-
if title:
194-
dialog.set_title(str(title))
195-
dialog.set_markup(message)
196233
yes_button = Gtk.Button.new_with_mnemonic(_("_Yes"))
197234
no_button = Gtk.Button.new_with_mnemonic(_("_No"))
198235
yes_button.set_size_request(-1, 56)
@@ -206,11 +243,30 @@ def yesno_dialog(self, caller, message, title = _("Operator Message")):
206243
box.set_layout(Gtk.ButtonBoxStyle.CENTER)
207244
dialog.action_area.add(box)
208245
dialog.set_border_width(5)
246+
dialog.connect("response", self.on_yn_response)
247+
return dialog
248+
249+
def show_yesno_dialog(self, caller, message, title = _("Operator Message")):
250+
dialog = self.yn_dialog
251+
dialog.set_markup(message)
252+
if title:
253+
dialog.set_title(str(title))
209254
dialog.show_all()
210255
self.emit("play_sound", "alert")
211-
response = dialog.run()
212-
dialog.destroy()
213-
return response == Gtk.ResponseType.YES
256+
257+
# wait but don't block event loop
258+
dialog.RESPONSE = None
259+
while dialog.RESPONSE is None:
260+
while Gtk.events_pending():
261+
Gtk.main_iteration()
262+
263+
rtn = dialog.RESPONSE
264+
dialog.hide()
265+
return bool(rtn in(Gtk.ResponseType.YES, Gtk.ResponseType.ACCEPT))
266+
267+
# update internal variable so dialog will respond
268+
def on_yn_response(self,dialog, rtn):
269+
dialog.RESPONSE = rtn
214270

215271
def show_user_message(self, caller, message, title = _("Operator Message"), checkbox = False):
216272
dialog = Gtk.MessageDialog(caller.widgets.window1,

0 commit comments

Comments
 (0)