Ejemplo n.º 1
0
void SettingsDelegate::setModelData(QWidget* editor,
                                    QAbstractItemModel* model,
                                    const QModelIndex& index) const
{
    // Get the setting type.
    int type = index.model()->data(index, SettingsModel::TypeRole).toInt();

    // Get the editor data.
    QVariant value;
    switch (type)
    {
        case SettingsValue::INT:
        case SettingsValue::UNSIGNED_INT:
        case SettingsValue::INT_POSITIVE:
        {
            // Spin box editors.
            value = static_cast<QSpinBox*>(editor)->value();
            break;
        }
        case SettingsValue::DOUBLE:
        case SettingsValue::UNSIGNED_DOUBLE:
        case SettingsValue::DOUBLE_RANGE:
        {
            // Double spin box editors.
            value = static_cast<DoubleSpinBox*>(editor)->cleanText();
            break;
        }
        case SettingsValue::DOUBLE_RANGE_EXT:
        {
            // Double spin box editors.
            DoubleSpinBox* e = static_cast<DoubleSpinBox*>(editor);
            double v = e->value();
            value = e->value();
            if (v <= e->rangeMin())
                value = e->minText();
            else
                value = e->cleanText();
            break;
        }
        case SettingsValue::DATE_TIME:
        {
            // Date and time editors.
            value = static_cast<QLineEdit*>(editor)->text();
            // Validate these strings...
            //      d-M-yyyy h:m:s.z
            //      yyyy/M/d/h:m:s.z
            //      yyyy-M-d h:m:s.z
            //      yyyy-M-dTh:m:s.z
            //      MJD.fraction
            QString h = "([01]?[0-9]|2[0-3])";       // 00-23
            QString m = "[0-5]?[0-9]";               // 00-59
            QString s = m;
            QString z = "(\\.\\d{1,3})?";            // 000-999
            QString d = "(0?[1-9]|[12][0-9]|3[01])"; // 01-31
            QString M = "(|1[0-2]|0?[1-9]|)";        // 01-12
            QString y = "\\d{4,4}";                  // yyyy
            QString rtime  = h+":"+m+":"+s+z;        // h:m:s.zzz
            QString rdate1 = "("+d+"-"+M+"-"+y+"\\s"+rtime+")";
            QString rdate2 = "("+y+"/"+M+"/"+d+"/"+rtime+")";
            QString rdate3 = "("+y+"-"+M+"-"+d+"\\s"+rtime+")";
            QString rdate4 = "("+y+"-"+M+"-"+d+"T"+rtime+")";
            QString rdate5 = "(\\d+\\.?\\d*)";
            QString rdate6 = "(\\d+\\.?\\d*[e|E]-?\\d{1,2})";
            QString rdatetime = rdate1+"|"+rdate2+"|"+rdate3+"|"+rdate4+
                            "|"+rdate5+"|"+rdate6;
            QRegExpValidator validator(QRegExp(rdatetime), 0);
            int pos = 0;
            QString v = value.toString();
            if (validator.validate(v, pos) != QValidator::Acceptable &&
                            !v.isEmpty()) {
                cerr << "WARNING: DateTime value failed to validate." << endl;
                return;
            }
            break;
        }
        case SettingsValue::TIME:
        {
            // Time editors.
            value = static_cast<QLineEdit*>(editor)->text();
            QString h = "(\\d+)";                    // >= 0
            QString m = "[0-5]?[0-9]";               // 00-59
            QString s = m;
            QString z = "(\\.\\d{1,8})?";            // 000-99999999
            QString rtime  = h+":"+m+":"+s+z;        // h:m:s.zzz
            QString sec    = "(\\d+\\.?\\d*)";
            QString exp_sec = "(\\d+\\.?\\d*[e|E]-?\\d{1,2})";
            QRegExpValidator validator(QRegExp(rtime+"|"+sec+"|"+exp_sec), 0);
            int pos = 0;
            QString v = value.toString();
            if (validator.validate(v, pos) != QValidator::Acceptable &&
                            !v.isEmpty()) {
                cerr << "WARNING: Time value failed to validate." << endl;
                return;
            }
            break;
        }
        case SettingsValue::RANDOM_SEED:
        {
            // Random seed editors.
            int val = static_cast<QSpinBox*>(editor)->value();
            if (val < 1)
                value = static_cast<QSpinBox*>(editor)->specialValueText();
            else
                value = val;
            break;
        }
        case SettingsValue::INT_RANGE_EXT:
        {
            // Random seed editors.
            int val = static_cast<QSpinBox*>(editor)->value();
            if (val < 0)
                value = static_cast<QSpinBox*>(editor)->specialValueText();
            else
                value = val;
            break;
        }
        case SettingsValue::OPTION_LIST:
        {
            // Options list.
            value = static_cast<QComboBox*>(editor)->currentText();
            break;
        }
        default:
        {
            value = static_cast<QLineEdit*>(editor)->text();
            break;
        }
    }

    model->setData(index, value, Qt::EditRole);
}
Ejemplo n.º 2
0
void SettingsDelegate::setEditorData(QWidget* editor,
                                     const QModelIndex& index) const
{
    // Get the setting type.
    int type = index.model()->data(index, SettingsModel::TypeRole).toInt();

    // Set the editor data.
    QVariant value = index.model()->data(index, Qt::EditRole);
    switch (type)
    {
        case SettingsValue::INT:
        case SettingsValue::UNSIGNED_INT:
        case SettingsValue::INT_POSITIVE:
        {
            // Spin box editors.
            static_cast<QSpinBox*>(editor)->setValue(value.toInt());
            break;
        }
        case SettingsValue::UNSIGNED_DOUBLE:
        case SettingsValue::DOUBLE:
        case SettingsValue::DOUBLE_RANGE:
        {
            // Double spin box editors.
            static_cast<DoubleSpinBox*>(editor)->setValue(
                    value.toString());
            break;
        }
        case SettingsValue::DOUBLE_RANGE_EXT:
        {
            DoubleSpinBox* e = static_cast<DoubleSpinBox*>(editor);
            QVariant v = index.model()->data(index, SettingsModel::ExtRangeRole);
            QList<QVariant> range = v.toList();
            double min_ = range[0].toDouble();
            QString ext_min_ = range[2].toString();
            if (value.toString().toUpper() == ext_min_ ||
                    value.toDouble() < min_)
                e->setValue(e->rangeMin());
            else
                e->setValue(value.toDouble());
            break;
        }
        case SettingsValue::DATE_TIME:
        {
            // Date and time editors.
            static_cast<QLineEdit*>(editor)->setText(value.toString());
            break;
        }
        case SettingsValue::TIME:
        {
            // Time editors.
            static_cast<QLineEdit*>(editor)->setText(value.toString());
            break;
        }
        case SettingsValue::RANDOM_SEED:
        {
            // Random seed editors.
            if (value.toString().toUpper() == "TIME" || value.toInt() < 1)
                static_cast<QSpinBox*>(editor)->setValue(0);
            else
                static_cast<QSpinBox*>(editor)->setValue(value.toInt());
            break;
        }
        case SettingsValue::INT_RANGE_EXT:  // AXIS_RANGE
        {
            QVariant v = index.model()->data(index, SettingsModel::ExtRangeRole);
            QList<QVariant> range = v.toList();
            int min_ = range[0].toInt();
            QString ext_min_ = range[2].toString();
            if (value.toString().toUpper() == ext_min_ || value.toInt() < min_)
                static_cast<QSpinBox*>(editor)->setValue(-1);
            else
                static_cast<QSpinBox*>(editor)->setValue(value.toInt());
            break;
        }
        case SettingsValue::OPTION_LIST:
        {
            // Options list.
            QString str = value.toString();
            int i = static_cast<QComboBox*>(editor)->findText(str,
                                                      Qt::MatchFixedString);
            if (i < 0) i = 0;
            static_cast<QComboBox*>(editor)->setCurrentIndex(i);
            break;
        }
        default:
        {
            // Line editors.
            static_cast<QLineEdit*>(editor)->setText(value.toString());
            break;
        }
    }
}