Subversion Repositories svn1

Rev

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

Rev 110 Rev 141
Line 1... Line 1...
1
/****************************************************************************
1
/*============================================================================
2
**
2
**
3
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
-
 
4
** All rights reserved.
3
**  Project/Product : 
5
** Contact: Nokia Corporation (qt-info@nokia.com)
-
 
6
**
-
 
7
** This file is part of the examples of the Qt Toolkit.
4
**  Filename        : spinboxdelegate.h
8
**
-
 
9
** $QT_BEGIN_LICENSE:LGPL$
5
**  Author(s)       : DDP
10
** Commercial Usage
-
 
11
** Licensees holding valid Qt Commercial licenses may use this file in
-
 
12
** accordance with the Qt Commercial License Agreement provided with the
-
 
13
** Software or, alternatively, in accordance with the terms contained in
-
 
14
** a written agreement between you and Nokia.
-
 
15
**
-
 
16
** GNU Lesser General Public License Usage
-
 
17
** Alternatively, this file may be used under the terms of the GNU Lesser
-
 
18
** General Public License version 2.1 as published by the Free Software
-
 
19
** Foundation and appearing in the file LICENSE.LGPL included in the
-
 
20
** packaging of this file.  Please review the following information to
-
 
21
** ensure the GNU Lesser General Public License version 2.1 requirements
-
 
22
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
-
 
23
**
6
**
24
** In addition, as a special exception, Nokia gives you certain additional
7
**  Description     : A delgated widget to assist in the editing of time items
25
** rights.  These rights are described in the Nokia Qt LGPL Exception
-
 
26
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
-
 
27
**
-
 
28
** GNU General Public License Usage
8
**                    within a table
29
** Alternatively, this file may be used under the terms of the GNU
-
 
30
** General Public License version 3.0 as published by the Free Software
-
 
31
** Foundation and appearing in the file LICENSE.GPL included in the
-
 
32
** packaging of this file.  Please review the following information to
-
 
33
** ensure the GNU General Public License version 3.0 requirements will be
-
 
34
** met: http://www.gnu.org/copyleft/gpl.html.
-
 
35
**
9
**
36
** If you have questions regarding the use of this file, please contact
-
 
37
** Nokia at qt-info@nokia.com.
-
 
38
** $QT_END_LICENSE$
-
 
39
**
10
**
40
****************************************************************************/
11
***==========================================================================*/
41
 
-
 
42
/*
-
 
43
    spinboxdelegate.cpp
-
 
44
 
-
 
45
    A delegate that allows the user to change integer values from the model
-
 
46
    using a spin box widget.
-
 
47
*/
-
 
48
 
12
 
49
#include <QtGui>
13
#include <QtGui>
50
 
-
 
51
#include "spinboxdelegate.h"
14
#include "spinboxdelegate.h"
52
 
15
 
-
 
16
/*----------------------------------------------------------------------------
-
 
17
** FUNCTION           : SpinBoxDelegate
-
 
18
**
-
 
19
** DESCRIPTION        : Construct a new object
-
 
20
**
-
 
21
**                      A delegate that allows the user to change integer
-
 
22
**                      values from the model using a date time widget.
-
 
23
**
-
 
24
** INPUTS             : minvalue
-
 
25
**                      maxvalue
-
 
26
**                      parent      - Parent object
-
 
27
**
-
 
28
** RETURNS            :
-
 
29
**
-
 
30
----------------------------------------------------------------------------*/
53
 
31
 
54
//! [0]
-
 
55
SpinBoxDelegate::SpinBoxDelegate(int minvalue, int maxvalue, QObject *parent)
32
SpinBoxDelegate::SpinBoxDelegate(int minvalue, int maxvalue, QObject *parent)
56
    : QItemDelegate(parent)
33
    : QItemDelegate(parent)
57
{
34
{
58
    min_value = minvalue;
35
    min_value = minvalue;
59
    max_value = maxvalue;
36
    max_value = maxvalue;
60
}
37
}
61
//! [0]
-
 
62
 
38
 
-
 
