Coverage for src/robotide/editor/cellrenderer.py: 9%

71 statements  

« prev     ^ index     » next       coverage.py v7.8.0, created at 2025-05-06 10:40 +0100

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 

15import wx.grid 1ab

16 

17 

18class CellRenderer(wx.grid.GridCellRenderer): 1ab

19 """ 

20 GridCellAutoWrapStringRenderer() 

21 

22 This class may be used to format string data in a cell. 

23 """ 

24 

25 def __init__(self, default_width, max_width, auto_fit, word_wrap=True): 1ab

26 wx.grid.GridCellRenderer.__init__(self) 

27 self.default_width = default_width 

28 self.max_width = max_width 

29 self.auto_fit = auto_fit 

30 self.word_wrap = word_wrap 

31 

32 @staticmethod 1ab

33 def _wordwrap(text, width, dc, break_long_words=True, margin=0): 1ab

34 """ modification of original wordwrap function without extra space """ 

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 start_idx = 0 

43 spc_idx = -1 

44 while idx < len(line): 

45 # remember the last seen space 

46 if line[idx] == ' ': 

47 spc_idx = idx 

48 

49 # have we reached the max width? 

50 if pte[idx] - start > wid and (spc_idx != -1 or break_long_words): 

51 if spc_idx != -1: 

52 idx = min(spc_idx + 1, len(pte) - 1) 

53 wrapped_lines.append(' ' * margin + line[start_idx: idx] + ' ' * margin) 

54 start = pte[idx] 

55 start_idx = idx 

56 spc_idx = -1 

57 

58 idx += 1 

59 

60 wrapped_lines.append(' ' * margin + line[start_idx: idx] + ' ' * margin) 

61 

62 return '\n'.join(wrapped_lines) 

63 

64 def Draw(self, grid, attr, dc, rect, row, col, is_selected): 1ab

65 text = grid.GetCellValue(row, col) 

66 dc.SetFont(attr.GetFont()) 

67 suggest_width = grid.GetColSize(col) 

68 text = self._wordwrap(text, suggest_width, dc, break_long_words=False) 

69 h_align, v_align = attr.GetAlignment() 

70 if is_selected: 

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, h_align, v_align) 

82 

83 def GetBestSize(self, grid, attr, dc, row, col): 1ab

84 """The width will be between values `col size` and `max col size` 

85 These can be changed in user preferences. 

86 """ 

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_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_fit: 

97 col_width = min(w, self.max_width) 

98 else: 

99 col_width = min(w, self.default_width) 

100 

101 if self.word_wrap: 

102 suggest_width = max(grid.GetColSize(col), col_width) 

103 text = self._wordwrap(text, suggest_width, dc, break_long_words=False) 

104 w, h = dc.GetMultiLineTextExtent(text) 

105 if self.auto_fit: 

106 col_width = min(w, col_width) 

107 else: 

108 col_width = min(w, self.default_width) 

109 row_height = h 

110 return wx.Size(col_width, row_height) 

111 

112 def Clone(self): # real signature unknown; restored from __doc__ 1ab

113 """ Clone(self) -> GridCellRenderer """ 

114 return CellRenderer