Skip to content

Commit

Permalink
for QtPainter use with context
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerum committed Nov 21, 2024
1 parent cd769c7 commit 23fe506
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 41 deletions.
40 changes: 20 additions & 20 deletions saenopy/gui/code/code_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,24 +82,24 @@ def highlightCurrentLine(self):
self.setExtraSelections(extraSelections)

def lineNumberAreaPaintEvent(self, event):
painter = QtGui.QPainter(self.lineNumberArea)
painter.fillRect(event.rect(), lineNumbersBackgroundColor)
block = self.firstVisibleBlock()
blockNumber = block.blockNumber()
top = round(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
bottom = top + round(self.blockBoundingRect(block).height())

while block.isValid() and top <= event.rect().bottom():
if block.isVisible() and bottom >= event.rect().top():
number = str(blockNumber + 1)
if blockNumber == self.textCursor().blockNumber():
painter.fillRect(QtCore.QRect(0, top, self.lineNumberArea.width()+10, self.fontMetrics().height()), lineColor)

painter.setPen(QtGui.QColor(lineNumbersTextColor))
painter.drawText(0, top, self.lineNumberArea.width()-lineNumbersRightMargin, self.fontMetrics().height(),
QtCore.Qt.AlignRight, number)

block = block.next()
top = bottom
with QtGui.QPainter(self.lineNumberArea) as painter:
painter.fillRect(event.rect(), lineNumbersBackgroundColor)
block = self.firstVisibleBlock()
blockNumber = block.blockNumber()
top = round(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
bottom = top + round(self.blockBoundingRect(block).height())
blockNumber += 1

while block.isValid() and top <= event.rect().bottom():
if block.isVisible() and bottom >= event.rect().top():
number = str(blockNumber + 1)
if blockNumber == self.textCursor().blockNumber():
painter.fillRect(QtCore.QRect(0, top, self.lineNumberArea.width()+10, self.fontMetrics().height()), lineColor)

painter.setPen(QtGui.QColor(lineNumbersTextColor))
painter.drawText(0, top, self.lineNumberArea.width()-lineNumbersRightMargin, self.fontMetrics().height(),
QtCore.Qt.AlignRight, number)

block = block.next()
top = bottom
bottom = top + round(self.blockBoundingRect(block).height())
blockNumber += 1
8 changes: 4 additions & 4 deletions saenopy/gui/common/QtShortCuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,10 @@ def paintEvent(self, event):

opt = QtWidgets.QStyleOption()
opt.initFrom(self)
painter = QtGui.QPainter(self)
with QtGui.QPainter(self) as painter:

self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)
self.style().drawPrimitive(QtWidgets.QStyle.PE_Widget, opt, painter, self)

self.style().drawItemText(painter, self.rect(),
self.textalignment, self.palette(), True, self.text())
self.style().drawItemText(painter, self.rect(),
self.textalignment, self.palette(), True, self.text())

16 changes: 8 additions & 8 deletions saenopy/gui/common/gui_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,14 @@ def __init__(self, parent=None, title='', animationDuration=300, url=None):
self.toggleButton.valueChanged.connect(self.changedActive)

def paintEvent(self, ev: QtGui.QPaintEvent) -> None:
p = QtGui.QPainter(self)
p.setPen(QtGui.QPen(QtGui.QColor("gray")))
#p.setBrush(QtGui.QBrush(QtGui.QColor("gray")))
top = 5
p.drawRect(0, self.height()-1, self.width(), 0)
p.drawRect(0, top, 0, self.height())
p.drawRect(0, top, 7, 0)
p.drawRect(self.width()-1, top, 0, self.height())
with QtGui.QPainter(self) as p:
p.setPen(QtGui.QPen(QtGui.QColor("gray")))
#p.setBrush(QtGui.QBrush(QtGui.QColor("gray")))
top = 5
p.drawRect(0, self.height()-1, self.width(), 0)
p.drawRect(0, top, 0, self.height())
p.drawRect(0, top, 7, 0)
p.drawRect(self.width()-1, top, 0, self.height())
super().paintEvent(ev)

def toggle(self):
Expand Down
18 changes: 9 additions & 9 deletions saenopy/gui/spheroid/modules/QSlider.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ def paintEvent(self, ev: QtGui.QPaintEvent) -> None:
super().paintEvent(ev)

def drawRect(self, start, end, color, border):
p = QtGui.QPainter(self)
p.setPen(QtGui.QPen(QtGui.QColor("transparent")))
p.setBrush(QtGui.QBrush(QtGui.QColor(color)))

if (self.min is not None) and (end != "None") and (start != "None"):
s = self.width() * (start - self.minimum()) / (self.maximum() - self.minimum() + 1e-5)
e = self.width() * (end - self.minimum()) / (self.maximum() - self.minimum() + 1e-5)
p.drawRect(int(s), int(self.height() * border),
int(e - s), int(self.height() * (1 - border * 2)))
with QtGui.QPainter(self) as p:
p.setPen(QtGui.QPen(QtGui.QColor("transparent")))
p.setBrush(QtGui.QBrush(QtGui.QColor(color)))

if (self.min is not None) and (end != "None") and (start != "None"):
s = self.width() * (start - self.minimum()) / (self.maximum() - self.minimum() + 1e-5)
e = self.width() * (end - self.minimum()) / (self.maximum() - self.minimum() + 1e-5)
p.drawRect(int(s), int(self.height() * border),
int(e - s), int(self.height() * (1 - border * 2)))

def setEvaluated(self, value):
self.evaluated = value
Expand Down

0 comments on commit 23fe506

Please sign in to comment.