Хранилища Subversion OpenInventory

Редакция

Редакция 225 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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
}
219 pingvin 241
 
242
 
243
 
244
 
245
 
246
 
247
 
248
 
249
 
250
 
251
 
252
//! [0]
253
FilterSpinBoxDelegate::FilterSpinBoxDelegate(QObject *parent)
254
    : QItemDelegate(parent)
255
{
256
}
257
//! [0]
258
 
259
void FilterSpinBoxDelegate::setClassID(QString newClassID){
260
    class_id = newClassID;
261
}
262
 
263
void FilterSpinBoxDelegate::getItems(){
264
    QMap <QString, QString> result_map;
265
    QSqlQuery q;
266
    QString query_str;
267
    bool ok;
268
 
269
    if (FilterSpinBoxDelegate::class_id.isEmpty())
270
    {
271
        items.clear();
272
        return;
273
    }
274
 
275
    query_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '");
276
    query_str.append(class_id);
277
    query_str.append( tr("'"));
278
 
279
    q.prepare(query_str);
280
 
281
 
282
    ok = q.exec();
283
 
284
 
285
 
286
 
287
    if (!ok) {
288
                            /*
289
                            QString error_str;
290
                            error_str =  tr("       ");
291
                            error_str.append(inctance);
292
                            QMessageBox::critical( //     .
293
                                                                            this,                      //  .
294
                                                                            QObject::tr("Database Error"),   // .
295
                                                                            q.lastError().text());          //  .
296
                                                                        //    tr("       "));    //  .
297
 
298
                            */
299
                            items.clear();
300
                            return;
301
                        }
302
 
303
 
304
 //   field_inctance = q.record().indexOf(tr("TableWhithInstance"));
305
    items.clear();
306
    while(q.next()){
307
 
308
                  QString ID;
309
                  QString Name;
310
                  ID = q.value(0).toString(); //  ID
311
                  Name = q.value(1).toString(); //  Name
312
                  items.insert(ID, Name);
313
                }
314
 
315
 
316
 //   return result_map;
317
}
318
 
319
 
320
//! [1]
321
QWidget *FilterSpinBoxDelegate::createEditor(QWidget *parent,
322
    const QStyleOptionViewItem &/* option */,
323
    const QModelIndex &/* index */) const
324
{
325
    //QSpinBox *editor = new QSpinBox(parent);
326
    //editor->setMinimum(0);
327
    //editor->setMaximum(100);
328
    QStringList ID_list;
329
    QStringList Name_list;
330
 
331
  // getItems();
332
 
333
 
334
 
335
 
336
 
337
    ID_list = items.keys();
338
    Name_list = items.values();
339
 
340
    QComboBox *editor = new QComboBox(parent);
341
 
342
/*
343
    editor->addItem("0");
344
    editor->addItem("1");
345
    editor->addItem("2");
346
    editor->addItem("3");
347
    editor->addItem("4");
348
    editor->addItem("5");
349
    editor->addItem("6");
350
    editor->addItem("7");
351
    editor->addItem("8");
352
    editor->addItem("9");
353
*/
354
 
355
//  editor->addItem("0");
356
 
357
    editor->addItems(Name_list);
358
//    editor->setEditable(false);
359
    return editor;
360
}
361
//! [1]
362
 
363
//! [2]
364
void FilterSpinBoxDelegate::setEditorData(QWidget *editor,
365
                                    const QModelIndex &index) const
