Esempio n. 1
0
 def create_window(self,owner,name,content=None):
     node=parser(content=content) if content else \
         self.__windows.get(name.lower())
     if node is not None:
         cfg=node.attrib
         owner.menus={}
         if not hasattr(owner,'_actions'):
             owner._actions={}
         if not hasattr(owner,'actiongroups'):
             owner.actiongroups={}
         for child in node:
             tag=child.tag
             if tag=='link':
                 child=self.get_link(child)
                 tag=child.tag
             attrib=child.attrib
             if tag in self.tools:
                 getattr(self,'create_%s'%(tag))(child,owner)
             elif tag in self.layouts:
                 owner.setLayout(self.create_layout(child,owner,owner))
             elif tag == 'property': 
                 self.set_property(owner,owner,attrib)
             elif tag=='connect':
                 self.do_connect(owner,owner,attrib) 
             elif tag=='variable':
                 self.set_var(owner,owner,attrib)
Esempio n. 2
0
 def open_ui(file_name):
     self=QGui
     try:
         self.ui_root=parser(file_name)
     except:
         self.ui_root=[]
     self.__windows={}
     self.__widgets={}
     for node in self.ui_root:
         name=node.attrib.get('name').lower()
         if name:
             if node.tag=='window':
                 self.__windows[name]=node
             elif node.tag=='widget':
                 self.__widgets[name]=node
Esempio n. 3
0
 def __init__(self,file_name=None):   #初始化函数
     if file_name is not None:   #如果有文件名,则加载UI资源
         try:
             for node in parser(file_name):
                 if 'name' in node.attrib:  #缓存窗口资源
                     self.windows[node.attrib['name'].lower()]=node
                 if 'id' in node.attrib:    #缓存锚点资源
                     self.resources[node.attrib['id'].lower()]=node
         except:
             pass
     #缓存所有的Widget及Layout
     widgets=[x for x in globals() if x.startswith('Q')]
     for x in widgets:
         try:
             v=eval(x)
             if issubclass(v,QWidget) or issubclass(v,QLayout):
                 self.WIDGETS[x.lower()]=v
         except:
             pass
Esempio n. 4
0
 def __init__(self, file_name=None):  #初始化函数
     if file_name is not None:  #如果有文件名,则加载UI资源
         try:
             for node in parser(file_name):
                 if 'name' in node.attrib:  #缓存窗口资源
                     self.windows[node.attrib['name'].lower()] = node
                 if 'id' in node.attrib:  #缓存锚点资源
                     self.resources[node.attrib['id'].lower()] = node
         except:
             pass
     #缓存所有的Widget及Layout
     widgets = [x for x in globals() if x.startswith('Q')]
     for x in widgets:
         try:
             v = eval(x)
             if issubclass(v, QWidget) or issubclass(v, QLayout):
                 self.WIDGETS[x.lower()] = v
         except:
             pass
Esempio n. 5
0
    def setup_UI(self,owner,name=None,text=None,apps=None):#创建窗口
        actions={}                  #初始化actions
        actiongroups={}             #初始化actiongroups
        def proc_apps(head):            #处理apps,主要是处理菜单及工具栏 
            for app in apps:
                name=app['class']
                text=app.get('text','')
                action=QAction(text,head)
                action.setIcon(self.create_icon(owner,app.get('icon')))
                shortcut=app.get('shortcut')
                if shortcut:
                    action.setShortcut(shortcut)
                action.triggered.connect(partial(owner.add_child,name))
                actions[name]=action
                group=app.get('group',None)
                if group:
                    if group not in actiongroups:
                        actiongroups[group]=QActionGroup(head)
                    actiongroups[group].addAction(action)
            owner.app_list=actions.copy()
                    
        def create_widget(head,tag,attrib):  #创建控件或布局
            Widget=self.get_widget(tag)      #获取控件的类型,如失败则返回
            if Widget is None:return     
            if issubclass(Widget,QLayout): #布局的处理
                return create_layout(head,Widget,attrib)
            text=attrib.get('text')     
            widget=Widget(text)             #生成控件
            if hasattr(head,'addPermanentWidget'):#StatusBar控件处理
                head.addPermanentWidget(widget)
            elif hasattr(head,'addWidget'):  #摆放控件
                add_layout(head,'addWidget',widget,attrib)
            elif hasattr(head,'setCentralWidget'):
                head.setCentralWidget(widget)
            return widget
        
        def create_layout(head,Layout,attrib):#生成布局
            layout=Layout()
            if isinstance(head,QLayout):  #摆放布局
                add_layout(head,'addLayout',layout,attrib)
            elif hasattr(head,'setLayout'):
                head.setLayout(layout)
            return layout

        def add_layout(layout,func,widget,attrib):#摆放布局或控件
            if isinstance(layout,QFormLayout):#表单布局的处理
                label=attrib.get('label')
                args=[widget] if label is None else[label,widget]
                layout.addRow(*args)
            elif isinstance(layout,QGridLayout):#表格布局的处理
                pos=attrib.get('pos')
                args=[int(x) for x in pos.split(',')]
                if len(args) in (2,4):
                    args.insert(0,widget)
                    getattr(layout,func)(*args)
            else:#其他布局的处理
                getattr(layout,func)(widget)
            
        def create_menubar(head,attrib):  #生成菜单栏
            menubar=QMenuBar()         #生成菜单栏
            if hasattr(head,'setMenuBar'):  #设置菜单栏
                head.setMenuBar(menubar)
            return menubar
        
        def create_menu(head,attrib):  #生成菜单项
            text=attrib.get('text')
            if hasattr(head,'addMenu'):  #添加菜单
                menu=head.addMenu(text)
                add_actions(menu,attrib)   #处理actions 属性
                return menu 

        def add_actions(widget,attrib):  #处理actions 属性
            actions=attrib.get('actions')
            if actions:
                widget.addActions(
                        actiongroups[actions].actions())

        def create_action(head,attrib):# 生成action
            href=attrib.get('href')  #引用action的处理
            if href:
                action=actions.get(href)
            else:
                text=attrib.get('text')
                action=QAction(text,head)
                icon=self.create_icon(owner,attrib.get('icon'))
                action.setIcon(icon)
                triggered=attrib.get('triggered')
                if triggered:
                    do_connect(action,'triggered',triggered)
                name=attrib.get('id')
                if name:
                    actions[name]=action
            if hasattr(head,'addAction'): #添加action
                head.addAction(action)
            return action

        def create_toolbar(head,attrib):  #添加工具栏
            toolbar=QToolBar()
            if hasattr(head,'addToolBar'):
                head.addToolBar(toolbar)
                add_actions(toolbar,attrib)
            return toolbar
        
        def create_actiongroup(head,attrib): #生成actiongroup
            actiongroup=QActionGroup(head)
            name=attrib.get('id').lower()
            if name is not None:
                actiongroups[name]=actiongroup
            return actiongroup
        
        def set_property(widget,k,v): #设置属性
            k=k.lower()
            ps=self.get_propertys(widget)
            if k=='icon':
                v=self.create_icon(v)
            else:
                v=eval(v)
            for p in ['set','add','']:
                s=p+k
                if s in ps:
                    getattr(widget,ps[s])(v)
                    break
                             
        def do_connect(widget,signal,slot): #处理连接
            signal=self.get_attr(signal,[widget,owner])
            slot=self.get_attr(slot,[owner,widget])
            if signal and slot:
                signal.connect(slot)

        def set_variable(widget,var,p):  #设置变量
            s=p.split('.')
            t=None
            if len(s)>1:
                p=s[0]
                t=eval(s[-1])
            owner.var_list[var]=WidgetVar(widget,p,t)

        def proc_child(head,node):  #遍历子类型
            if head is None:return
            for child in node:
                tag=child.tag
                if tag=='a':   #处理锚点
                    href=child.attrib.get('href')
                    child=self.resources.get(href)
                    tag=child.tag
                attrib=child.attrib
                func=functions.get(child.tag) #根据TAG获取处理函数
                if func in [set_variable,set_property,do_connect]:
                    [func(head,k,v)for k,v in attrib.items()]
                else:
                    w=func(head,attrib) if func else create_widget(head,tag,attrib)
                    if w is not None:
                        name=attrib.get('name')
                        if name:
                            setattr(owner,name,w)
                        proc_child(w,child)
               
        def create_statusbar(head,attrib): #处理状态栏
            if hasattr(head,'statusBar'):
                statusbar=head.statusBar()
            if statusbar is None:
                statusbar=QStatusBar()
                if hasattr('setStatusBar'):
                    head.setSatusBar(statusbar)
            return statusbar

        def add_separator(head,attrib):  #处理分割符
            if hasattr(head,'addSeparator'):
                head.addSeparator()

        def add_stretch(head,attrib):  #设置分割栏
            if hasattr(head,'addStretch'):
                head.addStretch()

        functions={
                'menubar':create_menubar,
                'toolbar':create_toolbar,
                'statusbar':create_statusbar,
                'menu':create_menu,
                'actiongroup':create_actiongroup,
                'group':create_actiongroup,
                'separator':add_separator,
                'sep':add_separator,
                'action':create_action,
                'variable':set_variable,
                'var':set_variable,
                'property':set_property,
                'signal':do_connect,                
                'stretch':add_stretch,
                'connect':do_connect,
                }

        node=parser(content=text) if text else self.windows.get(name) #获取窗口资源
        if node:
            widget=create_widget(owner,node.tag,node.attrib) #生成主窗口
            if widget:
                if apps:   #处理apps
                    proc_apps(widget)
                widget.owner=owner
                proc_child(widget,node) # 处理属性及子控件
                owner.widget=widget  #这一句主要是防止系统自动进行垃圾回收
                return widget
