コード例 #1
0
    def data(self, mi, role):
        """Reimplemented to return the data."""

        obj = list(self._editor.items())[mi.row()]
        column = self._editor.columns[mi.column()]

        if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            text = column.get_value(obj)
            if text is not None:
                return text

        elif role == QtCore.Qt.DecorationRole:
            image = self._editor._get_image(column.get_image(obj))
            if image is not None:
                return image

        elif role == QtCore.Qt.ToolTipRole:
            tooltip = column.get_tooltip(obj)
            if tooltip:
                return tooltip

        elif role == QtCore.Qt.FontRole:
            font = column.get_text_font(obj)
            if font is not None:
                return QtGui.QFont(font)

        elif role == QtCore.Qt.TextAlignmentRole:
            string = column.get_horizontal_alignment(obj)
            h_alignment = h_alignment_map.get(string, QtCore.Qt.AlignLeft)
            string = column.get_vertical_alignment(obj)
            v_alignment = v_alignment_map.get(string, QtCore.Qt.AlignVCenter)
            return int(h_alignment | v_alignment)

        elif role == QtCore.Qt.BackgroundRole:
            color = column.get_cell_color(obj)
            if color is None:
                # FIXME: Yes, this is weird. It should work fine to fall through
                # to the catch-all None at the end, but it doesn't.
                return None
            else:
                q_color = as_qcolor(color)
                return QtGui.QBrush(q_color)

        elif role == QtCore.Qt.ForegroundRole:
            color = column.get_text_color(obj)
            if color is not None:
                q_color = as_qcolor(color)
                return QtGui.QBrush(q_color)

        elif role == QtCore.Qt.UserRole:
            return obj

        elif role == QtCore.Qt.CheckStateRole:
            if column.get_type(obj) == "bool" and column.show_checkbox:
                if column.get_raw_value(obj):
                    return QtCore.Qt.Checked
                else:
                    return QtCore.Qt.Unchecked

        return None
コード例 #2
0
    def data(self, mi, role):
        """Reimplemented to return the data."""
        editor = self._editor
        adapter = editor.adapter
        index = mi.row()

        if role == QtCore.Qt.ItemDataRole.DisplayRole or role == QtCore.Qt.ItemDataRole.EditRole:
            if editor.is_auto_add(index):
                text = adapter.get_default_text(editor.object, editor.name)
            else:
                text = adapter.get_text(editor.object, editor.name, index)
            if role == QtCore.Qt.ItemDataRole.DisplayRole and text == "":
                # FIXME: This is a hack to make empty strings editable.
                text = " "
            return text

        elif role == QtCore.Qt.ItemDataRole.DecorationRole:
            if editor.is_auto_add(index):
                image = adapter.get_default_image(editor.object, editor.name)
            else:
                image = adapter.get_image(editor.object, editor.name, index)
            image = editor.get_image(image)
            if image is not None:
                return image

        elif role == QtCore.Qt.ItemDataRole.ToolTipRole:
            tooltip = adapter.get_tooltip(editor.object, editor.name, index)
            if tooltip:
                return tooltip

        elif role == QtCore.Qt.ItemDataRole.BackgroundRole:
            if editor.is_auto_add(index):
                color = adapter.get_default_bg_color(editor.object,
                                                     editor.name)
            else:
                color = adapter.get_bg_color(editor.object, editor.name, index)
            if color is not None:
                if isinstance(color, SequenceTypes):
                    q_color = QtGui.QColor(*color)
                else:
                    q_color = QtGui.QColor(color)
                return QtGui.QBrush(q_color)

        elif role == QtCore.Qt.ItemDataRole.ForegroundRole:
            if editor.is_auto_add(index):
                color = adapter.get_default_text_color(editor.object,
                                                       editor.name)
            else:
                color = adapter.get_text_color(editor.object, editor.name,
                                               index)
            if color is not None:
                if isinstance(color, SequenceTypes):
                    q_color = QtGui.QColor(*color)
                else:
                    q_color = QtGui.QColor(color)
                return QtGui.QBrush(q_color)

        return None
