-
Notifications
You must be signed in to change notification settings - Fork 6
/
attributedelegate.cpp
138 lines (122 loc) · 3.39 KB
/
attributedelegate.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
#include <QtGui>
#include "attributedelegate.h"
#include <iostream>
using namespace::std;
AttributeDelegate::AttributeDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWidget *AttributeDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex & index ) const
{
int col=index.column();
int row=index.row();
if (col==0 && row==index.model()->rowCount() -1 )
{
//We are editing a new attribute, starting with attribute name
QComboBox *editor = new QComboBox(parent);
editor->insertItems (0,attributeTable->getKeys());
return editor;
}
if (col==1 && row==index.model()->rowCount() -1 )
{
qDebug()<< "Edit value now...";
//We are editing a new attribute, starting with attribute name
QComboBox *editor = new QComboBox(parent);
editor->insertItems (0,attributeTable->getKeys());
return editor;
}
// Is there already an atttribute defined or
// do we need to create a new one?
QVariant var=index.model()->data(index.model()->index(row,2,QModelIndex()));
QString typeName=var.toString();
qDebug()<< "AttrDel::createEditor type="<<qPrintable (typeName);
if (typeName=="IntList")
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(5);
return editor;
} else if (typeName=="FreeInt")
{
QSpinBox *editor = new QSpinBox(parent);
editor->setMinimum(0);
editor->setMaximum(100);
return editor;
} else if (typeName=="FreeString")
{
QComboBox *editor = new QComboBox(parent);
return editor;
} else if (typeName=="StringList")
{
QComboBox *editor = new QComboBox(parent);
return editor;
}
return NULL;
}
void AttributeDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
QVariant value= index.model()->data(index, Qt::DisplayRole);
switch (value.type())
{
case QVariant::Int:
{
int value = index.model()->data(index, Qt::DisplayRole).toInt();
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->setValue(value);
break;
}
/*
{
QString value = index.model()->data(index, Qt::DisplayRole).toString();
QLineEdit *le= static_cast<QLineEdit*>(editor);
le->setText(value);
break;
}
*/
case QVariant::String:
{
QComboBox *cb= static_cast<QComboBox*>(editor);
QStringList sl;
sl<< index.model()->data(index, Qt::DisplayRole).toString();
cb->insertStringList (sl);
break;
}
default:
break;
}
}
void AttributeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QVariant value= index.model()->data(index, Qt::DisplayRole);
switch (value.type())
{
case QVariant::Int:
{
QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
spinBox->interpretText();
model->setData(index, spinBox->value(), Qt::EditRole);
break;
}
case QVariant::String:
{
QComboBox *cb = static_cast<QComboBox*>(editor);
model->setData(index, cb->currentText(), Qt::EditRole);
break;
}
default:
break;
}
}
void AttributeDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
{
editor->setGeometry(option.rect);
}
void AttributeDelegate::setAttributeTable (AttributeTable *table)
{
attributeTable=table;
}