* PdfDoc.py: Support drawing functions.

* PdfDrawDoc.py: remove - all functions integrated into
PdfDoc.py
* DbPrompter.py: change title from "Save" to "Create" when
opening a new database, prompt with last database saved.


svn: r2064
This commit is contained in:
Don Allingham 2003-08-31 03:26:13 +00:00
parent 77a0a7f728
commit 65f18d93ac
3 changed files with 207 additions and 303 deletions

View File

@ -93,7 +93,10 @@ class DbPrompter:
"on_ok_button1_clicked": self.save_ok_button_clicked,
"destroy_passed_object": self.cancel_button_clicked,
})
wFs.get_widget('fileselection').set_title('%s - GRAMPS' % _('Save database'))
if self.new:
wFs.get_widget('fileselection').set_title('%s - GRAMPS' % _('Create database'))
else:
wFs.get_widget('fileselection').set_title('%s - GRAMPS' % _('Save database'))
def save_ok_button_clicked(self,obj):
filename = obj.get_filename().encode('iso8859-1')
@ -119,8 +122,14 @@ class DbPrompter:
self.getoldrev = wFs.get_widget("getoldrev")
if GrampsCfg.db_dir:
self.dbname.set_default_path(GrampsCfg.db_dir)
if GrampsCfg.lastfile:
self.dbname.set_filename(GrampsCfg.lastfile)
self.dbname.gtk_entry().set_position(len(GrampsCfg.lastfile))
elif GrampsCfg.db_dir:
self.dbname.set_filename(GrampsCfg.db_dir)
self.dbname.gtk_entry().set_position(len(GrampsCfg.db_dir))
self.getoldrev.set_sensitive(GrampsCfg.usevc)
def cancel_button_clicked(self,obj):

View File

