Mercurial > libervia-web
comparison src/browser/sat_browser/base_widget.py @ 603:462d0458e679 frontends_multi_profiles
browser_side: base_widget.DropCell raises an exception if the callback doesn't return a LiberviaWidget instance
author | souliane <souliane@mailoo.org> |
---|---|
date | Sat, 07 Feb 2015 12:07:12 +0100 |
parents | ed6d8f7c6026 |
children | ea27925ef2a8 |
comparison
equal
deleted
inserted
replaced
602:be3fd3874c3a | 603:462d0458e679 |
---|---|
44 | 44 |
45 import dialog | 45 import dialog |
46 import base_menu | 46 import base_menu |
47 | 47 |
48 | 48 |
49 class NoLiberviaWidgetException(Exception): | |
50 pass | |
51 | |
52 | |
49 class DragLabel(DragWidget): | 53 class DragLabel(DragWidget): |
50 | 54 |
51 def __init__(self, text, type_, host=None): | 55 def __init__(self, text, type_, host=None): |
52 """Base of Drag n Drop mecanism in Libervia | 56 """Base of Drag n Drop mecanism in Libervia |
53 | 57 |
104 self.host = host | 108 self.host = host |
105 self.setStyleName('dropCell') | 109 self.setStyleName('dropCell') |
106 | 110 |
107 @classmethod | 111 @classmethod |
108 def addDropKey(cls, key, cb): | 112 def addDropKey(cls, key, cb): |
109 """Add a association between a key and a class to create on drop | 113 """Add a association between a key and a class to create on drop. |
110 | 114 |
111 @param key: key to be associated (e.g. "CONTACT", "CHAT") | 115 @param key: key to be associated (e.g. "CONTACT", "CHAT") |
112 @param cb: either a LiberviaWidget instance, or a callback which return one | 116 @param cb: a callable (either a class or method) returning a |
117 LiberviaWidget instance | |
113 """ | 118 """ |
114 DropCell.drop_keys[key] = cb | 119 DropCell.drop_keys[key] = cb |
115 | |
116 | 120 |
117 def onDragEnter(self, event): | 121 def onDragEnter(self, event): |
118 if self == LiberviaDragWidget.current: | 122 if self == LiberviaDragWidget.current: |
119 return | 123 return |
120 self.addStyleName('dragover') | 124 self.addStyleName('dragover') |
136 cell = grid.getEventTargetCell(event) | 140 cell = grid.getEventTargetCell(event) |
137 row = DOM.getParent(cell) | 141 row = DOM.getParent(cell) |
138 return (row.rowIndex, cell.cellIndex) | 142 return (row.rowIndex, cell.cellIndex) |
139 | 143 |
140 def onDrop(self, event): | 144 def onDrop(self, event): |
145 """ | |
146 @raise NoLiberviaWidgetException: something else than a LiberviaWidget | |
147 has been returned by the callback. | |
148 """ | |
141 self.removeStyleName('dragover') | 149 self.removeStyleName('dragover') |
142 DOM.eventPreventDefault(event) | 150 DOM.eventPreventDefault(event) |
143 dt = event.dataTransfer | 151 dt = event.dataTransfer |
144 # 'text', 'text/plain', and 'Text' are equivalent. | 152 # 'text', 'text/plain', and 'Text' are equivalent. |
145 try: | 153 try: |
169 # as the target widget (self), we don't do anything | 177 # as the target widget (self), we don't do anything |
170 return | 178 return |
171 widgets_panel.removeWidget(_new_panel) | 179 widgets_panel.removeWidget(_new_panel) |
172 elif item_type in self.drop_keys: | 180 elif item_type in self.drop_keys: |
173 _new_panel = self.drop_keys[item_type](self.host, item) | 181 _new_panel = self.drop_keys[item_type](self.host, item) |
182 if not isinstance(_new_panel, LiberviaWidget): | |
183 raise NoLiberviaWidgetException | |
174 else: | 184 else: |
175 log.warning("unmanaged item type") | 185 log.warning("unmanaged item type") |
176 return | 186 return |
177 if isinstance(self, LiberviaWidget): | 187 if isinstance(self, LiberviaWidget): |
178 # self.host.unregisterWidget(self) # FIXME | 188 # self.host.unregisterWidget(self) # FIXME |