attributedelegate.cpp
author insilmaril
Mon, 20 Apr 2009 10:42:05 +0000
changeset 754 db0ec4bcf416
parent 678 fd6d1292c4ad
permissions -rw-r--r--
minor fixes
insilmaril@678
     1
#include <QtGui>
insilmaril@678
     2
insilmaril@678
     3
#include "attributedelegate.h"
insilmaril@678
     4
#include <iostream>
insilmaril@678
     5
insilmaril@678
     6
using namespace::std;
insilmaril@678
     7
insilmaril@678
     8
AttributeDelegate::AttributeDelegate(QObject *parent)
insilmaril@678
     9
    : QItemDelegate(parent)
insilmaril@678
    10
{
insilmaril@678
    11
}
insilmaril@678
    12
insilmaril@678
    13
QWidget *AttributeDelegate::createEditor(QWidget *parent,
insilmaril@678
    14
    const QStyleOptionViewItem &/* option */,
insilmaril@678
    15
    const QModelIndex & index ) const
insilmaril@678
    16
{
insilmaril@678
    17
	int col=index.column();
insilmaril@678
    18
	int row=index.row();
insilmaril@678
    19
	
insilmaril@678
    20
	if (col==0 && row==index.model()->rowCount() -1 )
insilmaril@678
    21
	{
insilmaril@678
    22
		//We are editing a new attribute, starting with attribute name
insilmaril@678
    23
		QComboBox *editor = new QComboBox(parent);
insilmaril@678
    24
		editor->insertItems (0,attributeTable->getKeys());
insilmaril@678
    25
		return editor;
insilmaril@678
    26
	}
insilmaril@678
    27
	if (col==1 && row==index.model()->rowCount() -1 )
insilmaril@678
    28
	{
insilmaril@678
    29
		cout << "Edit value now..."<<endl;
insilmaril@678
    30
		//We are editing a new attribute, starting with attribute name
insilmaril@678
    31
		QComboBox *editor = new QComboBox(parent);
insilmaril@678
    32
		editor->insertItems (0,attributeTable->getKeys());
insilmaril@678
    33
		return editor;
insilmaril@678
    34
insilmaril@678
    35
	}
insilmaril@678
    36
insilmaril@678
    37
	// Is there already an atttribute defined or 
insilmaril@678
    38
	// do we need to create a new one?
insilmaril@678
    39
insilmaril@678
    40
	QVariant var=index.model()->data(index.model()->index(row,2,QModelIndex()));
insilmaril@678
    41
	QString typeName=var.toString();
insilmaril@678
    42
	cout << "AttrDel::createEditor type="<<qPrintable (typeName)<<endl;
insilmaril@678
    43
insilmaril@678
    44
	if (typeName=="IntList")
insilmaril@678
    45
	{
insilmaril@678
    46
		QSpinBox *editor = new QSpinBox(parent);
insilmaril@678
    47
		editor->setMinimum(0);
insilmaril@678
    48
		editor->setMaximum(5);
insilmaril@678
    49
		return editor;
insilmaril@678
    50
	} else if (typeName=="FreeInt")
insilmaril@678
    51
	{
insilmaril@678
    52
		QSpinBox *editor = new QSpinBox(parent);
insilmaril@678
    53
		editor->setMinimum(0);
insilmaril@678
    54
		editor->setMaximum(100);
insilmaril@678
    55
		return editor;
insilmaril@678
    56
	} else if (typeName=="FreeString")
insilmaril@678
    57
	{
insilmaril@678
    58
		QComboBox *editor = new QComboBox(parent);
insilmaril@678
    59
		return editor;
insilmaril@678
    60
	} else if (typeName=="StringList")
insilmaril@678
    61
	{
insilmaril@678
    62
		QComboBox *editor = new QComboBox(parent);
insilmaril@678
    63
		return editor;
insilmaril@678
    64
	} 
insilmaril@678
    65
insilmaril@678
    66
    return NULL;
insilmaril@678
    67
}
insilmaril@678
    68
insilmaril@678
    69
void AttributeDelegate::setEditorData(QWidget *editor,
insilmaril@678
    70
                                    const QModelIndex &index) const
insilmaril@678
    71
{
insilmaril@678
    72
	QVariant value= index.model()->data(index, Qt::DisplayRole);
insilmaril@678
    73
	switch (value.type())
insilmaril@678
    74
	{
insilmaril@678
    75
		case QVariant::Int:
insilmaril@678
    76
		{
insilmaril@678
    77
			int value = index.model()->data(index, Qt::DisplayRole).toInt();
insilmaril@678
    78
			QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
insilmaril@678
    79
			spinBox->setValue(value);
insilmaril@678
    80
			break;
insilmaril@678
    81
		}	
insilmaril@678
    82
		/*
insilmaril@678
    83
		{
insilmaril@678
    84
			QString value = index.model()->data(index, Qt::DisplayRole).toString();
insilmaril@678
    85
			QLineEdit *le= static_cast<QLineEdit*>(editor);
insilmaril@678
    86
			le->setText(value);
insilmaril@678
    87
			break;
insilmaril@678
    88
		}
insilmaril@678
    89
		*/
insilmaril@678
    90
		case QVariant::String:
insilmaril@678
    91
		{
insilmaril@678
    92
			QComboBox *cb= static_cast<QComboBox*>(editor);
insilmaril@678
    93
			QStringList sl;
insilmaril@678
    94
			sl<< index.model()->data(index, Qt::DisplayRole).toString();
insilmaril@678
    95
			cb->insertStringList (sl);
insilmaril@678
    96
			break;
insilmaril@678
    97
		}
insilmaril@678
    98
		default: 
insilmaril@678
    99
			break;
insilmaril@678
   100
	}		
insilmaril@678
   101
}
insilmaril@678
   102
insilmaril@678
   103
void AttributeDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
insilmaril@678
   104
                                   const QModelIndex &index) const
insilmaril@678
   105
{
insilmaril@678
   106
	QVariant value= index.model()->data(index, Qt::DisplayRole);
insilmaril@678
   107
	switch (value.type())
insilmaril@678
   108
	{
insilmaril@678
   109
		case QVariant::Int:
insilmaril@678
   110
		{
insilmaril@678
   111
			QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
insilmaril@678
   112
			spinBox->interpretText();
insilmaril@678
   113
			model->setData(index, spinBox->value(), Qt::EditRole);
insilmaril@678
   114
			break;
insilmaril@678
   115
		}	
insilmaril@678
   116
		case QVariant::String:
insilmaril@678
   117
		{
insilmaril@678
   118
			QComboBox *cb = static_cast<QComboBox*>(editor);
insilmaril@678
   119
			model->setData(index, cb->currentText(), Qt::EditRole);
insilmaril@678
   120
			break;
insilmaril@678
   121
		}
insilmaril@678
   122
		default:
insilmaril@678
   123
			break;
insilmaril@678
   124
	}		
insilmaril@678
   125
insilmaril@678
   126
}
insilmaril@678
   127
insilmaril@678
   128
void AttributeDelegate::updateEditorGeometry(QWidget *editor,
insilmaril@678
   129
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
insilmaril@678
   130
{
insilmaril@678
   131
    editor->setGeometry(option.rect);
insilmaril@678
   132
}
insilmaril@678
   133
insilmaril@678
   134
void AttributeDelegate::setAttributeTable (AttributeTable *table)
insilmaril@678
   135
{
insilmaril@678
   136
	attributeTable=table;
insilmaril@678
   137
}
insilmaril@678
   138