class PM_ToolButtonGrid(PM_WidgetGrid): """ The PM_ToolButtonGrid widget provides a grid of tool buttons that function as an I{exclusive button group}. @see: B{PM_ElementChooser} for an example of how this is used. @todo: Fix button size issue (e.g. all buttons are sized 32 x 32). """ buttonList = [] defaultCheckedId = -1 # -1 means no checked Id setAsDefault = True def __init__(self, parentWidget, title='', buttonList=[], alignment=None, label='', labelColumn=0, spanWidth=True, checkedId=-1, setAsDefault=False, isAutoRaise=False, isCheckable=True): """ Appends a PM_ToolButtonGrid widget to the bottom of I{parentWidget}, the Property Manager Group box. @param parentWidget: The parent group box containing this widget. @type parentWidget: PM_GroupBox @param title: The group box title. @type title: str @param buttonList: A list of I{button info lists}. There is one button info list for each button in the grid. The button info list contains the following items: 1. Button Type - in this case its 'ToolButton'(str), 2. Button Id (int), 3. Button text (str), 4. Button icon path (str), 5. Button tool tip (str), 6. Column (int), 7. Row (int). @type buttonList: list @param alignment: The alignment of the toolbutton row in the parent groupbox. Based on its value,spacer items is added to the grid layout of the parent groupbox. @type alignment: str @param label: The label for the toolbutton row. If present, it is added to the same grid layout as the rest of the toolbuttons, in column number E{0}. @type label: str @param labelColumn: The column in the parentWidget's grid layout to which this widget's label will be added. The labelColum can only be E{0} or E{1} @type labelColumn: int @param spanWidth: If True, the widget and its label will span the width of the group box. Its label will appear directly above the widget (unless the label is empty) and is left justified. @type spanWidth: bool (default False) @param checkedId: Checked button id in the button group. Default value is -1 that implies no button is checked. @type checkedId: int @param setAsDefault: If True, sets the I{checkedId} specified by the user as the default checked @type setAsDefault: boolean """ self.buttonGroup = QButtonGroup() self.buttonGroup.setExclusive(True) self.isAutoRaise = isAutoRaise self.isCheckable = isCheckable self.buttonsById = {} self.buttonsByText = {} if setAsDefault: self.setDefaultCheckedId(checkedId) PM_WidgetGrid.__init__(self, parentWidget, title, buttonList, alignment, label, labelColumn, spanWidth) def _createWidgetUsingParameters(self, widgetParams): """ Returns a tool button created using the parameters specified by the user @param widgetParams: A list of label parameters. This is a modified using the original list returned by L{self.getWidgetInfoList}. The modified list doesn't contain the row and column information. @type widgetParams: list @see: L{PM_WidgetGrid._createWidgetUsingParameters} (overrided in this method) @see: L{PM_WidgetGrid.loadWidgets} which calls this method. """ buttonFont = self.getButtonFont() buttonParams = list(widgetParams) button = self._createToolButton(buttonParams) buttonId = buttonParams[1] if self.defaultCheckedId == buttonId: button.setChecked(True) button.setFont(buttonFont) button.setAutoRaise(self.isAutoRaise) button.setCheckable(self.isCheckable) self.buttonGroup.addButton(button, buttonId) self.buttonsById[buttonId] = button self.buttonsByText[str(button.text())] = button return button def getButtonFont(self): """ Returns the font for the tool buttons in the grid. @return: Button font. @rtype: U{B{QFont}<http://doc.trolltech.com/4/qfont.html>} """ # Font for tool buttons. buttonFont = QFont(self.font()) buttonFont.setFamily(BUTTON_FONT) buttonFont.setPointSize(BUTTON_FONT_POINT_SIZE) buttonFont.setBold(BUTTON_FONT_BOLD) return buttonFont def restoreDefault(self): """ Restores the default checkedId. """ if self.setAsDefault: for buttonInfo in self.buttonList: buttonId = buttonInfo[0] if buttonId == self.defaultCheckedId: button = self.getButtonById(buttonId) button.setChecked(True) return def setDefaultCheckedId(self, checkedId): """ Sets the default checked id (button) to I{checkedId}. The current checked button is unchanged. @param checkedId: The new default id for the tool button group. @type checkedId: int """ self.setAsDefault = True self.defaultCheckedId = checkedId def checkedButton(self): """ Returns the tool button group's checked button, or E{0} if no button is checked. @return: Checked tool button or E{0} @rtype: instance of QToolButton or int """ return self.buttonGroup.checkedButton() def checkedId(self): """ Returns the id of the checkedButton(), or -1 if no button is checked. @return: The checked button Id @rtype: int """ return self.buttonGroup.checkedId() def getButtonByText(self, text): """ Returns the button with its current text set to I{text}. @return: The button, or B{None} if no button was found. @rtype: U{B{QToolButton}<http://doc.trolltech.com/4/qtoolbutton.html>} @note: If multiple buttons have the same text, only the last one is returned. """ if self.buttonsByText.has_key(text): return self.buttonsByText[text] else: return None def getButtonById(self, buttonId): """ Returns the button with the button id of I{buttonId}. return: The button, or B{None} if no button was found. rtype: U{B{QToolButton}<http://doc.trolltech.com/4/qtoolbutton.html>} """ if self.buttonsById.has_key(buttonId): return self.buttonsById[buttonId] else: return None def setButtonSize(self, width=32, height=32): """ """ for btn in self.buttonGroup.buttons(): btn.setFixedSize(QSize(width, height))
class PM_RadioButtonList(PM_GroupBox): """ The PM_RadioButtonList widget provides a list of radio buttons that function as an I{exclusive button group}. """ buttonList = [] defaultCheckedId = -1 # -1 means no checked Id setAsDefault = True labelWidget = None def __init__(self, parentWidget, title='', label='', labelColumn=0, buttonList=[], checkedId=-1, setAsDefault=False, spanWidth=True, borders=True): """ Appends a PM_RadioButtonList widget to the bottom of I{parentWidget}, the Property Manager dialog or group box. @param parentWidget: The parent group box containing this widget. @type parentWidget: PM_GroupBox or PM_Dialog @param title: The group box title. @type title: str @param label: The label for the coordinate spinbox. @type label: str @param labelColumn: The column in the parentWidget's grid layout to which this widget's label will be added. The labelColum can only be E{0} or E{1} @type labelColumn: int @param buttonList: A list of I{button info lists}. There is one button info list for each radio button in the list. The button info list contains the following three items: 1). Button Id (int), 2). Button text (str), 3). Button tool tip (str). @type buttonList: list @param spanWidth: If True, the widget and its label will span the width of the group box. Its label will appear directly above the widget (unless the label is empty) and is left justified. @type spanWidth: bool (default False) @param borders: If true (default), this widget will have borders displayed. otherwise the won't be any outside borders around the set of radio buttons this class provides @type borders: boolean """ # Intializing label, labelColumn etc is needed before doing # PM_GroupBox.__init__. This is done so that # self.parentWidget.addPmWidget(self) done at the end of __init__ # works properly. # 'self.parentWidget.addPmWidget(self)' is done to avoid a bug where a # groupbox is always appended as the 'last widget' when its # parentWidget is also a groupbox. This is due to other PM widgets #(e.g. PM_PushButton)add themselves to their parent widget in their #__init__ using self.parentWidget.addPmWidget(self). So doing the #same thing here. More general fix is needed in PM_GroupBox code # --Ninad 2007-11-14 (comment copied from PM_coordinateSpinBoxes) self.label = label self.labelColumn = labelColumn self.spanWidth = spanWidth if label: # Create this widget's QLabel. self.labelWidget = QLabel() self.labelWidget.setText(label) PM_GroupBox.__init__(self, parentWidget, title) # These are needed to properly maintain the height of the grid if # all buttons in a row are hidden via hide(). self.vBoxLayout.setMargin(0) self.vBoxLayout.setSpacing(0) self.buttonGroup = QButtonGroup() self.buttonGroup.setExclusive(True) self.parentWidget = parentWidget self.buttonList = buttonList if setAsDefault: self.setDefaultCheckedId(checkedId) self.buttonsById = {} self.buttonsByText = {} # Create radio button list from button info. for buttonInfo in buttonList: buttonId = buttonInfo[0] buttonText = buttonInfo[1] buttonToolTip = buttonInfo[2] button = QRadioButton(self) button.setText(buttonText) button.setToolTip(buttonToolTip) # Not working. button.setCheckable(True) if checkedId == buttonId: button.setChecked(True) self.buttonGroup.addButton(button, buttonId) self.vBoxLayout.addWidget(button) self.buttonsById[buttonId] = button self.buttonsByText[buttonText] = button if isinstance(self.parentWidget, PM_GroupBox): self.parentWidget.addPmWidget(self) else: #@@ Should self be added to self.parentWidget's widgetList? #don't know. Retaining old code -- Ninad 2008-06-23 self._widgetList.append(self) self._rowCount += 1 if not borders: #reset the style sheet so that there are no borders around the #radio button set this class provides. self.setStyleSheet(self._getAlternateStyleSheet()) def restoreDefault(self): """ Restores the default checkedId. """ if self.setAsDefault: for buttonInfo in self.buttonList: buttonId = buttonInfo[0] if buttonId == self.defaultCheckedId: button = self.getButtonById(buttonId) button.setChecked(True) return def setDefaultCheckedId(self, checkedId): """ Sets the default checked id (button) to I{checkedId}. The current checked button is unchanged. @param checkedId: The new default id for the tool button group. @type checkedId: int """ self.setAsDefault = True self.defaultCheckedId = checkedId def checkedButton(self): """ Returns the tool button group's checked button, or 0 if no buttons are checked. """ return self.buttonGroup.checkedButton() def checkedId(self): """ Returns the id of the checkedButton(), or -1 if no button is checked. """ return self.buttonGroup.checkedId() def getButtonByText(self, text): """ Returns the button with its current text set to I{text}. """ if self.buttonsByText.has_key(text): return self.buttonsByText[text] else: return None def getButtonById(self, buttonId): """ Returns the button with the button id of I{buttonId}. """ if self.buttonsById.has_key(buttonId): return self.buttonsById[buttonId] else: return None def _getAlternateStyleSheet(self): """ Return the style sheet for the groupbox. This sets the following properties only: - border style - border width - border color - border radius (on corners) @see: L{PM_GroupBox._getStyleSheet} (overrided here) """ styleSheet = "QGroupBox {border-style:hidden;\ border-width: 0px;\ border-color: " ";\ border-radius: 0px;\ min-width: 10em; }" return styleSheet # End of PM_RadioButtonList ############################
class PM_ToolButtonGrid( PM_WidgetGrid ): """ The PM_ToolButtonGrid widget provides a grid of tool buttons that function as an I{exclusive button group}. @see: B{PM_ElementChooser} for an example of how this is used. @todo: Fix button size issue (e.g. all buttons are sized 32 x 32). """ buttonList = [] defaultCheckedId = -1 # -1 means no checked Id setAsDefault = True def __init__(self, parentWidget, title = '', buttonList = [], alignment = None, label = '', labelColumn = 0, spanWidth = True, checkedId = -1, setAsDefault = False, isAutoRaise = False, isCheckable = True ): """ Appends a PM_ToolButtonGrid widget to the bottom of I{parentWidget}, the Property Manager Group box. @param parentWidget: The parent group box containing this widget. @type parentWidget: PM_GroupBox @param title: The group box title. @type title: str @param buttonList: A list of I{button info lists}. There is one button info list for each button in the grid. The button info list contains the following items: 1. Button Type - in this case its 'ToolButton'(str), 2. Button Id (int), 3. Button text (str), 4. Button icon path (str), 5. Button tool tip (str), 6. Column (int), 7. Row (int). @type buttonList: list @param alignment: The alignment of the toolbutton row in the parent groupbox. Based on its value,spacer items is added to the grid layout of the parent groupbox. @type alignment: str @param label: The label for the toolbutton row. If present, it is added to the same grid layout as the rest of the toolbuttons, in column number E{0}. @type label: str @param labelColumn: The column in the parentWidget's grid layout to which this widget's label will be added. The labelColum can only be E{0} or E{1} @type labelColumn: int @param spanWidth: If True, the widget and its label will span the width of the group box. Its label will appear directly above the widget (unless the label is empty) and is left justified. @type spanWidth: bool (default False) @param checkedId: Checked button id in the button group. Default value is -1 that implies no button is checked. @type checkedId: int @param setAsDefault: If True, sets the I{checkedId} specified by the user as the default checked @type setAsDefault: boolean """ self.buttonGroup = QButtonGroup() self.buttonGroup.setExclusive(True) self.isAutoRaise = isAutoRaise self.isCheckable = isCheckable self.buttonsById = {} self.buttonsByText = {} if setAsDefault: self.setDefaultCheckedId(checkedId) PM_WidgetGrid.__init__(self, parentWidget , title, buttonList, alignment, label, labelColumn, spanWidth ) def _createWidgetUsingParameters(self, widgetParams): """ Returns a tool button created using the parameters specified by the user @param widgetParams: A list of label parameters. This is a modified using the original list returned by L{self.getWidgetInfoList}. The modified list doesn't contain the row and column information. @type widgetParams: list @see: L{PM_WidgetGrid._createWidgetUsingParameters} (overrided in this method) @see: L{PM_WidgetGrid.loadWidgets} which calls this method. """ buttonFont = self.getButtonFont() buttonParams = list(widgetParams) button = self._createToolButton(buttonParams) buttonId = buttonParams[1] if self.defaultCheckedId == buttonId: button.setChecked(True) button.setFont(buttonFont) button.setAutoRaise(self.isAutoRaise) button.setCheckable(self.isCheckable) self.buttonGroup.addButton(button, buttonId) self.buttonsById[buttonId] = button self.buttonsByText[str(button.text())] = button return button def getButtonFont(self): """ Returns the font for the tool buttons in the grid. @return: Button font. @rtype: U{B{QFont}<http://doc.trolltech.com/4/qfont.html>} """ # Font for tool buttons. buttonFont = QFont(self.font()) buttonFont.setFamily(BUTTON_FONT) buttonFont.setPointSize(BUTTON_FONT_POINT_SIZE) buttonFont.setBold(BUTTON_FONT_BOLD) return buttonFont def restoreDefault(self): """ Restores the default checkedId. """ if self.setAsDefault: for buttonInfo in self.buttonList: buttonId = buttonInfo[0] if buttonId == self.defaultCheckedId: button = self.getButtonById(buttonId) button.setChecked(True) return def setDefaultCheckedId(self, checkedId): """ Sets the default checked id (button) to I{checkedId}. The current checked button is unchanged. @param checkedId: The new default id for the tool button group. @type checkedId: int """ self.setAsDefault = True self.defaultCheckedId = checkedId def checkedButton(self): """ Returns the tool button group's checked button, or E{0} if no button is checked. @return: Checked tool button or E{0} @rtype: instance of QToolButton or int """ return self.buttonGroup.checkedButton() def checkedId(self): """ Returns the id of the checkedButton(), or -1 if no button is checked. @return: The checked button Id @rtype: int """ return self.buttonGroup.checkedId() def getButtonByText(self, text): """ Returns the button with its current text set to I{text}. @return: The button, or B{None} if no button was found. @rtype: U{B{QToolButton}<http://doc.trolltech.com/4/qtoolbutton.html>} @note: If multiple buttons have the same text, only the last one is returned. """ if self.buttonsByText.has_key(text): return self.buttonsByText[text] else: return None def getButtonById(self, buttonId): """ Returns the button with the button id of I{buttonId}. return: The button, or B{None} if no button was found. rtype: U{B{QToolButton}<http://doc.trolltech.com/4/qtoolbutton.html>} """ if self.buttonsById.has_key(buttonId): return self.buttonsById[buttonId] else: return None def setButtonSize(self, width = 32, height = 32): """ """ for btn in self.buttonGroup.buttons(): btn.setFixedSize(QSize(width, height))
class PM_RadioButtonList( PM_GroupBox ): """ The PM_RadioButtonList widget provides a list of radio buttons that function as an I{exclusive button group}. """ buttonList = [] defaultCheckedId = -1 # -1 means no checked Id setAsDefault = True labelWidget = None def __init__(self, parentWidget, title = '', label = '', labelColumn = 0, buttonList = [], checkedId = -1, setAsDefault = False, spanWidth = True, borders = True ): """ Appends a PM_RadioButtonList widget to the bottom of I{parentWidget}, the Property Manager dialog or group box. @param parentWidget: The parent group box containing this widget. @type parentWidget: PM_GroupBox or PM_Dialog @param title: The group box title. @type title: str @param label: The label for the coordinate spinbox. @type label: str @param labelColumn: The column in the parentWidget's grid layout to which this widget's label will be added. The labelColum can only be E{0} or E{1} @type labelColumn: int @param buttonList: A list of I{button info lists}. There is one button info list for each radio button in the list. The button info list contains the following three items: 1). Button Id (int), 2). Button text (str), 3). Button tool tip (str). @type buttonList: list @param spanWidth: If True, the widget and its label will span the width of the group box. Its label will appear directly above the widget (unless the label is empty) and is left justified. @type spanWidth: bool (default False) @param borders: If true (default), this widget will have borders displayed. otherwise the won't be any outside borders around the set of radio buttons this class provides @type borders: boolean """ # Intializing label, labelColumn etc is needed before doing # PM_GroupBox.__init__. This is done so that # self.parentWidget.addPmWidget(self) done at the end of __init__ # works properly. # 'self.parentWidget.addPmWidget(self)' is done to avoid a bug where a # groupbox is always appended as the 'last widget' when its # parentWidget is also a groupbox. This is due to other PM widgets #(e.g. PM_PushButton)add themselves to their parent widget in their #__init__ using self.parentWidget.addPmWidget(self). So doing the #same thing here. More general fix is needed in PM_GroupBox code # --Ninad 2007-11-14 (comment copied from PM_coordinateSpinBoxes) self.label = label self.labelColumn = labelColumn self.spanWidth = spanWidth if label: # Create this widget's QLabel. self.labelWidget = QLabel() self.labelWidget.setText(label) PM_GroupBox.__init__(self, parentWidget, title) # These are needed to properly maintain the height of the grid if # all buttons in a row are hidden via hide(). self.vBoxLayout.setMargin(0) self.vBoxLayout.setSpacing(0) self.buttonGroup = QButtonGroup() self.buttonGroup.setExclusive(True) self.parentWidget = parentWidget self.buttonList = buttonList if setAsDefault: self.setDefaultCheckedId(checkedId) self.buttonsById = {} self.buttonsByText = {} # Create radio button list from button info. for buttonInfo in buttonList: buttonId = buttonInfo[0] buttonText = buttonInfo[1] buttonToolTip = buttonInfo[2] button = QRadioButton(self) button.setText(buttonText) button.setToolTip(buttonToolTip) # Not working. button.setCheckable(True) if checkedId == buttonId: button.setChecked(True) self.buttonGroup.addButton(button, buttonId) self.vBoxLayout.addWidget(button) self.buttonsById[buttonId] = button self.buttonsByText[buttonText] = button if isinstance(self.parentWidget, PM_GroupBox): self.parentWidget.addPmWidget(self) else: #@@ Should self be added to self.parentWidget's widgetList? #don't know. Retaining old code -- Ninad 2008-06-23 self._widgetList.append(self) self._rowCount += 1 if not borders: #reset the style sheet so that there are no borders around the #radio button set this class provides. self.setStyleSheet(self._getAlternateStyleSheet()) def restoreDefault(self): """ Restores the default checkedId. """ if self.setAsDefault: for buttonInfo in self.buttonList: buttonId = buttonInfo[0] if buttonId == self.defaultCheckedId: button = self.getButtonById(buttonId) button.setChecked(True) return def setDefaultCheckedId(self, checkedId): """ Sets the default checked id (button) to I{checkedId}. The current checked button is unchanged. @param checkedId: The new default id for the tool button group. @type checkedId: int """ self.setAsDefault = True self.defaultCheckedId = checkedId def checkedButton(self): """ Returns the tool button group's checked button, or 0 if no buttons are checked. """ return self.buttonGroup.checkedButton() def checkedId(self): """ Returns the id of the checkedButton(), or -1 if no button is checked. """ return self.buttonGroup.checkedId() def getButtonByText(self, text): """ Returns the button with its current text set to I{text}. """ if self.buttonsByText.has_key(text): return self.buttonsByText[text] else: return None def getButtonById(self, buttonId): """ Returns the button with the button id of I{buttonId}. """ if self.buttonsById.has_key(buttonId): return self.buttonsById[buttonId] else: return None def _getAlternateStyleSheet(self): """ Return the style sheet for the groupbox. This sets the following properties only: - border style - border width - border color - border radius (on corners) @see: L{PM_GroupBox._getStyleSheet} (overrided here) """ styleSheet = "QGroupBox {border-style:hidden;\ border-width: 0px;\ border-color: "";\ border-radius: 0px;\ min-width: 10em; }" return styleSheet # End of PM_RadioButtonList ############################