def add_square(self, pos, size, col='black', thickness=None): """Draw a square. Args: pos (QPoint): square position size (QSize): square size col (str): square colour thickness (float): line thickness """ from psyhive import qt _pos = qt.get_p(pos) _size = qt.get_size(size) _rect = QtCore.QRect(_pos, _size) _brush = QtGui.QBrush(qt.HColor(0, 0, 0, 0)) _col = qt.get_col(col) _pen = QtGui.QPen(_col) if thickness: _pen.setWidthF(thickness) _pnt = qt.HPainter() _pnt.begin(self) _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawRect(_rect) _pnt.end() return _rect
def add_polygon(self, pts, col, outline='black', thickness=1.0): """Draw a polygon on this pixmap. Args: pts (QPointF list): polygon points col (QColor): fill colour outline (str|None): outline colour (if any) thickness (float): line thickness """ from psyhive import qt if outline: _pen = QtGui.QPen(outline) _pen.setCapStyle(Qt.RoundCap) if thickness: _pen.setWidthF(thickness) else: _pen = QtGui.QPen() _pen.setStyle(Qt.NoPen) _col = qt.get_col(col) _brush = QtGui.QBrush(_col) _poly = QtGui.QPolygonF() for _pt in pts: _pt = qt.get_p(_pt) _poly.append(_pt) _pnt = HPainter() _pnt.begin(self) _pnt.setBrush(_brush) _pnt.setPen(_pen) _pnt.drawPolygon(_poly) _pnt.end()
def set_col(self, col): """Set colour for this progress bar. Args: col (str): colour to apply """ _col = get_col(col) _palette = QtGui.QPalette() _brush = QtGui.QBrush(_col) for _state in [QtGui.QPalette.Active, QtGui.QPalette.Inactive]: _palette.setBrush(_state, QtGui.QPalette.Highlight, _brush) self.setPalette(_palette)
def add_dot(self, pos, col='black', radius=1.0, outline=None, thickness=None, operation=None, render_hint=None): """Draw a circle on this pixmap. Args: pos (QPoint): centre point col (str): dot colour radius (float): dot radius outline (QPen): apply outline pen thickness (float): line thickness operation (str): compositing operation render_hint (RenderHint): add render hint """ from psyhive import qt _pos = qt.get_p(pos) _col = qt.get_col(col) _brush = QtGui.QBrush(_col) # Set outline if thickness: _pen = QtGui.QPen(qt.get_col('Black')) _pen.setWidthF(thickness) elif not outline: _pen = QtGui.QPen(_col) _pen.setStyle(Qt.NoPen) elif isinstance(outline, QtGui.QPen): _pen = outline elif isinstance(outline, six.string_types): _out_col = qt.get_col(outline) _pen = QtGui.QPen(_out_col) else: raise ValueError(outline) _pnt = HPainter() _pnt.begin(self) _pnt.setBrush(_brush) _pnt.setPen(_pen) if render_hint: _pnt.setRenderHint(render_hint) _pnt.set_operation(operation) _pnt.drawEllipse(_pos.x() - radius, _pos.y() - radius, radius * 2, radius * 2) _pnt.end()
def add_rounded_rect(self, pos, size, col='White', bevel=5, anchor='TL', pen=None, outline=True): """Draw a rounded rectangle on this pixmap. Args: pos (QPoint): position size (QSize): rectangle size col (str): rectangle fill colour bevel (int): edge bevel anchor (str): position anchor point pen (QPen): override pen outline (bool): show outline Returns: (QRect): draw region """ from psyhive import qt if isinstance(col, QtGui.QPixmap): _col = col else: _col = qt.get_col(col) _brush = QtGui.QBrush(_col) _rect = _get_rect(pos=pos, size=size, anchor=anchor) # Set pen _pen = None if pen: _pen = pen elif not outline: _pen = QtGui.QPen(_col) _pen.setStyle(Qt.NoPen) _pnt = qt.HPainter() _pnt.begin(self) if _pen: _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawRoundedRect(_rect, bevel, bevel) _pnt.end() return _rect
def add_circle(self, pos, col='black', radius=10, thickness=None, operation=None, pen=None): """Draw a circle on this pixmap. Args: pos (QPoint): centre point col (str): line colour radius (int): circle radius thickness (float): line thickness operation (str): compositing operation pen (QPen): override pen """ from psyhive import qt _pos = qt.get_p(pos) _col = qt.get_col(col) _pen = pen or QtGui.QPen(_col) if thickness: _pen.setWidthF(thickness) _rect = QtCore.QRect(_pos.x() - radius, _pos.y() - radius, radius * 2, radius * 2) _pnt = HPainter() _pnt.begin(self) _pnt.set_operation(operation) _pnt.setPen(_pen) _pnt.drawArc(_rect, 0, 360 * 16) _pnt.end() return _rect
def set_icon(self, pixmap): """Set icon for this widget. Args: pixmap (str|QPixmap): pixmap to apply """ _icon = QtGui.QIcon(get_pixmap(pixmap)) self.setIcon(_icon)
def set_icon(self, image): """Set icon for this item. Args: image (str|QPixmap): icon to apply """ _icon = QtGui.QIcon(get_pixmap(image)) self.setIcon(_icon)
def set_col(self, col): """Set text colour of this item. Args: col (str|QColor): colour to apply """ _brush = QtGui.QBrush(get_col(col)) self.setForeground(_brush)
def add_rect(self, pos, size, col='white', outline='black', operation=None, anchor='TL', thickness=None): """Draw a rectangle on this pixmap. Args: pos (QPoint): position size (QSize): rectangle size col (str): rectangle colour outline (str): outline colour operation (str): overlay mode anchor (str): position anchor point thickness (float): line thickness """ from psyhive import qt _col = qt.get_col(col) _brush = QtGui.QBrush(_col) _rect = _get_rect(pos=pos, size=size, anchor=anchor) # Set outline if outline: _pen = QtGui.QPen(outline) else: _pen = QtGui.QPen() _pen.setStyle(Qt.NoPen) if thickness: _pen.setWidthF(thickness) _pnt = HPainter() _pnt.begin(self) _pnt.set_operation(operation) _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawRect(_rect) _pnt.end() return _rect
def rotated(self, degrees): """Get a rotated version of this pixmap. Args: degrees (float): rotation to apply Returns: (QPixmap): rotate pixmap """ _tfm = QtGui.QTransform() _tfm.rotate(degrees) return HPixmap(self.transformed(_tfm))
def add_path(self, pts, col='black', thickness=None, pen=None): """Draw a path on this pixmap. Args: pts (QPoint list): list of points in path col (str): path colour thickness (float): line thickness pen (QPen): override pen (ignores all other pen attrs) """ from psyhive import qt # Set pen if pen: _pen = pen else: _col = qt.get_col(col) _pen = pen or QtGui.QPen(_col) _pen.setCapStyle(Qt.RoundCap) if thickness: _pen.setWidthF(thickness) _brush = QtGui.QBrush() _brush.setStyle(Qt.NoBrush) # Make path object _path = QtGui.QPainterPath() _path.moveTo(qt.get_p(pts[0])) for _pt in pts[1:]: _path.lineTo(qt.get_p(_pt)) _pnt = HPainter() _pnt.begin(self) _pnt.setPen(_pen) _pnt.setBrush(_brush) _pnt.drawPath(_path) _pnt.end()
def __init__(self, text=None, col=None, data=None): """Constructor. Args: text (str): display text col (str): text colour data (any): data to store with item """ self._col = col or self._col super(HTreeWidgetItem, self).__init__() if text and isinstance(text, six.string_types): self.setText(0, text) if self._col: _brush = QtGui.QBrush(get_col(self._col)) self.setForeground(0, _brush) if data: self.setData(0, Qt.UserRole, data)
def add_line(self, pt1, pt2, col='black', thickness=None, operation=None, pen=None, verbose=0): """Draw a straight line on this pixmap. Args: pt1 (QPoint): start point pt2 (QPoint): end point col (str): line colour thickness (float): line thickness operation (str): compositing operation pen (QPen): override pen (ignores all other pen attrs) verbose (int): print process data """ from psyhive import qt _pt1 = qt.get_p(pt1) _pt2 = qt.get_p(pt2) # Get pen if pen: _pen = pen else: _col = qt.get_col(col) _pen = QtGui.QPen(_col) _pen.setCapStyle(Qt.RoundCap) _pen.setJoinStyle(Qt.RoundJoin) if thickness: _pen.setWidthF(thickness) lprint("COL", _col, verbose=verbose) _pnt = HPainter() _pnt.begin(self) _pnt.setPen(_pen) _pnt.set_operation(operation) _pnt.drawLine(_pt1.x(), _pt1.y(), _pt2.x(), _pt2.y()) _pnt.end()
def _add_icon(self, icon, icon_size): """Add the icon to the interface. Args: icon (str): path to icon icon_size (int): icon size in pixels """ if not icon: return if isinstance(icon, six.string_types): _pixmap = QtGui.QPixmap(icon) elif isinstance(icon, QtGui.QPixmap): _pixmap = icon else: raise ValueError(_pixmap) if icon_size is None: if _pixmap.width() == 144 and _pixmap.height() == 144: icon_size = 72 if icon_size: _pixmap = _pixmap.scaled(icon_size, icon_size) self.setIconPixmap(_pixmap)
def add_text(self, text, pos=(0, 0), anchor='TL', col='white', font=None, size=None, verbose=0): """Write text to the image. Args: text (str): text to add pos (tuple|QPoint): text position anchor (str): text anchor col (str|QColor): text colour font (QFont): text font size (int): apply font size verbose (int): print process data """ from psyhive.qt import get_p, get_col lprint("Adding text", text, verbose=verbose) _window = self.window() _pos = get_p(pos) _x, _y = _pos.x(), _pos.y() _w, _h = _window.width(), _window.height() if anchor == 'BL': _rect = QtCore.QRect(_x, 0, _w - _x, _y) _align = Qt.AlignLeft | Qt.AlignBottom elif anchor == 'BR': _rect = QtCore.QRect(0, 0, _x, _y) _align = Qt.AlignRight | Qt.AlignBottom elif anchor == 'B': _rect = QtCore.QRect(0, 0, 2 * _x, _y) _align = Qt.AlignHCenter | Qt.AlignBottom elif anchor == 'C': _rect = QtCore.QRect(0, 0, 2 * _x, 2 * _y) _align = Qt.AlignHCenter | Qt.AlignVCenter elif anchor == 'L': _rect = QtCore.QRect(_x, 0, _w, 2 * _y) _align = Qt.AlignVCenter | Qt.AlignLeft elif anchor == 'R': _rect = QtCore.QRect(0, 0, _x, 2 * _y) _align = Qt.AlignRight | Qt.AlignVCenter elif anchor in ('T', 'TC'): _rect = QtCore.QRect(0, _y, 2 * _x, _h) _align = Qt.AlignHCenter | Qt.AlignTop elif anchor == 'TL': _rect = QtCore.QRect(_x, _y, _w, _h) _align = Qt.AlignLeft | Qt.AlignTop elif anchor == 'TR': _rect = QtCore.QRect(0, _y, _x, _h - _y) _align = Qt.AlignRight | Qt.AlignTop else: raise ValueError('Unhandled anchor: %s' % anchor) if font: self.setFont(font) elif size is not None: _font = QtGui.QFont() _font.setPointSize(size) self.setFont(_font) # Draw text self.setPen(get_col(col or 'white')) self.drawText(_rect, _align, text)
def set_maya_palette(): """Apply maya palette. This allows interfaces outside maya to use the same colouring. """ _base_palette = QtGui.QPalette() _base_palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(MID_COLOR)) _base_palette.setBrush(QtGui.QPalette.WindowText, QtGui.QBrush(TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Foreground, QtGui.QBrush(BRIGHT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Base, QtGui.QBrush(DARK_COLOR)) _base_palette.setBrush(QtGui.QPalette.AlternateBase, QtGui.QBrush(ALTERNATE_BASE_COLOR)) _base_palette.setBrush(QtGui.QPalette.ToolTipBase, QtGui.QBrush(BASE_COLOR)) _base_palette.setBrush(QtGui.QPalette.ToolTipText, QtGui.QBrush(TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Text, QtGui.QBrush(TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Text, QtGui.QBrush(DISABLED_TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Button, QtGui.QBrush(LIGHT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, QtGui.QBrush(DISABLED_BUTTON_COLOR)) _base_palette.setBrush(QtGui.QPalette.ButtonText, QtGui.QBrush(TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, QtGui.QBrush(DISABLED_TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.BrightText, QtGui.QBrush(TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.BrightText, QtGui.QBrush(DISABLED_TEXT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Light, QtGui.QBrush(LIGHT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Midlight, QtGui.QBrush(MID_LIGHT_COLOR)) _base_palette.setBrush(QtGui.QPalette.Mid, QtGui.QBrush(MID_COLOR)) _base_palette.setBrush(QtGui.QPalette.Dark, QtGui.QBrush(DARK_COLOR)) _base_palette.setBrush(QtGui.QPalette.Shadow, QtGui.QBrush(SHADOW_COLOR)) _base_palette.setBrush(QtGui.QPalette.Highlight, QtGui.QBrush(HIGHLIGHT_COLOR)) _base_palette.setBrush(QtGui.QPalette.HighlightedText, QtGui.QBrush(HIGHLIGHTEDTEXT_COLOR)) # Setup additional palettes for QTabBar and QTabWidget to look more like # maya. _tab_palette = QtGui.QPalette(_base_palette) _tab_palette.setBrush(QtGui.QPalette.Window, QtGui.QBrush(LIGHT_COLOR)) _tab_palette.setBrush(QtGui.QPalette.Button, QtGui.QBrush(MID_COLOR)) _widget_palettes = {} _widget_palettes["QTabBar"] = _tab_palette _widget_palettes["QTabWidget"] = _tab_palette QtWidgets.QApplication.setStyle("Plastique") QtWidgets.QApplication.setPalette(_base_palette) for _name, _palette in _widget_palettes.items(): QtWidgets.QApplication.setPalette(_palette, _name)
"""Tools for mimicing maya's palette outside maya.""" from psyhive.qt.wrapper.mgr import QtGui, QtWidgets HIGHLIGHT_COLOR = QtGui.QColor(103, 141, 178) BRIGHTNESS_SPREAD = 2.5 BRIGHT_COLOR = QtGui.QColor(200, 200, 200) LIGHT_COLOR = QtGui.QColor(100, 100, 100) DARK_COLOR = QtGui.QColor(42, 42, 42) MID_COLOR = QtGui.QColor(68, 68, 68) MID_LIGHT_COLOR = QtGui.QColor(84, 84, 84) SHADOW_COLOR = QtGui.QColor(21, 21, 21) BASE_COLOR = MID_COLOR TEXT_COLOR = BRIGHT_COLOR DISABLED_BUTTON_COLOR = QtGui.QColor(78, 78, 78) DISABLED_TEXT_COLOR = QtGui.QColor(128, 128, 128) ALTERNATE_BASE_COLOR = QtGui.QColor(46, 46, 46) SPREAD = 100 * BRIGHTNESS_SPREAD HIGHLIGHTEDTEXT_COLOR = BASE_COLOR.lighter(SPREAD * 2) def set_maya_palette(): """Apply maya palette. This allows interfaces outside maya to use the same colouring. """ _base_palette = QtGui.QPalette()