comparison browser_side/panels.py @ 226:744426c2b699

browser_side, misc: better PEP8 compliance
author souliane <souliane@mailoo.org>
date Thu, 26 Sep 2013 14:32:24 +0200
parents 624a87377412
children 266e9678eec0
comparison
equal deleted inserted replaced
225:9b93a21dc5e2 226:744426c2b699
17 17
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 import pyjd # this is dummy in pyjs 22 import pyjd # this is dummy in pyjs
23 from pyjamas.ui.SimplePanel import SimplePanel 23 from pyjamas.ui.SimplePanel import SimplePanel
24 from pyjamas.ui.AbsolutePanel import AbsolutePanel 24 from pyjamas.ui.AbsolutePanel import AbsolutePanel
25 from pyjamas.ui.VerticalPanel import VerticalPanel 25 from pyjamas.ui.VerticalPanel import VerticalPanel
26 from pyjamas.ui.HorizontalPanel import HorizontalPanel 26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
27 from pyjamas.ui.HTMLPanel import HTMLPanel 27 from pyjamas.ui.HTMLPanel import HTMLPanel
115 self._popup.setStyleName("warningPopup") 115 self._popup.setStyleName("warningPopup")
116 if style: 116 if style:
117 self._popup.addStyleName(style) 117 self._popup.addStyleName(style)
118 118
119 left = 0 119 left = 0
120 top = 0 #max(0, self.getAbsoluteTop() - contents.getOffsetHeight() - 2) 120 top = 0 # max(0, self.getAbsoluteTop() - contents.getOffsetHeight() - 2)
121 self._popup.setPopupPosition(left, top) 121 self._popup.setPopupPosition(left, top)
122 self._popup.show() 122 self._popup.show()
123 123
124 def _timeCb(self, timer): 124 def _timeCb(self, timer):
125 if self._popup: 125 if self._popup:
136 target = self._selected_cache 136 target = self._selected_cache
137 137
138 def getSelectedOrStatus(): 138 def getSelectedOrStatus():
139 if target: 139 if target:
140 _type, msg = target.getWarningData() 140 _type, msg = target.getWarningData()
141 target_hook = None # we use the selected widget, not a hook 141 target_hook = None # we use the selected widget, not a hook
142 else: 142 else:
143 _type, msg = "STATUS", "This will be your new status message" 143 _type, msg = "STATUS", "This will be your new status message"
144 target_hook = (txt, None) 144 target_hook = (txt, None)
145 return (target_hook, _type, msg) 145 return (target_hook, _type, msg)
146 146
153 elif txt.startswith('@'): 153 elif txt.startswith('@'):
154 _end = txt.find(': ') 154 _end = txt.find(': ')
155 if _end == -1: 155 if _end == -1:
156 target_hook, _type, msg = getSelectedOrStatus() 156 target_hook, _type, msg = getSelectedOrStatus()
157 else: 157 else:
158 group = txt[1:_end] #only one target group is managed for the moment 158 group = txt[1:_end] # only one target group is managed for the moment
159 if not group or not group in self.host.contact_panel.getGroups(): 159 if not group or not group in self.host.contact_panel.getGroups():
160 # the group doesn't exists, we ignore the key 160 # the group doesn't exists, we ignore the key
161 group = None 161 group = None
162 target_hook, _type, msg = getSelectedOrStatus() 162 target_hook, _type, msg = getSelectedOrStatus()
163 else: 163 else:
164 _type = "GROUP" 164 _type = "GROUP"
165 msg = MicroblogPanel.warning_msg_group % group 165 msg = MicroblogPanel.warning_msg_group % group
166 target_hook = (txt[_end+2:], group) 166 target_hook = (txt[_end + 2:], group)
167 else: 167 else:
168 print "ERROR: Unknown target" 168 print "ERROR: Unknown target"
169 target_hook, _type, msg = getSelectedOrStatus() 169 target_hook, _type, msg = getSelectedOrStatus()
170 170
171 return (target_hook, _type, msg) 171 return (target_hook, _type, msg)
172 172
173 def onBrowserEvent(self, event): 173 def onBrowserEvent(self, event):
174 #XXX: woraroung a pyjamas bug: self.currentEvent is not set 174 # XXX: woraroung a pyjamas bug: self.currentEvent is not set
175 # so the TextBox's cancelKey doens't work. This is a workaround 175 # so the TextBox's cancelKey doens't work. This is a workaround
176 # FIXME: fix the bug upstream 176 # FIXME: fix the bug upstream
177 self.currentEvent = event 177 self.currentEvent = event
178 TextArea.onBrowserEvent(self, event) 178 TextArea.onBrowserEvent(self, event)
179 179
181 _txt = self.getText() 181 _txt = self.getText()
182 target = self._getTarget(_txt) 182 target = self._getTarget(_txt)
183 if not self._popup: 183 if not self._popup:
184 self.showWarning(target) 184 self.showWarning(target)
185 elif target != self._popup.target_data: 185 elif target != self._popup.target_data:
186 self._timeCb(None) #we remove the popup 186 self._timeCb(None) # we remove the popup
187 self.showWarning(target) 187 self.showWarning(target)
188 188
189 self._timer.schedule(2000) 189 self._timer.schedule(2000)
190 190
191 #if keycode == KEY_ENTER and not self.visible: 191 # if keycode == KEY_ENTER and not self.visible:
192 if keycode == KEY_ENTER: 192 if keycode == KEY_ENTER:
193 if _txt: 193 if _txt:
194 target_hook, _type, msg = target 194 target_hook, _type, msg = target
195 if target_hook: 195 if target_hook:
196 parsed_txt, data = target_hook 196 parsed_txt, data = target_hook
200 self.host.bridge.call("sendMblog", None, "GROUP", data, parsed_txt) 200 self.host.bridge.call("sendMblog", None, "GROUP", data, parsed_txt)
201 elif _type == "STATUS": 201 elif _type == "STATUS":
202 self.host.bridge.call('setStatus', None, parsed_txt) 202 self.host.bridge.call('setStatus', None, parsed_txt)
203 else: 203 else:
204 print "ERROR: Unknown target hook type" 204 print "ERROR: Unknown target hook type"
205 else: #we send the message to the selected target 205 else: # we send the message to the selected target
206 self._selected_cache.onTextEntered(_txt) 206 self._selected_cache.onTextEntered(_txt)
207 self.setText('') 207 self.setText('')
208 self._timeCb(None) #we remove the popup 208 self._timeCb(None) # we remove the popup
209 sender.cancelKey() 209 sender.cancelKey()
210 else: 210 else:
211 self.__onComposing() 211 self.__onComposing()
212 212
213 def __onComposing(self): 213 def __onComposing(self):
230 #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this 230 #XXX: fixed directly on pyjamas, if the patch is accepted, no need to walk around this
231 return AutoCompleteTextBox.complete(self)""" 231 return AutoCompleteTextBox.complete(self)"""
232 232
233 233
234 class MicroblogItem(): 234 class MicroblogItem():
235 #XXX: should be moved in a separated module 235 # XXX: should be moved in a separated module
236 236
237 def __init__(self, data): 237 def __init__(self, data):
238 self.id = data['id'] 238 self.id = data['id']
239 self.type = data.get('type', 'main_item') 239 self.type = data.get('type', 'main_item')
240 self.content = data['content'] 240 self.content = data['content']
241 self.author = data['author'] 241 self.author = data['author']
242 self.timestamp = float(data.get('timestamp', 0)) #XXX: int doesn't work here 242 self.timestamp = float(data.get('timestamp', 0)) # XXX: int doesn't work here
243 self.comments = data.get('comments', False) 243 self.comments = data.get('comments', False)
244 if self.comments: 244 if self.comments:
245 try: 245 try:
246 self.comments_hash = (data['comments_service'], data['comments_node']) 246 self.comments_hash = (data['comments_service'], data['comments_node'])
247 self.comments_service = data['comments_service'] 247 self.comments_service = data['comments_service']
248 self.comments_node = data['comments_node'] 248 self.comments_node = data['comments_node']
249 except KeyError: 249 except KeyError:
250 print "Warning: can't manage comment [%s], some keys are missing in microblog data (%s)" % (data["comments"], data.keys()) 250 print "Warning: can't manage comment [%s], some keys are missing in microblog data (%s)" % (data["comments"], data.keys())
251 self.comments = False 251 self.comments = False
252 if set(("service","node")).issubset(data.keys()): 252 if set(("service", "node")).issubset(data.keys()):
253 self.service = data["service"] 253 self.service = data["service"]
254 self.node = data["node"] 254 self.node = data["node"]
255 self.hash = (self.service, self.node) 255 self.hash = (self.service, self.node)
256 256
257 257
429 _entry = MicroblogEntry(self, mblog_item) 429 _entry = MicroblogEntry(self, mblog_item)
430 parent = self.comments[mblog_item.hash] 430 parent = self.comments[mblog_item.hash]
431 parent_idx = self.vpanel.getWidgetIndex(parent) 431 parent_idx = self.vpanel.getWidgetIndex(parent)
432 # we find or create the panel where the comment must be inserted 432 # we find or create the panel where the comment must be inserted
433 try: 433 try:
434 sub_panel = self.vpanel.getWidget(parent_idx+1) 434 sub_panel = self.vpanel.getWidget(parent_idx + 1)
435 except IndexError: 435 except IndexError:
436 sub_panel = None 436 sub_panel = None
437 if not sub_panel or not isinstance(sub_panel, VerticalPanel): 437 if not sub_panel or not isinstance(sub_panel, VerticalPanel):
438 sub_panel = VerticalPanel() 438 sub_panel = VerticalPanel()
439 sub_panel.setStyleName('microblogPanel') 439 sub_panel.setStyleName('microblogPanel')
440 sub_panel.addStyleName('subPanel') 440 sub_panel.addStyleName('subPanel')
441 self.vpanel.insert(sub_panel, parent_idx+1) 441 self.vpanel.insert(sub_panel, parent_idx + 1)
442 442
443 # we want comments to be inserted in chronological order 443 # we want comments to be inserted in chronological order
444 self._chronoInsert(sub_panel, _entry, reverse=False) 444 self._chronoInsert(sub_panel, _entry, reverse=False)
445 return 445 return
446 446
516 def changeStatus(self, new_status): 516 def changeStatus(self, new_status):
517 self.status = new_status or '&nbsp;' 517 self.status = new_status or '&nbsp;'
518 self.setHTML(self.__getContent()) 518 self.setHTML(self.__getContent())
519 519
520 def onClick(self, sender): 520 def onClick(self, sender):
521 #As status is the default target of uniBar, we don't want to select anything if click on it 521 # As status is the default target of uniBar, we don't want to select anything if click on it
522 self.host.setSelected(None) 522 self.host.setSelected(None)
523 523
524 524
525 class ChatText(HTMLPanel): 525 class ChatText(HTMLPanel):
526 526
623 623
624 @classmethod 624 @classmethod
625 def createChat(cls, host, item): 625 def createChat(cls, host, item):
626 _contact = JID(item) 626 _contact = JID(item)
627 host.contact_panel.setContactMessageWaiting(_contact.bare, False) 627 host.contact_panel.setContactMessageWaiting(_contact.bare, False)
628 _new_panel = ChatPanel(host, _contact) #XXX: pyjamas doesn't seems to support creating with cls directly 628 _new_panel = ChatPanel(host, _contact) # XXX: pyjamas doesn't seems to support creating with cls directly
629 _new_panel.historyPrint() 629 _new_panel.historyPrint()
630 return _new_panel 630 return _new_panel
631 631
632 def getWarningData(self): 632 def getWarningData(self):
633 if self.type not in ["one2one", "group"]: 633 if self.type not in ["one2one", "group"]:
700 700
701 def printMessage(self, from_jid, msg, timestamp=None): 701 def printMessage(self, from_jid, msg, timestamp=None):
702 """Print message in chat window. Must be implemented by child class""" 702 """Print message in chat window. Must be implemented by child class"""
703 _jid = JID(from_jid) 703 _jid = JID(from_jid)
704 nick = _jid.node if self.type == 'one2one' else _jid.resource 704 nick = _jid.node if self.type == 'one2one' else _jid.resource
705 mymess = _jid.resource == self.nick if self.type == "group" else _jid.bare == self.host.whoami.bare #mymess = True if message comes from local user 705 mymess = _jid.resource == self.nick if self.type == "group" else _jid.bare == self.host.whoami.bare # mymess = True if message comes from local user
706 if msg.startswith('/me '): 706 if msg.startswith('/me '):
707 self.printInfo('* %s %s' % (nick, msg[4:]), type='me') 707 self.printInfo('* %s %s' % (nick, msg[4:]), type='me')
708 return 708 return
709 self.content.add(ChatText(timestamp, nick, mymess, msg)) 709 self.content.add(ChatText(timestamp, nick, mymess, msg))
710 self.content_scroll.scrollToBottom() 710 self.content_scroll.scrollToBottom()
716 return 716 return
717 self.tarot_panel = CardPanel(self, referee, players, self.nick) 717 self.tarot_panel = CardPanel(self, referee, players, self.nick)
718 self.vpanel.insert(self.tarot_panel, 0) 718 self.vpanel.insert(self.tarot_panel, 0)
719 self.vpanel.setCellHeight(self.tarot_panel, self.tarot_panel.getHeight()) 719 self.vpanel.setCellHeight(self.tarot_panel, self.tarot_panel.getHeight())
720 elif game_type == "RadioCol": 720 elif game_type == "RadioCol":
721 #XXX: We can have double panel if we join quickly enough to have the group chat start signal 721 # XXX: We can have double panel if we join quickly enough to have the group chat start signal
722 # on invitation + the one triggered on room join 722 # on invitation + the one triggered on room join
723 if hasattr(self, "radiocol_panel"): 723 if hasattr(self, "radiocol_panel"):
724 return 724 return
725 self.radiocol_panel = RadioColPanel(self, referee, self.nick) 725 self.radiocol_panel = RadioColPanel(self, referee, self.nick)
726 self.vpanel.insert(self.radiocol_panel, 0) 726 self.vpanel.insert(self.radiocol_panel, 0)
727 self.vpanel.setCellHeight(self.radiocol_panel, self.radiocol_panel.getHeight()) 727 self.vpanel.setCellHeight(self.radiocol_panel, self.radiocol_panel.getHeight())
728 728
729 def getGame(self, game_type): 729 def getGame(self, game_type):
730 """Return class managing the game type""" 730 """Return class managing the game type"""
731 #TODO: check that the game is launched, and manage errors 731 # TODO: check that the game is launched, and manage errors
732 if game_type == "Tarot": 732 if game_type == "Tarot":
733 return self.tarot_panel 733 return self.tarot_panel
734 elif game_type == "RadioCol": 734 elif game_type == "RadioCol":
735 return self.radiocol_panel 735 return self.radiocol_panel
736 736
771 771
772 def __init__(self, host): 772 def __init__(self, host):
773 self.host = host 773 self.host = host
774 AbsolutePanel.__init__(self) 774 AbsolutePanel.__init__(self)
775 775
776 #menu 776 # menu
777 self.menu = Menu(host) 777 self.menu = Menu(host)
778 778
779 #unibox 779 # unibox
780 unibox_panel = UniBoxPanel(host) 780 unibox_panel = UniBoxPanel(host)
781 self.host.setUniBox(unibox_panel.unibox) 781 self.host.setUniBox(unibox_panel.unibox)
782 782
783 #status bar 783 # status bar
784 status = host.status_panel 784 status = host.status_panel
785 785
786 #contacts 786 # contacts
787 _contacts = HorizontalPanel() 787 _contacts = HorizontalPanel()
788 _contacts.addStyleName('globalLeftArea') 788 _contacts.addStyleName('globalLeftArea')
789 contacts_switch = Button(u'«', self._contactsSwitch) 789 contacts_switch = Button(u'«', self._contactsSwitch)
790 contacts_switch.addStyleName('contactsSwitch') 790 contacts_switch.addStyleName('contactsSwitch')
791 _contacts.add(contacts_switch) 791 _contacts.add(contacts_switch)
792 _contacts.add(self.host.contact_panel) 792 _contacts.add(self.host.contact_panel)
793 793
794 #tabs 794 # tabs
795 self.tab_panel = base_widget.MainTabPanel(host) 795 self.tab_panel = base_widget.MainTabPanel(host)
796 self.discuss_panel = base_widget.WidgetsPanel(self.host, locked=True) 796 self.discuss_panel = base_widget.WidgetsPanel(self.host, locked=True)
797 self.tab_panel.add(self.discuss_panel, "Discussions") 797 self.tab_panel.add(self.discuss_panel, "Discussions")
798 self.tab_panel.selectTab(0) 798 self.tab_panel.selectTab(0)
799 799