groups.py
Go to the documentation of this file.
00001 # Software License Agreement (BSD License)
00002 #
00003 # Copyright (c) 2009, 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: Ze'ev Klapow
00034 
00035 from QtCore import Qt
00036 import QtGui
00037 from QtGui import QWidget, QLabel, QPushButton, QTabWidget, QGridLayout, QGroupBox
00038 
00039 from .editors import *
00040 
00041 group_types = {
00042     '': 'BoxGroup',
00043     'collapse': 'CollapseGroup',
00044     'tab': 'TabGroup',
00045     'hide': 'HideGroup',
00046     'apply': 'ApplyGroup',
00047 }
00048 
00049 def find_cfg(config, name):
00050     """
00051     reaaaaallly cryptic function which returns the config object for specified group.
00052     """
00053     cfg = None
00054     for k, v in config.items():
00055         try:
00056             if k.lower() == name.lower():
00057                 cfg = v
00058                 return cfg
00059             else:
00060                 try:
00061                     cfg = find_cfg(v, name)
00062                     if not cfg == None:
00063                         return cfg
00064                 except Exception as exc:
00065                     raise exc
00066         except AttributeError:
00067             pass
00068         except Exception as exc:
00069             raise exc
00070     return cfg
00071 
00072 class Group(QWidget):
00073     def __init__(self, updater, config):
00074         super(Group, self).__init__()
00075         self.state = config['state']
00076         self.name = config['name']
00077 
00078         # Every group can have one tab bar
00079         self.tab_bar = None
00080         self.tab_bar_shown = False
00081 
00082         self.grid = QtGui.QGridLayout()
00083 
00084         self.updater = updater 
00085 
00086         self.widgets = []
00087         self.add_widgets(config)
00088 
00089         # Labels should not stretch
00090         self.grid.setColumnStretch(1,1)
00091         self.setLayout(self.grid)
00092 
00093     def add_widgets(self, descr):
00094         for param in descr['parameters']:
00095             if param['edit_method']:
00096                 widget = EnumEditor(self.updater, param)
00097             elif param['type'] in editor_types:
00098                 widget = eval(editor_types[param['type']])(self.updater, param)
00099 
00100             self.widgets.append(widget)
00101         
00102         for name, group in descr['groups'].items():
00103             if group['type'] == 'tab':
00104                 widget = TabGroup(self, self.updater, group)
00105             elif group['type'] in group_types.keys():
00106                 widget = eval(group_types[group['type']])(self.updater, group)
00107 
00108             self.widgets.append(widget)
00109 
00110         for i, ed in enumerate(self.widgets):
00111             ed.display(self.grid, i)
00112 
00113     def display(self, grid, row):
00114         #groups span across all columns
00115         grid.addWidget(self, row, 0, 1, -1)
00116 
00117     def update_group(self, config):
00118         self.state = config['state']
00119         
00120         # TODO: should use config.keys but this method doesnt exist
00121         names = [name for name, v in config.items()]
00122 
00123         for widget in self.widgets:
00124             if isinstance(widget, Editor):
00125                 if widget.name in names:
00126                     widget.update_value(config[widget.name])
00127             elif isinstance(widget, Group):
00128                 cfg = find_cfg(config, widget.name)
00129                 widget.update_group(cfg)
00130 
00131     def close(self):
00132         for w in self.widgets:
00133             w.close()
00134 
00135 class BoxGroup(Group):
00136     def __init__(self, updater, config):
00137         super(BoxGroup, self).__init__(updater, config)
00138 
00139         self.box = QGroupBox(self.name)
00140         self.box.setLayout(self.grid)
00141 
00142     def display(self, grid, row):
00143         grid.addWidget(self.box, row, 0, 1, -1)
00144 
00145 class CollapseGroup(BoxGroup):
00146     def __init__(self, updater, config):
00147         super(CollapseGroup, self).__init__(updater, config)
00148         self.box.setCheckable(True)
00149 
00150 class HideGroup(BoxGroup):
00151     def update_group(self, config):
00152         super(HideGroup, self).update_group(config)
00153         self.box.setVisible(self.state)
00154 
00155 class TabGroup(Group):
00156     def __init__(self, parent, updater, config):
00157         super(TabGroup, self).__init__(updater, config)
00158         self.parent = parent
00159 
00160         if self.parent.tab_bar is None:
00161             self.parent.tab_bar = QTabWidget()
00162 
00163         parent.tab_bar.addTab(self, self.name)
00164 
00165     def display(self, grid, row):
00166         if not self.parent.tab_bar_shown:
00167             grid.addWidget(self.parent.tab_bar, row, 0, 1, -1)
00168             self.parent.tab_bar_shown = True
00169 
00170     def close(self):
00171         super(TabGroup, self).close()
00172         self.parent.tab_bar = None
00173         self.parent.tab_bar_shown = False
00174 
00175 class ApplyGroup(BoxGroup):
00176     class ApplyUpdater:
00177         def __init__(self, updater):
00178             self.updater = updater
00179             self._pending_config = {}
00180 
00181         def update(self, config):
00182             for name, value in config.items():
00183                 self._pending_config[name] = value
00184 
00185         def apply_update(self):
00186             self.updater.update(self._pending_config)
00187             self._pending_config = {}
00188 
00189     def __init__(self, updater, config):
00190         self.updater = ApplyGroup.ApplyUpdater(updater)
00191         super(ApplyGroup, self).__init__(self.updater, config)
00192 
00193         self.button = QPushButton("Apply %s" % self.name)
00194         self.button.clicked.connect(self.updater.apply_update)
00195 
00196         rows = self.grid.rowCount()
00197         self.grid.addWidget(self.button, rows+1, 1, Qt.AlignRight)
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends


reconfigure_gui
Author(s): Ze'ev Klapow
autogenerated on Mon Aug 19 2013 12:15:18