layout_util.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 # Author: Isaac Saito
00034 
00035 from python_qt_binding.QtCore import Qt
00036 from python_qt_binding.QtGui import QWidgetItem
00037 import roslib
00038 import rospy
00039 
00040 
00041 class LayoutUtil(object):
00042 
00043     @staticmethod
00044     def alternate_color(list_widgets, colors_alter=[Qt.white, Qt.gray]):
00045         """
00046         Alternate the background color of the widgets that are ordered
00047         linearly, by the given list of colors.
00048 
00049         Originally intended for the elements of QHBoxLayout & QVBoxLayout.
00050 
00051         @type list_widgets: QtGui.QWidget[]
00052         @type colors_alter: QtCore.Qt.GlobalColor[]
00053         @param colors_alter: 1st element is used as initial/default color.
00054         @rtype: void
00055 
00056         @author: Isaac Saito
00057         """
00058 
00059         colors_num = len(colors_alter)
00060         i_widget = 0
00061         for w in list_widgets:
00062             w.setAutoFillBackground(True)
00063             p = w.palette()
00064 
00065             divisor = (i_widget + colors_num) % colors_num
00066             i_widget += 1
00067 
00068             rospy.logdebug('LayoutUtil divisor={} i_widget={} colors_num={}'.format(
00069                                                                    divisor,
00070                                                                    i_widget,
00071                                                                    colors_num))
00072 
00073             p.setColor(w.backgroundRole(), colors_alter[divisor])
00074             w.setPalette(p)
00075 
00076     @staticmethod
00077     def clear_layout(layout):
00078         """
00079         Clear all items in the given layout. Currently, only the instances of
00080         QWidgetItem get cleared (ie. QSpaceItem is ignored).
00081 
00082         Originally taken from http://stackoverflow.com/a/9375273/577001
00083 
00084         :type layout: QLayout
00085         """
00086         for i in reversed(range(layout.count())):
00087             item = layout.itemAt(i)
00088 
00089             if isinstance(item, QWidgetItem):
00090                 # print "widget" + str(item)
00091                 item.widget().close()
00092                 # or
00093                 # item.widget().setParent(None)
00094             elif isinstance(item, QSpacerItem):
00095                 # print "spacer " + str(item)
00096                 continue
00097                 # no need to do extra stuff
00098             else:
00099                 # print "layout " + str(item)
00100                 LayoutUtil.clear_layout(item.layout())
00101 
00102             # remove the item from layout
00103             layout.removeItem(item)


rqt_py_common
Author(s): Dorian Scholz, Isaac Saito
autogenerated on Wed Sep 16 2015 06:58:00