Хранилища Subversion OpenInventory

Редакция

Редакция 129 | Редакция 138 | К новейшей редакции | Только различия | Не учитывать пробелы | Содержимое файла | Авторство | Последнее изменение | Открыть журнал | RSS

Редакция 129 Редакция 136
1
1
2
2
3
/****************************************************************************
3
/****************************************************************************
4
**
4
**
5
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
5
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
6
** All rights reserved.
6
** All rights reserved.
7
** Contact: Nokia Corporation (qt-info@nokia.com)
7
** Contact: Nokia Corporation (qt-info@nokia.com)
8
**
8
**
9
** This file is part of the examples of the Qt Toolkit.
9
** This file is part of the examples of the Qt Toolkit.
10
**
10
**
11
** $QT_BEGIN_LICENSE:LGPL$
11
** $QT_BEGIN_LICENSE:LGPL$
12
** Commercial Usage
12
** Commercial Usage
13
** Licensees holding valid Qt Commercial licenses may use this file in
13
** Licensees holding valid Qt Commercial licenses may use this file in
14
** accordance with the Qt Commercial License Agreement provided with the
14
** accordance with the Qt Commercial License Agreement provided with the
15
** Software or, alternatively, in accordance with the terms contained in
15
** Software or, alternatively, in accordance with the terms contained in
16
** a written agreement between you and Nokia.
16
** a written agreement between you and Nokia.
17
**
17
**
18
** GNU Lesser General Public License Usage
18
** GNU Lesser General Public License Usage
19
** Alternatively, this file may be used under the terms of the GNU Lesser
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
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
21
** Foundation and appearing in the file LICENSE.LGPL included in the
22
** packaging of this file.  Please review the following information to
22
** packaging of this file.  Please review the following information to
23
** ensure the GNU Lesser General Public License version 2.1 requirements
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.
24
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25
**
25
**
26
** In addition, as a special exception, Nokia gives you certain additional
26
** In addition, as a special exception, Nokia gives you certain additional
27
** rights.  These rights are described in the Nokia Qt LGPL Exception
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.
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
**
29
**
30
** GNU General Public License Usage
30
** GNU General Public License Usage
31
** Alternatively, this file may be used under the terms of the GNU
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
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
33
** Foundation and appearing in the file LICENSE.GPL included in the
34
** packaging of this file.  Please review the following information to
34
** packaging of this file.  Please review the following information to
35
** ensure the GNU General Public License version 3.0 requirements will be
35
** ensure the GNU General Public License version 3.0 requirements will be
36
** met: http://www.gnu.org/copyleft/gpl.html.
36
** met: http://www.gnu.org/copyleft/gpl.html.
37
**
37
**
38
** If you have questions regarding the use of this file, please contact
38
** If you have questions regarding the use of this file, please contact
39
** Nokia at qt-info@nokia.com.
39
** Nokia at qt-info@nokia.com.
40
** $QT_END_LICENSE$
40
** $QT_END_LICENSE$
41
**
41
**
42
****************************************************************************/
42
****************************************************************************/
43
43
44
/*
44
/*
45
    delegate.cpp
45
    delegate.cpp
46

46

47
    A delegate that allows the user to change integer values from the model
47
    A delegate that allows the user to change integer values from the model
48
    using a spin box widget.
48
    using a spin box widget.
49
*/
49
*/
50
50
51
#include <QtGui>
51
#include <QtGui>
52
52
53
#include "delegate.h"
53
#include "delegate.h"
54
54
55
55
56
//! [0]
56
//! [0]
57
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
57
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
58
    : QItemDelegate(parent)
58
    : QItemDelegate(parent)
