Хранилища Subversion OpenInventory

Редакция

Редакция 180 | Редакция 202 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | RSS

Редакция Автор № строки Строка
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
 
126 pingvin 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"));
129 pingvin 109
    items.clear();
126 pingvin 110
    while(q.next()){
111
 
112
                  QString ID;
113
                  QString Name;
114
                  ID = q.value(0).toString(); //  ID
115
                  Name = q.value(1).toString(); //  Name
116
                  items.insert(ID, Name);
117
                }
118
 
119
 
120
 //   return result_map;
121
}
122
 
123
 
123 pingvin 124
//! [1]
125
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
126
    const QStyleOptionViewItem &/* option */,
127
    const QModelIndex &/* index */) const
128
{
129
    //QSpinBox *editor = new QSpinBox(parent);
130
    //editor->setMinimum(0);
131
    //editor->setMaximum(100);
126 pingvin 132
    QStringList ID_list;
133
    QStringList Name_list;
134
 
135
  // getItems();
136
 
137
 
138
 
139
 
140
 
141
    ID_list = items.keys();
142
    Name_list = items.values();
143
 
123 pingvin 144
    QComboBox *editor = new QComboBox(parent);
126 pingvin 145
 
146
/*
123 pingvin 147
    editor->addItem("0");
148
    editor->addItem("1");
149
    editor->addItem("2");
150
    editor->addItem("3");
151
    editor->addItem("4");
152
    editor->addItem("5");
153
    editor->addItem("6");
154
    editor->addItem("7");
155
    editor->addItem("8");
156
    editor->addItem("9");
126 pingvin 157
*/
123 pingvin 158
 
126 pingvin 159
//  editor->addItem("0");
160
 
161
    editor->addItems(Name_list);
179 pingvin 162
//    editor->setEditable(false);
123 pingvin 163
    return editor;
164
}
165
//! [1]
166
 
167
//! [2]
168
void SpinBoxDelegate::setEditorData(QWidget *editor,
169
                                    const QModelIndex &index) const
170
{
171
   // int value = index.model()->data(index, Qt::EditRole).toInt();
172
 
173
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
174
 //   spinBox->setValue(value);
175
QString value = index.model()->data(index, Qt::EditRole).toString();
136 pingvin 176
 
123 pingvin 177
QComboBox *comboBox = static_cast<QComboBox*>(editor);
126 pingvin 178
 
179
QStringList ID_list;
180
QStringList Name_list;
181
int curr_index;
182
 
183
ID_list = items.keys();
184
Name_list = items.values();
185
 
186
curr_index = ID_list.indexOf(value);
136 pingvin 187
if (curr_index==-1) return;
126 pingvin 188
comboBox->setCurrentIndex(curr_index);
189
/****************************************************
123 pingvin 190
if (value == "0") comboBox->setCurrentIndex(0);
191
if (value == "1") comboBox->setCurrentIndex(1);
192
if (value == "2") comboBox->setCurrentIndex(2);
193
if (value == "3") comboBox->setCurrentIndex(3);
194
if (value == "4") comboBox->setCurrentIndex(4);
195
if (value == "5") comboBox->setCurrentIndex(5);
196
if (value == "6") comboBox->setCurrentIndex(6);
197
if (value == "7") comboBox->setCurrentIndex(7);
198
if (value == "8") comboBox->setCurrentIndex(8);
199
if (value == "9") comboBox->setCurrentIndex(9);
126 pingvin 200
******************************************************/
179 pingvin 201
comboBox->setEditable(false);
123 pingvin 202
 
203
 
204
//comboBox->setItemText(0, value);
205
 
206
}
207
//! [2]
208
 
209
//! [3]
210
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
211
                                   const QModelIndex &index) const
212
{
213
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
214
  //  spinBox->interpretText();
215
  //  int value = spinBox->value();
216
 
217
  //  model->setData(index, value, Qt::EditRole);
126 pingvin 218
    QStringList ID_list;
219
    QStringList Name_list;
123 pingvin 220
 
221
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
222
    int currIndex;
223
    currIndex = comboBox->currentIndex();
136 pingvin 224
    if (currIndex==-1) return;
126 pingvin 225
  //  QString value = comboBox->itemText(currIndex);
226
 
227
    ID_list = items.keys();
228
    Name_list = items.values();
229
 
230
    QString value = ID_list.at(currIndex);
123 pingvin 231
    model->setData(index, value, Qt::EditRole);
232
}
233
//! [3]
234
 
