def flextable(data): table = FlexTable() rows = len(data) columns = len(data[0]) datalist = flatten(data) columnlist = flatten([range(columns) for x in range(rows)]) rowlist = flatten([[x for y in range(columns)] for x in range(rows)]) tablelist = zip(rowlist,columnlist,datalist) for x in tablelist: table.setText(x[0],x[1],x[2]) table.setBorderWidth(2) return table
class DialogBoxModal(PopupPanel): def __init__(self, identifier, autoHide=None, modal=False, rootpanel=None): PopupPanel.__init__(self, autoHide, modal, rootpanel) self.identifier = identifier self.caption = HTML() self.child = None self.showing = False self.dragging = False self.dragStartX = 0 self.dragStartY = 0 self.panel = FlexTable() self.closeButton = Image("images/cancel.png") self.closeButton.addClickListener(self) dock = DockPanel() dock.setSpacing(0) dock.add(self.closeButton, DockPanel.EAST) dock.add(self.caption, DockPanel.WEST) dock.setCellHorizontalAlignment(self.closeButton, HasAlignment.ALIGN_RIGHT) dock.setCellHorizontalAlignment(self.caption, HasAlignment.ALIGN_LEFT) dock.setCellWidth(self.caption, "100%") dock.setWidth("100%") self.panel.setWidget(0, 0, dock) self.panel.setHeight("100%") self.panel.setBorderWidth(0) self.panel.setCellPadding(0) self.panel.setCellSpacing(0) self.panel.getCellFormatter().setHeight(1, 0, "100%") self.panel.getCellFormatter().setWidth(1, 0, "100%") #self.panel.getCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_CENTER, HasVerticalAlignment.ALIGN_MIDDLE) PopupPanel.setWidget(self, self.panel) self.setStyleName("gwt-DialogBox") self.caption.setStyleName("Caption") self.closeButton.setStyleName("Close") dock.setStyleName("Header") self.caption.addMouseListener(self) def getHTML(self): return self.caption.getHTML() def getText(self): return self.caption.getText() def onMouseDown(self, sender, x, y): self.dragging = True DOM.setCapture(self.caption.getElement()) self.dragStartX = x self.dragStartY = y def onMouseEnter(self, sender): pass def onMouseLeave(self, sender): pass def onMouseMove(self, sender, x, y): if self.dragging: absX = x + self.getAbsoluteLeft() absY = y + self.getAbsoluteTop() self.setPopupPosition(absX - self.dragStartX, absY - self.dragStartY) def onMouseUp(self, sender, x, y): self.dragging = False DOM.releaseCapture(self.caption.getElement()) def remove(self, widget): if self.child != widget: return False self.panel.remove(widget) self.child = None return True def setHTML(self, html): self.caption.setHTML(html) def setText(self, text): self.caption.setText(text) def doAttachChildren(self): PopupPanel.doAttachChildren(self) self.caption.onAttach() def doDetachChildren(self): PopupPanel.doDetachChildren(self) self.caption.onDetach() def setWidget(self, widget): if self.child is not None: self.panel.remove(self.child) if widget is not None: self.panel.setWidget(1, 0, widget) self.child = widget def createElement(self): return DOM.createDiv() def setPopupPosition(self, left, top): if left < 0: left = 0 if top < 0: top = 0 element = self.getElement() DOM.setStyleAttribute(element, "left", "%dpx" % left) DOM.setStyleAttribute(element, "top", "%dpx" % top) def show(self): if self.showing: return if modal_popups.has_key(self.identifier) and \ modal_popups[self.identifier] != self: return modal_popups[self.identifier] = self PopupPanel.show(self) def hide(self, autoClosed=False): if not self.showing: return if modal_popups.has_key(self.identifier): del modal_popups[self.identifier] PopupPanel.hide(self) def onEventPreview(self, event): # preventDefault on mousedown events, outside of the # dialog, to stop text-selection on dragging type = DOM.eventGetType(event) if type == 'mousedown': target = DOM.eventGetTarget(event) elem = self.caption.getElement() event_targets_popup = target and DOM.isOrHasChild(elem, target) if event_targets_popup: DOM.eventPreventDefault(event) return PopupPanel.onEventPreview(self, event)
class CollapserPanel(SimplePanel): def __init__(self, sink): SimplePanel.__init__(self) self.sink = sink self.caption = HTML() self.child = None self.showing = False self.dragging = False self.dragStartX = 0 self.dragStartY = 0 self.panel = FlexTable() self.collapse = Image("./images/cancel.png") self.collapse.addClickListener(self) dock = DockPanel() dock.setSpacing(0) dock.add(self.collapse, DockPanel.EAST) dock.add(self.caption, DockPanel.WEST) dock.setCellHorizontalAlignment(self.collapse, HasAlignment.ALIGN_RIGHT) dock.setCellVerticalAlignment(self.collapse, HasAlignment.ALIGN_TOP) dock.setCellHorizontalAlignment(self.caption, HasAlignment.ALIGN_LEFT) dock.setCellWidth(self.caption, "100%") dock.setWidth("100%") dock.setHeight("100%") self.panel.setWidget(0, 0, dock) self.panel.setHeight("100%") self.panel.setWidth("100%") self.panel.setBorderWidth(0) self.panel.setCellPadding(0) self.panel.setCellSpacing(0) self.panel.getCellFormatter().setHeight(1, 0, "100%") self.panel.getCellFormatter().setWidth(1, 0, "100%") self.panel.getCellFormatter().setAlignment( 1, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP) SimplePanel.setWidget(self, self.panel) self.setStyleName("gwt-DialogBox") self.caption.setStyleName("Caption") self.collapse.setStyleName("Close") dock.setStyleName("Header") #self.caption.addMouseListener(self) self.collapsed = False self.collapsed_width = "15px" self.uncollapsed_width = "100%" def setInitialWidth(self, width): self.uncollapsed_width = width SimplePanel.setWidth(self, width) self.sink.setCollapserWidth(self, width) def setHeight(self, height): SimplePanel.setHeight(self, height) def onClick(self, sender): if self.collapsed == False: self.collapse.setUrl("./tree_closed.gif") self.collapsed = True self.caption.setVisible(False) if self.child: self.child.setVisible(False) self.setWidth(self.collapsed_width) self.sink.setCollapserWidth(self, self.collapsed_width) else: self.collapse.setUrl("./images/cancel.png") self.collapsed = False self.caption.setVisible(True) if self.child: self.child.setVisible(True) self.setWidth(self.uncollapsed_width) self.sink.setCollapserWidth(self, self.uncollapsed_width) def setHTML(self, html): self.caption.setHTML(html) def setText(self, text): self.caption.setText(text) def remove(self, widget): if self.child != widget: return False self.panel.remove(widget) return True def doAttachChildren(self): SimplePanel.doAttachChildren(self) self.caption.onAttach() def doDetachChildren(self): SimplePanel.doDetachChildren(self) self.caption.onDetach() def setWidget(self, widget): if self.child is not None: self.panel.remove(self.child) if widget is not None: self.panel.setWidget(1, 0, widget) self.child = widget
class RightGrid(DockPanel): def __init__(self, title): DockPanel.__init__(self) self.grid = FlexTable() title = HTML(title) self.add(title, DockPanel.NORTH) self.setCellHorizontalAlignment(title, HasHorizontalAlignment.ALIGN_LEFT) self.add(self.grid, DockPanel.CENTER) self.grid.setBorderWidth("0px") self.grid.setCellSpacing("0px") self.grid.setCellPadding("4px") self.formatCell(0, 0) self.grid.setHTML(0, 0, " ") def clear_items(self): self.index = 0 self.items = {} def set_items(self, items): self.items = items self.index = 0 self.max_rows = 0 self.max_cols = 0 Timer(1, self) def onTimer(self, t): count = 0 while count < 10 and self.index < len(self.items): self._add_items(self.index) self.index += 1 count += 1 if self.index < len(self.items): Timer(1, self) def _add_items(self, i): item = self.items[i] command = item[0] col = item[1] row = item[2] data = item[3] format_row = -1 format_col = -1 if col + 1 > self.max_cols: format_col = self.max_cols #self.grid.resizeColumns(col+1) self.max_cols = col + 1 if row + 1 >= self.max_rows: format_row = self.max_rows #self.grid.resizeRows(row+1) self.max_rows = row + 1 if format_row >= 0: for k in range(format_row, self.max_rows): self.formatCell(k, 0) self.formatCell(row, col) cf = self.grid.getCellFormatter() if command == 'data': self.grid.setHTML(row, col, data) elif command == 'cellstyle': data = space_split(data) attr = data[0] val = data[1] cf.setStyleAttr(row, col, attr, val) elif command == 'align': data = space_split(data) vert = data[0] horiz = data[1] if vert != '-': cf.setVerticalAlignment(row, col, vert) if horiz != '-': cf.setHorizontalAlignment(row, col, horiz) elif command == 'cellspan': data = space_split(data) rowspan = data[0] colspan = data[1] if colspan != '-': cf.setColSpan(row, col, colspan) if rowspan != '-': cf.setRowSpan(row, col, rowspan) def formatCell(self, row, col): self.grid.prepareCell(row, col) if col == 0 and row != 0: self.grid.setHTML(row, col, "%d" % row) if row != 0 and col != 0: #self.grid.setHTML(row, col, " ") fmt = "rightpanel-cellformat" if col == 0 and row == 0: fmt = "rightpanel-cellcornerformat" elif row == 0: fmt = "rightpanel-celltitleformat" elif col == 0: fmt = "rightpanel-cellleftformat" self.grid.getCellFormatter().setStyleName(row, col, fmt)
class DialogBoxModal(PopupPanel): def __init__(self, identifier, autoHide=None, modal=False, rootpanel=None): PopupPanel.__init__(self, autoHide, modal, rootpanel) self.identifier = identifier self.caption = HTML() self.child = None self.showing = False self.dragging = False self.dragStartX = 0 self.dragStartY = 0 self.panel = FlexTable() self.closeButton = Image('cancel.png') self.closeButton.addClickListener(self) dock = DockPanel() dock.setSpacing(0) dock.add(self.closeButton, DockPanel.EAST) dock.add(self.caption, DockPanel.WEST) dock.setCellHorizontalAlignment(self.closeButton, HasAlignment.ALIGN_RIGHT) dock.setCellHorizontalAlignment(self.caption, HasAlignment.ALIGN_LEFT) dock.setCellWidth(self.caption, '100%') dock.setWidth('100%') self.panel.setWidget(0, 0, dock) self.panel.setHeight('100%') self.panel.setBorderWidth(0) self.panel.setCellPadding(0) self.panel.setCellSpacing(0) self.panel.getCellFormatter().setHeight(1, 0, '100%') self.panel.getCellFormatter().setWidth(1, 0, '100%') #self.panel.getCellFormatter().setAlignment(1, 0, # HasHorizontalAlignment.ALIGN_CENTER, # HasVerticalAlignment.ALIGN_MIDDLE) PopupPanel.setWidget(self, self.panel) self.setStyleName('gwt-DialogBox') self.caption.setStyleName('Caption') self.closeButton.setStyleName('Close') dock.setStyleName('Header') self.caption.addMouseListener(self) def getHTML(self): return self.caption.getHTML() def getText(self): return self.caption.getText() def onMouseDown(self, sender, x, y): self.dragging = True DOM.setCapture(self.caption.getElement()) self.dragStartX = x self.dragStartY = y def onMouseEnter(self, sender): pass def onMouseLeave(self, sender): pass def onMouseMove(self, sender, x, y): if self.dragging: absX = x + self.getAbsoluteLeft() absY = y + self.getAbsoluteTop() self.setPopupPosition(absX - self.dragStartX, absY - self.dragStartY) def onMouseUp(self, sender, x, y): self.dragging = False DOM.releaseCapture(self.caption.getElement()) def remove(self, widget): if self.child != widget: return False self.panel.remove(widget) self.child = None return True def setHTML(self, html): self.caption.setHTML(html) def setText(self, text): self.caption.setText(text) def doAttachChildren(self): PopupPanel.doAttachChildren(self) self.caption.onAttach() def doDetachChildren(self): PopupPanel.doDetachChildren(self) self.caption.onDetach() def setWidget(self, widget): if self.child is not None: self.panel.remove(self.child) if widget is not None: self.panel.setWidget(1, 0, widget) self.child = widget def createElement(self): return DOM.createDiv() def setPopupPosition(self, left, top): if left < 0: left = 0 if top < 0: top = 0 element = self.getElement() DOM.setStyleAttribute(element, 'left', '%dpx' % left) DOM.setStyleAttribute(element, 'top', '%dpx' % top) def show(self): if self.showing: return if (self.identifier in modal_popups and modal_popups[self.identifier] != self): return modal_popups[self.identifier] = self PopupPanel.show(self) def hide(self, autoClosed=False): if not self.showing: return if self.identifier in modal_popups: del modal_popups[self.identifier] PopupPanel.hide(self) def onEventPreview(self, event): # preventDefault on mousedown events, outside of the # dialog, to stop text-selection on dragging type = DOM.eventGetType(event) if type == 'mousedown': target = DOM.eventGetTarget(event) elem = self.caption.getElement() event_targets_popup = target and DOM.isOrHasChild(elem, target) if event_targets_popup: DOM.eventPreventDefault(event) return PopupPanel.onEventPreview(self, event)
class IntroPage: DiceInstance = 0 VarTempScore = 0 #Temporary Score Variable VarTotScore = [] #Total Score Variable CountTurn = 1 #Count to display player number in Temporary Score Board def __init__(self): self.DPanel = DockPanel(HorizontalAlignment = HasAlignment.ALIGN_CENTER, Spacing=10) # Creates the Docker Panel Instance self.VPanel = VerticalPanel() # Creates the Vertical Panel Instance self.VPanel1 = VerticalPanel() # Creates the Vertical Panel Instance self.HPanel = HorizontalPanel() # Creates a Horizontal Panel Instance self.HPanel1 = HorizontalPanel()# Creates a Horizontal Panel Instance self.image=Image()#Creates the Image instance to embed the images of dice self.DummyUrl = self.image.getUrl() self.timer = Timer(notify=self.StillImage)#Timer for display of gif animation self.timerRButton = Timer(notify=self.OneAlert)#Timer for controlling states of Roll button #whenever the output of the dice is 1 self.RollButton = Button("Roll", getattr(self, "RollButtonPressed")) #Initially Disabled self.RollButton.setEnabled(False) self.BankButton = Button("Bank", getattr(self, "BankButtonPressed")) #Initially Disabled self.BankButton.setEnabled(False) #The start button controls both the number players as well the winning score self.StartButton = Button("Start", getattr(self, "StartButtonPressed")) #Intially Enabled self.StartButton.setEnabled(True) self.PlayerNum = TextBox() #Enter the Number of Players self.WinScore = TextBox() #Enter the Target Score self.PlayerNum.setText("0") self.WinScore.setText("0") # self.OK = Button("OK", getattr(self, "okButtonPressed")) self.NameScore = FlexTable() #main score board self.NameScore.setStyleName("NameScore") self.TempBoard = FlexTable() #Temporary score board self.TempBoard.setStyleName("TempBoard") self.TxtInstructions = HTML() def StartButtonPressed(self): self.CountTurn = 1 if int(self.PlayerNum.getText()) >= 2 and int(self.PlayerNum.getText()) <= 6 and int(self.WinScore.getText()) >= 10 and int(self.WinScore.getText()) <= 100: self.DPanel.remove(self.TxtInstructions, DockPanel.CENTER) self.BankButton.setVisible(True) self.RollButton.setVisible(True) # self.image.setVisible(True) self.TempBoard.setVisible(True) self.NameScore.setVisible(True) self.image = Image( self.DummyUrl + "images/0.png") self.image.setSize("200px", "300px") self.DPanel.add(self.image, DockPanel.CENTER) RootPanel().add(self.DPanel) self.StartButton.setEnabled(False) self.PlayerNum.setEnabled(False) self.WinScore.setEnabled(False) self.RollButton.setEnabled(True) self.TempBoard.setText(1,0,"Player"+str(1)) self.TempBoard.setText(1, 1, "0") self.NameScore.getRowFormatter().addStyleName(self.CountTurn,"Rows") else: Window.alert("Please Enter Correct Parameters " ) #Command for alert window return 0 VarPlayer = ["Player" + str(i) for i in xrange(1,int(self.PlayerNum.getText())+1)] i = 0 while i < int(self.PlayerNum.getText()): self.NameScore.setText(i+1, 0, VarPlayer[i]) self.NameScore.setText(i+1, 1, "0") self.VarTotScore.append(0) #m*1 vector of zeros indicating the initial scores i += 1 def OneAlert(self): AlrtTxt = " Sorry, your turn is over" Window.alert(AlrtTxt) self.timerRButton.cancel() self.RollButton.setEnabled(True) def StillImage(self): self.DPanel.remove(self.image, DockPanel.CENTER) self.image = Image( self.DummyUrl + "images/" +str(self.DiceInstance)+".png") self.image.setSize("300px", "300px") self.DPanel.add(self.image, DockPanel.CENTER) self.DPanel.setCellHeight(self.image, "300px") self.DPanel.setCellWidth(self.image, "600px") RootPanel().add(self.DPanel) self.timer.cancel() if self.DiceInstance != 1: self.TempBoard.setText(1, 1, self.DiceInstance + int(self.TempBoard.getText(1, 1))) self.BankButton.setEnabled(True) self.RollButton.setEnabled(True) else: self.NameScore.getRowFormatter().removeStyleName(self.CountTurn,"Rows") self.RollButton.setEnabled(False) self.timerRButton.schedule(1500) self.CountTurn += 1 if self.CountTurn % int(self.PlayerNum.getText()) == 1: self.CountTurn = 1 self.TempBoard.setText(1,0,"Player"+str(self.CountTurn)) self.TempBoard.setText(1, 1, "0") self.NameScore.getRowFormatter().addStyleName(self.CountTurn,"Rows"); else: self.TempBoard.setText(1,0,"Player"+str(self.CountTurn)) self.TempBoard.setText(1, 1, "0") self.NameScore.getRowFormatter().addStyleName(self.CountTurn,"Rows"); def RollButtonPressed(self): self.DiceInstance = random.randint(1, 6) # value turned after rolling the dice self.DPanel.remove(self.image, DockPanel.CENTER) self.image = Image("http://www.animatedimages.org/data/media/710/animated-dice-image-0064.gif") self.image.setSize("100px", "200px") self.DPanel.add(self.image, DockPanel.CENTER) self.DPanel.setCellHeight(self.image, "300px") self.DPanel.setCellWidth(self.image, "600px") RootPanel().add(self.DPanel) self.BankButton.setEnabled(False) self.RollButton.setEnabled(False) self.timer.schedule(3000) def BankButtonPressed(self): self.BankButton.setEnabled(False) self.NameScore.setText(self.CountTurn, 1, int(self.NameScore.getText(self.CountTurn, 1)) + int(self.TempBoard.getText(1,1))) if int(self.NameScore.getText(self.CountTurn, 1)) >= int(self.WinScore.getText()): AlrtTxt = "Congratulation!!! Player"+ str(self.CountTurn) + " wins !!!!" Window.alert(AlrtTxt) self.DPanel.remove(self.image, DockPanel.CENTER) self.DPanel.add(self.TxtInstructions, DockPanel.CENTER) self.BankButton.setVisible(False) self.RollButton.setVisible(False) # self.image.setVisible(False) self.TempBoard.setVisible(False) self.NameScore.setVisible(False) i = int(self.PlayerNum.getText()) while i > 0: self.NameScore. removeRow(i) i -= 1 self.TempBoard.setText(1,0,"X") self.TempBoard.setText(1, 1, "0") self.StartButton.setEnabled(True) # self.OK.setEnabled(True) self.PlayerNum.setEnabled(True) self.WinScore.setEnabled(True) self.RollButton.setEnabled(False) self.BankButton.setEnabled(False) self.NameScore.getRowFormatter().removeStyleName(self.CountTurn,"Rows"); self.DPanel.remove(self.image, DockPanel.CENTER) self.image = Image( self.DummyUrl + "images/0.png") self.image.setSize("200px", "300px") self.DPanel.add(self.image, DockPanel.CENTER) self.DPanel.setCellHeight(self.image, "200px") self.DPanel.setCellWidth(self.image, "400px") RootPanel().add(self.DPanel) else: self.NameScore.getRowFormatter().removeStyleName(self.CountTurn,"Rows"); self.CountTurn += 1 if self.CountTurn % int(self.PlayerNum.getText()) == 1: self.CountTurn = 1 self.TempBoard.setText(1,0,"Player"+str(self.CountTurn)) self.TempBoard.setText(1, 1, "0") self.NameScore.getRowFormatter().addStyleName(self.CountTurn,"Rows"); else: self.TempBoard.setText(1,0,"Player"+str(self.CountTurn)) self.TempBoard.setText(1, 1, "0") self.NameScore.getRowFormatter().addStyleName(self.CountTurn,"Rows"); def OnGameLoad(self): self.NameScore.setText(0, 0, "Player ID") self.NameScore.setText(0, 1, "Score") self.NameScore.setCellSpacing(10) self.NameScore.setCellPadding(10) self.NameScore.setBorderWidth(2) self.NameScore.setVisible(False) self.TempBoard.setText(0, 0, "Player's Turn") self.TempBoard.setText(0, 1, "Temporary Score") self.TempBoard.setText(1, 0, "X") self.TempBoard.setText(1, 1, "0") self.TempBoard.setCellSpacing(10) self.TempBoard.setCellPadding(10) self.TempBoard.setBorderWidth(2) self.TempBoard.setVisible(False) #Adding StartButton to Dock panel self.DPanel.add(self.StartButton, DockPanel.EAST) self.DPanel.setCellHeight(self.StartButton, "200px") self.DPanel.setCellWidth(self.StartButton, "20px") Txt = HTML("<center><b>Enter Number of Players (between 2 & 6)</b><center>")#Adding playernumber and winscore textbox to Horizontal Panel Txt1 = HTML("<left><b>Enter Target Score (between 10 & 100)</b><left>") self.HPanel1.add(Txt) self.HPanel1.add(self.PlayerNum) self.HPanel1.add(Txt1) self.HPanel1.add(self.WinScore) self.HPanel1.add(self.StartButton) self.HPanel1.setSpacing(20) #Adding Horizontal panel containing playernumber and winscore textbox to Dock Panel self.DPanel.add(self.HPanel1, DockPanel.NORTH) self.DPanel.setCellHeight(self.HPanel1, "30px") self.DPanel.setCellWidth(self.HPanel1, "2000px") self.TxtInstructions = HTML("<b><u><center>Instructions</center></u><ul><li>Pig is game for 2 to 6 Players.</li><li>Players take turns rolling a dice as many times as they like. </li><li>If a roll is 2, 3, 4, 5 or 6, the player adds that many points to their score for the turn. </li><li>A player may choose to end their turn at any time and 'bank' their points.</li><li>If a player rolls a 1, they lose all their unbanked points and their turn is over.</li><li>The first player to score the target or more wins.</li></ul></b>") self.TxtInstructions.setStyleName("TxtInstructions") self.DPanel.add(self.TxtInstructions, DockPanel.CENTER) self.DPanel.add(self.NameScore, DockPanel.WEST) #Adding main scoreboard to Dock Panel self.DPanel.setCellHeight(self.NameScore, "300px") self.DPanel.setCellWidth(self.NameScore, "100px") self.DPanel.setSpacing(10) self.DPanel.setPadding(2) #Adding Tempboard and BankButton to Horizontal Panel self.HPanel.add(self.TempBoard) #Adding BankButton and RollButton to vertical panel self.VPanel.add(self.RollButton) self.RollButton.setVisible(False) self.VPanel.add(self.BankButton) self.BankButton.setVisible(False) self.VPanel.setSpacing(10) #Adding Vertical panel containing BankButton and RollButton to Horizontal Panel self.HPanel.add(self.VPanel) self.HPanel.setSpacing(40) #Adding Horizontal panel containing Tempboard and vertical panel containing BankButton and RollButton to Dock Panel self.DPanel.add(self.HPanel, DockPanel.SOUTH) self.DPanel.setCellHeight(self.HPanel, "20px") self.DPanel.setCellWidth(self.HPanel, "2000px") RootPanel().add(self.DPanel)
class CollapserPanel(SimplePanel): def __init__(self, sink): SimplePanel.__init__(self) self.sink = sink self.caption = HTML() self.child = None self.showing = False self.dragging = False self.dragStartX = 0 self.dragStartY = 0 self.panel = FlexTable() self.collapse = Image("./images/cancel.png") self.collapse.addClickListener(self) dock = DockPanel() dock.setSpacing(0) dock.add(self.collapse, DockPanel.EAST) dock.add(self.caption, DockPanel.WEST) dock.setCellHorizontalAlignment(self.collapse, HasAlignment.ALIGN_RIGHT) dock.setCellVerticalAlignment(self.collapse, HasAlignment.ALIGN_TOP) dock.setCellHorizontalAlignment(self.caption, HasAlignment.ALIGN_LEFT) dock.setCellWidth(self.caption, "100%") dock.setWidth("100%") dock.setHeight("100%") self.panel.setWidget(0, 0, dock) self.panel.setHeight("100%") self.panel.setWidth("100%") self.panel.setBorderWidth(0) self.panel.setCellPadding(0) self.panel.setCellSpacing(0) self.panel.getCellFormatter().setHeight(1, 0, "100%") self.panel.getCellFormatter().setWidth(1, 0, "100%") self.panel.getCellFormatter().setAlignment(1, 0, HasHorizontalAlignment.ALIGN_LEFT, HasVerticalAlignment.ALIGN_TOP) SimplePanel.setWidget(self, self.panel) self.setStyleName("gwt-DialogBox") self.caption.setStyleName("Caption") self.collapse.setStyleName("Close") dock.setStyleName("Header") #self.caption.addMouseListener(self) self.collapsed = False self.collapsed_width = "15px" self.uncollapsed_width = "100%" def setInitialWidth(self, width): self.uncollapsed_width = width SimplePanel.setWidth(self, width) self.sink.setCollapserWidth(self, width) def setHeight(self, height): SimplePanel.setHeight(self, height) def onClick(self, sender): if self.collapsed == False: self.collapse.setUrl("./tree_closed.gif") self.collapsed = True self.caption.setVisible(False) if self.child: self.child.setVisible(False) self.setWidth(self.collapsed_width) self.sink.setCollapserWidth(self, self.collapsed_width) else: self.collapse.setUrl("./images/cancel.png") self.collapsed = False self.caption.setVisible(True) if self.child: self.child.setVisible(True) self.setWidth(self.uncollapsed_width) self.sink.setCollapserWidth(self, self.uncollapsed_width) def setHTML(self, html): self.caption.setHTML(html) def setText(self, text): self.caption.setText(text) def remove(self, widget): if self.child != widget: return False self.panel.remove(widget) return True def doAttachChildren(self): SimplePanel.doAttachChildren(self) self.caption.onAttach() def doDetachChildren(self): SimplePanel.doDetachChildren(self) self.caption.onDetach() def setWidget(self, widget): if self.child is not None: self.panel.remove(self.child) if widget is not None: self.panel.setWidget(1, 0, widget) self.child = widget
class RightGrid(DockPanel): def __init__(self, title): DockPanel.__init__(self) self.grid = FlexTable() title = HTML(title) self.add(title, DockPanel.NORTH) self.setCellHorizontalAlignment(title, HasHorizontalAlignment.ALIGN_LEFT) self.add(self.grid, DockPanel.CENTER) self.grid.setBorderWidth("0px") self.grid.setCellSpacing("0px") self.grid.setCellPadding("4px") self.formatCell(0, 0) self.grid.setHTML(0, 0, " ") def clear_items(self): self.index = 0 self.items = {} def set_items(self, items): self.items = items self.index = 0 self.max_rows = 0 self.max_cols = 0 Timer(1, self) def onTimer(self, t): count = 0 while count < 10 and self.index < len(self.items): self._add_items(self.index) self.index += 1 count += 1 if self.index < len(self.items): Timer(1, self) def _add_items(self, i): item = self.items[i] command = item[0] col = item[1] row = item[2] data = item[3] format_row = -1 format_col = -1 if col+1 > self.max_cols: format_col = self.max_cols #self.grid.resizeColumns(col+1) self.max_cols = col+1 if row+1 >= self.max_rows: format_row = self.max_rows #self.grid.resizeRows(row+1) self.max_rows = row+1 if format_row >= 0: for k in range(format_row, self.max_rows): self.formatCell(k, 0) self.formatCell(row, col) cf = self.grid.getCellFormatter() if command == 'data': self.grid.setHTML(row, col, data) elif command == 'cellstyle': data = space_split(data) attr = data[0] val = data[1] cf.setStyleAttr(row, col, attr, val) elif command == 'align': data = space_split(data) vert = data[0] horiz = data[1] if vert != '-': cf.setVerticalAlignment(row, col, vert) if horiz != '-': cf.setHorizontalAlignment(row, col, horiz) elif command == 'cellspan': data = space_split(data) rowspan = data[0] colspan = data[1] if colspan != '-': cf.setColSpan(row, col, colspan) if rowspan != '-': cf.setRowSpan(row, col, rowspan) def formatCell(self, row, col): self.grid.prepareCell(row, col) if col == 0 and row != 0: self.grid.setHTML(row, col, "%d" % row) if row != 0 and col != 0: #self.grid.setHTML(row, col, " ") fmt = "rightpanel-cellformat" if col == 0 and row == 0: fmt = "rightpanel-cellcornerformat" elif row == 0: fmt = "rightpanel-celltitleformat" elif col == 0: fmt = "rightpanel-cellleftformat" self.grid.getCellFormatter().setStyleName(row, col, fmt)
class AlarmWidget(object): weekday_name = { 0: 'Mo', 1: 'Di', 2: 'Mi', 3: 'Do', 4: 'Fr', 5: 'Sa', 6: 'So' } def __init__(self): self.alarms = Alarms(self) self.make_table() self.fill_table() self.status = Label() self.panel = self.make_panel() def make_panel(self): message = Label( 'The configuration has been changed.\n' 'You must apply the changes in order for them to take effect.') DOM.setStyleAttribute(message.getElement(), "whiteSpace", 'pre') msgbox = Grid(1, 2, StyleName='changes') msgbox.setWidget(0, 0, Image('icons/exclam.png')) msgbox.setWidget(0, 1, message) msgbox.getCellFormatter().setStyleName(0, 0, 'changes-image') msgbox.getCellFormatter().setStyleName(0, 1, 'changes-text') button = Button('apply changes') button.addClickListener(self.apply_clicked) self.changes = VerticalPanel() self.changes.setHorizontalAlignment('right') self.changes.setVisible(False) self.changes.add(msgbox) self.changes.add(button) panel = VerticalPanel() panel.setSpacing(10) panel.add(self.table) panel.add(self.status) panel.add(self.changes) return panel def make_table(self): self.table = FlexTable(StyleName='alarms') self.table.setBorderWidth(1) self.make_header() self.make_footer() def make_header(self): headers = [ 'Time', 'Days', 'Duration' ] for col, text in enumerate(headers): self.table.setText(0, col, text) self.table.getCellFormatter().setStyleName(0, col, 'tablecell header') def make_footer(self): self.time = {} self.time['hour'] = ListBox() self.time['hour'].setVisibleItemCount(1) for hour in range(24): self.time['hour'].addItem('%02d' % hour) self.time['hour'].setSelectedIndex(12) self.time['minute'] = ListBox() self.time['minute'].setVisibleItemCount(1) for minute in range(0, 60, 5): self.time['minute'].addItem('%02d' % minute) self.time['minute'].setSelectedIndex(0) time_panel = HorizontalPanel() time_panel.setVerticalAlignment('center') time_panel.add(self.time['hour']) time_panel.add(Label(':')) time_panel.add(self.time['minute']) self.table.setWidget(1, 0, time_panel) weekdays_panel = HorizontalPanel() weekdays_panel.setSpacing(5) self.weekdays = {} for i in range(7): self.weekdays[i] = CheckBox(AlarmWidget.weekday_name[i]) self.weekdays[i].setChecked(i < 6) weekdays_panel.add(self.weekdays[i]) self.table.setWidget(1, 1, weekdays_panel) self.duration = ListBox() self.duration.setVisibleItemCount(1) choices = [ 1, 2, 3, 4, 5, 7, 10, 15, 20, 25, 30, 40, 50, 60 ] for seconds in choices: self.duration.addItem( '%ds' % seconds if seconds < 60 else '%dm' % (seconds / 60), seconds) self.duration.setSelectedIndex(2) self.table.setWidget(1, 2, self.duration) image = Image('icons/plus.png') image.setTitle('add'); image.addClickListener(self.plus_clicked) self.table.setWidget(1, 3, image) for col in range(4): self.table.getCellFormatter().setStyleName(1, col, 'tablecell noborder') def fill_table(self): for idx, alarm in enumerate(self.alarms.get()): self.add(alarm['time'], alarm['weekdays'], alarm['duration']) def add(self, time, weekdays=range(5), duration=3): row = self.table.getRowCount()-1 self.table.insertRow(row) self.table.setText(row, 0, time) weekdays_str = [] for weekday in weekdays: weekdays_str.append(AlarmWidget.weekday_name[weekday]) self.table.setText(row, 1, ', '.join(weekdays_str)) self.table.setText(row, 2, str(duration) + 's') image = Image('icons/x.png') image.setTitle('delete'); image.addClickListener(lambda x: self.x_clicked(row-1)) self.table.setWidget(row, 3, image) for col in range(3): self.table.getCellFormatter().setStyleName(row, col, 'tablecell') self.table.getCellFormatter().setStyleName(row, 3, 'tablecell noborder') def remove(self, idx): if idx >= 0 and idx < self.table.getRowCount()-2: #self.status.setText('removing idx: %d' % idx) self.table.removeRow(idx+1) else: #self.status.setText('tried to remove idx: %d' % idx) pass def plus_clicked(self): self.changes.setVisible(True) getSelectedValue = lambda widget: widget.getValue(widget.getSelectedIndex()) hour = getSelectedValue(self.time['hour']) minute = getSelectedValue(self.time['minute']) time = '%02d:%02d:%02d' % ( hour, minute, 0 ) weekdays = [ i for i in range(7) if self.weekdays[i].isChecked() ] duration = getSelectedValue(self.duration) self.add(time, weekdays, duration) self.alarms.add({'time': time, 'weekdays': weekdays, 'duration': duration}) def x_clicked(self, idx): self.changes.setVisible(True) self.remove(idx) #self.alarms.remove(idx) def apply_clicked(self): self.alarms.save() self.changes.setVisible(False)