Subversion Repositories OpenInventory

Rev

Rev 123 | Rev 129 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 123 Rev 126
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){
-
 
64
    inctance = newInctance;
-
 
65
}
-
 
66
-
 
67
void SpinBoxDelegate::getItems(){
-
 
68
    QMap <QString, QString> result_map;
-
 
69
    QSqlQuery q;
-
 
70
    QString query_str;
-
 
71
    bool ok;
-
 
72
-
 
73
    if (SpinBoxDelegate::inctance.isEmpty())
-
 
74
    {
-
 
75
        items.clear();
-
 
76
        return;
-
 
77
    }
-
 
78
-
 
79
    query_str = tr("select * from `");
-
 
80
    query_str.append(inctance);
-
 
81
    query_str.append( tr("`"));
-
 
82
-
 
83
    q.prepare(query_str);
-
 
84
-
 
85
-
 
86
    ok = q.exec();
-
 
87
-
 
88
-
 
89
-
 
90
-
 
91
    if (!ok) {
-
 
92
                            /*
-
 
93
                            QString error_str;
-
 
94
                            error_str =  tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû ");
-
 
95
                            error_str.append(inctance);
-
 
96
                            QMessageBox::critical( // Äèàëîã ñ ñîîáùåíèåì îá îøèáêå.
-
 
97
                                                                            this,                      // Ðîäèòåëüñêèé âèäæåò.
-
 
98
                                                                            QObject::tr("Database Error"),   // Çàãîëîâîê.
-
 
99
                                                                            q.lastError().text());          // Òåêñò ñîîáùåíèÿ.
-
 
100
                                                                        //    tr("íå óäàëîñü ïîëó÷èòü ñïèñîê îáúåêòîâ èç òàáëèöû "));    // Òåêñò ñîîáùåíèÿ.
-
 
101

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