Skip to content

Commit

Permalink
Merge pull request #260 from ficapy/main
Browse files Browse the repository at this point in the history
fix multi-screen screenshot #194
  • Loading branch information
lukas-blecher committed Apr 17, 2023
2 parents 19b332e + d4241f7 commit 2edd957
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions pix2tex/gui.py
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 2edd957

Please sign in to comment.