web_widget.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2012, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 import os
34 import rospkg
35 
36 from python_qt_binding import loadUi
37 from python_qt_binding.QtCore import Qt, QUrl
38 from python_qt_binding.QtGui import QIcon
39 try:
40  # Qt 5.6 and higher
41  from python_qt_binding.QtWebEngine import QWebEnginePage as QWebPage
42  from python_qt_binding.QtWebEngine import QWebEngineView as QWebView
43 except ImportError:
44  try:
45  # Qt 5.0 - 5.5
46  from python_qt_binding.QtWebKitWidgets import QWebPage, QWebView
47  except ImportError:
48  # Qt 4
49  from python_qt_binding.QtWebKit import QWebPage, QWebView
50 from python_qt_binding.QtWidgets import QCompleter, QWidget
51 
52 
53 def is_string(s):
54  """Check if the argument is a string which works for both Python 2 and 3."""
55  try:
56  return isinstance(s, basestring)
57  except NameError:
58  return isinstance(s, str)
59 
60 
61 class WebWidget(QWidget):
62 
63  def __init__(self, url=None):
64  """
65  Class to load a webpage in a widget.
66 
67  :param url: If url is empty then a navigation bar is shown otherwise the url is loaded and the navigation bar is hidden, ''str''
68  """
69  super(WebWidget, self).__init__()
70  rp = rospkg.RosPack()
71  ui_file = os.path.join(rp.get_path('rqt_web'), 'resource', 'web_widget.ui')
72  loadUi(ui_file, self)
73  self.setObjectName('WebWidget')
74 
75  self._loading = False
76  self._stop_icon = QIcon.fromTheme('process-stop')
77  self._reload_icon = QIcon.fromTheme('view-refresh')
78  self._working_icon = QIcon.fromTheme('process-working')
79 
81  self._view = QWebView()
82  self.verticalLayout.addWidget(self._view)
83  if url is None:
84  self.set_url("http://ros.org", True)
85  else:
86  self.set_url(url, False)
87 
88  self.url_lineedit.returnPressed.connect(self._handle_url_change)
89  self._view.loadFinished[bool].connect(self._handle_load_finished)
90  self.reload_button.clicked.connect(self._handle_reload_clicked)
91  self._view.linkClicked.connect(self._handle_link_clicked)
92  self._view.urlChanged[QUrl].connect(self._handle_url_changed)
93 
94  def set_url(self, url, showinput=False):
95  """
96  Sets the url and begins loading that page
97  :param url: url to load in the webview, ''str or QUrl''
98  :param showinput: if true the input bar will be shown, else hidden, ''bool''
99  """
100  if url is not None:
101  self._url = QUrl(url)
102  self.set_show_url_input(showinput)
103  self._view.setUrl(self._url)
104 
105  def set_show_url_input(self, showinput):
106  """
107  Sets the value of the show_url_input flag and hides/shows the widgets as required
108  :param showinput: true - show inputbar false - hide , ''bool''
109  """
110  self._show_url_input = showinput
111  self.url_lineedit.setVisible(self._show_url_input)
112  self.reload_button.setVisible(self._show_url_input)
113  if self._show_url_input:
114  self._view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
115  else:
116  self._view.page().setLinkDelegationPolicy(QWebPage.DontDelegateLinks)
117 
118  def save_settings(self, settings):
119  settings.set_value('url_completion', self._pack(self._completer_word_list))
120  settings.set_value('url_current', self._url.toString())
121 
122  def restore_settings(self, settings):
123  self._completer_word_list += self._unpack(settings.value('url_completion'))
124  self._completer_word_list = list(set(self._completer_word_list))
125  url = settings.value('url_current')
126  if url:
127  self.set_url(url, self._show_url_input)
128 
130  self.set_url(self.url_lineedit.text(), True)
131 
132  def _handle_link_clicked(self, url):
133  self.set_url(url, True)
134 
136  if self._loading:
137  self._view.stop()
138  self._loading = False
139  self.reload_button.setIcon(self._reload_icon)
140  else:
141  self._view.reload()
142  self._loading = True
143  self.reload_button.setIcon(self._stop_icon)
144 
145  def _handle_url_changed(self, url):
146  # set text to the current loading item
147  self.url_lineedit.setText(url.toString())
148  self.reload_button.setIcon(self._stop_icon)
149  self._loading = True
150 
151  def _handle_load_finished(self, ok):
152  self._loading = False
153  self.reload_button.setIcon(self._reload_icon)
154  if ok:
155  self._add_completer_list_item(self._url.toString())
156  else:
157  # need to disconnect or we will resend the signal once the error page loads
158  self._view.loadFinished[bool].disconnect(self._handle_load_finished)
159  self._view.page().currentFrame().setHtml(
160  '<html><h2>The url you entered seems to be faulty.</h2></html>')
161  self._view.loadFinished[bool].connect(self._handle_load_finished)
162 
163  def _add_completer_list_item(self, url):
164  self._completer_word_list.append(self.url_lineedit.text())
165  self._completer_word_list = list(set(self._completer_word_list))
166  self._completer = QCompleter(self._completer_word_list)
167  self._completer.setCaseSensitivity(Qt.CaseInsensitive)
168  self._completer.setCompletionMode(QCompleter.PopupCompletion)
169  self.url_lineedit.setCompleter(self._completer)
170 
171  @staticmethod
172  def _pack(data):
173  """
174  Packs 'data' into a form that can be easily and readably written to an ini file
175  :param data: A list of strings to be flattened into a string ''list''
176  :return: A string suitable for output to ini files ''str''
177  """
178  if len(data) == 0:
179  return ''
180  if len(data) == 1:
181  return data[0]
182  return data
183 
184  @staticmethod
185  def _unpack(data):
186  """
187  Unpacks the values read from an ini file
188  :param data: An entry taken from an ini file ''list or string''
189  :return: A list of strings ''list''
190  """
191  if data is None or data == '':
192  data = []
193  elif is_string(data):
194  data = [data]
195  return data
def save_settings(self, settings)
Definition: web_widget.py:118
def _add_completer_list_item(self, url)
Definition: web_widget.py:163
def _handle_link_clicked(self, url)
Definition: web_widget.py:132
def __init__(self, url=None)
Definition: web_widget.py:63
def set_url(self, url, showinput=False)
Definition: web_widget.py:94
def _handle_url_changed(self, url)
Definition: web_widget.py:145
def restore_settings(self, settings)
Definition: web_widget.py:122
def set_show_url_input(self, showinput)
Definition: web_widget.py:105
def _handle_load_finished(self, ok)
Definition: web_widget.py:151


rqt_web
Author(s): Aaron Blasdel, Dirk Thomas
autogenerated on Fri Mar 17 2023 02:49:14