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 import rospkg
00035
00036 from python_qt_binding import loadUi
00037 from python_qt_binding.QtCore import Qt, QUrl
00038 from python_qt_binding.QtGui import QIcon
00039 try:
00040
00041 from python_qt_binding.QtWebEngine import QWebEnginePage as QWebPage
00042 from python_qt_binding.QtWebEngine import QWebEngineView as QWebView
00043 except ImportError:
00044 try:
00045
00046 from python_qt_binding.QtWebKitWidgets import QWebPage, QWebView
00047 except ImportError:
00048
00049 from python_qt_binding.QtWebKit import QWebPage, QWebView
00050 from python_qt_binding.QtWidgets import QCompleter, QWidget
00051
00052
00053 def is_string(s):
00054 """Check if the argument is a string which works for both Python 2 and 3."""
00055 try:
00056 return isinstance(s, basestring)
00057 except NameError:
00058 return isinstance(s, str)
00059
00060
00061 class WebWidget(QWidget):
00062
00063 def __init__(self, url=None):
00064 """
00065 Class to load a webpage in a widget.
00066
00067 :param url: If url is empty then a navigation bar is shown otherwise the url is loaded and the navigation bar is hidden, ''str''
00068 """
00069 super(WebWidget, self).__init__()
00070 rp = rospkg.RosPack()
00071 ui_file = os.path.join(rp.get_path('rqt_web'), 'resource', 'web_widget.ui')
00072 loadUi(ui_file, self)
00073 self.setObjectName('WebWidget')
00074
00075 self._loading = False
00076 self._stop_icon = QIcon.fromTheme('process-stop')
00077 self._reload_icon = QIcon.fromTheme('view-refresh')
00078 self._working_icon = QIcon.fromTheme('process-working')
00079
00080 self._completer_word_list = ['']
00081 self._view = QWebView()
00082 self.verticalLayout.addWidget(self._view)
00083 if url is None:
00084 self.set_url("http://ros.org", True)
00085 else:
00086 self.set_url(url, False)
00087
00088 self.url_lineedit.returnPressed.connect(self._handle_url_change)
00089 self._view.loadFinished[bool].connect(self._handle_load_finished)
00090 self.reload_button.clicked.connect(self._handle_reload_clicked)
00091 self._view.linkClicked.connect(self._handle_link_clicked)
00092 self._view.urlChanged[QUrl].connect(self._handle_url_changed)
00093
00094 def set_url(self, url, showinput=False):
00095 """
00096 Sets the url and begins loading that page
00097 :param url: url to load in the webview, ''str or QUrl''
00098 :param showinput: if true the input bar will be shown, else hidden, ''bool''
00099 """
00100 if url is not None:
00101 self._url = QUrl(url)
00102 self.set_show_url_input(showinput)
00103 self._view.setUrl(self._url)
00104
00105 def set_show_url_input(self, showinput):
00106 """
00107 Sets the value of the show_url_input flag and hides/shows the widgets as required
00108 :param showinput: true - show inputbar false - hide , ''bool''
00109 """
00110 self._show_url_input = showinput
00111 self.url_lineedit.setVisible(self._show_url_input)
00112 self.reload_button.setVisible(self._show_url_input)
00113 if self._show_url_input:
00114 self._view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
00115 else:
00116 self._view.page().setLinkDelegationPolicy(QWebPage.DontDelegateLinks)
00117
00118 def save_settings(self, settings):
00119 settings.set_value('url_completion', self._pack(self._completer_word_list))
00120 settings.set_value('url_current', self._url.toString())
00121
00122 def restore_settings(self, settings):
00123 self._completer_word_list += self._unpack(settings.value('url_completion'))
00124 self._completer_word_list = list(set(self._completer_word_list))
00125 url = settings.value('url_current')
00126 if url:
00127 self.set_url(url, self._show_url_input)
00128
00129 def _handle_url_change(self):
00130 self.set_url(self.url_lineedit.text(), True)
00131
00132 def _handle_link_clicked(self, url):
00133 self.set_url(url, True)
00134
00135 def _handle_reload_clicked(self):
00136 if self._loading:
00137 self._view.stop()
00138 self._loading = False
00139 self.reload_button.setIcon(self._reload_icon)
00140 else:
00141 self._view.reload()
00142 self._loading = True
00143 self.reload_button.setIcon(self._stop_icon)
00144
00145 def _handle_url_changed(self, url):
00146
00147 self.url_lineedit.setText(url.toString())
00148 self.reload_button.setIcon(self._stop_icon)
00149 self._loading = True
00150
00151 def _handle_load_finished(self, ok):
00152 self._loading = False
00153 self.reload_button.setIcon(self._reload_icon)
00154 if ok:
00155 self._add_completer_list_item(self._url.toString())
00156 else:
00157
00158 self._view.loadFinished[bool].disconnect(self._handle_load_finished)
00159 self._view.page().currentFrame().setHtml(
00160 '<html><h2>The url you entered seems to be faulty.</h2></html>')
00161 self._view.loadFinished[bool].connect(self._handle_load_finished)
00162
00163 def _add_completer_list_item(self, url):
00164 self._completer_word_list.append(self.url_lineedit.text())
00165 self._completer_word_list = list(set(self._completer_word_list))
00166 self._completer = QCompleter(self._completer_word_list)
00167 self._completer.setCaseSensitivity(Qt.CaseInsensitive)
00168 self._completer.setCompletionMode(QCompleter.PopupCompletion)
00169 self.url_lineedit.setCompleter(self._completer)
00170
00171 @staticmethod
00172 def _pack(data):
00173 """
00174 Packs 'data' into a form that can be easily and readably written to an ini file
00175 :param data: A list of strings to be flattened into a string ''list''
00176 :return: A string suitable for output to ini files ''str''
00177 """
00178 if len(data) == 0:
00179 return ''
00180 if len(data) == 1:
00181 return data[0]
00182 return data
00183
00184 @staticmethod
00185 def _unpack(data):
00186 """
00187 Unpacks the values read from an ini file
00188 :param data: An entry taken from an ini file ''list or string''
00189 :return: A list of strings ''list''
00190 """
00191 if data is None or data == '':
00192 data = []
00193 elif is_string(data):
00194 data = [data]
00195 return data