comparison browser_side/base_widget.py @ 439:d52f529a6d42

browser side: use of new log system (first draft): - configuration is hardcoded in libervia.py, it will change in the (hopefuly) near future - log level is DEBUG for the moment, will be changed to INFO when configuration will not be hardcoded anymore - the basic log backend is used, in the future, a console.debug/info/etc should be used instead. A log widget which HTML colors is also an option
author Goffi <goffi@goffi.org>
date Thu, 08 May 2014 17:21:34 +0200
parents 8ecc5a7062e4
children
comparison
equal deleted inserted replaced
438:582c435dab6b 439:d52f529a6d42
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 import pyjd # this is dummy in pyjs 20 import pyjd # this is dummy in pyjs
21 from sat.core.log import getLogger
22 log = getLogger(__name__)
21 from pyjamas.ui.SimplePanel import SimplePanel 23 from pyjamas.ui.SimplePanel import SimplePanel
22 from pyjamas.ui.AbsolutePanel import AbsolutePanel 24 from pyjamas.ui.AbsolutePanel import AbsolutePanel
23 from pyjamas.ui.VerticalPanel import VerticalPanel 25 from pyjamas.ui.VerticalPanel import VerticalPanel
24 from pyjamas.ui.HorizontalPanel import HorizontalPanel 26 from pyjamas.ui.HorizontalPanel import HorizontalPanel
25 from pyjamas.ui.ScrollPanel import ScrollPanel 27 from pyjamas.ui.ScrollPanel import ScrollPanel
114 try: 116 try:
115 item, item_type = dt.getData("text/plain").split('\n') # Workaround for webkit, only text/plain seems to be managed 117 item, item_type = dt.getData("text/plain").split('\n') # Workaround for webkit, only text/plain seems to be managed
116 if item_type and item_type[-1] == '\0': # Workaround for what looks like a pyjamas bug: the \0 should not be there, and 118 if item_type and item_type[-1] == '\0': # Workaround for what looks like a pyjamas bug: the \0 should not be there, and
117 item_type = item_type[:-1] # .strip('\0') and .replace('\0','') don't work. TODO: check this and fill a bug report 119 item_type = item_type[:-1] # .strip('\0') and .replace('\0','') don't work. TODO: check this and fill a bug report
118 # item_type = dt.getData("type") 120 # item_type = dt.getData("type")
119 print "message: %s" % item 121 log.debug("message: %s" % item)
120 print "type: %s" % item_type 122 log.debug("type: %s" % item_type)
121 except: 123 except:
122 print "no message found" 124 log.debug("no message found")
123 item = '&nbsp;' 125 item = '&nbsp;'
124 item_type = None 126 item_type = None
125 if item_type == "WIDGET": 127 if item_type == "WIDGET":
126 if not LiberviaDragWidget.current: 128 if not LiberviaDragWidget.current:
127 print "ERROR: No widget registered in LiberviaDragWidget !" 129 log.error("No widget registered in LiberviaDragWidget !")
128 return 130 return
129 _new_panel = LiberviaDragWidget.current 131 _new_panel = LiberviaDragWidget.current
130 if self == _new_panel: # We can't drop on ourself 132 if self == _new_panel: # We can't drop on ourself
131 return 133 return
132 # we need to remove the widget from the panel as it will be inserted elsewhere 134 # we need to remove the widget from the panel as it will be inserted elsewhere
139 return 141 return
140 widgets_panel.removeWidget(_new_panel) 142 widgets_panel.removeWidget(_new_panel)
141 elif item_type in self.drop_keys: 143 elif item_type in self.drop_keys:
142 _new_panel = self.drop_keys[item_type](self.host, item) 144 _new_panel = self.drop_keys[item_type](self.host, item)
143 else: 145 else:
144 print "WARNING: unmanaged item type" 146 log.warning("unmanaged item type")
145 return 147 return
146 if isinstance(self, LiberviaWidget): 148 if isinstance(self, LiberviaWidget):
147 self.host.unregisterWidget(self) 149 self.host.unregisterWidget(self)
148 self.onQuit() 150 self.onQuit()
149 if not isinstance(_new_panel, LiberviaWidget): 151 if not isinstance(_new_panel, LiberviaWidget):
150 print ('WARNING: droping an object which is not a class of LiberviaWidget') 152 log.warning("droping an object which is not a class of LiberviaWidget")
151 _flextable = self.getParent() 153 _flextable = self.getParent()
152 _widgetspanel = _flextable.getParent().getParent() 154 _widgetspanel = _flextable.getParent().getParent()
153 row_idx, cell_idx = self._getCellAndRow(_flextable, event) 155 row_idx, cell_idx = self._getCellAndRow(_flextable, event)
154 if self.host.getSelected == self: 156 if self.host.getSelected == self:
155 self.host.setSelected(None) 157 self.host.setSelected(None)
227 229
228 def getParent(self, class_=None, verbose=True): 230 def getParent(self, class_=None, verbose=True):
229 """ 231 """
230 Note: this method overrides pyjamas.ui.Widget.getParent 232 Note: this method overrides pyjamas.ui.Widget.getParent
231 @param class_: class of the ancestor to look for or None to return the first parent 233 @param class_: class of the ancestor to look for or None to return the first parent
232 @param verbose: set to True to log error messages 234 @param verbose: set to True to log error messages # FIXME: must be removed
233 @return: the parent/ancestor or None if it has not been found 235 @return: the parent/ancestor or None if it has not been found
234 """ 236 """
235 current = Widget.getParent(self) 237 current = Widget.getParent(self)
236 if class_ is None: 238 if class_ is None:
237 return current # this is the default behavior 239 return current # this is the default behavior
238 while current is not None and not isinstance(current, class_): 240 while current is not None and not isinstance(current, class_):
239 current = Widget.getParent(current) 241 current = Widget.getParent(current)
240 if current is None and verbose: 242 if current is None and verbose:
241 print "Can't find parent %s for %s" % (class_, self) 243 log.debug("Can't find parent %s for %s" % (class_, self))
242 return current 244 return current
243 245
244 def onClick(self, sender): 246 def onClick(self, sender):
245 self.host.setSelected(self) 247 self.host.setSelected(self)
246 248
363 - ONE2ONE 365 - ONE2ONE
364 - MISC 366 - MISC
365 - NONE 367 - NONE
366 """ 368 """
367 if not self.__selectable: 369 if not self.__selectable:
368 print "ERROR: getWarningLevel must not be called for an unselectable widget" 370 log.error("getWarningLevel must not be called for an unselectable widget")
369 raise Exception 371 raise Exception
370 # TODO: cleaner warning types (more general constants) 372 # TODO: cleaner warning types (more general constants)
371 return ("NONE", None) 373 return ("NONE", None)
372 374
373 def setWidget(self, widget, scrollable=True): 375 def setWidget(self, widget, scrollable=True):
484 def isLocked(self): 486 def isLocked(self):
485 return self.locked 487 return self.locked
486 488
487 def changeWidget(self, row, col, wid): 489 def changeWidget(self, row, col, wid):
488 """Change the widget in the given location, add row or columns when necessary""" 490 """Change the widget in the given location, add row or columns when necessary"""
489 print "changing widget:", wid.getDebugName(), row, col 491 log.debug("changing widget: %s %s %s" % (wid.getDebugName(), row, col))
490 last_row = max(0, self.flextable.getRowCount() - 1) 492 last_row = max(0, self.flextable.getRowCount() - 1)
491 try: 493 try:
492 prev_wid = self.flextable.getWidget(row, col) 494 prev_wid = self.flextable.getWidget(row, col)
493 except: 495 except:
494 print "ERROR: Trying to change an unexisting widget !" 496 log.error("Trying to change an unexisting widget !")
495 return 497 return
496 498
497 cellFormatter = self.flextable.getFlexCellFormatter() 499 cellFormatter = self.flextable.getFlexCellFormatter()
498 500
499 if isinstance(prev_wid, BorderWidget): 501 if isinstance(prev_wid, BorderWidget):
500 # We are on a border, we must create a row and/or columns 502 # We are on a border, we must create a row and/or columns
501 print "BORDER WIDGET" 503 log.debug("BORDER WIDGET")
502 prev_wid.removeStyleName('dragover') 504 prev_wid.removeStyleName('dragover')
503 505
504 if isinstance(prev_wid, BottomBorderWidget): 506 if isinstance(prev_wid, BottomBorderWidget):
505 # We are on the bottom border, we create a new row 507 # We are on the bottom border, we create a new row
506 self.flextable.insertRow(last_row) 508 self.flextable.insertRow(last_row)
510 cellFormatter.setHorizontalAlignment(last_row, 2, HasAlignment.ALIGN_RIGHT) 512 cellFormatter.setHorizontalAlignment(last_row, 2, HasAlignment.ALIGN_RIGHT)
511 row = last_row 513 row = last_row
512 514
513 elif isinstance(prev_wid, LeftBorderWidget): 515 elif isinstance(prev_wid, LeftBorderWidget):
514 if col != 0: 516 if col != 0:
515 print "ERROR: LeftBorderWidget must be on the first column !" 517 log.error("LeftBorderWidget must be on the first column !")
516 return 518 return
517 self.flextable.insertCell(row, col + 1) 519 self.flextable.insertCell(row, col + 1)
518 self.flextable.setWidget(row, 1, wid) 520 self.flextable.setWidget(row, 1, wid)
519 521
520 elif isinstance(prev_wid, RightBorderWidget): 522 elif isinstance(prev_wid, RightBorderWidget):
521 if col != self.flextable.getCellCount(row) - 1: 523 if col != self.flextable.getCellCount(row) - 1:
522 print "ERROR: RightBorderWidget must be on the last column !" 524 log.error("RightBorderWidget must be on the last column !")
523 return 525 return
524 self.flextable.insertCell(row, col) 526 self.flextable.insertCell(row, col)
525 self.flextable.setWidget(row, col, wid) 527 self.flextable.setWidget(row, col, wid)
526 528
527 else: 529 else:
548 cellFormatter.setColSpan(last_row, 0, self._max_cols) 550 cellFormatter.setColSpan(last_row, 0, self._max_cols)
549 551
550 def addWidget(self, wid): 552 def addWidget(self, wid):
551 """Add a widget to a new cell on the next to last row""" 553 """Add a widget to a new cell on the next to last row"""
552 last_row = max(0, self.flextable.getRowCount() - 1) 554 last_row = max(0, self.flextable.getRowCount() - 1)
553 print "putting widget %s at %d, %d" % (wid.getDebugName(), last_row, 0) 555 log.debug("putting widget %s at %d, %d" % (wid.getDebugName(), last_row, 0))
554 self.changeWidget(last_row, 0, wid) 556 self.changeWidget(last_row, 0, wid)
555 557
556 def removeWidget(self, wid): 558 def removeWidget(self, wid):
557 """Remove a widget and the cell where it is""" 559 """Remove a widget and the cell where it is"""
558 _row, _col = self.flextable.getIndex(wid) 560 _row, _col = self.flextable.getIndex(wid)
575 while current is not None: 577 while current is not None:
576 if isinstance(current, MainTabPanel): 578 if isinstance(current, MainTabPanel):
577 current.onWidgetPanelRemove(self) 579 current.onWidgetPanelRemove(self)
578 return 580 return
579 current = current.getParent() 581 current = current.getParent()
580 print "Error: no MainTabPanel found !" 582 log.error("no MainTabPanel found !")
581 583
582 def getWidgetCoords(self, wid): 584 def getWidgetCoords(self, wid):
583 return self.flextable.getIndex(wid) 585 return self.flextable.getIndex(wid)
584 586
585 def getLiberviaRowWidgets(self, row): 587 def getLiberviaRowWidgets(self, row):
658 try: 660 try:
659 item, item_type = dt.getData("text/plain").split('\n') # Workaround for webkit, only text/plain seems to be managed 661 item, item_type = dt.getData("text/plain").split('\n') # Workaround for webkit, only text/plain seems to be managed
660 if item_type and item_type[-1] == '\0': # Workaround for what looks like a pyjamas bug: the \0 should not be there, and 662 if item_type and item_type[-1] == '\0': # Workaround for what looks like a pyjamas bug: the \0 should not be there, and
661 item_type = item_type[:-1] # .strip('\0') and .replace('\0','') don't work. TODO: check this and fill a bug report 663 item_type = item_type[:-1] # .strip('\0') and .replace('\0','') don't work. TODO: check this and fill a bug report
662 # item_type = dt.getData("type") 664 # item_type = dt.getData("type")
663 print "message: %s" % item 665 log.debug("message: %s" % item)
664 print "type: %s" % item_type 666 log.debug("type: %s" % item_type)
665 except: 667 except:
666 print "no message found" 668 log.debug("no message found")
667 item = '&nbsp;' 669 item = '&nbsp;'
668 item_type = None 670 item_type = None
669 if item_type == "WIDGET": 671 if item_type == "WIDGET":
670 if not LiberviaDragWidget.current: 672 if not LiberviaDragWidget.current:
671 print "ERROR: No widget registered in LiberviaDragWidget !" 673 log.error("No widget registered in LiberviaDragWidget !")
672 return 674 return
673 _new_panel = LiberviaDragWidget.current 675 _new_panel = LiberviaDragWidget.current
674 _new_panel.getWidgetsPanel().removeWidget(_new_panel) 676 _new_panel.getWidgetsPanel().removeWidget(_new_panel)
675 elif item_type in DropCell.drop_keys: 677 elif item_type in DropCell.drop_keys:
676 _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host, item) 678 _new_panel = DropCell.drop_keys[item_type](self.tab_panel.host, item)
677 else: 679 else:
678 print "WARNING: unmanaged item type" 680 log.warning("unmanaged item type")
679 return 681 return
680 682
681 widgets_panel = self.tab_panel.getWidget(self._getIndex()) 683 widgets_panel = self.tab_panel.getWidget(self._getIndex())
682 widgets_panel.addWidget(_new_panel) 684 widgets_panel.addWidget(_new_panel)
683 685
698 700
699 def onWindowResized(self, width, height): 701 def onWindowResized(self, width, height):
700 tab_panel_elt = self.getElement() 702 tab_panel_elt = self.getElement()
701 _elts = doc().getElementsByClassName('gwt-TabBar') 703 _elts = doc().getElementsByClassName('gwt-TabBar')
702 if not _elts.length: 704 if not _elts.length:
703 print ("ERROR: no TabBar found, it should exist !") 705 log.error("no TabBar found, it should exist !")
704 tab_bar_h = 0 706 tab_bar_h = 0
705 else: 707 else:
706 tab_bar_h = _elts.item(0).offsetHeight 708 tab_bar_h = _elts.item(0).offsetHeight
707 ideal_height = height - DOM.getAbsoluteTop(tab_panel_elt) - tab_bar_h - 5 709 ideal_height = height - DOM.getAbsoluteTop(tab_panel_elt) - tab_bar_h - 5
708 ideal_width = width - DOM.getAbsoluteLeft(tab_panel_elt) - 5 710 ideal_width = width - DOM.getAbsoluteLeft(tab_panel_elt) - 5