packetMonitorPrefsImpl.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 LICOS Packet Monitor Preferences handler"
00012 __author__   = "A. Kavelaars <aliciak@SLAC.Stanford.edu> SLAC - GLAST LAT I&T/Online"
00013 __date__     = "11/15/2005"
00014 __version__  = "$Revision: 1.7 $"
00015 __credits__  = "SLAC"
00016 
00017 import LICOS.copyright_SLAC
00018 
00019 import os
00020 from   qt                 import *
00021 import logging            as myLog
00022 import ConfigParser
00023 from   packetMonitorPrefs import packetMonitorPrefs
00024 from   LICOS.lib.LATconstants       import *
00025 
00026 
00027 
00028 class packetMonitorPrefsImpl(packetMonitorPrefs):
00029   """!
00030   \brief Class for managing Packet Monitor user's preferences
00031 
00032   The Packet Monitor keeps a user's preferences information in a file that is
00033   called .pktmonrc when os.name returns 'posix', and pktmon.cfg otherwise, e.g.
00034   Windows. If the file doesn't exist, a set of defaults are used.
00035   These will be written to a file in the packet monitor source directory if
00036   it is writable. Packet Monitor searches for the preferences file in the
00037   following directories in the order specified:
00038   the current directory, the directory pointed to by the HOME environment variable,
00039   and the Packet Monitor source directory.
00040   """
00041 
00042   def __init__(self, confParser, parent = None,name = None,modal = 0,fl = 0):
00043     packetMonitorPrefs.__init__(self,parent,name,modal,fl)
00044 
00045     self.__gui = parent
00046     self.__confParser = confParser
00047 
00048     for style in QStyleFactory.keys():
00049       self.styleCombo.insertItem(style)
00050 
00051     # Load configuration from file
00052     self.loadConfig()
00053 
00054     self.connect(self.styleCombo,       SIGNAL("activated(const QString&)"), self.selectStyle)
00055     self.connect(self.fontButton,       SIGNAL("clicked()"),                 self.selectFont)
00056     self.connect(self.defaultSysButton, SIGNAL("clicked()"),                 self.setSysDefaults)
00057     self.connect(self.defaultMonButton, SIGNAL("clicked()"),                 self.setMonDefaults)
00058     self.connect(self.buttonOk,         SIGNAL('clicked()'),                 self.savePrefs)
00059 
00060   def loadConfigFile(self):
00061     # Format file name
00062     if os.name == "posix":
00063       configName = ".pktmonrc"
00064     else:
00065       configName = "pktmon.cfg"
00066     # Look for file in current directory
00067     configFile = "./" + configName
00068     # If not found, look for file in HOME directory
00069     if not os.path.exists(configFile):
00070       try:
00071         home = os.environ['HOME']
00072         configFile = os.path.expandvars('$HOME/%s' % configName)    # Home directory
00073       except KeyError:
00074         print "HOME environment variable does not exist"
00075       # If not found in HOME (either because it does not exist or HOME is not set) look in SOURCE directory.
00076       if not os.path.exists(configFile):
00077         configFile = os.path.join(ONLINE_ROOT, 'LICOS/tools/monitor/%s' % configName)
00078 
00079     self.__configFile = configFile
00080 
00081   def loadConfig(self):
00082     """ Loads configuration from file if configuration file exists.
00083     """
00084     self.loadConfigFile()
00085     cp = self.__confParser
00086     if os.path.exists(self.__configFile):
00087       cp.read(self.__configFile)
00088       try:
00089          # Main Window Size, always saved when parent gui closes.
00090         width  = cp.get("pktmon options", "mainWidth")
00091         height = cp.get("pktmon options", "mainHeight")
00092         self.__gui.resize(int(width), int(height))
00093         # Style
00094         style = cp.get("pktmon options", "style")
00095         if QString(style) in QStyleFactory.keys():
00096           for i in range(0, self.styleCombo.count()):
00097             item = self.styleCombo.setCurrentItem(i)
00098             if style == str(self.styleCombo.currentText()):
00099               break
00100           #~ self.styleCombo.setCurrentText(style)
00101         QApplication.setStyle(QStyleFactory.create(style))
00102         # Font
00103         font = QApplication.font()
00104         fontFamily = cp.get("pktmon options", 'fontfamily')
00105         font.setFamily(fontFamily)
00106         fontSize = cp.get("pktmon options", 'fontsize')
00107         font.setPointSize(int(fontSize))
00108         fontWeight = cp.get("pktmon options", 'fontweight')
00109         font.setWeight(int(fontWeight))
00110         fontItalic = cp.get("pktmon options", 'fontitalic')
00111         font.setItalic(int(fontItalic))
00112         QApplication.setFont(font, 1)
00113         # Monitor Row values
00114         maxRow = cp.get("pktmon options", 'maxrow')
00115         self.maxRow.setText(str(maxRow))
00116         delRow = cp.get("pktmon options", 'delrow')
00117         self.delRow.setText(str(delRow))
00118       except ConfigParser.NoSectionError:
00119         pass
00120       except ConfigParser.NoOptionError:
00121         pass
00122       except IOError, e:
00123         myLog.warning("Preferences file not loaded due to: %s" % e)
00124     # Pass row values to GUI
00125     #~ self.__gui.passRowValues()
00126 
00127 
00128   def __defaults(self):
00129     """ Load default values into configuration file. OBSOLETE.
00130     """
00131     cp = self.__confParser
00132     cp.set("pktmon options", "fontfamily", 'LucidaTypewriter')
00133     cp.set("pktmon options", "fontsize",   8)
00134     cp.set("pktmon options", "fontweight", 50)
00135     cp.set("pktmon options", "fontitalic", 0)
00136     #~ cp.set("pktmon options", "mainWidth",  self.__gui.width())
00137     #~ cp.set("pktmon options", "mainHeight", self.__gui.height())
00138 
00139   def setSysDefaults(self):
00140     """ Load default GUI values.
00141     """
00142     self.__gui.resize(775, 625)
00143     if os.name == "posix":
00144       style = 'Bluecurve'
00145     elif os.name == 'nt':
00146       style = 'WindowsXP'
00147     else:
00148       style = 'Default'
00149     family = 'Sans'
00150     font = QApplication.font()
00151     font.setFamily(family)
00152     font.setPointSize(10)
00153     font.setWeight(48)
00154     font.setItalic(0)
00155     QApplication.setFont(font, 1)
00156     QApplication.setStyle(QStyleFactory.create(style))
00157     # Find Style in list and select it
00158     notListed = True
00159     for i in range(0, self.styleCombo.count()):
00160       item = self.styleCombo.setCurrentItem(i)
00161       if style == str(self.styleCombo.currentText()):
00162         notListed = False
00163         break
00164     # Create default style at end of list if it does not exist
00165     if notListed:
00166       self.styleCombo.insertItem('Default', -1)
00167       self.styleCombo.setCurrentItem(self.styleCombo.count() - 1)
00168 
00169   def setMonDefaults(self):
00170     """ Sets Monitors' default maximum number of rows and
00171         number of rows deleted when this number is reached.
00172     """
00173     self.delRow.setText(str(100))
00174     self.maxRow.setText(str(1000))
00175 
00176   def selectStyle(self, style):
00177     """ Sets the application style to the selected style.
00178     """
00179     QApplication.setStyle(QStyleFactory.create(style))
00180     #~ cp = self.__confParser
00181     #~ cp.set("pktmon options", "style", str(style))
00182 
00183   def selectFont(self):
00184     """ Calls a select font dialog and sets the application font
00185         to the selected font value.
00186     """
00187     ok = 0
00188     oldfont = QApplication.font()
00189     newfont, ok = QFontDialog.getFont(oldfont, self)
00190     if ok:
00191       try:
00192         QApplication.setFont(newfont, 1)
00193       except IOError, e:
00194         myLog.warning("New font not loaded due to: %s" % e)
00195         print "New font not loaded"
00196 
00197   def saveMainWindowSize(self):
00198     """ Saves Main Window Size when it is closed.
00199     """
00200     cp = self.__confParser
00201     if not cp.has_section("pktmon options"):
00202       cp.add_section("pktmon options")
00203     cp.set("pktmon options", "mainWidth",  self.__gui.width())
00204     cp.set("pktmon options", "mainHeight", self.__gui.height())
00205     try:
00206       cp.write(file(self.__configFile, "w+"))
00207     except IOError, e:
00208       myLog.warning("Preferences file not saved due to: %s" % e)
00209 
00210   def savePrefs(self):
00211     """ Saves preferences when pressing the OK button of the
00212         Preferences Dialog.
00213     """
00214     cp = self.__confParser
00215     if not cp.has_section("pktmon options"):
00216       cp.add_section("pktmon options")
00217     # Font
00218     font = QApplication.font()
00219     cp.set("pktmon options", "fontfamily", str(font.family()))
00220     cp.set("pktmon options", "fontsize",   font.pointSize())
00221     cp.set("pktmon options", "fontweight", font.weight())
00222     if font.italic():
00223       fontitalic = 1
00224     else:
00225       fontitalic = 0
00226     cp.set("pktmon options", 'fontitalic', fontitalic)
00227     # Style
00228     style = self.styleCombo.currentText()
00229     cp.set("pktmon options", "style", str(style))
00230     # Montior Rows
00231     maxRow = str(self.maxRow.text())
00232     delRow = str(self.delRow.text())
00233     cp.set("pktmon options", "maxrow", maxRow)
00234     cp.set("pktmon options", "delrow", delRow)
00235     # Write it out
00236     try:
00237       cp.write(file(self.__configFile, "w+"))
00238     except IOError, e:
00239       myLog.warning("Preferences file not saved due to: %s" % e)
00240     self.__gui.passRowValues()
00241     self.accept()
00242 

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