Decorate slots with pyqtSlot.

This seems to reduce memory usage a bit and make things a bit faster.
This commit is contained in:
Florian Bruhin 2014-02-17 12:00:08 +01:00
parent 0abb5cf738
commit d5807169d1
6 changed files with 55 additions and 27 deletions

View File

@ -40,7 +40,7 @@ import qutebrowser.utils.harfbuzz as harfbuzz
harfbuzz.fix() harfbuzz.fix()
from PyQt5.QtWidgets import QApplication, QDialog from PyQt5.QtWidgets import QApplication, QDialog
from PyQt5.QtCore import QTimer from PyQt5.QtCore import pyqtSlot, QTimer
import qutebrowser import qutebrowser
import qutebrowser.commands.utils as cmdutils import qutebrowser.commands.utils as cmdutils
@ -96,9 +96,10 @@ class QuteBrowser(QApplication):
self.aboutToQuit.connect(self._shutdown) self.aboutToQuit.connect(self._shutdown)
self.mainwindow.tabs.keypress.connect(self.keyparser.handle) 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.on_set_cmd_text)
self.mainwindow.tabs.set_cmd_text.connect( self.mainwindow.tabs.set_cmd_text.connect(
self.mainwindow.status.cmd.set_cmd) self.mainwindow.status.cmd.on_set_cmd_text)
self.mainwindow.status.cmd.got_cmd.connect(self.commandparser.run) self.mainwindow.status.cmd.got_cmd.connect(self.commandparser.run)
self.mainwindow.status.cmd.got_search.connect(self.searchparser.search) self.mainwindow.status.cmd.got_search.connect(self.searchparser.search)
self.mainwindow.status.cmd.got_search_rev.connect( self.mainwindow.status.cmd.got_search_rev.connect(
@ -244,11 +245,13 @@ class QuteBrowser(QApplication):
except KeyError: except KeyError:
pass pass
@pyqtSlot()
def _shutdown(self): def _shutdown(self):
"""Try to shutdown everything cleanly.""" """Try to shutdown everything cleanly."""
config.config.save() config.config.save()
self.mainwindow.tabs.shutdown() self.mainwindow.tabs.shutdown()
@pyqtSlot(tuple)
def cmd_handler(self, tpl): def cmd_handler(self, tpl):
"""Handle commands and delegate the specific actions. """Handle commands and delegate the specific actions.

View File

@ -20,7 +20,7 @@
import inspect import inspect
import shlex import shlex
from PyQt5.QtCore import QObject, pyqtSignal from PyQt5.QtCore import QObject, pyqtSlot, pyqtSignal
from PyQt5.QtWebKitWidgets import QWebPage from PyQt5.QtWebKitWidgets import QWebPage
import qutebrowser.commands.commands import qutebrowser.commands.commands
@ -55,6 +55,7 @@ class SearchParser(QObject):
flags = 0 flags = 0
do_search = pyqtSignal(str, 'QWebPage::FindFlags') do_search = pyqtSignal(str, 'QWebPage::FindFlags')
@pyqtSlot(str)
def search(self, text): def search(self, text):
"""Search for a text on a website. """Search for a text on a website.
@ -63,6 +64,7 @@ class SearchParser(QObject):
""" """
self._search(text) self._search(text)
@pyqtSlot(str)
def search_rev(self, text): def search_rev(self, text):
"""Search for a text on a website in reverse direction. """Search for a text on a website in reverse direction.
@ -142,6 +144,7 @@ class CommandParser(QObject):
else: else:
self.cmd.run(self.args) self.cmd.run(self.args)
@pyqtSlot(str, int, bool)
def run(self, text, count=None, ignore_exc=True): def run(self, text, count=None, ignore_exc=True):
"""Parse a command from a line of text. """Parse a command from a line of text.

View File

@ -27,7 +27,7 @@ import functools
import sip import sip
from PyQt5.QtWidgets import QShortcut, QApplication, QSizePolicy from PyQt5.QtWidgets import QShortcut, QApplication, QSizePolicy
from PyQt5.QtCore import pyqtSignal, Qt, QEvent from PyQt5.QtCore import pyqtSignal, pyqtSlot, Qt, QEvent
from PyQt5.QtGui import QClipboard from PyQt5.QtGui import QClipboard
from PyQt5.QtPrintSupport import QPrintPreviewDialog from PyQt5.QtPrintSupport import QPrintPreviewDialog
from PyQt5.QtNetwork import QNetworkReply, QNetworkAccessManager from PyQt5.QtNetwork import QNetworkReply, QNetworkAccessManager
@ -218,6 +218,7 @@ class TabbedBrowser(TabWidget):
for i in range(count): # pylint: disable=unused-variable for i in range(count): # pylint: disable=unused-variable
self.currentWidget().forward() self.currentWidget().forward()
@pyqtSlot(str, int)
def cur_search(self, text, flags): def cur_search(self, text, flags):
"""Search for text in the current page. """Search for text in the current page.
@ -436,6 +437,7 @@ class TabbedBrowser(TabWidget):
for tabidx in range(self.count()): for tabidx in range(self.count()):
self.widget(tabidx).shutdown() self.widget(tabidx).shutdown()
class BrowserTab(QWebView): class BrowserTab(QWebView):
"""One browser tab in TabbedBrowser. """One browser tab in TabbedBrowser.
@ -457,11 +459,11 @@ class BrowserTab(QWebView):
super().__init__(parent) super().__init__(parent)
self.setPage(BrowserPage()) self.setPage(BrowserPage())
self.signal_cache = SignalCache(uncached=['linkHovered']) self.signal_cache = SignalCache(uncached=['linkHovered'])
self.loadProgress.connect(self.set_progress) self.loadProgress.connect(self.on_load_progress)
self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks) self.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
self.page().linkHovered.connect(self.linkHovered) self.page().linkHovered.connect(self.linkHovered)
self.installEventFilter(self) self.installEventFilter(self)
self.linkClicked.connect(self.link_handler) self.linkClicked.connect(self.on_link_clicked)
# FIXME find some way to hide scrollbars without setScrollBarPolicy # FIXME find some way to hide scrollbars without setScrollBarPolicy
def openurl(self, url): def openurl(self, url):
@ -485,7 +487,8 @@ class BrowserTab(QWebView):
else: else:
return self.load(u) return self.load(u)
def link_handler(self, url): @pyqtSlot(str)
def on_link_clicked(self, url):
"""Handle a link. """Handle a link.
Called from the linkClicked signal. Checks if it should open it in a Called from the linkClicked signal. Checks if it should open it in a
@ -499,7 +502,8 @@ class BrowserTab(QWebView):
else: else:
self.openurl(url) self.openurl(url)
def set_progress(self, prog): @pyqtSlot(int)
def on_load_progress(self, prog):
"""Update the progress property if the loading progress changed. """Update the progress property if the loading progress changed.
Slot for the loadProgress signal. Slot for the loadProgress signal.
@ -515,6 +519,7 @@ class BrowserTab(QWebView):
Inspired by [1]. Inspired by [1].
[1] https://github.com/integricho/path-of-a-pyqter/tree/master/qttut08 [1] https://github.com/integricho/path-of-a-pyqter/tree/master/qttut08
""" """
self.stop() self.stop()
self.close() self.close()

View File

@ -26,7 +26,7 @@ import html
from PyQt5.QtWidgets import (QTreeView, QStyledItemDelegate, QStyle, from PyQt5.QtWidgets import (QTreeView, QStyledItemDelegate, QStyle,
QStyleOptionViewItem, QSizePolicy) QStyleOptionViewItem, QSizePolicy)
from PyQt5.QtCore import (QRectF, QRect, QPoint, pyqtSignal, Qt, from PyQt5.QtCore import (pyqtSlot, pyqtSignal, QRectF, QRect, QPoint, Qt,
QItemSelectionModel, QSize) QItemSelectionModel, QSize)
from PyQt5.QtGui import (QIcon, QPalette, QTextDocument, QTextOption, from PyQt5.QtGui import (QIcon, QPalette, QTextDocument, QTextOption,
QTextCursor) QTextCursor)
@ -119,6 +119,7 @@ class CompletionView(QTreeView):
self.expandAll() self.expandAll()
self.resizeColumnToContents(0) self.resizeColumnToContents(0)
@pyqtSlot('QRect')
def resize_to_bar(self, geom): def resize_to_bar(self, geom):
"""Resize the completion area to the statusbar geometry. """Resize the completion area to the statusbar geometry.
@ -133,6 +134,7 @@ class CompletionView(QTreeView):
assert topleft.y() < bottomright.y() assert topleft.y() < bottomright.y()
self.setGeometry(QRect(topleft, bottomright)) self.setGeometry(QRect(topleft, bottomright))
@pyqtSlot('QPoint')
def move_to_bar(self, pos): def move_to_bar(self, pos):
"""Move the completion area to the statusbar geometry. """Move the completion area to the statusbar geometry.
@ -142,7 +144,8 @@ class CompletionView(QTreeView):
""" """
self.move(pos - self.height) self.move(pos - self.height)
def cmd_text_changed(self, text): @pyqtSlot(str)
def on_cmd_text_changed(self, text):
"""Check if completions are available and activate them. """Check if completions are available and activate them.
Slot for the textChanged signal of the statusbar command widget. Slot for the textChanged signal of the statusbar command widget.
@ -167,7 +170,8 @@ class CompletionView(QTreeView):
if self.enabled: if self.enabled:
self.show() self.show()
def tab_handler(self, shift): @pyqtSlot(bool)
def on_tab_pressed(self, shift):
"""Handle a tab press for the CompletionView. """Handle a tab press for the CompletionView.
Select the previous/next item and write the new text to the Select the previous/next item and write the new text to the

View File

@ -71,9 +71,11 @@ class MainWindow(QWidget):
self.tabs.cur_link_hovered.connect(self.status.url.set_hover_url) self.tabs.cur_link_hovered.connect(self.status.url.set_hover_url)
self.status.cmd.esc_pressed.connect(self.tabs.setFocus) self.status.cmd.esc_pressed.connect(self.tabs.setFocus)
self.status.cmd.hide_completion.connect(self.completion.hide) self.status.cmd.hide_completion.connect(self.completion.hide)
self.status.cmd.textChanged.connect(self.completion.cmd_text_changed) self.status.cmd.textChanged.connect(
self.status.cmd.tab_pressed.connect(self.completion.tab_handler) self.completion.on_cmd_text_changed)
self.completion.append_cmd_text.connect(self.status.cmd.append_cmd) self.status.cmd.tab_pressed.connect(self.completion.on_tab_pressed)
self.completion.append_cmd_text.connect(
self.status.cmd.on_append_cmd_text)
#self.retranslateUi(MainWindow) #self.retranslateUi(MainWindow)
#self.tabWidget.setCurrentIndex(0) #self.tabWidget.setCurrentIndex(0)

View File

@ -19,7 +19,7 @@
import logging import logging
from PyQt5.QtCore import pyqtSignal, Qt, pyqtProperty from PyQt5.QtCore import pyqtSignal, pyqtSlot, pyqtProperty, Qt
from PyQt5.QtWidgets import (QLineEdit, QShortcut, QHBoxLayout, QWidget, from PyQt5.QtWidgets import (QLineEdit, QShortcut, QHBoxLayout, QWidget,
QSizePolicy, QProgressBar, QLabel, QStyle, QSizePolicy, QProgressBar, QLabel, QStyle,
QStyleOption) QStyleOption)
@ -117,6 +117,7 @@ class StatusBar(QWidget):
self.style().drawPrimitive(QStyle.PE_Widget, self._option, self.style().drawPrimitive(QStyle.PE_Widget, self._option,
painter, self) painter, self)
@pyqtSlot(str)
def disp_error(self, text): def disp_error(self, text):
"""Displaysan error in the statusbar.""" """Displaysan error in the statusbar."""
self.error = True self.error = True
@ -180,14 +181,14 @@ class Command(QLineEdit):
} }
""") """)
self.setValidator(CommandValidator()) self.setValidator(CommandValidator())
self.returnPressed.connect(self.process_cmdline) self.returnPressed.connect(self.on_return_pressed)
self.textEdited.connect(self._histbrowse_stop) self.textEdited.connect(self._histbrowse_stop)
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Ignored) self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Ignored)
for (key, handler) in [ for (key, handler) in [
(Qt.Key_Escape, self.esc_pressed), (Qt.Key_Escape, self.esc_pressed),
(Qt.Key_Up, self.key_up_handler), (Qt.Key_Up, self.on_key_up_pressed),
(Qt.Key_Down, self.key_down_handler), (Qt.Key_Down, self.on_key_down_pressed),
(Qt.Key_Tab | Qt.SHIFT, lambda: self.tab_pressed.emit(True)), (Qt.Key_Tab | Qt.SHIFT, lambda: self.tab_pressed.emit(True)),
(Qt.Key_Tab, lambda: self.tab_pressed.emit(False)) (Qt.Key_Tab, lambda: self.tab_pressed.emit(False))
]: ]:
@ -197,7 +198,8 @@ class Command(QLineEdit):
sc.activated.connect(handler) sc.activated.connect(handler)
self._shortcuts.append(sc) self._shortcuts.append(sc)
def process_cmdline(self): @pyqtSlot()
def on_return_pressed(self):
"""Handle the command in the status bar.""" """Handle the command in the status bar."""
signals = { signals = {
':': self.got_cmd, ':': self.got_cmd,
@ -212,12 +214,14 @@ class Command(QLineEdit):
if text[0] in signals: if text[0] in signals:
signals[text[0]].emit(text.lstrip(text[0])) signals[text[0]].emit(text.lstrip(text[0]))
def set_cmd(self, text): @pyqtSlot(str)
def on_set_cmd_text(self, text):
"""Preset the statusbar to some text.""" """Preset the statusbar to some text."""
self.setText(text) self.setText(text)
self.setFocus() self.setFocus()
def append_cmd(self, text): @pyqtSlot(str)
def on_append_cmd_text(self, text):
"""Append text to the commandline.""" """Append text to the commandline."""
# FIXME do the right thing here # FIXME do the right thing here
self.setText(':' + text) self.setText(':' + text)
@ -252,11 +256,13 @@ class Command(QLineEdit):
self._tmphist = self.history self._tmphist = self.history
self._histpos = len(self._tmphist) - 1 self._histpos = len(self._tmphist) - 1
@pyqtSlot()
def _histbrowse_stop(self): def _histbrowse_stop(self):
"""Stop browsing the history.""" """Stop browsing the history."""
self._histpos = None self._histpos = None
def key_up_handler(self): @pyqtSlot()
def on_key_up_pressed(self):
"""Handle Up presses (go back in history).""" """Handle Up presses (go back in history)."""
logging.debug("history up [pre]: pos {}".format(self._histpos)) logging.debug("history up [pre]: pos {}".format(self._histpos))
if self._histpos is None: if self._histpos is None:
@ -271,7 +277,8 @@ class Command(QLineEdit):
self._tmphist, len(self._tmphist), self._histpos)) self._tmphist, len(self._tmphist), self._histpos))
self.set_cmd(self._tmphist[self._histpos]) self.set_cmd(self._tmphist[self._histpos])
def key_down_handler(self): @pyqtSlot()
def on_key_down_pressed(self):
"""Handle Down presses (go forward in history).""" """Handle Down presses (go forward in history)."""
logging.debug("history up [pre]: pos {}".format(self._histpos, logging.debug("history up [pre]: pos {}".format(self._histpos,
self._tmphist, len(self._tmphist), self._histpos)) self._tmphist, len(self._tmphist), self._histpos))
@ -331,6 +338,7 @@ class Progress(QProgressBar):
self.setTextVisible(False) self.setTextVisible(False)
self.hide() self.hide()
@pyqtSlot()
def on_load_started(self): def on_load_started(self):
"""Clear old error and show progress, used as slot to loadStarted.""" """Clear old error and show progress, used as slot to loadStarted."""
self.setValue(0) self.setValue(0)
@ -413,9 +421,9 @@ class Percentage(TextBase):
"""Reading percentage displayed in the statusbar.""" """Reading percentage displayed in the statusbar."""
def set_perc(self, x, y): @pyqtSlot(int, int)
def set_perc(self, _, y):
"""Setter to be used as a Qt slot.""" """Setter to be used as a Qt slot."""
# pylint: disable=unused-argument
if y == 0: if y == 0:
self.setText('[top]') self.setText('[top]')
elif y == 100: elif y == 100:
@ -472,16 +480,20 @@ class Url(TextBase):
self._urltype = val self._urltype = val
self.setStyleSheet(config.get_stylesheet(self._stylesheet)) self.setStyleSheet(config.get_stylesheet(self._stylesheet))
@pyqtSlot(bool)
def on_loading_finished(self, ok): def on_loading_finished(self, ok):
"""Slot for cur_loading_finished. Colors the URL according to ok.""" """Slot for cur_loading_finished. Colors the URL according to ok."""
# FIXME: set color to warn if there was an SSL error # FIXME: set color to warn if there was an SSL error
self.urltype = 'success' if ok else 'error' self.urltype = 'success' if ok else 'error'
@pyqtSlot(str)
def set_url(self, s): def set_url(self, s):
"""Setter to be used as a Qt slot.""" """Setter to be used as a Qt slot."""
self.setText(urlstring(s)) self.setText(urlstring(s))
self.urltype = 'normal' self.urltype = 'normal'
# pylint: disable=unused-argument
@pyqtSlot(str, str, str)
def set_hover_url(self, link, title, text): def set_hover_url(self, link, title, text):
"""Setter to be used as a Qt slot. """Setter to be used as a Qt slot.
@ -489,7 +501,6 @@ class Url(TextBase):
"un-hovered" when it gets called with empty parameters. "un-hovered" when it gets called with empty parameters.
""" """
# pylint: disable=unused-argument
if link: if link:
if self._old_url is None: if self._old_url is None:
self._old_url = self.text() self._old_url = self.text()