diff libervia.py @ 267:a76243c02074

browser_side: changes regarding widgets and tabs: - getOrCreateLiberviaWidget gets a tab_label argument to add widgets in a specific tab - ChatPanel can be matched not only regarding the target jid but also the dialog type ("one2one", "group"...)
author souliane <souliane@mailoo.org>
date Sun, 17 Nov 2013 17:53:37 +0100
parents cc778206b7ae
children 9eb9c7d41bdc
line wrap: on
line diff
--- a/libervia.py	Sun Nov 17 17:47:39 2013 +0100
+++ b/libervia.py	Sun Nov 17 17:53:37 2013 +0100
@@ -169,7 +169,7 @@
         self.tab_panel = self.panel.tab_panel
         self.tab_panel.addTabListener(self)
         self.libervia_widgets = set() #keep track of all actives LiberviaWidgets
-        self.room_list = set() #set of rooms 
+        self.room_list = set() #set of rooms
         self.mblog_cache = [] #used to keep our own blog entries in memory, to show them in new mblog panel
         self.avatars_cache = {} #keep track of jid's avatar hash (key=jid, value=file)
         self.current_action_ids = set()
@@ -267,19 +267,27 @@
         self.uni_box = unibox
         self.uni_box.addKey("@@: ")
 
-    def addTab(self, wid, label):
-        """Create a new tab and add a widget in
+    def addTab(self, label, wid, select=True):
+        """Create a new tab and eventually add a widget in
+        @param label: label of the tab
         @param wid: LiberviaWidget to add
-        @param label: label of the tab"""
-        _widgets_panel = WidgetsPanel(self)
-        _widgets_panel.addWidget(wid)
-        self.tab_panel.add(_widgets_panel, label)
-        self.tab_panel.selectTab(self.tab_panel.getWidgetCount() - 1)
+        @param select: True to select the added tab
+        """
+        widgets_panel = WidgetsPanel(self)
+        self.tab_panel.add(widgets_panel, label)
+        widgets_panel.addWidget(wid)
+        if select:
+            self.tab_panel.selectTab(self.tab_panel.getWidgetCount() - 1)
+        return widgets_panel
 
-    def addWidget(self, wid):
-        """ Add a widget at the bottom of the current tab
-        @param wid: LiberviaWidget to add """
-        panel = self.tab_panel.getCurrentPanel()
+    def addWidget(self, wid, tab_index=None):
+        """ Add a widget at the bottom of the current or specified tab
+        @param wid: LiberviaWidget to add
+        @param tab_index: index of the tab to add the widget to"""
+        if tab_index is None or tab_index < 0 or tab_index >= self.tab_panel.getWidgetCount():
+            panel = self.tab_panel.getCurrentPanel()
+        else:
+            panel = self.tab_panel.tabBar.getTabWidget(tab_index)
         panel.addWidget(wid)
 
     def _isRegisteredCB(self, registered):
@@ -513,26 +521,36 @@
                     return None
         return None
 
-    def getOrCreateLiberviaWidget(self, class_, entity, select=True):
+    def getOrCreateLiberviaWidget(self, class_, entity, select=True, new_tab=None):
         """Get the matching LiberviaWidget if it exists, or create a new one.
         @param class_: class of the panel (ChatPanel, MicroblogPanel...)
         @param entity: polymorphic parameter, see class_.matchEntity.
         @param select: if True, select the widget that has been found or created
+        @param new_tab: if not None, a widget which is created is created in
+        a new tab. In that case new_tab is a unicode to label that new tab.
+        If new_tab is not None and a widget is found, no tab is created.
         @return: the newly created wigdet if REUSE_EXISTING_LIBERVIA_WIDGETS
          is set to False or if the widget has not been found, the existing
          widget that has been found otherwise."""
         lib_wid = None
+        tab = None
         if REUSE_EXISTING_LIBERVIA_WIDGETS:
-            lib_wid = self.getLiberviaWidget(class_, entity)
-        if lib_wid is None:
-            lib_wid = class_.createPanel(self, entity)
-        else:
-            # remove the widget from its previous panel
-            panel = lib_wid.getWidgetsPanel(verbose=False)
-            if panel is not None:
-                panel.removeWidget(lib_wid)
-        self.addWidget(lib_wid)
+            lib_wid = self.getLiberviaWidget(class_, entity, new_tab is None)
+        if lib_wid is None:  # create a new widget
+            lib_wid = class_.createPanel(self, entity[0] if isinstance(entity, tuple) else entity)
+            if new_tab is None:
+                self.addWidget(lib_wid)
+            else:
+                tab = self.addTab(new_tab, lib_wid, False)
+        else:  # reuse existing widget
+            tab = lib_wid.getWidgetsPanel(verbose=False)
+            if new_tab is None:
+                if tab is not None:
+                    tab.removeWidget(lib_wid)
+                self.addWidget(lib_wid)
         if select:
+            if new_tab is not None:
+                self.tab_panel.selectTab(tab)
             # must be done after the widget is added,
             # for example to scroll to the bottom
             self.setSelected(lib_wid)
@@ -564,11 +582,11 @@
         chat_panel = panels.ChatPanel(self, _target, type_='group')
         chat_panel.setUserNick(user_nick)
         if _target.node.startswith('sat_tarot_'): #XXX: it's not really beautiful, but it works :)
-            self.addTab(chat_panel, "Tarot")
+            self.addTab("Tarot", chat_panel)
         elif _target.node.startswith('sat_radiocol_'):
-            self.addTab(chat_panel, "Radio collective")
+            self.addTab("Radio collective", chat_panel)
         else:
-            self.addTab(chat_panel, _target.node)
+            self.addTab(_target.node, chat_panel)
         chat_panel.setPresents(room_nicks)
         chat_panel.historyPrint()