コード例 #1
0
ファイル: children.py プロジェクト: kaijfox/captan
def parse(root,mainWindowId,interfacename, iswindow=True, isbufferobject=False):
    code = ""
    
    for element in root:
        tag = element.tag.lower()
        try:
            widgetType = types[tag] # try to get the widgets type
        except KeyError: # if there's an error, throw a fit
            raise captan.errors.UnkownWidgetError(interfacename,tag," is not a known widget type")
        
        
        if element.get("id") is None and root.tag.lower() != "buffer": # make sure the widget has an id
            # buffer objects are explicitly given id's
            addDefaultId(element)
            
        if isbufferobject is True: # if this widget is a buffer object
            code += "class " + element.get("type") + widgetType['widgetName'] + '(Q' + widgetType['widgetName'] + '):\n'
            code += "\tdef __init__(self,parent=None):\n"
            code += "\t\tsuper(" + element.get("type") + widgetType['widgetName'] + ",self).__init__(parent)\n"
            if tag == "sheet": code += "\t\tself.setWindowModality(Qt.WindowModal)\n"
            
            element.set("id", "self") # the main object in a class has id self
                
        elif iswindow is False: # if the widget is not in a main window (it bust be somewhere in the buffer)
            # but is not a buffer object
            element.set("id", "self." + element.get("id"))
            # we need to add 'self.' to the id so that the object can be an instance variable
        
        
        if tag not in Layouts: # if the widget is not a layout
            code += parseWidget(widgetType, element, root, mainWindowId, interfacename, iswindow)
        elif tag in Layouts: # if the element is a layout
            code += parseLayout(widgetType, element, root, mainWindowId, interfacename, iswindow)
        

        code += handleText(widgetType,element, iswindow)
        code += captan.props.handleProps(widgetType,element, iswindow)
        code += handleSlots(widgetType,element, iswindow)
        
        
        if len(element) > 0: # parse the element's children, if it has any
            code += parse(element, mainWindowId, interfacename, iswindow)
            
        
        if isbufferobject is True: # this part is separate because the 
            # buffer object models come last
            if element.get("model") is not None:
                # get the functions of the buffer objects model
                for line in getFuncs(element.get("model")).split("\n"):
                    code += "\t" + line + "\n"
            # and here we create the new function
            code += "def new" + element.get("type") + widgetType['widgetName'] + "(parent=None):\n"
            code += "\tdef slotfunc(*args):\n"
            code += "\t\td = " + element.get("type") + widgetType['widgetName'] + "(parent)\n"
            code += "\t\td.show()\n"
            code += "\treturn slotfunc\n"

            
    return code
コード例 #2
0
ファイル: main.py プロジェクト: kaijfox/captan
def compile(interfacename, dist=False):  # @ReservedAssignment
    code = ""
    
    mainWindowId = None

    
    code += "from PySide.QtGui import *\nfrom PySide.QtCore import *\nimport sys\n"
    
    if interfacename == app.manifest['start']:
        code += "app = QApplication(sys.argv)\n"
        a = captan.style.parseStyle()
        code += "app.setStyleSheet('''" + a + "''')\n"
        print a
    # only create a QApplication if it is the main file
    
    
    for filename in [ f for f in os.listdir("views") if os.path.isfile(os.path.join("views",f)) ]:
        if not filename == interfacename + ".xml" and interfacename == app.manifest['start']:
            code += "import "
            if not dist:
                code += "bin."
            code += ".".join(filename.split(".")[:-1]) + " as " + ".".join(filename.split(".")[:-1]) + "\n"
    
    
    code += getFuncs(interfacename)
    
    if interfacename == app.manifest['start']:
        try: # call the end function if it exists
            exec "models." + interfacename + ".init" 
            code += "init(sys.argv)\n"
        except: pass # init should only be called for the main file     
    
    tree = xmlparse("views" + QDir.separator() + interfacename + '.xml')
    tree = tree.getroot() # get the tree
    
    
    for element in tree: # parse the top-level tags
        if element.tag.lower() == "window": # the MainWindow
            if element.get("id") is None:
                captan.children.addDefaultId(element)
            mainWindowId = element.get("id")
            
            code += parseWindow(element, tree, mainWindowId, interfacename)
            # delegate the task to captan.main.parseWindow
            
        elif element.tag.lower() == "buffer":
            code += parseBuffer(element,interfacename)
    
    if mainWindowId is not None: # show the MainWindow, if there is one
        code += mainWindowId + ".show()\n"
    
    
    if interfacename == app.manifest['start']: # execute the app if it is the main view
        code += "APP_RESULT = app.exec_()\n" # execute the application and get the result
    
        try: # call the end function if it exists
            exec "models." + interfacename + ".end" 
            code += "end(APP_RESULT)\n"
        except: pass
    
        code += "sys.exit(APP_RESULT)\n"


    f = open(getProjectFile(interfacename + ".py"),'w')
    f.write(code)