Хранилища Subversion OpenInventory

Редакция

Редакция 134 | Редакция 165 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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
{
134 pingvin 59
  //  SpinBoxDelegate::getItems();
53 pingvin 60
}
61
//! [0]
62
 
63
//! [1]
64
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
65
    const QStyleOptionViewItem &/* option */,
66
    const QModelIndex &/* index */) const
67
{
134 pingvin 68
    QStringList ID_list;
69
    QStringList Name_list;
70
 
71
  // getItems();
72
 
73
 
74
 
75
 
76
 
77
    ID_list = items.keys();
78
    Name_list = items.values();
79
 
53 pingvin 80
    QComboBox *editor = new QComboBox(parent);
134 pingvin 81
 
82
/*
53 pingvin 83
    editor->addItem("0");
84
    editor->addItem("1");
85
    editor->addItem("2");
86
    editor->addItem("3");
87
    editor->addItem("4");
88
    editor->addItem("5");
89
    editor->addItem("6");
90
    editor->addItem("7");
91
    editor->addItem("8");
92
    editor->addItem("9");
134 pingvin 93
*/
53 pingvin 94
 
134 pingvin 95
//  editor->addItem("0");
96
 
97
    editor->addItems(Name_list);
53 pingvin 98
    return editor;
99
}
100
//! [1]
101
 
102
//! [2]
103
void SpinBoxDelegate::setEditorData(QWidget *editor,
104
                                    const QModelIndex &index) const
105
{
134 pingvin 106
    QString value = index.model()->data(index, Qt::EditRole).toString();
107
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
53 pingvin 108
 
134 pingvin 109
    QStringList ID_list;
110
    QStringList Name_list;
111
    int curr_index;
53 pingvin 112
 
134 pingvin 113
    ID_list = items.keys();
114
    Name_list = items.values();
53 pingvin 115
 
134 pingvin 116
    curr_index = ID_list.indexOf(value);
117
    comboBox->setCurrentIndex(curr_index);
118
    /****************************************************
119
    if (value == "0") comboBox->setCurrentIndex(0);
120
    if (value == "1") comboBox->setCurrentIndex(1);
121
    if (value == "2") comboBox->setCurrentIndex(2);
122
    if (value == "3") comboBox->setCurrentIndex(3);
123
    if (value == "4") comboBox->setCurrentIndex(4);
124
    if (value == "5") comboBox->setCurrentIndex(5);
125
    if (value == "6") comboBox->setCurrentIndex(6);
126
    if (value == "7") comboBox->setCurrentIndex(7);
127
    if (value == "8") comboBox->setCurrentIndex(8);
128
    if (value == "9") comboBox->setCurrentIndex(9);
129
    ******************************************************/
130
    comboBox->setEditable(true);
131
 
132
 
53 pingvin 133
//comboBox->setItemText(0, value);
134
 
135
}
136
//! [2]
137
 
138
//! [3]
139
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
140
                                   const QModelIndex &index) const
141
{
134 pingvin 142
    QStringList ID_list;
143
    QStringList Name_list;
53 pingvin 144
 
145
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
146
    int currIndex;
147
    currIndex = comboBox->currentIndex();
134 pingvin 148
  //  QString value = comboBox->itemText(currIndex);
149
 
150
    ID_list = items.keys();
151
    Name_list = items.values();
152
 
153
    QString value = ID_list.at(currIndex);
53 pingvin 154
    model->setData(index, value, Qt::EditRole);
134 pingvin 155
 
156
 
53 pingvin 157
}
158
//! [3]
159
 
160
//! [4]
161
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
162
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
163
{
164
    editor->setGeometry(option.rect);
165
}
166
//! [4]
134 pingvin 167
 
