Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/global hotkey and add to clipboard #327

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions pix2tex/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from PyQt6.QtWidgets import QMainWindow, QApplication, QMessageBox, QVBoxLayout, QWidget, \
QPushButton, QTextEdit, QFormLayout, QHBoxLayout, QDoubleSpinBox
from pynput.mouse import Controller
from pynput.keyboard import Listener, HotKey

from PIL import ImageGrab, Image
import numpy as np
Expand All @@ -21,13 +22,16 @@
import pix2tex.resources.resources
class App(QMainWindow):
isProcessing = False
hotkey_signal = pyqtSignal()

def __init__(self, args=None):
super().__init__()
self.args = args
self.model = cli.LatexOCR(self.args)
self.initUI()
self.snipWidget = SnipWidget(self)
self.hotkey_signal.connect(self.onClick)
self.clipboard = QApplication.clipboard()
self.show()

def initUI(self):
Expand Down Expand Up @@ -63,8 +67,6 @@ def initUI(self):
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)

# Create retry button
self.retryButton = QPushButton('Retry', self)
Expand All @@ -86,6 +88,23 @@ def initUI(self):
settings = QFormLayout()
settings.addRow('Temperature:', self.tempField)
lay.addLayout(settings)

def hotkey(self):
def on_activate():
if not self.isProcessing:
self.hotkey_signal.emit()

def for_canonical(f):
return lambda k: f(l.canonical(k))

hotkey = HotKey(
HotKey.parse('<alt>+s'),
on_activate)
l = Listener(
on_press=for_canonical(hotkey.press),
on_release=for_canonical(hotkey.release)
)
l.start()

def toggleProcessing(self, value=None):
if value is None:
Expand All @@ -102,7 +121,6 @@ def toggleProcessing(self, value=None):
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)
Expand Down Expand Up @@ -183,6 +201,8 @@ def returnPrediction(self, result):
if success:
self.displayPrediction(prediction)
self.retryButton.setEnabled(True)
self.clipboard.setText(prediction)

else:
self.webView.setHtml("")
msg = QMessageBox()
Expand Down Expand Up @@ -351,4 +371,5 @@ def main(arguments):
os.environ['QTWEBENGINE_DISABLE_SANDBOX'] = '1'
app = QApplication(sys.argv)
ex = App(arguments)
ex.hotkey()
sys.exit(app.exec())