2007-09-12 Don Allingham <don@gramps-project.org>

* src/docgen/ODFDoc.py (ODFDoc): Fix ratio assignment



svn: r8965
This commit is contained in:
Don Allingham 2007-09-12 15:02:03 +00:00
parent 1cc238eea2
commit 04e249f902
3 changed files with 106 additions and 119 deletions

View File

@ -1,3 +1,6 @@
2007-09-12 Don Allingham <don@gramps-project.org>
* src/docgen/ODFDoc.py (ODFDoc): Fix ratio assignment
2007-09-12 Zsolt Foldvari <zfoldvar@users.sourceforge.net> 2007-09-12 Zsolt Foldvari <zfoldvar@users.sourceforge.net>
* src/plugins/IndivComplete.py (write_person): * src/plugins/IndivComplete.py (write_person):
* src/plugins/IndivSummary.py (write_report): * src/plugins/IndivSummary.py (write_report):

View File

@ -10,7 +10,7 @@
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
@ -61,12 +61,12 @@ from xml.sax.saxutils import escape
_apptype = 'application/vnd.oasis.opendocument.text' _apptype = 'application/vnd.oasis.opendocument.text'
_esc_map = { _esc_map = {
'\x1a' : '', '\x1a' : '',
'\x0c' : '', '\x0c' : '',
'\n' : '<text:line-break/>', '\n' : '<text:line-break/>',
'\t' : '<text:tab />', '\t' : '<text:tab />',
'&lt;super&gt;' : '<text:span text:style-name="GSuper">', '&lt;super&gt;' : '<text:span text:style-name="GSuper">',
'&lt;/super&gt;' : '</text:span>', '&lt;/super&gt;' : '</text:span>',
} }
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
@ -74,10 +74,10 @@ _esc_map = {
# ODFDoc # ODFDoc
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc): class ODFDoc(BaseDoc.BaseDoc, BaseDoc.TextDoc, BaseDoc.DrawDoc):
def __init__(self,styles,type,template): def __init__(self, styles, type, template):
BaseDoc.BaseDoc.__init__(self,styles,type,template) BaseDoc.BaseDoc.__init__(self, styles, type, template)
self.media_list = [] self.media_list = []
self.cntnt = None self.cntnt = None
self.filename = None self.filename = None
@ -88,10 +88,10 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.page = 0 self.page = 0
self.first_page = 1 self.first_page = 1
def open(self,filename): def open(self, filename):
t = time.localtime(time.time()) t = time.localtime(time.time())
self.time = "%04d-%02d-%02dT%02d:%02d:%02d" % \ self.time = "%04d-%02d-%02dT%02d:%02d:%02d" % \
(t[0],t[1],t[2],t[3],t[4],t[5]) (t[0], t[1], t[2], t[3], t[4], t[5])
if filename[-4:] != ".odt": if filename[-4:] != ".odt":
self.filename = filename + ".odt" self.filename = filename + ".odt"
@ -109,7 +109,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
current_locale = locale.getlocale() current_locale = locale.getlocale()
self.lang = current_locale[0] self.lang = current_locale[0]
if self.lang: if self.lang:
self.lang = self.lang.replace('_','-') self.lang = self.lang.replace('_', '-')
else: else:
self.lang = "en-US" self.lang = "en-US"
@ -318,7 +318,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.cntnt.write('<style:table-properties-properties style:width="%scm" '%table_width_str) self.cntnt.write('<style:table-properties-properties style:width="%scm" '%table_width_str)
self.cntnt.write('/>\n') self.cntnt.write('/>\n')
self.cntnt.write('</style:style>\n') self.cntnt.write('</style:style>\n')
for col in range(0,style.get_columns()): for col in range(0, style.get_columns()):
self.cntnt.write('<style:style style:name="') self.cntnt.write('<style:style style:name="')
self.cntnt.write(style_name + '.' + str(chr(ord('A')+col)) +'" ') self.cntnt.write(style_name + '.' + str(chr(ord('A')+col)) +'" ')
self.cntnt.write('style:family="table-column">') self.cntnt.write('style:family="table-column">')
@ -433,16 +433,17 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self._write_zip() self._write_zip()
if self.print_req: if self.print_req:
app = Mime.get_application(_apptype) app = Mime.get_application(_apptype)
Utils.launch(app[0],self.filename) Utils.launch(app[0], self.filename)
def add_media_object(self,name,pos,x_cm,y_cm): def add_media_object(self, name, pos, x_cm, y_cm):
# try to open the image. If the open fails, it probably wasn't # try to open the image. If the open fails, it probably wasn't
# a valid image (could be a PDF, or a non-image) # a valid image (could be a PDF, or a non-image)
(x,y) = ImgManip.image_size(name) (x, y) = ImgManip.image_size(name)
if (x,y) == (0,0): if (x, y) == (0, 0):
return return
ratio = float(x_cm)*float(y)/(float(y_cm)*float(x))
if ratio < 1: if ratio < 1:
act_width = x_cm act_width = x_cm
act_height = y_cm*ratio act_height = y_cm*ratio
@ -450,12 +451,12 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
act_height = y_cm act_height = y_cm
act_width = x_cm/ratio act_width = x_cm/ratio
media_list_item = (name,act_width,act_height) media_list_item = (name, act_width, act_height)
if not media_list_item in self.media_list: if not media_list_item in self.media_list:
self.media_list.append(media_list_item) self.media_list.append(media_list_item)
base = os.path.basename(name) base = os.path.basename(name)
tag = base.replace('.','_') tag = base.replace('.', '_')
if self.new_cell: if self.new_cell:
self.cntnt.write('<text:p>') self.cntnt.write('<text:p>')
@ -481,12 +482,12 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
if self.new_cell: if self.new_cell:
self.cntnt.write('</text:p>\n') self.cntnt.write('</text:p>\n')
def start_table(self,name,style_name): def start_table(self, name, style_name):
self.cntnt.write('<table:table table:name="%s" ' % name) self.cntnt.write('<table:table table:name="%s" ' % name)
self.cntnt.write('table:style-name="%s">\n' % style_name) self.cntnt.write('table:style-name="%s">\n' % style_name)
styles = self.get_style_sheet() styles = self.get_style_sheet()
table = styles.get_table_style(style_name) table = styles.get_table_style(style_name)
for col in range(0,table.get_columns()): for col in range(0, table.get_columns()):
self.cntnt.write('<table:table-column table:style-name="') self.cntnt.write('<table:table-column table:style-name="')
self.cntnt.write(style_name + '.' + str(chr(ord('A')+col)) +'"/>\n') self.cntnt.write(style_name + '.' + str(chr(ord('A')+col)) +'"/>\n')
@ -499,7 +500,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def end_row(self): def end_row(self):
self.cntnt.write('</table:table-row>\n') self.cntnt.write('</table:table-row>\n')
def start_cell(self,style_name,span=1): def start_cell(self, style_name, span=1):
self.span = span self.span = span
self.cntnt.write('<table:table-cell table:style-name="%s" ' % style_name) self.cntnt.write('<table:table-cell table:style-name="%s" ' % style_name)
self.cntnt.write('table:value-type="string"') self.cntnt.write('table:value-type="string"')
@ -511,7 +512,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def end_cell(self): def end_cell(self):
self.cntnt.write('</table:table-cell>\n') self.cntnt.write('</table:table-cell>\n')
#for col in range(1,self.span): #for col in range(1, self.span):
# self.cntnt.write('<table:covered-table-cell/>\n') # self.cntnt.write('<table:covered-table-cell/>\n')
self.new_cell = 0 self.new_cell = 0
@ -527,16 +528,16 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def end_superscript(self): def end_superscript(self):
self.cntnt.write('</text:span>') self.cntnt.write('</text:span>')
def _add_zip(self,zfile,name,data,t): def _add_zip(self, zfile, name, data, t):
zipinfo = zipfile.ZipInfo(name.encode('latin-1')) zipinfo = zipfile.ZipInfo(name.encode('latin-1'))
zipinfo.date_time = t zipinfo.date_time = t
zipinfo.compress_type = zipfile.ZIP_DEFLATED zipinfo.compress_type = zipfile.ZIP_DEFLATED
zfile.writestr(zipinfo,data) zfile.writestr(zipinfo, data)
def _write_zip(self): def _write_zip(self):
try: try:
zfile = zipfile.ZipFile(self.filename,"w",zipfile.ZIP_DEFLATED) zfile = zipfile.ZipFile(self.filename, "w", zipfile.ZIP_DEFLATED)
except IOError,msg: except IOError, msg:
errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg) errmsg = "%s\n%s" % (_("Could not create %s") % self.filename, msg)
raise Errors.ReportError(errmsg) raise Errors.ReportError(errmsg)
except: except:
@ -544,11 +545,11 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
t = time.localtime(time.time())[:6] t = time.localtime(time.time())[:6]
self._add_zip(zfile,"META-INF/manifest.xml",self.mfile.getvalue(),t) self._add_zip(zfile, "META-INF/manifest.xml", self.mfile.getvalue(), t)
self._add_zip(zfile,"content.xml",self.cntnt.getvalue(),t) self._add_zip(zfile, "content.xml", self.cntnt.getvalue(), t)
self._add_zip(zfile,"meta.xml",self.meta.getvalue(),t) self._add_zip(zfile, "meta.xml", self.meta.getvalue(), t)
self._add_zip(zfile,"styles.xml",self.sfile.getvalue(),t) self._add_zip(zfile, "styles.xml", self.sfile.getvalue(), t)
self._add_zip(zfile,"mimetype",self.mimetype.getvalue(),t) self._add_zip(zfile, "mimetype", self.mimetype.getvalue(), t)
self.mfile.close() self.mfile.close()
self.cntnt.close() self.cntnt.close()
@ -558,9 +559,9 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
for image in self.media_list: for image in self.media_list:
try: try:
ifile = open(image[0],mode='rb') ifile = open(image[0], mode='rb')
base = os.path.basename(image[0]) base = os.path.basename(image[0])
self._add_zip(zfile,"Pictures/%s" % base, ifile.read(),t) self._add_zip(zfile, "Pictures/%s" % base, ifile.read(), t)
ifile.close() ifile.close()
except: except:
print "Could not open %s" % image[0] print "Could not open %s" % image[0]
@ -829,7 +830,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
def end_page(self): def end_page(self):
self.cntnt.write('</text:p>\n') self.cntnt.write('</text:p>\n')
def start_paragraph(self,style_name,leader=None): def start_paragraph(self, style_name, leader=None):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
style = style_sheet.get_paragraph_style(style_name) style = style_sheet.get_paragraph_style(style_name)
self.level = style.get_header_level() self.level = style.get_header_level()
@ -856,11 +857,11 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.cntnt.write('</text:h>\n') self.cntnt.write('</text:h>\n')
self.new_cell = 1 self.new_cell = 1
def write_note(self,text,format,style_name): def write_note(self, text, format, style_name):
if format == 1: if format == 1:
text = escape(text,_esc_map) text = escape(text, _esc_map)
# Replace multiple spaces: have to go from the largest number down # Replace multiple spaces: have to go from the largest number down
for n in range(text.count(' '),1,-1): for n in range(text.count(' '), 1, -1):
text = text.replace(' '*n, ' <text:s text:c="%d"/>' % (n-1) ) text = text.replace(' '*n, ' <text:s text:c="%d"/>' % (n-1) )
self.start_paragraph(style_name) self.start_paragraph(style_name)
self.cntnt.write('<text:span text:style-name="GRAMPS-preformat">') self.cntnt.write('<text:span text:style-name="GRAMPS-preformat">')
@ -870,20 +871,20 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
elif format == 0: elif format == 0:
for line in text.split('\n\n'): for line in text.split('\n\n'):
self.start_paragraph(style_name) self.start_paragraph(style_name)
line = line.replace('\n',' ') line = line.replace('\n', ' ')
line = ' '.join(line.split()) line = ' '.join(line.split())
self.write_text(line) self.write_text(line)
self.end_paragraph() self.end_paragraph()
def write_text(self,text,mark=None): def write_text(self, text, mark=None):
""" """
Uses the xml.sax.saxutils.escape function to convert XML Uses the xml.sax.saxutils.escape function to convert XML
entities. The _esc_map dictionary allows us to add our own entities. The _esc_map dictionary allows us to add our own
mappings. mappings.
""" """
if mark: if mark:
key = escape(mark.key,_esc_map) key = escape(mark.key, _esc_map)
key = key.replace('"','&quot;') key = key.replace('"', '&quot;')
if mark.type == BaseDoc.INDEX_TYPE_ALP: if mark.type == BaseDoc.INDEX_TYPE_ALP:
self.cntnt.write('<text:alphabetical-index-mark ') self.cntnt.write('<text:alphabetical-index-mark ')
self.cntnt.write('text:string-value="%s" />' % key) self.cntnt.write('text:string-value="%s" />' % key)
@ -891,7 +892,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.cntnt.write('<text:toc-mark ') self.cntnt.write('<text:toc-mark ')
self.cntnt.write('text:string-value="%s" ' % key) self.cntnt.write('text:string-value="%s" ' % key)
self.cntnt.write('text:outline-level="%d" />' % mark.level) self.cntnt.write('text:outline-level="%d" />' % mark.level)
self.cntnt.write(escape(text,_esc_map)) self.cntnt.write(escape(text, _esc_map))
def _write_manifest(self): def _write_manifest(self):
self.mfile = StringIO() self.mfile = StringIO()
@ -973,7 +974,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.meta.write('</office:meta>\n') self.meta.write('</office:meta>\n')
self.meta.write('</office:document-meta>\n') self.meta.write('</office:document-meta>\n')
def rotate_text(self,style,text,x,y,angle): def rotate_text(self, style, text, x, y, angle):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
stype = style_sheet.get_draw_style(style) stype = style_sheet.get_draw_style(style)
pname = stype.get_paragraph_style() pname = stype.get_paragraph_style()
@ -984,7 +985,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
height = size*(len(text)) height = size*(len(text))
width = 0 width = 0
for line in text: for line in text:
width = max(width,FontScale.string_width(font,line)) width = max(width, FontScale.string_width(font, line))
wcm = ReportUtils.pt2cm(width) wcm = ReportUtils.pt2cm(width)
hcm = ReportUtils.pt2cm(height) hcm = ReportUtils.pt2cm(height)
@ -999,48 +1000,48 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.cntnt.write('rotate (%.8f) ' % -rangle) self.cntnt.write('rotate (%.8f) ' % -rangle)
xloc = x-((wcm/2.0)*cos(rangle))+((hcm/2.0)*sin(rangle)) xloc = x-((wcm/2.0)*cos(rangle))+((hcm/2.0)*sin(rangle))
yloc = y-((hcm/2.0)*cos(rangle))-((wcm/2.0)*sin(rangle)) yloc = y-((hcm/2.0)*cos(rangle))-((wcm/2.0)*sin(rangle))
self.cntnt.write('translate (%.3fcm %.3fcm)">\n' % (xloc,yloc)) self.cntnt.write('translate (%.3fcm %.3fcm)">\n' % (xloc, yloc))
self.cntnt.write('<draw:text-box>\n') self.cntnt.write('<draw:text-box>\n')
self.cntnt.write('<text:p text:style-name="X%s">' % pname) self.cntnt.write('<text:p text:style-name="X%s">' % pname)
self.cntnt.write('<text:span text:style-name="F%s">' % pname) self.cntnt.write('<text:span text:style-name="F%s">' % pname)
self.cntnt.write(escape('\n'.join(text),_esc_map)) self.cntnt.write(escape('\n'.join(text), _esc_map))
self.cntnt.write('</text:span></text:p>\n</draw:text-box>\n') self.cntnt.write('</text:span></text:p>\n</draw:text-box>\n')
self.cntnt.write('</draw:frame>\n') self.cntnt.write('</draw:frame>\n')
def draw_path(self,style,path): def draw_path(self, style, path):
minx = 9e12 minx = 9e12
miny = 9e12 miny = 9e12
maxx = 0 maxx = 0
maxy = 0 maxy = 0
for point in path: for point in path:
minx = min(point[0],minx) minx = min(point[0], minx)
miny = min(point[1],miny) miny = min(point[1], miny)
maxx = max(point[0],maxx) maxx = max(point[0], maxx)
maxy = max(point[1],maxy) maxy = max(point[1], maxy)
self.cntnt.write('<draw:polygon draw:style-name="%s" draw:layer="layout" ' % style) self.cntnt.write('<draw:polygon draw:style-name="%s" draw:layer="layout" ' % style)
self.cntnt.write('draw:z-index="1" ') self.cntnt.write('draw:z-index="1" ')
x = int((minx)*1000) x = int((minx)*1000)
y = int((miny)*1000) y = int((miny)*1000)
self.cntnt.write('svg:x="%d" svg:y="%d" ' % (x,y)) self.cntnt.write('svg:x="%d" svg:y="%d" ' % (x, y))
self.cntnt.write('svg:viewBox="0 0 %d %d" ' % (int((maxx-minx)*1000),int((maxy-miny)*1000))) self.cntnt.write('svg:viewBox="0 0 %d %d" ' % (int((maxx-minx)*1000), int((maxy-miny)*1000)))
self.cntnt.write('svg:width="%.4fcm" ' % (maxx-minx)) self.cntnt.write('svg:width="%.4fcm" ' % (maxx-minx))
self.cntnt.write('svg:height="%.4fcm" ' % (maxy-miny)) self.cntnt.write('svg:height="%.4fcm" ' % (maxy-miny))
point = path[0] point = path[0]
x1 = int((point[0]-minx)*1000) x1 = int((point[0]-minx)*1000)
y1 = int((point[1]-miny)*1000) y1 = int((point[1]-miny)*1000)
self.cntnt.write('draw:points="%d,%d' % (x1,y1)) self.cntnt.write('draw:points="%d, %d' % (x1, y1))
for point in path[1:]: for point in path[1:]:
x1 = int((point[0]-minx)*1000) x1 = int((point[0]-minx)*1000)
y1 = int((point[1]-miny)*1000) y1 = int((point[1]-miny)*1000)
self.cntnt.write(' %d,%d' % (x1,y1)) self.cntnt.write(' %d, %d' % (x1, y1))
self.cntnt.write('"/>\n') self.cntnt.write('"/>\n')
def draw_line(self,style,x1,y1,x2,y2): def draw_line(self, style, x1, y1, x2, y2):
self.cntnt.write('<draw:line text:anchor-type="paragraph" ') self.cntnt.write('<draw:line text:anchor-type="paragraph" ')
self.cntnt.write('draw:z-index="3" ') self.cntnt.write('draw:z-index="3" ')
self.cntnt.write('draw:text-style-name="%s" ' % style ) self.cntnt.write('draw:text-style-name="%s" ' % style )
@ -1051,13 +1052,13 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.cntnt.write('<text:p/>\n') self.cntnt.write('<text:p/>\n')
self.cntnt.write('</draw:line>\n') self.cntnt.write('</draw:line>\n')
def draw_text(self,style,text,x,y): def draw_text(self, style, text, x, y):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style) box_style = style_sheet.get_draw_style(style)
para_name = box_style.get_paragraph_style() para_name = box_style.get_paragraph_style()
pstyle = style_sheet.get_paragraph_style(para_name) pstyle = style_sheet.get_paragraph_style(para_name)
font = pstyle.get_font() font = pstyle.get_font()
sw = ReportUtils.pt2cm(FontScale.string_width(font,text))*1.3 sw = ReportUtils.pt2cm(FontScale.string_width(font, text))*1.3
self.cntnt.write('<draw:frame text:anchor-type="paragraph" ') self.cntnt.write('<draw:frame text:anchor-type="paragraph" ')
self.cntnt.write('draw:z-index="2" ') self.cntnt.write('draw:z-index="2" ')
@ -1072,12 +1073,12 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.cntnt.write('<text:p text:style-name="F%s">' % para_name) self.cntnt.write('<text:p text:style-name="F%s">' % para_name)
self.cntnt.write('<text:span text:style-name="F%s"' % para_name) self.cntnt.write('<text:span text:style-name="F%s"' % para_name)
self.cntnt.write(' fo:max-height="%.2f">' % font.get_size() ) self.cntnt.write(' fo:max-height="%.2f">' % font.get_size() )
self.cntnt.write(escape(text,_esc_map)) self.cntnt.write(escape(text, _esc_map))
self.cntnt.write('</text:span></text:p>') self.cntnt.write('</text:span></text:p>')
self.cntnt.write('</draw:text-box>\n') self.cntnt.write('</draw:text-box>\n')
self.cntnt.write('</draw:frame>\n') self.cntnt.write('</draw:frame>\n')
def draw_box(self,style,text,x,y, w, h): def draw_box(self, style, text, x, y, w, h):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style) box_style = style_sheet.get_draw_style(style)
para_name = box_style.get_paragraph_style() para_name = box_style.get_paragraph_style()
@ -1105,19 +1106,19 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
if text != "": if text != "":
self.cntnt.write('<text:p text:style-name="%s">' % para_name) self.cntnt.write('<text:p text:style-name="%s">' % para_name)
self.cntnt.write('<text:span text:style-name="F%s">' % para_name) self.cntnt.write('<text:span text:style-name="F%s">' % para_name)
self.cntnt.write(escape(text,_esc_map)) self.cntnt.write(escape(text, _esc_map))
self.cntnt.write('</text:span>') self.cntnt.write('</text:span>')
self.cntnt.write('</text:p>\n') self.cntnt.write('</text:p>\n')
self.cntnt.write('</draw:rect>\n') self.cntnt.write('</draw:rect>\n')
def center_text(self,style,text,x,y): def center_text(self, style, text, x, y):
style_sheet = self.get_style_sheet() style_sheet = self.get_style_sheet()
box_style = style_sheet.get_draw_style(style) box_style = style_sheet.get_draw_style(style)
para_name = box_style.get_paragraph_style() para_name = box_style.get_paragraph_style()
pstyle = style_sheet.get_paragraph_style(para_name) pstyle = style_sheet.get_paragraph_style(para_name)
font = pstyle.get_font() font = pstyle.get_font()
size = (FontScale.string_width(font,text)/72.0) * 2.54 size = (FontScale.string_width(font, text)/72.0) * 2.54
self.cntnt.write('<draw:frame text:anchor-type="paragraph" ') self.cntnt.write('<draw:frame text:anchor-type="paragraph" ')
self.cntnt.write('draw:style-name="%s" ' % style) self.cntnt.write('draw:style-name="%s" ' % style)
@ -1132,7 +1133,7 @@ class ODFDoc(BaseDoc.BaseDoc,BaseDoc.TextDoc,BaseDoc.DrawDoc):
self.cntnt.write('<draw:text-box>') self.cntnt.write('<draw:text-box>')
self.cntnt.write('<text:p text:style-name="X%s">' % para_name) self.cntnt.write('<text:p text:style-name="X%s">' % para_name)
self.cntnt.write('<text:span text:style-name="F%s">' % para_name) self.cntnt.write('<text:span text:style-name="F%s">' % para_name)
self.cntnt.write(escape(text,_esc_map)) self.cntnt.write(escape(text, _esc_map))
self.cntnt.write('</text:span>\n') self.cntnt.write('</text:span>\n')
self.cntnt.write('</text:p>\n') self.cntnt.write('</text:p>\n')
self.cntnt.write('</draw:text-box>') self.cntnt.write('</draw:text-box>')