235
//! [4]
236
void SpinBoxDelegate::updateEditorGeometry(QWidget *editor,
237
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
238
{
239
    editor->setGeometry(option.rect);
240
}
241
//! [4]
242
MyDEDelegate::MyDEDelegate(
243
                       bool calpopup,
244
                       QObject *parent)
245
               : QItemDelegate(parent),
246
                 m_calpopup(calpopup) {
247
   }
248
 
249
   QWidget *MyDEDelegate::createEditor(
250
               QWidget *parent,
251
               const QStyleOptionViewItem& /* option */,
252
               const QModelIndex& /* index */) const {
253
       QDateEdit *editor = new QDateEdit(parent);
254
       editor->setCalendarPopup(m_calpopup);
255
       editor->installEventFilter(const_cast<MyDEDelegate*>(this));
256
       return editor;
257
   }
258
 
259
   void MyDEDelegate::setEditorData(
260
                   QWidget *editor,
261
                   const QModelIndex &index) const {
262
       QDate value = index.model()->data(
263
               index, Qt::EditRole).toDate();
264
       QDateEdit *de = static_cast<QDateEdit*>(editor);
265
       de->setDate(value);
266
   }
267
 
268
   void MyDEDelegate::setModelData(
269
               QWidget *editor,
270
               QAbstractItemModel *model,
271
               const QModelIndex& index) const {
272
       QDateEdit *de = static_cast<QDateEdit*>(editor);
273
       de->interpretText();
274
       QDate value = de->date();
275
       model->setData(index, value);
276
   }
277
 
278
   void MyDEDelegate::updateEditorGeometry(
279
               QWidget *editor,
280
               const QStyleOptionViewItem &option,
281
               const QModelIndex& /* index */) const {
169 pingvin 282
 
283
 
284
 
123 pingvin 285
       editor->setGeometry(option.rect);
286
   }
138 pingvin 287
 
288
 
289
 
290
 
291
 
292
   CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent)
293
   {
294
   }
295
 
296
   void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
297
   {
298
           m_pxPicture.fill( QColor(Qt::white) );
299
 
300
           const QAbstractItemModel * model = index.model();
301
           QString sFileName = model->data( index, Qt::DisplayRole ).toString();
302
 
303
           if ( !sFileName.isEmpty() )
304
                   m_pxPicture.load( sFileName );
305
           else {
306
               //QItemDelegate::paint(painter, option, index);
307
           return;
308
           }
309
 
310
           QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
311
                                                             ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
312
                                                             QPalette::Disabled;
313
 
314
            if (option.state & QStyle::State_Selected)
315
                    painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
316
 
317
           int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
318
           int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
319
           painter->drawPixmap( nX, nY, m_pxPicture );
320
 
321
 
322
       ////    drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
323
 
324
           /*
325
           QPen pen = painter->pen();
326
           painter->setPen(option.palette.color(QPalette::Mid));
327
           painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
328
           painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
329
           painter->setPen(pen);
330
   */
331
   }
158 pingvin 332
 
333
 
334
 
335
 
336
   void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
337
                            const QModelIndex &index) const
338
   {
339
       int datetime = index.model()->data(index, Qt::DisplayRole).toInt();
340
 
341
       QString indexvalue = "";
342
 
343
       if (datetime > 0)
344
       {
345
           QDateTime dateTime2 = QDateTime();
346
           dateTime2.setTime_t(datetime);
347
           indexvalue = dateTime2.toString(this->timeformat);
348
       }
349
       else
350
       {
351
           indexvalue = tr("Date not set");
352
       }
353
 
354
       Q_ASSERT(index.isValid());
355
 
356
       QStyleOptionViewItemV3 opt = setOptions(index, option);
357
 
358
       const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option);
