packetMonitorPrinter.py

Go to the documentation of this file.
00001 #!/usr/local/bin/python
00002 #
00003 #                               Copyright 2005
00004 #                                     by
00005 #                        The Board of Trustees of the
00006 #                     Leland Stanford Junior University.
00007 #                            All rights reserved.
00008 #
00009 
00010 __facility__ = "Online"
00011 __abstract__ = "GLAST ISOC Pcaket Monitor Printer"
00012 __author__   = "A. Kavelaars <aliciak@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "10/19/2005"
00014 __version__  = "$Revision: 1.3 $"
00015 __credits__  = "SLAC"
00016 
00017 import LICOS.copyright_SLAC
00018 import sys, string
00019 from qt                         import *
00020 
00021 class packetMonitorPrinter(object):
00022   def __init__(self, parent,name = None,fl = 0):
00023     self.__parent    = parent
00024     self.printer     = QPrinter()
00025 
00026   def printMon(self, header):
00027     log = self.__parent
00028     
00029     #If Monitor is empty, cancel the print
00030     if log.firstChild() is None:
00031       message = 'This Monitor is empty. Printing cancelled.'
00032       self.showErrorMessage(message)
00033       return
00034   
00035     # Set Defaults and create headers
00036     self.hMargin = 50
00037     self.vMargin = 50
00038     self.pageNo = 1
00039     self.numCols = log.columns()
00040     self.maxLength = 40
00041     # Obtain Column Widths from Log
00042     self.columnWidth = [0]
00043     for i in range(0, self.numCols):
00044       width = log.columnWidth(i)
00045       self.columnWidth.append(width)
00046   
00047     # Call the printer dialog, if setup is OK, start printing.
00048     if self.printer.setup(log):
00049       self.printer.setOrientation(QPrinter.Landscape)
00050       self.__p = QPainter()
00051       self.__p.begin(self.printer)
00052       self.fm = self.__p.fontMetrics()
00053       self.metrics = QPaintDeviceMetrics(self.printer)
00054       yPos = self.createPageTemplate(log, header)
00055       origYPos = yPos
00056 
00057       # Pick rows and print them. If row text is too long, place in next line.
00058       item = log.firstChild()
00059       for j in range(log.childCount()):
00060         # This check makes sure that when next  row is out of page margins, 
00061         # a new page is called with defaults.
00062         if yPos >= self.metrics.height() - self.vMargin - 7*self.fm.lineSpacing():
00063           self.pageNo = self.pageNo + 1
00064           self.printer.newPage()
00065           yPos = self.createPageTemplate(log, header)
00066         line = 1
00067         currentHMargin = 0
00068         for i in range (0,self.numCols):
00069           text = str(item.text(i))
00070           colLine = 0
00071           list = string.split(text, "\n")
00072           for textItem in list:
00073             # If lines of text in a  column field line are too long, new page is called
00074             if yPos + colLine*self.fm.lineSpacing() >= \
00075                self.metrics.height() - self.vMargin - 7*self.fm.lineSpacing():
00076               yPos = self.newPage(log, header, colLine)
00077               line = 1
00078               colLine = 0
00079             self.__p.drawText( self.columnWidth[i] + currentHMargin + self.hMargin,
00080                         self.vMargin + yPos + 2 + colLine*self.fm.lineSpacing(),
00081                         self.metrics.width(),
00082                         self.fm.lineSpacing(),
00083                         Qt.ExpandTabs | Qt.DontClip,
00084                         textItem[:self.maxLength]
00085                       )
00086             colLine += 1
00087             while len(textItem) > self.maxLength:
00088               # If text in a line too long, call new page is called
00089               if yPos + colLine*self.fm.lineSpacing() \
00090                  >= self.metrics.height() - self.vMargin - 7*self.fm.lineSpacing():
00091                 yPos = self.newPage(log, header, colLine)
00092                 line = 1
00093                 colLine = 0
00094               textItem = textItem[self.maxLength:]
00095               self.__p.drawText( self.columnWidth[i] + currentHMargin + self.hMargin,
00096                           self.vMargin + yPos + 2 + colLine*self.fm.lineSpacing(),
00097                           self.metrics.width(),
00098                           self.fm.lineSpacing(),
00099                           Qt.ExpandTabs | Qt.DontClip,
00100                           textItem[:self.maxLength]
00101                         )
00102               colLine += 1
00103             if colLine > line:
00104               line = colLine
00105           currentHMargin += self.columnWidth[i] + 5
00106         self.__p.drawRect( self.hMargin - 5,
00107                     self.vMargin + yPos,
00108                     self.metrics.width() - self.hMargin - 10,
00109                     line*self.fm.lineSpacing()
00110                   )
00111         yPos += line*self.fm.lineSpacing()
00112         item = item.nextSibling()
00113       while yPos <= self.metrics.height() - self.vMargin - 7*self.fm.lineSpacing():
00114         self.__p.drawRect( self.hMargin - 5,
00115                     self.vMargin + yPos, \
00116                     self.metrics.width() - self.hMargin - 10,
00117                     self.fm.lineSpacing()
00118                   )
00119         yPos = yPos + self.fm.lineSpacing()
00120       self.__p.end()
00121     else:
00122       message = 'Printing aborted.'
00123       self.showErrorMessage(message)
00124   
00125   def newPage(self, log, header, colLine):
00126     self.__p.drawRect( self.hMargin - 5,
00127                         self.vMargin + yPos,
00128                         self.metrics.width() - self.hMargin - 10,
00129                         colLine*self.fm.lineSpacing()
00130                       )
00131     self.pageNo = self.pageNo + 1
00132     self.printer.newPage()
00133     yPos = self.createPageTemplate(log, header)
00134     return yPos
00135     
00136 
00137   def createPageTemplate(self, log, header):
00138     self.headerFont = QFont()
00139     self.headerFont.setFamily("Arial")
00140     self.headerFont.setPointSize(12)
00141     self.headerFont.setBold(1)
00142     self.normalFont = QFont()
00143     self.normalFont.setFamily("Arial")
00144     self.normalFont.setPointSize(8)
00145     self.headerPen = QPen()
00146     self.headerPen.setWidth(1)
00147     self.normalPen = QPen()
00148     self.normalPen.setWidth(0)
00149     yPos = 0
00150     
00151     # Main headers
00152     self.__p.setFont(self.headerFont)
00153     if self.secondHeader != None:
00154       self.__p.drawText( self.hMargin + self.metrics.width()/3 + 30,
00155                   self.vMargin + yPos, \
00156                   self.metrics.width(),
00157                   self.fm.lineSpacing(),
00158                   Qt.ExpandTabs | Qt.DontClip,
00159                   header
00160                 )
00161       self.headerFont.setBold(0)
00162       self.__p.setFont(self.headerFont)
00163       self.__p.drawText( self.hMargin,
00164                   self.vMargin + yPos + 2*self.fm.lineSpacing(), \
00165                   self.metrics.width(),
00166                   self.fm.lineSpacing(),
00167                   Qt.ExpandTabs | Qt.DontClip,
00168                   self.secondHeader
00169                 )
00170     else:
00171       self.__p.drawText( self.hMargin + self.metrics.width()/3 + 30,
00172                   self.vMargin + yPos + self.fm.lineSpacing(), \
00173                   self.metrics.width(),
00174                   self.fm.lineSpacing(),
00175                   Qt.ExpandTabs | Qt.DontClip,
00176                   header
00177                 )
00178     yPos = yPos + 4*self.fm.lineSpacing()
00179     self.headerFont.setBold(1)
00180     
00181     # Enclosing frames
00182     self.__p.setPen(self.headerPen)
00183     self.__p.drawRect( self.hMargin - 5,
00184                 self.vMargin - 5,
00185                 self.metrics.width() - self.hMargin - 10,
00186                 yPos
00187               )
00188     self.headerFont.setPointSize(8)
00189     self.__p.setFont(self.headerFont)
00190     
00191     # Column's headers and vertical lines
00192     currentHMargin = 0
00193     for i in range (0, self.numCols):
00194       self.__p.drawText( self.columnWidth[i] + currentHMargin + self.hMargin,
00195                   self.vMargin + yPos,
00196                   self.metrics.width(),
00197                   self.fm.lineSpacing(),
00198                   Qt.ExpandTabs | Qt.DontClip,
00199                   str(log.columnText(i).upper())
00200                 )
00201       self.__p.drawLine( self.columnWidth[i] + currentHMargin + self.hMargin - 5,
00202                   self.vMargin + yPos - 5,
00203                   self.columnWidth[i] + currentHMargin + self.hMargin - 5,
00204                   self.vMargin + yPos + 42*self.fm.lineSpacing()
00205                 )
00206       currentHMargin += self.columnWidth[i] + 5
00207     self.__p.setPen(self.headerPen)
00208     self.__p.drawLine( self.metrics.width() - 15,
00209                 self.vMargin + yPos - 5, \
00210                 self.metrics.width() - 15,
00211                 self.vMargin + yPos + 2*self.fm.lineSpacing() - 1
00212               )
00213     yPos = yPos + 2*self.fm.lineSpacing()
00214     self.__p.drawRect( self.hMargin - 5,
00215                 self.vMargin + yPos,
00216                 self.metrics.width() - self.hMargin - 10,
00217                 40*self.fm.lineSpacing()
00218               )
00219     # Page Number
00220     self.__p.setFont(self.normalFont)
00221     self.__p.drawText( self.hMargin + 10,
00222                 self.metrics.height() - 30,
00223                 5, 5,
00224                 Qt.ExpandTabs | Qt.DontClip,
00225                 str(self.pageNo)
00226               )
00227     self.__p.setPen(self.normalPen)
00228     self.__p.setFont(self.normalFont)
00229     return yPos
00230 
00231   def showErrorMessage(self, message):
00232     QMessageBox.warning(self.__parent, 'Packet Monitor Print Error',\
00233                         message,\
00234                         QMessageBox.Ok)
00235 
00236 
00237 

Generated on Thu Apr 27 20:52:42 2006 for LICOS L02-01-00 by doxygen 1.4.6-NO