diff src/browser/sat_browser/base_widget.py @ 637:7113d40533d6 frontends_multi_profiles

merged souliane changes
author Goffi <goffi@goffi.org>
date Mon, 23 Feb 2015 18:47:27 +0100
parents 86ae737da6f3 b77e1070035c
children e0021d571eef
line wrap: on
line diff
--- a/src/browser/sat_browser/base_widget.py	Mon Feb 23 18:44:58 2015 +0100
+++ b/src/browser/sat_browser/base_widget.py	Mon Feb 23 18:47:27 2015 +0100
@@ -21,7 +21,9 @@
 from sat.core.log import getLogger
 log = getLogger(__name__)
 from sat.core import exceptions
+from sat.core.i18n import _
 from sat_frontends.quick_frontend import quick_widgets
+
 from pyjamas.ui.SimplePanel import SimplePanel
 from pyjamas.ui.AbsolutePanel import AbsolutePanel
 from pyjamas.ui.VerticalPanel import VerticalPanel
@@ -586,10 +588,17 @@
 class WidgetsPanel(ScrollPanelWrapper):
 
     def __init__(self, host, locked=False):
+        """
+
+        @param host (SatWebFrontend): host instance
+        @param locked (bool): If True, the tab containing self will not be
+            removed when there are no more widget inside self. If False, the
+            tab will be removed with self's last widget.
+        """
         ScrollPanelWrapper.__init__(self)
         self.setSize('100%', '100%')
         self.host = host
-        self.locked = locked  # if True: tab will not be removed when there are no more widgets inside
+        self.locked = locked
         self.selected = None
         self.flextable = FlexTable()
         self.flextable.setSize('100%', '100%')
@@ -760,18 +769,18 @@
     def onDragEnter(self, event):
         #if self == LiberviaDragWidget.current:
         #    return
-        self.addStyleName('dragover')
+        self.parent.addStyleName('dragover')
         DOM.eventPreventDefault(event)
 
     def onDragLeave(self, event):
-        self.removeStyleName('dragover')
+        self.parent.removeStyleName('dragover')
 
     def onDragOver(self, event):
         DOM.eventPreventDefault(event)
 
     def onDrop(self, event):
         DOM.eventPreventDefault(event)
-        self.removeStyleName('dragover')
+        self.parent.removeStyleName('dragover')
         if self._getIndex() == self.tab_panel.tabBar.getSelectedTab():
             # the widget come from the DragTab, so nothing to do, we let it there
             return
@@ -806,16 +815,32 @@
         widgets_panel.addWidget(_new_panel)
 
 
-class MainTabPanel(TabPanel):
+class MainTabPanel(TabPanel, ClickHandler):
 
     def __init__(self, host):
         TabPanel.__init__(self)
+        ClickHandler.__init__(self)
         self.host = host
-        self.tabBar.setVisible(False)
         self.setStyleName('liberviaTabPanel')
         self.addStyleName('mainTabPanel')
         Window.addWindowResizeListener(self)
 
+        self.tabBar.addTab(u'✚', True)
+
+    def onTabSelected(self, sender, tabIndex):
+        if tabIndex < self.getWidgetCount():
+            TabPanel.onTabSelected(self, sender, tabIndex)
+            return
+        # user clicked the "+" tab
+        default_label = _(u'new tab')
+        try:
+            label = Window.prompt(_(u'Name of the new tab'), default_label)
+            if not label:
+                label = default_label
+        except:  # this happens when the user prevents the page to open the prompt dialog
+            label = default_label
+        self.addWidgetsTab(label, select=True)
+
     def getCurrentPanel(self):
         """ Get the panel of the currently selected tab
 
@@ -836,20 +861,34 @@
         self.setWidth("%s%s" % (ideal_width, "px"))
         self.setHeight("%s%s" % (ideal_height, "px"))
 
-    def add(self, widget, text=''):
-        tab = DropTab(self, text)
-        TabPanel.add(self, widget, tab, False)
-        if self.getWidgetCount() > 1:
-            self.tabBar.setVisible(True)
-            self.host.resize()
+    def addTab(self, widget, label, select=False):
+        """Create a new tab for the given widget.
+
+        @param widget (Widget): widget to associate to the tab
+        @param label (unicode): label of the tab
+        @param select (bool): True to select the added tab
+        """
+        TabPanel.add(self, widget, DropTab(self, label), False)
+        if select:
+            self.selectTab(self.getWidgetCount() - 1)
+
+    def addWidgetsTab(self, label, select=False, locked=False):
+        """Create a new tab for containing LiberviaWidgets.
+
+        @param label (unicode): label of the tab
+        @param select (bool): True to select the added tab
+        @param locked (bool): If True, the tab will not be removed when there
+            are no more widget inside. If False, the tab will be removed with
+            the last widget.
+        @return: WidgetsPanel
+        """
+        widgets_panel = WidgetsPanel(self, locked=locked)
+        self.addTab(widgets_panel, label, select)
+        return widgets_panel
 
     def onWidgetPanelRemove(self, panel):
         """ Called when a child WidgetsPanel is empty and need to be removed """
+        widget_index = self.getWidgetIndex(panel)
         self.remove(panel)
         widgets_count = self.getWidgetCount()
-        if widgets_count == 1:
-            self.tabBar.setVisible(False)
-            self.host.resize()
-            self.selectTab(0)
-        else:
-            self.selectTab(widgets_count - 1)
+        self.selectTab(widget_index if widget_index < widgets_count else widgets_count - 1)