59
{
59
{
60
}
60
}
61
//! [0]
61
//! [0]
62
62
63
void SpinBoxDelegate::setInctance(QString newInctance){
63
void SpinBoxDelegate::setInctance(QString newInctance){
64
    inctance = newInctance;
64
    inctance = newInctance;
65
}
65
}
66
66
67
void SpinBoxDelegate::getItems(){
67
void SpinBoxDelegate::getItems(){
68
    QMap <QString, QString> result_map;
68
    QMap <QString, QString> result_map;
69
    QSqlQuery q;
69
    QSqlQuery q;
70
    QString query_str;
70
    QString query_str;
71
    bool ok;
71
    bool ok;
72
72
73
    if (SpinBoxDelegate::inctance.isEmpty())
73
    if (SpinBoxDelegate::inctance.isEmpty())
74
    {
74
    {
75
        items.clear();
75
        items.clear();
76
        return;
76
        return;
77
    }
77
    }
78
78
79
    query_str = tr("select * from `");
79
    query_str = tr("select * from `");
80
    query_str.append(inctance);
80
    query_str.append(inctance);
81
    query_str.append( tr("`"));
81
    query_str.append( tr("`"));
82
82
83
    q.prepare(query_str);
83
    q.prepare(query_str);
84
84
85
85
86
    ok = q.exec();
86
    ok = q.exec();
87
87
88
88
89
89
90
90
91
    if (!ok) {
91
    if (!ok) {
92
                            /*
92
                            /*
93
                            QString error_str;
93
                            QString error_str;
94
                            error_str =  tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
94
                            error_str =  tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
95
                            error_str.append(inctance);
95
                            error_str.append(inctance);
96
                            QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
96
                            QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
97
                                                                            this,                      // Ðîäèòåëüñêèé âèäæåò.
97
                                                                            this,                      // Ðîäèòåëüñêèé âèäæåò.
98
                                                                            QObject::tr("Database Error"),   // Çàãîëîâîê.
98
                                                                            QObject::tr("Database Error"),   // Çàãîëîâîê.
99
                                                                            q.lastError().text());          // Òåêñò ñîîáùåíèÿ.
99
                                                                            q.lastError().text());          // Òåêñò ñîîáùåíèÿ.
100
                                                                        //    tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû "));    // Òåêñò ñîîáùåíèÿ.
100
                                                                        //    tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû "));    // Òåêñò ñîîáùåíèÿ.
101

101

102
                            */
102
                            */
103
                            items.clear();
103
                            items.clear();
104
                            return;
104
                            return;
105
                        }
105
                        }
106
106
107
107
108
 //   field_inctance = q.record().indexOf(tr("TableWhithInstance"));
108
 //   field_inctance = q.record().indexOf(tr("TableWhithInstance"));
109
    items.clear();
109
    items.clear();
110
    while(q.next()){
110
    while(q.next()){
111
111
112
                  QString ID;
112
                  QString ID;
113
                  QString Name;
113
                  QString Name;
114
                  ID = q.value(0).toString(); // îïðåäåëÿåì ID
114
                  ID = q.value(0).toString(); // îïðåäåëÿåì ID
115
                  Name = q.value(1).toString(); // îïðåäåëÿåì Name
115
                  Name = q.value(1).toString(); // îïðåäåëÿåì Name
116
                  items.insert(ID, Name);
116
                  items.insert(ID, Name);
117
                }
117
                }
118
118
119
119
120
 //   return result_map;
120
 //   return result_map;
121
}
121
}
122
122
123
123
124
//! [1]
124
//! [1]
125
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
125
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
126
    const QStyleOptionViewItem &/* option */,
126
    const QStyleOptionViewItem &/* option */,
127
    const QModelIndex &/* index */) const
127
    const QModelIndex &/* index */) const
128
{
128
{
129
    //QSpinBox *editor = new QSpinBox(parent);
129
    //QSpinBox *editor = new QSpinBox(parent);
130
    //editor->setMinimum(0);
130
    //editor->setMinimum(0);
131
    //editor->setMaximum(100);
131
    //editor->setMaximum(100);
132
    QStringList ID_list;
132
    QStringList ID_list;
133
    QStringList Name_list;
133
    QStringList Name_list;
134
134
135
  // getItems();
135
  // getItems();
136
136
137
137
138
138
139
139
140
140
141
    ID_list = items.keys();
141
    ID_list = items.keys();
142
    Name_list = items.values();
142
    Name_list = items.values();
143
143
144
    QComboBox *editor = new QComboBox(parent);
144
    QComboBox *editor = new QComboBox(parent);
145
145
146
/*
146
/*
147
    editor->addItem("0");
147
    editor->addItem("0");
148
    editor->addItem("1");
148
    editor->addItem("1");
149
    editor->addItem("2");
149
    editor->addItem("2");
150
    editor->addItem("3");
150
    editor->addItem("3");
151
    editor->addItem("4");
151
    editor->addItem("4");
152
    editor->addItem("5");
152
    editor->addItem("5");
153
    editor->addItem("6");
153
    editor->addItem("6");
154
    editor->addItem("7");
154
    editor->addItem("7");
155
    editor->addItem("8");
155
    editor->addItem("8");
156
    editor->addItem("9");
156
    editor->addItem("9");
157
*/
157
*/
158
158
159
//  editor->addItem("0");
159
//  editor->addItem("0");
160
160
161
    editor->addItems(Name_list);
161
    editor->addItems(Name_list);
162
    return editor;
162
    return editor;
163
}
163
}
164
//! [1]
164
//! [1]
165
165
166
//! [2]
166
//! [2]
167
void SpinBoxDelegate::setEditorData(QWidget *editor,
167
void SpinBoxDelegate::setEditorData(QWidget *editor,
168
                                    const QModelIndex &index) const
168
                                    const QModelIndex &index) const