366
{
367
   // int value = index.model()->data(index, Qt::EditRole).toInt();
368
 
369
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
370
 //   spinBox->setValue(value);
371
QString value = index.model()->data(index, Qt::EditRole).toString();
372
 
373
QComboBox *comboBox = static_cast<QComboBox*>(editor);
374
 
375
QStringList ID_list;
376
QStringList Name_list;
377
int curr_index;
378
 
379
ID_list = items.keys();
380
Name_list = items.values();
381
 
382
curr_index = ID_list.indexOf(value);
383
if (curr_index==-1) return;
384
comboBox->setCurrentIndex(curr_index);
385
/****************************************************
386
if (value == "0") comboBox->setCurrentIndex(0);
387
if (value == "1") comboBox->setCurrentIndex(1);
388
if (value == "2") comboBox->setCurrentIndex(2);
389
if (value == "3") comboBox->setCurrentIndex(3);
390
if (value == "4") comboBox->setCurrentIndex(4);
391
if (value == "5") comboBox->setCurrentIndex(5);
392
if (value == "6") comboBox->setCurrentIndex(6);
393
if (value == "7") comboBox->setCurrentIndex(7);
394
if (value == "8") comboBox->setCurrentIndex(8);
395
if (value == "9") comboBox->setCurrentIndex(9);
396
******************************************************/
397
comboBox->setEditable(false);
398
 
399
 
400
//comboBox->setItemText(0, value);
401
 
402
}
403
//! [2]
404
 
405
//! [3]
406
void FilterSpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
407
                                   const QModelIndex &index) const
408
{
409
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
410
  //  spinBox->interpretText();
411
  //  int value = spinBox->value();
412
 
413
  //  model->setData(index, value, Qt::EditRole);
414
    QStringList ID_list;
415
    QStringList Name_list;
416
 
417
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
418
    int currIndex;
419
    currIndex = comboBox->currentIndex();
420
    if (currIndex==-1) return;
421
  //  QString value = comboBox->itemText(currIndex);
422
 
423
    ID_list = items.keys();
424
    Name_list = items.values();
425
 
426
 //   QString value = ID_list.at(currIndex);
427
    QString value = Name_list.at(currIndex);
428
    model->setData(index, value, Qt::EditRole);
429
}
430
//! [3]
431
 
123 pingvin 432
//! [4]
219 pingvin 433
void FilterSpinBoxDelegate::updateEditorGeometry(QWidget *editor,
434
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
435
{
436
    editor->setGeometry(option.rect);
437
}
438
 
439
 
440
 
441
 
442
 
443
 
444
 
445
 
446
 
447
 
448
//! [0]
449
FilterConditionDelegate::FilterConditionDelegate(QObject *parent)
450
    : QItemDelegate(parent)
451
{
452
}
453
//! [0]
454
 
455
void FilterConditionDelegate::setClassID(QString newClassID){
456
    class_id = newClassID;
457
}
458
 
459
 
460
 
461
//! [1]
462
QWidget *FilterConditionDelegate::createEditor(QWidget *parent,
463
    const QStyleOptionViewItem &/* option */,
464
    const QModelIndex &/* index */) const
465
{
466
    QStringList Name_list;
467
 
468
    Name_list = items;
469
 
470
    QComboBox *editor = new QComboBox(parent);
471
 
472
/*
473
    editor->addItem("0");
474
    editor->addItem("1");
475
    editor->addItem("2");
476
    editor->addItem("3");
477
    editor->addItem("4");
478
    editor->addItem("5");
479
    editor->addItem("6");
480
    editor->addItem("7");
481
    editor->addItem("8");
482
    editor->addItem("9");
483
*/
484
 
485
//  editor->addItem("0");
486
 
487
    editor->addItems(Name_list);
488
//    editor->setEditable(false);
489
    return editor;
490
}
491
//! [1]
492
 
493
//! [2]
494
void FilterConditionDelegate::setEditorData(QWidget *editor,
495
                                    const QModelIndex &index) const
496
{
497
   // int value = index.model()->data(index, Qt::EditRole).toInt();
498
 
499
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
500
 //   spinBox->setValue(value);
501
QString value = index.model()->data(index, Qt::EditRole).toString();
502
 
503
QComboBox *comboBox = static_cast<QComboBox*>(editor);
504
 
505
//QStringList ID_list;
506
//QStringList Name_list;
507
int curr_index;
508
 
509
//ID_list = items.keys();
510
//Name_list = items.values();
511
 
512
curr_index = items.indexOf(value);
513
if (curr_index==-1) return;
514
comboBox->setCurrentIndex(curr_index);
515
/****************************************************
516
if (value == "0") comboBox->setCurrentIndex(0);
517
if (value == "1") comboBox->setCurrentIndex(1);
518
if (value == "2") comboBox->setCurrentIndex(2);
519
if (value == "3") comboBox->setCurrentIndex(3);
520
if (value == "4") comboBox->setCurrentIndex(4);
521
if (value == "5") comboBox->setCurrentIndex(5);
522
if (value == "6") comboBox->setCurrentIndex(6);
523
if (value == "7") comboBox->setCurrentIndex(7);
524
if (value == "8") comboBox->setCurrentIndex(8);
525
if (value == "9") comboBox->setCurrentIndex(9);
526
******************************************************/
527
comboBox->setEditable(false);
528
 
529
 
530
//comboBox->setItemText(0, value);
531
 
532
}
533
//! [2]
534
 
535
//! [3]
536
void FilterConditionDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
537
                                   const QModelIndex &index) const
