Start reworking key parsing

This commit is contained in:
Florian Bruhin 2014-01-19 19:41:34 +01:00
parent 3fa18c5599
commit fb54eb58c1
5 changed files with 32 additions and 26 deletions

View File

@ -16,9 +16,10 @@ class QuteBrowser(QApplication):
self.commandparser = cmdutils.CommandParser()
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.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.init_cmds()

View File

@ -4,34 +4,31 @@ from PyQt5.QtGui import QKeySequence
import logging
class KeyParser(QObject):
keyparent = None
keystring = ''
set_cmd_text = pyqtSignal(str)
id_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)
key_to_cmd = {}
def from_cmd_dict(self, d):
for cmd in d.values():
if cmd.key is not None:
logging.debug('registered: {} -> {}'.format(cmd.name, cmd.key))
sc = QShortcut(self.keyparent)
sc.setKey(QKeySequence(cmd.key))
sc.setContext(Qt.WidgetWithChildrenShortcut)
sc.activated.connect(self.handle)
self.id_to_cmd[sc.id()] = cmd
self.key_to_cmd[cmd.key] = cmd
def handle(self):
cmd = self.id_to_cmd[self.sender().id()]
if cmd.nargs and cmd.nargs != 0:
self.set_cmd_text.emit(':{} '.format(cmd.name))
def handle(self, e):
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:
cmd.run()
def handle_empty(self):
self.set_cmd_text.emit(':')
self.keystring = ''
if cmd.nargs and cmd.nargs != 0:
self.set_cmd_text.emit(':{} '.format(cmd.name))
else:
cmd.run()

View File

@ -7,6 +7,7 @@ import logging
class TabbedBrowser(TabWidget):
cur_progress = pyqtSignal(int)
cur_load_finished = pyqtSignal(bool)
keypress = pyqtSignal('QKeyEvent')
url_stack = []
def __init__(self, parent):
@ -126,6 +127,10 @@ class TabbedBrowser(TabWidget):
tab = self.widget(idx)
self.cur_progress.emit(tab.progress)
def keyPressEvent(self, e):
self.keypress.emit(e)
super().keyPressEvent(e)
class BrowserTab(QWebView):
parent = None
progress = 0

View File

@ -20,7 +20,7 @@ class MainWindow(QMainWindow):
self.tabs.setObjectName("tabs")
self.vbox.addWidget(self.tabs)
self.status = StatusBar(self.cwidget)
self.status = StatusBar(self)
self.vbox.addWidget(self.status)
self.tabs.cur_progress.connect(self.status.prog.set_progress)

View File

@ -4,10 +4,12 @@ from PyQt5.QtGui import QValidator, QKeySequence
class StatusBar(QWidget):
has_error = False
parent = None
# TODO: the statusbar should be a bit smaller
def __init__(self, parent):
super().__init__(parent)
self.parent = parent
self.setObjectName(self.__class__.__name__)
self.set_color("white", "black")
self.hbox = QHBoxLayout(self)
@ -117,7 +119,8 @@ class StatusCommand(QLineEdit):
self.esc = QShortcut(self)
self.esc.setKey(QKeySequence(Qt.Key_Escape))
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):
text = self.text().lstrip(':')