169
{
169
{
170
   // int value = index.model()->data(index, Qt::EditRole).toInt();
170
   // int value = index.model()->data(index, Qt::EditRole).toInt();
171
171
172
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
172
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
173
 //   spinBox->setValue(value);
173
 //   spinBox->setValue(value);
174
QString value = index.model()->data(index, Qt::EditRole).toString();
174
QString value = index.model()->data(index, Qt::EditRole).toString();
-
 
175
175
QComboBox *comboBox = static_cast<QComboBox*>(editor);
176
QComboBox *comboBox = static_cast<QComboBox*>(editor);
176
177
177
QStringList ID_list;
178
QStringList ID_list;
178
QStringList Name_list;
179
QStringList Name_list;
179
int curr_index;
180
int curr_index;
180
181
181
ID_list = items.keys();
182
ID_list = items.keys();
182
Name_list = items.values();
183
Name_list = items.values();
183
184
184
curr_index = ID_list.indexOf(value);
185
curr_index = ID_list.indexOf(value);
-
 
186
if (curr_index==-1) return;
185
comboBox->setCurrentIndex(curr_index);
187
comboBox->setCurrentIndex(curr_index);
186
/****************************************************
188
/****************************************************
187
if (value == "0") comboBox->setCurrentIndex(0);
189
if (value == "0") comboBox->setCurrentIndex(0);
188
if (value == "1") comboBox->setCurrentIndex(1);
190
if (value == "1") comboBox->setCurrentIndex(1);
189
if (value == "2") comboBox->setCurrentIndex(2);
191
if (value == "2") comboBox->setCurrentIndex(2);
190
if (value == "3") comboBox->setCurrentIndex(3);
192
if (value == "3") comboBox->setCurrentIndex(3);
191
if (value == "4") comboBox->setCurrentIndex(4);
193
if (value == "4") comboBox->setCurrentIndex(4);
192
if (value == "5") comboBox->setCurrentIndex(5);
194
if (value == "5") comboBox->setCurrentIndex(5);
193
if (value == "6") comboBox->setCurrentIndex(6);
195
if (value == "6") comboBox->setCurrentIndex(6);
194
if (value == "7") comboBox->setCurrentIndex(7);
196
if (value == "7") comboBox->setCurrentIndex(7);
195
if (value == "8") comboBox->setCurrentIndex(8);
197
if (value == "8") comboBox->setCurrentIndex(8);
196
if (value == "9") comboBox->setCurrentIndex(9);
198
if (value == "9") comboBox->setCurrentIndex(9);
197
******************************************************/
199
******************************************************/
198
comboBox->setEditable(true);
200
comboBox->setEditable(true);
199
201
200
202
201
//comboBox->setItemText(0, value);
203
//comboBox->setItemText(0, value);
202
204
203
}
205
}
204
//! [2]
206
//! [2]
205
207
206
//! [3]
208
//! [3]
207
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
209
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
208
                                   const QModelIndex &index) const
210
                                   const QModelIndex &index) const