Esempio n. 6
0
    def setup_UI(self, owner, name=None, text=None, apps=None):  #创建窗口
        actions = {}  #初始化actions
        actiongroups = {}  #初始化actiongroups

        def proc_apps(head):  #处理apps,主要是处理菜单及工具栏
            for app in apps:
                name = app['class']
                text = app.get('text', '')
                action = QAction(text, head)
                action.setIcon(self.create_icon(owner, app.get('icon')))
                shortcut = app.get('shortcut')
                if shortcut:
                    action.setShortcut(shortcut)
                action.triggered.connect(partial(owner.add_child, name))
                actions[name] = action
                group = app.get('group', None)
                if group:
                    if group not in actiongroups:
                        actiongroups[group] = QActionGroup(head)
                    actiongroups[group].addAction(action)
            owner.app_list = actions.copy()

        def create_widget(head, tag, attrib):  #创建控件或布局
            Widget = self.get_widget(tag)  #获取控件的类型,如失败则返回
            if Widget is None: return
            if issubclass(Widget, QLayout):  #布局的处理
                return create_layout(head, Widget, attrib)
            text = attrib.get('text')
            widget = Widget(text)  #生成控件
            if hasattr(head, 'addPermanentWidget'):  #StatusBar控件处理
                head.addPermanentWidget(widget)
            elif hasattr(head, 'addWidget'):  #摆放控件
                add_layout(head, 'addWidget', widget, attrib)
            elif hasattr(head, 'setCentralWidget'):
                head.setCentralWidget(widget)
            return widget

        def create_layout(head, Layout, attrib):  #生成布局
            layout = Layout()
            if isinstance(head, QLayout):  #摆放布局
                add_layout(head, 'addLayout', layout, attrib)
            elif hasattr(head, 'setLayout'):
                head.setLayout(layout)
            return layout

        def add_layout(layout, func, widget, attrib):  #摆放布局或控件
            if isinstance(layout, QFormLayout):  #表单布局的处理
                label = attrib.get('label')
                args = [widget] if label is None else [label, widget]
                layout.addRow(*args)
            elif isinstance(layout, QGridLayout):  #表格布局的处理
                pos = attrib.get('pos')
                args = [int(x) for x in pos.split(',')]
                if len(args) in (2, 4):
                    args.insert(0, widget)
                    getattr(layout, func)(*args)
            else:  #其他布局的处理
                getattr(layout, func)(widget)

        def create_menubar(head, attrib):  #生成菜单栏
            menubar = QMenuBar()  #生成菜单栏
            if hasattr(head, 'setMenuBar'):  #设置菜单栏
                head.setMenuBar(menubar)
            return menubar

        def create_menu(head, attrib):  #生成菜单项
            text = attrib.get('text')
            if hasattr(head, 'addMenu'):  #添加菜单
                menu = head.addMenu(text)
                add_actions(menu, attrib)  #处理actions 属性
                return menu

        def add_actions(widget, attrib):  #处理actions 属性
            actions = attrib.get('actions')
            if actions:
                widget.addActions(actiongroups[actions].actions())

        def create_action(head, attrib):  # 生成action
            href = attrib.get('href')  #引用action的处理
            if href:
                action = actions.get(href)
            else:
                text = attrib.get('text')
                action = QAction(text, head)
                icon = self.create_icon(owner, attrib.get('icon'))
                action.setIcon(icon)
                triggered = attrib.get('triggered')
                if triggered:
                    do_connect(action, 'triggered', triggered)
                name = attrib.get('id')
                if name:
                    actions[name] = action
            if hasattr(head, 'addAction'):  #添加action
                head.addAction(action)
            return action

        def create_toolbar(head, attrib):  #添加工具栏
            toolbar = QToolBar()
            if hasattr(head, 'addToolBar'):
                head.addToolBar(toolbar)
                add_actions(toolbar, attrib)
            return toolbar

        def create_actiongroup(head, attrib):  #生成actiongroup
            actiongroup = QActionGroup(head)
            name = attrib.get('id').lower()
            if name is not None:
                actiongroups[name] = actiongroup
            return actiongroup

        def set_property(widget, k, v):  #设置属性
            k = k.lower()
            ps = self.get_propertys(widget)
            if k == 'icon':
                v = self.create_icon(v)
            else:
                v = eval(v)
            for p in ['set', 'add', '']:
                s = p + k
                if s in ps:
                    getattr(widget, ps[s])(v)
                    break

        def do_connect(widget, signal, slot):  #处理连接
            signal = self.get_attr(signal, [widget, owner])
            slot = self.get_attr(slot, [owner, widget])
            if signal and slot:
                signal.connect(slot)

        def set_variable(widget, var, p):  #设置变量
            s = p.split('.')
            t = None
            if len(s) > 1:
                p = s[0]
                t = eval(s[-1])
            owner.var_list[var] = WidgetVar(widget, p, t)

        def proc_child(head, node):  #遍历子类型
            if head is None: return
            for child in node:
                tag = child.tag
                if tag == 'a':  #处理锚点
                    href = child.attrib.get('href')
                    child = self.resources.get(href)
                    tag = child.tag
                attrib = child.attrib
                func = functions.get(child.tag)  #根据TAG获取处理函数
                if func in [set_variable, set_property, do_connect]:
                    [func(head, k, v) for k, v in attrib.items()]
                else:
                    w = func(head, attrib) if func else create_widget(
                        head, tag, attrib)
                    if w is not None:
                        name = attrib.get('name')
                        if name:
                            setattr(owner, name, w)
                        proc_child(w, child)

        def create_statusbar(head, attrib):  #处理状态栏
            if hasattr(head, 'statusBar'):
                statusbar = head.statusBar()
            if statusbar is None:
                statusbar = QStatusBar()
                if hasattr('setStatusBar'):
                    head.setSatusBar(statusbar)
            return statusbar

        def add_separator(head, attrib):  #处理分割符
            if hasattr(head, 'addSeparator'):
                head.addSeparator()

        def add_stretch(head, attrib):  #设置分割栏
            if hasattr(head, 'addStretch'):
                head.addStretch()

        functions = {
            'menubar': create_menubar,
            'toolbar': create_toolbar,
            'statusbar': create_statusbar,
            'menu': create_menu,
            'actiongroup': create_actiongroup,
            'group': create_actiongroup,
            'separator': add_separator,
            'sep': add_separator,
            'action': create_action,
            'variable': set_variable,
            'var': set_variable,
            'property': set_property,
            'signal': do_connect,
            'stretch': add_stretch,
            'connect': do_connect,
        }

        node = parser(
            content=text) if text else self.windows.get(name)  #获取窗口资源
        if node:
            widget = create_widget(owner, node.tag, node.attrib)  #生成主窗口
            if widget:
                if apps:  #处理apps
                    proc_apps(widget)
                widget.owner = owner
                proc_child(widget, node)  # 处理属性及子控件
                owner.widget = widget  #这一句主要是防止系统自动进行垃圾回收
                return widget