Mercurial > libervia-web
comparison browser_side/panels.py @ 85:a8f027738c16
browser side: widgets cells can now be added by putting a widget on a border
author | Goffi <goffi@goffi.org> |
---|---|
date | Mon, 27 Jun 2011 03:14:37 +0200 |
parents | 8f35e9970e7f |
children | 6c3b3254605f |
comparison
equal
deleted
inserted
replaced
84:8f35e9970e7f | 85:a8f027738c16 |
---|---|
51 from __pyjamas__ import doc | 51 from __pyjamas__ import doc |
52 | 52 |
53 class DropCell(DropWidget): | 53 class DropCell(DropWidget): |
54 """Cell in the middle grid which replace itself with the dropped widget on DnD""" | 54 """Cell in the middle grid which replace itself with the dropped widget on DnD""" |
55 | 55 |
56 def __init__(self): | 56 def __init__(self, host): |
57 DropWidget.__init__(self) | 57 DropWidget.__init__(self) |
58 self.host = host | |
58 self.setStyleName('dropCell') | 59 self.setStyleName('dropCell') |
59 | 60 |
60 def onDragEnter(self, event): | 61 def onDragEnter(self, event): |
61 self.addStyleName('dragover') | 62 self.addStyleName('dragover') |
62 DOM.eventPreventDefault(event) | 63 DOM.eventPreventDefault(event) |
110 return False | 111 return False |
111 if isinstance(self, LiberviaWidget): | 112 if isinstance(self, LiberviaWidget): |
112 self.host.unregisterWidget(self) | 113 self.host.unregisterWidget(self) |
113 if not isinstance(_new_panel, LiberviaWidget): | 114 if not isinstance(_new_panel, LiberviaWidget): |
114 print ('WARNING: droping an object which is not a class of LiberviaWidget') | 115 print ('WARNING: droping an object which is not a class of LiberviaWidget') |
115 grid = self.getParent() | 116 _flextable = self.getParent() |
116 row_idx, cell_idx = self._getCellAndRow(grid, event) | 117 _widgetpanel = _flextable.getParent() |
118 row_idx, cell_idx = self._getCellAndRow(_flextable, event) | |
117 if self.host.selected == self: | 119 if self.host.selected == self: |
118 self.host.select(None) | 120 self.host.select(None) |
119 self.removeFromParent() | 121 _widgetpanel.changeWidget(row_idx, cell_idx, _new_panel) |
120 grid.setWidget(row_idx, cell_idx, _new_panel) | 122 """_unempty_panels = filter(lambda wid:not isinstance(wid,EmptyWidget),list(_flextable)) |
121 _panels = list(grid) #this suppose that we only use 1 row, need to be changed in the futur | |
122 _unempty_panels = filter(lambda wid:not isinstance(wid,EmptyPanel),list(grid)) | |
123 _width = 90/float(len(_unempty_panels) or 1) | 123 _width = 90/float(len(_unempty_panels) or 1) |
124 #now we resize all the cell of the column | 124 #now we resize all the cell of the column |
125 for panel in _unempty_panels: | 125 for panel in _unempty_panels: |
126 td_elt = panel.getElement().parentNode | 126 td_elt = panel.getElement().parentNode |
127 DOM.setStyleAttribute(td_elt, "width", "%s%%" % _width) | 127 DOM.setStyleAttribute(td_elt, "width", "%s%%" % _width)""" |
128 #FIXME: delete object ? Check the right way with pyjamas | 128 #FIXME: delete object ? Check the right way with pyjamas |
129 | 129 |
130 class LiberviaWidget(DropCell, VerticalPanel, ClickHandler): | 130 class LiberviaWidget(DropCell, VerticalPanel, ClickHandler): |
131 """Libervia's widget which can replace itself with a dropped widget on DnD""" | 131 """Libervia's widget which can replace itself with a dropped widget on DnD""" |
132 | 132 |
134 """Init the widget | 134 """Init the widget |
135 @param host: SatWebFrontend object | 135 @param host: SatWebFrontend object |
136 @param title: title show in the header of the widget | 136 @param title: title show in the header of the widget |
137 @param selectable: True is widget can be selected by user""" | 137 @param selectable: True is widget can be selected by user""" |
138 VerticalPanel.__init__(self) | 138 VerticalPanel.__init__(self) |
139 DropCell.__init__(self) | 139 DropCell.__init__(self, host) |
140 ClickHandler.__init__(self) | 140 ClickHandler.__init__(self) |
141 self.host = host | |
142 self.__selectable = selectable | 141 self.__selectable = selectable |
143 self.__title_id = HTMLPanel.createUniqueId() | 142 self.__title_id = HTMLPanel.createUniqueId() |
144 self.__setting_button_id = HTMLPanel.createUniqueId() | 143 self.__setting_button_id = HTMLPanel.createUniqueId() |
145 self.__close_button_id = HTMLPanel.createUniqueId() | 144 self.__close_button_id = HTMLPanel.createUniqueId() |
146 header = AbsolutePanel() | 145 header = AbsolutePanel() |
231 self.spanel.setScrollPosition(position) | 230 self.spanel.setScrollPosition(position) |
232 | 231 |
233 def scrollToBottom(self): | 232 def scrollToBottom(self): |
234 self.setScrollPosition(self.spanel.getElement().scrollHeight) | 233 self.setScrollPosition(self.spanel.getElement().scrollHeight) |
235 | 234 |
236 class EmptyPanel(DropCell, SimplePanel): | 235 class EmptyWidget(DropCell, SimplePanel): |
237 """Empty dropable panel""" | 236 """Empty dropable panel""" |
238 | 237 |
239 def __init__(self, host): | 238 def __init__(self, host): |
240 SimplePanel.__init__(self) | 239 SimplePanel.__init__(self) |
241 self.host = host | 240 DropCell.__init__(self, host) |
241 self.setWidget(HTML(' ')) | |
242 self.setSize('100%','100%') | 242 self.setSize('100%','100%') |
243 DropCell.__init__(self) | 243 |
244 self.setWidget(HTML(' ')) | 244 class BorderWidget(EmptyWidget): |
245 | 245 def __init__(self, host): |
246 EmptyWidget.__init__(self, host) | |
247 self.addStyleName('borderPanel') | |
248 | |
249 class LeftBorderWidget(BorderWidget): | |
250 def __init__(self, host): | |
251 BorderWidget.__init__(self, host) | |
252 self.addStyleName('leftBorderWidget') | |
253 | |
254 class RightBorderWidget(BorderWidget): | |
255 def __init__(self, host): | |
256 BorderWidget.__init__(self, host) | |
257 self.addStyleName('rightBorderWidget') | |
258 | |
259 class BottomBorderWidget(BorderWidget): | |
260 def __init__(self, host): | |
261 BorderWidget.__init__(self, host) | |
262 self.addStyleName('bottomBorderWidget') | |
263 | |
246 class UniBoxPanel(SimplePanel): | 264 class UniBoxPanel(SimplePanel): |
247 """Panel containing the UniBox""" | 265 """Panel containing the UniBox""" |
248 | 266 |
249 def __init__(self, host): | 267 def __init__(self, host): |
250 SimplePanel.__init__(self) | 268 SimplePanel.__init__(self) |
616 if game_type=="Tarot": | 634 if game_type=="Tarot": |
617 return self.tarot_panel | 635 return self.tarot_panel |
618 | 636 |
619 class WidgetsPanel(SimplePanel): | 637 class WidgetsPanel(SimplePanel): |
620 | 638 |
621 def __init__(self): | 639 def __init__(self, host): |
622 SimplePanel.__init__(self) | 640 SimplePanel.__init__(self) |
641 self.host = host | |
623 self.flextable = FlexTable() | 642 self.flextable = FlexTable() |
624 self.flextable.setSize('100%','100%') | 643 self.flextable.setSize('100%','100%') |
625 self.add(self.flextable) | 644 self.add(self.flextable) |
626 self.setStyleName('widgetsPanel') | 645 self.setStyleName('widgetsPanel') |
627 | 646 _bottom = BottomBorderWidget(self.host) |
647 self.flextable.setWidget(0, 0, _bottom) #There will be always an Empty widget on the last row, | |
648 #dropping a widget there will add a new row | |
649 self._max_cols = 1 #give the maximum number of columns i a raw | |
650 | |
651 def changeWidget(self, row, col, wid): | |
652 """Change the widget in the given location, add row or columns when necessary""" | |
653 print "changing widget:", wid, row, col | |
654 last_row = max(0, self.flextable.getRowCount()-1) | |
655 try: | |
656 prev_wid = self.flextable.getWidget(row, col) | |
657 except: | |
658 print "ERROR: Trying to change an unexisting widget !" | |
659 return | |
660 | |
661 cellFormatter = self.flextable.getFlexCellFormatter() | |
662 | |
663 if isinstance(prev_wid, BorderWidget): | |
664 #We are on a border, we must create a row and/or columns | |
665 print "BORDER WIDGET" | |
666 prev_wid.removeStyleName('dragover') | |
667 | |
668 if isinstance(prev_wid, BottomBorderWidget): | |
669 #We are on the bottom border, we create a new row | |
670 self.flextable.insertRow(last_row) | |
671 self.flextable.setWidget(last_row, 0, LeftBorderWidget(self.host)) | |
672 self.flextable.setWidget(last_row, 1, wid) | |
673 self.flextable.setWidget(last_row, 2, RightBorderWidget(self.host)) | |
674 cellFormatter.setHorizontalAlignment(last_row, 2, HasAlignment.ALIGN_RIGHT) | |
675 row = last_row | |
676 last_row+=1 | |
677 | |
678 elif isinstance(prev_wid, LeftBorderWidget): | |
679 if col!=0: | |
680 print "ERROR: LeftBorderWidget must be on the first column !" | |
681 return | |
682 self.flextable.insertCell(row, col+1) | |
683 self.flextable.setWidget(row, 1, wid) | |
684 | |
685 elif isinstance(prev_wid, RightBorderWidget): | |
686 if col!=self.flextable.getCellCount(row)-1: | |
687 print "ERROR: RightBorderWidget must be on the last column !" | |
688 return | |
689 self.flextable.insertCell(row, col) | |
690 self.flextable.setWidget(row, col, wid) | |
691 | |
692 else: | |
693 prev_wid.removeFromParent() | |
694 self.flextable.setWidget(row, col, wid) | |
695 | |
696 _max_cols = max(self._max_cols, self.flextable.getCellCount(row)) | |
697 if _max_cols != self._max_cols: | |
698 #we hage to adjust sizes | |
699 self._max_cols = _max_cols | |
700 width = 100.0/max(1, self._max_cols-2) #we don't count the borders | |
701 for row_idx in range(self.flextable.getRowCount()): | |
702 for col_idx in range(self.flextable.getCellCount(row_idx)): | |
703 _widget = self.flextable.getWidget(row_idx, col_idx) | |
704 if not isinstance(_widget, BorderWidget): | |
705 td_elt = _widget.getElement().parentNode | |
706 DOM.setStyleAttribute(td_elt, "width", "%.2f%%" % width) | |
707 | |
708 cellFormatter.setColSpan(last_row, 0, self._max_cols) | |
709 | |
710 | |
628 def addWidget(self, wid): | 711 def addWidget(self, wid): |
629 """Add a widget to a new cell""" | 712 """Add a widget to a new cell on the next to last row""" |
630 row = max(0, self.flextable.getRowCount()-1) | 713 last_row = max(0, self.flextable.getRowCount()-1) |
631 try: | 714 print "putting widget %s at %d, %d" % (wid, last_row, 0) |
632 col = self.flextable.getCellCount(0) | 715 self.changeWidget(last_row, 0, wid) |
633 except AttributeError: | |
634 col = 0 | |
635 print "putting widget %s at %d, %d" % (wid, row, col) | |
636 self.flextable.setWidget(row, col, wid) | |
637 | 716 |
638 class MainDiscussionPanel(HorizontalPanel): | 717 class MainDiscussionPanel(HorizontalPanel): |
639 | 718 |
640 def __init__(self, host): | 719 def __init__(self, host): |
641 self.host=host | 720 self.host=host |
642 HorizontalPanel.__init__(self) | 721 HorizontalPanel.__init__(self) |
643 self._left = self.host.contact_panel | 722 self._left = self.host.contact_panel |
644 self._right = WidgetsPanel() | 723 self._right = WidgetsPanel(host) |
645 self._right.setWidth('100%') | 724 self._right.setWidth('100%') |
646 self._right.setHeight('100%') | 725 self._right.setHeight('100%') |
647 self.add(self._left) | 726 self.add(self._left) |
648 #self.setCellWidth(self._left, "15%") | |
649 self.add(self._right) | 727 self.add(self._right) |
650 #self.setCellWidth(self._right, "85%") | |
651 self.setCellWidth(self._right, "100%") | 728 self.setCellWidth(self._right, "100%") |
652 | 729 |
653 def addWidget(self, wid): | 730 def addWidget(self, wid): |
654 """Add a widget to the WidgetsPanel""" | 731 """Add a widget to the WidgetsPanel""" |
655 print "main addWidget", wid | 732 print "main addWidget", wid |
656 self._right.addWidget(wid) | 733 self._right.addWidget(wid) |
657 | |
658 def changePanel(self, idx, panel): | |
659 pass | |
660 #self._right.setWidget(0,idx,panel) | |
661 #self._right.getCellFormatter().setWidth(0, idx, '5%' if isinstance(panel, EmptyPanel) else '90%') | |
662 | 734 |
663 class MainTabPanel(TabPanel): | 735 class MainTabPanel(TabPanel): |
664 | 736 |
665 def __init__(self, host): | 737 def __init__(self, host): |
666 TabPanel.__init__(self) | 738 TabPanel.__init__(self) |