1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
|
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
from enaml.qt.compat import global_pos_from_event
from enaml.qt.QtCore import QPoint, QRect, QEvent
from enaml.qt.QtGui import QPainter, QColor
from enaml.qt.QtWidgets import (
QAbstractButton, QStyle, QStyleOption, QToolTip
)
class QBitmapButton(QAbstractButton):
""" A button widget which renders a bitmap.
This class is used to render the various maximize, restore, and
close buttons in the docking framework. Bitmap images are chosen
for rendering so that the button can be fully styled using Qt
style sheets.
"""
_bitmap = None
def bitmap(self):
""" Get the bitmap associated with the button.
"""
return self._bitmap
def setBitmap(self, bitmap):
""" Set the bitmap associate with the button.
"""
self._bitmap = bitmap
self.update()
def sizeHint(self):
""" Get the size hint for the bitmap button.
The size hint of the button is equal to it's icon size.
"""
return self.minimumSizeHint()
def minimumSizeHint(self):
""" Get the minimum size hint for the bitmap button.
The minimum size hint of the button is equal to it's icon size.
"""
return self.iconSize()
def enterEvent(self, event):
""" Handle the enter event for the button.
"""
if self.isEnabled():
self.update()
super(QBitmapButton, self).enterEvent(event)
def leaveEvent(self, event):
""" Handle the leave event for the button.
"""
if self.isEnabled():
self.update()
super(QBitmapButton, self).leaveEvent(event)
def styleOption(self):
""" Get a filled style option for the button.
Returns
-------
result : QStyleOption
A style option initialized for the current button state.
"""
opt = QStyleOption()
opt.initFrom(self)
opt.state |= QStyle.State_AutoRaise
is_down = self.isDown()
is_enabled = self.isEnabled()
is_checked = self.isChecked()
under_mouse = self.underMouse()
if is_enabled and under_mouse and not is_checked and not is_down:
opt.state |= QStyle.State_Raised
if is_checked:
opt.state |= QStyle.State_On
if is_down:
opt.state |= QStyle.State_Sunken
return opt
def drawBitmap(self, bmp, opt, painter):
""" Draw the bitmap for the button.
The bitmap will be drawn with the foreground color set by
the style sheet and the style option.
Parameters
----------
bmp : QBitmap
The bitmap to draw.
opt : QStyleOption
The style option to use for drawing.
painter : QPainter
The painter to use for drawing.
"""
# hack to get the current stylesheet foreground color
hint = QStyle.SH_GroupBox_TextLabelColor
fg = self.style().styleHint(hint, opt, self)
# mask signed to unsigned which 'fromRgba' requires
painter.setPen(QColor.fromRgba(0xffffffff & fg))
size = self.size()
im_size = bmp.size()
x = int(round(size.width() / 2 - im_size.width() / 2))
y = int(round(size.height() / 2 - im_size.height() / 2))
source = QRect(QPoint(0, 0), im_size)
dest = QRect(QPoint(x, y), im_size)
painter.drawPixmap(dest, bmp, source)
def paintEvent(self, event):
""" Handle the paint event for the button.
"""
painter = QPainter(self)
opt = self.styleOption()
self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
bmp = self._bitmap
if bmp is not None:
self.drawBitmap(bmp, opt, painter)
class QCheckedBitmapButton(QBitmapButton):
""" A bitmap button subclass which supports a checked bitmap.
"""
_tool_tip = ''
_checked_tool_tip = ''
_checked_bitmap = None
def __init__(self, parent=None):
""" Initialize a QCheckedBitmapButton.
Parameters
----------
parent : QWidget or None
The parent widget of the button.
"""
super(QCheckedBitmapButton, self).__init__(parent)
self.setCheckable(True)
#--------------------------------------------------------------------------
# Private API
#--------------------------------------------------------------------------
def _effectiveToolTip(self):
""" Get the current effective tool tip for the button.
"""
if self.isChecked():
tool_tip = self._checked_tool_tip or self._tool_tip
else:
tool_tip = self._tool_tip
return tool_tip
#--------------------------------------------------------------------------
# Public API
#--------------------------------------------------------------------------
def toolTip(self):
""" Get the tool tip for the button.
"""
return self._tool_tip
def setToolTip(self, tool_tip):
""" Set the tool tip for the button.
"""
self._tool_tip = tool_tip
def checkedToolTip(self):
""" Get the checked tool tip for the button.
"""
return self._checked_tool_tip
def setCheckedToolTip(self, tool_tip):
""" Set the checked tool tip for the button.
"""
self._checked_tool_tip = tool_tip
def checkedBitmap(self):
""" Get the bitmap associated with the button checked state.
"""
return self._checked_bitmap
def setCheckedBitmap(self, bitmap):
""" Set the bitmap associate with the button checked state.
"""
self._checked_bitmap = bitmap
self.update()
def event(self, event):
""" A generic event handler for the button.
This handler shows the effective tool tip on a tool tip event.
"""
if event.type() == QEvent.ToolTip:
tool_tip = self._effectiveToolTip()
if tool_tip:
QToolTip.showText(global_pos_from_event(event), tool_tip, self)
return True
return super(QCheckedBitmapButton, self).event(event)
def paintEvent(self, event):
""" Handle the paint event for the button.
"""
painter = QPainter(self)
opt = self.styleOption()
self.style().drawPrimitive(QStyle.PE_Widget, opt, painter, self)
if self.isChecked():
bmp = self._checked_bitmap or self._bitmap
else:
bmp = self._bitmap
if bmp is not None:
self.drawBitmap(bmp, opt, painter)
|