Coverage for src/robotide/editor/gridcolorizer.py: 69%
75 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
« prev ^ index » next coverage.py v7.8.0, created at 2025-05-06 10:40 +0100
1# Copyright 2008-2015 Nokia Networks
2# Copyright 2016- Robot Framework Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15import wx 1bc
17from ..controller.cellinfo import CellType 1bc
20# this import fails in HUDSON
21# from wxPython._gdi import wxFONTWEIGHT_BOLD, wxFONTWEIGHT_NORMAL
22# wxFONTWEIGHT_BOLD = 92
23# wxFONTWEIGHT_NORMAL = 90
24# DEBUG using wx.FONTWEIGHT_BOLD, wx.FONTWEIGHT_NORMAL
27class Colorizer(object): 1bc
29 def __init__(self, grid, controller): 1bc
30 self._grid = grid 1defgha
31 self._controller = controller 1defgha
32 self._colors = ColorizationSettings(grid.settings) 1defgha
33 self._current_task_id = 0 1defgha
34 self._timer = None 1defgha
36 def close(self): 1bc
37 self._grid = None
39 def colorize(self, selection_content): 1bc
40 self._current_task_id += 1
41 if self._timer is None:
42 self._timer = wx.CallLater(1, self._coloring_task, self._current_task_id, selection_content)
43 else:
44 self._timer.Restart(50, self._current_task_id, selection_content)
46 def _coloring_task(self, task_index, selection_content, row=0, col=0): 1bc
47 if task_index != self._current_task_id or self._grid is None:
48 return
49 if row >= self._grid.NumberRows:
50 self._grid.ForceRefresh()
51 elif col < self._grid.NumberCols:
52 self._colorize_cell(row, col, selection_content)
53 wx.CallAfter(self._coloring_task, task_index, selection_content, row, col+1)
54 else:
55 self._coloring_task(task_index, selection_content, row+1, 0)
57 def _colorize_cell(self, row, col, selection_content): 1bc
58 cell_info = self._controller.get_cell_info(row, col) 1a
59 if cell_info is None: 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true1a
60 self._set_default_colors(row, col)
61 return
62 self._grid.SetCellTextColour(row, col, self._get_text_color(cell_info)) 1a
63 self._grid.SetCellBackgroundColour(row, col, self._get_background_color(cell_info, selection_content)) 1a
64 self._grid.SetCellFont(row, col, self._get_cell_font(row, col, cell_info)) 1a
66 def _set_default_colors(self, row, col): 1bc
67 self._grid.SetCellTextColour(row, col, self._colors.DEFAULT_TEXT)
68 self._grid.SetCellBackgroundColour(row, col, self._colors.DEFAULT_BACKGROUND)
70 def _get_text_color(self, cell_info): 1bc
71 return self._colors.get_text_color(cell_info.content_type) 1defgha
73 def _get_background_color(self, cell_info, selection_content): 1bc
74 if cell_info.matches(selection_content): 1defgha
75 return self._colors.get_highlight_color() 1defgha
76 if cell_info.has_error(): 1a
77 return self._colors.get_error_color() 1a
78 return self._colors.get_background_color(cell_info.cell_type) 1a
80 def _get_cell_font(self, row, col, cell_info): 1bc
81 font = self._grid.GetCellFont(row, col) 1a
82 font.SetWeight(self._get_weight(cell_info)) 1a
83 return font 1a
85 @staticmethod 1bc
86 def _get_weight(cell_info): 1bc
87 if cell_info.cell_type == CellType.KEYWORD: 1a
88 return wx.FONTWEIGHT_BOLD 1a
89 return wx.FONTWEIGHT_NORMAL 1a
92class ColorizationSettings(object): 1bc
94 DEFAULT_TEXT = '' # Colour('black') # Colour(7, 0, 70) # 'black' 1bc
95 DEFAULT_BACKGROUND = '' # 'light grey' # Colour('light grey') # Colour(200, 222, 40) # 'white' 1bc
97 def __init__(self, settings=None): 1bc
98 self._settings = settings 1defgha
100 def get_background_color(self, elem_type): 1bc
101 if not self._settings: 101 ↛ 102line 101 didn't jump to line 102 because the condition on line 101 was never true1defgha
102 return self.DEFAULT_BACKGROUND
103 return self._get(f'background {elem_type}') 1defgha
105 def get_text_color(self, elem_type): 1bc
106 if not self._settings: 106 ↛ 107line 106 didn't jump to line 107 because the condition on line 106 was never true1defgha
107 return self.DEFAULT_TEXT
108 return self._get(f'text {elem_type}') 1defgha
110 def get_highlight_color(self): 1bc
111 return self.get_background_color('highlight') 1defgha
113 def get_error_color(self): 1bc
114 return self.get_background_color('error') 1a
116 def _get(self, name): 1bc
117 color_setting = name.lower().replace('_', ' ') 1defgha
118 if color_setting in self._settings: 1defgha
119 return self._settings[color_setting] 1defgha
120 else:
121 return CellType.UNKNOWN 1a