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.QtGui import *
00030 from PyQt4.QtCore import *
00031 from PyQt4.QtWebKit import *
00032 
00033 from Koo import Rpc
00034 from Koo.Common import Api
00035 
00036 class HelpWidget( QWebView ):
00037 
00038         FieldType = 1
00039         ViewType = 2
00040         MenuType = 3
00041 
00042         def __init__(self, parent=None):
00043                 QWebView.__init__(self, parent)
00044                 self.setWindowFlags( Qt.Popup )
00045                 self.setFixedSize( 600, 400 )
00046                 self.manager = Rpc.RpcNetworkAccessManager( self.page().networkAccessManager() )
00047                 self.page().setNetworkAccessManager( self.manager )
00048                 self.page().setLinkDelegationPolicy( QWebPage.DelegateExternalLinks )
00049                 self.connect( self, SIGNAL( 'linkClicked(QUrl)' ), self.openLink )
00050 
00051                 
00052                 screenHeight = QApplication.desktop().screenGeometry().height()
00053                 screenWidth = QApplication.desktop().screenGeometry().width()
00054                 pos = parent.parent().mapToGlobal( parent.pos() )
00055 
00056                 
00057                 y = pos.y() + parent.height()
00058                 if y + self.height() > screenHeight:
00059                         y = pos.y() - self.height()
00060                         if y < 0:
00061                                 y = screenHeight - self.height()
00062                 
00063                 x = pos.x()
00064                 if x < 0:
00065                         x = 0
00066                 elif x + self.width() > screenWidth:
00067                         x = screenWidth - self.width()
00068 
00069                 self.move( x, y )
00070 
00071                 self._label = ''
00072                 self._help = ''
00073                 self._filter = ()
00074                 self._type = None
00075 
00076         def mousePressEvent(self, event):
00077                 
00078                 
00079                 if not self.geometry().contains( event.globalPos() ):
00080                         self.hide()
00081                         return
00082                 QWebView.mousePressEvent(self, event)
00083 
00084 
00085         def setLabel(self, text):
00086                 self._label = text
00087                 self.updateText()
00088 
00089         def setHelp(self, text):
00090                 self._help = text
00091                 self.updateText()
00092         
00093         def setFilter(self, filter):
00094                 self._filter = filter
00095                 self.updateText()
00096 
00097         def setType(self, type):
00098                 self._type = type
00099                 self.updateText()
00100 
00101         def openLink(self, url):
00102                 Api.instance.createWebWindow( unicode( url.toString() ), _('Documentation') )
00103                 self.hide()
00104 
00105         def updateText(self):
00106                 if not self._type:
00107                         return
00108 
00109                 installed = True
00110                 if self._filter:
00111                         if self._type == self.FieldType:
00112                                 function = 'get_field_headings'
00113                         elif self._type == self.ViewType:
00114                                 function = 'get_view_headings'
00115                         else:
00116                                 function = 'get_menu_headings'
00117                         try:
00118                                 headings = Rpc.session.call('/object','execute','ir.documentation.paragraph', function, self._filter, Rpc.session.context)
00119                         except Rpc.RpcProtocolException:
00120                                 headings = []
00121                                 installed = False
00122                         except Rpc.RpcServerException:
00123                                 headings = []
00124                                 installed = False
00125                 else:
00126                         headings = []
00127 
00128                 htmlHeadings = []
00129                 for heading in headings:
00130                         html = '<div style="padding: 2px; background-color: Lavender;"><p><small><a style="text-decoration:none;" href="openerp://ir.documentation.file/get/index.html#%s">%s</a></small></p></div>' % (heading[0], heading[1])
00131                         html = html.replace('\\n','')
00132                         htmlHeadings.append( html )
00133 
00134                 
00135 
00136 
00137                 if not installed:
00138                         notInstalledMessages = {
00139                                 self.FieldType : _('<p><i>Documentation module is not installed.</i></p>'),
00140                                 self.MenuType : _('<p><i>The following sections in the documentation refer to this menu entry:</i></p>'),
00141                         }
00142                         references = _("<p><i>You may want to install the 'documentation' module for more information.</i></p>")
00143                 elif htmlHeadings:
00144                         foundMessages = {
00145                                 self.FieldType : _('<p><i>The following sections in the documentation refer to this field:</i></p>'),
00146                                 self.ViewType : _('<p><i>The following sections in the documentation refer to this view:</i></p>'),
00147                                 self.MenuType : _('<p><i>The following sections in the documentation refer to this menu entry:</i></p>'),
00148                         }
00149                         references = foundMessages[self._type]
00150                         references += '<br/>'.join( htmlHeadings )
00151                 else:
00152                         notFoundMessages = {
00153                                 self.FieldType : _('<p><i>No sections in the documentation refer to this field.</i></p>'),
00154                                 self.ViewType : _('<p><i>No sections in the documentation refer to this view.</i></p>'),
00155                                 self.MenuType : _('<p><i>No sections in the documentation refer to this menu entry.</i></p>'),
00156                         }
00157                         references = notFoundMessages[self._type]
00158                         
00159                 html = '<html><body style="background-color: #FFFFF0;"><p><b>%s</b></p><p>%s</p><p>%s</p></body></html>' % (self._label, self._help, references)
00160                 self.setHtml( html )
00161