| 1 | /*************************************************************************** |
| 2 | miscdelegate.cpp |
| 3 | ------------------- |
| 4 | Misc 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 | #include <QLineEdit> |
| 14 | |
| 15 | #include "data.h" |
| 16 | #include "misc.h" |
| 17 | #include "miscdelegate.h" |
| 18 | #include "miscmodel.h" |
| 19 | |
| 20 | MiscDelegate::MiscDelegate(QObject *parent) : QItemDelegate(parent) {} |
| 21 | |
| 22 | MiscDelegate::~MiscDelegate() {} |
| 23 | |
| 24 | QWidget *MiscDelegate::createEditor(QWidget *parent, |
| 25 | const QStyleOptionViewItem &/*option*/, |
| 26 | const QModelIndex &index) const |
| 27 | { |
| 28 | QComboBox *combo; |
| 29 | QDoubleSpinBox *spin; |
| 30 | QLineEdit *edit; |
| 31 | QString suffix; |
| 32 | |
| 33 | // can only edit name on blank row |
| 34 | bool blank = index.row() >= index.model()->rowCount(); |
| 35 | |
| 36 | // different kind of editor for each column |
| 37 | switch (index.column()) { |
| 38 | case MiscModel::NAME: |
| 39 | combo = new QComboBox(parent); |
| 40 | combo->setEditable(true); |
| 41 | combo->addItem(QString()); |
| 42 | combo->addItems(Data::instance()->miscList()); |
| 43 | combo->installEventFilter(const_cast<MiscDelegate*>(this)); |
| 44 | return combo; |
| 45 | |
| 46 | case MiscModel::QUANTITY: |
| 47 | if (blank) return 0; |
| 48 | spin = new QDoubleSpinBox(parent); |
| 49 | spin->setDecimals(3); |
| 50 | spin->setRange(0.00, 1000.00); |
| 51 | spin->setSingleStep(0.25); |
| 52 | suffix = " " + Data::instance()->defaultMiscUnit().symbol(); |
| 53 | spin->setSuffix(suffix); |
| 54 | spin->setAccelerated(true); |
| 55 | spin->installEventFilter(const_cast<MiscDelegate*>(this)); |
| 56 | return spin; |
| 57 | |
| 58 | case MiscModel::TYPE: |
| 59 | combo = new QComboBox(parent); |
| 60 | combo->setEditable(false); |
| 61 | combo->addItems(Misc::typeStringList()); |
| 62 | combo->installEventFilter(const_cast<MiscDelegate*>(this)); |
| 63 | return combo; |
| 64 | |
| 65 | case MiscModel::NOTES: |
| 66 | if (blank) return 0; |
| 67 | edit = new QLineEdit(parent); |
| 68 | edit->installEventFilter(const_cast<MiscDelegate*>(this)); |
| 69 | return edit; |
| 70 | |
| 71 | default: |
| 72 | return 0;; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | void MiscDelegate::setEditorData(QWidget *editor, |
| 77 | const QModelIndex &index) const |
| 78 | { |
| 79 | QComboBox *combo; |
| 80 | QDoubleSpinBox *spin; |
| 81 | QLineEdit *edit; |
| 82 | int comboindex; |
| 83 | |
| 84 | QVariant value = index.model()->data(index, Qt::EditRole); |
| 85 | |
| 86 | // different kind of editor for each column |
| 87 | switch (index.column()) { |
| 88 | case MiscModel::NAME: |
| 89 | combo = static_cast<QComboBox*>(editor); |
| 90 | if (!combo) return; |
| 91 | comboindex = combo->findText(value.toString()); |
| 92 | if (comboindex > 0) { |
| 93 | combo->setCurrentIndex(comboindex); |
| 94 | } else { |
| 95 | combo->setEditText(value.toString()); |
| 96 | } |
| 97 | break; |
| 98 | |
| 99 | case MiscModel::QUANTITY: |
| 100 | spin = static_cast<QDoubleSpinBox*>(editor); |
| 101 | if (!spin) return; |
| 102 | spin->setValue(value.toDouble()); |
| 103 | break; |
| 104 | |
| 105 | case MiscModel::NOTES: |
| 106 | edit = static_cast<QLineEdit*>(editor); |
| 107 | if (!edit) return; |
| 108 | edit->setText(value.toString()); |
| 109 | break; |
| 110 | |
| 111 | case MiscModel::TYPE: |
| 112 | combo = static_cast<QComboBox*>(editor); |
| 113 | if (!combo) return; |
| 114 | combo->setCurrentIndex(combo->findText(value.toString())); |
| 115 | break; |
| 116 | |
| 117 | default: |
| 118 | QItemDelegate::setEditorData(editor, index); |
| 119 | break; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | void MiscDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
| 124 | const QModelIndex &index) const |
| 125 | { |
| 126 | QComboBox *combo; |
| 127 | QDoubleSpinBox *spin; |
| 128 | QLineEdit *edit; |
| 129 | QVariant value; |
| 130 | |
| 131 | // different kind of editor for each column |
| 132 | switch (index.column()) { |
| 133 | case MiscModel::NAME: |
| 134 | case MiscModel::TYPE: |
| 135 | combo = static_cast<QComboBox*>(editor); |
| 136 | if (!combo) return; |
| 137 | value = combo->currentText(); |
| 138 | model->setData(index, value); |
| 139 | break; |
| 140 | |
| 141 | case MiscModel::NOTES: |
| 142 | edit = static_cast<QLineEdit*>(editor); |
| 143 | if (!edit) return; |
| 144 | value = edit->text(); |
| 145 | model->setData(index, value); |
| 146 | break; |
| 147 | |
| 148 | case MiscModel::QUANTITY: |
| 149 | spin = static_cast<QDoubleSpinBox*>(editor); |
| 150 | if (!spin) return; |
| 151 | value = spin->value(); |
| 152 | model->setData(index, value); |
| 153 | break; |
| 154 | |
| 155 | default: |
| 156 | QItemDelegate::setModelData(editor, model,index); |
| 157 | break; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void MiscDelegate::updateEditorGeometry(QWidget *editor, |
| 162 | const QStyleOptionViewItem &option, |
| 163 | const QModelIndex &/*index*/) const |
| 164 | { |
| 165 | editor->setGeometry(option.rect); |
| 166 | } |