Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 import os
00034
00035 from python_qt_binding import loadUi
00036 from python_qt_binding.QtCore import Qt, QUrl
00037 from python_qt_binding.QtGui import QCompleter, QIcon, QWidget
00038 from python_qt_binding.QtWebKit import QWebPage, QWebView
00039
00040
00041 class WebWidget(QWidget):
00042 def __init__(self, url=None):
00043 """
00044 Class to load a webpage in a widget.
00045
00046 :param url: If url is empty then a navigation bar is shown otherwise the url is loaded and the navigation bar is hidden, ''str''
00047 """
00048 super(WebWidget, self).__init__()
00049 ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'web_widget.ui')
00050 loadUi(ui_file, self)
00051 self.setObjectName('WebWidget')
00052
00053 self._loading = False
00054 self._stop_icon = QIcon.fromTheme('process-stop')
00055 self._reload_icon = QIcon.fromTheme('view-refresh')
00056 self._working_icon = QIcon.fromTheme('process-working')
00057
00058 self._completer_word_list = ['']
00059 self._view = QWebView()
00060 self.verticalLayout.addWidget(self._view)
00061 if url is None:
00062 self.set_url("http://ros.org", True)
00063 else:
00064 self.set_url(url, False)
00065
00066 self.url_lineedit.returnPressed.connect(self._handle_url_change)
00067 self._view.loadFinished[bool].connect(self._handle_load_finished)
00068 self.reload_button.clicked.connect(self._handle_reload_clicked)
00069 self._view.linkClicked.connect(self._handle_link_clicked)
00070 self._view.urlChanged[QUrl].connect(self._handle_url_changed)
00071
00072 def set_url(self, url, showinput=False):
00073 """
00074 Sets the url and begins loading that page
00075 :param url: url to load in the webview, ''str or QUrl''
00076 :param showinput: if true the input bar will be shown, else hidden, ''bool''
00077 """
00078 if url is not None:
00079 self._url = QUrl(url)
00080 self.set_show_url_input(showinput)
00081 self._view.setUrl(self._url)
00082
00083 def set_show_url_input(self, showinput):
00084 """
00085 Sets the value of the show_url_input flag and hides/shows the widgets as required
00086 :param showinput: true - show inputbar false - hide , ''bool''
00087 """
00088 self._show_url_input = showinput
00089 self.url_lineedit.setVisible(self._show_url_input)
00090 self.reload_button.setVisible(self._show_url_input)
00091 if self._show_url_input:
00092 self._view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
00093 else:
00094 self._view.page().setLinkDelegationPolicy(QWebPage.DontDelegateLinks)
00095
00096 def save_settings(self, settings):
00097 settings.set_value('url_completion', self._pack(self._completer_word_list))
00098 settings.set_value('url_current', self._url.toString())
00099
00100 def restore_settings(self, settings):
00101 self._completer_word_list += self._unpack(settings.value('url_completion'))
00102 self._completer_word_list = list(set(self._completer_word_list))
00103 url = settings.value('url_current')
00104 if url:
00105 self.set_url(url, self._show_url_input)
00106
00107 def _handle_url_change(self):
00108 self.set_url(self.url_lineedit.text(), True)
00109
00110 def _handle_link_clicked(self, url):
00111 self.set_url(url, True)
00112
00113 def _handle_reload_clicked(self):
00114 if self._loading:
00115 self._view.stop()
00116 self._loading = False
00117 self.reload_button.setIcon(self._reload_icon)
00118 else:
00119 self._view.reload()
00120 self._loading = True
00121 self.reload_button.setIcon(self._stop_icon)
00122
00123 def _handle_url_changed(self, url):
00124
00125 self.url_lineedit.setText(url.toString())
00126 self.reload_button.setIcon(self._stop_icon)
00127 self._loading = True
00128
00129 def _handle_load_finished(self, ok):
00130 self._loading = False
00131 self.reload_button.setIcon(self._reload_icon)
00132 if ok:
00133 self._add_completer_list_item(self._url.toString())
00134 else:
00135
00136 self._view.loadFinished[bool].disconnect(self._handle_load_finished)
00137 self._view.page().currentFrame().setHtml('<html><h2>The url you entered seems to be faulty.</h2></html>')
00138 self._view.loadFinished[bool].connect(self._handle_load_finished)
00139
00140 def _add_completer_list_item(self, url):
00141 self._completer_word_list.append(self.url_lineedit.text())
00142 self._completer_word_list = list(set(self._completer_word_list))
00143 self._completer = QCompleter(self._completer_word_list)
00144 self._completer.setCaseSensitivity(Qt.CaseInsensitive)
00145 self._completer.setCompletionMode(QCompleter.PopupCompletion)
00146 self.url_lineedit.setCompleter(self._completer)
00147
00148 @staticmethod
00149 def _pack(data):
00150 """
00151 Packs 'data' into a form that can be easily and readably written to an ini file
00152 :param data: A list of strings to be flattened into a string ''list''
00153 :return: A string suitable for output to ini files ''str''
00154 """
00155 if len(data) == 0:
00156 return ''
00157 if len(data) == 1:
00158 return data[0]
00159 return data
00160
00161 @staticmethod
00162 def _unpack(data):
00163 """
00164 Unpacks the values read from an ini file
00165 :param data: An entry taken from an ini file ''list or string''
00166 :return: A list of strings ''list''
00167 """
00168 if data is None or data == '':
00169 data = []
00170 elif isinstance(data, basestring):
00171 data = [data]
00172 return data