CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ToolDialog.py
Go to the documentation of this file.
1 import logging
2 import copy
3 import os.path
4 import imp
5 import inspect
6 
7 from PyQt4.QtCore import QCoreApplication,Qt,SIGNAL
8 from PyQt4.QtGui import QDialog,QListWidget,QVBoxLayout,QHBoxLayout,QToolButton,QPushButton,QSplitter,QFileDialog,QMessageBox
9 
10 from Vispa.Main.Application import Application
11 from Vispa.Views.PropertyView import PropertyView
12 from Vispa.Main.Exceptions import exception_traceback
13 from ToolDataAccessor import standardToolsDir,ConfigToolBase
14 
15 class ToolDialog(QDialog):
16  def __init__(self,parent=None):
17  logging.debug(__name__ +': __init__')
18  QDialog.__init__(self,parent)
19  self.resize(600,500)
20  self._selectedTool=None
21  self._processCopy=None
23  self._toolsDir=standardToolsDir
24  self.setWindowFlags(Qt.Window)
25  self.setWindowTitle("Apply tool...")
26  self.fill()
27 
28  def fill(self):
29  logging.debug(__name__ +': fill')
30  self.setLayout(QVBoxLayout())
31 
32  self._splitter=QSplitter()
33  self.layout().addWidget(self._splitter)
34 
35  self._toolList = QListWidget(self._splitter)
36  self.connect(self._toolList, SIGNAL("itemSelectionChanged()"), self.toolSelected)
38 
39  bottom=QHBoxLayout()
40  self.layout().addLayout(bottom)
41  changedir=QPushButton("&Change tools directory...")
42  bottom.addWidget(changedir)
43  self.connect(changedir, SIGNAL('clicked()'), self.changedir)
44  help=QPushButton("&Help")
45  bottom.addWidget(help)
46  self.connect(help, SIGNAL('clicked()'), self.help)
47  bottom.addStretch()
48  cancel = QPushButton('&Cancel')
49  bottom.addWidget(cancel)
50  self.connect(cancel, SIGNAL('clicked()'), self.reject)
51  self.ok=QPushButton("&Apply")
52  bottom.addWidget(self.ok)
53  self.ok.setDefault(True)
54  self.connect(self.ok, SIGNAL('clicked()'), self.apply)
55 
56  def updateToolList(self):
57  self._toolList.clear()
58  # import all tools and register them in toolsDict
59  toolsFiles = [os.path.join(self._toolsDir,f) for f in os.listdir(self._toolsDir) if f.endswith(".py") and not f.startswith("_")]
60  self._toolsDict={}
61  for toolsFile in toolsFiles:
62  pythonModule = os.path.splitext(os.path.basename(toolsFile))[0]
63  module=imp.load_source(pythonModule, toolsFile)
64  for name in dir(module):
65  tool=getattr(module,name)
66  if inspect.isclass(tool) and issubclass(tool,ConfigToolBase) and not self._toolDataAccessor.label(tool) in self._toolsDict.keys() and not tool==ConfigToolBase:
67  self._toolsDict[self._toolDataAccessor.label(tool)]=tool
68  # Show test tool
69  #from FWCore.GuiBrowsers.editorTools import ChangeSource
70  #self._toolsDict["ChangeSource"]=ChangeSource
71  if len(self._toolsDict.keys())==0 and self._toolsDir==standardToolsDir:
72  logging.error(__name__ + ": Could not find any PAT tools. These will be available for the ConfigEditor in a future release.")
73  QCoreApplication.instance().errorMessage("Could not find any PAT tools. These will be available for the ConfigEditor in a future release.")
74  return
75  for item in self._toolsDict.keys():
76  self._toolList.addItem(item)
77  self._toolList.sortItems()
78 
79  def keyPressEvent(self, event):
80  if event.modifiers() == Qt.ControlModifier and event.key() == Qt.Key_W:
81  self.reject()
82  QDialog.keyPressEvent(self, event)
83 
84  def tool(self):
85  return self._selectedTool
86 
87  def toolSelected(self):
88  self._selectedTool=self._toolsDict[str(self._toolList.currentItem().text())]()
89  self._properties.setDataObject(self._selectedTool)
90  self._properties.updateContent()
91 
92  def setDataAccessor(self,accessor):
93  self._properties.setDataAccessor(accessor)
94  self._toolDataAccessor=accessor
95  # save process copy to undo changes during the tool dialog
96  self._processCopy=copy.deepcopy(self._toolDataAccessor.configDataAccessor().process())
97  self.updateToolList()
98 
99  def apply(self):
100  # allow property view to process parameter changes
101  if not self.ok.hasFocus():
102  self._properties.clearFocus()
103  self.ok.setFocus()
104  return
105  parameterErrors=self._toolDataAccessor.parameterErrors(self._selectedTool)
106  if len(parameterErrors)>0:
107  message="\n".join([error for error in parameterErrors])
108  QCoreApplication.instance().errorMessage(message)
109  return
110  ok=True
111  if self._selectedTool:
112  try:
113  self._selectedTool.apply(self._toolDataAccessor.configDataAccessor().process())
114  if not self._toolDataAccessor.configDataAccessor().process().checkRecording():
115  ok=False
116  logging.error(__name__ + ": Could not apply tool "+self._toolDataAccessor.label(self._selectedTool)+" (problem with enable recording flag) Please restart the ConfigEditor.")
117  QCoreApplication.instance().errorMessage("Could not apply tool "+self._toolDataAccessor.label(self._selectedTool)+" (problem with enable recording flag) Please restart the ConfigEditor.")
118  except Exception,e:
119  ok=False
120  logging.error(__name__ + ": Could not apply tool "+self._toolDataAccessor.label(self._selectedTool)+": "+exception_traceback())
121  QCoreApplication.instance().errorMessage("Cannot apply tool "+self._toolDataAccessor.label(self._selectedTool)+" (see log file for details):\n"+str(e))
122  else:
123  ok=False
124  logging.error(__name__ + ": Could not apply tool: No tool selected.")
125  QCoreApplication.instance().errorMessage("Cannot apply tool: No tool selected.")
126  # recover process copy to undo changes during the tool dialog
127  self._toolDataAccessor.configDataAccessor().setProcess(self._processCopy)
128  if ok:
129  self.accept()
130 
131  def changedir(self):
132  filename = QFileDialog.getExistingDirectory(
133  self,'Select a directory',self._toolsDir,QFileDialog.ShowDirsOnly)
134  if not filename.isEmpty():
135  self._toolsDir=str(filename)
136  self.updateToolList()
137 
138  def help(self):
139  QMessageBox.about(self, 'Info', "This dialog let's you choose and configure a tool.\n 1. Select a tool on the left\n 2. Set the parameters on the right. If you hold the mouse over a parameter name a tooltip with a description of the parameter will show up.\n 3. Apply the tool. In case the tool has wrong parameters set an error message will be displayed.")
tuple text
Definition: runonSM.py:42
static std::string join(char **cmd)
Definition: RemoteFile.cc:18
dbl *** dir
Definition: mlp_gen.cc:35
tuple process
Definition: LaserDQM_cfg.py:3