# HG changeset patch
# User insilmaril
# Date 1204536340 0
# Node ID fd6d1292c4ada54606030b57c44843bbd702a99d
# Parent  3472b93efb0c630c6151c194229cadd285774182
added attributedelegate .h .cpp

diff -r 3472b93efb0c -r fd6d1292c4ad attributedelegate.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/attributedelegate.cpp	Mon Mar 03 09:25:40 2008 +0000
@@ -0,0 +1,138 @@
+#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 )
+	{
+		cout << "Edit value now..."<<endl;
+		//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();
+	cout << "AttrDel::createEditor type="<<qPrintable (typeName)<<endl;
+
+	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;
+}
+
diff -r 3472b93efb0c -r fd6d1292c4ad attributedelegate.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/attributedelegate.h	Mon Mar 03 09:25:40 2008 +0000
@@ -0,0 +1,37 @@
+
+#ifndef ATTRIBUTEDELEGATE_H
+#define ATTRIBUTEDELEGATE_H
+
+#include <QItemDelegate>
+#include <QModelIndex>
+#include <QObject>
+#include <QSize>
+#include <QSpinBox>
+#include <QLineEdit>
+
+#include "attribute.h"
+
+class AttributeDelegate : public QItemDelegate
+{
+    Q_OBJECT
+
+enum EditorType {Undefined,SpinBox,LineEdit,ComboBox};
+
+public:
+    AttributeDelegate(QObject *parent = 0);
+
+    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
+                          const QModelIndex &index) const;
+
+    void setEditorData(QWidget *editor, const QModelIndex &index) const ;
+    void setModelData(QWidget *editor, QAbstractItemModel *model,
+                      const QModelIndex &index) const ;
+
+	void updateEditorGeometry(QWidget *editor,
+	const QStyleOptionViewItem &option, const QModelIndex &index) const;
+	void setAttributeTable(AttributeTable *table);
+private:
+	AttributeTable *attributeTable;
+};
+
+#endif