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/>.
18 from __future__
import with_statement
22 from xml
.sax
import make_parser
23 from xml
.sax
.handler
import feature_namespaces
26 from traitlist_box
import TraitlistBox
27 from text_box
import TextBox
28 from text_attribute_box
import TextAttributeBox
29 from number_as_text_attribute_box
import NumberAsTextAttributeBox
30 from number_as_text_with_temporary_attribute_box
import NumberAsTextWithTemporaryAttributeBox
31 from menu_navigator
import MenuNavigator
33 from trait
import Trait
34 from experience
import ExperienceEntry
35 from dateutil
.parser
import parse
36 from datetime
import datetime
, date
40 class CharacterWindow
:
41 def __init__(self
, character
):
42 self
.character
= character
43 self
.xml
= gtk
.glade
.XML(configuration
.get_grapevine_xml_file_path())
44 self
.overlord
= MenuNavigator(self
.xml
)
46 parser
= make_parser()
47 parser
.setFeature(feature_namespaces
, 0)
48 parser
.setContentHandler(self
.overlord
.menu_loader
)
49 parser
.parse(configuration
.get_menu_file_path())
51 for tlname
in [tl
.name
for tl
in self
.character
.traitlists
]:
52 my_win
= self
.xml
.get_widget(tlname
)
54 my_vbox
= TraitlistBox(tlname
, tlname
, self
.overlord
, self
.character
)
55 my_win
.add(my_vbox
.get_vbox())
56 notes
= self
.xml
.get_widget('notes')
58 my_vbox
= TextBox('notes', self
.overlord
, self
.character
)
59 notes
.add(my_vbox
.get_vbox())
61 biography
= self
.xml
.get_widget('biography')
63 my_vbox
= TextBox('biography', self
.overlord
, self
.character
)
64 biography
.add(my_vbox
.get_vbox())
66 for text_attr_name
in self
.character
.text_attrs
:
67 my_win
= self
.xml
.get_widget('text_attr_%s' % (text_attr_name
))
69 my_vbox
= TextAttributeBox(text_attr_name
, text_attr_name
, self
.overlord
, self
.character
)
70 my_win
.add(my_vbox
.get_vbox())
72 for nat_name
in self
.character
.number_as_text_attrs
:
73 my_win
= self
.xml
.get_widget('number_as_text_attr_%s' % (nat_name
))
75 my_vbox
= NumberAsTextAttributeBox(nat_name
, nat_name
, self
.overlord
, self
.character
)
76 my_win
.add(my_vbox
.get_vbox())
78 for my_win
in self
.xml
.get_widget_prefix('number_as_text_with_temporary_attr_'):
79 pieces
= my_win
.get_name().split('_')
80 primary_attr
= pieces
[6]
82 my_vbox_generator
= NumberAsTextWithTemporaryAttributeBox(primary_attr
, primary_attr
, tmp_attr
, self
.overlord
, self
.character
)
83 my_win
.add(my_vbox_generator
.get_vbox())
85 self
.__create
_experience
_tree
(self
.character
.experience
)
87 window
= self
.xml
.get_widget('winCharacter')
88 window
.set_title(character
.name
)
91 self
.xml
.signal_autoconnect({
92 'on_btnAddTrait_clicked' : self
.overlord
.on_btnAddTrait_clicked
,
93 'on_btnRemoveTrait_clicked' : self
.overlord
.on_btnRemoveTrait_clicked
,
94 'on_treeMenu_row_activated' : self
.overlord
.on_treeMenu_row_activated
,
95 'on_save_as' : self
.on_save_as
,
96 'add_custom_entry' : self
.overlord
.add_custom
,
97 'add_note_to_entry' : self
.overlord
.add_note
,
98 'add_experience' : self
.add_experience
,
99 'activate_row' : self
.activate_row
103 def add_experience(self
, widget
=None):
104 dlg_xml
= gtk
.glade
.XML(configuration
.get_add_experience_xml_file_path())
105 dlg
= dlg_xml
.get_widget('add_experience_entry')
106 dlg_xml
.get_widget('type').set_active(0)
107 date_widget
= dlg_xml
.get_widget('date')
108 now_date
= datetime
.now()
109 date_widget
.select_month(now_date
.month
- 1, now_date
.year
)
110 date_widget
.select_day(now_date
.day
)
114 if response
== gtk
.RESPONSE_ACCEPT
:
116 e
= ExperienceEntry()
117 buffer = dlg_xml
.get_widget('reason').get_buffer()
118 e
.reason
= buffer.get_text(
119 buffer.get_start_iter(),
120 buffer.get_end_iter(),
122 e
.change
= dlg_xml
.get_widget('change').get_value()
123 e
.type = dlg_xml
.get_widget('type').get_active()
124 date_tuple
= date_widget
.get_date()
126 e
.date
= datetime(date_tuple
[0], date_tuple
[1] + 1, date_tuple
[2])
128 self
.character
.experience
.add_entry(e
)
129 def activate_row(self
, treeview
, path
, view_column
):
130 experience_entry
= treeview
.get_model().get_item_from_path(path
)
131 dlg_xml
= gtk
.glade
.XML(configuration
.get_add_experience_xml_file_path())
132 dlg
= dlg_xml
.get_widget('add_experience_entry')
133 dlg_xml
.get_widget('type').set_active(int(experience_entry
.type))
134 date_widget
= dlg_xml
.get_widget('date')
135 ee_date
= experience_entry
.date
136 date_widget
.select_month(ee_date
.month
- 1, ee_date
.year
)
137 date_widget
.select_day(ee_date
.day
)
138 dlg_xml
.get_widget('reason').get_buffer().set_text(experience_entry
.reason
)
139 dlg_xml
.get_widget('change').set_value(int(experience_entry
.change
))
143 if response
== gtk
.RESPONSE_ACCEPT
:
145 buffer = dlg_xml
.get_widget('reason').get_buffer()
146 experience_entry
.reason
= buffer.get_text(
147 buffer.get_start_iter(),
148 buffer.get_end_iter(),
150 experience_entry
.change
= dlg_xml
.get_widget('change').get_value()
151 experience_entry
.type = dlg_xml
.get_widget('type').get_active()
152 date_tuple
= date_widget
.get_date()
154 experience_entry
.date
= datetime(date_tuple
[0], date_tuple
[1] + 1, date_tuple
[2])
155 model
= treeview
.get_model()
156 model
.update_entry(path
, experience_entry
)
158 def on_save_as(self
, menuitem
):
159 file_chooser
= gtk
.FileChooserDialog('Choose Where to Save %s' % (self
.character
.name
), None, gtk
.FILE_CHOOSER_ACTION_SAVE
, (gtk
.STOCK_CANCEL
, gtk
.RESPONSE_REJECT
, gtk
.STOCK_OK
, gtk
.RESPONSE_ACCEPT
))
160 response
= file_chooser
.run()
162 if response
== gtk
.RESPONSE_ACCEPT
:
163 out
= ['<?xml version="1.0"?>',
164 '<grapevine version="3">',
165 self
.character
.get_xml(' '),
167 with
file(file_chooser
.get_filename(), 'w') as f
:
168 f
.write("\n".join(out
))
170 def __create_experience_tree(self
, experience
):
171 self
.tree_experience
= self
.xml
.get_widget('treeExperience')
172 for en
in enumerate(experience
.column_attrs
):
173 title
= en
[1].capitalize()
177 renderer
= gtk
.CellRendererText()
178 renderer
.set_data("column", idx
)
179 column
= gtk
.TreeViewColumn(title
, renderer
, text
=idx
)
180 self
.tree_experience
.append_column(column
)
182 model
= gtk
.ListStore(
190 for entry
in experience
.entries
:
191 iter = model
.append()
192 for i
in range(len(experience
.column_attrs
)):
193 model
.set(iter, i
, entry
[experience
.column_attrs
[i
]])
194 self
.tree_experience
.set_model(self
.character
.experience
)
197 for entry
in experience
.entries
: