Small program to test initial bin reading work
[crapvine.git] / character_window.py
blobeb487525c6723ba37dae73fd659cee9916863d2a
1 ## This file is part of Crapvine.
2 ##
3 ## Copyright (C) 2007 Andrew Sayman <lorien420@myrealbox.com>
4 ##
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.
9 ##
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
20 import gtk
21 import gobject
22 from xml.sax import make_parser
23 from xml.sax.handler import feature_namespaces
25 import configuration
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
38 import pdb
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)
53 if my_win:
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')
57 if notes:
58 my_vbox = TextBox('notes', self.overlord, self.character)
59 notes.add(my_vbox.get_vbox())
61 biography = self.xml.get_widget('biography')
62 if 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))
68 if my_win:
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))
74 if my_win:
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]
81 tmp_attr = pieces[7]
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)
89 window.show()
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)
111 response = dlg.run()
112 print response
113 dlg.hide()
114 if response == gtk.RESPONSE_ACCEPT:
115 print 'Accepted'
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(),
121 False)
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()
125 print date_tuple
126 e.date = datetime(date_tuple[0], date_tuple[1] + 1, date_tuple[2])
127 print e
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))
140 response = dlg.run()
141 print response
142 dlg.hide()
143 if response == gtk.RESPONSE_ACCEPT:
144 print 'Accepted'
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(),
149 False)
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()
153 print date_tuple
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()
161 file_chooser.hide()
162 if response == gtk.RESPONSE_ACCEPT:
163 out = ['<?xml version="1.0"?>',
164 '<grapevine version="3">',
165 self.character.get_xml(' '),
166 '</grapevine>']
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()
174 idx = en[0]
175 print title
176 print idx
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(
183 gobject.TYPE_STRING,
184 gobject.TYPE_STRING,
185 gobject.TYPE_STRING,
186 gobject.TYPE_STRING,
187 gobject.TYPE_STRING,
188 gobject.TYPE_STRING)
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:
198 print entry