Subversion Repositories svn1

Rev

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