359
       opt.features = v2 ? v2->features
360
                       : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
361
       const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option);
362
       opt.locale = v3 ? v3->locale : QLocale();
363
       opt.widget = v3 ? v3->widget : 0;
364
 
365
       // prepare
366
       painter->save();
367
 
368
       painter->setClipRect(opt.rect);
369
 
370
       // get the data and the rectangles
371
       QVariant value;
372
 
373
       QPixmap pixmap;
374
       QRect decorationRect;
375
       value = index.data(Qt::DecorationRole);
376
 
377
       QString text;
378
       QRect displayRect;
379
       value = index.data(Qt::DisplayRole);
380
       if (value.isValid()) {
381
           text = indexvalue;
382
           displayRect = textRectangle(painter, option.rect, opt.font, text);
383
       }
384
 
385
       QRect checkRect;
386
       Qt::CheckState checkState = Qt::Unchecked;
387
       value = index.data(Qt::CheckStateRole);
388
       if (value.isValid()) {
389
           checkState = static_cast<Qt::CheckState>(value.toInt());
390
           checkRect = check(opt, opt.rect, value);
391
       }
392
 
393
       // do the layout
394
       doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
395
       // draw the item
396
 
397
       drawBackground(painter, opt, index);
398
       drawCheck(painter, opt, checkRect, checkState);
399
       drawDecoration(painter, opt, decorationRect, pixmap);
400
       drawDisplay(painter, opt, displayRect, text);
401
       drawFocus(painter, opt, displayRect);
402
 
403
       // done
404
       painter->restore();
405
   }
180 pingvin 406
 
407
 
408
 
409
 
410
 
411
 
412
   IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent)
413
   {
414
   }
415
 
416
 
417
 
418
 
419
 
420
   QWidget *IconDelegate::createEditor(QWidget *parent,
421
       const QStyleOptionViewItem &/* option */,
422
       const QModelIndex & /* index */) const
423
   {
424
 
425
       IconForm *editor = new IconForm(parent);
426
 
427
       return editor;
428
 
429
   }
430
 
431
 
432
 
433
 
434
   void IconDelegate::setEditorData(QWidget *editor,
435
                                       const QModelIndex &index) const
436
   {
437
 
438
 
439
       const QAbstractItemModel * model = index.model();
440
       IconForm *icnFrm = static_cast<IconForm*>(editor);
441
       QVariant currentImage = model->data(index,0);
442
       QByteArray bytes = currentImage.toByteArray();
443
       if (currentImage.isValid()) {
444
            m_pxPicture.loadFromData(bytes);
445
       }
446
       else {
447
           //QItemDelegate::paint(painter, option, index);
448
 
449
 
450
           return;
451
       }
452
       icnFrm->setPixmap(m_pxPicture);
453
     //  tblView->setRowHeight(index.row(), icnFrm->geometry().height());
454
   }
455
 
456
 
457
 
458
 
459
   void IconDelegate::setModelData(
460
               QWidget *editor,
461
               QAbstractItemModel *model,
462
               const QModelIndex& index) const {
463
       IconForm *icnFrm = static_cast<IconForm*>(editor);
464
       if (!(icnFrm->dataIsChanged())) return;
465
 
466
       m_pxPicture = icnFrm->pixmap();
467
       QImage currentImage = m_pxPicture.toImage();
468
          QByteArray bytes;
469
          QBuffer buffer(&bytes);
470
          buffer.open(QIODevice::WriteOnly);
471
          currentImage.save(&buffer, "PNG");
472
 
473
 
474
 
475
 
476
         model->setData(index, QVariant (bytes), Qt::EditRole);
477
 
478
     //    int widht = m_pxPicture.width();
479
     //    int heigh =  m_pxPicture.height();
480
     //    tblView->setRowHeight(index.row(), heigh + 10);
481
         // model->submitAll();
482
 
483
 
484
 
485
 
486
      // model->setData(index, value);
487
   }
488
 
489
 
490
 
