1 ## This file is part of Crapvine.
3 ## Copyright (C) 2007 Andrew Sayman <lorien420@myrealbox.com>
5 ## Crapvine is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or
8 ## (at your option) any later version.
10 ## Crapvine is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ## GNU General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from grapevine_xml
import Attributed
, AttributedListModel
26 from attribute
import AttributeBuilder
28 class TraitList(AttributedListModel
):
29 required_attrs
= ['name']
30 number_as_text_attrs
= ['display']
31 bool_attrs
= ['abc', 'atomic', 'negative']
32 defaults
= { 'atomic' : False, 'negative' : False }
34 column_attrs
= [ 'name', 'val', 'note' ]
35 column_attr_types
= [ gobject
.TYPE_STRING
, gobject
.TYPE_STRING
, gobject
.TYPE_STRING
]
40 AttributedListModel
.__init
__(self
)
42 self
.traits
= self
.list
44 def add_menu_item(self
, menu_item
):
46 t
.name
= copy
.copy(menu_item
.name
)
47 t
.note
= copy
.copy(menu_item
.note
)
48 t
.val
= copy
.copy(menu_item
.cost
)
51 def add_trait(self
, trait
):
53 self
.traits
.append(trait
)
54 path
= (len(self
.traits
) - 1, )
55 self
.row_inserted(path
, self
.get_iter(path
))
57 if trait
.name
in [t
.name
for t
in self
.traits
]:
58 for en
in enumerate(self
.traits
):
60 if trait
.name
== t
.name
:
63 t
.val
= str(float(t
.val
) + float(trait
.val
))
69 self
.row_changed(path
, self
.get_iter(path
))
71 self
.traits
.append(trait
)
72 path
= (len(self
.traits
) - 1, )
73 self
.row_inserted(path
, self
.get_iter(path
))
75 def increment_trait(self
, trait_name
):
76 if trait_name
in [t
.name
for t
in self
.traits
]:
77 for en
in enumerate(self
.traits
):
79 if trait_name
== t
.name
:
82 t
.val
= str(float(t
.val
) + 1)
86 self
.row_changed(path
, self
.get_iter(path
))
88 raise ValueError('Unknown trait')
90 def decrement_trait(self
, trait_name
):
91 if trait_name
in [t
.name
for t
in self
.traits
]:
92 for en
in enumerate(self
.traits
):
94 if trait_name
== t
.name
:
99 self
.row_deleted(path
)
102 t
.val
= str(float(t
.val
) - 1)
106 self
.row_changed(path
, self
.get_iter(path
))
108 raise ValueError('Unknown trait')
110 def get_total_value(self
):
112 for t
in self
.traits
:
118 print t
.val
.__class
__
122 def get_num_entries(self
):
123 return len(self
.traits
)
125 def get_display_total(self
):
127 return self
.get_num_entries()
129 return self
.get_total_value()
131 def get_xml(self
, indent
=''):
132 end_tag
= ">\n" if len(self
.traits
) > 0 else "/>"
133 ret
= '%s<traitlist %s%s' % (indent
, self
.get_attrs_xml(), end_tag
)
134 local_indent
= '%s ' % (indent
)
135 ret
+= "\n".join([trait
.get_xml(local_indent
) for trait
in self
.traits
])
136 if len(self
.traits
) > 0:
137 ret
+= '%s%s</traitlist>' % ("\n", indent
)
140 return self
.get_xml()
142 class Trait(Attributed
):
143 required_attrs
= ['name']
144 text_attrs
= ['note', 'cumguzzle']
145 number_as_text_attrs
= ['val']
146 defaults
= { 'val' : '1' }
148 def get_xml(self
, indent
=''):
149 return '%s<trait %s/>' % (indent
, self
.get_attrs_xml())
151 return self
.get_xml()
153 def __val_as_float(self
):
155 return float(self
.val
)
158 def __show_note(self
):
159 return True if self
.note
else False
160 def __show_val(self
):
161 return True if self
.val
else False
162 def __tally_val(self
):
163 return True if self
.__val
_as
_float
else False
165 def tally_str(self
, dot
="O"):
166 if not self
.__tally
_val
():
169 num_dots
= int(round(self
.__val
_as
_float
()))
171 for i
in range(num_dots
):
174 def display_str(self
, display
="1", dot
="O"):
175 show_note
= self
.__show
_note
()
176 show_val
= self
.__show
_val
()
177 tally_val
= self
.__tally
_val
()
180 return "%s" % (self
.name
)
182 vstr
= (" x%s" % (self
.val
)) if show_val
else ''
183 nstr
= (" (%s)" % (self
.note
)) if show_note
else ''
184 return "%s%s%s" % (self
.name
, vstr
, nstr
)
187 vstr
= (" x%s" % (self
.val
))
189 vstr
+= " %s" % (self
.tally_str(dot
))
190 nstr
= (" (%s)" % (self
.note
)) if show_note
else ''
191 return "%s%s%s" % (self
.name
, vstr
, nstr
)
194 vstr
= " %s" % (self
.tally_str(dot
))
195 nstr
= (" (%s)" % (self
.note
)) if show_note
else ''
196 return "%s%s%s" % (self
.name
, vstr
, nstr
)
199 if show_note
and show_val
:
200 paren_str
= " (%s, %s)" % (self
.val
, self
.note
)
201 elif show_note
and not show_val
:
202 paren_str
= " (%s)" % (self
.note
)
203 elif show_val
and not show_note
:
204 paren_str
= " (%s)" % (self
.val
)
205 return "%s%s" % (self
.name
, paren_str
)
209 paren_str
= " (%s)" % (self
.note
)
210 return "%s%s" % (self
.name
, paren_str
)
214 paren_str
= " (%s)" % (self
.val
)
215 return "%s%s" % (self
.name
, paren_str
)
217 paren_str
= (" (%s)" % (self
.note
)) if show_note
else ''
218 dstr
= "%s%s" % (self
.name
, paren_str
)
220 for i
in range(int(round(self
.__val
_as
_float
()))):
222 return ("%s" % (dot
)).join(its
)
224 return self
.tally_str(dot
)
227 return "%s" % (self
.val
)
230 elif display
== "10":
232 return "%s" % (self
.note
)
235 elif display
== "Default":
236 return self
.display_str()