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

View File

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

View File

@ -27,7 +27,7 @@ import functools
import sip
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.QtPrintSupport import QPrintPreviewDialog
from PyQt5.QtNetwork import QNetworkReply, QNetworkAccessManager
@ -218,6 +218,7 @@ class TabbedBrowser(TabWidget):
for i in range(count): # pylint: disable=unused-variable
self.currentWidget().forward()
@pyqtSlot(str, int)
def cur_search(self, text, flags):
"""Search for text in the current page.
@ -436,6 +437,7 @@ class TabbedBrowser(TabWidget):
for tabidx in range(self.count()):
self.widget(tabidx).shutdown()
class BrowserTab(QWebView):
"""One browser tab in TabbedBrowser.
@ -457,11 +459,11 @@ class BrowserTab(QWebView):
super().__init__(parent)
self.setPage(BrowserPage())
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().linkHovered.connect(self.linkHovered)
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
def openurl(self, url):
@ -485,7 +487,8 @@ class BrowserTab(QWebView):
else:
return self.load(u)
def link_handler(self, url):
@pyqtSlot(str)
def on_link_clicked(self, url):
"""Handle a link.
Called from the linkClicked signal. Checks if it should open it in a
@ -499,7 +502,8 @@ class BrowserTab(QWebView):
else:
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.
Slot for the loadProgress signal.
@ -515,6 +519,7 @@ class BrowserTab(QWebView):
Inspired by [1].
[1] https://github.com/integricho/path-of-a-pyqter/tree/master/qttut08
"""
self.stop()
self.close()

View File

@ -26,7 +26,7 @@ import html
from PyQt5.QtWidgets import (QTreeView, QStyledItemDelegate, QStyle,
QStyleOptionViewItem, QSizePolicy)
from PyQt5.QtCore import (QRectF, QRect, QPoint, pyqtSignal, Qt,
from PyQt5.QtCore import (pyqtSlot, pyqtSignal, QRectF, QRect, QPoint, Qt,
QItemSelectionModel, QSize)
from PyQt5.QtGui import (QIcon, QPalette, QTextDocument, QTextOption,
QTextCursor)
@ -119,6 +119,7 @@ class CompletionView(QTreeView):
self.expandAll()
self.resizeColumnToContents(0)
@pyqtSlot('QRect')
def resize_to_bar(self, geom):
"""Resize the completion area to the statusbar geometry.
@ -133,6 +134,7 @@ class CompletionView(QTreeView):
assert topleft.y() < bottomright.y()
self.setGeometry(QRect(topleft, bottomright))
@pyqtSlot('QPoint')
def move_to_bar(self, pos):
"""Move the completion area to the statusbar geometry.
@ -142,7 +144,8 @@ class CompletionView(QTreeView):
"""
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.
Slot for the textChanged signal of the statusbar command widget.
@ -167,7 +170,8 @@ class CompletionView(QTreeView):
if self.enabled:
self.show()
def tab_handler(self, shift):
@pyqtSlot(bool)
def on_tab_pressed(self, shift):
"""Handle a tab press for the CompletionView.
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.status.cmd.esc_pressed.connect(self.tabs.setFocus)
self.status.cmd.hide_completion.connect(self.completion.hide)
self.status.cmd.textChanged.connect(self.completion.cmd_text_changed)
self.status.cmd.tab_pressed.connect(self.completion.tab_handler)
self.completion.append_cmd_text.connect(self.status.cmd.append_cmd)
self.status.cmd.textChanged.connect(
self.completion.on_cmd_text_changed)
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.tabWidget.setCurrentIndex(0)

View File

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