def __init__(self, parent=None): QWidget.__init__(self, parent) self.home_url = None self.webview = WebView(self) self.connect(self.webview, SIGNAL("loadFinished(bool)"), self.load_finished) self.connect(self.webview, SIGNAL("titleChanged(QString)"), self.setWindowTitle) self.connect(self.webview, SIGNAL("urlChanged(QUrl)"), self.url_changed) home_button = create_toolbutton(self, icon=get_icon('home.png'), tip=_("Home"), triggered=self.go_home) zoom_out_button = action2button(self.webview.zoom_out_action) zoom_in_button = action2button(self.webview.zoom_in_action) pageact2btn = lambda prop: action2button(self.webview.pageAction(prop), parent=self.webview) refresh_button = pageact2btn(QWebPage.Reload) stop_button = pageact2btn(QWebPage.Stop) previous_button = pageact2btn(QWebPage.Back) next_button = pageact2btn(QWebPage.Forward) stop_button.setEnabled(False) self.connect(self.webview, SIGNAL("loadStarted()"), lambda: stop_button.setEnabled(True)) self.connect(self.webview, SIGNAL("loadFinished(bool)"), lambda: stop_button.setEnabled(False)) progressbar = QProgressBar(self) progressbar.setTextVisible(False) progressbar.hide() self.connect(self.webview, SIGNAL("loadStarted()"), progressbar.show) self.connect(self.webview, SIGNAL("loadProgress(int)"), progressbar.setValue) self.connect(self.webview, SIGNAL("loadFinished(bool)"), lambda _state: progressbar.hide()) label = QLabel(self.get_label()) self.url_combo = UrlComboBox(self) self.connect(self.url_combo, SIGNAL('valid(bool)'), self.url_combo_activated) self.connect(self.webview, SIGNAL("iconChanged()"), self.icon_changed) self.find_widget = FindReplace(self) self.find_widget.set_editor(self.webview) self.find_widget.hide() find_button = create_toolbutton(self, icon='find.png', tip=_("Find text"), toggled=self.toggle_find_widget) self.connect(self.find_widget, SIGNAL("visibility_changed(bool)"), find_button.setChecked) hlayout = QHBoxLayout() for widget in (previous_button, next_button, home_button, find_button, label, self.url_combo, zoom_out_button, zoom_in_button, refresh_button, progressbar, stop_button): hlayout.addWidget(widget) layout = QVBoxLayout() layout.addLayout(hlayout) layout.addWidget(self.webview) layout.addWidget(self.find_widget) self.setLayout(layout)
class WebBrowser(QWidget): """ Web browser widget """ def __init__(self, parent=None): QWidget.__init__(self, parent) self.home_url = None self.webview = WebView(self) self.connect(self.webview, SIGNAL("loadFinished(bool)"), self.load_finished) self.connect(self.webview, SIGNAL("titleChanged(QString)"), self.setWindowTitle) self.connect(self.webview, SIGNAL("urlChanged(QUrl)"), self.url_changed) home_button = create_toolbutton(self, icon=get_icon('home.png'), tip=_("Home"), triggered=self.go_home) zoom_out_button = action2button(self.webview.zoom_out_action) zoom_in_button = action2button(self.webview.zoom_in_action) pageact2btn = lambda prop: action2button(self.webview.pageAction(prop), parent=self.webview) refresh_button = pageact2btn(QWebPage.Reload) stop_button = pageact2btn(QWebPage.Stop) previous_button = pageact2btn(QWebPage.Back) next_button = pageact2btn(QWebPage.Forward) stop_button.setEnabled(False) self.connect(self.webview, SIGNAL("loadStarted()"), lambda: stop_button.setEnabled(True)) self.connect(self.webview, SIGNAL("loadFinished(bool)"), lambda: stop_button.setEnabled(False)) progressbar = QProgressBar(self) progressbar.setTextVisible(False) progressbar.hide() self.connect(self.webview, SIGNAL("loadStarted()"), progressbar.show) self.connect(self.webview, SIGNAL("loadProgress(int)"), progressbar.setValue) self.connect(self.webview, SIGNAL("loadFinished(bool)"), lambda _state: progressbar.hide()) label = QLabel(self.get_label()) self.url_combo = UrlComboBox(self) self.connect(self.url_combo, SIGNAL('valid(bool)'), self.url_combo_activated) self.connect(self.webview, SIGNAL("iconChanged()"), self.icon_changed) self.find_widget = FindReplace(self) self.find_widget.set_editor(self.webview) self.find_widget.hide() find_button = create_toolbutton(self, icon='find.png', tip=_("Find text"), toggled=self.toggle_find_widget) self.connect(self.find_widget, SIGNAL("visibility_changed(bool)"), find_button.setChecked) hlayout = QHBoxLayout() for widget in (previous_button, next_button, home_button, find_button, label, self.url_combo, zoom_out_button, zoom_in_button, refresh_button, progressbar, stop_button): hlayout.addWidget(widget) layout = QVBoxLayout() layout.addLayout(hlayout) layout.addWidget(self.webview) layout.addWidget(self.find_widget) self.setLayout(layout) def get_label(self): """Return address label text""" return _("Address:") def set_home_url(self, text): """Set home URL""" self.home_url = QUrl(text) def set_url(self, url): """Set current URL""" self.url_changed(url) self.go_to(url) def go_to(self, url_or_text): """Go to page *address*""" if isinstance(url_or_text, basestring): url = QUrl(url_or_text) else: url = url_or_text self.webview.load(url) def go_home(self): """Go to home page""" if self.home_url is not None: self.set_url(self.home_url) def text_to_url(self, text): """Convert text address into QUrl object""" return QUrl(text) def url_combo_activated(self, valid): """Load URL from combo box first item""" text = unicode(self.url_combo.currentText()) self.go_to(self.text_to_url(text)) def load_finished(self, ok): if not ok: self.webview.setHtml(_("Unable to load page")) def url_to_text(self, url): """Convert QUrl object to displayed text in combo box""" return url.toString() def url_changed(self, url): """Displayed URL has changed -> updating URL combo box""" self.url_combo.add_text(self.url_to_text(url)) def icon_changed(self): self.url_combo.setItemIcon(self.url_combo.currentIndex(), self.webview.icon()) self.setWindowIcon(self.webview.icon()) def toggle_find_widget(self, state): if state: self.find_widget.show() else: self.find_widget.hide()