Improve ScreenshotOverlay paintEvent to enhance selection rectangle rendering and dimmed background
This commit is contained in:
@@ -109,22 +109,32 @@ class ScreenshotOverlay(QtWidgets.QWidget):
|
|||||||
|
|
||||||
def paintEvent(self, event):
|
def paintEvent(self, event):
|
||||||
qp = QtGui.QPainter(self)
|
qp = QtGui.QPainter(self)
|
||||||
# Draw dimmed background
|
if not self._bg:
|
||||||
if self._bg:
|
return
|
||||||
qp.drawPixmap(0, 0, self._bg)
|
|
||||||
qp.setBrush(QtGui.QColor(0, 0, 0, 120))
|
# Draw the background screenshot first.
|
||||||
qp.setPen(QtCore.Qt.PenStyle.NoPen)
|
qp.drawPixmap(0, 0, self._bg)
|
||||||
qp.drawRect(self.geometry())
|
|
||||||
# Draw selection rectangle
|
# Prepare a path for the dimmed overlay.
|
||||||
|
dim_path = QtGui.QPainterPath()
|
||||||
|
dim_path.addRect(QtCore.QRectF(self.geometry()))
|
||||||
|
|
||||||
|
# If a selection is being made, subtract it from the dimmed path.
|
||||||
if self.start and self.end:
|
if self.start and self.end:
|
||||||
sel_rect = QtCore.QRect(self.start, self.end).normalized()
|
selection_rect = QtCore.QRect(self.start, self.end).normalized()
|
||||||
|
selection_path = QtGui.QPainterPath()
|
||||||
|
selection_path.addRect(QtCore.QRectF(selection_rect))
|
||||||
|
dim_path = dim_path.subtracted(selection_path)
|
||||||
|
|
||||||
|
# Draw the border of the selection.
|
||||||
qp.setBrush(QtCore.Qt.BrushStyle.NoBrush)
|
qp.setBrush(QtCore.Qt.BrushStyle.NoBrush)
|
||||||
qp.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0), 2))
|
qp.setPen(QtGui.QPen(QtGui.QColor(0, 255, 0), 2))
|
||||||
qp.drawRect(sel_rect)
|
qp.drawRect(selection_rect)
|
||||||
# Optionally, highlight the selected area
|
|
||||||
qp.setBrush(QtGui.QColor(0, 255, 0, 40))
|
# Fill the dimmed area (the path that excludes the selection).
|
||||||
qp.setPen(QtCore.Qt.PenStyle.NoPen)
|
qp.setBrush(QtGui.QColor(0, 0, 0, 120))
|
||||||
qp.drawRect(sel_rect)
|
qp.setPen(QtCore.Qt.PenStyle.NoPen)
|
||||||
|
qp.drawPath(dim_path)
|
||||||
|
|
||||||
class MainWindow(QtWidgets.QMainWindow):
|
class MainWindow(QtWidgets.QMainWindow):
|
||||||
def automation_loop(self):
|
def automation_loop(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user