1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/attributedelegate.cpp Mon Aug 04 13:35:54 2008 +0000
1.3 @@ -0,0 +1,138 @@
1.4 +#include <QtGui>
1.5 +
1.6 +#include "attributedelegate.h"
1.7 +#include <iostream>
1.8 +
1.9 +using namespace::std;
1.10 +
1.11 +AttributeDelegate::AttributeDelegate(QObject *parent)
1.12 + : QItemDelegate(parent)
1.13 +{
1.14 +}
1.15 +
1.16 +QWidget *AttributeDelegate::createEditor(QWidget *parent,
1.17 + const QStyleOptionViewItem &/* option */,
1.18 + const QModelIndex & index ) const
1.19 +{
1.20 + int col=index.column();
1.21 + int row=index.row();
1.22 +
1.23 + if (col==0 && row==index.model()->rowCount() -1 )
1.24 + {
1.25 + //We are editing a new attribute, starting with attribute name
1.26 + QComboBox *editor = new QComboBox(parent);
1.27 + editor->insertItems (0,attributeTable->getKeys());
1.28 + return editor;
1.29 + }
1.30 + if (col==1 && row==index.model()->rowCount() -1 )
1.31 + {
1.32 + cout << "Edit value now..."<<endl;
1.33 + //We are editing a new attribute, starting with attribute name
1.34 + QComboBox *editor = new QComboBox(parent);
1.35 + editor->insertItems (0,attributeTable->getKeys());
1.36 + return editor;
1.37 +
1.38 + }
1.39 +
1.40 + // Is there already an atttribute defined or
1.41 + // do we need to create a new one?
1.42 +
1.43 + QVariant var=index.model()->data(index.model()->index(row,2,QModelIndex()));
1.44 + QString typeName=var.toString();
1.45 + cout << "AttrDel::createEditor type="<<qPrintable (typeName)<<endl;
1.46 +
1.47 + if (typeName=="IntList")
1.48 + {
1.49 + QSpinBox *editor = new QSpinBox(parent);
1.50 + editor->setMinimum(0);
1.51 + editor->setMaximum(5);
1.52 + return editor;
1.53 + } else if (typeName=="FreeInt")
1.54 + {
1.55 + QSpinBox *editor = new QSpinBox(parent);
1.56 + editor->setMinimum(0);
1.57 + editor->setMaximum(100);
1.58 + return editor;
1.59 + } else if (typeName=="FreeString")
1.60 + {
1.61 + QComboBox *editor = new QComboBox(parent);
1.62 + return editor;
1.63 + } else if (typeName=="StringList")
1.64 + {
1.65 + QComboBox *editor = new QComboBox(parent);
1.66 + return editor;
1.67 + }
1.68 +
1.69 + return NULL;
1.70 +}
1.71 +
1.72 +void AttributeDelegate::setEditorData(QWidget *editor,
1.73 + const QModelIndex &index) const
1.74 +{
1.75 + QVariant value= index.model()->data(index, Qt::DisplayRole);
1.76 + switch (value.type())
1.77 + {
1.78 + case QVariant::Int:
1.79 + {
1.80 + int value = index.model()->data(index, Qt::DisplayRole).toInt();
1.81 + QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
1.82 + spinBox->setValue(value);
1.83 + break;
1.84 + }
1.85 + /*
1.86 + {
1.87 + QString value = index.model()->data(index, Qt::DisplayRole).toString();
1.88 + QLineEdit *le= static_cast<QLineEdit*>(editor);
1.89 + le->setText(value);
1.90 + break;
1.91 + }
1.92 + */
1.93 + case QVariant::String:
1.94 + {
1.95 + QComboBox *cb= static_cast<QComboBox*>(editor);
1.96 + QStringList sl;
1.97 + sl<< index.model()->data(index, Qt::DisplayRole).toString();
1.98 + cb->insertStringList (sl);
1.99 + break;
1.100 + }
1.101 + default:
1.102 + break;
1.103 + }
1.104 +}
1.105 +
1.106 +void AttributeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
1.107 + const QModelIndex &index) const
1.108 +{
1.109 + QVariant value= index.model()->data(index, Qt::DisplayRole);
1.110 + switch (value.type())
1.111 + {
1.112 + case QVariant::Int:
1.113 + {
1.114 + QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
1.115 + spinBox->interpretText();
1.116 + model->setData(index, spinBox->value(), Qt::EditRole);
1.117 + break;
1.118 + }
1.119 + case QVariant::String:
1.120 + {
1.121 + QComboBox *cb = static_cast<QComboBox*>(editor);
1.122 + model->setData(index, cb->currentText(), Qt::EditRole);
1.123 + break;
1.124 + }
1.125 + default:
1.126 + break;
1.127 + }
1.128 +
1.129 +}
1.130 +
1.131 +void AttributeDelegate::updateEditorGeometry(QWidget *editor,
1.132 + const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
1.133 +{
1.134 + editor->setGeometry(option.rect);
1.135 +}
1.136 +
1.137 +void AttributeDelegate::setAttributeTable (AttributeTable *table)
1.138 +{
1.139 + attributeTable=table;
1.140 +}
1.141 +