Home | Trees | Indices | Help |
|
---|
|
1 from decimal import Decimal 2 import sys 3 import re 4 import os 5 import xml.sax.handler 6 import traceback 7 from muntjac.api import * 8 from mjextras import * 9 103813 self.stack = [] 14 self.root = DataNode() 15 self.current = self.root 16 self.text_parts = [] 17 self.parent=parent19 self.stack.append((self.current, self.text_parts)) 20 self.current = DataNode() 21 self.text_parts = [] 22 # xml attributes --> python attributes 23 for k, v in attrs.items(): 24 self.current._add_xml_attr(self.parent._name_mangle(k), v)26 text = ''.join(self.text_parts).strip() 27 if text: 28 self.current.data = text 29 if self.current._attrs: 30 obj = self.current 31 else: 32 # a text only node is simply represented by the string 33 obj = text or '' 34 self.current, self.text_parts = self.stack.pop() 35 self.current._add_xml_attr(self.parent._name_mangle(name), obj)37 self.text_parts.append(content)7747 if isinstance(key, basestring): 48 return self._attrs.get(key,None) 49 else: 50 return [self][key]54 return bool(self._attrs or self.data)56 if name.startswith('__'): 57 # need to do this for Python special methods??? 58 raise AttributeError(name) 59 return self._attrs.get(name,None)61 if name in self._attrs: 62 # multiple attribute of the same name are represented by a list 63 children = self._attrs[name] 64 if not isinstance(children, list): 65 children = [children] 66 self._attrs[name] = children 67 children.append(value) 68 else: 69 self._attrs[name] = value71 return self.data or ''79 83 86 8795 96 #see if key exists in an object89 builder = TreeBuilder(self) 90 if isinstance(self.src,basestring): 91 xml.sax.parseString(self.src, builder) 92 else: 93 xml.sax.parse(self.src, builder) 94 return builder.root._attrs.values()[0]98 try: 99 e=getattr(theobj,thevar) 100 if e=="":return False 101 else:return True 102 except: 103 return False104 108 109 #generate QIC code class that will create the code for 123 #print self.ui.__dict__ 124412 413 # test against a test ui file if run as main 414 if __name__=="__main__": 415 conv = QT2Muntjac() 416 conv.translateUI('DEMO.ui') 417127 #when the initial widget is passed in main eq true so that A main object can be generated 128 if mainUI==None: 129 self.mainUI=MuntJacWindow() 130 #when child widgets are created the original object reference is passed in. 131 else: 132 self.mainUI=mainUI 133 134 self.main = main 135 self.parent = parent # parent 136 self.widget = widget # current widget 137 self.cls = widget['class'] # for some reason class is a reserved word in python ( :) ) 138 self.mainwidget = None 139 self.isLayout = False 140 141 if self.main==False: 142 if self.parent.wHandleSize==False: 143 self.wHandleSize=False 144 else: 145 self.wHandleSize=True 146 else: 147 self.wHandleSize=False 148 149 found, geometry = self.getProperty("geometry") 150 if found: 151 self.geometry=geometry 152 else: 153 self.geometry=None 154 155 self.makeWidget() 156 self.makeChildren()157159 if not self.main: 160 #TODO: handle the enabled disabled and readonly properties of all widgets 161 162 if self.widget['class']=="QWidget": 163 #the qwidget is like an Absolutelayout unless it has a layout defined inside of it 164 if self.widget.layout!=None: 165 if self.widget.layout["class"]=="QVBoxLayout": 166 self.mainUI.__dict__[self.widget.layout.name]=VerticalLayout() 167 self.thiswidget = self.mainUI.__dict__[self.widget.layout.name] 168 self.wHandleSize=False 169 self.isLayout=True 170 elif self.widget.layout["class"]=="QHBoxLayout": 171 self.mainUI.__dict__[self.widget.layout.name]=HorizontalLayout() 172 self.thiswidget = self.mainUI.__dict__[self.widget.layout.name] 173 self.wHandleSize=False 174 self.isLayout=True 175 176 elif self.widget.layout==None: 177 self.mainUI.__dict__[self.widget.name]=AbsoluteLayout() 178 self.thiswidget = self.mainUI.__dict__[self.widget.name] 179 self.wHandleSize=True 180 #if the parent of this qwidget is the mainwindow then use the geometry from the mainwindow for it 181 if self.parent.widget['class']=="QMainWindow": 182 self.parent.mainwidget = self.thiswidget 183 self.mainUI.width = self.parent.geometry.rect.width 184 self.mainUI.height = self.parent.geometry.rect.height 185 186 elif self.widget['class']=="QStackedWidget": 187 #there is not a stacked widget in munjac. So in mjextras i made one by removing the 188 #tab headers from a tabsheet 189 self.mainUI.__dict__[self.widget.name]=StackedSheet() 190 self.thiswidget = self.mainUI.__dict__[self.widget.name] 191 192 elif self.widget['class']=="QGroupBox": 193 #for muntjac since there is no groupbox I am adding an AbsoluteLayout to a Panel 194 title=str(self.getTitle()) 195 self.mainUI.__dict__[self.widget.name+'Panel']=Panel(title) 196 self.thiswidget = self.mainUI.__dict__[self.widget.name+'Panel'] 197 self.mainUI.__dict__[self.widget.name]=AbsoluteLayout() 198 self.thiswidget.setContent(self.mainUI.__dict__[self.widget.name]) 199 200 elif self.widget['class']=="QLabel": 201 #QLabel makes a Label() they are alike 202 text=str(self.getText()) 203 self.mainUI.__dict__[self.widget.name]=Label(text) 204 self.thiswidget = self.mainUI.__dict__[self.widget.name] 205 206 elif self.widget['class']=="QLineEdit": 207 #conversion about the same only you cannot use the height or textfield gets 208 #automatically converted to a TextArea by muntjac 209 text=str(self.getText()) 210 self.mainUI.__dict__[self.widget.name]=TextField("") 211 self.thiswidget = self.mainUI.__dict__[self.widget.name] 212 self.thiswidget.setValue(text) 213 214 elif self.widget['class']=="QPushButton": 215 #QPushButton converts to Button very well. In some of the widgetsets height doesnt translate well ie runo 216 text=str(self.getText()) 217 self.mainUI.__dict__[self.widget.name]=Button(text) 218 self.thiswidget = self.mainUI.__dict__[self.widget.name] 219 220 elif self.widget['class']=="QTextEdit": 221 #the difference with QTextEdit and TextArea is that textarea is resizeable 222 #and as well QTextEdit has richtext in it. 223 #TODO: convert richtext over to text for TextArea 224 #TODO: figure out if richtextarea is needed 225 html=str(self.getHtml()) 226 self.mainUI.__dict__[self.widget.name]=TextArea() 227 self.thiswidget = self.mainUI.__dict__[self.widget.name] 228 self.thiswidget.setValue(html) 229 230 elif self.widget['class']=="QTabWidget": 231 #QTabWidget and TabSheet are very similar 232 self.mainUI.__dict__[self.widget.name]=TabSheet() 233 self.thiswidget = self.mainUI.__dict__[self.widget.name] 234 235 elif self.widget['class']=="QTableView" or self.widget['class']=="QTableWidget": 236 #QTableView is more like Table than QTableWidget. But I convert both to Table 237 self.mainUI.__dict__[self.widget.name]=Table() 238 self.thiswidget = self.mainUI.__dict__[self.widget.name] 239 240 elif self.widget['class']=="QCheckBox": 241 #QCheckBox works pretty much like CheckBox only the state info is different. 242 #TODO: set the correct state of the checkbox from the ui file 243 self.mainUI.__dict__[self.widget.name]=CheckBox() 244 self.thiswidget = self.mainUI.__dict__[self.widget.name] 245 246 else: 247 #left in print statement for unhandled widgets 248 print "could not handle ",self.widget['class'] 249 self.wHandleSize=False 250 return 251 252 if self.wHandleSize: 253 self.handleSize() 254 255 256 #determine the name and class name of the widget 257 258 #handle if it is a layout 259 if self.parent.widget['class']=="QWidget": 260 if self.parent.widget.layout!=None: 261 pnname=self.parent.widget.layout.name 262 pnclass=self.parent.widget.layout['class'] 263 else: 264 pnname=self.parent.widget.name 265 pnclass=self.parent.widget['class'] 266 if self.widget.layout!=None: 267 thname=self.widget.layout.name 268 thclass=self.widget.layout['class'] 269 else: 270 thname=self.widget.name 271 thclass=self.widget['class'] 272 273 #handle if it is not a layout 274 else: 275 pnname=self.parent.widget.name 276 pnclass=self.parent.widget['class'] 277 thname=self.widget.name 278 thclass=self.widget['class'] 279 280 281 #handle adding the components to the parent 282 #handle containers 283 if pnclass=="QVBoxLayout" or \ 284 pnclass=="QHBoxLayout" or \ 285 pnclass=="QTabWidget" or \ 286 pnclass=="QStackedWidget": 287 if pnclass!="QMainWindow": 288 if self.parent.widget['class']=="QStackedWidget" or self.parent.widget['class']=="QTabWidget": 289 self.parent.thiswidget.addTab(self.thiswidget) 290 if thclass=="QGroupBox": 291 self.thiswidget=self.mainUI.__dict__[thname] 292 else: 293 self.parent.thiswidget.addComponent(self.thiswidget) 294 if thclass=="QGroupBox": 295 self.thiswidget=self.mainUI.__dict__[thname] 296 #handle widgets 297 else: 298 if pnclass!="QMainWindow" and pnclass!="QVBoxLayout" and pnclass!="QHBoxLayout": 299 if self.parent.geometry!=None: 300 print "adding",thname,"to",pnname,"is a",pnclass,"is a",type(self.parent.thiswidget) 301 xpos="%.2f" % (((float(self.geometry.rect.x)+.001)/(float(self.parent.geometry.rect.width)+.001))*100) 302 ypos="%.2f" % (((float(self.geometry.rect.y)+.001)/(float(self.parent.geometry.rect.height)+.001))*100) 303 self.parent.thiswidget.addComponent(self.thiswidget, 304 "top:"+str(ypos)+"%;left:"+str(xpos)+"%") 305 else: 306 print "adding",thname,"to",pnname,"is a",pnclass,"is a",type(self.parent.thiswidget) 307 self.parent.thiswidget.addComponent(self.thiswidget, 308 "top:"+str(self.geometry.rect.y)+"%;left:"+str(self.geometry.rect.x)+"%") 309 if thclass=="QGroupBox": 310 self.thiswidget=self.mainUI.__dict__[thname]311 312 #handle the sizing of the current widget314 if self.geometry==None: 315 self.geometry=self.parent.geometry 316 wdth = "%.2f" % ((float(self.parent.geometry.rect.width)/float(self.parent.geometry.rect.width))*100) 317 hgth = "%.2f" % ((float(self.parent.geometry.rect.height)/float(self.parent.geometry.rect.height))*100) 318 if self.widget.name=="gbValidation": 319 print self.widget.name,wdth,hgth 320 if self.widget['class']!="QPanel": 321 self.thiswidget.setHeight(str(hgth)+"%") 322 else: 323 self.thiswidget.setHeight(str(self.parent.geometry.rect.height)+"px") 324 self.thiswidget.setWidth(str(wdth)+"%") 325 else: 326 wdth = "%.2f" % ((float(self.geometry.rect.width)/(float(self.parent.geometry.rect.width)-float(self.geometry.rect.x)))*100) 327 hgth = "%.2f" % ((float(self.geometry.rect.height)/(float(self.parent.geometry.rect.height)-float(self.geometry.rect.y)))*100) 328 if self.widget.name=="gbValidation": 329 print self.widget.name,wdth,hgth 330 if self.widget['class']!="QLineEdit": 331 #if self.widget['class']!="QPanel": 332 self.thiswidget.setHeight(str(hgth)+"%") 333 #else: 334 # self.thiswidget.setHeight(str(self.geometry.rect.height)+"px") 335 self.thiswidget.setWidth(str(wdth)+"%")336 337 #get title from xml properties339 found, title=self.getProperty("title") 340 if found:title=title.string 341 else:title="" 342 return title343 344 #get text from xml properties346 found, thetext=self.getProperty("text") 347 if found:thetext=thetext.string 348 else:thetext="" 349 return thetext350 351 #get html from xml properties353 found, html=self.getProperty("html") 354 if found:html=html.string 355 else:html="" 356 return html357 358 #handle creation of the children widgets within this widget360 if self.isLayout: 361 if not self.widget.layout.item is None: 362 for awidget in self.widget.layout.item: 363 aWidget(self,awidget.widget,self.mainUI) 364 else: 365 if not self.widget.widget is None: 366 for awidget in self.widget.widget: 367 aWidget(self,awidget,self.mainUI) 368 if self.main: 369 #finish up mainwindow commands 370 pass371 372 #get a property from xml of this widget374 widget=self.widget 375 if cfkey(widget,"property"): 376 if type(widget.property) is list: 377 for prop in widget.property: 378 if prop.name==name: 379 return True,prop 380 return False,"" 381 else: 382 try: 383 if widget.property.name==name: 384 return True,widget.property 385 else: 386 return False,"" 387 except: 388 return False,"" 389 else: 390 return False,""391 392 #get a property from a widget394 if cfkey(widget,"property"): 395 if type(widget.property) is list: 396 for prop in widget.property: 397 if prop.name==name: 398 ret=str(getattr(prop,ref)) 399 return ret 400 return "" 401 else: 402 try: 403 if widget.property.name==name: 404 ret=str(getattr(widget.property,ref)) 405 return ret 406 else: 407 return "" 408 except: 409 return "" 410 else: 411 return ""
Home | Trees | Indices | Help |
|
---|
Generated by Epydoc 3.0.1 on Sat Apr 20 16:01:33 2013 | http://epydoc.sourceforge.net |