Skip to content

Commit

Permalink
Merge pull request #258 from MoetaYuko/main
Browse files Browse the repository at this point in the history
qt6 migration leftover & wlroots fix
  • Loading branch information
lukas-blecher committed Apr 16, 2023
2 parents 3bd6f9a + f85a9d7 commit 19b332e
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions pix2tex/gui.py
@@ -1,4 +1,6 @@
from shutil import which
import io
import subprocess
import sys
import os
import tempfile
Expand Down Expand Up @@ -54,11 +56,11 @@ def initUI(self):

# Create snip button
if sys.platform == "darwin":
self.snipButton = QPushButton('Snip [Option+S]', self)
self.snipButton.clicked.connect(self.onClick)
self.snipButton = QPushButton('Snip [Option+S]', self)
self.snipButton.clicked.connect(self.onClick)
else:
self.snipButton = QPushButton('Snip [Alt+S]', self)
self.snipButton.clicked.connect(self.onClick)
self.snipButton = QPushButton('Snip [Alt+S]', self)
self.snipButton.clicked.connect(self.onClick)

self.shortcut = QtGui.QShortcut(QtGui.QKeySequence('Alt+S'), self)
self.shortcut.activated.connect(self.onClick)
Expand Down Expand Up @@ -95,21 +97,23 @@ def toggleProcessing(self, value=None):
else:
if sys.platform == "darwin":
text = 'Snip [Option+S]'
else:
else:
text = 'Snip [Alt+S]'
func = self.onClick
self.retryButton.setEnabled(True)
self.shortcut.setEnabled(not self.isProcessing)
self.snipButton.setText(text)
self.snipButton.clicked.disconnect()
self.snipButton.clicked.connect(func)
self.displayPrediction()
self.displayPrediction()

@pyqtSlot()
def onClick(self):
self.close()
if which('gnome-screenshot'):
self.snip_using_gnome_screenshot()
elif which('grim') and which('slurp'):
self.snip_using_grim()
else:
self.snipWidget.snip()

Expand All @@ -123,14 +127,31 @@ def interrupt(self):
def snip_using_gnome_screenshot(self):
try:
with tempfile.NamedTemporaryFile() as tmp:
os.system(f"gnome-screenshot --area --file={tmp.name}")
subprocess.run(["gnome-screenshot", "--area", f"--file={tmp.name}"])
# Use `tmp.name` instead of `tmp.file` due to compatability issues between Pillow and tempfile
self.returnSnip(Image.open(tmp.name))
except:
print(f"Failed to load saved screenshot! Did you cancel the screenshot?")
print("If you don't have gnome-screenshot installed, please install it.")
self.returnSnip()

def snip_using_grim(self):
try:
p = subprocess.run('slurp',
check=True,
capture_output=True,
text=True)
geometry = p.stdout.strip()

p = subprocess.run(['grim', '-g', geometry, '-'],
check=True,
capture_output=True)
self.returnSnip(Image.open(io.BytesIO(p.stdout)))
except:
print(f"Failed to load saved screenshot! Did you cancel the screenshot?")
print("If you don't have slurp and grim installed, please install them.")
self.returnSnip()

def returnSnip(self, img=None):
self.toggleProcessing(True)
self.retryButton.setEnabled(False)
Expand Down Expand Up @@ -160,7 +181,7 @@ def returnPrediction(self, result):
msg = QMessageBox()
msg.setWindowTitle(" ")
msg.setText("Prediction failed.")
msg.exec_()
msg.exec()

def displayPrediction(self, prediction=None):
if self.isProcessing:
Expand Down

0 comments on commit 19b332e

Please sign in to comment.