538
{
539
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
540
  //  spinBox->interpretText();
541
  //  int value = spinBox->value();
542
 
543
  //  model->setData(index, value, Qt::EditRole);
544
    QStringList ID_list;
545
    QStringList Name_list;
546
 
547
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
548
    int currIndex;
549
    currIndex = comboBox->currentIndex();
550
    if (currIndex==-1) return;
551
  //  QString value = comboBox->itemText(currIndex);
552
 
553
    // ID_list = items.keys();
554
    // Name_list = items.values();
555
 
556
 //   QString value = ID_list.at(currIndex);
557
    QString value = items.at(currIndex);
558
    model->setData(index, value, Qt::EditRole);
559
}
560
//! [3]
561
 
562
//! [4]
563
void FilterConditionDelegate::updateEditorGeometry(QWidget *editor,
564
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
565
{
566
    editor->setGeometry(option.rect);
567
}
568
 
569
 
570
 
571
 
572
 
573
 
574
 
575
 
576
 
577
 
224 pingvin 578
FilterValueDelegate::FilterValueDelegate(QObject *parent)
579
    : QItemDelegate(parent)
580
{
581
}
582
//! [0]
583
 
584
void FilterValueDelegate::setClassID(QString newClassID){
585
    class_id = newClassID;
586
}
587
 
588
 
589
 
225 pingvin 590
//    -   
591
QString FilterValueDelegate::getClassInctanc(QString class_id){
592
 
593
 
594
 
595
 
596
    QSqlQuery q;
597
    QString sql_str;
598
    QString result;
599
    QString classInctance;
600
    int field_inctance;
601
    bool ok;
602
    sql_str = tr("select * "
603
                 " from ListOfClasses where  ListOfClasses.ID = '"       // ,     
604
                 );
605
    sql_str.append(class_id);
606
     sql_str.append(tr("'"));
607
    q.prepare(sql_str);
608
 
609
    ok = q.exec();
610
 
611
    if (!ok) return result;
612
 
613
    /*
614
    if (!ok) {
615
                            QMessageBox::critical( //     .
616
                                                                            this,                      //  .
617
                                                                            QObject::tr("Database Error"),   // .
618
                                                                            q.lastError().text());          //  .
619
                                                                            return result;
620
                     }
621
 
622
   */
623
 
624
 
625
 
626
 
627
    field_inctance = q.record().indexOf(tr("TableWhithInstance"));
628
    while(q.next()){
629
 
630
                  classInctance = q.value(field_inctance).toString();
631
                  result.append(classInctance);
632
                  }
633
 
634
  return result;
635
 
636
 
637
 
638
 
639
 
640
}
641
 
642
 
643
 
226 pingvin 644
 QString FilterValueDelegate::getFieldType(QString class_id, QString fieldName)//   
225 pingvin 645
 {
646
 
647
 }
648
 
649
 
226 pingvin 650
 
651
 
652
 
653
 
654
 
224 pingvin 655
//! [1]
656
QWidget *FilterValueDelegate::createEditor(QWidget *parent,
657
    const QStyleOptionViewItem &/* option */,
658
    const QModelIndex &/* index */) const
659
{
660
    QStringList Name_list;
661
 
662
    Name_list = items;
663
 
664
    QComboBox *editor = new QComboBox(parent);
665
 
666
/*
667
    editor->addItem("0");
668
    editor->addItem("1");
669
    editor->addItem("2");
670
    editor->addItem("3");
671
    editor->addItem("4");
672
    editor->addItem("5");
673
    editor->addItem("6");
674
    editor->addItem("7");
675
    editor->addItem("8");
676
    editor->addItem("9");
677
*/
678
 
679
//  editor->addItem("0");
680
 
681
// // //    editor->addItems(Name_list);
225 pingvin 682
    editor->setEditable(true);
224 pingvin 683
    return editor;
684
}
685
//! [1]
686
 
225 pingvin 687
 
688
 
689
 
226 pingvin 690
TField FilterValueDelegate::getField(const QModelIndex &index) //   (  )    
691
{
692
    const QAbstractItemModel * model = index.model();
225 pingvin 693
 
226 pingvin 694
    TField field_result;
695
    QSqlQuery q;
696
    QString sql_str;
697
    bool ok;
225 pingvin 698
 
699
 
226 pingvin 700
    // QString fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
701
  //  QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
702
    field_result.FieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
703
    field_result.ClassIdentifer = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
225 pingvin 704
 
226 pingvin 705
    sql_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '");
706
    sql_str.append(field_result.ClassIdentifer);
707
    sql_str.append(tr("' and `FieldName` = '"));
708
    sql_str.append(field_result.FieldName);
709
    sql_str.append(tr("'"));
225 pingvin 710
 
226 pingvin 711
    ok = q.prepare(sql_str);
225 pingvin 712
 
226 pingvin 713
    if(!ok)  return field_result;
714
 
715
    ok = q.exec();
716
 
717
    if(!ok)  return field_result;
718
 
719
    int ID_index = q.record().indexOf(tr("ID"));
720
 
721
    int FieldAlias_index = q.record().indexOf(tr("FieldAlias"));
722
 
723
    int FieldType_index = q.record().indexOf(tr("FieldType"));
724
 
725
    int DefaultValue_index = q.record().indexOf(tr("DefaultValue"));
726
 
727
    int Comment_index = q.record().indexOf(tr("Comment"));
728
 
729
 
730
 
731
    while(q.next()){
732
    field_result.ID = q.value(ID_index).asString();
733
    field_result.FieldAlias = q.value(FieldAlias_index).asString();
734
    field_result.FieldType = q.value(FieldType_index).asString();
735
    field_result.DefaultValue = q.value(DefaultValue_index).asString();
736
    field_result.Comment = q.value(Comment_index).asString();
737
    }
738
 
739
    return field_result;
740
 
741
 
742
 
743
}
744
 
745
 
746
 
747
 
748
 
224 pingvin 749
//! [2]
750
void FilterValueDelegate::setEditorData(QWidget *editor,
751
                                    const QModelIndex &index) const
752
{
753
   // int value = index.model()->data(index, Qt::EditRole).toInt();
754
 
755
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
756
 //   spinBox->setValue(value);
757
 
758
 
759
const QAbstractItemModel * model = index.model();
760
 
761
 
762
QString value = index.model()->data(index, Qt::EditRole).toString();
763
QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
764
QComboBox *comboBox = static_cast<QComboBox*>(editor);
225 pingvin 765
QString inctance; //  
766
QString fieldName;// 
767
QSqlQuery q;
768
QString sql_str;
769
QStringList itemsList;
770
bool ok;
226 pingvin 771
TField Field;
224 pingvin 772
 
226 pingvin 773
Field = getField(index);
225 pingvin 774
 
775
 
226 pingvin 776
 
777
 
778
 
779
 
225 pingvin 780
fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
781
 
782
 
783
inctance = FilterValueDelegate::getClassInctanc(class_id);
784
if (!inctance.isEmpty() && !fieldName.isEmpty()){
785
sql_str = tr("select `");
786
sql_str.append(fieldName);
787
sql_str.append(tr("` from `"));
788
sql_str.append(inctance);
789
sql_str.append(tr("`"));
790
ok = q.prepare(sql_str);
791
ok = q.exec();
792
 
793
while(q.next()){
794
QString tmp_str;
795
tmp_str = q.record().value(fieldName).asString();
796
itemsList.append(tmp_str);
797
FilterValueDelegate::items.append(tmp_str);
798
        }
799
if(!itemsList.isEmpty()) {
800
                            comboBox->addItems(itemsList);
801
                            items = itemsList; //  
802
                         }
803
 
804
}
805
 
806
 
807
 
808
 
809
 
224 pingvin 810
//QStringList ID_list;
811
//QStringList Name_list;
812
int curr_index;
813
 
814
//ID_list = items.keys();
815
//Name_list = items.values();
816
 
225 pingvin 817
curr_index = itemsList.indexOf(value);
224 pingvin 818
if (curr_index==-1) return;
819
comboBox->setCurrentIndex(curr_index);
820
/****************************************************
821
if (value == "0") comboBox->setCurrentIndex(0);
822
if (value == "1") comboBox->setCurrentIndex(1);
823
if (value == "2") comboBox->setCurrentIndex(2);
824
if (value == "3") comboBox->setCurrentIndex(3);
825
if (value == "4") comboBox->setCurrentIndex(4);
826
if (value == "5") comboBox->setCurrentIndex(5);
827
if (value == "6") comboBox->setCurrentIndex(6);
828
if (value == "7") comboBox->setCurrentIndex(7);
829
if (value == "8") comboBox->setCurrentIndex(8);
830
if (value == "9") comboBox->setCurrentIndex(9);
831
******************************************************/
225 pingvin 832
comboBox->setEditable(true);
224 pingvin 833
 
834
 
835
//comboBox->setItemText(0, value);
836
 
837
}
838
//! [2]
839
 
840
//! [3]
841
void FilterValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
842
                                   const QModelIndex &index) const
