Debian Codegraph @

src/hopdelegate.cpp

raw symbols SCIP
1 /***************************************************************************
2 hopdelegate.cpp
3 -------------------
4 Hop 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 <QSpinBox>
14
15 #include "data.h"
16 #include "hop.h"
17 #include "hopdelegate.h"
18 #include "hopmodel.h"
19
20 HopDelegate::HopDelegate(QObject *parent) : QItemDelegate(parent) {}
21
22 HopDelegate::~HopDelegate() {}
23
24 QWidget *HopDelegate::createEditor(QWidget *parent,
25 const QStyleOptionViewItem &/*option*/,
26 const QModelIndex &index) const
27 {
28 QComboBox *combo;
29 QSpinBox *spin;
30 QDoubleSpinBox *dspin;
31 QString suffix;
32 // can only edit name on blank row
33 bool blank = index.row() >= index.model()->rowCount();
34
35
36 // different kind of editor for each column
37 switch (index.column()) {
38 case HopModel::NAME:
39 combo = new QComboBox(parent);
40 combo->setEditable(true);
41 combo->addItem(QString());
42 combo->addItems(Data::instance()->hopsList());
43 combo->installEventFilter(const_cast<HopDelegate*>(this));
44 return combo;
45
46 case HopModel::WEIGHT:
47 if (blank) return 0;
48 dspin = new QDoubleSpinBox(parent);
49 dspin->setDecimals(3);
50 dspin->setRange(0.00, 1000.00);
51 dspin->setSingleStep(0.25);
52 suffix = " " + Data::instance()->defaultHopUnit().symbol();
53 dspin->setSuffix(suffix);
54 dspin->setAccelerated(true);
55 dspin->installEventFilter(const_cast<HopDelegate*>(this));
56 return dspin;
57
58 case HopModel::ALPHA:
59 if (blank) return 0;
60 dspin = new QDoubleSpinBox(parent);
61 dspin->setDecimals(1);
62 dspin->setRange(0.0, 50.0);
63 dspin->setSingleStep(0.1);
64 dspin->setSuffix("%");
65 dspin->setAccelerated(true);
66 dspin->installEventFilter(const_cast<HopDelegate*>(this));
67 return dspin;
68
69 case HopModel::TIME:
70 if (blank) return 0;
71 spin = new QSpinBox(parent);
72 spin->setRange(0, 120);
73 spin->setSingleStep(5);
74 spin->setSuffix(tr(" min", "minutes"));
75 spin->setAccelerated(true);
76 spin->installEventFilter(const_cast<HopDelegate*>(this));
77 return spin;
78
79 case HopModel::TYPE:
80 if (blank) return 0;
81 combo = new QComboBox(parent);
82 combo->setEditable(true);
83 combo->addItems(Hop::typeStringList());
84 combo->installEventFilter(const_cast<HopDelegate*>(this));
85 return combo;
86
87 default:
88 return 0;
89 }
90 }
91
92 void HopDelegate::setEditorData(QWidget *editor,
93 const QModelIndex &index) const
94 {
95 QComboBox *combo;
96 QSpinBox *spin;
97 QDoubleSpinBox *dspin;
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 HopModel::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 HopModel::WEIGHT:
116 case HopModel::ALPHA:
117 dspin = static_cast<QDoubleSpinBox*>(editor);
118 if (!dspin) return;
119 dspin->setValue(value.toDouble());
120 break;
121
122 case HopModel::TIME:
123 spin = static_cast<QSpinBox*>(editor);
124 if (!spin) return;
125 spin->setValue(value.toUInt());
126 break;
127
128 case HopModel::TYPE:
129 combo = static_cast<QComboBox*>(editor);
130 if (!combo) return;
131 comboindex = combo->findText(value.toString());
132 if (comboindex > 0) {
133 combo->setCurrentIndex(comboindex);
134 } else {
135 combo->setEditText(value.toString());
136 }
137 break;
138
139 default:
140 QItemDelegate::setEditorData(editor, index);
141 break;
142 }
143 }
144
145 void HopDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
146 const QModelIndex &index) const
147 {
148 QComboBox *combo;
149 QSpinBox *spin;
150 QDoubleSpinBox *dspin;
151 QVariant value;
152
153 // different kind of editor for each column
154 switch (index.column()) {
155 case HopModel::NAME:
156 case HopModel::TYPE:
157 combo = static_cast<QComboBox*>(editor);
158 if (!combo) return;
159 value = combo->currentText();
160 model->setData(index, value);
161 break;
162
163 case HopModel::WEIGHT:
164 case HopModel::ALPHA:
165 dspin = static_cast<QDoubleSpinBox*>(editor);
166 if (!dspin) return;
167 value = dspin->value();
168 model->setData(index, value);
169 break;
170
171 case HopModel::TIME:
172 spin = static_cast<QSpinBox*>(editor);
173 if (!spin) return;
174 value = spin->value();
175 model->setData(index, value);
176 break;
177
178 default:
179 QItemDelegate::setModelData(editor, model,index);
180 break;
181 }
182 }
183
184 void HopDelegate::updateEditorGeometry(QWidget *editor,
185 const QStyleOptionViewItem &option,
186 const QModelIndex &/*index*/) const
187 {
188 editor->setGeometry(option.rect);
189 }