Skip to content

Commit

Permalink
fix multi-screen screenshot lukas-blecher#194
Browse files Browse the repository at this point in the history
  • Loading branch information
ficapy committed Apr 17, 2023
1 parent 19b332e commit d4241f7
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pix2tex/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
import os
import tempfile
from PyQt6 import QtCore, QtGui
from PyQt6.QtCore import Qt, pyqtSlot, pyqtSignal, QThread
from PyQt6.QtCore import Qt, pyqtSlot, pyqtSignal, QThread, QTimer
from PyQt6.QtGui import QGuiApplication
from PyQt6.QtWebEngineWidgets import QWebEngineView
from PyQt6.QtWidgets import QMainWindow, QApplication, QMessageBox, QVBoxLayout, QWidget,\
from PyQt6.QtWidgets import QMainWindow, QApplication, QMessageBox, QVBoxLayout, QWidget, \
QPushButton, QTextEdit, QFormLayout, QHBoxLayout, QDoubleSpinBox
from pynput.mouse import Controller

Expand Down Expand Up @@ -251,6 +252,25 @@ def __init__(self, parent):

self.mouse = Controller()

# Create and start the timer
self.factor = QGuiApplication.primaryScreen().devicePixelRatio()
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_geometry_based_on_cursor_position)
self.timer.start(500)

def update_geometry_based_on_cursor_position(self):
if not self.isSnipping:
return

# Update the geometry of the SnipWidget based on the current screen
mouse_pos = QtGui.QCursor.pos()
screen = QGuiApplication.screenAt(mouse_pos)
if screen:
self.factor = screen.devicePixelRatio()
screen_geometry = screen.geometry()
self.setGeometry(screen_geometry)


def snip(self):
self.isSnipping = True
self.setWindowFlags(QtCore.Qt.WindowType.WindowStaysOnTopHint)
Expand All @@ -273,7 +293,7 @@ def paintEvent(self, event):
qp.drawRect(QtCore.QRect(self.begin, self.end))

def keyPressEvent(self, event):
if event.key() == QtCore.Qt.Key_Escape:
if event.key() == QtCore.Qt.Key.Key_Escape.value:
QApplication.restoreOverrideCursor()
self.close()
self.parent.show()
Expand All @@ -296,8 +316,6 @@ def mouseReleaseEvent(self, event):

startPos = self.startPos
endPos = self.mouse.position
# account for retina display. #TODO how to check if device is actually using retina display
factor = 2 if sys.platform == "darwin" else 1

x1 = int(min(startPos[0], endPos[0]))
y1 = int(min(startPos[1], endPos[1]))
Expand All @@ -310,7 +328,8 @@ def mouseReleaseEvent(self, event):
img = ImageGrab.grab(bbox=(x1, y1, x2, y2), all_screens=True)
except Exception as e:
if sys.platform == "darwin":
img = ImageGrab.grab(bbox=(x1//factor, y1//factor, x2//factor, y2//factor), all_screens=True)
img = ImageGrab.grab(bbox=(x1//self.factor, y1//self.factor,
x2//self.factor, y2//self.factor), all_screens=True)
else:
raise e
QApplication.processEvents()
Expand Down

0 comments on commit d4241f7

Please sign in to comment.