Skip to content

highlight of table cells in front end. #516

Open

Description

@WAvdBeek

I wrote some code on the backend to change color of a tab item.
I copy paste the original table & table item and added the code to it via mouseover & mouseout function.
i think this also should be possible on the front end.
any idea how?

here is the code:

from remi import App, gui, start


class HighLightTableItem(gui.TableItem):
    ''' Hightlight the item when hovered over
    '''

    def __init__(self, *arg, **kwargs):
        """ init, like before """
        super(HighLightTableItem, self).__init__(*arg, **kwargs)
        self.onmouseout.connect(self.mouseout)
        self.onmouseover.connect(self.mouseover)
        self.hover_over = False

    def mouseover(self, _emitter):
        """ mouse over """
        self.hover_over = True
        #print("item over")
        # css background: rgba(4, 90, 188, 0.26);
        #outline: 1px solid red;
        self.style['background'] = "rgba(4, 90, 188, 0.26)"
        #self.style['outline'] = "1px solid red"

    def mouseout(self, _emitter):
        """ mouse out """
        self.hover_over = False
        #print("item out")
        self.style['background'] = "white"
        #self.style['outline'] = "white"
        #outline: 1px solid red;

class HighLightTableWidget(gui.Table):
    ''' A subclass of gui.Table that has the feature
        that the last selected row is highlighted
        and that allows for multi_row selection.
    '''
    @property
    @gui.editor_attribute_decorator("WidgetSpecific", '''Table colum count.''', int, {'possible_values': '', 'min': 0, 'max': 100, 'default': 1, 'step': 1})
    def column_count(self): return self.__column_count
    @column_count.setter
    def column_count(self, value): self.set_column_count(value)
        
    @property
    @gui.editor_attribute_decorator("WidgetSpecific", '''Table row count.''', int, {'possible_values': '', 'min': 0, 'max': 100, 'default': 1, 'step': 1})
    def row_count(self): return len(self.children)
    @row_count.setter
    def row_count(self, value): self.set_row_count(value)

    @property
    @gui.editor_attribute_decorator("WidgetSpecific", '''Table use title.''', bool, {})
    def use_title(self): return self.__use_title
    @use_title.setter
    def use_title(self, value): self.set_use_title(value)

    def __init__(self, n_rows=2, n_columns=2, use_title=True, editable=False, use_selection=False, *arg, **kwargs):
        self.__column_count = 0
        self.__use_title = use_title 
        super(HighLightTableWidget, self).__init__(*arg, **kwargs)
        self._editable = editable
        self.set_use_title(use_title)
        self.set_column_count(n_columns)
        self.set_row_count(n_rows)
        self.css_display = 'table'
        # the additions below
        self.attributes['tabindex'] = '1'
        self.onkeydown.connect(self.keydown)
        self.onkeyup.connect(self.keyup)
        #self.onmouseout.connect(self.mouseout)
        #self.onmouseover.connect(self.mouseover)
        self.multi_selection_enabled = False
        self.hover_over = False
        self.selected_row_list = []
        self.use_selection = use_selection

    def set_use_title(self, use_title):
        """Returns the TableItem instance at row, column cordinates

        Args:
            use_title (bool): enable title bar.
        """
        self.__use_title = use_title
        self._update_first_row()

    def _update_first_row(self):
        cl = HighLightTableItem
        if self.__use_title:
            cl = gui.TableTitle
        if len(self.children) > 0:
            for c_key in self.children['0'].children.keys():
                instance = cl(self.children['0'].children[c_key].get_text())
                self.children['0'].append(instance, c_key)
                # here the cells of the first row are overwritten
                # and aren't appended by the standard Table.append
                # method. We have to restore de standard on_click
                #internal listener in order to make it working
                # the Table.on_table_row_click functionality
                instance.onclick.connect(self.children['0'].on_row_item_click)

    def item_at(self, row, column):
        """Returns the TableItem instance at row, column cordinates

        Args:
            row (int): zero based index
            column (int): zero based index
        """
        return self.children[str(row)].children[str(column)]

    def item_coords(self, table_item):
        """Returns table_item's (row, column) cordinates.
        Returns None in case of item not found.

        Args:
            table_item (TableItem): an item instance
        """
        for row_key in self.children.keys():
            for item_key in self.children[row_key].children.keys():
                if self.children[row_key].children[item_key] == table_item:
                    return (int(row_key), int(item_key))
        return None

    def set_row_count(self, count):
        """Sets the table row count.

        Args:
            count (int): number of rows