/**
 *  Returns true if all relevant children of the widget managed by this
 *  instance are valid AND if #isOtherValid() returns true; otherwise returns
 *  false. Disabled children and children without validation
 *  are skipped and don't affect the result.
 *
 *  The method emits the #isValidRequested() signal before calling
 *  #isOtherValid(), thus giving someone an opportunity to affect its result by
 *  calling #setOtherValid() from the signal handler. Note that #isOtherValid()
 *  returns true by default, until #setOtherValid( false ) is called.
 *
 *  @note If #isOtherValid() returns true this method does a hierarchy scan, so
 *  it's a good idea to store the returned value in a local variable if needed
 *  more than once within a context of a single check.
 */
bool QIWidgetValidator::isValid() const
{
    // wgt is null, we assume we're valid
    if (!mWidget)
        return true;

    QIWidgetValidator *that = const_cast <QIWidgetValidator *> (this);
    emit that->isValidRequested (that);
    if (!isOtherValid())
        return false;

    QValidator::State state = QValidator::Acceptable;

    foreach (Watched watched, mWatched)
    {
        if (watched.widget->inherits ("QLineEdit"))
        {
            QLineEdit *le = ((QLineEdit *) watched.widget);
            Assert (le->validator());
            if (!le->validator() || !le->isEnabled())
                continue;
            QString text = le->text();
            int pos;
            state = le->validator()->validate (text, pos);
        }
        else if (watched.widget->inherits ("QComboBox"))
        {
            QComboBox *cb = ((QComboBox *) watched.widget);
            Assert (cb->validator());
            if (!cb->validator() || !cb->isEnabled())
                continue;
            QString text = cb->lineEdit()->text();
            int pos;
            state = cb->lineEdit()->validator()->validate (text, pos);
        }

        if (state != QValidator::Acceptable)
        {
            that->mLastInvalid = watched;
            that->mLastInvalid.state = state;
            return false;
        }
    }

    /* reset last invalid */
    that->mLastInvalid = Watched();
    return true;
}
void EditCallDialog::checkValidity(void)
{
    QValidator::State state = QValidator::Acceptable;
    
    int i;
    for (i=0; i<m_qObjects.size(); i++) {
        const FunctionCall::Argument *arg = m_pChangedCall->getArgument(i);
        switch (arg->iType) {
            case DBG_TYPE_ENUM:
                {
                    QComboBox *cb = (QComboBox*)m_qObjects[i];
                    const QValidator *vldr = cb->validator();
                    QString s = cb->currentText();
                    int pos = 0;
                    if (vldr->validate(s, pos) != QValidator::Acceptable) {
                        state = QValidator::Invalid;
                    }
                    break;
                }
            default:
                break;
        }
    }

    /* Find OK button */
    QAbstractButton *okButton = NULL;
    QList<QAbstractButton*> buttonList;
    buttonList = buttonBox->buttons();
    for(i=0; i<buttonList.size(); i++) {
        if (buttonBox->buttonRole(buttonList[i]) == 
                QDialogButtonBox::AcceptRole) {
            okButton = buttonList[i];
        }
    }

    if (!okButton) {
        return;
    }

    if (state == QValidator::Acceptable) {
        okButton->setEnabled(true);
    } else {
        okButton->setEnabled(false);
    }
}