Пример #1
0
    def rebuild(self):
        """
        Rebuilds the user interface buttons for this widget.
        """
        self.setUpdatesEnabled(False)

        # sync up the toolbuttons with our actions
        actions = self._actionGroup.actions()
        btns = self.findChildren(QToolButton)
        horiz = self.direction() in (QBoxLayout.LeftToRight,
                                     QBoxLayout.RightToLeft)

        # remove unnecessary buttons
        if len(actions) < len(btns):
            rem_btns = btns[len(actions) - 1:]
            btns = btns[:len(actions)]

            for btn in rem_btns:
                btn.close()
                btn.setParent(None)
                btn.deleteLater()

        # create new buttons
        elif len(btns) < len(actions):
            for i in range(len(btns), len(actions)):
                btn = QToolButton(self)
                btn.setAutoFillBackground(True)
                btns.append(btn)
                self.layout().addWidget(btn)

                btn.clicked.connect(self.emitClicked)

        # determine coloring options
        palette = self.palette()
        checked = palette.color(palette.Highlight)
        checked_fg = palette.color(palette.HighlightedText)
        unchecked = palette.color(palette.Button)
        unchecked_fg = palette.color(palette.ButtonText)
        border = palette.color(palette.Mid)

        # define the stylesheet options
        options = {}
        options['top_left_radius'] = 0
        options['top_right_radius'] = 0
        options['bot_left_radius'] = 0
        options['bot_right_radius'] = 0
        options['border_color'] = border.name()
        options['checked_fg'] = checked_fg.name()
        options['checked_bg'] = checked.name()
        options['checked_bg_alt'] = checked.darker(120).name()
        options['unchecked_fg'] = unchecked_fg.name()
        options['unchecked_bg'] = unchecked.name()
        options['unchecked_bg_alt'] = unchecked.darker(120).name()
        options['padding_top'] = 1
        options['padding_bottom'] = 1
        options['padding_left'] = 1
        options['padding_right'] = 1

        if horiz:
            options['x1'] = 0
            options['y1'] = 0
            options['x2'] = 0
            options['y2'] = 1
        else:
            options['x1'] = 0
            options['y1'] = 0
            options['x2'] = 1
            options['y2'] = 1

        # sync up the actions and buttons
        count = len(actions)
        palette = self.palette()
        font = self.font()
        for i, action in enumerate(actions):
            btn = btns[i]

            # assign the action for this button
            if btn.defaultAction() != action:
                # clear out any existing actions
                for act in btn.actions():
                    btn.removeAction(act)

                # assign the given action
                btn.setDefaultAction(action)

            options['top_left_radius'] = 1
            options['bot_left_radius'] = 1
            options['top_right_radius'] = 1
            options['bot_right_radius'] = 1

            if horiz:
                options['padding_left'] = self._padding
                options['padding_right'] = self._padding
            else:
                options['padding_top'] = self._padding
                options['padding_bottom'] = self._padding

            if not i:
                if horiz:
                    options['top_left_radius'] = self.cornerRadius()
                    options['bot_left_radius'] = self.cornerRadius()
                    options['padding_left'] += self.cornerRadius() / 3.0
                else:
                    options['top_left_radius'] = self.cornerRadius()
                    options['top_right_radius'] = self.cornerRadius()
                    options['padding_top'] += self.cornerRadius() / 3.0

            if i == count - 1:
                if horiz:
                    options['top_right_radius'] = self.cornerRadius()
                    options['bot_right_radius'] = self.cornerRadius()
                    options['padding_right'] += self.cornerRadius() / 3.0
                else:
                    options['bot_left_radius'] = self.cornerRadius()
                    options['bot_right_radius'] = self.cornerRadius()
                    options['padding_bottom'] += self.cornerRadius() / 3.0

            btn.setFont(font)
            btn.setPalette(palette)
            btn.setStyleSheet(TOOLBUTTON_STYLE % options)
            if horiz:
                btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
            else:
                btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)

        self.setUpdatesEnabled(True)
