gramps/gramps2/src/StyleEditor.py

276 lines
9.9 KiB
Python
Raw Normal View History

2002-10-20 19:55:16 +05:30
#
# Gramps - a GTK+/GNOME based genealogy program
#
# Copyright (C) 2000 Donald N. Allingham
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
"""
Paragraph/Font style editor
"""
__author__ = "Donald N. Allingham"
__version__ = "$Revision$"
#------------------------------------------------------------------------
#
# GNOME/GTK modules
#
#------------------------------------------------------------------------
import gtk.glade
import gtk
#------------------------------------------------------------------------
#
# GRAMPS modules
#
#------------------------------------------------------------------------
import Utils
import const
import TextDoc
2002-10-28 19:06:39 +05:30
import ListModel
2002-10-20 19:55:16 +05:30
class StyleListDisplay:
"""
Shows the available paragraph/font styles. Allows the user to select,
add, edit, and delete styles from a StyleSheet.
"""
def __init__(self,stylesheetlist,callback):
"""
Creates a StyleListDisplay object that displays the styles in the
StyleSheet.
stylesheetlist - styles that can be editied
callback - task called with an object has been added.
"""
self.callback = callback
self.sheetlist = stylesheetlist
self.top = gtk.glade.XML(const.stylesFile,"styles")
self.top.signal_autoconnect({
"destroy_passed_object" : Utils.destroy_passed_object,
"on_ok_clicked" : self.on_ok_clicked,
"on_add_clicked" : self.on_add_clicked,
"on_delete_clicked" : self.on_delete_clicked,
2002-10-28 19:06:39 +05:30
"on_button_press" : self.on_button_press,
2002-10-20 19:55:16 +05:30
"on_edit_clicked" : self.on_edit_clicked
})
2002-10-28 19:06:39 +05:30
self.list = ListModel.ListModel(self.top.get_widget("list"),[('Style',10,10)])
2002-10-20 19:55:16 +05:30
self.redraw()
def redraw(self):
"""Redraws the list of styles that are current available"""
2002-10-28 19:06:39 +05:30
self.list.model.clear()
2002-10-20 19:55:16 +05:30
sheet = self.sheetlist.get_style_sheet("default")
2002-10-28 19:06:39 +05:30
self.list.add(["default"])
2002-10-20 19:55:16 +05:30
index = 1
for style in self.sheetlist.get_style_names():
if style == "default":
continue
sheet = self.sheetlist.get_style_sheet(style)
2002-10-28 19:06:39 +05:30
self.list.add([style])
2002-10-20 19:55:16 +05:30
index = index + 1
def on_add_clicked(self,obj):
"""Called with the ADD button is clicked. Invokes the StyleEditor to
create a new style"""
style = self.sheetlist.get_style_sheet("default")
StyleEditor("New Style",style,self)
def on_ok_clicked(self,obj):
"""Called with the OK button is clicked; Calls the callback task, then
saves the stylesheet, and destroys the window."""
self.callback()
self.sheetlist.save()
Utils.destroy_passed_object(obj)
2002-10-28 19:06:39 +05:30
def on_button_press(self,obj,event):
if event.type == gtk.gdk._2BUTTON_PRESS and event.button == 1:
self.on_edit_clicked(obj)
2002-10-20 19:55:16 +05:30
def on_edit_clicked(self,obj):
"""
Called with the EDIT button is clicked. Calls the StyleEditor to edit the
selected style.
"""
2002-10-28 19:06:39 +05:30
store,iter = self.list.selection.get_selected()
if not iter:
return
name = self.list.model.get_value(iter,0)
style = self.sheetlist.get_style_sheet(name)
StyleEditor(name,style,self)
2002-10-20 19:55:16 +05:30
def on_delete_clicked(self,obj):
"""Deletes teh selected style."""
2002-10-28 19:06:39 +05:30
store,iter = self.list.selection.get_selected()
if not iter:
return
name = self.list.model.get_value(iter,0)
style = self.sheetlist.get_style_sheet(name)
self.sheetlist.delete_style_sheet(name)
self.redraw()
2002-10-20 19:55:16 +05:30
class StyleEditor:
"""
Edits the current style definition. Presents a dialog allowing the values of
the paragraphs in the style to be altered.
"""
def __init__(self,name,style,parent):
"""
Creates the StyleEditor.
name - name of the style that is to be edited
style - style object that is to be edited
parent - StyleListDisplay object that called the editor
"""
self.original_style = style
self.style = TextDoc.StyleSheet(style)
self.parent = parent
self.top = gtk.glade.XML(const.stylesFile,"editor")
self.current_p = None
self.top.signal_autoconnect({
"on_save_style_clicked" : self.on_save_style_clicked,
2002-10-28 19:06:39 +05:30
"fg_color_set":self.fg_color_set,
"bg_color_set":self.bg_color_set,
2002-10-20 19:55:16 +05:30
"destroy_passed_object" : Utils.destroy_passed_object
})
self.window = self.top.get_widget("editor")
self.pnames = self.top.get_widget("name")
self.top.get_widget("style_name").set_text(name)
2002-10-28 19:06:39 +05:30
myMenu = gtk.Menu()
2002-10-20 19:55:16 +05:30
first = 0
for p_name in self.style.get_names():
p = self.style.get_style(p_name)
if first == 0:
self.draw(p)
first = 1
2002-10-28 19:06:39 +05:30
menuitem = gtk.MenuItem(p_name)
2002-10-20 19:55:16 +05:30
menuitem.set_data("o",p)
menuitem.connect("activate",self.change_display)
menuitem.show()
myMenu.append(menuitem)
self.pnames.set_menu(myMenu)
def draw(self,p):
"""Updates the display with the selected paragraph."""
self.current_p = p
font = p.get_font()
self.top.get_widget("size").set_value(font.get_size())
if font.get_type_face() == TextDoc.FONT_SANS_SERIF:
self.top.get_widget("roman").set_active(1)
else:
self.top.get_widget("swiss").set_active(1)
self.top.get_widget("bold").set_active(font.get_bold())
self.top.get_widget("italic").set_active(font.get_italic())
self.top.get_widget("underline").set_active(font.get_underline())
if p.get_alignment() == TextDoc.PARA_ALIGN_LEFT:
self.top.get_widget("lalign").set_active(1)
elif p.get_alignment() == TextDoc.PARA_ALIGN_RIGHT:
self.top.get_widget("ralign").set_active(1)
elif p.get_alignment() == TextDoc.PARA_ALIGN_CENTER:
self.top.get_widget("calign").set_active(1)
else:
self.top.get_widget("jalign").set_active(1)
self.top.get_widget("rmargin").set_text(str(p.get_right_margin()))
self.top.get_widget("lmargin").set_text(str(p.get_left_margin()))
self.top.get_widget("pad").set_text(str(p.get_padding()))
self.top.get_widget("tborder").set_active(p.get_top_border())
self.top.get_widget("lborder").set_active(p.get_left_border())
self.top.get_widget("rborder").set_active(p.get_right_border())
self.top.get_widget("bborder").set_active(p.get_bottom_border())
c = font.get_color()
self.top.get_widget("color").set_i8(c[0],c[1],c[2],0)
c = p.get_background_color()
self.top.get_widget("bgcolor").set_i8(c[0],c[1],c[2],0)
2002-10-28 19:06:39 +05:30
def bg_color_set(self,obj,r,g,b,a):
print r,g,b,a
def fg_color_set(self,obj,r,g,b,a):
print r,g,b,ax
2002-10-20 19:55:16 +05:30
def save_paragraph(self,p):
"""Saves the current paragraph displayed on the dialog"""
font = p.get_font()
font.set_size(int(self.top.get_widget("size").get_value()))
if self.top.get_widget("roman").get_active():
font.set_type_face(TextDoc.FONT_SANS_SERIF)
else:
font.set_type_face(TextDoc.FONT_SERIF)
font.set_bold(self.top.get_widget("bold").get_active())
font.set_italic(self.top.get_widget("italic").get_active())
font.set_underline(self.top.get_widget("underline").get_active())
if self.top.get_widget("lalign").get_active():
p.set_alignment(TextDoc.PARA_ALIGN_LEFT)
elif self.top.get_widget("ralign").get_active():
p.set_alignment(TextDoc.PARA_ALIGN_RIGHT)
elif self.top.get_widget("calign").get_active():
p.set_alignment(TextDoc.PARA_ALIGN_CENTER)
else:
p.set_alignment(TextDoc.PARA_ALIGN_JUSTIFY)
p.set_right_margin(float(self.top.get_widget("rmargin").get_text()))
p.set_left_margin(float(self.top.get_widget("lmargin").get_text()))
p.set_padding(float(self.top.get_widget("pad").get_text()))
p.set_top_border(self.top.get_widget("tborder").get_active())
p.set_left_border(self.top.get_widget("lborder").get_active())
p.set_right_border(self.top.get_widget("rborder").get_active())
p.set_bottom_border(self.top.get_widget("bborder").get_active())
2002-10-28 19:06:39 +05:30
c = fg.get_i8()
2002-10-20 19:55:16 +05:30
font.set_color((c[0],c[1],c[2]))
2002-10-28 19:06:39 +05:30
c = bg.get_i8()
2002-10-20 19:55:16 +05:30
p.set_background_color((c[0],c[1],c[2]))
def on_save_style_clicked(self,obj):
"""
Saves the current style sheet and causes the parent to be updated with
the changes.
"""
p = self.current_p
name = self.top.get_widget("style_name").get_text()
self.save_paragraph(p)
self.parent.sheetlist.set_style_sheet(name,self.style)
self.parent.redraw()
Utils.destroy_passed_object(obj)
def change_display(self,obj):
"""Called when the paragraph selection has been changed. Saves the
old paragraph, then draws the newly selected paragraph"""
style = obj.get_data("o")
self.save_paragraph(self.current_p)
self.draw(style)