コード例 #3
0
ファイル: my_tabular_editor.py プロジェクト: NMGRL/sandbox
    def data(self, mi, role=None):
        """ Reimplemented to return the data.
        """
        if role is None:
            role = QtCore.Qt.DisplayRole

        editor = self._editor
        adapter = editor.adapter
        obj, name = editor.object, editor.name
        row, column = mi.row(), mi.column()

        if role == QtCore.Qt.DisplayRole or role == QtCore.Qt.EditRole:
            return adapter.get_text(obj, name, row, column)

        elif role == QtCore.Qt.DecorationRole:
            image = editor._get_image(adapter.get_image(obj, name, row, column))

            if image is not None:
                return image

        elif role == QtCore.Qt.ToolTipRole:
            tooltip = adapter.get_tooltip(obj, name, row, column)
            if tooltip:
                return tooltip

        elif role == QtCore.Qt.FontRole:
            font = adapter.get_font(obj, name, row, column)
            if font is not None:
                return QtGui.QFont(font)

        elif role == QtCore.Qt.TextAlignmentRole:
            string = adapter.get_alignment(obj, name, column)
            alignment = alignment_map.get(string, QtCore.Qt.AlignLeft)
            return int(alignment | QtCore.Qt.AlignVCenter)

        elif role == QtCore.Qt.BackgroundRole:
            color = adapter.get_bg_color(obj, name, row, column)
            if color is not None:
                if isinstance(color, SequenceTypes):
                    q_color = QtGui.QColor(*color)
                else:
                    q_color = QtGui.QColor(color)
                return QtGui.QBrush(q_color)

        elif role == QtCore.Qt.ForegroundRole:
            color = adapter.get_text_color(obj, name, row, column)
            if color is not None:
                if isinstance(color, SequenceTypes):
                    q_color = QtGui.QColor(*color)
                else:
                    q_color = QtGui.QColor(color)
                return QtGui.QBrush(q_color)

        return None
コード例 #4
0
ファイル: qpainter.py プロジェクト: xinshuwei/enable
    def __init__(self, size, *args, **kwargs):
        super(GraphicsContext, self).__init__()
        self._width = size[0]
        self._height = size[1]

        self.text_pos = [0.0, 0.0]
        self.text_transform = (1.0, 0.0, 0.0, 1.0, 0.0, 0.0)

        # create some sort of device context
        parent = kwargs.pop("parent", None)
        if parent is None:
            # no parent -> offscreen context
            self.qt_dc = QtGui.QPixmap(*size)
        else:
            # normal windowed context
            self.qt_dc = parent

        self.gc = QtGui.QPainter(self.qt_dc)
        self.path = CompiledPath()

        # flip y
        trans = QtGui.QTransform()
        trans.translate(0, size[1])
        trans.scale(1.0, -1.0)
        self.gc.setWorldTransform(trans)

        # enable antialiasing
        self.gc.setRenderHints(
            QtGui.QPainter.Antialiasing | QtGui.QPainter.TextAntialiasing,
            True)
        # set the pen and brush to useful defaults
        self.gc.setPen(QtCore.Qt.black)
        self.gc.setBrush(QtGui.QBrush(QtCore.Qt.SolidPattern))
コード例 #5
0
    def paintEvent(self, event):
        super(QFunctionControl, self).paintEvent(event)

        painter = QtGui.QPainter(self)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        painter.setBrush(brush)
        width, height = self.size().width(), self.size().height()
        painter.drawRect(0, 0, width, height)
        for channel in self.channels:
            channel.paint(painter)
コード例 #6
0
    def _get_brush(self, color):
        """ Returns a brush for the color.
        """
        result = self._brushes.get(color)
        if result is None:
            qcolor = self._get_color(color)
            result = QtGui.QBrush(qcolor)
            self._brushes[color] = result

        return result
コード例 #7
0
ファイル: qpainter.py プロジェクト: xinshuwei/enable
 def clear(self, clear_color=(1.0, 1.0, 1.0, 1.0)):
     """
     """
     if len(clear_color) == 4:
         r, g, b, a = clear_color
     else:
         r, g, b = clear_color
         a = 1.0
     self.gc.setBackground(QtGui.QBrush(QtGui.QColor.fromRgbF(r, g, b, a)))
     self.gc.eraseRect(QtCore.QRectF(0, 0, self.width(), self.height()))
