web_widget.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2012, Willow Garage, Inc.
00004 # All rights reserved.
00005 #
00006 # Redistribution and use in source and binary forms, with or without
00007 # modification, are permitted provided that the following conditions
00008 # are met:
00009 #
00010 #  * Redistributions of source code must retain the above copyright
00011 #    notice, this list of conditions and the following disclaimer.
00012 #  * Redistributions in binary form must reproduce the above
00013 #    copyright notice, this list of conditions and the following
00014 #    disclaimer in the documentation and/or other materials provided
00015 #    with the distribution.
00016 #  * Neither the name of Willow Garage, Inc. nor the names of its
00017 #    contributors may be used to endorse or promote products derived
00018 #    from this software without specific prior written permission.
00019 #
00020 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031 # POSSIBILITY OF SUCH DAMAGE.
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 QCompleter, QIcon, QWidget
00039 from python_qt_binding.QtWebKit import QWebPage, QWebView
00040 
00041 
00042 class WebWidget(QWidget):
00043     def __init__(self, url=None):
00044         """
00045         Class to load a webpage in a widget.
00046 
00047         :param url: If url is empty then a navigation bar is shown otherwise the url is loaded and the navigation bar is hidden, ''str''
00048         """
00049         super(WebWidget, self).__init__()
00050         rp = rospkg.RosPack()
00051         ui_file = os.path.join(rp.get_path('rqt_web'), 'resource', 'web_widget.ui')
00052         loadUi(ui_file, self)
00053         self.setObjectName('WebWidget')
00054 
00055         self._loading = False
00056         self._stop_icon = QIcon.fromTheme('process-stop')
00057         self._reload_icon = QIcon.fromTheme('view-refresh')
00058         self._working_icon = QIcon.fromTheme('process-working')
00059 
00060         self._completer_word_list = ['']
00061         self._view = QWebView()
00062         self.verticalLayout.addWidget(self._view)
00063         if url is None:
00064             self.set_url("http://ros.org", True)
00065         else:
00066             self.set_url(url, False)
00067 
00068         self.url_lineedit.returnPressed.connect(self._handle_url_change)
00069         self._view.loadFinished[bool].connect(self._handle_load_finished)
00070         self.reload_button.clicked.connect(self._handle_reload_clicked)
00071         self._view.linkClicked.connect(self._handle_link_clicked)
00072         self._view.urlChanged[QUrl].connect(self._handle_url_changed)
00073 
00074     def set_url(self, url, showinput=False):
00075         """
00076         Sets the url and begins loading that page
00077         :param url: url to load in the webview, ''str or QUrl''
00078         :param showinput: if true the input bar will be shown, else hidden, ''bool''
00079         """
00080         if url is not None:
00081             self._url = QUrl(url)
00082             self.set_show_url_input(showinput)
00083             self._view.setUrl(self._url)
00084 
00085     def set_show_url_input(self, showinput):
00086         """
00087         Sets the value of the show_url_input flag and hides/shows the widgets as required
00088         :param showinput: true - show inputbar false - hide , ''bool''
00089         """
00090         self._show_url_input = showinput
00091         self.url_lineedit.setVisible(self._show_url_input)
00092         self.reload_button.setVisible(self._show_url_input)
00093         if self._show_url_input:
00094             self._view.page().setLinkDelegationPolicy(QWebPage.DelegateAllLinks)
00095         else:
00096             self._view.page().setLinkDelegationPolicy(QWebPage.DontDelegateLinks)
00097 
00098     def save_settings(self, settings):
00099         settings.set_value('url_completion', self._pack(self._completer_word_list))
00100         settings.set_value('url_current', self._url.toString())
00101 
00102     def restore_settings(self, settings):
00103         self._completer_word_list += self._unpack(settings.value('url_completion'))
00104         self._completer_word_list = list(set(self._completer_word_list))
00105         url = settings.value('url_current')
00106         if url:
00107             self.set_url(url, self._show_url_input)
00108 
00109     def _handle_url_change(self):
00110         self.set_url(self.url_lineedit.text(), True)
00111 
00112     def _handle_link_clicked(self, url):
00113         self.set_url(url, True)
00114 
00115     def _handle_reload_clicked(self):
00116         if self._loading:
00117             self._view.stop()
00118             self._loading = False
00119             self.reload_button.setIcon(self._reload_icon)
00120         else:
00121             self._view.reload()
00122             self._loading = True
00123             self.reload_button.setIcon(self._stop_icon)
00124 
00125     def _handle_url_changed(self, url):
00126         # set text to the current loading item
00127         self.url_lineedit.setText(url.toString())
00128         self.reload_button.setIcon(self._stop_icon)
00129         self._loading = True
00130 
00131     def _handle_load_finished(self, ok):
00132         self._loading = False
00133         self.reload_button.setIcon(self._reload_icon)
00134         if ok:
00135             self._add_completer_list_item(self._url.toString())
00136         else:
00137             # need to disconnect or we will resend the signal once the error page loads
00138             self._view.loadFinished[bool].disconnect(self._handle_load_finished)
00139             self._view.page().currentFrame().setHtml('<html><h2>The url you entered seems to be faulty.</h2></html>')
00140             self._view.loadFinished[bool].connect(self._handle_load_finished)
00141 
00142     def _add_completer_list_item(self, url):
00143         self._completer_word_list.append(self.url_lineedit.text())
00144         self._completer_word_list = list(set(self._completer_word_list))
00145         self._completer = QCompleter(self._completer_word_list)
00146         self._completer.setCaseSensitivity(Qt.CaseInsensitive)
00147         self._completer.setCompletionMode(QCompleter.PopupCompletion)
00148         self.url_lineedit.setCompleter(self._completer)
00149 
00150     @staticmethod
00151     def _pack(data):
00152         """
00153         Packs 'data' into a form that can be easily and readably written to an ini file
00154         :param data: A list of strings to be flattened into a string ''list''
00155         :return: A string suitable for output to ini files ''str''
00156         """
00157         if len(data) == 0:
00158             return ''
00159         if len(data) == 1:
00160             return data[0]
00161         return data
00162 
00163     @staticmethod
00164     def _unpack(data):
00165         """
00166         Unpacks the values read from an ini file
00167         :param data: An entry taken from an ini file ''list or string''
00168         :return: A list of strings ''list''
00169         """
00170         if data is None or data == '':
00171             data = []
00172         elif isinstance(data, basestring):
00173             data = [data]
00174         return data


rqt_web
Author(s): Aaron Blasdel
autogenerated on Wed Sep 16 2015 06:58:17