@ -29,6 +29,11 @@ import Errors
import ImgManip
from gettext import gettext as _
_H = 'Helvetica'
_HB = 'Helvetica-Bold'
_T = 'Times-Roman'
_TB = 'Times-Bold'
#------------------------------------------------------------------------
#
# ReportLab python/PDF modules
@ -41,6 +46,7 @@ try:
from reportlab.lib.units import cm
from reportlab.lib.colors import Color
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
from reportlab.graphics.shapes import *
import reportlab.lib.styles
except ImportError:
raise Errors.PluginError( _("The ReportLab modules are not installed"))
@ -301,6 +307,189 @@ class PdfDoc(BaseDoc.BaseDoc):
text = text.replace('&lt;/super&gt;','</super></font>')
self.text = self.text + text.replace('\n','<br>')
def print_report(self):
return run_print_dialog (self.filename)
def start_page(self,orientation=None):
self.drawing = Drawing(self.get_usable_width()*cm, self.get_usable_height()*cm)
def end_page(self):
self.story.append(self.drawing)
def draw_line(self,style,x1,y1,x2,y2):
y1 = self.get_usable_height() - y1
y2 = self.get_usable_height() - y2
stype = self.draw_styles[style]
if stype.get_line_style() == BaseDoc.SOLID:
line_array = None
else:
line_array = [2,4]
self.drawing.add(Line(x1*cm,y1*cm,x2*cm,y2*cm,
strokeWidth=stype.get_line_width(),
strokeDashArray=line_array))
def draw_bar(self,style,x1,y1,x2,y2):
pass
def draw_path(self,style,path):
stype = self.draw_styles[style]
color = make_color(stype.get_fill_color())
y = self.get_usable_height()*cm
if stype.get_line_style() == BaseDoc.SOLID:
line_array = None
else:
line_array = [2,4]
p = Path(strokeWidth=stype.get_line_width(),
strokeDashArray=line_array,
fillColor=color,
strokeColor=make_color(stype.get_color()))
point = path[0]
p.moveTo(point[0]*cm,y-point[1]*cm)
for point in path[1:]:
p.lineTo(point[0]*cm,y-point[1]*cm)
p.closePath()
self.drawing.add(p)
def draw_box(self,style,text,x,y):
y = self.get_usable_height() - y
box_style = self.draw_styles[style]
para_name = box_style.get_paragraph_style()
p = self.style_list[para_name]
w = box_style.get_width()*cm
h = box_style.get_height()*cm
if box_style.get_shadow():
col = make_color((0xc0,0xc0,0xc0))
r = Rect((x+0.2)*cm,(y-0.2)*cm-h,w,h,
fillColor=col,
strokeColor=col)
self.drawing.add(r)
self.drawing.add(Rect((x)*cm,(y*cm)-h,w,h,
strokeWidth=box_style.get_line_width(),
fillColor=box_style.get_fill_color(),
strokeColor=box_style.get_color()))
size = p.get_font().get_size()
x = x + 0.2
if text != "":
lines = text.split('\n')
self.left_print(lines,p.get_font(),x*cm,y*cm - size)
def draw_text(self,style,text,x,y):
stype = self.draw_styles[style]
pname = stype.get_paragraph_style()
p = self.style_list[pname]
font = p.get_font()
size = font.get_size()
s = String(x*cm,
(self.get_usable_height()*cm)-(y*cm),
str(text),
strokeColor=make_color(font.get_color()),
fillColor=make_color(font.get_color()),
fontName=self.pdf_set_font(font),
fontSize=size)
self.drawing.add(s)
def pdf_set_font(self,font):
if font.get_type_face() == BaseDoc.FONT_SANS_SERIF:
if font.get_bold():
return _HB
else:
return _H
else:
if font.get_bold():
return _TB
else:
return _T
def rotate_text(self,style,text,x,y,angle):
stype = self.draw_styles[style]
pname = stype.get_paragraph_style()
p = self.style_list[pname]
font = p.get_font()
size = font.get_size()
yt = (self.get_usable_height()*cm) - (y*cm)
yval = 0
g = Group()
for line in text:
s = String(0,yval,str(line),
fontName=self.pdf_set_font(font),
fontSize=size,
strokeColor=make_color(font.get_color()),
fillColor=make_color(font.get_color()),
textAnchor='middle')
yval -= size
g.add(s)
g.translate(x*cm,yt)
g.rotate(-angle)
self.drawing.add(g)
def center_text(self,style,text,x,y):
stype = self.draw_styles[style]
pname = stype.get_paragraph_style()
p = self.style_list[pname]
font = p.get_font()
yt = (self.get_usable_height()*cm) - (y*cm)
s = String(x*cm,
yt,
str(text),
fontName=self.pdf_set_font(font),
fontSize=font.get_size(),
strokeColor=make_color(font.get_color()),
fillColor=make_color(font.get_color()),
textAnchor='middle')
self.drawing.add(s)
def center_print(self,lines,font,x,y,w,h):
l = len(lines)
size = font.get_size()
start_y = (y + h/2.0 + l/2.0 + l) - ((l*size) + ((l-1)*0.2))/2.0
start_x = (x + w/2.0)
for text in lines:
s = String(startx, start_y,
str(line),
fontName=self.pdf_set_font(font),
fontSize=font.get_size(),
strokeColor=make_color(font.get_color()),
fillColor=make_color(font.get_color()),
)
self.drawing.add(String(start_x,start_y,str(text)))
start_y = start_y - size*1.2
def left_print(self,lines,font,x,y):
l = len(lines)
size = font.get_size()
start_y = self.get_usable_height() - (y*cm)
start_x = x * cm
for text in lines:
s = String(start_x,
start_y,
str(text),
fontSize=size,
strokeColor=make_color(font.get_color()),
fillColor=make_color(font.get_color()),
fontName=self.pdf_set_font(font))
self.drawing.add(s)
start_y = start_y - size*1.2
def make_color(c):
return Color(float(c[0])/255.0, float(c[1])/255.0, float(c[2])/255.0)
#------------------------------------------------------------------------
#
# Convert an RGB color tulple to a Color instance
@ -315,6 +504,14 @@ def make_color(c):
#
#------------------------------------------------------------------------
Plugins.register_draw_doc(
_("PDF"),
PdfDoc,
1,
1,
".pdf"
)
Plugins.register_text_doc(
name=_("PDF"),
classref=PdfDoc,

View File

@ -1,302 +0,0 @@
#
# 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
#
#-------------------------------------------------------------------------
#
# Standard python libraries
#
#-------------------------------------------------------------------------
from math import sin, cos, pi
#-------------------------------------------------------------------------
#
# GRAMPS libraries
#
#-------------------------------------------------------------------------
import Plugins
import Errors
import TextDoc
import DrawDoc
from Report import run_print_dialog
from gettext import gettext as _
#-------------------------------------------------------------------------
#
# ReportLab PDF detection, PIL detection
#
#-------------------------------------------------------------------------
try:
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from reportlab.lib.colors import Color
except ImportError:
raise Errors.PluginError( _("The ReportLab modules are not installed"))
_H = 'Helvetica'
_HB = 'Helvetica-Bold'
_T = 'Times-Roman'
_TB = 'Times-Bold'
#-------------------------------------------------------------------------
#
# PdfDrawDoc
#
#-------------------------------------------------------------------------
class PdfDrawDoc(DrawDoc.DrawDoc):
"""
PDF drawing class. Uses the ReportLab libraries to build the PDF files
"""
def __init__(self,styles,type,orientation):
DrawDoc.DrawDoc.__init__(self,styles,type,orientation)
self.f = None
self.filename = None
self.level = 0
self.time = "0000-00-00T00:00:00"
self.page = 0
def open(self,filename):
if filename[-4:] != ".pdf":
self.filename = filename + ".pdf"
else:
self.filename = filename
self.f = canvas.Canvas(self.filename,(self.width*cm,self.height*cm),0)
if self.name:
self.f.setAuthor(self.name)
def close(self):
try:
self.f.save()
except IOError,msg:
raise Errors.ReportError(_("Could not create %s") % self.filename,msg)
except:
raise Errors.ReportError(_("Could not create %s") % self.filename)
def print_report(self):
return run_print_dialog (self.filename)
def start_paragraph(self,style_name):
pass
def end_paragraph(self):
pass
def write_text(self,text):
pass
def start_page(self,orientation=None):
pass
def end_page(self):
self.f.showPage()
def draw_line(self,style,x1,y1,x2,y2):
x1 = x1 + self.lmargin
x2 = x2 + self.lmargin
y1 = y1 + self.tmargin
y2 = y2 + self.tmargin
stype = self.draw_styles[style]
if stype.get_line_style() == DrawDoc.SOLID:
self.f.setDash([],0)
else:
self.f.setDash([2,4],0)
self.f.setLineWidth(stype.get_line_width())
self.f.line(x1*cm,y1*cm,x2*cm,y2*cm)
def draw_bar(self,style,x1,y1,x2,y2):
x1 = x1 + self.lmargin
x2 = x2 + self.lmargin
y1 = y1 + self.tmargin
y2 = y2 + self.tmargin
stype = self.draw_styles[style]
if stype.get_line_style() == DrawDoc.SOLID:
self.f.setDash([],0)
else:
self.f.setDash([2,4],0)
self.f.setLineWidth(stype.get_line_width())
self.f.rect(x1*cm,y1*cm,(x2-x1)*cm,(y2-y1)*cm,fill=0,stroke=1)
def draw_path(self,style,path):
stype = self.draw_styles[style]
if stype.get_line_style() == DrawDoc.SOLID:
self.f.setDash([],0)
else:
self.f.setDash([2,4],0)
self.f.setLineWidth(stype.get_line_width())
color = stype.get_fill_color()
self.f.setFillColor((float(color[0])/255.0,float(color[1])/255.0,
float(color[2])/255.0))
p = self.f.beginPath()
point = path[0]
p.moveTo((point[0]+self.lmargin)*cm,(point[1]+self.tmargin)*cm)
for point in path[1:]:
p.lineTo((point[0]+self.lmargin)*cm,(point[1]+self.tmargin)*cm)
p.close()
fill = stype.get_color()
if fill[0] == 0:
self.f.drawPath(p,stroke=1,fill=1)
else:
self.f.drawPath(p,stroke=1,fill=0)
def draw_box(self,style,text,x,y):
x = x + self.lmargin
y = y + self.tmargin
box_style = self.draw_styles[style]
para_name = box_style.get_paragraph_style()
p = self.style_list[para_name]
w = box_style.get_width()*cm
h = box_style.get_height()*cm
self.f.setLineWidth(box_style.get_line_width())
if box_style.get_shadow():
self.f.setFillColorRGB(0.5,0.5,0.5)
self.f.rect((x+0.3)*cm,(y+0.3)*cm,w,h,fill=1,stroke=0)
font = p.get_font()
self.f.setStrokeColor(make_color(font.get_color()))
self.f.setFillColor(make_color(box_style.get_fill_color()))
self.f.rect(x*cm,y*cm,w,h,fill=1)
if text != "":
lines = text.split('\n')
self.center_print(lines,font,x*cm,y*cm,w,h)
def draw_text(self,style,text,x,y):
x = x + self.lmargin
y = y + self.tmargin
stype = self.draw_styles[style]
pname = stype.get_paragraph_style()
p = self.style_list[pname]
font = p.get_font()
self.f.setStrokeColor(make_color(font.get_color()))
self.left_print(text,font,x*cm,(y*cm)+font.get_size())
def pdf_set_font(self,font):
size = font.get_size()
if font.get_type_face() == TextDoc.FONT_SANS_SERIF:
if font.get_bold():
self.f.setFont(_HB,size)
else:
self.f.setFont(_H,size)
else:
if font.get_bold():
self.f.setFont(_TB,size)
else:
self.f.setFont(_T,size)
def rotate_text(self,style,text,x,y,angle):
x += self.lmargin
y += self.tmargin
stype = self.draw_styles[style]
pname = stype.get_paragraph_style()
p = self.style_list[pname]
font = p.get_font()
size = font.get_size()
self.f.saveState()
self.pdf_set_font(font)
self.f.translate(x*cm,y*cm)
self.f.rotate(angle)
self.f.setStrokeColor(make_color(font.get_color()))
self.f.setFillColor(make_color(font.get_color()))
val = len(text)
y = ((-size * val)/2.0) + size
for line in text:
self.f.drawCentredString(0,y,line.encode('iso-8859-1'))
y += size
self.f.restoreState()
def center_text(self,style,text,x,y):
x += self.lmargin
y += self.tmargin
stype = self.draw_styles[style]
pname = stype.get_paragraph_style()
p = self.style_list[pname]
font = p.get_font()
self.f.saveState()
self.f.setStrokeColor(make_color(font.get_color()))
self.f.setFillColor(make_color(font.get_color()))
self.pdf_set_font(font)
self.f.drawCentredString(x*cm,y*cm,text.encode('iso-8859-1'))
self.f.restoreState()
def center_print(self,lines,font,x,y,w,h):
l = len(lines)
size = font.get_size()
start_y = (y + h/2.0 + l/2.0 + l) - ((l*size) + ((l-1)*0.2))/2.0
start_x = (x + w/2.0)
self.f.saveState()
self.f.setFillColor(make_color(font.get_color()))
self.f.setStrokeColor(make_color(font.get_color()))
self.pdf_set_font(font)
for text in lines:
self.f.drawCentredString(start_x,start_y,text.encode('iso-8859-1'))
start_y = start_y + size*1.2
start_y = start_y + size*1.2
self.f.restoreState()
def left_print(self,text,font,x,y):
self.f.saveState()
self.f.setStrokeColor(make_color(font.get_color()))
self.pdf_set_font(font)
self.f.drawString(x,y,text.encode('iso-8859-1'))
self.f.restoreState()
def make_color(c):
return Color(float(c[0])/255.0, float(c[1])/255.0, float(c[2])/255.0)
#-------------------------------------------------------------------------
#
# Register the document class
#
#-------------------------------------------------------------------------
Plugins.register_draw_doc(_("PDF"),PdfDrawDoc,1,1,".pdf");