Subversion Repositories svn1-original

Rev

Rev 115 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 115 Rev 145
Line -... Line 1...
-
 
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
***==========================================================================*/
1
#include <QtGui>
12
#include <QtGui>
2
#include "textdelegate.h"
13
#include "textdelegate.h"
3
 
14
 
-
 
15
/*----------------------------------------------------------------------------
-
 
16
** FUNCTION           : textDelegate
4
/*
17
**
5
    textdelegate.cpp
18
** DESCRIPTION        : Construct a new object
6
 
19
**
7
    A delegate that allows the user to change integer values from the model
20
**                      A delegate that allows the user to change text
8
    using a line edit widget.
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            :
9
*/
27
**
-
 
28
----------------------------------------------------------------------------*/
10
 
29
 
11
textDelegate::textDelegate(int maxlen, QObject *parent)
30
textDelegate::textDelegate(int maxlen, QObject *parent)
12
    : QItemDelegate(parent)
31
    : QItemDelegate(parent)
13
{
32
{
14
    max_len = maxlen;
33
    max_len = maxlen;
15
}
34
}
16
 
35
 
-
 
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
 
17
QWidget *textDelegate::createEditor(QWidget *parent,
50
QWidget *textDelegate::createEditor(QWidget *parent,
18
    const QStyleOptionViewItem &/* option */,
51
    const QStyleOptionViewItem &/* option */,
19
    const QModelIndex &/* index */) const
52
    const QModelIndex &/* index */) const
20
{
53
{
21
    QLineEdit *editor = new QLineEdit(parent);
54
    QLineEdit *editor = new QLineEdit(parent);
22
    editor->setMaxLength(max_len);
55
    editor->setMaxLength(max_len);
23
 
56
 
24
    return editor;
57
    return editor;
25
}
58
}
26
 
59
 
-
 
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
 
27
void textDelegate::setEditorData(QWidget *editor,
73
void textDelegate::setEditorData(QWidget *editor,
28
                                    const QModelIndex &index) const
74
                                    const QModelIndex &index) const
29
{
75
{
30
    QString value = index.model()->data(index, Qt::EditRole).toString();
76
    QString value = index.model()->data(index, Qt::EditRole).toString();
31
 
77
 
32
    QLineEdit *lineEditor = static_cast<QLineEdit*>(editor);
78
    QLineEdit *lineEditor = static_cast<QLineEdit*>(editor);
33
    lineEditor->setText(value);
79
    lineEditor->setText(value);
34
}
80
}
35
 
81
 
-
 
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
----------------------------------------------------------------------------*/
36
 
95
 
37
void textDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
96
void textDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
38
                                   const QModelIndex &index) const
97
                                   const QModelIndex &index) const
39
{
98
{
40
    QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
99
    QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
41
    QString value = lineEdit->text();
100
    QString value = lineEdit->text();
42
 
101
 
43
    model->setData(index, value, Qt::EditRole);
102
    model->setData(index, value, Qt::EditRole);
44
}
103
}
45
 
104
 
-
 
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
 
46
void textDelegate::updateEditorGeometry(QWidget *editor,
119
void textDelegate::updateEditorGeometry(QWidget *editor,
47
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
120
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
48
{
121
{
49
    editor->setGeometry(option.rect);
122
    editor->setGeometry(option.rect);
50
}
123
}