843
{
844
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
845
  //  spinBox->interpretText();
846
  //  int value = spinBox->value();
847
 
848
  //  model->setData(index, value, Qt::EditRole);
225 pingvin 849
 
224 pingvin 850
    QStringList ID_list;
851
    QStringList Name_list;
852
 
853
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
854
    int currIndex;
855
    currIndex = comboBox->currentIndex();
856
    if (currIndex==-1) return;
857
  //  QString value = comboBox->itemText(currIndex);
858
 
859
    // ID_list = items.keys();
860
    // Name_list = items.values();
861
 
862
 //   QString value = ID_list.at(currIndex);
863
    QString value = items.at(currIndex);
864
    model->setData(index, value, Qt::EditRole);
225 pingvin 865
 
866
 
224 pingvin 867
}
225 pingvin 868
 
869
 
870
 
871
 
872
 
873
 
874
 
875
 
224 pingvin 876
//! [3]
877
 
219 pingvin 878
//! [4]
224 pingvin 879
void FilterValueDelegate::updateEditorGeometry(QWidget *editor,
880
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
881
{
882
    editor->setGeometry(option.rect);
883
}
884
 
885
 
886
 
887
 
888
 
889
 
890
 
891
 
892
 
893
 
894
 
895
 
896
 
897
 
898
 
899
//! [4]
123 pingvin 900
MyDEDelegate::MyDEDelegate(
901
                       bool calpopup,
902
                       QObject *parent)
903
               : QItemDelegate(parent),
904
                 m_calpopup(calpopup) {
905
   }
906
 
907
   QWidget *MyDEDelegate::createEditor(
908
               QWidget *parent,
909
               const QStyleOptionViewItem& /* option */,
910
               const QModelIndex& /* index */) const {
911
       QDateEdit *editor = new QDateEdit(parent);
912
       editor->setCalendarPopup(m_calpopup);
913
       editor->installEventFilter(const_cast<MyDEDelegate*>(this));
914
       return editor;
915
   }
916
 
917
   void MyDEDelegate::setEditorData(
918
                   QWidget *editor,
919
                   const QModelIndex &index) const {
920
       QDate value = index.model()->data(
921
               index, Qt::EditRole).toDate();
922
       QDateEdit *de = static_cast<QDateEdit*>(editor);
923
       de->setDate(value);
924
   }
925
 
926
   void MyDEDelegate::setModelData(
927
               QWidget *editor,
928
               QAbstractItemModel *model,
929
               const QModelIndex& index) const {
930
       QDateEdit *de = static_cast<QDateEdit*>(editor);
931
       de->interpretText();
932
       QDate value = de->date();
933
       model->setData(index, value);
934
   }
935
 
936
   void MyDEDelegate::updateEditorGeometry(
937
               QWidget *editor,
938
               const QStyleOptionViewItem &option,
939
               const QModelIndex& /* index */) const {
169 pingvin 940
 
941
 
942
 
123 pingvin 943
       editor->setGeometry(option.rect);
944
   }
138 pingvin 945
 
946
 
947
 
948
 
949
 
950
   CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent)
