1717# ============= enthought library imports =======================
1818import os
1919
20+ from enable .api import Interactor
2021from enable .enable_traits import Pointer
2122from pyface .action .menu_manager import MenuManager
23+ from pyface .qt .QtCore import QPoint
2224from pyface .qt .QtGui import QToolTip
2325from traits .api import Any , Str , on_trait_change , Bool , List
2426from traitsui .menu import Action
@@ -60,6 +62,17 @@ class ExtractionLineAction(Action):
6062 chamber = Str
6163
6264
65+ class ExtractionLineMenuTool (Interactor ):
66+ parent = Any
67+
68+ def normal_right_down (self , event ):
69+ if event .handled :
70+ return
71+
72+ if self .parent is not None :
73+ self .parent .show_menu (event )
74+
75+
6376class ExtractionLineCanvas2D (SceneCanvas ):
6477 """ """
6578
@@ -96,6 +109,8 @@ class ExtractionLineCanvas2D(SceneCanvas):
96109
97110 _px = None
98111 _py = None
112+ canvas_state = Any
113+ _context_menu = Any
99114
100115 def __init__ (self , * args , ** kw ):
101116 super (ExtractionLineCanvas2D , self ).__init__ (* args , ** kw )
@@ -105,6 +120,7 @@ def __init__(self, *args, **kw):
105120 )
106121 overlay = ExtractionLineInfoOverlay (tool = tool , component = self )
107122 self .tool = tool
123+ self .tools .append (ExtractionLineMenuTool (component = self , parent = self ))
108124 self .tools .append (tool )
109125 self .overlays .append (overlay )
110126
@@ -130,6 +146,27 @@ def update_switch_state(self, name, nstate, refresh=True, mode=None):
130146 if refresh :
131147 self .invalidate_and_redraw ()
132148
149+ def apply_canvas_state (self , state , refresh = True ):
150+ self .canvas_state = state
151+ for name , valve_state in state .valves .items ():
152+ self .set_valve_visual_state (name , valve_state , refresh = False )
153+
154+ if refresh :
155+ self .invalidate_and_redraw ()
156+
157+ def set_valve_visual_state (self , name , visual_state , refresh = True ):
158+ switch = self ._get_switch_by_name (name )
159+ if switch is None or visual_state is None :
160+ return
161+
162+ if hasattr (switch , "apply_visual_state" ):
163+ switch .apply_visual_state (visual_state )
164+ else :
165+ switch .state = visual_state .is_open
166+
167+ if refresh :
168+ self .invalidate_and_redraw ()
169+
133170 def update_switch_owned_state (self , name , owned ):
134171 switch = self ._get_switch_by_name (name )
135172 if switch is not None :
@@ -184,10 +221,10 @@ def normal_mouse_move(self, event):
184221 self .event_state = "select"
185222 if item != self .active_item :
186223 self .active_item = item
224+ if self .manager :
225+ self .manager .set_active_canvas_item (item )
187226 if isinstance (item , (BaseValve , Switch )):
188227 event .window .set_pointer (self .select_pointer )
189- if self .manager :
190- self .manager .set_selected_explanation_item (item )
191228 else :
192229 event .window .control .setToolTip ("" )
193230 QToolTip .hideText ()
@@ -196,7 +233,7 @@ def normal_mouse_move(self, event):
196233 self .event_state = "normal"
197234 event .window .set_pointer (self .normal_pointer )
198235 if self .manager :
199- self .manager .set_selected_explanation_item (None )
236+ self .manager .set_active_canvas_item (None )
200237
201238 def select_mouse_move (self , event ):
202239 """ """
@@ -211,6 +248,18 @@ def select_mouse_move(self, event):
211248 def select_right_down (self , event ):
212249 item = self .active_item
213250 if item is not None :
251+ if self .manager and isinstance (item , (BaseValve , Switch )):
252+ self .manager .set_selected_explanation_item (item )
253+ self ._show_menu (event , item )
254+ event .handled = True
255+
256+ def show_menu (self , event ):
257+ item = self ._over_item (event )
258+ if item is not None :
259+ self .active_item = item
260+ if self .manager and isinstance (item , (BaseValve , Switch )):
261+ self .manager .set_active_canvas_item (item )
262+ self .manager .set_selected_explanation_item (item )
214263 self ._show_menu (event , item )
215264 event .handled = True
216265
@@ -243,6 +292,9 @@ def set_state(state):
243292 if item is None :
244293 return
245294
295+ if self .manager and isinstance (item , (BaseValve , Switch )):
296+ self .manager .set_selected_explanation_item (item )
297+
246298 if self .edit_mode :
247299 self .event_state = "drag"
248300 event .window .set_pointer (self .drag_pointer )
@@ -348,9 +400,8 @@ def drag_mouse_leave(self, event):
348400 def on_lock (self ):
349401 item = self ._active_item
350402 if item :
351- item . soft_lock = lock = not item .soft_lock
403+ lock = not item .soft_lock
352404 self .manager .set_software_lock (item .name , lock )
353- self .request_redraw ()
354405
355406 def on_force_close (self ):
356407 self ._force_actuate (self .manager .close_valve , False )
@@ -414,31 +465,45 @@ def _action_factory(self, name, func, klass=None, **kw):
414465
415466 def _show_menu (self , event , obj ):
416467 actions = []
468+ allow_locking = self .manager .mode != "client" or globalv .client_only_locking
469+ allow_force = self .manager .mode != "client" and self .force_actuate_enabled
417470
418- if self .manager .mode != "client" or not globalv .client_only_locking :
419- # print self.active_item, isinstance(self.active_item, Switch)
420- # if isinstance(self.active_item, Switch):
421- if isinstance (self .active_item , BaseValve ):
422- t = "Lock"
423- if obj .soft_lock :
424- t = "Unlock"
471+ if allow_locking and isinstance (obj , BaseValve ):
472+ t = "Lock"
473+ if obj .soft_lock :
474+ t = "Unlock"
425475
426- action = self ._action_factory (t , "on_lock" )
427- actions .append (action )
476+ action = self ._action_factory (t , "on_lock" )
477+ actions .append (action )
428478
429- if self . force_actuate_enabled :
430- action = self ._action_factory ("Force Close" , "on_force_close" )
431- actions .append (action )
479+ if allow_force :
480+ action = self ._action_factory ("Force Close" , "on_force_close" )
481+ actions .append (action )
432482
433- action = self ._action_factory ("Force Open" , "on_force_open" )
434- actions .append (action )
483+ action = self ._action_factory ("Force Open" , "on_force_open" )
484+ actions .append (action )
435485
436486 if actions :
437487 menu_manager = MenuManager (* actions )
438488
439489 self ._active_item = self .active_item
440- menu = menu_manager .create_menu (event .window .control , None )
441- menu .show ()
490+ control = event .window .control
491+ menu = menu_manager .create_menu (control , None )
492+ self ._context_menu = menu
493+
494+ global_pos = getattr (event , "global_pos" , None )
495+ if global_pos is None and control is not None :
496+ size = control .size ()
497+ global_pos = control .mapToGlobal (
498+ QPoint (int (event .x ), int (size .height () - event .y ))
499+ )
500+
501+ if global_pos is not None and hasattr (menu , "exec_" ):
502+ menu .exec_ (global_pos )
503+ elif hasattr (menu , "exec_" ):
504+ menu .exec_ ()
505+ else :
506+ menu .show ()
442507
443508
444509# ============= EOF ====================================
0 commit comments