/*============================================================================ ** ** Project/Product : ** Filename : textdelegate.h ** Author(s) : DDP ** ** Description : A delgated widget to assist in the editing of text items ** within a table ** ** ***==========================================================================*/ #include #include "textdelegate.h" /*---------------------------------------------------------------------------- ** FUNCTION : textDelegate ** ** DESCRIPTION : Construct a new object ** ** A delegate that allows the user to change text ** values from the model using a lineEditor widget. ** ** INPUTS : maxlen - Max length of text allowed ** parent - Parent object ** ** RETURNS : ** ----------------------------------------------------------------------------*/ textDelegate::textDelegate(int maxlen, QObject *parent) : QItemDelegate(parent) { max_len = maxlen; } /*---------------------------------------------------------------------------- ** FUNCTION : createEditor ** ** DESCRIPTION : Create an editor widget ** ** ** INPUTS : parent ** option ** index ** ** RETURNS : A QLineEdit widget ** ----------------------------------------------------------------------------*/ QWidget *textDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QLineEdit *editor = new QLineEdit(parent); editor->setMaxLength(max_len); return editor; } /*---------------------------------------------------------------------------- ** FUNCTION : setEditorData ** ** DESCRIPTION : Inserts model data into the editor widget ** ** ** INPUTS : editor - Ref to editor widget ** index - ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void textDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const { QString value = index.model()->data(index, Qt::EditRole).toString(); QLineEdit *lineEditor = static_cast(editor); lineEditor->setText(value); } /*---------------------------------------------------------------------------- ** FUNCTION : setModelData ** ** DESCRIPTION : Extract data from the editor widget and update the model ** ** ** INPUTS : editor - Target widget ** model - Current model ** index - Index into model ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void textDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QLineEdit *lineEdit = static_cast(editor); QString value = lineEdit->text(); model->setData(index, value, Qt::EditRole); } /*---------------------------------------------------------------------------- ** FUNCTION : updateEditorGeometry ** ** DESCRIPTION : Updates the geometry of the editor widget ** ** ** INPUTS : editor - Target widget ** option - * index ** ** RETURNS : ** ----------------------------------------------------------------------------*/ void textDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &/* index */) const { editor->setGeometry(option.rect); }