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