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