-
Notifications
You must be signed in to change notification settings - Fork 1
/
memorydelegate.cpp
145 lines (128 loc) · 4.37 KB
/
memorydelegate.cpp
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
/*!
* \file memorydelegate.cpp
* \author Simon Coakley
* \date 2012
* \copyright Copyright (c) 2012 University of Sheffield
* \brief Implementation of the model memory delegate
*/
#include <QtGui>
#include "./memorydelegate.h"
MemoryDelegate::MemoryDelegate(QObject *parent)
: QItemDelegate(parent) {
}
QWidget *MemoryDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const {
/*if(index.column() == 0)
{
QComboBox *editor = new QComboBox(parent);
editor->insertItem(0, "int");
editor->insertItem(1, "double");
return editor;
}
else if(index.column() == 2)
{
QModelIndex typeIndex = index.model()->index(index.row(), 0, QModelIndex());
if (typeIndex.data().toString() == "int")
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(-1000);
editor->setMaximum(1000);
return editor;
}
//if (typeIndex.data().toString() == "double")
else
{
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setMinimum(-1000);
editor->setMaximum(1000);
return editor;
}
}
else */if(index.column() == 0 || index.column() == 1 ||
index.column() == 2) {
QLineEdit *editor = new QLineEdit(parent);
return editor;
} else {
return QItemDelegate::createEditor(parent, option, index);
}
}
void MemoryDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const {
/*if(index.column() == 0)
{
QString value = index.data().toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
for(int i = 0; i < comboBox->count(); i++)
{
if(comboBox->itemText(i) == value)
comboBox->setCurrentIndex(i);
}
}
else if(index.column() == 2)
{
QModelIndex typeIndex = index.model()->index(index.row(), 0, QModelIndex());
if (typeIndex.data().toString() == "int")
{
int value = index.data().toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
}
//if (typeIndex.data().toString() == "double")
else
{
double value = index.data().toDouble();
QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
spinBox->setValue(value);
}
}
else */if(index.column() == 0 || index.column() == 1 ||
index.column() == 2) {
QString value = index.data().toString();
QLineEdit * lineEdit = static_cast<QLineEdit*>(editor);
lineEdit->setText(value);
} else {
QItemDelegate::setEditorData(editor, index);
}
}
void MemoryDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const {
/*if(index.column() == 0)
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();
model->setData(index, value, Qt::EditRole);
}
else if(index.column() == 2)
{
QModelIndex typeIndex = index.model()->index(index.row(), 0, QModelIndex());
if (typeIndex.data().toString() == "int")
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
int value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
//if (typeIndex.data().toString() == "double")
else
{
QDoubleSpinBox *spinBox = static_cast<QDoubleSpinBox*>(editor);
//spinBox->interpretText();
double value = spinBox->value();
model->setData(index, value, Qt::EditRole);
}
}
else */
if (index.column() == 0 || index.column() == 1 ||
index.column() == 2) {
QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
QString value = lineEdit->text();
model->setData(index, value, Qt::EditRole);
} else {
QItemDelegate::setModelData(editor, model, index);
}
}
void MemoryDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const {
editor->setGeometry(option.rect);
}