00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 from PyQt4.QtCore import *
00030 from PyQt4.QtGui import *
00031 from PyQt4.uic import *
00032 
00033 from Koo import Rpc
00034 from Koo.Common import Common
00035 from Koo.Model.Group import RecordGroup
00036 
00037 import copy
00038 
00039 (TranslationDialogUi, TranslationDialogBase) = loadUiType( Common.uiPath('translationdialog.ui') ) 
00040 
00041 
00042 
00043 class TranslationDialog( QDialog, TranslationDialogUi ):
00044         LineEdit = 0
00045         TextEdit = 1
00046         RichEdit = 2
00047 
00048         
00049         def __init__(self, id, model, fieldName, value, type, parent = None):
00050                 QDialog.__init__(self, parent)
00051                 TranslationDialogUi.__init__(self)
00052                 self.setupUi( self )
00053 
00054                 self.connect( self.pushAccept, SIGNAL('clicked()'), self.slotAccept )
00055                 self.id = id
00056                 self.model = model
00057                 self.fieldName = fieldName
00058                 self.value = value
00059                 self.type = type
00060 
00061                 self.values = {}
00062                 self.result = value
00063                 
00064                 QTimer.singleShot( 0, self.init )
00065 
00066         def adaptContext(self, value):
00067                 if value == 'en_US':
00068                         return False
00069                 else:
00070                         return value
00071 
00072         def init(self):
00073                 self.currentCode = Rpc.session.context.get('lang', 'en_US')
00074 
00075                 languageIds = Rpc.session.execute( '/object', 'execute', 'res.lang', 'search', [('translatable','=','1')])
00076                 languages = Rpc.session.execute( '/object', 'execute', 'res.lang', 'read', languageIds, ['code', 'name'] )
00077 
00078                 arch = []
00079                 fields = {}
00080                 for lang in languages:
00081                         if self.type == TranslationDialog.LineEdit:
00082                                 widget = 'char'
00083                                 fieldType = 'char'
00084                         elif self.type == TranslationDialog.TextEdit:
00085                                 widget = 'text'
00086                                 fieldType = 'text'
00087                         else:
00088                                 widget = 'text_tag'
00089                                 fieldType = 'text'
00090 
00091                         arch.append( '<field name="%s" widget="%s"/>' % (lang['code'], widget) )
00092                         fields[lang['code']] = {
00093                                 'string': lang['name'],
00094                                 'type': fieldType,
00095                         }
00096                         if lang['code'] == self.currentCode:
00097                                 self.values[ lang['code'] ] = self.value
00098                                 continue
00099 
00100                         context = copy.copy(Rpc.session.context)                        
00101                         context['lang'] = self.adaptContext( lang['code'] )
00102                         val = Rpc.session.execute( '/object', 'execute', self.model, 'read', [self.id], [self.fieldName], context)
00103                         val = val[0]
00104 
00105                         self.values[ lang['code'] ] = val[self.fieldName]
00106 
00107                 arch = '<form string="%s" col="2">%s</form>' % (_('Translation Dialog'), ''.join( arch ) )
00108 
00109                 self.group = RecordGroup( 'translator' )
00110                 
00111                 
00112                 self.group.setDomainForEmptyGroup()
00113                 self.uiScreen.setRecordGroup( self.group )
00114                 self.uiScreen.new(default=False)
00115                 self.uiScreen.addView(arch, fields, display=True)
00116                 self.uiScreen.currentRecord().set(self.values)
00117                 self.uiScreen.display()
00118 
00119         def slotAccept(self):
00120                 self.uiScreen.currentView().store()
00121                 for lang, oldValue in self.values.iteritems():
00122                         newValue = self.uiScreen.currentRecord().value( lang )
00123                         
00124                         
00125                         
00126                         if lang == self.currentCode:
00127                                 self.result = newValue
00128                                 continue
00129                         
00130                         if newValue == oldValue:
00131                                 continue
00132                         context = copy.copy(Rpc.session.context)
00133                         context['lang'] = self.adaptContext( lang )
00134                         Rpc.session.execute( '/object', 'execute', self.model, 'write', [self.id], {self.fieldName: newValue}, context )
00135                 self.accept()
00136