void MButtonGroupPrivate::buttonToggle(bool checked)
{
    Q_Q(MButtonGroup);

    MButton *button = static_cast<MButton *>(this->sender());
    if (!button) {
        mWarning("MButtonGroup") << "buttonToggle(): slot unexpectedly called by someone else than MButton";
        return;
    }

    if (!button->isCheckable()) {
        mWarning("MButtonGroup") << "buttonToggle(): Cannot check non-checkable button.";
        return;
    }

    if (checked && q->exclusive() && checkedButton) {
        exclusive = false;
        checkedButton->setChecked(false);
        exclusive = true;
    }

    if (checked) {
        checkedButton = button;
    } else if (checkedButton == button) {
        /* If a button is unchecked and its same as checkedButton, then
           checkedButton should be reset as well */
        checkedButton = 0;
    }
}
void MButtonGroupPrivate::enforceExclusivity()
{
    if (!exclusive)
        return;

    /* In case of exclusive mode:
    case 1: If there is no button checked, find the first checkable
    button check it.
    case 2: If there is more than one button checked,
    uncheck all buttons except the first checked one
    */
    MButton *checkableButton = 0;
    MButton *newCheckedButton = 0;
    QList<MButton *>::iterator i = buttonList.begin();
    while (i != buttonList.end()) {
        MButton *b = *i;
        /* Get the first checkable button */
        if (b->isCheckable() && checkableButton == 0) {
            checkableButton = b;
        }
        /* Set the first checked button and uncheck the rest */
        if (b->isChecked()) {
            if (newCheckedButton == 0) {
                newCheckedButton = b;
                checkedButton = b;
            } else {
                b->setChecked(false);
            }
        }
        ++i;
    }
    if (checkedButton == 0 && checkableButton != 0) {
        /* Make sure that exactly one button is checked */
        checkableButton->setChecked(true);
        checkedButton = checkableButton;
    }
}