Subversion Repositories OpenInventory

Rev

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

Rev Author Line No. Line
123 pingvin 1
 
2
 
3
/****************************************************************************
4
**
5
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
** All rights reserved.
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
**
9
** This file is part of the examples of the Qt Toolkit.
10
**
11
** $QT_BEGIN_LICENSE:LGPL$
12
** Commercial Usage
13
** Licensees holding valid Qt Commercial licenses may use this file in
14
** accordance with the Qt Commercial License Agreement provided with the
15
** Software or, alternatively, in accordance with the terms contained in
16
** a written agreement between you and Nokia.
17
**
18
** GNU Lesser General Public License Usage
19
** Alternatively, this file may be used under the terms of the GNU Lesser
20
** General Public License version 2.1 as published by the Free Software
21
** Foundation and appearing in the file LICENSE.LGPL included in the
22
** packaging of this file.  Please review the following information to
23
** ensure the GNU Lesser General Public License version 2.1 requirements
24
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
**
26
** In addition, as a special exception, Nokia gives you certain additional
27
** rights.  These rights are described in the Nokia Qt LGPL Exception
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
**
30
** GNU General Public License Usage
31
** Alternatively, this file may be used under the terms of the GNU
32
** General Public License version 3.0 as published by the Free Software
33
** Foundation and appearing in the file LICENSE.GPL included in the
34
** packaging of this file.  Please review the following information to
35
** ensure the GNU General Public License version 3.0 requirements will be
36
** met: http://www.gnu.org/copyleft/gpl.html.
37
**
38
** If you have questions regarding the use of this file, please contact
39
** Nokia at qt-info@nokia.com.
40
** $QT_END_LICENSE$
41
**
42
****************************************************************************/
43
 
44
/*
45
    delegate.cpp
46
 
47
    A delegate that allows the user to change integer values from the model
48
    using a spin box widget.
49
*/
50
 
51
#include <QtGui>
52
 
53
#include "delegate.h"
54
 
55
 
56
//! [0]
57
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
58
    : QItemDelegate(parent)
59
{
60
}
61
//! [0]
62
 
63
//! [1]
64
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
65
    const QStyleOptionViewItem &/* option */,
66
    const QModelIndex &/* index */) const
67
{
68
    //QSpinBox *editor = new QSpinBox(parent);
69
    //editor->setMinimum(0);
70
    //editor->setMaximum(100);
71
    QComboBox *editor = new QComboBox(parent);
72
    editor->addItem("0");
73
    editor->addItem("1");
74
    editor->addItem("2");
75
    editor->addItem("3");
76
    editor->addItem("4");
77
    editor->addItem("5");
78
    editor->addItem("6");
79
    editor->addItem("7");
80
    editor->addItem("8");
81
    editor->addItem("9");
82
  //  editor->addItem("0");
83
 
84
    return editor;
85
}
86
//! [1]
87
 
88
//! [2]
89
void SpinBoxDelegate::setEditorData(QWidget *editor,
90
                                    const QModelIndex &index) const
91
{
92
   // int value = index.model()->data(index, Qt::EditRole).toInt();
93
 
94
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
95
 //   spinBox->setValue(value);
96
QString value = index.model()->data(index, Qt::EditRole).toString();
97
QComboBox *comboBox = static_cast<QComboBox*>(editor);
98
if (value == "0") comboBox->setCurrentIndex(0);
99
if (value == "1") comboBox->setCurrentIndex(1);
100
if (value == "2") comboBox->setCurrentIndex(2);
101
if (value == "3") comboBox->setCurrentIndex(3);
102
if (value == "4") comboBox->setCurrentIndex(4);
103
if (value == "5") comboBox->setCurrentIndex(5);
104
if (value == "6") comboBox->setCurrentIndex(6);
105
if (value == "7") comboBox->setCurrentIndex(7);
106
if (value == "8") comboBox->setCurrentIndex(8);
107
if (value == "9") comboBox->setCurrentIndex(9);
108
comboBox->setEditable(true);
109
 
110
 
111
//comboBox->setItemText(0, value);
112
 
113
}
114
//! [2]
115
 
116
//! [3]
117
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
118
                                   const QModelIndex &index) const
119
{
120
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
121
  //  spinBox->interpretText();
122
  //  int value = spinBox->value();
123
 
124
  //  model->setData(index, value, Qt::EditRole);
125
 
126
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
127
    int currIndex;
128
    currIndex = comboBox->currentIndex();
129
    QString value = comboBox->itemText(currIndex);
130
    model->setData(index, value, Qt::EditRole);
131
}
132
//! [3]
133
 
134
//! [4]
135
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
136
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
137
{
138
    editor->setGeometry(option.rect);
139
}
140
//! [4]
141
MyDEDelegate::MyDEDelegate(
142
                       bool calpopup,
143
                       QObject *parent)
144
               : QItemDelegate(parent),
145
                 m_calpopup(calpopup) {
146
   }
147
 
148
   QWidget *MyDEDelegate::createEditor(
149
               QWidget *parent,
150
               const QStyleOptionViewItem& /* option */,
151
               const QModelIndex& /* index */) const {
152
       QDateEdit *editor = new QDateEdit(parent);
153
       editor->setCalendarPopup(m_calpopup);
154
       editor->installEventFilter(const_cast<MyDEDelegate*>(this));
155
       return editor;
156
   }
157
 
158
   void MyDEDelegate::setEditorData(
159
                   QWidget *editor,
160
                   const QModelIndex &index) const {
161
       QDate value = index.model()->data(
162
               index, Qt::EditRole).toDate();
163
       QDateEdit *de = static_cast<QDateEdit*>(editor);
164
       de->setDate(value);
165
   }
166
 
167
   void MyDEDelegate::setModelData(
168
               QWidget *editor,
169
               QAbstractItemModel *model,
170
               const QModelIndex& index) const {
171
       QDateEdit *de = static_cast<QDateEdit*>(editor);
172
       de->interpretText();
173
       QDate value = de->date();
174
       model->setData(index, value);
175
   }
176
 
177
   void MyDEDelegate::updateEditorGeometry(
178
               QWidget *editor,
179
               const QStyleOptionViewItem &option,
180
               const QModelIndex& /* index */) const {
181
       editor->setGeometry(option.rect);
182
   }