qutebrowser/qutebrowser/widgets/browser.py

128 lines
3.3 KiB
Python
Raw Normal View History

2014-01-17 12:24:38 +01:00
from PyQt5.QtCore import QObject, pyqtSlot, QUrl, pyqtSignal
from PyQt5.QtPrintSupport import QPrintPreviewDialog
2013-12-15 21:40:15 +01:00
from PyQt5.QtWebKitWidgets import QWebView
from qutebrowser.widgets.tabbar import TabWidget
2014-01-17 20:29:20 +01:00
import logging
2013-12-15 21:40:15 +01:00
class TabbedBrowser(TabWidget):
tabs = []
2014-01-17 12:24:38 +01:00
cur_progress = pyqtSignal(int)
2013-12-15 21:40:15 +01:00
def __init__(self, parent):
super().__init__(parent)
2014-01-17 13:16:13 +01:00
self.currentChanged.connect(self.index_changed)
2013-12-15 21:40:15 +01:00
self.tabopen("http://ddg.gg/")
@pyqtSlot(str)
def tabopen(self, url):
tab = BrowserTab(self)
tab.openurl(url)
self.tabs.append(tab)
self.addTab(tab, url)
2013-12-16 22:07:11 +01:00
self.setCurrentWidget(tab)
2014-01-17 20:29:07 +01:00
self.progress_changed(tab.progress)
tab.loadProgress.connect(self.progress_changed)
2013-12-15 21:40:15 +01:00
2013-12-16 22:01:06 +01:00
@pyqtSlot(str)
def openurl(self, url):
tab = self.tabs[self.currentIndex()]
tab.openurl(url)
2014-01-17 08:03:42 +01:00
@pyqtSlot()
def close_act(self):
2014-01-17 08:28:04 +01:00
if len(self.tabs) > 1:
idx = self.currentIndex()
self.tabs.pop(idx)
self.removeTab(idx)
else:
# FIXME
pass
2014-01-17 08:03:42 +01:00
@pyqtSlot()
def reload_act(self):
self.currentWidget().reload()
@pyqtSlot()
def stop_act(self):
self.currentWidget().stop()
@pyqtSlot()
def print_act(self):
# FIXME that does not what I expect
preview = QPrintPreviewDialog()
preview.paintRequested.connect(self.currentWidget().print)
preview.exec_()
@pyqtSlot()
def back_act(self):
# FIXME display warning if beginning of history
self.currentWidget().back()
@pyqtSlot()
def forward_act(self):
# FIXME display warning if end of history
self.currentWidget().forward()
2014-01-17 22:02:11 +01:00
@pyqtSlot()
@pyqtSlot(int)
def scroll_down_act(self, count=50):
self.currentWidget().page().mainFrame().scroll(0, count)
@pyqtSlot()
@pyqtSlot(int)
def scroll_up_act(self, count=50):
self.currentWidget().page().mainFrame().scroll(0, -count)
2014-01-17 08:39:14 +01:00
@pyqtSlot()
def switch_prev(self):
idx = self.currentIndex()
if idx > 0:
self.setCurrentIndex(idx - 1)
else:
# FIXME
pass
@pyqtSlot()
def switch_next(self):
idx = self.currentIndex()
if idx < self.count() - 1:
self.setCurrentIndex(idx + 1)
else:
# FIXME
pass
@pyqtSlot(int)
def progress_changed(self, prog):
self.filter_signals(self.cur_progress, prog)
def filter_signals(self, signal, *args):
dbgstr = "{} ({})".format(
signal.signal, ','.join([str(e) for e in args]))
if self.currentWidget() == self.sender():
logging.debug('{} - emitting'.format(dbgstr))
signal.emit(*args)
2014-01-17 20:29:20 +01:00
else:
logging.debug('{} - ignoring'.format(dbgstr))
2014-01-17 08:39:14 +01:00
2014-01-17 13:16:13 +01:00
@pyqtSlot(int)
def index_changed(self, idx):
tab = self.widget(idx)
self.cur_progress.emit(tab.progress)
2013-12-15 21:40:15 +01:00
class BrowserTab(QWebView):
2014-01-17 12:24:38 +01:00
parent = None
2014-01-17 13:16:13 +01:00
progress = 0
2014-01-17 12:24:38 +01:00
2013-12-15 21:40:15 +01:00
def __init__(self, parent):
super().__init__(parent)
2014-01-17 13:16:13 +01:00
self.loadProgress.connect(self.set_progress)
2013-12-15 21:40:15 +01:00
self.show()
def openurl(self, url):
if not url.startswith('http://'):
url = 'http://' + url
super().load(QUrl(url))
2014-01-17 13:16:13 +01:00
def set_progress(self, prog):
self.progress = prog