951
   {
952
   }
953
 
954
   void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
955
   {
956
           m_pxPicture.fill( QColor(Qt::white) );
957
 
958
           const QAbstractItemModel * model = index.model();
959
           QString sFileName = model->data( index, Qt::DisplayRole ).toString();
960
 
961
           if ( !sFileName.isEmpty() )
962
                   m_pxPicture.load( sFileName );
963
           else {
964
               //QItemDelegate::paint(painter, option, index);
965
           return;
966
           }
967
 
968
           QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
969
                                                             ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
970
                                                             QPalette::Disabled;
971
 
972
            if (option.state & QStyle::State_Selected)
973
                    painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
974
 
975
           int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
976
           int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
977
           painter->drawPixmap( nX, nY, m_pxPicture );
978
 
979
 
980
       ////    drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
981
 
982
           /*
983
           QPen pen = painter->pen();
984
           painter->setPen(option.palette.color(QPalette::Mid));
985
           painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
986
           painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
987
           painter->setPen(pen);
988
   */
989
   }
158 pingvin 990
 
991
 
992
 
993
 
994
   void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
995
                            const QModelIndex &index) const
996
   {
997
       int datetime = index.model()->data(index, Qt::DisplayRole).toInt();
998
 
999
       QString indexvalue = "";
1000
 
1001
       if (datetime > 0)
1002
       {
1003
           QDateTime dateTime2 = QDateTime();
1004
           dateTime2.setTime_t(datetime);
1005
           indexvalue = dateTime2.toString(this->timeformat);
1006
       }
1007
       else
1008
       {
1009
           indexvalue = tr("Date not set");
1010
       }
1011
 
1012
       Q_ASSERT(index.isValid());
1013
 
1014
       QStyleOptionViewItemV3 opt = setOptions(index, option);
1015
 
1016
       const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option);
