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
00030 from PyQt4.QtCore import *
00031 from PyQt4.QtGui import *
00032 from PyQt4.uic import *
00033 from Koo.Common import Common
00034 from Koo.Common import Shortcuts
00035 from Koo.Fields.TranslationDialog import *
00036 from Koo.Fields.AbstractFieldWidget import *
00037
00038 (RichTextFieldWidgetUi, RichTextFieldWidgetBase) = loadUiType( Common.uiPath('richtext.ui') )
00039
00040 class RichTextFieldWidget(AbstractFieldWidget, RichTextFieldWidgetUi):
00041 def __init__(self, parent, model, attrs={}):
00042 AbstractFieldWidget.__init__(self, parent, model, attrs)
00043 RichTextFieldWidgetUi.__init__(self)
00044 self.setupUi(self)
00045
00046 self.setSizePolicy( QSizePolicy.Preferred, QSizePolicy.Expanding )
00047 self.installPopupMenu( self.uiText )
00048
00049 self.connect( self.pushBold, SIGNAL('clicked()'), self.bold )
00050 self.connect( self.pushItalic, SIGNAL('clicked()'), self.italic )
00051 self.connect( self.pushUnderline, SIGNAL('clicked()'), self.underline )
00052 self.connect( self.pushStrike, SIGNAL('clicked()'), self.strike )
00053 self.connect( self.pushLeftJustify, SIGNAL('clicked()'), self.leftJustify )
00054 self.connect( self.pushCenter, SIGNAL('clicked()'), self.center )
00055 self.connect( self.pushRightJustify, SIGNAL('clicked()'), self.rightJustify )
00056 self.connect( self.pushJustify, SIGNAL('clicked()'), self.justify )
00057 self.connect( self.pushForegroundColor, SIGNAL('clicked()'), self.foregroundColor )
00058 self.connect( self.pushBackgroundColor, SIGNAL('clicked()'), self.backgroundColor )
00059 self.connect( self.uiFont, SIGNAL('currentFontChanged(QFont)'), self.font )
00060 self.connect( self.uiFontSize, SIGNAL('valueChanged(int)'), self.fontSize )
00061 self.connect( self.uiText, SIGNAL('cursorPositionChanged()'), self.cursorPosition)
00062
00063 self.updateFont = True
00064 font = self.uiText.document().defaultFont()
00065 self.font( font )
00066 self.fontSize( font.pointSize() )
00067
00068 if attrs.get('translate', False):
00069 self.connect( self.pushTranslate, SIGNAL('clicked()'), self.translate )
00070
00071 self.scTranslate = QShortcut( self.uiText )
00072 self.scTranslate.setKey( Shortcuts.SearchInField )
00073 self.scTranslate.setContext( Qt.WidgetShortcut )
00074 self.connect( self.scTranslate, SIGNAL('activated()'), self.translate )
00075 else:
00076 self.pushTranslate.setVisible( False )
00077
00078 def translate(self):
00079 if not self.record.id:
00080 QMessageBox.information( self, _('Translation dialog'), _('You must save the resource before adding translations'))
00081 return
00082
00083 html = Common.simplifyHtml( self.uiText.document().toHtml() )
00084 dialog = TranslationDialog( self.record.id, self.record.group.resource, self.attrs['name'], html, TranslationDialog.RichEdit, self )
00085 if dialog.exec_() == QDialog.Accepted:
00086 self.record.setValue(self.name, unicode( dialog.result ) or False )
00087
00088 def updateCurrentColors(self):
00089 cursor = self.uiText.textCursor()
00090 format = cursor.charFormat()
00091
00092 p = QPixmap( QSize(40, 10) )
00093 p.fill( format.foreground().color() )
00094 self.pushForegroundColor.setIcon( QIcon(p) )
00095
00096 p = QPixmap( QSize(40, 40) )
00097 p.fill( format.background().color() )
00098 self.pushBackgroundColor.setIcon( QIcon(p) )
00099
00100 def foregroundColor(self):
00101 cursor = self.uiText.textCursor()
00102 format = cursor.charFormat()
00103 color = QColorDialog.getColor( format.foreground().color(), self )
00104 format.setForeground( QBrush( color ) )
00105 cursor.setCharFormat( format )
00106 self.uiText.setTextCursor( cursor )
00107 self.uiText.setFocus()
00108 self.updateCurrentColors()
00109
00110 def backgroundColor(self):
00111 cursor = self.uiText.textCursor()
00112 format = cursor.charFormat()
00113 color = QColorDialog.getColor( format.background().color(), self )
00114 format.setBackground( QBrush( color ) )
00115 cursor.setCharFormat( format )
00116 self.uiText.setTextCursor( cursor )
00117 self.uiText.setFocus()
00118 self.updateCurrentColors()
00119
00120 def cursorPosition(self):
00121 align = self.uiText.alignment()
00122 if align == Qt.AlignLeft:
00123 self.pushLeftJustify.setChecked( True )
00124 elif align == Qt.AlignHCenter:
00125 self.pushCenter.setChecked( True )
00126 elif align == Qt.AlignRight:
00127 self.pushRightJustify.setChecked( True )
00128 else:
00129 self.pushJustify.setChecked( True )
00130 self.charFormatChanged( self.uiText.currentCharFormat() )
00131
00132 def charFormatChanged(self, format):
00133 font = format.font()
00134 self.updateFont = False
00135 self.uiFont.setCurrentFont( font )
00136 self.uiFontSize.setValue( font.pointSize() )
00137 self.pushBold.setChecked( font.weight() == QFont.Bold )
00138 self.pushUnderline.setChecked( font.underline() )
00139 self.pushItalic.setChecked( font.italic() )
00140 self.pushStrike.setChecked( font.strikeOut() )
00141 self.updateCurrentColors()
00142 self.updateFont = True
00143
00144 def fontSize(self, size):
00145 if not self.updateFont:
00146 return
00147 cursor = self.uiText.textCursor()
00148 format = cursor.charFormat()
00149 format.setFontPointSize( size )
00150 cursor.setCharFormat( format )
00151 self.uiText.setTextCursor( cursor )
00152 self.uiText.setFocus()
00153
00154 def font(self, font):
00155 if not self.updateFont:
00156 return
00157 cursor = self.uiText.textCursor()
00158 format = cursor.charFormat()
00159 format.setFont( font )
00160 cursor.setCharFormat( format )
00161 self.uiText.setTextCursor( cursor )
00162 self.uiText.setFocus()
00163
00164 def bold(self):
00165 cursor = self.uiText.textCursor()
00166 format = cursor.charFormat()
00167 if format.fontWeight() == QFont.Bold:
00168 format.setFontWeight( QFont.Normal )
00169 else:
00170 format.setFontWeight( QFont.Bold )
00171 cursor.setCharFormat( format )
00172 self.uiText.setTextCursor( cursor )
00173 self.uiText.setFocus()
00174
00175 def italic(self):
00176 cursor = self.uiText.textCursor()
00177 format = cursor.charFormat()
00178 format.setFontItalic( not format.fontItalic() )
00179 cursor.setCharFormat( format )
00180 self.uiText.setTextCursor( cursor )
00181 self.uiText.setFocus()
00182
00183 def underline(self):
00184 cursor = self.uiText.textCursor()
00185 format = cursor.charFormat()
00186 format.setFontUnderline( not format.fontUnderline() )
00187 cursor.setCharFormat( format )
00188 self.uiText.setTextCursor( cursor )
00189 self.uiText.setFocus()
00190
00191 def strike(self):
00192 cursor = self.uiText.textCursor()
00193 format = cursor.charFormat()
00194 format.setFontStrikeOut( not format.fontStrikeOut() )
00195 cursor.setCharFormat( format )
00196 self.uiText.setTextCursor( cursor )
00197 self.uiText.setFocus()
00198
00199 def leftJustify(self):
00200 self.setAlignment( Qt.AlignLeft )
00201
00202 def center(self):
00203 self.setAlignment( Qt.AlignHCenter )
00204
00205 def rightJustify(self):
00206 self.setAlignment( Qt.AlignRight )
00207
00208 def justify(self):
00209 self.setAlignment( Qt.AlignJustify )
00210
00211 def setAlignment(self, align):
00212 cursor = self.uiText.textCursor()
00213 format = cursor.blockFormat()
00214 format.setAlignment( align )
00215 cursor.setBlockFormat( format )
00216 self.uiText.setTextCursor( cursor )
00217 self.uiText.setFocus()
00218
00219 def setReadOnly(self, value):
00220 AbstractFieldWidget.setReadOnly(self, value)
00221 self.uiText.setReadOnly( value )
00222 self.pushBold.setEnabled( not value )
00223 self.pushItalic.setEnabled( not value )
00224 self.pushUnderline.setEnabled( not value )
00225 self.pushStrike.setEnabled( not value )
00226 self.pushLeftJustify.setEnabled( not value )
00227 self.pushCenter.setEnabled( not value )
00228 self.pushRightJustify.setEnabled( not value )
00229 self.pushJustify.setEnabled( not value )
00230 self.pushForegroundColor.setEnabled( not value )
00231 self.pushBackgroundColor.setEnabled( not value )
00232 self.uiFont.setEnabled( not value )
00233 self.uiFontSize.setEnabled( not value )
00234
00235 def colorWidget(self):
00236 return self.uiText
00237
00238 def storeValue(self):
00239
00240
00241
00242 if self.uiText.document().isModified():
00243 html = Common.simplifyHtml( self.uiText.document().toHtml() )
00244 self.record.setValue(self.name, html or False )
00245
00246 def clear(self):
00247 self.uiText.setHtml('')
00248
00249 def showValue(self):
00250 value = self.record.value(self.name)
00251 if not value:
00252 value=''
00253 self.uiText.setHtml( value )
00254
00255
00256
00257 self.uiText.document().setModified( False )