layout_util.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 # Author: Isaac Saito
34 
35 from python_qt_binding.QtCore import Qt
36 from python_qt_binding.QtWidgets import QWidgetItem, QSpacerItem
37 import roslib
38 import rospy
39 
40 
41 class LayoutUtil(object):
42 
43  @staticmethod
44  def alternate_color(list_widgets, colors_alter=[Qt.white, Qt.gray]):
45  """
46  Alternate the background color of the widgets that are ordered
47  linearly, by the given list of colors.
48 
49  Originally intended for the elements of QHBoxLayout & QVBoxLayout.
50 
51  @type list_widgets: QtGui.QWidget[]
52  @type colors_alter: QtCore.Qt.GlobalColor[]
53  @param colors_alter: 1st element is used as initial/default color.
54  @rtype: void
55 
56  @author: Isaac Saito
57  """
58 
59  colors_num = len(colors_alter)
60  i_widget = 0
61  for w in list_widgets:
62  w.setAutoFillBackground(True)
63  p = w.palette()
64 
65  divisor = (i_widget + colors_num) % colors_num
66  i_widget += 1
67 
68  rospy.logdebug('LayoutUtil divisor={} i_widget={} colors_num={}'.format(divisor,
69  i_widget,
70  colors_num))
71 
72  p.setColor(w.backgroundRole(), colors_alter[divisor])
73  w.setPalette(p)
74 
75  @staticmethod
76  def clear_layout(layout):
77  """
78  Clear all items in the given layout. Currently, only the instances of
79  QWidgetItem get cleared (ie. QSpaceItem is ignored).
80 
81  Originally taken from http://stackoverflow.com/a/9375273/577001
82 
83  :type layout: QLayout
84  """
85  for i in reversed(range(layout.count())):
86  item = layout.itemAt(i)
87 
88  if isinstance(item, QWidgetItem):
89  # print "widget" + str(item)
90  item.widget().close()
91  # or
92  # item.widget().setParent(None)
93  elif isinstance(item, QSpacerItem):
94  # print "spacer " + str(item)
95  continue
96  # no need to do extra stuff
97  else:
98  # print "layout " + str(item)
99  LayoutUtil.clear_layout(item.layout())
100 
101  # remove the item from layout
102  layout.removeItem(item)
def alternate_color(list_widgets, colors_alter=[Qt.white, Qt, gray)
Definition: layout_util.py:44


rqt_py_common
Author(s): Dorian Scholz , Isaac Saito, Dirk Thomas
autogenerated on Mon May 16 2022 02:49:40