OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
preset.py
Go to the documentation of this file.
1 #------------------------------------------------------------------------------
2 # This file is part of the OpenStructure project <www.openstructure.org>
3 #
4 # Copyright (C) 2008-2011 by the OpenStructure authors
5 #
6 # This library is free software; you can redistribute it and/or modify it under
7 # the terms of the GNU Lesser General Public License as published by the Free
8 # Software Foundation; either version 3.0 of the License, or (at your option)
9 # any later version.
10 # This library is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this library; if not, write to the Free Software Foundation, Inc.,
17 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 #------------------------------------------------------------------------------
19 # -*- coding: utf-8 -*-
20 
21 from ost import info
22 from ost import gfx
23 from PyQt4 import QtGui
24 
25 from ost.gfx import ColorOp
26 
27 #Rendering Preset
28 class Preset:
29  NAME_ATTRIBUTE_NAME = "Name"
30  OP_GROUP_NAME = "Op"
31  CLASS_NAME_ATTRIBUTE_NAME = "ClassName"
32  INDEX_ATTRIBUTE_NAME = "Index"
33 
34  MODULE_NAMES = ["ost.gfx","ost.gui.scene.visibility_op","ost.gui.scene.render_op"]
35 
36  def __init__(self, name, parent=None):
37  self.name_ = name
38  self.ops_ = list()
39 
40  def SetName(self, name):
41  self.name_ = name
42 
43  def GetName(self):
44  return self.name_
45 
46  def InsertOp(self, index, op):
47  self.ops_.insert(index, op)
48 
49  def RemoveOp(self, op):
50  self.ops_.remove(op)
51 
52  def RemoveOpAt(self, index):
53  del(self.ops_[index])
54 
55  def GetOp(self, index):
56  return self.ops_[index]
57 
58  def GetOpCount(self):
59  return len(self.ops_)
60 
61  def SetOp(self, index, op):
62  self.ops_[index] = op
63 
64  def AddOp(self, op):
65  self.ops_.append(op)
66 
67  def GetOps(self):
68  return self.ops_
69 
70  def ApplyOn(self, entity):
71  if (entity is not None) and isinstance(entity, gfx.Entity):
72  for op in self.ops_:
73  if isinstance(op,ColorOp):
74  entity.Apply(op)
75  else:
76  op.ApplyOn(entity)
77 
78  def ToInfo(self,group):
79  group.SetAttribute(Preset.NAME_ATTRIBUTE_NAME, self.name_)
80  for i in range(0,len(self.ops_)):
81  op_group = group.CreateGroup(Preset.OP_GROUP_NAME)
82  op_group.SetAttribute(Preset.INDEX_ATTRIBUTE_NAME, str(i))
83  op_group.SetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME, "%s"%(self.ops_[i].__class__.__name__))
84  self.ops_[i].ToInfo(op_group)
85 
86  @staticmethod
87  def FromInfo(group):
88  preset = None
89  if group.HasAttribute(Preset.NAME_ATTRIBUTE_NAME):
90  name = group.GetAttribute(Preset.NAME_ATTRIBUTE_NAME)
91  preset = Preset(name)
92  group_list = group.GetGroups(Preset.OP_GROUP_NAME)
93 
94  class_order_dict = dict()
95  for op_group in group_list:
96  if(op_group.HasAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME) and op_group.HasAttribute(Preset.INDEX_ATTRIBUTE_NAME)):
97  class_name = op_group.GetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME)
98  index = int(op_group.GetAttribute(Preset.INDEX_ATTRIBUTE_NAME))
99  op_class = None
100  for module in Preset.MODULE_NAMES:
101  try:
102  op_class = Preset.__get_op_class("%s.%s"%(module,class_name))
103  break
104  except AttributeError:
105  pass
106  if op_class is not None:
107  op = op_class.FromInfo(op_group)
108  class_order_dict[index]=op
109  for i in range(0, len(class_order_dict)):
110  if(class_order_dict.has_key(i)):
111  preset.AddOp(class_order_dict[i])
112  return preset
113 
114  @staticmethod
115  def __get_op_class( cls ):
116  parts = cls.split('.')
117  module = ".".join(parts[:-1])
118  m = __import__( module )
119  for comp in parts[1:]:
120  m = getattr(m, comp)
121  return m