1017
       opt.features = v2 ? v2->features
1018
                       : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
1019
       const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option);
1020
       opt.locale = v3 ? v3->locale : QLocale();
1021
       opt.widget = v3 ? v3->widget : 0;
1022
 
1023
       // prepare
1024
       painter->save();
1025
 
1026
       painter->setClipRect(opt.rect);
1027
 
1028
       // get the data and the rectangles
1029
       QVariant value;
1030
 
1031
       QPixmap pixmap;
1032
       QRect decorationRect;
1033
       value = index.data(Qt::DecorationRole);
1034
 
1035
       QString text;
1036
       QRect displayRect;
1037
       value = index.data(Qt::DisplayRole);
1038
       if (value.isValid()) {
1039
           text = indexvalue;
1040
           displayRect = textRectangle(painter, option.rect, opt.font, text);
1041
       }
1042
 
1043
       QRect checkRect;
1044
       Qt::CheckState checkState = Qt::Unchecked;
1045
       value = index.data(Qt::CheckStateRole);
1046
       if (value.isValid()) {
1047
           checkState = static_cast<Qt::CheckState>(value.toInt());
1048
           checkRect = check(opt, opt.rect, value);
1049
       }
1050
 
1051
       // do the layout
1052
       doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
1053
       // draw the item
