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