changeset 198:ab239b3b67b3

browser side: tabs are now dropable (i.e. we can now move widgets between tabs)
author Goffi <goffi@goffi.org>
date Thu, 07 Mar 2013 01:16:13 +0100
parents 0fc32122a877
children 39311c7dad77
files browser_side/base_widget.py
diffstat 1 files changed, 68 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/browser_side/base_widget.py	Wed Mar 06 22:36:21 2013 +0100
+++ b/browser_side/base_widget.py	Thu Mar 07 01:16:13 2013 +0100
@@ -78,6 +78,7 @@
 
 
     def onDrop(self, event):
+        DOM.eventPreventDefault(event)
         dt = event.dataTransfer
         #'text', 'text/plain', and 'Text' are equivalent.
         try:
@@ -91,7 +92,6 @@
             print "no message found"
             item='&nbsp;'
             item_type = None
-        DOM.eventPreventDefault(event)
         if item_type == "WIDGET":
             if not LiberviaDragWidget.current:
                 print "ERROR: No widget registered in LiberviaDragWidget !"
@@ -521,6 +521,70 @@
         cellFormatter = self.flextable.getFlexCellFormatter()
         return cellFormatter.setRowSpan(row, col, value)
 
+class DropTab(Label, DropWidget):
+
+    def __init__(self, tab_panel, text):
+        Label.__init__(self, text)
+        DropWidget.__init__(self, tab_panel)
+        self.tab_panel = tab_panel
+        self.setStyleName('dropCell')
+        self.setWordWrap(False)
+        DOM.setStyleAttribute(self.getElement(), "min-width", "30px")
+
+    def _getIndex(self):
+        """ get current index of the DropTab """
+        # XXX: awful hack, but seems the only way to get index 
+        return self.tab_panel.tabBar.panel.getWidgetIndex(self.getParent().getParent()) - 1
+        
+
+    def onDragEnter(self, event):
+        #if self == LiberviaDragWidget.current:
+        #    return
+        self.addStyleName('dragover')
+        DOM.eventPreventDefault(event)
+
+    def onDragLeave(self, event):
+        self.removeStyleName('dragover')
+
+    def onDragOver(self, event):
+        DOM.eventPreventDefault(event)
+
+    def onDrop(self, event):
+        DOM.eventPreventDefault(event)
+        self.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
+        
+        # FIXME: quite the same stuff as in DropCell, need some factorisation
+        dt = event.dataTransfer
+        #'text', 'text/plain', and 'Text' are equivalent.
+        try:
+            item, item_type = dt.getData("text/plain").split('\n') #Workaround for webkit, only text/plain seems to be managed
+            if item_type and item_type[-1] == '\0': #Workaround for what looks like a pyjamas bug: the \0 should not be there, and 
+                item_type = item_type[:-1]          # .strip('\0') and .replace('\0','') don't work. TODO: check this and fill a bug report
+            #item_type = dt.getData("type")
+            print "message: %s" % item
+            print "type: %s" % item_type
+        except:
+            print "no message found"
+            item='&nbsp;'
+            item_type = None
+        if item_type == "WIDGET":
+            if not LiberviaDragWidget.current:
+                print "ERROR: No widget registered in LiberviaDragWidget !"
+                return
+            _new_panel = LiberviaDragWidget.current
+            _new_panel.getWidgetsPanel().removeWidget(_new_panel)
+        elif item_type in DropCell.drop_keys:
+            _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host,item)
+        else:
+            print "WARNING: unmanaged item type"
+            return
+
+        widgets_panel = self.tab_panel.getWidget(self._getIndex())
+        widgets_panel.addWidget(_new_panel)
+
 class MainTabPanel(TabPanel):
 
     def __init__(self, host):
@@ -548,8 +612,9 @@
         self.setWidth("%s%s" % (ideal_width, "px"));
         self.setHeight("%s%s" % (ideal_height, "px"));
 
-    def add(self, widget, tabText=None, asHTML=False):
-        TabPanel.add(self, widget, tabText, asHTML)
+    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()