コード例 #8
0
ファイル: qpainter.py プロジェクト: pingleewu/ShareRoot
    def _apply_gradient(self, grad, stops, spread_method, units):
        """ Configures a gradient object and sets it as the current brush.
        """
        grad.setSpread(gradient_spread_modes.get(spread_method,
                                                 QtGui.QGradient.PadSpread))
        grad.setCoordinateMode(gradient_coord_modes.get(
            units, QtGui.QGradient.LogicalMode))

        for stop in stops:
            grad.setColorAt(stop[0], QtGui.QColor.fromRgbF(*stop[1:]))

        self.gc.setBrush(QtGui.QBrush(grad))
コード例 #9
0
def _color_to_brush(color):
    """ Returns a QBrush with the color specified in **color** """
    brush = QtGui.QBrush()
    if isinstance(color, str) and hasattr(QtCore.Qt, color):
        col = getattr(QtCore.Qt, color)
    elif isinstance(color, tuple):
        col = QtGui.QColor()
        col.setRgb(*color[:4])
    else:
        raise RuntimeError("Invalid color specification '%r'" % color)

    brush.setColor(col)
    return brush
コード例 #10
0
ファイル: qpainter.py プロジェクト: xinshuwei/enable
 def draw_rect(self, rect, mode=constants.FILL_STROKE):
     """ Draw a rect.
     """
     rect = QtCore.QRectF(*rect)
     if mode == constants.STROKE:
         save_brush = self.gc.brush()
         self.gc.setBrush(QtGui.QBrush(QtCore.Qt.NoBrush))
         self.gc.drawRect(rect)
         self.gc.setBrush(save_brush)
     elif mode in [constants.FILL, constants.EOF_FILL]:
         self.gc.fillRect(rect, self.gc.brush())
     else:
         self.gc.fillRect(rect, self.gc.brush())
         self.gc.drawRect(rect)
コード例 #11
0
    def paint(self, painter, option, index):
        # column = index.model()._editor.columns[index.column()]
        obj = index.data(QtCore.Qt.UserRole)
        # v = column.get_raw_value(obj)
        v = obj.get_percent_value()

        painter.save()
        # painter.begin(self)
        # qp.setBrush(QtGui.QColor(*self.value))
        brush = QtGui.QBrush(QtCore.Qt.SolidPattern)
        brush.setColor(QtGui.QColor(255, 0, 0))
        rect = option.rect
        w = rect.width()

        rect.setWidth(w * min(1.0, v))
        painter.fillRect(rect, brush)
        painter.restore()
コード例 #12
0
    def paint(self, painter):
        """Paint current channel into Canvas (a canvas of a function control
        object).

        Contents of the canvas are not deleted prior to painting,
        so more than one channel can be painted into the same canvas.
        """

        table = self.control.table
        # only control points which are active for the current channel
        # are to be painted. filter them out.
        relevant_control_points = filter( \
            lambda x: self.name in x.active_channels,
            table.control_points )
        # lines between control points
        color = QtGui.QColor(*self.rgb_color)
        painter.setPen(color)
        brush = QtGui.QBrush(color)
        painter.setBrush(brush)
        painter.setBackgroundMode(QtCore.Qt.OpaqueMode)
        for k in range(len(relevant_control_points) - 1):
            cur_point = relevant_control_points[k]
            next_point = relevant_control_points[1 + k]

            painter.drawLine(self.get_pos_index(cur_point.pos),
                             self.get_value_index(cur_point.color),
                             self.get_pos_index(next_point.pos),
                             self.get_value_index(next_point.color))

        # control points themself.
        color = QtCore.Qt.black
        painter.setPen(color)
        for control_point in relevant_control_points:
            x = self.get_pos_index(control_point.pos)
            y = self.get_value_index(control_point.color)
            radius = 6
            #print(x,y)
            painter.drawRect(x - (radius / 2.0), y - (radius / 2.0), radius,
                             radius)
            painter.drawRect(100, 80, 6, 6)
