update_combo.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 
00003 # Copyright (C) 2014, PAL Robotics S.L.
00004 #
00005 # Redistribution and use in source and binary forms, with or without
00006 # modification, are permitted provided that the following conditions are met:
00007 # * Redistributions of source code must retain the above copyright notice,
00008 # this list of conditions and the following disclaimer.
00009 # * Redistributions in binary form must reproduce the above copyright
00010 # notice, this list of conditions and the following disclaimer in the
00011 # documentation and/or other materials provided with the distribution.
00012 # * Neither the name of PAL Robotics S.L. nor the names of its
00013 # contributors may be used to endorse or promote products derived from
00014 # this software without specific prior written permission.
00015 #
00016 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00019 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00020 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00021 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00022 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00023 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00024 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00025 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00026 # POSSIBILITY OF SUCH DAMAGE.
00027 
00028 def update_combo(combo, new_vals):
00029     """
00030     Update the contents of a combo box with a set of new values.
00031 
00032     If the previously selected element is still present in the new values, it
00033     will remain as active selection, even if its index has changed. This will
00034     not trigger any signal.
00035 
00036     If the previously selected element is no longer present in the new values,
00037     the combo will unset its selection. This will trigger signals for changed
00038     element.
00039     """
00040     selected_val = combo.currentText()
00041     old_vals = [combo.itemText(i) for i in range(combo.count())]
00042 
00043     # Check if combo items changed
00044     if not _is_permutation(old_vals, new_vals):
00045         # Determine if selected value is in the new list
00046         selected_id = -1
00047         try:
00048             selected_id = new_vals.index(selected_val)
00049         except (ValueError):
00050             combo.setCurrentIndex(-1)
00051 
00052         # Re-populate items
00053         combo.blockSignals(True)  # No need to notify these changes
00054         combo.clear()
00055         combo.insertItems(0, new_vals)
00056         combo.setCurrentIndex(selected_id)  # Restore selection
00057         combo.blockSignals(False)
00058 
00059 def _is_permutation(a, b):
00060     """
00061     @type a []
00062     @type b []
00063     @return True if C{a} is a permutation of C{b}, false otherwise
00064     @rtype bool
00065     """
00066     return len(a) == len(b) and sorted(a) == sorted(b)


rqt_controller_manager
Author(s): Kelsey Hawkins , Adolfo Rodríguez Tsouroukdissian
autogenerated on Thu Dec 1 2016 03:46:09