491
   void IconDelegate::updateEditorGeometry(
492
               QWidget *editor,
493
               const QStyleOptionViewItem &option,
494
               const QModelIndex& /* index */) const {
495
 
496
 
497
   //    QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
498
     //      editor->setGeometry(r);
499
 
500
        editor->setGeometry(option.rect);
501
   }
502
 
503
 
504
 
505
 
506
   void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
507
   {
508
 
509
 
510
          m_pxPicture.fill( QColor(Qt::white) );
511
 
512
          const QAbstractItemModel * model = index.model();
513
          QString sFileName = model->data( index, Qt::DisplayRole ).toString();
514
 
515
          QVariant currentImage = model->data(index,0);
516
 
517
 
518
           QByteArray bytes = currentImage.toByteArray();
519
     //       QImage image;
520
     //       image.loadFromData(bytes);
521
 
522
 
523
           if (currentImage.isValid()) {
524
                m_pxPicture.loadFromData(bytes);
525
 
526
           }
527
           else {
528
               //QItemDelegate::paint(painter, option, index);
529
           return;
530
           }
531
 
532
 
533
 
534
 
535
 
536
           /*
537
 
538
           if ( !sFileName.isEmpty() )
539
                   m_pxPicture.load( sFileName );
540
           else {
541
               //QItemDelegate::paint(painter, option, index);
542
           return;
543
           }
544
*/
545
 
546
 
547
           QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
548
                                                             ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
549
                                                             QPalette::Disabled;
550
 
551
            if (option.state & QStyle::State_Selected)
552
 
553
 
554
                painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
555
 
556
           int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
557
           int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
558
           painter->drawPixmap( nX, nY, m_pxPicture );
559
 
560
 
561
       //    drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
562
 
563
           /*
564
           QPen pen = painter->pen();
565
           painter->setPen(option.palette.color(QPalette::Mid));
566
           painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
567
           painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
568
           painter->setPen(pen);
569
   */
570
}
571
 
572
 
573
   void IconDelegate::setTableView(QTableView* table){
574
       tblView = table;
575
   }
201 pingvin 576
 
577
 
578
 
579
 
580
 
581
 
582
 
583
 
584
   FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent)
585
   {
586
   }
587
 
588
 
589
 
590
 
591
 
592
   QWidget *FileDelegate::createEditor(QWidget *parent,
593
       const QStyleOptionViewItem &/* option */,
594
       const QModelIndex & /* index */) const
595
   {
596
 
597
       FileForm *editor = new FileForm(parent);
598
 
599
       return editor;
600
 
601
   }
602
 
603
 
604
 
605
 
606
   void FileDelegate::setEditorData(QWidget *editor,
607
                                       const QModelIndex &index) const
608
   {
609
 
610
 
611
       const QAbstractItemModel * model = index.model();
612
       FileForm *flFrm = static_cast<FileForm*>(editor);
613
       QVariant currentData = model->data(index,0);
614
       QByteArray bytes = currentData.toByteArray();
615
       if (currentData.isValid()) flFrm->setData(bytes);
616
       else return;
617
 
618
   }
619
 
620
 
621
 
622
 
623
void FileDelegate::setModelData(
624
               QWidget *editor,
625
               QAbstractItemModel *model,
626
               const QModelIndex& index) const {
627
       FileForm *flFrm = static_cast<FileForm*>(editor);
628
       if (!(flFrm->dataIsChanged())) return;
629
 
630
       m_Data = flFrm->data();
631
       if (!m_Data.isEmpty()) { //    
632
                                   model->setData(index, QVariant (m_Data), Qt::EditRole);
633
 
634
                              }
635
       else {       //    ( )
636
           QVariant val_null;
637
           model->setData(index, val_null, Qt::EditRole);
638
 
639
            }
640
 
641
 
642
 
643
   }
644
 
645
 
646
 
647
   void FileDelegate::updateEditorGeometry(
648
               QWidget *editor,
649
               const QStyleOptionViewItem &option,
650
               const QModelIndex& /* index */) const {
651
 
652
 
653
   //    QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
654
     //      editor->setGeometry(r);
655
 
656
        editor->setGeometry(option.rect);
657
   }
658
 
659
 
660
 
661