00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 from ost import info
00022 from ost import gfx
00023 from PyQt4 import QtGui
00024
00025 from ost.gfx import ColorOp
00026
00027
00028 class Preset:
00029 NAME_ATTRIBUTE_NAME = "Name"
00030 OP_GROUP_NAME = "Op"
00031 CLASS_NAME_ATTRIBUTE_NAME = "ClassName"
00032 INDEX_ATTRIBUTE_NAME = "Index"
00033
00034 MODULE_NAMES = ["ost.gfx","ost.gui.scene.visibility_op","ost.gui.scene.render_op"]
00035
00036 def __init__(self, name, parent=None):
00037 self.name_ = name
00038 self.ops_ = list()
00039
00040 def SetName(self, name):
00041 self.name_ = name
00042
00043 def GetName(self):
00044 return self.name_
00045
00046 def InsertOp(self, index, op):
00047 self.ops_.insert(index, op)
00048
00049 def RemoveOp(self, op):
00050 self.ops_.remove(op)
00051
00052 def RemoveOpAt(self, index):
00053 del(self.ops_[index])
00054
00055 def GetOp(self, index):
00056 return self.ops_[index]
00057
00058 def GetOpCount(self):
00059 return len(self.ops_)
00060
00061 def SetOp(self, index, op):
00062 self.ops_[index] = op
00063
00064 def AddOp(self, op):
00065 self.ops_.append(op)
00066
00067 def GetOps(self):
00068 return self.ops_
00069
00070 def ApplyOn(self, entity):
00071 if (entity is not None) and isinstance(entity, gfx.Entity):
00072 for op in self.ops_:
00073 if isinstance(op,ColorOp):
00074 entity.Apply(op)
00075 else:
00076 op.ApplyOn(entity)
00077
00078 def ToInfo(self,group):
00079 group.SetAttribute(Preset.NAME_ATTRIBUTE_NAME, self.name_)
00080 for i in range(0,len(self.ops_)):
00081 op_group = group.CreateGroup(Preset.OP_GROUP_NAME)
00082 op_group.SetAttribute(Preset.INDEX_ATTRIBUTE_NAME, str(i))
00083 op_group.SetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME, "%s"%(self.ops_[i].__class__.__name__))
00084 self.ops_[i].ToInfo(op_group)
00085
00086 @staticmethod
00087 def FromInfo(group):
00088 preset = None
00089 if group.HasAttribute(Preset.NAME_ATTRIBUTE_NAME):
00090 name = group.GetAttribute(Preset.NAME_ATTRIBUTE_NAME)
00091 preset = Preset(name)
00092 group_list = group.GetGroups(Preset.OP_GROUP_NAME)
00093
00094 class_order_dict = dict()
00095 for op_group in group_list:
00096 if(op_group.HasAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME) and op_group.HasAttribute(Preset.INDEX_ATTRIBUTE_NAME)):
00097 class_name = op_group.GetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME)
00098 index = int(op_group.GetAttribute(Preset.INDEX_ATTRIBUTE_NAME))
00099 op_class = None
00100 for module in Preset.MODULE_NAMES:
00101 try:
00102 op_class = Preset.__get_op_class("%s.%s"%(module,class_name))
00103 break
00104 except AttributeError:
00105 pass
00106 if op_class is not None:
00107 op = op_class.FromInfo(op_group)
00108 class_order_dict[index]=op
00109 for i in range(0, len(class_order_dict)):
00110 if(class_order_dict.has_key(i)):
00111 preset.AddOp(class_order_dict[i])
00112 return preset
00113
00114 @staticmethod
00115 def __get_op_class( cls ):
00116 parts = cls.split('.')
00117 module = ".".join(parts[:-1])
00118 m = __import__( module )
00119 for comp in parts[1:]:
00120 m = getattr(m, comp)
00121 return m