View File

@ -8,7 +8,7 @@
# the Free Software Foundation; either version 2 of the License, or # the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. # (at your option) any later version.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
@ -79,9 +79,9 @@ _title_string = _("Export to CD")
# writeData # writeData
# #
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
def writeData(database,filename,person,option_box=None,callback=None): def writeData(database, filename, person, option_box=None, callback=None):
ret = 0 ret = 0
writer = PackageWriter(database,filename,callback) writer = PackageWriter(database, filename, callback)
ret = writer.export() ret = writer.export()
return ret return ret
@ -92,7 +92,7 @@ def writeData(database,filename,person,option_box=None,callback=None):
#------------------------------------------------------------------------- #-------------------------------------------------------------------------
class PackageWriter: class PackageWriter:
def __init__(self,database,filename="",cl=0,callback=None): def __init__(self, database, filename="", cl=0, callback=None):
self.db = database self.db = database
self.cl = cl self.cl = cl
self.filename = filename self.filename = filename
@ -109,41 +109,30 @@ class PackageWriter:
try: try:
uri = URI('burn:///%s' % base) uri = URI('burn:///%s' % base)
make_directory(uri,OPEN_WRITE) make_directory(uri, OPEN_WRITE)
except FileExistsError, msg: except FileExistsError, msg:
QuestionDialog.ErrorDialog(_("CD export preparation failed"), QuestionDialog.ErrorDialog(_("CD export preparation failed"),
"1 %s " % str(msg)) "1 %s " % str(msg))
return return
except: except:
uri_name = "burn:///" + base uri_name = "burn:///" + base
QuestionDialog.ErrorDialog("CD export preparation failed", QuestionDialog.ErrorDialog("CD export preparation failed",
'Could not create %s' % uri_name) 'Could not create %s' % uri_name)
return return
try:
uri = URI('burn:///%s/.thumb' % base)
make_directory(uri,OPEN_WRITE)
except FileExistsError, msg:
QuestionDialog.ErrorDialog("CD export preparation failed",
"2 %s " % str(msg))
return
for obj_id in self.db.get_media_object_handles(): for obj_id in self.db.get_media_object_handles():
obj = self.db.get_object_from_handle(obj_id) obj = self.db.get_object_from_handle(obj_id)
oldfile = obj.get_path() oldfile = obj.get_path()
root = os.path.basename(oldfile) root = os.path.basename(oldfile)
if os.path.isfile(oldfile): if os.path.isfile(oldfile):
self.copy_file(oldfile,'burn:///%s/%s' % (base,root)) self.copy_file(oldfile, 'burn:///%s/%s' % (base, root))
mime_type = obj.get_mime_type()
if mime_type and mime_type.startswith("image"):
self.make_thumbnail(base,root,obj.get_path())
else: else:
print "Warning: media file %s was not found," % root,\ print "Warning: media file %s was not found, " % root, \
"so it was ignored." "so it was ignored."
# Write XML now # Write XML now
g = create('burn:///%s/data.gramps' % base,OPEN_WRITE ) g = create('burn:///%s/data.gramps' % base, OPEN_WRITE )
gfile = XmlWriter(self.db,None,2) gfile = XmlWriter(self.db, None, 2)
gfile.write_handle(g) gfile.write_handle(g)
g.close() g.close()
@ -154,27 +143,27 @@ class PackageWriter:
try: try:
uri = URI('burn:///%s' % base) uri = URI('burn:///%s' % base)
make_directory(uri,OPEN_WRITE) make_directory(uri, OPEN_WRITE)
except FileExistsError: except FileExistsError:
QuestionDialog.ErrorDialog(_("CD export preparation failed"), QuestionDialog.ErrorDialog(_("CD export preparation failed"),
"File already exists") "File already exists")
return return
except: except:
uri_name = "burn:///" + base uri_name = "burn:///" + base
QuestionDialog.ErrorDialog(_("CD export preparation failed"), QuestionDialog.ErrorDialog(_("CD export preparation failed"),
_('Could not create %s') % uri_name) _('Could not create %s') % uri_name)
return return
try: try:
uri = URI('burn:///%s/.thumb' % base) uri = URI('burn:///%s/.thumb' % base)
make_directory(uri,OPEN_WRITE) make_directory(uri, OPEN_WRITE)
except FileExistsError, msg: except FileExistsError, msg:
QuestionDialog.ErrorDialog("CD export preparation failed", QuestionDialog.ErrorDialog("CD export preparation failed",
"4 %s " % str(msg)) "4 %s " % str(msg))
return return
except: except:
uri_name = "burn:///" + base + "/.thumb" uri_name = "burn:///" + base + "/.thumb"
QuestionDialog.ErrorDialog(_("CD export preparation failed"), QuestionDialog.ErrorDialog(_("CD export preparation failed"),
_('Could not create %s') % uri_name) _('Could not create %s') % uri_name)
return return
@ -188,7 +177,7 @@ class PackageWriter:
if o.get_reference_handle() == self.object_handle: if o.get_reference_handle() == self.object_handle:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_family(p,None) self.db.commit_family(p, None)
for key in self.db.get_person_handles(sort_handles=False): for key in self.db.get_person_handles(sort_handles=False):
p = self.db.get_person_from_handle(key) p = self.db.get_person_from_handle(key)
@ -197,7 +186,7 @@ class PackageWriter:
if o.get_reference_handle() == self.object_handle: if o.get_reference_handle() == self.object_handle:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_person(p,None) self.db.commit_person(p, None)
for key in self.db.get_source_handles(): for key in self.db.get_source_handles():
p = self.db.get_source_from_handle(key) p = self.db.get_source_from_handle(key)
nl = p.get_media_list() nl = p.get_media_list()
@ -205,7 +194,7 @@ class PackageWriter:
if o.get_reference_handle() == self.object_handle: if o.get_reference_handle() == self.object_handle:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_source(p,None) self.db.commit_source(p, None)
for key in self.db.get_place_handles(): for key in self.db.get_place_handles():
p = self.db.get_place_from_handle(key) p = self.db.get_place_from_handle(key)
nl = p.get_media_list() nl = p.get_media_list()
@ -213,7 +202,7 @@ class PackageWriter:
if o.get_reference_handle() == self.object_handle: if o.get_reference_handle() == self.object_handle:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_place(p,None) self.db.commit_place(p, None)
for key in self.db.get_event_handles(): for key in self.db.get_event_handles():
p = self.db.get_event_from_handle(key) p = self.db.get_event_from_handle(key)
nl = p.get_media_list() nl = p.get_media_list()
@ -221,8 +210,8 @@ class PackageWriter:
if o.get_reference_handle() == self.object_handle: if o.get_reference_handle() == self.object_handle:
nl.remove(o) nl.remove(o)
p.set_media_list(nl) p.set_media_list(nl)
self.db.commit_event(p,None) self.db.commit_event(p, None)
self.db.remove_object(self.object_handle,None) self.db.remove_object(self.object_handle, None)
def leave_clicked(): def leave_clicked():
# File is lost => do nothing, leave as is # File is lost => do nothing, leave as is
@ -234,18 +223,15 @@ class PackageWriter:
pass pass
def fs_ok_clicked(obj): def fs_ok_clicked(obj):
newfile = unicode(fs_top.get_filename(), newfile = unicode(fs_top.get_filename(),
sys.getfilesystemencoding()) sys.getfilesystemencoding())
if os.path.isfile(newfile): if os.path.isfile(newfile):
self.copy_file(newfile,'burn:///%s/%s' % (base,obase)) self.copy_file(newfile, 'burn:///%s/%s' % (base, obase))
ntype = Mime.get_type(newfile)
if ntype and ntype.startswith("image"):
self.make_thumbnail(base,obase,newfile)
fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file")) fs_top = gtk.FileSelection("%s - GRAMPS" % _("Select file"))
fs_top.hide_fileop_buttons() fs_top.hide_fileop_buttons()
fs_top.ok_button.connect('clicked',fs_ok_clicked) fs_top.ok_button.connect('clicked', fs_ok_clicked)
fs_top.cancel_button.connect('clicked',fs_close_window) fs_top.cancel_button.connect('clicked', fs_close_window)
fs_top.run() fs_top.run()
fs_top.destroy() fs_top.destroy()
@ -259,20 +245,17 @@ class PackageWriter:
oldfile = obj.get_path() oldfile = obj.get_path()
root = os.path.basename(oldfile) root = os.path.basename(oldfile)
if os.path.isfile(oldfile): if os.path.isfile(oldfile):
self.copy_file(oldfile,'burn:///%s/%s' % (base,root)) self.copy_file(oldfile, 'burn:///%s/%s' % (base, root))
mime_type = obj.get_mime_type()
if mime_type and mime_type.startswith("image"):
self.make_thumbnail(base,root,obj.get_path())
else: else:
# File is lost => ask what to do # File is lost => ask what to do
self.object_handle = obj.get_handle() self.object_handle = obj.get_handle()
if missmedia_action == 0: if missmedia_action == 0:
mmd = QuestionDialog.MissingMediaDialog(_("Media object could not be found"), mmd = QuestionDialog.MissingMediaDialog(_("Media object could not be found"),
_("%(file_name)s is referenced in the database, but no longer exists. " _("%(file_name)s is referenced in the database, but no longer exists. "
"The file may have been deleted or moved to a different location. " "The file may have been deleted or moved to a different location. "
"You may choose to either remove the reference from the database, " "You may choose to either remove the reference from the database, "
"keep the reference to the missing file, or select a new file." "keep the reference to the missing file, or select a new file."
) % { 'file_name' : oldfile }, ) % { 'file_name' : oldfile },
remove_clicked, leave_clicked, select_clicked) remove_clicked, leave_clicked, select_clicked)
missmedia_action = mmd.default_action missmedia_action = mmd.default_action
elif missmedia_action == 1: elif missmedia_action == 1:
@ -285,17 +268,17 @@ class PackageWriter:
# Write XML now # Write XML now
uri = 'burn:///%s/data.gramps' % base uri = 'burn:///%s/data.gramps' % base
uri = uri.encode('utf8') uri = uri.encode('utf8')
g = create(uri,OPEN_WRITE) g = create(uri, OPEN_WRITE)
gfile = XmlWriter(self.db,self.callback,2) gfile = XmlWriter(self.db, self.callback, 2)
gfile.write_handle(g) gfile.write_handle(g)
g.close() g.close()
os.system("nautilus --no-desktop burn:///") os.system("nautilus --no-desktop burn:///")
return 1 return 1
def copy_file(self,src,dest): def copy_file(self, src, dest):
original = open(src,"r") original = open(src, "r")
destobj = URI(dest) destobj = URI(dest)
target = create(destobj,OPEN_WRITE) target = create(destobj, OPEN_WRITE)
done = 0 done = 0
while 1: while 1:
buf = original.read(2048) buf = original.read(2048)
@ -319,4 +302,4 @@ _description = _('Exporting to CD copies all your data and media '
_config = None _config = None
_filename = 'burn' _filename = 'burn'
register_export(writeData,_title,_description,_config,_filename) register_export(writeData, _title, _description, _config, _filename)