Robot Framework Integrated Development Environment (RIDE)
cellrenderer.py
Go to the documentation of this file.
1 # Copyright 2019- Robot Framework Foundation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 
15 import wx.grid
16 
17 
23 
24  def __init__(self, default_width, max_width, auto_fit, word_wrap=True):
25  wx.grid.GridCellRenderer.__init__(self)
26  self.default_widthdefault_width = default_width
27  self.max_widthmax_width = max_width
28  self.auto_fitauto_fit = auto_fit
29  self.word_wrapword_wrap = word_wrap
30 
31 
34  def _wordwrap(self, text, width, dc, breakLongWords=True, margin=0):
35  wrapped_lines = []
36  text = text.split('\n')
37  for line in text:
38  pte = dc.GetPartialTextExtents(line)
39  wid = (width - (2 * margin + 1) * dc.GetTextExtent(' ')[0])
40  idx = 0
41  start = 0
42  startIdx = 0
43  spcIdx = -1
44  while idx < len(line):
45  # remember the last seen space
46  if line[idx] == ' ':
47  spcIdx = idx
48 
49  # have we reached the max width?
50  if pte[idx] - start > wid and (spcIdx != -1 or breakLongWords):
51  if spcIdx != -1:
52  idx = min(spcIdx + 1, len(pte) - 1)
53  wrapped_lines.append(' ' * margin + line[startIdx: idx] + ' ' * margin)
54  start = pte[idx]
55  startIdx = idx
56  spcIdx = -1
57 
58  idx += 1
59 
60  wrapped_lines.append(' ' * margin + line[startIdx: idx] + ' ' * margin)
61 
62  return '\n'.join(wrapped_lines)
63 
64  def Draw(self, grid, attr, dc, rect, row, col, isSelected):
65  text = grid.GetCellValue(row, col)
66  dc.SetFont(attr.GetFont())
67  suggest_width = grid.GetColSize(col)
68  text = self._wordwrap_wordwrap(text, suggest_width, dc, breakLongWords=False)
69  hAlign, vAlign = attr.GetAlignment()
70  if isSelected:
71  bg = grid.GetSelectionBackground()
72  fg = grid.GetSelectionForeground()
73  else:
74  bg = attr.GetBackgroundColour()
75  fg = attr.GetTextColour()
76  dc.SetTextBackground(bg)
77  dc.SetTextForeground(fg)
78  dc.SetBrush(wx.Brush(bg, wx.SOLID))
79  dc.SetPen(wx.TRANSPARENT_PEN)
80  dc.DrawRectangle(rect)
81  grid.DrawTextRectangle(dc, text, rect, hAlign, vAlign)
82 
83 
86  def GetBestSize(self, grid, attr, dc, row, col):
87  text = grid.GetCellValue(row, col)
88  dc.SetFont(attr.GetFont())
89 
90  w, h = dc.GetTextExtent('00') # use 2 digits for size reference
91  if self.auto_fitauto_fit:
92  grid.SetRowMinimalAcceptableHeight(int(h+h/2))
93  grid.SetColMinimalAcceptableWidth(int(w+w/2))
94 
95  w, h = dc.GetTextExtent(text)
96  if self.auto_fitauto_fit:
97  col_width = min(w, self.max_widthmax_width)
98  else:
99  col_width = min(w, self.default_widthdefault_width)
100 
101  if self.word_wrapword_wrap:
102  suggest_width = max(grid.GetColSize(col), col_width)
103  text = self._wordwrap_wordwrap(text, suggest_width, dc, breakLongWords=False)
104  w, h = dc.GetMultiLineTextExtent(text)
105  if self.auto_fitauto_fit:
106  col_width = min(w, col_width)
107  else:
108  col_width = min(w, self.default_widthdefault_width)
109  row_height = h
110  return wx.Size(col_width, row_height)
111 
112 
113  def Clone(self): # real signature unknown; restored from __doc__
114  return CellRenderer
GridCellAutoWrapStringRenderer()
Definition: cellrenderer.py:22
def _wordwrap(self, text, width, dc, breakLongWords=True, margin=0)
modification of original wordwrap function without extra space
Definition: cellrenderer.py:34
def Draw(self, grid, attr, dc, rect, row, col, isSelected)
Definition: cellrenderer.py:64
def __init__(self, default_width, max_width, auto_fit, word_wrap=True)
Definition: cellrenderer.py:24
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.
Definition: cellrenderer.py:86
def Clone(self)
Clone(self) -> GridCellRenderer.