(self, painter)
paints selection rects
paints selection rects
paints selection rects
def paint_selection(self, painter):
        """ paints selection rects
        """
        if self.selection.start == self.selection.end:
            return

        pen_color = QColor(self._ctrl_colors['selection_bg'])
        brush_color = QColor(self._ctrl_colors['selection_bg'])
        brush_color.setAlpha(100)

        start_x_hex, start_y_hex = self.index_to_hexcol(self.selection.start)
        end_x_hex, end_y_hex = self.index_to_hexcol(self.selection.end)
        start_x_ascii, start_y_ascii = self.index_to_asciicol(
            self.selection.start)
        end_x_ascii, end_y_ascii = self.index_to_asciicol(self.selection.end)

        # single line selection
        if start_y_hex == end_y_hex:
            # select color if hexsel then with background or outline only
            if self.caret.mode == 'hex':
                painter.setPen(pen_color)
                painter.setBrush(brush_color)
            else:
                painter.setPen(pen_color)
                painter.setBrush(Qt.NoBrush)

            # draw selection in hexcolumn
            painter.drawRect(
                start_x_hex
                - (self._char_width / 2),  # offset by half charwidth
                start_y_hex - self._base_line - 1,  # baseline offset
                end_x_hex - start_x_hex,
                self._char_height + 1)

            # switch style
            if self.caret.mode == 'hex':
                painter.setPen(pen_color)
                painter.setBrush(Qt.NoBrush)
            else:
                painter.setPen(pen_color)
                painter.setBrush(brush_color)

            # draw selection in asciicolumn
            painter.drawRect(
                start_x_ascii,  # offset by half charwidth
                start_y_ascii - self._base_line - 1,  # baseline offset
                end_x_ascii - start_x_ascii,
                self._char_height + 1)

        elif self.selection.end - self.selection.start <= self.bytes_per_line:
            # selection over two lines but not joining draw 2 rects
            if self.caret.mode == 'hex':
                painter.setPen(pen_color)
                painter.setBrush(brush_color)
            else:
                painter.setPen(pen_color)
                painter.setBrush(Qt.NoBrush)

            # draw selection in hexcolumn
            painter.drawRect(
                start_x_hex
                - (self._char_width / 2),  # offset by half charwidth
                start_y_hex - self._base_line - 1,  # baseline offset
                ((self._hex_start + self._hex_width) - start_x_hex)
                + self._char_width,
                self._char_height + 2)
            if self.selection.end % self.bytes_per_line != 0:
                painter.drawRect(
                    self._hex_start
                    - (self._char_width / 2),  # offset by half charwidth
                    end_y_hex - self._base_line - 1,  # baseline offset
                    end_x_hex - self._hex_start,
                    self._char_height + 2)

            if self.caret.mode == 'hex':
                painter.setPen(pen_color)
                painter.setBrush(Qt.NoBrush)
            else:
                painter.setPen(pen_color)
                painter.setBrush(brush_color)

            # draw selection in hexcolumn
            painter.drawRect(
                start_x_ascii,  # offset by half charwidth
                start_y_ascii - self._base_line - 1,  # baseline offset
                ((self._ascii_start + self._ascii_width) - start_x_ascii),
                self._char_height + 2)
            if self.selection.end % self.bytes_per_line != 0:
                painter.drawRect(
                    self._ascii_start,  # offset by half charwidth
                    end_y_ascii - self._base_line - 1,  # baseline offset
                    end_x_ascii - self._ascii_start,
                    self._char_height + 2)
        else:
            # multiple lines selection
            painter.setPen(pen_color)
            painter.setBrush(brush_color)
            polygon = QPolygon()
            polygon.append(
                QPoint(start_x_hex - (self._char_width / 2),
                       start_y_hex + self._char_height - self._base_line + 1))
            polygon.append(
                QPoint(start_x_hex - (self._char_width / 2),
                       start_y_hex - self._base_line - 1))
            polygon.append(
                QPoint(
                    self._hex_start + self._hex_width + (self._char_width / 2),
                    start_y_hex - self._base_line - 1))
            polygon.append(
                QPoint(
                    self._hex_start + self._hex_width + (self._char_width / 2),
                    end_y_hex - self._base_line - 1))
            if self.selection.end % self.bytes_per_line != 0:
                polygon.append(
                    QPoint(end_x_hex - (self._char_width / 2),
                           end_y_hex - self._base_line - 1))
                polygon.append(
                    QPoint(
                        end_x_hex - (self._char_width / 2),
                        end_y_hex + self._char_height - self._base_line + 1))
                polygon.append(
                    QPoint(
                        self._hex_start - (self._char_width / 2),
                        end_y_hex + self._char_height - self._base_line + 1))
            else:
                polygon.append(
                    QPoint(end_x_hex - (self._char_width / 2),
                           end_y_hex - self._base_line - 1))
            polygon.append(
                QPoint(self._hex_start - (self._char_width / 2),
                       start_y_hex + self._char_height - self._base_line + 1))
            polygon.append(
                QPoint(start_x_hex - (self._char_width / 2),
                       start_y_hex + self._char_height - self._base_line + 1))

            if self.caret.mode == 'hex':
                painter.drawPolygon(polygon)
            else:
                painter.drawPolyline(polygon)

            polygon = QPolygon()
            # left linebottom -> left linetop
            polygon.append(
                QPoint(
                    start_x_ascii,
                    start_y_ascii + self._char_height - self._base_line + 1))
            # top left -> right top
            polygon.append(
                QPoint(start_x_ascii, start_y_ascii - self._base_line - 1))
            # right top -> right bottom
            polygon.append(
                QPoint(self._ascii_start + self._ascii_width,
                       start_y_ascii - self._base_line - 1))
            polygon.append(
                QPoint(self._ascii_start + self._ascii_width,
                       end_y_ascii - self._base_line - 1))

            if self.selection.end % self.bytes_per_line != 0:  # caret already next linestart
                polygon.append(
                    QPoint(end_x_ascii, end_y_ascii - self._base_line - 1))
                polygon.append(
                    QPoint(
                        end_x_ascii,
                        end_y_ascii + self._char_height - self._base_line + 1))
                polygon.append(
                    QPoint(
                        self._ascii_start,
                        end_y_ascii + self._char_height - self._base_line + 1))
            else:
                polygon.append(
                    QPoint(end_x_ascii, end_y_ascii - self._base_line - 1))

            # bottom left -> linetop
            polygon.append(
                QPoint(
                    self._ascii_start,
                    start_y_ascii + self._char_height - self._base_line + 1))
            # back to begin
            polygon.append(
                QPoint(
                    start_x_ascii,
                    start_y_ascii + self._char_height - self._base_line + 1))

            if self.caret.mode == 'hex':
                painter.drawPolyline(polygon)
            else:
                painter.drawPolygon(polygon)
