00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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
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
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
00115 grid.addWidget(self, row, 0, 1, -1)
00116
00117 def update_group(self, config):
00118 self.state = config['state']
00119
00120
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)