def setState(self, state, note=None): if state == self.state and note == self.note: return self.state = state self.note = note palette = self.palette() palette.setColor(QPalette.Button, QColor(state.color)) self.setPalette(palette) if note and not state.internal: menu = self.menu() actions = menu.actions()[5:] try: action = next(action for action in actions if action.state is state and action.note == note) except StopIteration: action = QAction(QIcon(state.icon), note, menu) if len(actions) == 0: menu.addAction(action) else: if len(actions) >= self.history_size: menu.removeAction(actions[-1]) menu.insertAction(actions[0], action) action.state = state action.note = note else: if action is not actions[0]: menu.removeAction(action) menu.insertAction(actions[0], action) self.stateChanged.emit()