def getGridSelectedCells(grid): """ Return the selected cells in the grid as a list of (row, col) pairs. We need to take care of three possibilities: 1. Multiple cells were click-selected (GetSelectedCells) 2. Multiple cells were drag selected (GetSelectionBlock...) 3. A single cell only is selected (CursorRow/Col) """ top_left = grid.GetSelectionBlockTopLeft() if top_left: bottom_right = grid.GetSelectionBlockBottomRight() return [ grid.GetCellValue(x[0], x[1]) for x in corners_to_cells(top_left, bottom_right) ] selection = grid.GetSelectedCells() if not selection: row = grid.GetGridCursorRow() col = grid.GetGridCursorCol() return [grid.GetCellValue(row, col)] return [grid.GetCellValue(x[0], x[1]) for x in selection]
def Draw(self, grid, attr, dc, rect, row, col, isSelected): f = attr.GetFont() text = grid.GetCellValue(row, col) if text == "0": dc.SetTextForeground("#cccccc") else: f.SetWeight(wx.BOLD) dc.SetTextForeground("BLACK") if (row == col): dc.SetBrush(wx.Brush("#f6f6f6", wx.SOLID)) else: dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID)) dc.SetBackgroundMode(wx.SOLID) dc.SetPen(wx.TRANSPARENT_PEN) dc.DrawRectangleRect(rect) dc.SetBackgroundMode(wx.TRANSPARENT) dc.SetFont(f) w, h = dc.GetTextExtent(text) x = rect.x + 1 + (rect.width - w) / 2 y = rect.y + 1 + (rect.height - h) / 2 for ch in text: dc.DrawText(ch, x, y) w, h = dc.GetTextExtent(ch) x = x + w if x > rect.right - 5: break
def Draw(self, grid, attr, drawingContext, rect, row, col, isSelected): drawingContext.SetClippingRegion(rect.x, rect.y, rect.width, rect.height) # Draw the background if isSelected: drawingContext.SetBrush( wx.Brush(wx.SystemSettings_GetColour(wx.SYS_COLOUR_HIGHLIGHT), wx.SOLID)) else: drawingContext.SetBrush( wx.Brush(wx.SystemSettings_GetColour(wx.SYS_COLOUR_WINDOW), wx.SOLID)) try: drawingContext.SetPen(wx.TRANSPARENT_PEN) drawingContext.DrawRectangle(rect.x, rect.y, rect.width, rect.height) finally: drawingContext.SetPen(wx.NullPen) drawingContext.SetBrush(wx.NullBrush) # Draw the appropriate icon try: value = grid.GetCellValue(row, col) if value in self.inspectorRef().icons: icon = self.inspectorRef().icons[value] drawingContext.DrawBitmap(icon, rect.x + rect.width / 2 - 8, rect.y + rect.height / 2 - 8, True) finally: drawingContext.DestroyClippingRegion()
def GetBestSize(self, grid, attr, dc, row, col): """The width will be between values `col size` and `max col size` These can be changed in user preferences. """ text = grid.GetCellValue(row, col) dc.SetFont(attr.GetFont()) w, h = dc.GetTextExtent('00') # use 2 digits for size reference if self.auto_fit: grid.SetRowMinimalAcceptableHeight(h + h / 2) grid.SetColMinimalAcceptableWidth(w + w / 2) w, h = dc.GetTextExtent(text) if self.auto_fit: col_width = min(w, self.max_width) else: col_width = min(w, self.default_width) if self.word_wrap: suggest_width = max(grid.GetColSize(col), col_width) text = self._wordwrap(text, suggest_width, dc, breakLongWords=True, margin=1) w, h = dc.GetMultiLineTextExtent(text) if self.auto_fit: col_width = min(w, col_width) else: col_width = min(w, self.default_width) row_height = h return wx.Size(col_width, row_height)
def GetBestSize(self, grid, attr, dc, row, col): """The width will be between values `col size` and `max col size` These can be changed in user preferences. """ text = grid.GetCellValue(row, col) _font = attr.GetFont() dc.SetFont(_font) if len(text) == 0: return dc.GetTextExtent(" ") # self.default_width w, h = dc.GetTextExtent(text) if self.auto_fit: col_width = min(w, self.max_width) else: col_width = min(w, self.default_width) row_height = h if self.word_wrap: suggest_width = max(grid.GetColSize(col), col_width) text = self._wordwrap(text, suggest_width, dc, breakLongWords=False) w, h = dc.GetMultiLineTextExtent(text) row_height = h if self.auto_fit: col_width = min(w, col_width) else: col_width = min(w, self.default_width) # do not shrink col size (subtract col margin which is 10 pixels ) col_width = max(grid.GetColSize(col) - 10, col_width) return wx.Size(col_width, row_height)
def Draw(self, grid, attr, dc, rect, row, col, is_selected): """Draws the text and the combobox icon""" render = wx.RendererNative.Get() # clear the background dc.SetBackgroundMode(wx.SOLID) if is_selected: dc.SetBrush(wx.Brush(wx.BLUE, wx.SOLID)) dc.SetPen(wx.Pen(wx.BLUE, 1, wx.SOLID)) else: dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID)) dc.SetPen(wx.Pen(wx.WHITE, 1, wx.SOLID)) dc.DrawRectangleRect(rect) cb_lbl = grid.GetCellValue(row, col) string_x = rect.x + 2 string_y = rect.y + 2 dc.DrawText(cb_lbl, string_x, string_y) button_x = rect.x + rect.width - self.iconwidth button_y = rect.y button_width = self.iconwidth button_height = rect.height button_size = button_x, button_y, button_width, button_height render.DrawComboBoxDropButton(grid, dc, button_size, wx.CONTROL_CURRENT)
def BeginEdit(self, row, col, grid): """Must Override.""" self.__startValue = grid.GetCellValue(row, col)[0] self.__textCtrl.SetValue(self.__startValue) self.__textCtrl.SetInsertionPointEnd() self.__textCtrl.SetFocus() self.__textCtrl.SetSelection(0, self.__textCtrl.GetLastPosition())
def Draw(self, grid, attr, dc, rect, row, col, isSelected): FTextRenderer.Draw(self, grid, attr, dc, rect, row, col, isSelected) value = grid.GetCellValue(row, col) if (value != None) and (value != ""): self.RenderWrappedText(grid, attr, dc, rect, row, col, isSelected, value[0])
def Draw(self, grid, attr, dc, rect, row, col, isSelected): # pylint: disable=too-many-arguments, arguments-differ # Draw background dc.SetBackgroundMode(wx.SOLID) if isSelected: dc.SetBrush( wx.Brush( wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT), wx.BRUSHSTYLE_SOLID ) ) else: dc.SetBrush(wx.Brush(attr.GetBackgroundColour(), wx.BRUSHSTYLE_SOLID)) dc.SetPen(wx.TRANSPARENT_PEN) dc.DrawRectangle(rect) # Draw text dc.SetBackgroundMode(wx.TRANSPARENT) dc.SetFont(attr.GetFont()) text = grid.GetCellValue(row, col) if grid.space_as_dot: text = text.replace(u' ', u'·') if isSelected: dc.SetTextForeground(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT)) else: dc.SetTextForeground(attr.GetTextColour()) h_align = grid.GetCellAlignment(row, col)[0] v_align = grid.GetCellAlignment(row, col)[1] text_rect = wx.Rect(rect.GetX(), rect.GetY(), rect.GetWidth(), rect.GetHeight()) # Margins 2px if text_rect.GetWidth() > 4 and text_rect.GetHeight() > 4: text_rect.SetX(rect.GetX() + 2) text_rect.SetY(rect.GetY() + 2) text_rect.SetWidth(rect.GetWidth() - 4) text_rect.SetHeight(rect.GetHeight() - 4) dc.DrawLabel(text, text_rect, h_align|v_align)
def GetBestSize(self, grid, attr, dc, row, col): # pylint: disable=too-many-arguments, arguments-differ text = grid.GetCellValue(row, col) if grid.space_as_dot: text = text.replace(u' ', u'·') dc.SetFont(attr.GetFont()) best_size = dc.GetTextExtent(text) return best_size
def Draw(self, grid, attr, dc, rect, row, col, _isSelected): dc.SetBrush(wx.Brush(attr.GetBackgroundColour())) dc.DrawRectangleRect(rect) if grid.GetCellValue(row, col) == "1": dc.SetBrush(wx.Brush(attr.GetTextColour())) dc.DrawCircle(rect.x + (rect.width / 2), rect.y + (rect.height / 2), rect.height / 4)
def Draw(self, grid, attr, dc, rect, row, col, isSelected): self.selectBmpIndex = grid.GetCellValue(row, col) try: selectedBmp = self.bmpList[int(self.selectBmpIndex)] except Exception as e: logger.error(e , exc_info=True) image = wx.MemoryDC() image.SelectObject(selectedBmp) # clear the background dc.SetBackgroundMode(wx.SOLID) if isSelected: # dc.SetBrush(wx.Brush(wx.BLUE, wx.SOLID)) dc.SetPen(wx.Pen(wx.BLUE, 1, wx.SOLID)) else: dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID)) dc.SetPen(wx.Pen(wx.WHITE, 1, wx.SOLID)) dc.DrawRectangle(rect) # copy the image but only to the size of the grid cell width, height = selectedBmp.GetWidth(), selectedBmp.GetHeight() if width > rect.width - 2: width = rect.width - 2 if height > rect.height - 2: height = rect.height - 2 dc.Blit(rect.x + 1, rect.y + 1, width, height, image, 0, 0, wx.COPY, True)
def Draw(self, grid, attr, dc, rect, row, col, isSelected): #print 'grid', grid #print 'attr', attr #print 'dc', dc #print 'rect', rect #print 'row', row #print 'col', col #print 'isSelected', isSelected #dc.SetPen(wx.TRANSPARENT_PEN) # do it like this for filling in background: dc.SetBackgroundMode(wx.SOLID) dc.SetBrush(wx.Brush(self.color, wx.BDIAGONAL_HATCH)) # or do it like this for highlighting the cell: #dc.SetPen(wx.Pen(self.color, 5, wx.SOLID)) dc.DrawRectangleRect(rect) dc.SetBackgroundMode(wx.TRANSPARENT) dc.SetFont(attr.GetFont()) text = grid.GetCellValue(row, col) #colors = ["RED", "WHITE", "SKY BLUE"] x = rect.x + 1 y = rect.y + 1 for ch in text: dc.SetTextForeground("BLACK") dc.DrawText(ch, x, y) w, h = dc.GetTextExtent(ch) x = x + w if x > rect.right - 5: break
def BeginEdit(self, row, col, grid): self._counter = 0 self._tc.SetSize((-1, self._height)) self._tc.set_row(row) self._original_value = grid.GetCellValue(row, col) self._tc.SetValue(self._original_value) self._grid = grid self._tc.SetInsertionPointEnd()
def Draw(self, grid, attr, dc, rect, row, col, isSelected): """ Takes care of drawing everything in the cell; aligns the text """ text = grid.GetCellValue(row, col) (align, text) = self._getvalue(row, col) if isSelected: bg = grid.GetSelectionBackground() else: bg = ["white", (240, 240, 240)][row%2] dc.SetTextBackground(bg) dc.SetBrush(wx.Brush(bg, wx.SOLID)) dc.SetPen(wx.TRANSPARENT_PEN) dc.SetFont(attr.GetFont()) dc.DrawRectangleRect(rect) dc.SetClippingRect(rect) # Format the text if align == -1: # left alignment (width, height) = dc.GetTextExtent(str(text)) x = rect[0]+1 y = rect[1]+0.5*(rect[3]-height) for (style, part) in text: if isSelected: fg = grid.GetSelectionForeground() else: fg = self.colormap[style.fg] dc.SetTextForeground(fg) (w, h) = dc.GetTextExtent(part) dc.DrawText(part, x, y) x += w elif align == 0: # center alignment (width, height) = dc.GetTextExtent(str(text)) x = rect[0]+0.5*(rect[2]-width) y = rect[1]+0.5*(rect[3]-height) for (style, part) in text: if isSelected: fg = grid.GetSelectionForeground() else: fg = self.colormap[style.fg] dc.SetTextForeground(fg) (w, h) = dc.GetTextExtent(part) dc.DrawText(part, x, y) x += w else: # right alignment (width, height) = dc.GetTextExtent(str(text)) x = rect[0]+rect[2]-1 y = rect[1]+0.5*(rect[3]-height) for (style, part) in reversed(text): (w, h) = dc.GetTextExtent(part) x -= w if isSelected: fg = grid.GetSelectionForeground() else: fg = self.colormap[style.fg] dc.SetTextForeground(fg) dc.DrawText(part, x, y) dc.DestroyClippingRegion()
def getFormulaEvaluatedValue(currentFormula,variableObj,grid,gridIndex): formulaResult=Parser().ast(currentFormula)[1].compile() elementInFormulaList = [] #print(formulaResult.inputs) for cellAddress in formulaResult.inputs: row,col = addressExcelToWxpythonFormat(cellAddress) elementInFormulaList.append(getIntTypeAddress(grid.GetCellValue(row,col))) #print("hi",getIntTypeAddress(grid.GetCellValue(row,col))) return int(round((float(formulaResult(*elementInFormulaList))),0))
def BeginEdit(self, row, col, grid): self._tc.SetSize((-1, self._height)) self._tc.SetBackgroundColour(context.POPUP_BACKGROUND) # DEBUG: We are now in Edit mode self._tc.set_row(row) self._original_value = grid.GetCellValue(row, col) self._tc.SetValue(self._original_value) self._tc.SetSelection(0, self._tc.GetLastPosition()) self._tc.SetFocus() self._grid = grid
def grid_to_df(self, grid, index): col = [] for r in range(len(index)): row = [] for c in range(5): row.append(grid.GetCellValue(r, c)) col.append(row) df = pd.DataFrame(col, index=index, columns=self.dye_li) return df
def BeginEdit(self, row, col, grid): self._counter = 0 self._tc.SetSize((-1, self._height)) self._tc.set_row(row) self._original_value = grid.GetCellValue(row, col) self._grid = grid self._tc.SetInsertionPointEnd() self._tc.SetFocus() # For this example, select the text # DEBUG nov_2017 self._tc.SetSelection(0, self._tc.GetLastPosition())
def _invalid_fldname(row, grid): "Return boolean and string message" other_fldnames = [] for i in range(grid.GetNumberRows()): if i == row: continue other_fldnames.append(grid.GetCellValue(row=i, col=0)) field_name = grid.GetCellValue(row=row, col=0) if field_name.strip() == '': return False, '' valid, err = dbe_sqlite.valid_fldname(field_name) if not valid: msg = _('Field names can only contain letters, numbers, and ' 'underscores.\nOrig error: %s') % err return True, msg if field_name in other_fldnames: msg = _('%s has already been used as a field name') % field_name return True, msg return False, ''
def __init__(self, parent, grid, format=[], total_col=None, total_row=None, rowLabels=True, colLabels=True): if total_row is None: total_row = grid.GetNumberRows() if total_col is None: total_col = grid.GetNumberCols() self.total_row = total_row self.total_col = total_col self.grid = grid self.rowLabels = rowLabels self.colLabels = colLabels data = [] for row in range(total_row): row_val = [] if rowLabels: row_val.append(grid.GetRowLabelValue(row)) for col in range(total_col): try: row_val.append(grid.GetCellValueAsString(row, col)) except: row_val.append(grid.GetCellValue(row, col)) data.append(row_val) if colLabels: label = [""] if rowLabels else [] for col in range(total_col): value = grid.GetColLabelValue(col) label.append(value) d = float(grid.GetColSize(0)) if format == []: if rowLabels: format.append(grid.GetRowLabelSize()) for col in range(total_col): col_size = grid.GetColSize(col) #print("Column size:", col,'\t',col_size) format.append(col_size) self.table = PrintTable(parent, rowLabels, colLabels) if colLabels: self.table.label = label self.table.cell_left_margin = 0.0 self.table.cell_right_margin = 0.0 self.table.set_column = format self.table.data = data
def GetBestSize(self, grid, attr, dc, row, col): """Customisation Point: Determine the appropriate (best) size for the control, return as wxSize Note: You _must_ return a wxSize object. Returning a two-value-tuple won't raise an error, but the value won't be respected by wxPython. """ x, y = wx.lib.fancytext.GetExtent(grid.GetCellValue(row, col), dc) # note that the two-tuple returned by GetTextExtent won't work, # need to give a wxSize object back! return wx.Size(x, y)
def get_state(self, grid, row, col): """Get a cell's press-state grid - the grid control row,col - the row and column of the cell """ value = grid.GetCellValue(row, col) values = value.split(':') if len(values) < 2: return None return values[1]
def BeginEdit(self, row, col, grid): self._counter = 0 self._tc.SetSize((-1, self._height)) self._tc.SetBackgroundColour( context.POPUP_BACKGROUND) # DEBUG: We are now in Edit mode self._tc.set_row(row) self._original_value = grid.GetCellValue(row, col) self._tc.SetValue(self._original_value) self._grid = grid self._tc.SetInsertionPointEnd() if not IS_WINDOWS: self._tc.SetFocus() # On Win 10 this breaks cell text selection
def get_bitmap(self, grid, attr, dc, row, col): """Get a cell's bitmap grid - the parent wx.grid attr - an instance of wx.grid.GriddCellAttr which provides rendering info dc - the device context to be used for printing row,col - the coordinates of the cell to be rendered """ value = grid.GetCellValue(row, col) key = value.split(':')[0] if self.__bitmap_dictionary.has_key(key): bitmap = self.__bitmap_dictionary[key] return bitmap return None
def _invalid_fldtype(row, grid): """ Return boolean and string message References field type label and not key because label is what is in grid. """ field_type = grid.GetCellValue(row=row, col=1) if field_type.strip() == '': return False, '' if field_type not in [mg.FLDTYPE_NUMERIC_LBL, mg.FLDTYPE_STRING_LBL, mg.FLDTYPE_DATE_LBL]: msg = _('%s is not a valid field type') % field_type return True, msg return False, ''
def Draw(self, grid, attr, dc, rect, row, col, _isSelected): text = grid.GetCellValue(row, col) hAlign, vAlign = attr.GetAlignment() bg = wx.WHITE dc.SetTextBackground(bg) dc.SetTextForeground(attr.GetTextColour()) font = attr.GetFont() font.SetPointSize(font.GetPointSize() + 3) dc.SetFont(font) dc.SetBrush(wx.Brush(bg, wx.SOLID)) dc.SetPen(wx.TRANSPARENT_PEN) dc.DrawRectangleRect(rect) grid.DrawTextRectangle(dc, len(text) * '\N{BULLET}', rect, hAlign, vAlign)
def OnClose(self): r = wx.MessageBox("保存修改?", "确认", wx.CANCEL | wx.OK | wx.ICON_QUESTION) if r == wx.OK: reviseData = [] for i in range(len(data)): reviseData.append( (int(grid.GetCellValue(i, 0)), grid.GetCellValue(i, 1), grid.GetCellValue(i, 2))) # 与原数据比较是否有更改 subData = [] for x in reviseData: flag = 1 for y in data: if x[1] == y[1] and x[2] == y[2]: flag = 0 break if (flag == 1): subData.append(x) db.updateWord(subData) that.Show(False) else: that.Show(False)
def Draw(self, grid, attr, dc, rect, row, col, isSelected): """ Отрисовка. """ sx, st = grid.GetRowState(row) img = None if st is not None and st < len(self._images): img = self._images[st] if img: img_dc = wx.MemoryDC() img_dc.SelectObject(img) # Очистка фона dc.SetBackgroundMode(wx.SOLID) if isSelected: dc.SetBrush(wx.Brush(SEL_BGR_CLR, wx.SOLID)) dc.SetPen(wx.Pen(SEL_BGR_CLR, 1, wx.SOLID)) else: dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID)) dc.SetPen(wx.Pen(wx.WHITE, 1, wx.SOLID)) dc.DrawRectangleRect(rect) # copy the image but only to the size of the grid cell if img: width, height = img.GetWidth(), img.GetHeight() if width > rect.GetWidth() - 2: width = rect.GetWidth() - 2 if height > rect.GetHeight() - 2: height = rect.GetHeight() - 2 dc.Blit(rect.GetX() + 1 + sx * img.GetWidth(), rect.GetY() + 1, width, height, img_dc, self._sx, self._sy, wx.COPY, True) else: width, height = 0, 0 hAlign, vAlign = grid.GetCellAlignment(row, col) text = grid.GetCellValue(row, col) if text: dc.SetPen(wx.TRANSPARENT_PEN) if hAlign < width: rect.SetX(rect.GetX() + width) rect.SetWidth(rect.GetWidth() - width) if sx: rect.SetX(rect.GetX() + sx * width) rect.SetWidth(rect.GetWidth() - sx * width) self.DrawText(grid, dc, rect, text, hAlign, vAlign)
def EndEdit(self, row, col, grid): """Must Override.""" newValue = self.__textCtrl.GetValue() if (self.__startValue != newValue): cellValue = grid.GetCellValue(row, col) test = cellValue[1] execution = cellValue[2] if (execution == None): test.SetDefaultComments(newValue) else: execution.SetComments(newValue) grid.SetCellValue(row, col, (newValue, cellValue[1], cellValue[2])) return True return False