update_combo.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 # Copyright (C) 2014, PAL Robotics S.L.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are met:
7 # * Redistributions of source code must retain the above copyright notice,
8 # this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution.
12 # * Neither the name of PAL Robotics S.L. nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
15 #
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 # POSSIBILITY OF SUCH DAMAGE.
27 
28 def update_combo(combo, new_vals):
29  """
30  Update the contents of a combo box with a set of new values.
31 
32  If the previously selected element is still present in the new values, it
33  will remain as active selection, even if its index has changed. This will
34  not trigger any signal.
35 
36  If the previously selected element is no longer present in the new values,
37  the combo will unset its selection. This will trigger signals for changed
38  element.
39  """
40  selected_val = combo.currentText()
41  old_vals = [combo.itemText(i) for i in range(combo.count())]
42 
43  # Check if combo items changed
44  if not _is_permutation(old_vals, new_vals):
45  # Determine if selected value is in the new list
46  selected_id = -1
47  try:
48  selected_id = new_vals.index(selected_val)
49  except (ValueError):
50  combo.setCurrentIndex(-1)
51 
52  # Re-populate items
53  combo.blockSignals(True) # No need to notify these changes
54  combo.clear()
55  combo.insertItems(0, new_vals)
56  combo.setCurrentIndex(selected_id) # Restore selection
57  combo.blockSignals(False)
58 
59 def _is_permutation(a, b):
60  """
61  @type a []
62  @type b []
63  @return True if C{a} is a permutation of C{b}, false otherwise
64  @rtype bool
65  """
66  return len(a) == len(b) and sorted(a) == sorted(b)
def update_combo(combo, new_vals)
Definition: update_combo.py:28


rqt_controller_manager
Author(s): Kelsey Hawkins , Adolfo Rodríguez Tsouroukdissian
autogenerated on Fri Jun 7 2019 22:00:16