209
{
211
{
210
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
212
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
211
  //  spinBox->interpretText();
213
  //  spinBox->interpretText();
212
  //  int value = spinBox->value();
214
  //  int value = spinBox->value();
213
215
214
  //  model->setData(index, value, Qt::EditRole);
216
  //  model->setData(index, value, Qt::EditRole);
215
    QStringList ID_list;
217
    QStringList ID_list;
216
    QStringList Name_list;
218
    QStringList Name_list;
217
219
218
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
220
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
219
    int currIndex;
221
    int currIndex;
220
    currIndex = comboBox->currentIndex();
222
    currIndex = comboBox->currentIndex();
-
 
223
    if (currIndex==-1) return;
221
  //  QString value = comboBox->itemText(currIndex);
224
  //  QString value = comboBox->itemText(currIndex);
222
225
223
    ID_list = items.keys();
226
    ID_list = items.keys();
224
    Name_list = items.values();
227
    Name_list = items.values();
225
228
226
    QString value = ID_list.at(currIndex);
229
    QString value = ID_list.at(currIndex);
227
    model->setData(index, value, Qt::EditRole);
230
    model->setData(index, value, Qt::EditRole);
228
}
231
}
229
//! [3]
232
//! [3]
230
233
231
//! [4]
234
//! [4]
232
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
235
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
233
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
236
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
234
{
237
{
235
    editor->setGeometry(option.rect);
238
    editor->setGeometry(option.rect);
236
}
239
}
237
//! [4]
240
//! [4]
238
MyDEDelegate::MyDEDelegate(
241
MyDEDelegate::MyDEDelegate(
239
                       bool calpopup,
242
                       bool calpopup,
240
                       QObject *parent)
243
                       QObject *parent)
241
               : QItemDelegate(parent),
244
               : QItemDelegate(parent),
242
                 m_calpopup(calpopup) {
245
                 m_calpopup(calpopup) {
243
   }
246
   }
244
247
245
   QWidget *MyDEDelegate::createEditor(
248
   QWidget *MyDEDelegate::createEditor(
246
               QWidget *parent,
249
               QWidget *parent,
247
               const QStyleOptionViewItem& /* option */,
250
               const QStyleOptionViewItem& /* option */,
248
               const QModelIndex& /* index */) const {
251
               const QModelIndex& /* index */) const {
249
       QDateEdit *editor = new QDateEdit(parent);
252
       QDateEdit *editor = new QDateEdit(parent);
250
       editor->setCalendarPopup(m_calpopup);
253
       editor->setCalendarPopup(m_calpopup);
251
       editor->installEventFilter(const_cast<MyDEDelegate*>(this));
254
       editor->installEventFilter(const_cast<MyDEDelegate*>(this));
252
       return editor;
255
       return editor;
253
   }
256
   }
254
257
255
   void MyDEDelegate::setEditorData(
258
   void MyDEDelegate::setEditorData(
256
                   QWidget *editor,
259
                   QWidget *editor,
257
                   const QModelIndex &index) const {
260
                   const QModelIndex &index) const {
258
       QDate value = index.model()->data(
261
       QDate value = index.model()->data(
259
               index, Qt::EditRole).toDate();
262
               index, Qt::EditRole).toDate();
260
       QDateEdit *de = static_cast<QDateEdit*>(editor);
263
       QDateEdit *de = static_cast<QDateEdit*>(editor);
261
       de->setDate(value);
264
       de->setDate(value);
262
   }
265
   }
263
266
264
   void MyDEDelegate::setModelData(
267
   void MyDEDelegate::setModelData(
265
               QWidget *editor,
268
               QWidget *editor,
266
               QAbstractItemModel *model,
269
               QAbstractItemModel *model,
267
               const QModelIndex& index) const {
270
               const QModelIndex& index) const {
268
       QDateEdit *de = static_cast<QDateEdit*>(editor);
271
       QDateEdit *de = static_cast<QDateEdit*>(editor);
269
       de->interpretText();
272
       de->interpretText();
270
       QDate value = de->date();
273
       QDate value = de->date();
271
       model->setData(index, value);
274
       model->setData(index, value);
272
   }
275
   }
273
276
274
   void MyDEDelegate::updateEditorGeometry(
277
   void MyDEDelegate::updateEditorGeometry(
275
               QWidget *editor,
278
               QWidget *editor,
276
               const QStyleOptionViewItem &option,
279
               const QStyleOptionViewItem &option,
277
               const QModelIndex& /* index */) const {
280
               const QModelIndex& /* index */) const {
278
       editor->setGeometry(option.rect);
281
       editor->setGeometry(option.rect);
279
   }
282
   }
280
 
283