| 1 | /*************************************************************************** |
| 2 | graindelegate.cpp |
| 3 | ------------------- |
| 4 | Grain delegate editor |
| 5 | ------------------- |
| 6 | Copyright 2006-2008, David Johnson |
| 7 | Please see the header file for copyright and license information |
| 8 | ***************************************************************************/ |
| 9 | |
| 10 | #include <QAbstractItemModel> |
| 11 | #include <QComboBox> |
| 12 | #include <QDoubleSpinBox> |
| 13 | |
| 14 | #include "data.h" |
| 15 | #include "grain.h" |
| 16 | #include "graindelegate.h" |
| 17 | #include "grainmodel.h" |
| 18 | |
| 19 | GrainDelegate::GrainDelegate(QObject *parent) : QItemDelegate(parent) {} |
| 20 | |
| 21 | GrainDelegate::~GrainDelegate() {} |
| 22 | |
| 23 | QWidget *GrainDelegate::createEditor(QWidget *parent, |
| 24 | const QStyleOptionViewItem &/*option*/, |
| 25 | const QModelIndex &index) const |
| 26 | { |
| 27 | QComboBox *combo; |
| 28 | QDoubleSpinBox *spin; |
| 29 | QString suffix; |
| 30 | |
| 31 | // can only edit name on blank row |
| 32 | if (index.row() >= index.model()->rowCount()) return 0; |
| 33 | |
| 34 | // different kind of editor for each column |
| 35 | switch (index.column()) { |
| 36 | case GrainModel::NAME: |
| 37 | combo = new QComboBox(parent); |
| 38 | combo->setEditable(true); |
| 39 | combo->addItem(QString()); |
| 40 | combo->addItems(Data::instance()->grainsList()); |
| 41 | combo->installEventFilter(const_cast<GrainDelegate*>(this)); |
| 42 | return combo; |
| 43 | |
| 44 | case GrainModel::WEIGHT: |
| 45 | spin = new QDoubleSpinBox(parent); |
| 46 | spin->setDecimals(3); |
| 47 | spin->setRange(0.00, 1000.00); |
| 48 | spin->setSingleStep(0.25); |
| 49 | spin->setAccelerated(true); |
| 50 | suffix = " " + Data::instance()->defaultGrainUnit().symbol(); |
| 51 | spin->setSuffix(suffix); |
| 52 | spin->installEventFilter(const_cast<GrainDelegate*>(this)); |
| 53 | return spin; |
| 54 | |
| 55 | case GrainModel::EXTRACT: |
| 56 | spin = new QDoubleSpinBox(parent); |
| 57 | spin->setDecimals(3); |
| 58 | spin->setRange(1.000, 1.100); |
| 59 | spin->setSingleStep(0.001); |
| 60 | spin->setAccelerated(true); |
| 61 | spin->installEventFilter(const_cast<GrainDelegate*>(this)); |
| 62 | return spin; |
| 63 | |
| 64 | case GrainModel::COLOR: |
| 65 | spin = new QDoubleSpinBox(parent); |
| 66 | spin->setDecimals(1); |
| 67 | spin->setRange(0.0, 500.0); |
| 68 | spin->setSingleStep(1.0); |
| 69 | spin->setSuffix(Resource::DEGREE); |
| 70 | spin->setAccelerated(true); |
| 71 | spin->installEventFilter(const_cast<GrainDelegate*>(this)); |
| 72 | return spin; |
| 73 | |
| 74 | case GrainModel::TYPE: |
| 75 | combo = new QComboBox(parent); |
| 76 | combo->setEditable(false); |
| 77 | combo->addItems(Grain::typeStringList()); |
| 78 | combo->installEventFilter(const_cast<GrainDelegate*>(this)); |
| 79 | return combo; |
| 80 | |
| 81 | case GrainModel::USE: |
| 82 | combo = new QComboBox(parent); |
| 83 | combo->setEditable(false); |
| 84 | combo->addItems(Grain::useStringList()); |
| 85 | combo->installEventFilter(const_cast<GrainDelegate*>(this)); |
| 86 | return combo; |
| 87 | |
| 88 | default: |
| 89 | return 0; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void GrainDelegate::setEditorData(QWidget *editor, |
| 94 | const QModelIndex &index) const |
| 95 | { |
| 96 | QComboBox *combo; |
| 97 | QDoubleSpinBox *spin; |
| 98 | int comboindex; |
| 99 | |
| 100 | QVariant value = index.model()->data(index, Qt::EditRole); |
| 101 | |
| 102 | // different kind of editor for each column |
| 103 | switch (index.column()) { |
| 104 | case GrainModel::NAME: |
| 105 | combo = static_cast<QComboBox*>(editor); |
| 106 | if (!combo) return; |
| 107 | comboindex = combo->findText(value.toString()); |
| 108 | if (comboindex > 0) { |
| 109 | combo->setCurrentIndex(comboindex); |
| 110 | } else { |
| 111 | combo->setEditText(value.toString()); |
| 112 | } |
| 113 | break; |
| 114 | |
| 115 | case GrainModel::WEIGHT: |
| 116 | case GrainModel::EXTRACT: |
| 117 | case GrainModel::COLOR: |
| 118 | spin = static_cast<QDoubleSpinBox*>(editor); |
| 119 | if (!spin) return; |
| 120 | spin->setValue(value.toDouble()); |
| 121 | break; |
| 122 | |
| 123 | case GrainModel::TYPE: |
| 124 | case GrainModel::USE: |
| 125 | combo = static_cast<QComboBox*>(editor); |
| 126 | if (!combo) return; |
| 127 | combo->setCurrentIndex(combo->findText(value.toString())); |
| 128 | break; |
| 129 | |
| 130 | default: |
| 131 | QItemDelegate::setEditorData(editor, index); |
| 132 | break; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | void GrainDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
| 137 | const QModelIndex &index) const |
| 138 | { |
| 139 | QComboBox *combo; |
| 140 | QDoubleSpinBox *spin; |
| 141 | QVariant value; |
| 142 | |
| 143 | // different kind of editor for each column |
| 144 | switch (index.column()) { |
| 145 | case GrainModel::NAME: |
| 146 | case GrainModel::TYPE: |
| 147 | case GrainModel::USE: |
| 148 | combo = static_cast<QComboBox*>(editor); |
| 149 | if (!combo) return; |
| 150 | value = combo->currentText(); |
| 151 | model->setData(index, value); |
| 152 | break; |
| 153 | |
| 154 | case GrainModel::WEIGHT: |
| 155 | case GrainModel::EXTRACT: |
| 156 | case GrainModel::COLOR: |
| 157 | spin = static_cast<QDoubleSpinBox*>(editor); |
| 158 | if (!spin) return; |
| 159 | value = spin->value(); |
| 160 | model->setData(index, value); |
| 161 | break; |
| 162 | |
| 163 | default: |
| 164 | QItemDelegate::setModelData(editor, model,index); |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void GrainDelegate::updateEditorGeometry(QWidget *editor, |
| 170 | const QStyleOptionViewItem &option, |
| 171 | const QModelIndex &/*index*/) const |
| 172 | { |
| 173 | if (editor) editor->setGeometry(option.rect); |
| 174 | } |
| 175 |