comparison src/browser/sat_browser/libervia_widget.py @ 659:8e7d4de56e75 frontends_multi_profiles

browser_side: allow to drop a widget in the "+" tab
author souliane <souliane@mailoo.org>
date Fri, 27 Feb 2015 16:05:28 +0100
parents 476d8d9973d3
children ebb602d8b3f2
comparison
equal deleted inserted replaced
658:476d8d9973d3 659:8e7d4de56e75
102 DragLabel.onDragStart(self, event) 102 DragLabel.onDragStart(self, event)
103 103
104 def onDragEnd(self, event): 104 def onDragEnd(self, event):
105 DragLabel.onDragEnd(self, event) 105 DragLabel.onDragEnd(self, event)
106 LiberviaDragWidget.current = None 106 LiberviaDragWidget.current = None
107
107 108
108 class DropCell(DropWidget): 109 class DropCell(DropWidget):
109 """Cell in the middle grid which replace itself with the dropped widget on DnD""" 110 """Cell in the middle grid which replace itself with the dropped widget on DnD"""
110 drop_keys = {} 111 drop_keys = {}
111 112
251 Label.__init__(self, text) 252 Label.__init__(self, text)
252 DropWidget.__init__(self, tab_panel) 253 DropWidget.__init__(self, tab_panel)
253 self.tab_panel = tab_panel 254 self.tab_panel = tab_panel
254 self.setStyleName('dropCell') 255 self.setStyleName('dropCell')
255 self.setWordWrap(False) 256 self.setWordWrap(False)
256 DOM.setStyleAttribute(self.getElement(), "min-width", "30px")
257 257
258 def _getIndex(self): 258 def _getIndex(self):
259 """ get current index of the DropTab """ 259 """ get current index of the DropTab """
260 # XXX: awful hack, but seems the only way to get index 260 # XXX: awful hack, but seems the only way to get index
261 return self.tab_panel.tabBar.panel.getWidgetIndex(self.getParent().getParent()) - 1 261 return self.tab_panel.tabBar.panel.getWidgetIndex(self.getParent().getParent()) - 1
274 274
275 def onDrop(self, event): 275 def onDrop(self, event):
276 DOM.eventPreventDefault(event) 276 DOM.eventPreventDefault(event)
277 self.parent.removeStyleName('dragover') 277 self.parent.removeStyleName('dragover')
278 if self._getIndex() == self.tab_panel.tabBar.getSelectedTab(): 278 if self._getIndex() == self.tab_panel.tabBar.getSelectedTab():
279 # the widget come from the DragTab, so nothing to do, we let it there 279 # the widget comes from the same tab, so nothing to do, we let it there
280 return 280 return
281 281
282 # FIXME: quite the same stuff as in DropCell, need some factorisation 282 # FIXME: quite the same stuff as in DropCell, need some factorisation
283 dt = event.dataTransfer 283 dt = event.dataTransfer
284 # 'text', 'text/plain', and 'Text' are equivalent. 284 # 'text', 'text/plain', and 'Text' are equivalent.
291 log.debug("type: %s" % item_type) 291 log.debug("type: %s" % item_type)
292 except: 292 except:
293 log.debug("no message found") 293 log.debug("no message found")
294 item = '&nbsp;' 294 item = '&nbsp;'
295 item_type = None 295 item_type = None
296
296 if item_type == "WIDGET": 297 if item_type == "WIDGET":
297 if not LiberviaDragWidget.current: 298 if not LiberviaDragWidget.current:
298 log.error("No widget registered in LiberviaDragWidget !") 299 log.error("No widget registered in LiberviaDragWidget !")
299 return 300 return
300 _new_panel = LiberviaDragWidget.current 301 _new_panel = LiberviaDragWidget.current
301 _new_panel.getParent(WidgetsPanel, expect=True).removeWidget(_new_panel)
302 elif item_type in DropCell.drop_keys: 302 elif item_type in DropCell.drop_keys:
303 _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host, item) 303 pass # create the widget when we are sure there's a tab for it
304 else: 304 else:
305 log.warning("unmanaged item type") 305 log.warning("unmanaged item type")
306 return 306 return
307 307
308 widgets_panel = self.tab_panel.getWidget(self._getIndex()) 308 # XXX: when needed, new tab creation must be done exactly here to not mess up with LiberviaDragWidget.onDragEnd
309 try:
310 widgets_panel = self.tab_panel.getWidget(self._getIndex())
311 except IndexError: # widgets panel doesn't exist, e.g. user dropped in "+" tab
312 widgets_panel = self.tab_panel.addWidgetsTab(None)
313 if widgets_panel is None: # user cancelled
314 return
315
316 if item_type == "WIDGET":
317 _new_panel.getParent(WidgetsPanel, expect=True).removeWidget(_new_panel)
318 else:
319 _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host, item)
320
309 widgets_panel.addWidget(_new_panel) 321 widgets_panel.addWidget(_new_panel)
310 322
311 323
312 ### Libervia Widget ### 324 ### Libervia Widget ###
313 325
747 def __init__(self, host): 759 def __init__(self, host):
748 TabPanel.__init__(self, FloatingTab=True) 760 TabPanel.__init__(self, FloatingTab=True)
749 ClickHandler.__init__(self) 761 ClickHandler.__init__(self)
750 self.host = host 762 self.host = host
751 self.setStyleName('liberviaTabPanel') 763 self.setStyleName('liberviaTabPanel')
752 self.tabBar.addTab(u'✚', asHTML=True) 764 self.tabBar.addTab(DropTab(self, u'✚'), asHTML=False)
753 self.tabBar.setVisible(False) # set to True when profile is logged 765 self.tabBar.setVisible(False) # set to True when profile is logged
754 self.tabBar.addStyleDependentName('oneTab') 766 self.tabBar.addStyleDependentName('oneTab')
755 767
756 def onTabSelected(self, sender, tabIndex): 768 def onTabSelected(self, sender, tabIndex):
757 if tabIndex < self.getWidgetCount(): 769 if tabIndex < self.getWidgetCount():
758 TabPanel.onTabSelected(self, sender, tabIndex) 770 TabPanel.onTabSelected(self, sender, tabIndex)
759 return 771 return
760 # user clicked the "+" tab 772 # user clicked the "+" tab
761 default_label = _(u'new tab') 773 self.addWidgetsTab(None, select=True)
762 try:
763 label = Window.prompt(_(u'Name of the new tab'), default_label)
764 if not label:
765 return # empty label or user pressed "cancel"
766 except: # this happens when the user prevents the page to open the prompt dialog
767 label = default_label
768 self.addWidgetsTab(label, select=True)
769 774
770 def getCurrentPanel(self): 775 def getCurrentPanel(self):
771 """ Get the panel of the currently selected tab 776 """ Get the panel of the currently selected tab
772 777
773 @return: WidgetsPanel 778 @return: WidgetsPanel
789 self.selectTab(self.getWidgetCount() - 1) 794 self.selectTab(self.getWidgetCount() - 1)
790 795
791 def addWidgetsTab(self, label, select=False, locked=False): 796 def addWidgetsTab(self, label, select=False, locked=False):
792 """Create a new tab for containing LiberviaWidgets. 797 """Create a new tab for containing LiberviaWidgets.
793 798
794 @param label (unicode): label of the tab 799 @param label (unicode): label of the tab (None or '' for user prompt)
795 @param select (bool): True to select the added tab 800 @param select (bool): True to select the added tab
796 @param locked (bool): If True, the tab will not be removed when there 801 @param locked (bool): If True, the tab will not be removed when there
797 are no more widget inside. If False, the tab will be removed with 802 are no more widget inside. If False, the tab will be removed with
798 the last widget. 803 the last widget.
799 @return: WidgetsPanel 804 @return: WidgetsPanel
800 """ 805 """
801 widgets_panel = WidgetsPanel(self.host, locked=locked) 806 widgets_panel = WidgetsPanel(self.host, locked=locked)
807
808 if not label:
809 default_label = _(u'new tab')
810 try:
811 label = Window.prompt(_(u'Name of the new tab'), default_label)
812 if not label: # empty label or user pressed "cancel"
813 return None
814 except: # this happens when the user prevents the page to open the prompt dialog
815 label = default_label
816
802 self.addTab(widgets_panel, label, select) 817 self.addTab(widgets_panel, label, select)
803 return widgets_panel 818 return widgets_panel
804 819
805 def onWidgetPanelRemove(self, panel): 820 def onWidgetPanelRemove(self, panel):
806 """ Called when a child WidgetsPanel is empty and need to be removed """ 821 """ Called when a child WidgetsPanel is empty and need to be removed """