168
void SpinBoxDelegate::getItems(){
169
    QMap <QString, QString> result_map;
170
    QSqlQuery q;
171
    QString query_str;
172
    bool ok;
173
 
174
    query_str = tr("select * from `ListOfClasses`");
175
    q.prepare(query_str);
176
 
177
 
178
    ok = q.exec();
179
 
180
 
181
 
182
 
183
    if (!ok) {
184
                            /*
185
                            QString error_str;
186
                            error_str =  tr("       ");
187
                            error_str.append(inctance);
188
                            QMessageBox::critical( //     .
189
                                                                            this,                      //  .
190
                                                                            QObject::tr("Database Error"),   // .
191
                                                                            q.lastError().text());          //  .
192
                                                                        //    tr("       "));    //  .
193
 
194
                            */
195
                            items.clear();
196
                            return;
197
                        }
198
 
199
 
200
 //   field_inctance = q.record().indexOf(tr("TableWhithInstance"));
201
    items.clear();
202
    while(q.next()){
203
 
204
                  QString ID;
205
                  QString ClassName;
206
                  ID = q.value(0).toString(); //  ID
207
                  ClassName = q.value(1).toString(); //  Name
208
                  items.insert(ID, ClassName);
209
                }
210
 
211
 
212
 //   return result_map;
213
}
214
 
215
 
216
 
217
 
218
 
219
 
220
 
221
 
222
 
223
 
224
 
225
 
226
 
227
 
228
 
229
 
230
 
53 pingvin 231
MyDEDelegate::MyDEDelegate(
232
                       bool calpopup,
233
                       QObject *parent)
234
               : QItemDelegate(parent),
235
                 m_calpopup(calpopup) {
236
   }
237
 
238
   QWidget *MyDEDelegate::createEditor(
239
               QWidget *parent,
240
               const QStyleOptionViewItem& /* option */,
241
               const QModelIndex& /* index */) const {
242
       QDateEdit *editor = new QDateEdit(parent);
243
       editor->setCalendarPopup(m_calpopup);
244
       editor->installEventFilter(const_cast<MyDEDelegate*>(this));
245
       return editor;
246
   }
247
 
248
   void MyDEDelegate::setEditorData(
249
                   QWidget *editor,
250
                   const QModelIndex &index) const {
251
       QDate value = index.model()->data(
252
               index, Qt::EditRole).toDate();
253
       QDateEdit *de = static_cast<QDateEdit*>(editor);
254
       de->setDate(value);
255
   }
256
 
257
   void MyDEDelegate::setModelData(
258
               QWidget *editor,
259
               QAbstractItemModel *model,
260
               const QModelIndex& index) const {
261
       QDateEdit *de = static_cast<QDateEdit*>(editor);
262
       de->interpretText();
263
       QDate value = de->date();
264
       model->setData(index, value);
265
   }
266
 
267
   void MyDEDelegate::updateEditorGeometry(
268
               QWidget *editor,
269
               const QStyleOptionViewItem &option,
270
               const QModelIndex& /* index */) const {
271
       editor->setGeometry(option.rect);
272
   }
134 pingvin 273
 
274
 
161 pingvin 275
 
276
   CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent)
277
   {
278
   }
279
 
280
   void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
281
   {
282
           m_pxPicture.fill( QColor(Qt::white) );
283
 
284
           const QAbstractItemModel * model = index.model();
285
           QString sFileName = model->data( index, Qt::DisplayRole ).toString();
286
 
287
           QVariant currentImage = model->data(index,0);
288
 
289
 
290
           QByteArray bytes = currentImage.toByteArray();
291
     //       QImage image;
292
     //       image.loadFromData(bytes);
293
 
294
 
295
           if (currentImage.isValid()) {
296
                m_pxPicture.loadFromData(bytes);
297
           }
298
           else {
299
               //QItemDelegate::paint(painter, option, index);
300
           return;
301
           }
302
 
303
 
304
           /*
305
 
306
           if ( !sFileName.isEmpty() )
307
                   m_pxPicture.load( sFileName );
308
           else {
309
               //QItemDelegate::paint(painter, option, index);
310
           return;
311
           }
312
*/
313
 
314
 
315
           QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
316
                                                             ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
317
                                                             QPalette::Disabled;
318
 
319
            if (option.state & QStyle::State_Selected)
320
                    painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
321
 
322
           int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
323
           int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
324
           painter->drawPixmap( nX, nY, m_pxPicture );
325
 
326
 
327
       ////    drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
328
 
329
           /*
330
           QPen pen = painter->pen();
331
           painter->setPen(option.palette.color(QPalette::Mid));
332
           painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
333
           painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
334
           painter->setPen(pen);
335
   */
336
   }
337
 
338