def paint_selection ( self , painter ) : if self . selection . start == self . selection . end : return pen_color = QColor ( self . _ctrl_colors [ 'selection_bg' ] ) brush_color = QColor ( self . _ctrl_colors [ 'selection_bg' ] ) brush_color . setAlpha ( 100 ) start_x_hex , start_y_hex = self . index_to_hexcol ( self . selection . start ) end_x_hex , end_y_hex = self . index_to_hexcol ( self . selection . end ) start_x_ascii , start_y_ascii = self . index_to_asciicol ( self . selection . start ) end_x_ascii , end_y_ascii = self . index_to_asciicol ( self . selection . end ) # single line selection if start_y_hex == end_y_hex : # select color if hexsel then with background or outline only if self . caret . mode == 'hex' : painter . setPen ( pen_color ) painter . setBrush ( brush_color ) else : painter . setPen ( pen_color ) painter . setBrush ( Qt . NoBrush ) # draw selection in hexcolumn painter . drawRect ( start_x_hex - ( self . _char_width / 2 ) , # offset by half charwidth start_y_hex - self . _base_line - 1 , # baseline offset end_x_hex - start_x_hex , self . _char_height + 1 ) # switch style if self . caret . mode == 'hex' : painter . setPen ( pen_color ) painter . setBrush ( Qt . NoBrush ) else : painter . setPen ( pen_color ) painter . setBrush ( brush_color ) # draw selection in asciicolumn painter . drawRect ( start_x_ascii , # offset by half charwidth start_y_ascii - self . _base_line - 1 , # baseline offset end_x_ascii - start_x_ascii , self . _char_height + 1 ) elif self . selection . end - self . selection . start <= self . bytes_per_line : # selection over two lines but not joining draw 2 rects if self . caret . mode == 'hex' : painter . setPen ( pen_color ) painter . setBrush ( brush_color ) else : painter . setPen ( pen_color ) painter . setBrush ( Qt . NoBrush ) # draw selection in hexcolumn painter . drawRect ( start_x_hex - ( self . _char_width / 2 ) , # offset by half charwidth start_y_hex - self . _base_line - 1 , # baseline offset ( ( self . _hex_start + self . _hex_width ) - start_x_hex ) + self . _char_width , self . _char_height + 2 ) if self . selection . end % self . bytes_per_line != 0 : painter . drawRect ( self . _hex_start - ( self . _char_width / 2 ) , # offset by half charwidth end_y_hex - self . _base_line - 1 , # baseline offset end_x_hex - self . _hex_start , self . _char_height + 2 ) if self . caret . mode == 'hex' : painter . setPen ( pen_color ) painter . setBrush ( Qt . NoBrush ) else : painter . setPen ( pen_color ) painter . setBrush ( brush_color ) # draw selection in hexcolumn painter . drawRect ( start_x_ascii , # offset by half charwidth start_y_ascii - self . _base_line - 1 , # baseline offset ( ( self . _ascii_start + self . _ascii_width ) - start_x_ascii ) , self . _char_height + 2 ) if self . selection . end % self . bytes_per_line != 0 : painter . drawRect ( self . _ascii_start , # offset by half charwidth end_y_ascii - self . _base_line - 1 , # baseline offset end_x_ascii - self . _ascii_start , self . _char_height + 2 ) else : # multiple lines selection painter . setPen ( pen_color ) painter . setBrush ( brush_color ) polygon = QPolygon ( ) polygon . append ( QPoint ( start_x_hex - ( self . _char_width / 2 ) , start_y_hex + self . _char_height - self . _base_line + 1 ) ) polygon . append ( QPoint ( start_x_hex - ( self . _char_width / 2 ) , start_y_hex - self . _base_line - 1 ) ) polygon . append ( QPoint ( self . _hex_start + self . _hex_width + ( self . _char_width / 2 ) , start_y_hex - self . _base_line - 1 ) ) polygon . append ( QPoint ( self . _hex_start + self . _hex_width + ( self . _char_width / 2 ) , end_y_hex - self . _base_line - 1 ) ) if self . selection . end % self . bytes_per_line != 0 : polygon . append ( QPoint ( end_x_hex - ( self . _char_width / 2 ) , end_y_hex - self . _base_line - 1 ) ) polygon . append ( QPoint ( end_x_hex - ( self . _char_width / 2 ) , end_y_hex + self . _char_height - self . _base_line + 1 ) ) polygon . append ( QPoint ( self . _hex_start - ( self . _char_width / 2 ) , end_y_hex + self . _char_height - self . _base_line + 1 ) ) else : polygon . append ( QPoint ( end_x_hex - ( self . _char_width / 2 ) , end_y_hex - self . _base_line - 1 ) ) polygon . append ( QPoint ( self . _hex_start - ( self . _char_width / 2 ) , start_y_hex + self . _char_height - self . _base_line + 1 ) ) polygon . append ( QPoint ( start_x_hex - ( self . _char_width / 2 ) , start_y_hex + self . _char_height - self . _base_line + 1 ) ) if self . caret . mode == 'hex' : painter . drawPolygon ( polygon ) else : painter . drawPolyline ( polygon ) polygon = QPolygon ( ) # left linebottom -> left linetop polygon . append ( QPoint ( start_x_ascii , start_y_ascii + self . _char_height - self . _base_line + 1 ) ) # top left -> right top polygon . append ( QPoint ( start_x_ascii , start_y_ascii - self . _base_line - 1 ) ) # right top -> right bottom polygon . append ( QPoint ( self . _ascii_start + self . _ascii_width , start_y_ascii - self . _base_line - 1 ) ) polygon . append ( QPoint ( self . _ascii_start + self . _ascii_width , end_y_ascii - self . _base_line - 1 ) ) if self . selection . end % self . bytes_per_line != 0 : # caret already next linestart polygon . append ( QPoint ( end_x_ascii , end_y_ascii - self . _base_line - 1 ) ) polygon . append ( QPoint ( end_x_ascii , end_y_ascii + self . _char_height - self . _base_line + 1 ) ) polygon . append ( QPoint ( self . _ascii_start , end_y_ascii + self . _char_height - self . _base_line + 1 ) ) else : polygon . append ( QPoint ( end_x_ascii , end_y_ascii - self . _base_line - 1 ) ) # bottom left -> linetop polygon . append ( QPoint ( self . _ascii_start , start_y_ascii + self . _char_height - self . _base_line + 1 ) ) # back to begin polygon . append ( QPoint ( start_x_ascii , start_y_ascii + self . _char_height - self . _base_line + 1 ) ) if self . caret . mode == 'hex' : painter . drawPolyline ( polygon ) else : painter . drawPolygon ( polygon )