Package PyFoam :: Package Applications :: Module APoMaFoXiiQt
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.APoMaFoXiiQt

  1  """ 
  2  Application-class that implements pyFoamAPoMaFoX.py (A Poor Man's FoamX) 
  3  """ 
  4  from os import path 
  5  import sys 
  6   
  7  from PyFoam.Applications.PyFoamApplication import PyFoamApplication 
  8  from PyFoam.Applications.CaseBuilderBackend import CaseBuilderFile,CaseBuilderDescriptionList 
  9  from PyFoam.Applications.CommonCaseBuilder import CommonCaseBuilder 
 10  from PyFoam import configuration as config 
 11   
 12  from PyFoam.Error import error,warning 
 13   
 14  from PyFoam.ThirdParty.six import print_ 
 15   
 16  try: 
 17      import PyQt4 
 18  except ImportError: 
 19      error("This application needs an installed PyQt4-library") 
 20   
 21  from PyQt4 import QtCore, QtGui 
 22   
23 -class APoMaFoXiiQt(PyFoamApplication, 24 CommonCaseBuilder):
25 - def __init__(self,args=None):
26 description="""\ 27 APoMaFoX is "A Poor Mans FoamX". 28 29 A small text interface to the CaseBuilder-Functionality 30 """ 31 PyFoamApplication.__init__(self, 32 args=args, 33 description=description, 34 usage="%prog <caseBuilderFile>", 35 interspersed=True, 36 nr=0, 37 exactNr=False)
38
39 - def addOptions(self):
41
42 - def run(self):
43 if self.pathInfo(): 44 return 45 46 app = QtGui.QApplication(self.parser.getArgs()) 47 48 fName=None 49 if len(self.parser.getArgs())==0: 50 dialog=CaseBuilderBrowser() 51 if len(dialog.descriptions)==1: 52 fName=dialog.descriptions[0][1] 53 self.warning("Automatically choosing the only description",fName) 54 elif len(self.parser.getArgs())==1: 55 fName=self.searchDescriptionFile(self.parser.getArgs()[0]) 56 57 if not path.exists(fName): 58 error("The description file",fName,"does not exist") 59 else: 60 error("Too many arguments") 61 62 if fName!=None: 63 dialog=CaseBuilderDialog(fName) 64 65 dialog.show() 66 sys.exit(app.exec_())
67
68 -class ComboWrapper(QtGui.QComboBox):
69 - def __init__(self):
70 super(ComboWrapper,self).__init__()
71
72 - def text(self):
73 return str(self.currentText())
74
75 -class FilenameWrapper(QtGui.QWidget):
76 - def __init__(self,parent=None):
77 super(FilenameWrapper,self).__init__(parent) 78 layout=QtGui.QHBoxLayout() 79 self.name=QtGui.QLineEdit() 80 layout.addWidget(self.name) 81 button=QtGui.QPushButton("File ...") 82 layout.addWidget(button) 83 self.connect(button,QtCore.SIGNAL("clicked()"),self.pushed) 84 self.setLayout(layout)
85
86 - def pushed(self):
87 try: 88 theDir=path.dirname(self.text()) 89 except AttributeError: 90 theDir=path.abspath(path.curdir) 91 92 fName=QtGui.QFileDialog.getOpenFileName(self, # parent 93 "Select File", # caption 94 theDir) 95 if fName!="": 96 self.setText(str(fName)) 97 98 return False
99
100 - def setText(self,txt):
101 self.name.setText(txt)
102
103 - def text(self):
104 return path.abspath(str(self.name.text()))
105
106 -class CaseBuilderQt(QtGui.QDialog):
107 """The common denominator for the windows"""
108 - def __init__(self,parent=None):
109 super(CaseBuilderQt,self).__init__(parent) 110 self.status=None
111
112 - def setStatus(self,text):
113 print_(text) 114 if not self.status: 115 self.status=QtGui.QStatusBar(self) 116 self.layout().addWidget(self.status) 117 118 self.status.showMessage(text)
119
120 -class CaseBuilderBrowser(CaseBuilderQt):
121 """A browser of all the available descriptions"""
122 - def __init__(self):
123 CaseBuilderQt.__init__(self) 124 125 self.descriptions=CaseBuilderDescriptionList() 126 if len(self.descriptions)==0: 127 error("No description-files (.pfcb) found in path",config().get("CaseBuilder","descriptionpath")) 128 129 mainLayout = QtGui.QVBoxLayout() 130 self.setLayout(mainLayout) 131 132 self.descriptsList = QtGui.QListWidget() 133 self.descriptsList.setSelectionMode(QtGui.QAbstractItemView.SingleSelection) 134 mainLayout.addWidget(self.descriptsList) 135 136 self.itemlist=[] 137 for d in self.descriptions: 138 item=QtGui.QListWidgetItem(d[2]) 139 item.setToolTip(d[3]) 140 self.descriptsList.addItem(item) 141 self.itemlist.append((item,d)) 142 143 buttons=QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel) 144 mainLayout.addWidget(buttons) 145 selectButton=QtGui.QPushButton("Select") 146 selectButton.setToolTip("Select the case description that we want to work with") 147 buttons.addButton(selectButton,QtGui.QDialogButtonBox.AcceptRole) 148 try: 149 buttons.accepted.connect(self.selectPressed) 150 buttons.rejected.connect(self.reject) 151 except AttributeError: 152 warning("Old QT-version where QDialogButtonBox doesn't have the accepted/rejected-attributes") 153 self.connect(buttons,QtCore.SIGNAL("accepted()"),self.selectPressed) 154 self.connect(buttons,QtCore.SIGNAL("rejected()"),self.reject)
155
156 - def selectPressed(self):
157 self.setStatus("Pressed selected") 158 selected=self.descriptsList.selectedItems() 159 if len(selected)!=1: 160 self.setStatus("Nothing selected") 161 return 162 163 desc=None 164 for it,d in self.itemlist: 165 if it==selected[0]: 166 desc=d 167 break 168 169 if desc==None: 170 self.setStatus("Did not find the selection") 171 return 172 173 self.setStatus("") 174 sub=CaseBuilderDialog(desc[1],parent=self) 175 sub.show()
176
177 -class CaseBuilderDialog(CaseBuilderQt):
178 """A dialog for a CaswBuilder-dialog"""
179 - def __init__(self,fName,parent=None):
180 CaseBuilderQt.__init__(self,parent=parent) 181 182 self.desc=CaseBuilderFile(fName) 183 184 # print_("Read case description",self.desc.name()) 185 186 mainLayout = QtGui.QVBoxLayout() 187 self.setLayout(mainLayout) 188 189 mainLayout.addWidget(QtGui.QLabel("Builder Template: " 190 + self.desc.name() 191 +"\n"+self.desc.description())) 192 193 mainLayout.addWidget(QtGui.QLabel("Data Template: " 194 + self.desc.templatePath())) 195 196 try: 197 caseLayout=QtGui.QFormLayout() 198 except AttributeError: 199 warning("Qt-version without QFormLayout") 200 caseLayout=QtGui.QVBoxLayout() 201 202 mainLayout.addLayout(caseLayout) 203 204 self.caseName=QtGui.QLineEdit() 205 self.caseName.setToolTip("The name under which the case will be saved") 206 207 try: 208 caseLayout.addRow("Case name",self.caseName) 209 except AttributeError: 210 caseLayout.addWidget(QtGui.QLabel("Case name")) 211 caseLayout.addWidget(self.caseName) 212 213 args=self.desc.arguments() 214 mLen=max(*list(map(len,args))) 215 aDesc=self.desc.argumentDescriptions() 216 aDef=self.desc.argumentDefaults() 217 allArgs=self.desc.argumentDict() 218 219 self.argfields={} 220 221 groups=[None]+self.desc.argumentGroups() 222 gDesc=self.desc.argumentGroupDescription() 223 224 theGroupTabs=QtGui.QTabWidget() 225 mainLayout.addWidget(theGroupTabs) 226 227 for g in groups: 228 if g==None: 229 name="Default" 230 desc="All the arguments that did not fit into another group" 231 else: 232 name=g 233 desc=gDesc[g] 234 gWidget=QtGui.QWidget() 235 try: 236 gLayout=QtGui.QFormLayout() 237 except AttributeError: 238 gLayout=QtGui.QVBoxLayout() 239 gWidget.setLayout(gLayout) 240 idx=theGroupTabs.addTab(gWidget,name) 241 theGroupTabs.setTabToolTip(idx,desc) 242 243 for a in self.desc.groupArguments(g): 244 theType=allArgs[a].type 245 if theType=="file": 246 print_("File",a) 247 aWidget=FilenameWrapper(self) 248 aWidget.setText(aDef[a]) 249 elif theType=="selection": 250 aWidget=ComboWrapper() 251 aWidget.addItems(allArgs[a].values) 252 aWidget.setCurrentIndex(allArgs[a].values.index(aDef[a])) 253 else: 254 aWidget=QtGui.QLineEdit() 255 aWidget.setText(aDef[a]) 256 aWidget.setToolTip(aDesc[a]) 257 self.argfields[a]=aWidget 258 try: 259 gLayout.addRow(a,aWidget) 260 except AttributeError: 261 gLayout.addWidget(QtGui.QLabel(a)) 262 gLayout.addWidget(aWidget) 263 264 bottomLayout=QtGui.QHBoxLayout() 265 mainLayout.addLayout(bottomLayout) 266 267 self.noClose=QtGui.QCheckBox("Don't close") 268 self.noClose.setToolTip("Do not close after 'Generate'") 269 bottomLayout.addWidget(self.noClose) 270 271 buttons=QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Cancel) 272 bottomLayout.addWidget(buttons) 273 generateButton=QtGui.QPushButton("Generate") 274 generateButton.setToolTip("Copy the template case and modify it according to the settings") 275 buttons.addButton(generateButton,QtGui.QDialogButtonBox.AcceptRole) 276 try: 277 buttons.accepted.connect(self.generatePressed) 278 buttons.rejected.connect(self.reject) 279 except AttributeError: 280 self.connect(buttons,QtCore.SIGNAL("accepted()"),self.generatePressed) 281 self.connect(buttons,QtCore.SIGNAL("rejected()"),self.reject)
282
283 - def generatePressed(self):
284 self.setStatus("Pressed generate") 285 ok=False 286 287 caseName=str(self.caseName.text()) 288 if len(caseName)==0: 289 self.setStatus("Casename empty") 290 return 291 292 if path.exists(caseName): 293 self.setStatus("Directory "+caseName+" already existing") 294 return 295 296 self.setStatus("Generating the case "+caseName) 297 args={} 298 for i,a in enumerate(self.desc.arguments()): 299 args[a]=str(self.argfields[a].text()) 300 if len(args[a])==0: 301 self.setStatus("No argument "+a+" was given") 302 return 303 304 msg=self.desc.verifyArguments(args) 305 if msg: 306 self.setStatus(msg) 307 return 308 309 self.setStatus("With the arguments "+str(args)) 310 311 self.desc.buildCase(caseName,args) 312 ok=True 313 if ok: 314 self.setStatus("") 315 if not self.noClose.isChecked(): 316 self.accept() 317 else: 318 self.setStatus("Generated "+caseName)
319 320 # Should work with Python3 and Python2 321