Subversion Repositories svn1

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
141 david 1
/*============================================================================
2
**
3
**  Project/Product : 
4
**  Filename        : textdelegate.h
5
**  Author(s)       : DDP
6
**
7
**  Description     : A delgated widget to assist in the editing of text items
8
**                    within a table
9
**
10
**
11
***==========================================================================*/
111 david 12
#include <QtGui>
384 david 13
#include <QLineEdit>
111 david 14
#include "textdelegate.h"
15
 
141 david 16
/*----------------------------------------------------------------------------
17
** FUNCTION           : textDelegate
18
**
19
** DESCRIPTION        : Construct a new object
20
**
21
**                      A delegate that allows the user to change text
22
**                      values from the model using a lineEditor widget.
23
**
24
** INPUTS             : maxlen      - Max length of text allowed
25
**                      parent      - Parent object
26
**
27
** RETURNS            :
28
**
29
----------------------------------------------------------------------------*/
111 david 30
 
31
textDelegate::textDelegate(int maxlen, QObject *parent)
32
    : QItemDelegate(parent)
33
{
34
    max_len = maxlen;
35
}
36
 
141 david 37
/*----------------------------------------------------------------------------
38
** FUNCTION           : createEditor
39
**
40
** DESCRIPTION        : Create an editor widget
41
**
42
**
43
** INPUTS             : parent
44
**                      option
45
**                      index
46
**
47
** RETURNS            : A QLineEdit widget
48
**
49
----------------------------------------------------------------------------*/
50
 
111 david 51
QWidget *textDelegate::createEditor(QWidget *parent,
52
    const QStyleOptionViewItem &/* option */,
53
    const QModelIndex &/* index */) const
54
{
55
    QLineEdit *editor = new QLineEdit(parent);
56
    editor->setMaxLength(max_len);
57
 
58
    return editor;
59
}
60
 
141 david 61
/*----------------------------------------------------------------------------
62
** FUNCTION           : setEditorData
63
**
64
** DESCRIPTION        : Inserts model data into the editor widget
65
**
66
**
67
** INPUTS             : editor      - Ref to editor widget
68
**                      index       -
69
**
70
** RETURNS            :
71
**
72
----------------------------------------------------------------------------*/
73
 
111 david 74
void textDelegate::setEditorData(QWidget *editor,
75
                                    const QModelIndex &index) const
76
{
77
    QString value = index.model()->data(index, Qt::EditRole).toString();
78
 
79
    QLineEdit *lineEditor = static_cast<QLineEdit*>(editor);
80
    lineEditor->setText(value);
81
}
82
 
141 david 83
/*----------------------------------------------------------------------------
84
** FUNCTION           : setModelData
85
**
86
** DESCRIPTION        : Extract data from the editor widget and update the model
87
**
88
**
89
** INPUTS             : editor      - Target widget
90
**                      model       - Current model
91
**                      index       - Index into model
92
**
93
** RETURNS            :
94
**
95
----------------------------------------------------------------------------*/
111 david 96
 
97
void textDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
98
                                   const QModelIndex &index) const
99
{
100
    QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
101
    QString value = lineEdit->text();
102
 
103
    model->setData(index, value, Qt::EditRole);
104
}
105
 
141 david 106
/*----------------------------------------------------------------------------
107
** FUNCTION           : updateEditorGeometry
108
**
109
** DESCRIPTION        : Updates the geometry of the editor widget
110
**
111
**
112
** INPUTS             : editor      - Target widget
113
**                      option      -
114
*                       index
115
**
116
** RETURNS            :
117
**
118
----------------------------------------------------------------------------*/
119
 
111 david 120
void textDelegate::updateEditorGeometry(QWidget *editor,
121
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
122
{
123
    editor->setGeometry(option.rect);
124
}
125
 
126