-
Notifications
You must be signed in to change notification settings - Fork 0
/
itemdelegate.h
165 lines (149 loc) · 4.39 KB
/
itemdelegate.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#ifndef ITEM_DELEGATE_H
#define ITEM_DELEGATE_H
#include <QAbstractItemDelegate>
#include <QItemDelegate>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QLineEdit>
#include "common.h"
class ItemDelegate : public QItemDelegate
{
public:
ItemDelegate(
QObject* iParent
) :
QItemDelegate(iParent)
{
}
private:
virtual
QWidget*
createEditor(
QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &iIndex
) const
{
const auto& lData = iIndex.data(TypeRole);
if(lData.isValid())
{
switch(lData.toUInt())
{
case TypeBool :
{
//Create the combobox editor
QComboBox *editor = new QComboBox(parent);
editor->addItem("false");
editor->addItem("true");
return editor;
}
case TypeDouble :
{
//Create double spinbox editor
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setDecimals(4);
editor->setSingleStep(0.01);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
}
case TypeString :
//Create line edit
return new QLineEdit(parent);
}
}
return nullptr;
}
virtual
void
setEditorData(
QWidget *iEditor,
const QModelIndex &iIndex
) const
{
const auto& lData = iIndex.data(TypeRole);
if(lData.isValid())
{
switch(lData.toUInt())
{
case TypeBool :
{
QComboBox *lControl = qobject_cast<QComboBox*>(iEditor);
//Extract data from model
const bool lValue = iIndex.model()->data(iIndex, Qt::EditRole).toBool();
lControl->setCurrentIndex(lValue?(1):(0));
}
break;
case TypeDouble :
{
QDoubleSpinBox *lControl = qobject_cast<QDoubleSpinBox*>(iEditor);
//Extract data from model
const double lValue = iIndex.model()->data(iIndex, Qt::EditRole).toDouble();
lControl->setValue(lValue);
}
break;
case TypeString :
{
QLineEdit *lControl = static_cast<QLineEdit*>(iEditor);
//Extract data from model
QString value = iIndex.model()->data(iIndex, Qt::EditRole).toString();
lControl->setText(value);
}
break;
}
}
}
virtual
void
setModelData(
QWidget *iEditor,
QAbstractItemModel *iModel,
const QModelIndex &iIndex
) const
{
const auto& lData = iIndex.data(TypeRole);
if(lData.isValid())
{
switch(lData.toUInt())
{
case TypeBool :
{
//Copy data from editor to model
QComboBox *lControl = static_cast<QComboBox*>(iEditor);
int lValue = lControl->currentIndex();
if(lValue != -1)
{
iModel->setData(iIndex, lValue?(true):(false), Qt::EditRole);
}
}
break;
case TypeDouble :
{
QDoubleSpinBox *lControl = static_cast<QDoubleSpinBox*>(iEditor);
lControl->interpretText();
const double lValue = lControl->value();
iModel->setData(iIndex, lValue, Qt::EditRole);
}
break;
case TypeString :
{
QLineEdit *lineEdit = static_cast<QLineEdit*>(iEditor);
const QString& lValue = lineEdit->text();
iModel->setData(iIndex, lValue, Qt::EditRole);
}
break;
}
}
}
virtual
void
updateEditorGeometry(
QWidget *editor,
const QStyleOptionViewItem &option,
const QModelIndex &/* index */
) const
{
editor->setGeometry(option.rect);
}
};
#endif // ITEM_DELEGATE_H