Mercurial > libervia-web
changeset 225:9b93a21dc5e2
browser_side: display widget title in the debug info (LiberviaWidget method "getDebugName")
- better PEP8 compliance
author | souliane <souliane@mailoo.org> |
---|---|
date | Thu, 26 Sep 2013 11:39:54 +0200 |
parents | f3a5a094c8d6 |
children | 744426c2b699 |
files | browser_side/base_widget.py |
diffstat | 1 files changed, 90 insertions(+), 75 deletions(-) [+] |
line wrap: on
line diff
--- a/browser_side/base_widget.py Thu Sep 26 11:33:55 2013 +0200 +++ b/browser_side/base_widget.py Thu Sep 26 11:39:54 2013 +0200 @@ -40,10 +40,11 @@ from pyjamas import Window from __pyjamas__ import doc + class DropCell(DropWidget): """Cell in the middle grid which replace itself with the dropped widget on DnD""" drop_keys = {} - + def __init__(self, host): DropWidget.__init__(self) self.host = host @@ -52,7 +53,7 @@ @classmethod def addDropKey(cls, key, callback): DropCell.drop_keys[key] = callback - + def onDragEnter(self, event): if self == LiberviaDragWidget.current: return @@ -61,10 +62,10 @@ def onDragLeave(self, event): if event.clientX <= self.getAbsoluteLeft() or event.clientY <= self.getAbsoluteTop() or\ - event.clientX >= self.getAbsoluteLeft() + self.getOffsetWidth()-1 or event.clientY >= self.getAbsoluteTop() + self.getOffsetHeight()-1: - #We check that we are inside widget's box, and we don't remove the style in this case because - #if the mouse is over a widget inside the DropWidget, if will leave the DropWidget, and we - #don't want that + event.clientX >= self.getAbsoluteLeft() + self.getOffsetWidth() - 1 or event.clientY >= self.getAbsoluteTop() + self.getOffsetHeight() - 1: + # We check that we are inside widget's box, and we don't remove the style in this case because + # if the mouse is over a widget inside the DropWidget, if will leave the DropWidget, and we + # don't want that self.removeStyleName('dragover') def onDragOver(self, event): @@ -80,24 +81,24 @@ def onDrop(self, event): DOM.eventPreventDefault(event) dt = event.dataTransfer - #'text', 'text/plain', and 'Text' are equivalent. + # '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") + 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=' ' + item = ' ' item_type = None if item_type == "WIDGET": if not LiberviaDragWidget.current: print "ERROR: No widget registered in LiberviaDragWidget !" return _new_panel = LiberviaDragWidget.current - if self == _new_panel: # We can't drop on ourself + if self == _new_panel: # We can't drop on ourself return # we need to remove the widget from the panel as it will be inserted elsewhere widgets_panel = _new_panel.getWidgetsPanel() @@ -110,7 +111,7 @@ return widgets_panel.removeWidget(_new_panel) elif item_type in self.drop_keys: - _new_panel = self.drop_keys[item_type](self.host,item) + _new_panel = self.drop_keys[item_type](self.host, item) else: print "WARNING: unmanaged item type" return @@ -181,14 +182,18 @@ self.addClickListener(self) self.host.registerWidget(self) - def getWidgetsPanel(self): - current = self - while current is not None and current.__class__ != WidgetsPanel: + def getDebugName(self): + return "%s (%s)" % (self, self.__title.getText()) + + def getWidgetsPanel(self, verbose=True): + # current = self was not reassuring for the code analyzer + current = self.getParent() + while current is not None and not isinstance(current, WidgetsPanel): current = current.getParent() - if current is None: + if current is None and verbose: print "Error: can't find WidgetsPanel" return current - + def onClick(self, sender): self.host.setSelected(self) @@ -206,14 +211,16 @@ widpanel = self.getWidgetsPanel() row, col = widpanel.getIndex(self) body = VerticalPanel() - - #colspan & rowspan + + # colspan & rowspan colspan = widpanel.getColSpan(row, col) rowspan = widpanel.getRowSpan(row, col) + def onColSpanChange(value): - widpanel.setColSpan(row, col, value) + widpanel.setColSpan(row, col, value) + def onRowSpanChange(value): - widpanel.setRowSpan(row, col, value) + widpanel.setRowSpan(row, col, value) colspan_setter = dialog.IntSetter("Columns span", colspan) colspan_setter.addValueChangeListener(onColSpanChange) colspan_setter.setWidth('100%') @@ -222,23 +229,25 @@ rowspan_setter.setWidth('100%') body.add(colspan_setter) body.add(rowspan_setter) - - #size + + # size width_str = self.getWidth() if width_str.endswith('px'): - width=int(width_str[:-2]) + width = int(width_str[:-2]) else: width = 0 height_str = self.getHeight() if height_str.endswith('px'): - height=int(height_str[:-2]) + height = int(height_str[:-2]) else: height = 0 + def onWidthChange(value): if not value: self.setWidth('100%') else: self.setWidth('%dpx' % value) + def onHeightChange(value): if not value: self.setHeight('100%') @@ -253,7 +262,7 @@ body.add(width_setter) body.add(height_setter) - #reset + # reset def onReset(sender): colspan_setter.setValue(1) rowspan_setter.setValue(1) @@ -316,15 +325,16 @@ self.setCellHeight(body_wid, '100%') def doDetachChildren(self): - #We need to force the use of a panel subclass method here, - #for the same reason as doAttachChildren + # We need to force the use of a panel subclass method here, + # for the same reason as doAttachChildren VerticalPanel.doDetachChildren(self) def doAttachChildren(self): - #We need to force the use of a panel subclass method here, else - #the event will not propagate to children + # We need to force the use of a panel subclass method here, else + # the event will not propagate to children VerticalPanel.doAttachChildren(self) + class ScrollPanelWrapper(SimplePanel): """Scroll Panel like component, wich use the full available space to work around percent size issue, it use some of the ideas found @@ -353,6 +363,7 @@ def scrollToBottom(self): self.setScrollPosition(self.spanel.getElement().scrollHeight) + class EmptyWidget(DropCell, SimplePanel): """Empty dropable panel""" @@ -360,70 +371,74 @@ SimplePanel.__init__(self) DropCell.__init__(self, host) #self.setWidget(HTML('')) - self.setSize('100%','100%') + self.setSize('100%', '100%') + class BorderWidget(EmptyWidget): def __init__(self, host): EmptyWidget.__init__(self, host) self.addStyleName('borderPanel') + class LeftBorderWidget(BorderWidget): def __init__(self, host): BorderWidget.__init__(self, host) self.addStyleName('leftBorderWidget') + class RightBorderWidget(BorderWidget): def __init__(self, host): BorderWidget.__init__(self, host) self.addStyleName('rightBorderWidget') + class BottomBorderWidget(BorderWidget): def __init__(self, host): BorderWidget.__init__(self, host) self.addStyleName('bottomBorderWidget') + class WidgetsPanel(ScrollPanelWrapper): - - def __init__(self, host, locked = False): + + def __init__(self, host, locked=False): 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 # if True: tab will not be removed when there are no more widgets inside self.selected = None self.flextable = FlexTable() - self.flextable.setSize('100%','100%') + self.flextable.setSize('100%', '100%') self.setWidget(self.flextable) self.setStyleName('widgetsPanel') _bottom = BottomBorderWidget(self.host) - self.flextable.setWidget(0, 0, _bottom) #There will be always an Empty widget on the last row, - #dropping a widget there will add a new row + self.flextable.setWidget(0, 0, _bottom) # There will be always an Empty widget on the last row, + # dropping a widget there will add a new row td_elt = _bottom.getElement().parentNode - DOM.setStyleAttribute(td_elt, "height", "1px") #needed so the cell adapt to the size of the border (specially in webkit) - self._max_cols = 1 #give the maximum number of columns i a raw + DOM.setStyleAttribute(td_elt, "height", "1px") # needed so the cell adapt to the size of the border (specially in webkit) + self._max_cols = 1 # give the maximum number of columns i a raw def isLocked(self): return self.locked def changeWidget(self, row, col, wid): """Change the widget in the given location, add row or columns when necessary""" - print "changing widget:", wid, row, col - last_row = max(0, self.flextable.getRowCount()-1) + print "changing widget:", wid.getDebugName(), row, col + last_row = max(0, self.flextable.getRowCount() - 1) try: prev_wid = self.flextable.getWidget(row, col) except: print "ERROR: Trying to change an unexisting widget !" return - cellFormatter = self.flextable.getFlexCellFormatter() if isinstance(prev_wid, BorderWidget): - #We are on a border, we must create a row and/or columns + # We are on a border, we must create a row and/or columns print "BORDER WIDGET" prev_wid.removeStyleName('dragover') - + if isinstance(prev_wid, BottomBorderWidget): - #We are on the bottom border, we create a new row + # We are on the bottom border, we create a new row self.flextable.insertRow(last_row) self.flextable.setWidget(last_row, 0, LeftBorderWidget(self.host)) self.flextable.setWidget(last_row, 1, wid) @@ -432,14 +447,14 @@ row = last_row elif isinstance(prev_wid, LeftBorderWidget): - if col!=0: + if col != 0: print "ERROR: LeftBorderWidget must be on the first column !" return - self.flextable.insertCell(row, col+1) + self.flextable.insertCell(row, col + 1) self.flextable.setWidget(row, 1, wid) elif isinstance(prev_wid, RightBorderWidget): - if col!=self.flextable.getCellCount(row)-1: + if col != self.flextable.getCellCount(row) - 1: print "ERROR: RightBorderWidget must be on the last column !" return self.flextable.insertCell(row, col) @@ -448,7 +463,7 @@ else: prev_wid.removeFromParent() self.flextable.setWidget(row, col, wid) - + _max_cols = max(self._max_cols, self.flextable.getCellCount(row)) if _max_cols != self._max_cols: self._max_cols = _max_cols @@ -456,8 +471,8 @@ def _sizesAdjust(self): cellFormatter = self.flextable.getFlexCellFormatter() - width = 100.0/max(1, self._max_cols-2) #we don't count the borders - + width = 100.0 / max(1, self._max_cols - 2) # we don't count the borders + for row_idx in xrange(self.flextable.getRowCount()): for col_idx in xrange(self.flextable.getCellCount(row_idx)): _widget = self.flextable.getWidget(row_idx, col_idx) @@ -465,13 +480,13 @@ td_elt = _widget.getElement().parentNode DOM.setStyleAttribute(td_elt, "width", "%.2f%%" % width) - last_row = max(0, self.flextable.getRowCount()-1) + last_row = max(0, self.flextable.getRowCount() - 1) cellFormatter.setColSpan(last_row, 0, self._max_cols) def addWidget(self, wid): """Add a widget to a new cell on the next to last row""" - last_row = max(0, self.flextable.getRowCount()-1) - print "putting widget %s at %d, %d" % (wid, last_row, 0) + last_row = max(0, self.flextable.getRowCount() - 1) + print "putting widget %s at %d, %d" % (wid.getDebugName(), last_row, 0) self.changeWidget(last_row, 0, wid) def removeWidget(self, wid): @@ -479,7 +494,7 @@ _row, _col = self.flextable.getIndex(wid) self.flextable.remove(wid) self.flextable.removeCell(_row, _col) - if not self.getLiberviaRowWidgets(_row): #we have no more widgets, we remove the row + if not self.getLiberviaRowWidgets(_row): # we have no more widgets, we remove the row self.flextable.removeRow(_row) _max_cols = 1 for row_idx in xrange(self.flextable.getRowCount()): @@ -489,10 +504,10 @@ self._sizesAdjust() current = self - blank_page = self.getLiberviaWidgetsCount() == 0 # do we still have widgets on the page ? + blank_page = self.getLiberviaWidgetsCount() == 0 # do we still have widgets on the page ? if blank_page and not self.isLocked(): - #we now notice the MainTabPanel that the WidgetsPanel is empty and need to be removed + # we now notice the MainTabPanel that the WidgetsPanel is empty and need to be removed while current is not None: if isinstance(current, MainTabPanel): current.onWidgetPanelRemove(self) @@ -525,15 +540,15 @@ def getColSpan(self, row, col): cellFormatter = self.flextable.getFlexCellFormatter() return cellFormatter.getColSpan(row, col) - + def setColSpan(self, row, col, value): cellFormatter = self.flextable.getFlexCellFormatter() return cellFormatter.setColSpan(row, col, value) - + def getRowSpan(self, row, col): cellFormatter = self.flextable.getFlexCellFormatter() return cellFormatter.getRowSpan(row, col) - + def setRowSpan(self, row, col, value): cellFormatter = self.flextable.getFlexCellFormatter() return cellFormatter.setRowSpan(row, col, value) @@ -550,9 +565,9 @@ def _getIndex(self): """ get current index of the DropTab """ - # XXX: awful hack, but seems the only way to get index + # 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: @@ -572,20 +587,20 @@ 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. + # '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") + 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=' ' + item = ' ' item_type = None if item_type == "WIDGET": if not LiberviaDragWidget.current: @@ -594,7 +609,7 @@ _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) + _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host, item) else: print "WARNING: unmanaged item type" return @@ -606,7 +621,7 @@ def __init__(self, host): TabPanel.__init__(self) - self.host=host + self.host = host self.tabBar.setVisible(False) self.setStyleName('liberviaTabPanel') self.addStyleName('mainTabPanel') @@ -632,7 +647,7 @@ def add(self, widget, text=''): tab = DropTab(self, text) TabPanel.add(self, widget, tab, False) - if self.getWidgetCount()>1: + if self.getWidgetCount() > 1: self.tabBar.setVisible(True) self.host.resize() @@ -646,4 +661,4 @@ self.selectTab(0) else: self.selectTab(widgets_count - 1) - +