Start reworking key parsing
This commit is contained in:
parent
3fa18c5599
commit
fb54eb58c1
qutebrowser
@ -16,9 +16,10 @@ class QuteBrowser(QApplication):
|
|||||||
self.commandparser = cmdutils.CommandParser()
|
self.commandparser = cmdutils.CommandParser()
|
||||||
self.keyparser = KeyParser(self.mainwindow)
|
self.keyparser = KeyParser(self.mainwindow)
|
||||||
|
|
||||||
|
self.mainwindow.tabs.keypress.connect(self.keyparser.handle)
|
||||||
self.keyparser.set_cmd_text.connect(self.mainwindow.status.cmd.set_cmd)
|
self.keyparser.set_cmd_text.connect(self.mainwindow.status.cmd.set_cmd)
|
||||||
self.mainwindow.status.cmd.got_cmd.connect(self.commandparser.parse)
|
self.mainwindow.status.cmd.got_cmd.connect(self.commandparser.parse)
|
||||||
self.mainwindow.status.cmd.got_cmd.connect(self.mainwindow.setFocus)
|
self.mainwindow.status.cmd.got_cmd.connect(self.mainwindow.tabs.setFocus)
|
||||||
self.commandparser.error.connect(self.mainwindow.status.disp_error)
|
self.commandparser.error.connect(self.mainwindow.status.disp_error)
|
||||||
|
|
||||||
self.init_cmds()
|
self.init_cmds()
|
||||||
|
@ -4,34 +4,31 @@ from PyQt5.QtGui import QKeySequence
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
class KeyParser(QObject):
|
class KeyParser(QObject):
|
||||||
keyparent = None
|
keystring = ''
|
||||||
set_cmd_text = pyqtSignal(str)
|
set_cmd_text = pyqtSignal(str)
|
||||||
id_to_cmd = {}
|
key_to_cmd = {}
|
||||||
|
|
||||||
def __init__(self, keyparent):
|
|
||||||
super().__init__()
|
|
||||||
self.keyparent = keyparent
|
|
||||||
sc = QShortcut(keyparent)
|
|
||||||
sc.setKey(QKeySequence(':'))
|
|
||||||
sc.setContext(Qt.WidgetWithChildrenShortcut)
|
|
||||||
sc.activated.connect(self.handle_empty)
|
|
||||||
|
|
||||||
def from_cmd_dict(self, d):
|
def from_cmd_dict(self, d):
|
||||||
for cmd in d.values():
|
for cmd in d.values():
|
||||||
if cmd.key is not None:
|
if cmd.key is not None:
|
||||||
logging.debug('registered: {} -> {}'.format(cmd.name, cmd.key))
|
logging.debug('registered: {} -> {}'.format(cmd.name, cmd.key))
|
||||||
sc = QShortcut(self.keyparent)
|
self.key_to_cmd[cmd.key] = cmd
|
||||||
sc.setKey(QKeySequence(cmd.key))
|
|
||||||
sc.setContext(Qt.WidgetWithChildrenShortcut)
|
|
||||||
sc.activated.connect(self.handle)
|
|
||||||
self.id_to_cmd[sc.id()] = cmd
|
|
||||||
|
|
||||||
def handle(self):
|
def handle(self, e):
|
||||||
cmd = self.id_to_cmd[self.sender().id()]
|
logging.debug('Got key: {} / text: {}'.format(e.key(), e.text()))
|
||||||
|
self.keystring += e.text()
|
||||||
|
if self.keystring == ':':
|
||||||
|
self.set_cmd_text.emit(':')
|
||||||
|
self.keystring = ''
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
cmd = self.key_to_cmd[self.keystring]
|
||||||
|
except KeyError:
|
||||||
|
logging.debug('No match for "{}" (added {})'.format(self.keystring, e.text()))
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
self.keystring = ''
|
||||||
if cmd.nargs and cmd.nargs != 0:
|
if cmd.nargs and cmd.nargs != 0:
|
||||||
self.set_cmd_text.emit(':{} '.format(cmd.name))
|
self.set_cmd_text.emit(':{} '.format(cmd.name))
|
||||||
else:
|
else:
|
||||||
cmd.run()
|
cmd.run()
|
||||||
|
|
||||||
def handle_empty(self):
|
|
||||||
self.set_cmd_text.emit(':')
|
|
||||||
|
@ -7,6 +7,7 @@ import logging
|
|||||||
class TabbedBrowser(TabWidget):
|
class TabbedBrowser(TabWidget):
|
||||||
cur_progress = pyqtSignal(int)
|
cur_progress = pyqtSignal(int)
|
||||||
cur_load_finished = pyqtSignal(bool)
|
cur_load_finished = pyqtSignal(bool)
|
||||||
|
keypress = pyqtSignal('QKeyEvent')
|
||||||
url_stack = []
|
url_stack = []
|
||||||
|
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
@ -126,6 +127,10 @@ class TabbedBrowser(TabWidget):
|
|||||||
tab = self.widget(idx)
|
tab = self.widget(idx)
|
||||||
self.cur_progress.emit(tab.progress)
|
self.cur_progress.emit(tab.progress)
|
||||||
|
|
||||||
|
def keyPressEvent(self, e):
|
||||||
|
self.keypress.emit(e)
|
||||||
|
super().keyPressEvent(e)
|
||||||
|
|
||||||
class BrowserTab(QWebView):
|
class BrowserTab(QWebView):
|
||||||
parent = None
|
parent = None
|
||||||
progress = 0
|
progress = 0
|
||||||
|
@ -20,7 +20,7 @@ class MainWindow(QMainWindow):
|
|||||||
self.tabs.setObjectName("tabs")
|
self.tabs.setObjectName("tabs")
|
||||||
self.vbox.addWidget(self.tabs)
|
self.vbox.addWidget(self.tabs)
|
||||||
|
|
||||||
self.status = StatusBar(self.cwidget)
|
self.status = StatusBar(self)
|
||||||
self.vbox.addWidget(self.status)
|
self.vbox.addWidget(self.status)
|
||||||
|
|
||||||
self.tabs.cur_progress.connect(self.status.prog.set_progress)
|
self.tabs.cur_progress.connect(self.status.prog.set_progress)
|
||||||
|
@ -4,10 +4,12 @@ from PyQt5.QtGui import QValidator, QKeySequence
|
|||||||
|
|
||||||
class StatusBar(QWidget):
|
class StatusBar(QWidget):
|
||||||
has_error = False
|
has_error = False
|
||||||
|
parent = None
|
||||||
|
|
||||||
# TODO: the statusbar should be a bit smaller
|
# TODO: the statusbar should be a bit smaller
|
||||||
def __init__(self, parent):
|
def __init__(self, parent):
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
|
self.parent = parent
|
||||||
self.setObjectName(self.__class__.__name__)
|
self.setObjectName(self.__class__.__name__)
|
||||||
self.set_color("white", "black")
|
self.set_color("white", "black")
|
||||||
self.hbox = QHBoxLayout(self)
|
self.hbox = QHBoxLayout(self)
|
||||||
@ -117,7 +119,8 @@ class StatusCommand(QLineEdit):
|
|||||||
self.esc = QShortcut(self)
|
self.esc = QShortcut(self)
|
||||||
self.esc.setKey(QKeySequence(Qt.Key_Escape))
|
self.esc.setKey(QKeySequence(Qt.Key_Escape))
|
||||||
self.esc.setContext(Qt.WidgetWithChildrenShortcut)
|
self.esc.setContext(Qt.WidgetWithChildrenShortcut)
|
||||||
self.esc.activated.connect(parent.setFocus)
|
# FIXME this is fugly and doesn't clear the keystring
|
||||||
|
self.esc.activated.connect(parent.parent.tabs.setFocus)
|
||||||
|
|
||||||
def process_cmd(self):
|
def process_cmd(self):
|
||||||
text = self.text().lstrip(':')
|
text = self.text().lstrip(':')
|
||||||
|
Loading…
Reference in New Issue
Block a user