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 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
|
from __future__ import annotations
from AnyQt.QtCore import Qt, QRect, QSize
from AnyQt.QtGui import QBrush, QIcon, QCursor, QPalette, QPainter, QMouseEvent
from AnyQt.QtWidgets import (
QHeaderView, QStyleOptionHeader, QStyle, QApplication, QStyleOptionViewItem
)
class HeaderView(QHeaderView):
"""
A QHeaderView reimplementing `paintSection` to better deal with
selections in large models.
In particular:
* `isColumnSelected`/`isRowSelected` are never queried, only
`rowIntersectsSelection`/`columnIntersectsSelection` are used.
* when `highlightSections` is not enabled the selection model is not
queried at all.
"""
def __init__(self, *args, **kwargs):
self.__pressed = -1 # Tracking the pressed section index
super().__init__(*args, **kwargs)
def set_pressed(index):
self.__pressed = index
self.sectionPressed.connect(set_pressed)
self.sectionEntered.connect(set_pressed)
# Workaround for QTBUG-89910
self.setFont(QApplication.font("QHeaderView"))
def mouseReleaseEvent(self, event: QMouseEvent):
self.__pressed = -1
super().mouseReleaseEvent(event)
def __sectionIntersectsSelection(self, logicalIndex: int) -> bool:
selmodel = self.selectionModel()
if selmodel is None:
return False # pragma: no cover
root = self.rootIndex()
if self.orientation() == Qt.Horizontal:
return selmodel.columnIntersectsSelection(logicalIndex, root)
else:
return selmodel.rowIntersectsSelection(logicalIndex, root)
def __isFirstVisibleSection(self, visualIndex):
log = self.logicalIndex(visualIndex)
if log != -1:
return (self.sectionPosition(log) == 0 and
self.sectionSize(log) > 0)
else:
return False # pragma: no cover
def __isLastVisibleSection(self, visualIndex):
log = self.logicalIndex(visualIndex)
if log != -1:
pos = self.sectionPosition(log)
size = self.sectionSize(log)
return size > 0 and pos + size == self.length()
else:
return False # pragma: no cover
# pylint: disable=too-many-branches
def initStyleOptionForIndex(
self, option: QStyleOptionHeader, logicalIndex: int
) -> None:
"""
Similar to initStyleOptionForIndex in Qt 6.0 with the difference that
`isSectionSelected` is not used, only `sectionIntersectsSelection`
is used (isSectionSelected will scan the entire model column/row
when the whole column/row is selected).
"""
model = self.model()
if model is None:
return
hover = self.logicalIndexAt(self.mapFromGlobal(QCursor.pos()))
pressed = self.__pressed
if self.highlightSections():
is_selected = self.__sectionIntersectsSelection
else:
is_selected = lambda _: False
state = QStyle.State_None
if self.isEnabled():
state |= QStyle.State_Enabled
if self.window().isActiveWindow():
state |= QStyle.State_Active
if self.sectionsClickable():
if logicalIndex == hover:
state |= QStyle.State_MouseOver
if logicalIndex == pressed:
state |= QStyle.State_Sunken
if self.highlightSections():
if is_selected(logicalIndex):
state |= QStyle.State_On
if self.isSortIndicatorShown() and \
self.sortIndicatorSection() == logicalIndex:
option.sortIndicator = (
QStyleOptionHeader.SortDown
if self.sortIndicatorOrder() == Qt.AscendingOrder
else QStyleOptionHeader.SortUp
)
style = self.style()
orientation = self.orientation()
textAlignment = model.headerData(logicalIndex, self.orientation(),
Qt.TextAlignmentRole)
defaultAlignment = self.defaultAlignment()
textAlignment = (textAlignment if isinstance(textAlignment, int)
else defaultAlignment)
option.section = logicalIndex
option.state = QStyle.State(option.state | state)
option.textAlignment = Qt.Alignment(textAlignment)
option.iconAlignment = Qt.AlignVCenter
text = model.headerData(logicalIndex, self.orientation(),
Qt.DisplayRole)
text = str(text) if text is not None else ""
option.text = text
icon = model.headerData(
logicalIndex, self.orientation(), Qt.DecorationRole)
try:
option.icon = QIcon(icon)
except (TypeError, ValueError): # pragma: no cover
pass
margin = 2 * style.pixelMetric(QStyle.PM_HeaderMargin, None, self)
headerArrowAlignment = style.styleHint(QStyle.SH_Header_ArrowAlignment,
None, self)
isHeaderArrowOnTheSide = headerArrowAlignment & Qt.AlignVCenter
if self.isSortIndicatorShown() and \
self.sortIndicatorSection() == logicalIndex \
and isHeaderArrowOnTheSide:
margin += style.pixelMetric(QStyle.PM_HeaderMarkSize, None, self)
if not option.icon.isNull():
margin += style.pixelMetric(QStyle.PM_SmallIconSize, None, self)
margin += style.pixelMetric(QStyle.PM_HeaderMargin, None, self)
if self.textElideMode() != Qt.ElideNone:
elideMode = self.textElideMode()
if hasattr(option, 'textElideMode'): # Qt 6.0
option.textElideMode = elideMode # pragma: no cover
else:
option.text = option.fontMetrics.elidedText(
option.text, elideMode, option.rect.width() - margin)
foregroundBrush = model.headerData(logicalIndex, orientation,
Qt.ForegroundRole)
try:
foregroundBrush = QBrush(foregroundBrush)
except (TypeError, ValueError):
pass
else:
option.palette.setBrush(QPalette.ButtonText, foregroundBrush)
backgroundBrush = model.headerData(logicalIndex, orientation,
Qt.BackgroundRole)
try:
backgroundBrush = QBrush(backgroundBrush)
except (TypeError, ValueError):
pass
else:
option.palette.setBrush(QPalette.Button, backgroundBrush)
option.palette.setBrush(QPalette.Window, backgroundBrush)
# the section position
visual = self.visualIndex(logicalIndex)
assert visual != -1
first = self.__isFirstVisibleSection(visual)
last = self.__isLastVisibleSection(visual)
if first and last:
option.position = QStyleOptionHeader.OnlyOneSection
elif first:
option.position = QStyleOptionHeader.Beginning
elif last:
option.position = QStyleOptionHeader.End
else:
option.position = QStyleOptionHeader.Middle
option.orientation = orientation
# the selected position (in QHeaderView this is always computed even if
# highlightSections is False).
if self.highlightSections():
previousSelected = is_selected(self.logicalIndex(visual - 1))
nextSelected = is_selected(self.logicalIndex(visual + 1))
else:
previousSelected = nextSelected = False
if previousSelected and nextSelected:
option.selectedPosition = QStyleOptionHeader.NextAndPreviousAreSelected
elif previousSelected:
option.selectedPosition = QStyleOptionHeader.PreviousIsSelected
elif nextSelected:
option.selectedPosition = QStyleOptionHeader.NextIsSelected
else:
option.selectedPosition = QStyleOptionHeader.NotAdjacent
def paintSection(self, painter, rect, logicalIndex):
# type: (QPainter, QRect, int) -> None
"""
Reimplemented from `QHeaderView`.
"""
# What follows is similar to QHeaderView::paintSection@Qt 6.0
if not rect.isValid():
return # pragma: no cover
oldBO = painter.brushOrigin()
opt = QStyleOptionHeader()
opt.rect = rect
self.initStyleOption(opt)
oBrushButton = opt.palette.brush(QPalette.Button)
oBrushWindow = opt.palette.brush(QPalette.Window)
self.initStyleOptionForIndex(opt, logicalIndex)
opt.rect = rect
nBrushButton = opt.palette.brush(QPalette.Button)
nBrushWindow = opt.palette.brush(QPalette.Window)
if oBrushButton != nBrushButton or oBrushWindow != nBrushWindow:
painter.setBrushOrigin(opt.rect.topLeft())
# draw the section
self.style().drawControl(QStyle.CE_Header, opt, painter, self)
painter.setBrushOrigin(oldBO)
class CheckableHeaderView(HeaderView):
"""
A HeaderView with checkable header items.
The header is checkable if the model defines a `Qt.CheckStateRole` value.
"""
__sectionPressed: int = -1
def paintSection(
self, painter: QPainter, rect: QRect, logicalIndex: int
) -> None:
opt = QStyleOptionHeader()
self.initStyleOption(opt)
self.initStyleOptionForIndex(opt, logicalIndex)
model = self.model()
if model is None:
return # pragma: no cover
opt.rect = rect
checkstate = self.sectionCheckState(logicalIndex)
ischeckable = checkstate is not None
style = self.style()
# draw background
style.drawControl(QStyle.CE_HeaderSection, opt, painter, self)
text_rect = QRect(rect)
optindicator = QStyleOptionViewItem()
optindicator.initFrom(self)
optindicator.font = self.font()
optindicator.fontMetrics = opt.fontMetrics
optindicator.features = QStyleOptionViewItem.HasCheckIndicator | QStyleOptionViewItem.HasDisplay
optindicator.rect = opt.rect
indicator_rect = style.subElementRect(
QStyle.SE_ItemViewItemCheckIndicator, optindicator, self)
text_rect.setLeft(indicator_rect.right() + 4)
if ischeckable:
optindicator.checkState = checkstate
optindicator.state |= QStyle.State_On if checkstate == Qt.Checked else QStyle.State_Off
optindicator.rect = indicator_rect
style.drawPrimitive(QStyle.PE_IndicatorItemViewItemCheck, optindicator,
painter, self)
opt.rect = text_rect
# draw section label
style.drawControl(QStyle.CE_HeaderLabel, opt, painter, self)
def mousePressEvent(self, event: QMouseEvent) -> None:
pos = event.pos()
section = self.logicalIndexAt(pos)
if section == -1 or not self.isSectionCheckable(section):
super().mousePressEvent(event)
return
if event.button() == Qt.LeftButton:
opt = self.__viewItemOption(section)
hitrect = self.style().subElementRect(QStyle.SE_ItemViewItemCheckIndicator, opt, self)
if hitrect.contains(pos):
self.__sectionPressed = section
event.accept()
return
super().mousePressEvent(event)
def mouseReleaseEvent(self, event: QMouseEvent) -> None:
pos = event.pos()
section = self.logicalIndexAt(pos)
if section == -1 or not self.isSectionCheckable(section) \
or self.__sectionPressed != section:
super().mouseReleaseEvent(event)
return
if event.button() == Qt.LeftButton:
opt = self.__viewItemOption(section)
hitrect = self.style().subElementRect(QStyle.SE_ItemViewItemCheckIndicator, opt, self)
if hitrect.contains(pos):
state = self.sectionCheckState(section)
newstate = Qt.Checked if state == Qt.Unchecked else Qt.Unchecked
model = self.model()
model.setHeaderData(
section, self.orientation(), newstate, Qt.CheckStateRole)
return
super().mouseReleaseEvent(event)
def isSectionCheckable(self, index: int) -> bool:
model = self.model()
if model is None: # pragma: no cover
return False
checkstate = model.headerData(index, self.orientation(), Qt.CheckStateRole)
return checkstate is not None
def sectionCheckState(self, index: int) -> Qt.CheckState | None:
model = self.model()
if model is None: # pragma: no cover
return None
checkstate = model.headerData(index, self.orientation(), Qt.CheckStateRole)
if checkstate is None:
return None
try:
return Qt.CheckState(checkstate)
except TypeError: # pragma: no cover
return None
def __viewItemOption(self, index: int) -> QStyleOptionViewItem:
opt = QStyleOptionHeader()
self.initStyleOption(opt)
self.initStyleOptionForIndex(opt, index)
pos = self.sectionViewportPosition(index)
size = self.sectionSize(index)
if self.orientation() == Qt.Horizontal:
rect = QRect(pos, 0, size, self.height())
else:
rect = QRect(0, pos, self.width(), size)
optindicator = QStyleOptionViewItem()
optindicator.initFrom(self)
optindicator.rect = rect
optindicator.font = self.font()
optindicator.fontMetrics = opt.fontMetrics
optindicator.features = QStyleOptionViewItem.HasCheckIndicator
if not opt.icon.isNull():
optindicator.icon = opt.icon
optindicator.features |= QStyleOptionViewItem.HasDecoration
return optindicator
def sectionSizeFromContents(self, logicalIndex: int) -> QSize:
style = self.style()
opt = QStyleOptionHeader()
self.initStyleOption(opt)
self.initStyleOptionForIndex(opt, logicalIndex)
sh = style.sizeFromContents(QStyle.CT_HeaderSection, opt,
QSize(), self)
optindicator = QStyleOptionViewItem()
optindicator.initFrom(self)
optindicator.font = self.font()
optindicator.fontMetrics = opt.fontMetrics
optindicator.features = QStyleOptionViewItem.HasCheckIndicator
optindicator.rect = opt.rect
indicator_rect = style.subElementRect(
QStyle.SE_ItemViewItemCheckIndicator, optindicator, self)
return QSize(sh.width() + indicator_rect.width() + 4,
max(sh.height(), indicator_rect.height()))
|