| 1 | /*************************************************************************** |
| 2 | styledelegate.cpp |
| 3 | ------------------- |
| 4 | Style 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 <QDoubleSpinBox> |
| 12 | #include <QLineEdit> |
| 13 | #include <QSpinBox> |
| 14 | |
| 15 | #include "data.h" |
| 16 | #include "style.h" |
| 17 | #include "styledelegate.h" |
| 18 | #include "stylemodel.h" |
| 19 | |
| 20 | StyleDelegate::StyleDelegate(QObject *parent) : QItemDelegate(parent) {} |
| 21 | |
| 22 | StyleDelegate::~StyleDelegate() {} |
| 23 | |
| 24 | QWidget *StyleDelegate::createEditor(QWidget *parent, |
| 25 | const QStyleOptionViewItem &/*option*/, |
| 26 | const QModelIndex &index) const |
| 27 | { |
| 28 | QLineEdit *edit; |
| 29 | QDoubleSpinBox *dspin; |
| 30 | QSpinBox *spin; |
| 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 StyleModel::NAME: |
| 39 | edit = new QLineEdit(parent); |
| 40 | edit->installEventFilter(const_cast<StyleDelegate*>(this)); |
| 41 | return edit; |
| 42 | |
| 43 | case StyleModel::OGLOW: |
| 44 | case StyleModel::OGHI: |
| 45 | case StyleModel::FGLOW: |
| 46 | case StyleModel::FGHI: |
| 47 | if (blank) return 0; |
| 48 | dspin = new QDoubleSpinBox(parent); |
| 49 | dspin->setDecimals(3); |
| 50 | dspin->setRange(0.990, 1.150); |
| 51 | dspin->setSingleStep(0.001); |
| 52 | dspin->setAccelerated(true); |
| 53 | dspin->installEventFilter(const_cast<StyleDelegate*>(this)); |
| 54 | return dspin; |
| 55 | |
| 56 | case StyleModel::IBULOW: |
| 57 | case StyleModel::IBUHI: |
| 58 | if (blank) return 0; |
| 59 | spin = new QSpinBox(parent); |
| 60 | spin->setRange(0, 120); |
| 61 | spin->setSingleStep(1); |
| 62 | spin->setAccelerated(true); |
| 63 | spin->installEventFilter(const_cast<StyleDelegate*>(this)); |
| 64 | return spin; |
| 65 | |
| 66 | case StyleModel::SRMLOW: |
| 67 | case StyleModel::SRMHI: |
| 68 | if (blank) return 0; |
| 69 | spin = new QSpinBox(parent); |
| 70 | spin->setRange(0, 50); |
| 71 | spin->setSingleStep(1); |
| 72 | spin->setAccelerated(true); |
| 73 | spin->installEventFilter(const_cast<StyleDelegate*>(this)); |
| 74 | return spin; |
| 75 | |
| 76 | default: |
| 77 | return 0; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | void StyleDelegate::setEditorData(QWidget *editor, |
| 82 | const QModelIndex &index) const |
| 83 | { |
| 84 | QLineEdit *edit; |
| 85 | QDoubleSpinBox *dspin; |
| 86 | QSpinBox *spin; |
| 87 | |
| 88 | QVariant value = index.model()->data(index, Qt::EditRole); |
| 89 | |
| 90 | // different kind of editor for each column |
| 91 | switch (index.column()) { |
| 92 | case StyleModel::NAME: |
| 93 | edit = static_cast<QLineEdit*>(editor); |
| 94 | if (!edit) return; |
| 95 | edit->setText(value.toString()); |
| 96 | break; |
| 97 | |
| 98 | case StyleModel::OGLOW: |
| 99 | case StyleModel::OGHI: |
| 100 | case StyleModel::FGLOW: |
| 101 | case StyleModel::FGHI: |
| 102 | dspin = static_cast<QDoubleSpinBox*>(editor); |
| 103 | if (!dspin) return; |
| 104 | dspin->setValue(value.toDouble()); |
| 105 | break; |
| 106 | |
| 107 | case StyleModel::IBULOW: |
| 108 | case StyleModel::IBUHI: |
| 109 | case StyleModel::SRMLOW: |
| 110 | case StyleModel::SRMHI: |
| 111 | spin = static_cast<QSpinBox*>(editor); |
| 112 | if (!spin) return; |
| 113 | spin->setValue(value.toUInt()); |
| 114 | break; |
| 115 | default: |
| 116 | QItemDelegate::setEditorData(editor, index); |
| 117 | break; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | void StyleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, |
| 122 | const QModelIndex &index) const |
| 123 | { |
| 124 | QLineEdit *edit; |
| 125 | QDoubleSpinBox *dspin; |
| 126 | QSpinBox *spin; |
| 127 | QVariant value; |
| 128 | |
| 129 | // different kind of editor for each column |
| 130 | switch (index.column()) { |
| 131 | case StyleModel::NAME: |
| 132 | edit = static_cast<QLineEdit*>(editor); |
| 133 | if (!edit) return; |
| 134 | value = edit->text(); |
| 135 | model->setData(index, value); |
| 136 | break; |
| 137 | |
| 138 | case StyleModel::OGLOW: |
| 139 | case StyleModel::OGHI: |
| 140 | case StyleModel::FGLOW: |
| 141 | case StyleModel::FGHI: |
| 142 | dspin = static_cast<QDoubleSpinBox*>(editor); |
| 143 | if (!dspin) return; |
| 144 | value = dspin->value(); |
| 145 | model->setData(index, value); |
| 146 | break; |
| 147 | |
| 148 | case StyleModel::IBULOW: |
| 149 | case StyleModel::IBUHI: |
| 150 | case StyleModel::SRMLOW: |
| 151 | case StyleModel::SRMHI: |
| 152 | spin = static_cast<QSpinBox*>(editor); |
| 153 | if (!spin) return; |
| 154 | value = spin->value(); |
| 155 | model->setData(index, value); |
| 156 | break; |
| 157 | |
| 158 | default: |
| 159 | QItemDelegate::setModelData(editor, model,index); |
| 160 | break; |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | void StyleDelegate::updateEditorGeometry(QWidget *editor, |
| 165 | const QStyleOptionViewItem &option, |
| 166 | const QModelIndex &/*index*/) const |
| 167 | { |
| 168 | editor->setGeometry(option.rect); |
| 169 | } |