39
/*----------------------------------------------------------------------------
-
 
40
** FUNCTION           : createEditor
-
 
41
**
-
 
42
** DESCRIPTION        : Create an editor widget
-
 
43
**
63
//! [1]
44
**
-
 
45
** INPUTS             : parent
-
 
46
**                      option
-
 
47
**                      index
-
 
48
**
-
 
49
** RETURNS            : A QSpinBox widget
-
 
50
**
-
 
51
----------------------------------------------------------------------------*/
-
 
52
 
64
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
53
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
65
    const QStyleOptionViewItem &/* option */,
54
    const QStyleOptionViewItem &/* option */,
66
    const QModelIndex &/* index */) const
55
    const QModelIndex &/* index */) const
67
{
56
{
68
    QSpinBox *editor = new QSpinBox(parent);
57
    QSpinBox *editor = new QSpinBox(parent);
69
    editor->setMinimum(min_value);
58
    editor->setMinimum(min_value);
70
    editor->setMaximum(max_value);
59
    editor->setMaximum(max_value);
71
 
60
 
72
    return editor;
61
    return editor;
73
}
62
}
74
//! [1]
-
 
75
 
63
 
-
 
64
/*----------------------------------------------------------------------------
-
 
65
** FUNCTION           : setEditorData
-
 
66
**
-
 
67
** DESCRIPTION        : Inserts model data into the editor widget
-
 
68
**
76
//! [2]
69
**
-
 
70
** INPUTS             : editor      - Ref to editor widget
-
 
71
**                      index       -
-
 
72
**
-
 
73
** RETURNS            :
-
 
74
**
-
 
75
----------------------------------------------------------------------------*/
-
 
76
 
77
void SpinBoxDelegate::setEditorData(QWidget *editor,
77
void SpinBoxDelegate::setEditorData(QWidget *editor,
78
                                    const QModelIndex &index) const
78
                                    const QModelIndex &index) const
79
{
79
{
80
    int value = index.model()->data(index, Qt::EditRole).toInt();
80
    int value = index.model()->data(index, Qt::EditRole).toInt();
81
 
81
 
82
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
82
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
83
    spinBox->setValue(value);
83
    spinBox->setValue(value);
84
}
84
}
85
//! [2]
-
 
86
 
85
 
-
 
86
/*----------------------------------------------------------------------------
-
 
87
** FUNCTION           : setModelData
-
 
88
**
-
 
89
** DESCRIPTION        : Extract data from the editor widget and update the model
-
 
90
**
87
//! [3]
91
**
-
 
92
** INPUTS             : editor      - Target widget
-
 
93
**                      model       - Current model
-
 
94
**                      index       - Index into model
-
 
95
**
-
 
96
** RETURNS            :
-
 
97
**
-
 
98
----------------------------------------------------------------------------*/
-
 
99
 
88
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
100
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
89
                                   const QModelIndex &index) const
101
                                   const QModelIndex &index) const
90
{
102
{
91
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
103
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
92
    spinBox->interpretText();
104
    spinBox->interpretText();
93
    int value = spinBox->value();
105
    int value = spinBox->value();
94
 
106
 
95
    model->setData(index, value, Qt::EditRole);
107
    model->setData(index, value, Qt::EditRole);
96
}
108
}
97
//! [3]
-
 
98
 
109
 
-
 
110
/*----------------------------------------------------------------------------
-
 
111
** FUNCTION           : updateEditorGeometry
-
 
112
**
-
 
113
** DESCRIPTION        : Updates the geometry of the editor widget
-
 
114
**
99
//! [4]
115
**
-
 
116
** INPUTS             : editor      - Target widget
-
 
117
**                      option      -
-
 
118
*                       index
-
 
119
**
-
 
120
** RETURNS            :
-
 
121
**
-
 
122
----------------------------------------------------------------------------*/
-
 
123
 
100
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
124
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
101
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
125
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
102
{
126
{
103
    editor->setGeometry(option.rect);
127
    editor->setGeometry(option.rect);
104
}
128
}
105
//! [4]
129