Added selection field into company, set font dynamically to all para style in header

bzr revid: chiragdd7@gmail.com-20130611121310-35ogs0ler806hkov
This commit is contained in:
Chirag Dodiya (OpenERP Trainee) 2013-06-11 17:43:10 +05:30
parent a94bbf8e5e
commit 489196ee76
4 changed files with 59 additions and 3 deletions

View File

@ -20,7 +20,7 @@
##############################################################################
import os
import re
import openerp
from openerp import SUPERUSER_ID, tools
from openerp.osv import fields, osv
@ -28,6 +28,25 @@ from openerp.tools.translate import _
from openerp.tools.safe_eval import safe_eval as eval
from openerp.tools import image_resize_image
_select_font=[ ('DejaVu Sans',"DejaVu Sans"),
('DejaVu Sans Bold',"DejaVu Sans Bold"),
('DejaVu Sans Oblique',"DejaVu Sans Oblique"),
('DejaVu Sans BoldOblique',"DejaVu Sans BoldOblique"),
('Liberation Serif',"Liberation Serif"),
('Liberation Serif Bold',"Liberation Serif Bold"),
('Liberation Serif Italic',"Liberation Serif Italic"),
('Liberation Serif BoldItalic',"Liberation Serif BoldItalic"),
('Liberation Serif',"Liberation Serif"),
('Liberation Serif Bold',"Liberation Serif Bold"),
('Liberation Serif Italic',"Liberation Serif Italic"),
('Liberation Serif BoldItalic',"Liberation Serif BoldItalic"),
('FreeMono',"FreeMono"),
('FreeMono Bold',"FreeMono Bold"),
('FreeMono Oblique',"FreeMono Oblique"),
('FreeMono BoldOblique',"FreeMono BoldOblique"),
('Sun-ExtA',"Sun-ExtA")
]
class multi_company_default(osv.osv):
"""
Manage multi company default value
@ -108,7 +127,7 @@ class res_company(osv.osv):
size = (180, None)
result[record.id] = image_resize_image(record.partner_id.image, size)
return result
def _get_companies_from_partner(self, cr, uid, ids, context=None):
return self.pool['res.company'].search(cr, uid, [('partner_id', 'in', ids)], context=context)
@ -147,6 +166,7 @@ class res_company(osv.osv):
'vat': fields.related('partner_id', 'vat', string="Tax ID", type="char", size=32),
'company_registry': fields.char('Company Registry', size=64),
'paper_format': fields.selection([('a4', 'A4'), ('us_letter', 'US Letter')], "Paper Format", required=True),
'font': fields.selection(_select_font, "Select Font"),
}
_sql_constraints = [
('name_uniq', 'unique (name)', 'The company name must be unique !')
@ -178,6 +198,25 @@ class res_company(osv.osv):
if state_id:
return {'value':{'country_id': self.pool.get('res.country.state').browse(cr, uid, state_id, context).country_id.id }}
return {}
def onchange_font_name(self, cr, uid, ids, font, context=None):
"""
To change default header style of all <para> and drawstring.
"""
def _change_header(header,font):
"""
Replace default fontname use in header and setfont tag
"""
default_para = re.sub('fontName.?=.?".*"', 'fontName="%s"'% font,header)
return re.sub('(<setFont.?name.?=.?)(".*?")(.)', '\g<1>"%s"\g<3>'% font,default_para)
if not ids: return {}
data = self.browse(cr, uid, ids[0], context=context)
return {'value':{
'rml_header': _change_header(data.rml_header,font),
'rml_header2':_change_header(data.rml_header2,font),
'rml_header3':_change_header(data.rml_header3,font)
}}
def on_change_country(self, cr, uid, ids, country_id, context=None):
res = {'domain': {'state_id': []}}
currency_id = self._get_euro(cr, uid, context=context)

View File

@ -83,6 +83,9 @@
</group>
</page>
<page string="Header/Footer" groups="base.group_no_one">
<group>
<field name="font" on_change="onchange_font_name(font)"/>
</group>
<label for="rml_header"/>
<field name="rml_header"/>
<label for="rml_header2"/>

View File

@ -28,6 +28,7 @@ from reportlab import rl_config
from openerp.tools import config
#.apidoc title: TTF Font Table
"""This module allows the mapping of some system-available TTF fonts to
the reportlab engine.

View File

@ -160,7 +160,7 @@ class _rml_styles(object,):
for style in node.findall('paraStyle'):
sname = style.get('name')
self.styles[sname] = self._para_style_update(style)
if sname in self.default_style:
if self.default_style.has_key(sname):
for key, value in self.styles[sname].items():
setattr(self.default_style[sname], key, value)
else:
@ -277,11 +277,24 @@ class _rml_doc(object):
fname = font.get('fontFile').encode('ascii')
if name not in pdfmetrics._fonts:
pdfmetrics.registerFont(TTFont(name, fname))
#by default, we map the fontName to each style (bold, italic, bold and italic), so that
#if there isn't any font defined for one of these style (via a font family), the system
#will fallback on the normal font.
addMapping(name, 0, 0, name) #normal
addMapping(name, 0, 1, name) #italic
addMapping(name, 1, 0, name) #bold
addMapping(name, 1, 1, name) #italic and bold
#if registerFontFamily is defined, we register the mapping of the fontName to use for each style.
for font_family in node.findall('registerFontFamily'):
family_name = font_family.get('normal').encode('ascii')
if font_family.get('italic'):
addMapping(family_name, 0, 1, font_family.get('italic').encode('ascii'))
if font_family.get('bold'):
addMapping(family_name, 1, 0, font_family.get('bold').encode('ascii'))
if font_family.get('boldItalic'):
addMapping(family_name, 1, 1, font_family.get('boldItalic').encode('ascii'))
def setTTFontMapping(self,face, fontname, filename, mode='all'):
from reportlab.lib.fonts import addMapping
from reportlab.pdfbase import pdfmetrics