Пример #2
0
 def rebuild( self ):
     """
     Rebuilds the user interface buttons for this widget.
     """
     self.setUpdatesEnabled(False)
     
     # sync up the toolbuttons with our actions
     actions = self._actionGroup.actions()
     btns    = self.findChildren(QToolButton)
     horiz   = self.direction() in (QBoxLayout.LeftToRight, 
                                    QBoxLayout.RightToLeft)
     
     # remove unnecessary buttons
     if len(actions) < len(btns):
         rem_btns = btns[len(actions)-1:]
         btns = btns[:len(actions)]
         
         for btn in rem_btns:
             btn.close()
             btn.setParent(None)
             btn.deleteLater()
     
     # create new buttons
     elif len(btns) < len(actions):
         for i in range(len(btns), len(actions)):
             btn = QToolButton(self)
             btn.setAutoFillBackground(True)
             btns.append(btn)
             self.layout().addWidget(btn)
             
             btn.clicked.connect(self.emitClicked)
     
     # determine coloring options
     palette      = self.palette()
     checked      = palette.color(palette.Highlight)
     checked_fg   = palette.color(palette.HighlightedText)
     unchecked    = palette.color(palette.Button)
     unchecked_fg = palette.color(palette.ButtonText)
     border       = palette.color(palette.Mid)
     
     # define the stylesheet options
     options = {}
     options['top_left_radius']      = 0
     options['top_right_radius']     = 0
     options['bot_left_radius']      = 0
     options['bot_right_radius']     = 0
     options['border_color']         = border.name()
     options['checked_fg']           = checked_fg.name()
     options['checked_bg']           = checked.name()
     options['checked_bg_alt']       = checked.darker(120).name()
     options['unchecked_fg']         = unchecked_fg.name()
     options['unchecked_bg']         = unchecked.name()
     options['unchecked_bg_alt']     = unchecked.darker(120).name()
     options['padding_top']          = 1
     options['padding_bottom']       = 1
     options['padding_left']         = 1
     options['padding_right']        = 1
     
     if horiz:
         options['x1'] = 0
         options['y1'] = 0
         options['x2'] = 0
         options['y2'] = 1
     else:
         options['x1'] = 0
         options['y1'] = 0
         options['x2'] = 1
         options['y2'] = 1
     
     # sync up the actions and buttons
     count = len(actions)
     palette = self.palette()
     font = self.font()
     for i, action in enumerate(actions):
         btn = btns[i]
         
         # assign the action for this button
         if btn.defaultAction() != action:
             # clear out any existing actions
             for act in btn.actions():
                 btn.removeAction(act)
             
             # assign the given action
             btn.setDefaultAction(action)
         
         options['top_left_radius']  = 1
         options['bot_left_radius']  = 1
         options['top_right_radius'] = 1
         options['bot_right_radius'] = 1
         
         if horiz:
             options['padding_left']     = self._padding
             options['padding_right']    = self._padding
         else:
             options['padding_top']      = self._padding
             options['padding_bottom']   = self._padding
         
         if not i:
             if horiz:
                 options['top_left_radius'] = self.cornerRadius()
                 options['bot_left_radius'] = self.cornerRadius()
                 options['padding_left']    += self.cornerRadius() / 3.0
             else:
                 options['top_left_radius'] = self.cornerRadius()
                 options['top_right_radius'] = self.cornerRadius()
                 options['padding_top']     += self.cornerRadius() / 3.0
                 
         if i == count - 1:
             if horiz:
                 options['top_right_radius'] = self.cornerRadius()
                 options['bot_right_radius'] = self.cornerRadius()
                 options['padding_right']    += self.cornerRadius() / 3.0
             else:
                 options['bot_left_radius']  = self.cornerRadius()
                 options['bot_right_radius'] = self.cornerRadius()
                 options['padding_bottom']   += self.cornerRadius() / 3.0
         
         btn.setFont(font)
         btn.setPalette(palette)
         btn.setStyleSheet(TOOLBUTTON_STYLE % options)
         if horiz:
             btn.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Preferred)
         else:
             btn.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Expanding)
     
     self.setUpdatesEnabled(True)