QWidget *DoubleSpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { DoubleSpinBox *editor = new DoubleSpinBox(parent); editor->setFrame(false); editor->setMinimum(0.); editor->setMaximum(10000000.); return editor; }
QWidget* SettingsDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& /*option*/, const QModelIndex& index) const { // Get the setting type. int type = index.model()->data(index, SettingsModel::TypeRole).toInt(); // Create the appropriate editor. QWidget* editor = 0; switch (type) { case SettingsValue::INT: { // Spin box editors. QSpinBox* spinner = new QSpinBox(parent); spinner->setFrame(false); spinner->setRange(-INT_MAX, INT_MAX); editor = spinner; break; } case SettingsValue::UNSIGNED_INT: { // Spin box editors. QSpinBox* spinner = new QSpinBox(parent); spinner->setFrame(false); spinner->setRange(0, INT_MAX); editor = spinner; break; } case SettingsValue::INT_POSITIVE: { // Spin box editors. QSpinBox* spinner = new QSpinBox(parent); spinner->setFrame(false); spinner->setRange(1, INT_MAX); editor = spinner; break; } case SettingsValue::UNSIGNED_DOUBLE: { // Double spin box editors. DoubleSpinBox* spinner = new DoubleSpinBox(parent); spinner->setFrame(false); spinner->setRange(0, DBL_MAX); editor = spinner; break; } case SettingsValue::DOUBLE: { // Double spin box editors. DoubleSpinBox* spinner = new DoubleSpinBox(parent); spinner->setFrame(false); spinner->setRange(-DBL_MAX, DBL_MAX); editor = spinner; break; } case SettingsValue::DOUBLE_RANGE: { // Double spin box editors. DoubleSpinBox* spinner = new DoubleSpinBox(parent); QVariant v = index.model()->data(index, SettingsModel::RangeRole); QList<QVariant> range = v.toList(); double min_ = range[0].toDouble(); double max_ = range[1].toDouble(); spinner->setFrame(false); spinner->setRange(min_, max_); editor = spinner; break; } case SettingsValue::DOUBLE_RANGE_EXT: { QVariant v = index.model()->data(index, SettingsModel::ExtRangeRole); QList<QVariant> range = v.toList(); double min_ = range[0].toDouble(); double max_ = range[1].toDouble(); QString ext_min_ = range[2].toString(); DoubleSpinBox* spinner = new DoubleSpinBox(parent); spinner->setFrame(false); spinner->setRange(min_, max_); spinner->setMinText(ext_min_); editor = spinner; break; } case SettingsValue::DATE_TIME: { // Date and time editors. QLineEdit* line = new QLineEdit(parent); line->setFrame(false); editor = line; break; } case SettingsValue::TIME: { // Time editors. QLineEdit* line = new QLineEdit(parent); line->setFrame(false); editor = line; break; } case SettingsValue::RANDOM_SEED: { // Random seed editors. QSpinBox* spinner = new QSpinBox(parent); spinner->setFrame(false); spinner->setRange(0, INT_MAX); spinner->setSpecialValueText("time"); editor = spinner; break; } case SettingsValue::INT_RANGE_EXT: { QVariant v = index.model()->data(index, SettingsModel::ExtRangeRole); QList<QVariant> range = v.toList(); int min_ = range[0].toInt(); int max_ = range[1].toInt(); QString ext_min_ = range[2].toString(); QSpinBox* spinner = new QSpinBox(parent); spinner->setFrame(false); spinner->setRange(min_, max_); spinner->setSpecialValueText(ext_min_); editor = spinner; break; } case SettingsValue::OPTION_LIST: { // Options list. QComboBox* list = new QComboBox(parent); QVariant data = index.model()->data(index, SettingsModel::OptionsRole); QStringList options = data.toStringList(); list->setFrame(false); list->addItems(options); connect(list, SIGNAL(activated(int)), this, SLOT(commitAndCloseEditor(int))); editor = list; break; } case SettingsValue::DOUBLE_LIST: { // Floating-point arrays. QLineEdit* line = new QLineEdit(parent); QRegExp regex("^[+-]?\\d+(?:\\.\\d+)?(,[+-]?\\d+(?:\\.\\d+)?)*$"); QValidator* validator = new QRegExpValidator(regex, line); line->setFrame(false); line->setValidator(validator); editor = line; break; } default: { // Line editors. QLineEdit* line = new QLineEdit(parent); line->setFrame(false); editor = line; break; } } editor->setAutoFillBackground(true); return editor; }