3 #include "attributedelegate.h"
8 AttributeDelegate::AttributeDelegate(QObject *parent)
9 : QItemDelegate(parent)
13 QWidget *AttributeDelegate::createEditor(QWidget *parent,
14 const QStyleOptionViewItem &/* option */,
15 const QModelIndex & index ) const
17 int col=index.column();
20 if (col==0 && row==index.model()->rowCount() -1 )
22 //We are editing a new attribute, starting with attribute name
23 QComboBox *editor = new QComboBox(parent);
24 editor->insertItems (0,attributeTable->getKeys());
27 if (col==1 && row==index.model()->rowCount() -1 )
29 cout << "Edit value now..."<<endl;
30 //We are editing a new attribute, starting with attribute name
31 QComboBox *editor = new QComboBox(parent);
32 editor->insertItems (0,attributeTable->getKeys());
37 // Is there already an atttribute defined or
38 // do we need to create a new one?
40 QVariant var=index.model()->data(index.model()->index(row,2,QModelIndex()));
41 QString typeName=var.toString();
42 cout << "AttrDel::createEditor type="<<qPrintable (typeName)<<endl;
44 if (typeName=="IntList")
46 QSpinBox *editor = new QSpinBox(parent);
47 editor->setMinimum(0);
48 editor->setMaximum(5);
50 } else if (typeName=="FreeInt")
52 QSpinBox *editor = new QSpinBox(parent);
53 editor->setMinimum(0);
54 editor->setMaximum(100);
56 } else if (typeName=="FreeString")
58 QComboBox *editor = new QComboBox(parent);
60 } else if (typeName=="StringList")
62 QComboBox *editor = new QComboBox(parent);
69 void AttributeDelegate::setEditorData(QWidget *editor,
70 const QModelIndex &index) const
72 QVariant value= index.model()->data(index, Qt::DisplayRole);
77 int value = index.model()->data(index, Qt::DisplayRole).toInt();
78 QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
79 spinBox->setValue(value);
84 QString value = index.model()->data(index, Qt::DisplayRole).toString();
85 QLineEdit *le= static_cast<QLineEdit*>(editor);
90 case QVariant::String:
92 QComboBox *cb= static_cast<QComboBox*>(editor);
94 sl<< index.model()->data(index, Qt::DisplayRole).toString();
95 cb->insertStringList (sl);
103 void AttributeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
104 const QModelIndex &index) const
106 QVariant value= index.model()->data(index, Qt::DisplayRole);
107 switch (value.type())
111 QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
112 spinBox->interpretText();
113 model->setData(index, spinBox->value(), Qt::EditRole);
116 case QVariant::String:
118 QComboBox *cb = static_cast<QComboBox*>(editor);
119 model->setData(index, cb->currentText(), Qt::EditRole);
128 void AttributeDelegate::updateEditorGeometry(QWidget *editor,
129 const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
131 editor->setGeometry(option.rect);
134 void AttributeDelegate::setAttributeTable (AttributeTable *table)
136 attributeTable=table;