CMS 3D CMS Logo

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