コード例 #13
0
    def paintEvent(self, event):
        """Paint handler."""
        super(QGradientControl, self).paintEvent(event)

        painter = QtGui.QPainter(self)
        brush = QtGui.QBrush(QtGui.QColor(0, 0, 0))
        painter.setBrush(brush)
        painter.setBackgroundMode(QtCore.Qt.OpaqueMode)
        sz = self.size()
        width, height = sz.width(), sz.height()

        xform = self.gradient_table.scaling_function
        start_y = 0
        end_y = height
        if xform:
            # if a scaling transformation is provided, paint the original
            # gradient under the scaled gradient.
            start_y = height / 2

        # paint the original gradient as it stands in the table.
        color = QtGui.QColor()
        for x in range(width):
            (r, g, b, a) = self.gradient_table.get_pos_rgba_color_lerped(
                float(x) / (width - 1))
            color.setRgb(int(255 * r), int(255 * g), int(255 * b))
            painter.setPen(color)
            brush.setColor(color)
            painter.drawLine(x, start_y, x, end_y)
        if xform:
            # paint the scaled gradient below
            end_y = start_y
            start_y = 0
            for x in range(width):
                f = float(x) / (width - 1)
                (r, g, b,
                 a) = self.gradient_table.get_pos_rgba_color_lerped(xform(f))
                color.set(int(255 * r), int(255 * g), int(255 * b))
                brush.setColor(color)
                painter.drawLine(x, start_y, x, end_y)
コード例 #14
0
ファイル: table_model.py プロジェクト: enthought/traitsui
    def data(self, mi, role):
        """Reimplemented to return the data."""

        obj = self._editor.items()[mi.row()]
        column = self._editor.columns[mi.column()]

        if self._editor.factory is None:
            # XXX This should never happen, but it does,
            # probably during shutdown, but I haven't investigated
            return None

        if role == QtCore.Qt.ItemDataRole.DisplayRole or role == QtCore.Qt.ItemDataRole.EditRole:
            text = column.get_value(obj)
            if text is not None:
                return text

        elif role == QtCore.Qt.ItemDataRole.DecorationRole:
            image = self._editor._get_image(column.get_image(obj))
            if image is not None:
                return image

        elif role == QtCore.Qt.ItemDataRole.ToolTipRole:
            tooltip = column.get_tooltip(obj)
            if tooltip:
                return tooltip

        elif role == QtCore.Qt.ItemDataRole.FontRole:
            font = column.get_text_font(obj)
            if font is None:
                font = self._editor.factory.cell_font
            if font is not None:
                return QtGui.QFont(font)

        elif role == QtCore.Qt.ItemDataRole.TextAlignmentRole:
            string = column.get_horizontal_alignment(obj)
            h_alignment = h_alignment_map.get(
                string, QtCore.Qt.AlignmentFlag.AlignLeft)
            string = column.get_vertical_alignment(obj)
            v_alignment = v_alignment_map.get(
                string, QtCore.Qt.AlignmentFlag.AlignVCenter)
            return int(h_alignment | v_alignment)

        elif role == QtCore.Qt.ItemDataRole.BackgroundRole:
            color = column.get_cell_color(obj)
            if color is None:
                if column.is_editable(obj):
                    color = self._editor.factory.cell_bg_color_
                else:
                    color = self._editor.factory.cell_read_only_bg_color_
                if color is None:
                    # FIXME: Yes, this is weird. It should work fine to fall through
                    # to the catch-all None at the end, but it doesn't.
                    return None
            q_color = as_qcolor(color)
            return QtGui.QBrush(q_color)

        elif role == QtCore.Qt.ItemDataRole.ForegroundRole:
            color = column.get_text_color(obj)
            if color is None:
                color = self._editor.factory.cell_color_
            if color is not None:
                q_color = as_qcolor(color)
                return QtGui.QBrush(q_color)

        elif role == QtCore.Qt.ItemDataRole.UserRole:
            return obj

        elif role == QtCore.Qt.ItemDataRole.CheckStateRole:
            if column.get_type(obj) == "bool" and column.show_checkbox:
                if column.get_raw_value(obj):
                    return QtCore.Qt.CheckState.Checked
                else:
                    return QtCore.Qt.CheckState.Unchecked

        return None