1054
 
1055
       drawBackground(painter, opt, index);
1056
       drawCheck(painter, opt, checkRect, checkState);
1057
       drawDecoration(painter, opt, decorationRect, pixmap);
1058
       drawDisplay(painter, opt, displayRect, text);
1059
       drawFocus(painter, opt, displayRect);
1060
 
1061
       // done
1062
       painter->restore();
1063
   }
180 pingvin 1064
 
1065
 
1066
 
1067
 
1068
 
1069
 
1070
   IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent)
1071
   {
1072
   }
1073
 
1074
 
1075
 
1076
 
1077
 
1078
   QWidget *IconDelegate::createEditor(QWidget *parent,
1079
       const QStyleOptionViewItem &/* option */,
1080
       const QModelIndex & /* index */) const
1081
   {
1082
 
1083
       IconForm *editor = new IconForm(parent);
1084
 
1085
       return editor;
1086
 
1087
   }
1088
 
1089
 
1090
 
1091
 
1092
   void IconDelegate::setEditorData(QWidget *editor,
1093
                                       const QModelIndex &index) const
1094
   {
1095
 
1096
 
1097
       const QAbstractItemModel * model = index.model();
1098
       IconForm *icnFrm = static_cast<IconForm*>(editor);
1099
       QVariant currentImage = model->data(index,0);
1100
       QByteArray bytes = currentImage.toByteArray();
1101
       if (currentImage.isValid()) {
1102
            m_pxPicture.loadFromData(bytes);
1103
       }
1104
       else {
1105
           //QItemDelegate::paint(painter, option, index);
1106
 
1107
 
1108
           return;
1109
       }
1110
       icnFrm->setPixmap(m_pxPicture);
1111
     //  tblView->setRowHeight(index.row(), icnFrm->geometry().height());
1112
   }
1113
 
1114
 
1115
 
1116
 
1117
   void IconDelegate::setModelData(
1118
               QWidget *editor,
1119
               QAbstractItemModel *model,
1120
               const QModelIndex& index) const {
1121
       IconForm *icnFrm = static_cast<IconForm*>(editor);
1122
       if (!(icnFrm->dataIsChanged())) return;
1123
 
1124
       m_pxPicture = icnFrm->pixmap();
1125
       QImage currentImage = m_pxPicture.toImage();
1126
          QByteArray bytes;
1127
          QBuffer buffer(&bytes);
1128
          buffer.open(QIODevice::WriteOnly);
1129
          currentImage.save(&buffer, "PNG");
1130
 
1131
 
1132
 
1133
 
1134
         model->setData(index, QVariant (bytes), Qt::EditRole);
1135
 
1136
     //    int widht = m_pxPicture.width();
1137
     //    int heigh =  m_pxPicture.height();
1138
     //    tblView->setRowHeight(index.row(), heigh + 10);
1139
         // model->submitAll();
1140
 
1141
 
1142
 
1143
 
1144
      // model->setData(index, value);
1145
   }
1146
 
1147
 
1148
 
1149
   void IconDelegate::updateEditorGeometry(
1150
               QWidget *editor,
1151
               const QStyleOptionViewItem &option,
1152
               const QModelIndex& /* index */) const {
1153
 
1154
 
1155
   //    QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
1156
     //      editor->setGeometry(r);
1157
 
1158
        editor->setGeometry(option.rect);
1159
   }
1160
 
1161
 
1162
 
1163
 
1164
   void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
