OpenStructure
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-2020 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 
24 from ost.gfx import ColorOp
25 
26 #Rendering Preset
27 class Preset:
28  NAME_ATTRIBUTE_NAME = "Name"
29  OP_GROUP_NAME = "Op"
30  CLASS_NAME_ATTRIBUTE_NAME = "ClassName"
31  INDEX_ATTRIBUTE_NAME = "Index"
32 
33  MODULE_NAMES = ["ost.gfx","ost.gui.scene.visibility_op","ost.gui.scene.render_op"]
34 
35  def __init__(self, name, parent=None):
36  self.name_name_ = name
37  self.ops_ops_ = list()
38 
39  def SetName(self, name):
40  self.name_name_ = name
41 
42  def GetName(self):
43  return self.name_name_
44 
45  def InsertOp(self, index, op):
46  self.ops_ops_.insert(index, op)
47 
48  def RemoveOp(self, op):
49  self.ops_ops_.remove(op)
50 
51  def RemoveOpAt(self, index):
52  del(self.ops_ops_[index])
53 
54  def GetOp(self, index):
55  return self.ops_ops_[index]
56 
57  def GetOpCount(self):
58  return len(self.ops_ops_)
59 
60  def SetOp(self, index, op):
61  self.ops_ops_[index] = op
62 
63  def AddOp(self, op):
64  self.ops_ops_.append(op)
65 
66  def GetOps(self):
67  return self.ops_ops_
68 
69  def ApplyOn(self, entity):
70  if (entity is not None) and isinstance(entity, gfx.Entity):
71  for op in self.ops_ops_:
72  if isinstance(op,ColorOp):
73  entity.Apply(op)
74  else:
75  op.ApplyOn(entity)
76 
77  def ToInfo(self,group):
78  group.SetAttribute(Preset.NAME_ATTRIBUTE_NAME, self.name_name_)
79  for i in range(0,len(self.ops_ops_)):
80  op_group = group.CreateGroup(Preset.OP_GROUP_NAME)
81  op_group.SetAttribute(Preset.INDEX_ATTRIBUTE_NAME, str(i))
82  op_group.SetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME, "%s"%(self.ops_ops_[i].__class__.__name__))
83  self.ops_ops_[i].ToInfo(op_group)
84 
85  @staticmethod
86  def FromInfo(group):
87  preset = None
88  if group.HasAttribute(Preset.NAME_ATTRIBUTE_NAME):
89  name = group.GetAttribute(Preset.NAME_ATTRIBUTE_NAME)
90  preset = Preset(name)
91  group_list = group.GetGroups(Preset.OP_GROUP_NAME)
92 
93  class_order_dict = dict()
94  for op_group in group_list:
95  if(op_group.HasAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME) and op_group.HasAttribute(Preset.INDEX_ATTRIBUTE_NAME)):
96  class_name = op_group.GetAttribute(Preset.CLASS_NAME_ATTRIBUTE_NAME)
97  index = int(op_group.GetAttribute(Preset.INDEX_ATTRIBUTE_NAME))
98  op_class = None
99  for module in Preset.MODULE_NAMES:
100  try:
101  op_class = Preset.__get_op_class("%s.%s"%(module,class_name))
102  break
103  except AttributeError:
104  pass
105  if op_class is not None:
106  op = op_class.FromInfo(op_group)
107  class_order_dict[index]=op
108  for i in range(0, len(class_order_dict)):
109  if(i in class_order_dict):
110  preset.AddOp(class_order_dict[i])
111  return preset
112 
113  @staticmethod
114  def __get_op_class( cls ):
115  parts = cls.split('.')
116  module = ".".join(parts[:-1])
117  m = __import__( module )
118  for comp in parts[1:]:
119  m = getattr(m, comp)
120  return m
graphical rendering of mol::EntityHandle entites
Definition: entity.hh:60
def RemoveOp(self, op)
Definition: preset.py:48
def AddOp(self, op)
Definition: preset.py:63
def SetOp(self, index, op)
Definition: preset.py:60
def SetName(self, name)
Definition: preset.py:39
def ToInfo(self, group)
Definition: preset.py:77
def GetOp(self, index)
Definition: preset.py:54
def ApplyOn(self, entity)
Definition: preset.py:69
def __init__(self, name, parent=None)
Definition: preset.py:35
def RemoveOpAt(self, index)
Definition: preset.py:51
def InsertOp(self, index, op)
Definition: preset.py:45