def addButton(toolbar,text,icon,func,repeat=False,toggle=False,checked=False,icon0=None): """Add a button to a toolbar. toolbar is where the button will be added, text appears as tooltip, icon is the name of the icon to be displayed on the button, func is called when button is pressed, repeat == True: func will repeatedly be called if button is held down. toggle == True: button is a toggle (stays in pressed state). If the button is a toggle, checked is the initial state and icon1 may specify an icon that will be displayed when button is not checked. """ iconset = QtGui.QIcon() icon_on = QtGui.QPixmap(utils.findIcon(icon)) iconset.addPixmap(icon_on,QtGui.QIcon.Normal,QtGui.QIcon.On) if toggle and icon0: icon_off = QtGui.QPixmap(utils.findIcon(icon0)) iconset.addPixmap(icon_off,QtGui.QIcon.Normal,QtGui.QIcon.Off) a = toolbar.addAction(iconset,text,func) b = toolbar.children()[-1] # Get the QToolButton for the last action if repeat: b.setAutoRepeat(True) QtCore.QObject.connect(b,QtCore.SIGNAL("clicked()"),a,QtCore.SLOT("trigger()")) if toggle: b.setCheckable(True) b.connect(b,QtCore.SIGNAL("clicked()"),QtCore.SLOT("toggle()")) b.setChecked(checked) b.setToolTip(text) return b
def addCameraButtons(toolbar): """Add the camera buttons to a toolbar.""" # The buttons have the following fields: # 0 : tooltip # 1 : icon # 2 : function # optional: # 3 : REPEAT (default True) buttons = [ [ "Rotate left", "rotleft", cameraMenu.rotLeft ], [ "Rotate right", "rotright", cameraMenu.rotRight ], [ "Rotate up", "rotup", cameraMenu.rotUp ], [ "Rotate down", "rotdown", cameraMenu.rotDown ], [ "Twist left", "twistleft", cameraMenu.twistLeft ], [ "Twist right", "twistright", cameraMenu.twistRight ], [ "Translate left", "left", cameraMenu.transLeft ], [ "Translate right", "right", cameraMenu.transRight ], [ "Translate down", "down", cameraMenu.transDown ], [ "Translate up", "up", cameraMenu.transUp ], [ "Zoom Out", "zoomout", cameraMenu.zoomOut ], [ "Zoom In", "zoomin", cameraMenu.zoomIn ], [ "Zoom All", "zoomall", draw.zoomAll, False ], ] for but in buttons: icon = QtGui.QIcon(QtGui.QPixmap(utils.findIcon(but[1]))) a = toolbar.addAction(icon,but[0],but[2]) b = toolbar.children()[-1] # Get the QToolButton for the last action if len(but) < 4 or but[3]: b.setAutoRepeat(True) QtCore.QObject.connect(b,QtCore.SIGNAL("clicked()"),a,QtCore.SLOT("trigger()")) if len(but) >= 5: b.setCheckable(but[4]) b.connect(b,QtCore.SIGNAL("clicked()"),QtCore.SLOT("toggle()")) b.setToolTip(but[0])
def insertItems(self,items,before=None): """Insert a list of items in the menu. Each item is a tuple of two to five elements: Text, Action, [ Icon, ShortCut, ToolTip ]. Item text is the text that will be displayed in the menu. It will be stored in a normalized way: all lower case and with '&' removed. Action can be any of the following: - a Python function or instance method : it will be called when the item is selected, - a string with the name of a function/method, - a list of Menu Items: a popup Menu will be created that will appear when the item is selected, - None : this will create a separator item with no action. Icon is the name of one of the icons in the installed icondir. ShortCut is an optional key combination to select the item. Tooltip is a popup help string. If before is given, it specifies the text OR the action of one of the items in the menu: the new items will be inserted before that one. """ if before: before = self.itemAction(before) for item in items: txt,val = item[:2] if val is None: a = self.insert_sep(before) elif isinstance(val, list): a = Menu(txt,self) a.insertItems(val) #self.insert_menu(a,before) else: if type(val) == str: val = eval(val) if len(item) > 2 and item[2].has_key('data'): a = DAction(txt,data=item[2]['data']) self.insert_action(a,before) else: a = self.create_insert_action(txt,val,before) if len(item) > 2: #print 'item = %s' % str(item) for k,v in item[2].items(): if k == 'icon': a.setIcon(QtGui.QIcon(QtGui.QPixmap(utils.findIcon(v)))) elif k == 'shortcut': a.setShortcut(v) elif k == 'tooltip': a.setToolTip(v) elif k == 'checkable': a.setCheckable(v) self.menuitems.append((normalize(txt),a))
def addActionButtons(toolbar): """Add the script action buttons to the toolbar.""" action = {} buttons = [ [ "Play", "next", draw.play, False ], [ "Step", "nextstop", draw.step, False ], [ "Continue", "ff", draw.fforward, False ], [ "Stop", "stop", draw.stopatbreakpt, False ], ] for b in buttons: icon = QtGui.QIcon(QtGui.QPixmap(utils.findIcon(b[1]))) a = toolbar.addAction(icon,b[0],b[2]) a.setEnabled(b[3]) action[b[0]] = a return action
def addButton(toolbar,tooltip,icon,func,repeat=False,toggle=False,checked=False,icon0=None): """Add a button to a toolbar. - `toolbar`: the toolbar where the button will be added - `tooltip`: the text to appears as tooltip - `icon`: name of the icon to be displayed on the button, - `func`: function to be called when the button is pressed, - `repeat`: if True, the `func` will repeatedly be called if button is held down. - `toggle`: if True, the button is a toggle and stays in depressed state until pressed again. - `checked`: initial state for a toggle buton. - `icon1`: for a toggle button, icon to display when button is not checked. """ iconset = QtGui.QIcon() icon_on = QtGui.QPixmap(utils.findIcon(icon)) iconset.addPixmap(icon_on,QtGui.QIcon.Normal,QtGui.QIcon.On) if toggle and icon0: icon_off = QtGui.QPixmap(utils.findIcon(icon0)) iconset.addPixmap(icon_off,QtGui.QIcon.Normal,QtGui.QIcon.Off) a = toolbar.addAction(iconset,tooltip,func) b = toolbar.widgetForAction(a) if repeat: b.setAutoRepeat(True) b.setAutoRepeatDelay(500) b.clicked.connect(a.trigger) if toggle: b.setCheckable(True) b.clicked.connect(b.toggle) b.setChecked(checked) b.setToolTip(tooltip) return b
def addActionButtons(toolbar): """Add the script action buttons to the toolbar.""" import fileMenu action = {} avail_buttons = [ ( "Play", "next", draw.play, False ), ( "ReRun", "rerun", draw.replay, False ), ## ( "Step", "nextstop", draw.step, False ), ( "Continue", "ff", draw.fforward, False ), ( "Stop", "stop", draw.raiseExit, False ), ( "Edit", "pencil", fileMenu.editApp, False ), ( "Info", "info", draw.showDoc, False ), ] # Filter configured buttons show_buttons = pf.cfg['gui/actionbuttons'] buttons = [ b for b in avail_buttons if b[0].lower() in show_buttons ] for b in buttons: icon = QtGui.QIcon(QtGui.QPixmap(utils.findIcon(b[1]))) a = toolbar.addAction(icon,b[0],b[2]) a.setEnabled(b[3]) action[b[0]] = a return action
def addCameraButtons(toolbar): """Add the camera buttons to a toolbar.""" # The buttons have the following fields: # 0 : tooltip # 1 : icon # 2 : function # optional: # 3 : REPEAT (default True) import cameraMenu buttons = [ [ "Rotate left", "rotleft", cameraMenu.rotLeft ], [ "Rotate right", "rotright", cameraMenu.rotRight ], [ "Rotate up", "rotup", cameraMenu.rotUp ], [ "Rotate down", "rotdown", cameraMenu.rotDown ], [ "Twist left", "twistleft", cameraMenu.twistLeft ], [ "Twist right", "twistright", cameraMenu.twistRight ], [ "Translate left", "left", cameraMenu.panLeft ], [ "Translate right", "right", cameraMenu.panRight ], [ "Translate down", "down", cameraMenu.panDown ], [ "Translate up", "up", cameraMenu.panUp ], [ "Zoom Out", "zoomout", cameraMenu.dollyOut ], [ "Zoom In", "zoomin", cameraMenu.dollyIn ], [ "Zoom Rectangle", "zoomrect", cameraMenu.zoomRectangle, False ], [ "Zoom All", "zoomall", cameraMenu.zoomAll, False ], ] for but in buttons: icon = QtGui.QIcon(QtGui.QPixmap(utils.findIcon(but[1]))) a = toolbar.addAction(icon,but[0],but[2]) b = toolbar.children()[-1] # Get the QToolButton for the last action if len(but) < 4 or but[3]: b.setAutoRepeat(True) b.setAutoRepeatDelay(500) b.released.connect(a.trigger) if len(but) >= 5: b.setCheckable(but[4]) b.released.connect(a.toggle) b.setToolTip(but[0])
def insertItems(self, items, before=None, debug=False): """Insert a list of items in the menu. Parameters: - `items`: a list of menuitem tuples. Each item is a tuple of two or three elements: (text, action, options): - `text`: the text that will be displayed in the menu item. It is stored in a normalized way: all lower case and with '&' removed. - `action`: can be any of the following: - a Python function or instance method : it will be called when the item is selected, - a string with the name of a function/method, - a list of Menu Items: a popup Menu will be created that will appear when the item is selected, - an existing Menu, - None : this will create a separator item with no action. - `options`: optional dictionary with following honoured fields: - `icon`: the name of an icon to be displayed with the item text. This name should be that of one of the icons in the pyFormex icondir. - `shortcut`: is an optional key combination to select the item. - `tooltip`: a text that is displayed as popup help. - `before`: if specified, should be the text *or* the action of one of the items in the Menu (not the items list!): the new list of items will be inserted before the specified item. """ if debug: print("Inserting %s items in menu %s" % (len(items), self.title())) before = self.action(before) for item in items: txt, val = item[:2] if debug: print("INSERTING %s: %s" % (txt, val)) if len(item) > 2: options = item[2] else: options = {} if val is None: a = self.insert_sep(before) self.separators[txt] = a elif isinstance(val, list): a = Menu(txt, parent=self, before=before) a.insertItems(val) elif isinstance(val, BaseMenu): #print("INSERTING MENU %s"%txt) self.insert_menu(val, before=before) else: if type(val) == str: val = eval(val) if 'data' in options: # DActions should be saved to keep them alive !!! if debug: print("INSERTING DAction %s" % txt) a = DAction(txt, data=options['data']) a.signal.connect(val) self.insert_action(a, before) # We need to store the DActions, or else they are # destroyed. QActions are stroed by Qt self._actions_.append(a) else: if debug: print("INSERTING QAction %s" % txt) if before is not None: raise RuntimeError, "I can not insert a QAction menu item before an existing one." a = self.create_insert_action(txt, val, before) for k, v in options.items(): if k == 'icon': a.setIcon(QtGui.QIcon(QtGui.QPixmap( utils.findIcon(v)))) elif k == 'shortcut': a.setShortcut(v) elif k == 'tooltip': a.setToolTip(v) elif k == 'checkable': a.setCheckable(v) elif k == 'checked': a.setCheckable(True) a.setChecked(v) elif k == 'disabled': a.setDisabled(True)
def insertItems(self,items,before=None,debug=False): """Insert a list of items in the menu. Parameters: - `items`: a list of menuitem tuples. Each item is a tuple of two or three elements: (text, action, options): - `text`: the text that will be displayed in the menu item. It is stored in a normalized way: all lower case and with '&' removed. - `action`: can be any of the following: - a Python function or instance method : it will be called when the item is selected, - a string with the name of a function/method, - a list of Menu Items: a popup Menu will be created that will appear when the item is selected, - an existing Menu, - None : this will create a separator item with no action. - `options`: optional dictionary with following honoured fields: - `icon`: the name of an icon to be displayed with the item text. This name should be that of one of the icons in the pyFormex icondir. - `shortcut`: is an optional key combination to select the item. - `tooltip`: a text that is displayed as popup help. - `before`: if specified, should be the text *or* the action of one of the items in the Menu (not the items list!): the new list of items will be inserted before the specified item. """ if debug: print("Inserting %s items in menu %s" % (len(items),self.title())) before = self.action(before) for item in items: txt,val = item[:2] if debug: print("INSERTING %s: %s" % (txt,val)) if len(item) > 2: options = item[2] else: options = {} if val is None: a = self.insert_sep(before) self.separators[txt] = a elif isinstance(val, list): a = Menu(txt,parent=self,before=before) a.insertItems(val) elif isinstance(val, BaseMenu): #print("INSERTING MENU %s"%txt) self.insert_menu(val,before=before) else: if type(val) == str: val = eval(val) if 'data' in options: # DActions should be saved to keep them alive !!! if debug: print("INSERTING DAction %s" % txt) a = DAction(txt,data = options['data']) a.signal.connect(val) self.insert_action(a,before) # We need to store the DActions, or else they are # destroyed. QActions are stroed by Qt self._actions_.append(a) else: if debug: print("INSERTING QAction %s" % txt) if before is not None: raise RuntimeError,"I can not insert a QAction menu item before an existing one." a = self.create_insert_action(txt,val,before) for k,v in options.items(): if k == 'icon': a.setIcon(QtGui.QIcon(QtGui.QPixmap(utils.findIcon(v)))) elif k == 'shortcut': a.setShortcut(v) elif k == 'tooltip': a.setToolTip(v) elif k == 'checkable': a.setCheckable(v) elif k == 'checked': a.setCheckable(True) a.setChecked(v) elif k == 'disabled': a.setDisabled(True)
def insertItems(self,items,before=None): """Insert a list of items in the menu. Each item is a tuple of two to five elements: Text, Action, [ Icon, ShortCut, ToolTip ]. Item text is the text that will be displayed in the menu. It will be stored in a normalized way: all lower case and with '&' removed. Action can be any of the following: - a Python function or instance method : it will be called when the item is selected, - a string with the name of a function/method, - a list of Menu Items: a popup Menu will be created that will appear when the item is selected, - an existing Menu, - None : this will create a separator item with no action. Icon is the name of one of the icons in the installed icondir. ShortCut is an optional key combination to select the item. Tooltip is a popup help string. If before is given, it specifies the text OR the action of one of the items in the menu: the new items will be inserted before that one. """ before = self.action(before) for item in items: txt,val = item[:2] if len(item) > 2: options = item[2] else: options = {} if val is None: a = self.insert_sep(before) self.separators[txt] = a elif isinstance(val, list): a = Menu(txt,parent=self,before=before) a.insertItems(val) else: if type(val) == str: val = eval(val) if 'data' in options: # DActions should be saved to keep them alive !!! a = DAction(txt,data = options['data']) QtCore.QObject.connect(a,QtCore.SIGNAL(a.signal),val) self.insert_action(a,before) else: if before is not None: raise RuntimeError,"I can not insert a QAction menu item before an existing one." #print ("CREATE %s,%s,%s" % (txt,val,before)) a = self.create_insert_action(txt,val,before) for k,v in options.items(): if k == 'icon': a.setIcon(QtGui.QIcon(QtGui.QPixmap(utils.findIcon(v)))) elif k == 'shortcut': a.setShortcut(v) elif k == 'tooltip': a.setToolTip(v) elif k == 'checkable': a.setCheckable(v) elif k == 'checked': a.setCheckable(True) a.setChecked(v) elif k == 'disabled': a.setDisabled(True)