1165
   {
1166
 
1167
 
1168
          m_pxPicture.fill( QColor(Qt::white) );
1169
 
1170
          const QAbstractItemModel * model = index.model();
1171
          QString sFileName = model->data( index, Qt::DisplayRole ).toString();
1172
 
1173
          QVariant currentImage = model->data(index,0);
1174
 
1175
 
1176
           QByteArray bytes = currentImage.toByteArray();
1177
     //       QImage image;
1178
     //       image.loadFromData(bytes);
1179
 
1180
 
1181
           if (currentImage.isValid()) {
1182
                m_pxPicture.loadFromData(bytes);
1183
 
1184
           }
1185
           else {
1186
               //QItemDelegate::paint(painter, option, index);
1187
           return;
1188
           }
1189
 
1190
 
1191
 
1192
 
1193
 
1194
           /*
1195
 
1196
           if ( !sFileName.isEmpty() )
1197
                   m_pxPicture.load( sFileName );
1198
           else {
1199
               //QItemDelegate::paint(painter, option, index);
1200
           return;
1201
           }
1202
*/
1203
 
1204
 
205 pingvin 1205
 
1206
 
1207
 
180 pingvin 1208
           QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
1209
                                                             ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
1210
                                                             QPalette::Disabled;
1211
 
1212
            if (option.state & QStyle::State_Selected)
1213
 
1214
 
1215
                painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
1216
 
205 pingvin 1217
 
1218
 
180 pingvin 1219
           int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
1220
           int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
205 pingvin 1221
   //        painter->setRenderHint(QPainter::Antialiasing);
180 pingvin 1222
           painter->drawPixmap( nX, nY, m_pxPicture );
1223
 
1224
       //    drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
1225
 
1226
           /*
1227
           QPen pen = painter->pen();
1228
           painter->setPen(option.palette.color(QPalette::Mid));
1229
           painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
1230
           painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
1231
           painter->setPen(pen);
1232
   */
1233
}
1234
 
1235
 
1236
   void IconDelegate::setTableView(QTableView* table){
1237
       tblView = table;
1238
   }
201 pingvin 1239
 
1240
 
1241
 
1242
 
1243
 
1244
 
1245
 
1246
 
1247
   FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent)
1248
   {
1249
   }
1250
 
1251
 
1252
 
1253
 
1254
 
1255
   QWidget *FileDelegate::createEditor(QWidget *parent,
1256
       const QStyleOptionViewItem &/* option */,
1257
       const QModelIndex & /* index */) const
1258
   {
1259
 
1260
       FileForm *editor = new FileForm(parent);
1261
 
1262
       return editor;
1263
 
1264
   }
1265
 
1266
 
1267
 
1268
 
1269
   void FileDelegate::setEditorData(QWidget *editor,
1270
                                       const QModelIndex &index) const
1271
   {
1272
 
1273
 
1274
       const QAbstractItemModel * model = index.model();
1275
       FileForm *flFrm = static_cast<FileForm*>(editor);
202 pingvin 1276
       QVariant currentData = model->data(index,Qt::EditRole);
1277
 
1278
      // QVariant currentData = QSqlTableModel::data(index, 0);
201 pingvin 1279
       QByteArray bytes = currentData.toByteArray();
1280
       if (currentData.isValid()) flFrm->setData(bytes);
1281
       else return;
1282
 
1283
   }
1284
 
1285
 
1286
 
1287
 
1288
void FileDelegate::setModelData(
1289
               QWidget *editor,
1290
               QAbstractItemModel *model,
1291
               const QModelIndex& index) const {
1292
       FileForm *flFrm = static_cast<FileForm*>(editor);
1293
       if (!(flFrm->dataIsChanged())) return;
1294
 
1295
       m_Data = flFrm->data();
1296
       if (!m_Data.isEmpty()) { //    
1297
                                   model->setData(index, QVariant (m_Data), Qt::EditRole);
1298
 
1299
                              }
1300
       else {       //    ( )
1301
           QVariant val_null;
1302
           model->setData(index, val_null, Qt::EditRole);
1303
 
1304
            }
1305
 
1306
 
1307
 
1308
   }
1309
 
1310
 
1311
 
1312
   void FileDelegate::updateEditorGeometry(
1313
               QWidget *editor,
1314
               const QStyleOptionViewItem &option,
1315
               const QModelIndex& /* index */) const {
1316
 
1317
 
1318
   //    QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
1319
     //      editor->setGeometry(r);
1320
 
1321
        editor->setGeometry(option.rect);
1322
   }
1323
 
1324
 
1325
 
1326