Хранилища Subversion OpenInventory

Редакция

Редакция 226 | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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
 
227 pingvin 649
 QMap<QString, QString>  FilterValueDelegate::getItems(QString inctance){ //      -
650
    QSqlQuery q;
651
    QString sql_str;
652
    QStringList keys_list;
653
    QStringList values_list;
654
    QMap<QString, QString> result_map;
225 pingvin 655
 
226 pingvin 656
 
657
 
658
 
227 pingvin 659
    bool ok;
660
    sql_str = tr("select * from `");
661
    sql_str.append(inctance);
662
    sql_str.append(tr("`"));
663
    ok = q.prepare(sql_str);
664
    ok = q.exec();
665
    if(!ok) return result_map;
666
    while (q.next()){
667
        QString key_tmp;
668
        QString value_tmp;
226 pingvin 669
 
227 pingvin 670
         key_tmp = q.record().value(0).asString(); // id 
671
         //keys_list.append(tmp_str); //    
226 pingvin 672
 
227 pingvin 673
        value_tmp = q.record().value(1).asString(); //  
674
 
675
        result_map.insert(key_tmp,  value_tmp);
676
        // values_list.append(tmp_str);
677
 
678
 
679
    }
680
 
681
 
682
 
683
 
684
 
685
    return result_map;
686
 
687
 }
688
 
689
 
690
 
691
 
224 pingvin 692
//! [1]
693
QWidget *FilterValueDelegate::createEditor(QWidget *parent,
694
    const QStyleOptionViewItem &/* option */,
695
    const QModelIndex &/* index */) const
696
{
697
    QStringList Name_list;
698
 
699
    Name_list = items;
700
 
701
    QComboBox *editor = new QComboBox(parent);
702
 
703
/*
704
    editor->addItem("0");
705
    editor->addItem("1");
706
    editor->addItem("2");
707
    editor->addItem("3");
708
    editor->addItem("4");
709
    editor->addItem("5");
710
    editor->addItem("6");
711
    editor->addItem("7");
712
    editor->addItem("8");
713
    editor->addItem("9");
714
*/
715
 
716
//  editor->addItem("0");
717
 
718
// // //    editor->addItems(Name_list);
225 pingvin 719
    editor->setEditable(true);
224 pingvin 720
    return editor;
721
}
722
//! [1]
723
 
225 pingvin 724
 
725
 
726
 
226 pingvin 727
TField FilterValueDelegate::getField(const QModelIndex &index) //   (  )    
728
{
729
    const QAbstractItemModel * model = index.model();
225 pingvin 730
 
226 pingvin 731
    TField field_result;
732
    QSqlQuery q;
733
    QString sql_str;
734
    bool ok;
225 pingvin 735
 
736
 
226 pingvin 737
    // QString fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
738
  //  QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
739
    field_result.FieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
740
    field_result.ClassIdentifer = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
225 pingvin 741
 
226 pingvin 742
    sql_str = tr("select * from `DescriptionOfClasses` where `ClassIdentifer` = '");
743
    sql_str.append(field_result.ClassIdentifer);
744
    sql_str.append(tr("' and `FieldName` = '"));
745
    sql_str.append(field_result.FieldName);
746
    sql_str.append(tr("'"));
225 pingvin 747
 
226 pingvin 748
    ok = q.prepare(sql_str);
225 pingvin 749
 
226 pingvin 750
    if(!ok)  return field_result;
751
 
752
    ok = q.exec();
753
 
754
    if(!ok)  return field_result;
755
 
756
    int ID_index = q.record().indexOf(tr("ID"));
757
 
758
    int FieldAlias_index = q.record().indexOf(tr("FieldAlias"));
759
 
760
    int FieldType_index = q.record().indexOf(tr("FieldType"));
761
 
762
    int DefaultValue_index = q.record().indexOf(tr("DefaultValue"));
763
 
764
    int Comment_index = q.record().indexOf(tr("Comment"));
765
 
766
 
767
 
768
    while(q.next()){
769
    field_result.ID = q.value(ID_index).asString();
770
    field_result.FieldAlias = q.value(FieldAlias_index).asString();
771
    field_result.FieldType = q.value(FieldType_index).asString();
772
    field_result.DefaultValue = q.value(DefaultValue_index).asString();
773
    field_result.Comment = q.value(Comment_index).asString();
774
    }
775
 
776
    return field_result;
777
 
778
 
779
 
780
}
781
 
782
 
783
 
784
 
785
 
224 pingvin 786
//! [2]
787
void FilterValueDelegate::setEditorData(QWidget *editor,
788
                                    const QModelIndex &index) const
789
{
790
   // int value = index.model()->data(index, Qt::EditRole).toInt();
791
 
792
 //   QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
793
 //   spinBox->setValue(value);
794
 
795
 
796
const QAbstractItemModel * model = index.model();
797
 
798
 
799
QString value = index.model()->data(index, Qt::EditRole).toString();
800
QString class_id = index.model()->data(model->index(index.row(),2), Qt::EditRole).toString();
801
QComboBox *comboBox = static_cast<QComboBox*>(editor);
225 pingvin 802
QString inctance; //  
803
QString fieldName;// 
804
QSqlQuery q;
805
QString sql_str;
806
QStringList itemsList;
807
bool ok;
226 pingvin 808
TField Field;
224 pingvin 809
 
226 pingvin 810
Field = getField(index);
225 pingvin 811
 
812
 
227 pingvin 813
if (Field.FieldType == tr("pointer")){ //       -
814
    QString parent_inctance;
815
    QMap<QString, QString> items_map;
816
    QStringList items_list;
226 pingvin 817
 
227 pingvin 818
    parent_inctance = getClassInctanc(Field.DefaultValue);//    -
819
    items_map = getItems(parent_inctance);
820
    items_list = items_map.values();
226 pingvin 821
 
227 pingvin 822
    comboBox->addItems(items_list);
823
    if (items_list.indexOf(value)!=-1) comboBox->setCurrentIndex(items_list.indexOf(value));
824
    return;
226 pingvin 825
 
826
 
227 pingvin 827
 
828
}
829
 
830
 
831
 
225 pingvin 832
fieldName = index.model()->data(model->index(index.row(),4), Qt::EditRole).toString();
833
 
834
 
835
inctance = FilterValueDelegate::getClassInctanc(class_id);
836
if (!inctance.isEmpty() && !fieldName.isEmpty()){
837
sql_str = tr("select `");
838
sql_str.append(fieldName);
839
sql_str.append(tr("` from `"));
840
sql_str.append(inctance);
841
sql_str.append(tr("`"));
842
ok = q.prepare(sql_str);
843
ok = q.exec();
844
 
845
while(q.next()){
846
QString tmp_str;
847
tmp_str = q.record().value(fieldName).asString();
848
itemsList.append(tmp_str);
849
FilterValueDelegate::items.append(tmp_str);
850
        }
851
if(!itemsList.isEmpty()) {
852
                            comboBox->addItems(itemsList);
853
                            items = itemsList; //  
854
                         }
855
 
856
}
857
 
858
 
859
 
860
 
861
 
224 pingvin 862
//QStringList ID_list;
863
//QStringList Name_list;
864
int curr_index;
865
 
866
//ID_list = items.keys();
867
//Name_list = items.values();
868
 
225 pingvin 869
curr_index = itemsList.indexOf(value);
224 pingvin 870
if (curr_index==-1) return;
871
comboBox->setCurrentIndex(curr_index);
872
/****************************************************
873
if (value == "0") comboBox->setCurrentIndex(0);
874
if (value == "1") comboBox->setCurrentIndex(1);
875
if (value == "2") comboBox->setCurrentIndex(2);
876
if (value == "3") comboBox->setCurrentIndex(3);
877
if (value == "4") comboBox->setCurrentIndex(4);
878
if (value == "5") comboBox->setCurrentIndex(5);
879
if (value == "6") comboBox->setCurrentIndex(6);
880
if (value == "7") comboBox->setCurrentIndex(7);
881
if (value == "8") comboBox->setCurrentIndex(8);
882
if (value == "9") comboBox->setCurrentIndex(9);
883
******************************************************/
225 pingvin 884
comboBox->setEditable(true);
224 pingvin 885
 
886
 
887
//comboBox->setItemText(0, value);
888
 
889
}
890
//! [2]
891
 
892
//! [3]
893
void FilterValueDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
894
                                   const QModelIndex &index) const
895
{
896
  //  QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
897
  //  spinBox->interpretText();
898
  //  int value = spinBox->value();
899
 
900
  //  model->setData(index, value, Qt::EditRole);
225 pingvin 901
 
224 pingvin 902
    QComboBox *comboBox = static_cast<QComboBox*>(editor);
227 pingvin 903
     int currIndex;
224 pingvin 904
    currIndex = comboBox->currentIndex();
227 pingvin 905
 
224 pingvin 906
    if (currIndex==-1) return;
907
 
227 pingvin 908
    QString new_value = comboBox->itemText(currIndex);
909
 
910
    QString curr_value = model->data(index, Qt::EditRole).toString();//   
911
 
912
 
913
 
914
    QString class_id = model->data(model->index(index.row(),2), Qt::EditRole).toString();// ,    
915
 
916
 
917
    QStringList ID_list;
918
    QStringList Name_list;
919
 
920
 
921
    TField Field;
922
 
923
    Field = getField(index);
924
 
925
    if(Field.FieldType==tr("pointer")){
926
        QString parent_inctance;
927
        QMap<QString, QString> items_map;
928
        QStringList keys_list; //  ID   
929
        QStringList values_list; //    
930
 
931
 
932
 
933
        parent_inctance = getClassInctanc(Field.DefaultValue);//    -
934
        items_map = getItems(parent_inctance); //
935
        keys_list = items_map.keys();
936
        values_list = items_map.values();
937
 
938
        if( values_list.indexOf(new_value)!=-1){ //
939
 
940
            QString new_key =  keys_list.at(values_list.indexOf(new_value)); //   
941
            if (new_key != curr_value) model->setData(index, new_key, Qt::EditRole);
942
            return;
943
        }
944
 
945
 
946
 
947
 
948
    }
949
 
950
 
951
 
952
 
953
 
954
 
955
 
956
 //   QString new_value = comboBox->itemText(currIndex);
957
 
958
 
959
 
960
 
224 pingvin 961
    // ID_list = items.keys();
962
    // Name_list = items.values();
963
 
964
 //   QString value = ID_list.at(currIndex);
227 pingvin 965
  //  QString value = items.at(currIndex);
966
     model->setData(index, new_value, Qt::EditRole);
225 pingvin 967
 
968
 
224 pingvin 969
}
225 pingvin 970
 
971
 
972
 
973
 
974
 
975
 
976
 
977
 
224 pingvin 978
//! [3]
979
 
219 pingvin 980
//! [4]
224 pingvin 981
void FilterValueDelegate::updateEditorGeometry(QWidget *editor,
982
    const QStyleOptionViewItem &option, const QModelIndex &/* index */) const
983
{
984
    editor->setGeometry(option.rect);
985
}
986
 
987
 
988
 
989
 
990
 
991
 
992
 
993
 
994
 
995
 
996
 
997
 
998
 
999
 
1000
 
1001
//! [4]
123 pingvin 1002
MyDEDelegate::MyDEDelegate(
1003
                       bool calpopup,
1004
                       QObject *parent)
1005
               : QItemDelegate(parent),
1006
                 m_calpopup(calpopup) {
1007
   }
1008
 
1009
   QWidget *MyDEDelegate::createEditor(
1010
               QWidget *parent,
1011
               const QStyleOptionViewItem& /* option */,
1012
               const QModelIndex& /* index */) const {
1013
       QDateEdit *editor = new QDateEdit(parent);
1014
       editor->setCalendarPopup(m_calpopup);
1015
       editor->installEventFilter(const_cast<MyDEDelegate*>(this));
1016
       return editor;
1017
   }
1018
 
1019
   void MyDEDelegate::setEditorData(
1020
                   QWidget *editor,
1021
                   const QModelIndex &index) const {
1022
       QDate value = index.model()->data(
1023
               index, Qt::EditRole).toDate();
1024
       QDateEdit *de = static_cast<QDateEdit*>(editor);
1025
       de->setDate(value);
1026
   }
1027
 
1028
   void MyDEDelegate::setModelData(
1029
               QWidget *editor,
1030
               QAbstractItemModel *model,
1031
               const QModelIndex& index) const {
1032
       QDateEdit *de = static_cast<QDateEdit*>(editor);
1033
       de->interpretText();
1034
       QDate value = de->date();
1035
       model->setData(index, value);
1036
   }
1037
 
1038
   void MyDEDelegate::updateEditorGeometry(
1039
               QWidget *editor,
1040
               const QStyleOptionViewItem &option,
1041
               const QModelIndex& /* index */) const {
169 pingvin 1042
 
1043
 
1044
 
123 pingvin 1045
       editor->setGeometry(option.rect);
1046
   }
138 pingvin 1047
 
1048
 
1049
 
1050
 
1051
 
1052
   CPictureDelegate::CPictureDelegate( QObject * parent ) : QItemDelegate(parent)
1053
   {
1054
   }
1055
 
1056
   void CPictureDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
1057
   {
1058
           m_pxPicture.fill( QColor(Qt::white) );
1059
 
1060
           const QAbstractItemModel * model = index.model();
1061
           QString sFileName = model->data( index, Qt::DisplayRole ).toString();
1062
 
1063
           if ( !sFileName.isEmpty() )
1064
                   m_pxPicture.load( sFileName );
1065
           else {
1066
               //QItemDelegate::paint(painter, option, index);
1067
           return;
1068
           }
1069
 
1070
           QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
1071
                                                             ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
1072
                                                             QPalette::Disabled;
1073
 
1074
            if (option.state & QStyle::State_Selected)
1075
                    painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
1076
 
1077
           int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
1078
           int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
1079
           painter->drawPixmap( nX, nY, m_pxPicture );
1080
 
1081
 
1082
       ////    drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
1083
 
1084
           /*
1085
           QPen pen = painter->pen();
1086
           painter->setPen(option.palette.color(QPalette::Mid));
1087
           painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
1088
           painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
1089
           painter->setPen(pen);
1090
   */
1091
   }
158 pingvin 1092
 
1093
 
1094
 
1095
 
1096
   void TimeEditDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
1097
                            const QModelIndex &index) const
1098
   {
1099
       int datetime = index.model()->data(index, Qt::DisplayRole).toInt();
1100
 
1101
       QString indexvalue = "";
1102
 
1103
       if (datetime > 0)
1104
       {
1105
           QDateTime dateTime2 = QDateTime();
1106
           dateTime2.setTime_t(datetime);
1107
           indexvalue = dateTime2.toString(this->timeformat);
1108
       }
1109
       else
1110
       {
1111
           indexvalue = tr("Date not set");
1112
       }
1113
 
1114
       Q_ASSERT(index.isValid());
1115
 
1116
       QStyleOptionViewItemV3 opt = setOptions(index, option);
1117
 
1118
       const QStyleOptionViewItemV2 *v2 = qstyleoption_cast<const QStyleOptionViewItemV2 *>(&option);
1119
       opt.features = v2 ? v2->features
1120
                       : QStyleOptionViewItemV2::ViewItemFeatures(QStyleOptionViewItemV2::None);
1121
       const QStyleOptionViewItemV3 *v3 = qstyleoption_cast<const QStyleOptionViewItemV3 *>(&option);
1122
       opt.locale = v3 ? v3->locale : QLocale();
1123
       opt.widget = v3 ? v3->widget : 0;
1124
 
1125
       // prepare
1126
       painter->save();
1127
 
1128
       painter->setClipRect(opt.rect);
1129
 
1130
       // get the data and the rectangles
1131
       QVariant value;
1132
 
1133
       QPixmap pixmap;
1134
       QRect decorationRect;
1135
       value = index.data(Qt::DecorationRole);
1136
 
1137
       QString text;
1138
       QRect displayRect;
1139
       value = index.data(Qt::DisplayRole);
1140
       if (value.isValid()) {
1141
           text = indexvalue;
1142
           displayRect = textRectangle(painter, option.rect, opt.font, text);
1143
       }
1144
 
1145
       QRect checkRect;
1146
       Qt::CheckState checkState = Qt::Unchecked;
1147
       value = index.data(Qt::CheckStateRole);
1148
       if (value.isValid()) {
1149
           checkState = static_cast<Qt::CheckState>(value.toInt());
1150
           checkRect = check(opt, opt.rect, value);
1151
       }
1152
 
1153
       // do the layout
1154
       doLayout(opt, &checkRect, &decorationRect, &displayRect, false);
1155
       // draw the item
1156
 
1157
       drawBackground(painter, opt, index);
1158
       drawCheck(painter, opt, checkRect, checkState);
1159
       drawDecoration(painter, opt, decorationRect, pixmap);
1160
       drawDisplay(painter, opt, displayRect, text);
1161
       drawFocus(painter, opt, displayRect);
1162
 
1163
       // done
1164
       painter->restore();
1165
   }
180 pingvin 1166
 
1167
 
1168
 
1169
 
1170
 
1171
 
1172
   IconDelegate::IconDelegate( QObject * parent ) : QItemDelegate(parent)
1173
   {
1174
   }
1175
 
1176
 
1177
 
1178
 
1179
 
1180
   QWidget *IconDelegate::createEditor(QWidget *parent,
1181
       const QStyleOptionViewItem &/* option */,
1182
       const QModelIndex & /* index */) const
1183
   {
1184
 
1185
       IconForm *editor = new IconForm(parent);
1186
 
1187
       return editor;
1188
 
1189
   }
1190
 
1191
 
1192
 
1193
 
1194
   void IconDelegate::setEditorData(QWidget *editor,
1195
                                       const QModelIndex &index) const
1196
   {
1197
 
1198
 
1199
       const QAbstractItemModel * model = index.model();
1200
       IconForm *icnFrm = static_cast<IconForm*>(editor);
1201
       QVariant currentImage = model->data(index,0);
1202
       QByteArray bytes = currentImage.toByteArray();
1203
       if (currentImage.isValid()) {
1204
            m_pxPicture.loadFromData(bytes);
1205
       }
1206
       else {
1207
           //QItemDelegate::paint(painter, option, index);
1208
 
1209
 
1210
           return;
1211
       }
1212
       icnFrm->setPixmap(m_pxPicture);
1213
     //  tblView->setRowHeight(index.row(), icnFrm->geometry().height());
1214
   }
1215
 
1216
 
1217
 
1218
 
1219
   void IconDelegate::setModelData(
1220
               QWidget *editor,
1221
               QAbstractItemModel *model,
1222
               const QModelIndex& index) const {
1223
       IconForm *icnFrm = static_cast<IconForm*>(editor);
1224
       if (!(icnFrm->dataIsChanged())) return;
1225
 
1226
       m_pxPicture = icnFrm->pixmap();
1227
       QImage currentImage = m_pxPicture.toImage();
1228
          QByteArray bytes;
1229
          QBuffer buffer(&bytes);
1230
          buffer.open(QIODevice::WriteOnly);
1231
          currentImage.save(&buffer, "PNG");
1232
 
1233
 
1234
 
1235
 
1236
         model->setData(index, QVariant (bytes), Qt::EditRole);
1237
 
1238
     //    int widht = m_pxPicture.width();
1239
     //    int heigh =  m_pxPicture.height();
1240
     //    tblView->setRowHeight(index.row(), heigh + 10);
1241
         // model->submitAll();
1242
 
1243
 
1244
 
1245
 
1246
      // model->setData(index, value);
1247
   }
1248
 
1249
 
1250
 
1251
   void IconDelegate::updateEditorGeometry(
1252
               QWidget *editor,
1253
               const QStyleOptionViewItem &option,
1254
               const QModelIndex& /* index */) const {
1255
 
1256
 
1257
   //    QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
1258
     //      editor->setGeometry(r);
1259
 
1260
        editor->setGeometry(option.rect);
1261
   }
1262
 
1263
 
1264
 
1265
 
1266
   void IconDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
1267
   {
1268
 
1269
 
1270
          m_pxPicture.fill( QColor(Qt::white) );
1271
 
1272
          const QAbstractItemModel * model = index.model();
1273
          QString sFileName = model->data( index, Qt::DisplayRole ).toString();
1274
 
1275
          QVariant currentImage = model->data(index,0);
1276
 
1277
 
1278
           QByteArray bytes = currentImage.toByteArray();
1279
     //       QImage image;
1280
     //       image.loadFromData(bytes);
1281
 
1282
 
1283
           if (currentImage.isValid()) {
1284
                m_pxPicture.loadFromData(bytes);
1285
 
1286
           }
1287
           else {
1288
               //QItemDelegate::paint(painter, option, index);
1289
           return;
1290
           }
1291
 
1292
 
1293
 
1294
 
1295
 
1296
           /*
1297
 
1298
           if ( !sFileName.isEmpty() )
1299
                   m_pxPicture.load( sFileName );
1300
           else {
1301
               //QItemDelegate::paint(painter, option, index);
1302
           return;
1303
           }
1304
*/
1305
 
1306
 
205 pingvin 1307
 
1308
 
1309
 
180 pingvin 1310
           QPalette::ColorGroup cg = (option.state & QStyle::State_Enabled) ?
1311
                                                             ((option.state & QStyle::State_Active) ? QPalette::Normal : QPalette::Inactive ) :
1312
                                                             QPalette::Disabled;
1313
 
1314
            if (option.state & QStyle::State_Selected)
1315
 
1316
 
1317
                painter->fillRect(option.rect, option.palette.color(cg, QPalette::Highlight));
1318
 
205 pingvin 1319
 
1320
 
180 pingvin 1321
           int nX = option.rect.x() + ( ( option.rect.width() - m_pxPicture.rect().width() ) / 2 );
1322
           int nY = option.rect.y() + ( ( option.rect.height() - m_pxPicture.rect().height() ) / 2 );
205 pingvin 1323
   //        painter->setRenderHint(QPainter::Antialiasing);
180 pingvin 1324
           painter->drawPixmap( nX, nY, m_pxPicture );
1325
 
1326
       //    drawFocus(painter, option, option.rect.adjusted(0, 0, -1, -1)); // since we draw the grid ourselves
1327
 
1328
           /*
1329
           QPen pen = painter->pen();
1330
           painter->setPen(option.palette.color(QPalette::Mid));
1331
           painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
1332
           painter->drawLine(option.rect.topRight(), option.rect.bottomRight());
1333
           painter->setPen(pen);
1334
   */
1335
}
1336
 
1337
 
1338
   void IconDelegate::setTableView(QTableView* table){
1339
       tblView = table;
1340
   }
201 pingvin 1341
 
1342
 
1343
 
1344
 
1345
 
1346
 
1347
 
1348
 
1349
   FileDelegate::FileDelegate( QObject * parent ) : QItemDelegate(parent)
1350
   {
1351
   }
1352
 
1353
 
1354
 
1355
 
1356
 
1357
   QWidget *FileDelegate::createEditor(QWidget *parent,
1358
       const QStyleOptionViewItem &/* option */,
1359
       const QModelIndex & /* index */) const
1360
   {
1361
 
1362
       FileForm *editor = new FileForm(parent);
1363
 
1364
       return editor;
1365
 
1366
   }
1367
 
1368
 
1369
 
1370
 
1371
   void FileDelegate::setEditorData(QWidget *editor,
1372
                                       const QModelIndex &index) const
1373
   {
1374
 
1375
 
1376
       const QAbstractItemModel * model = index.model();
1377
       FileForm *flFrm = static_cast<FileForm*>(editor);
202 pingvin 1378
       QVariant currentData = model->data(index,Qt::EditRole);
1379
 
1380
      // QVariant currentData = QSqlTableModel::data(index, 0);
201 pingvin 1381
       QByteArray bytes = currentData.toByteArray();
1382
       if (currentData.isValid()) flFrm->setData(bytes);
1383
       else return;
1384
 
1385
   }
1386
 
1387
 
1388
 
1389
 
1390
void FileDelegate::setModelData(
1391
               QWidget *editor,
1392
               QAbstractItemModel *model,
1393
               const QModelIndex& index) const {
1394
       FileForm *flFrm = static_cast<FileForm*>(editor);
1395
       if (!(flFrm->dataIsChanged())) return;
1396
 
1397
       m_Data = flFrm->data();
1398
       if (!m_Data.isEmpty()) { //    
1399
                                   model->setData(index, QVariant (m_Data), Qt::EditRole);
1400
 
1401
                              }
1402
       else {       //    ( )
1403
           QVariant val_null;
1404
           model->setData(index, val_null, Qt::EditRole);
1405
 
1406
            }
1407
 
1408
 
1409
 
1410
   }
1411
 
1412
 
1413
 
1414
   void FileDelegate::updateEditorGeometry(
1415
               QWidget *editor,
1416
               const QStyleOptionViewItem &option,
1417
               const QModelIndex& /* index */) const {
1418
 
1419
 
1420
   //    QRect r(option.rect.x() + option.rect.width()/2 - 7, option.rect.y() + option.rect.height()/2 - 7, 150, 100);
1421
     //      editor->setGeometry(r);
1422
 
1423
        editor->setGeometry(option.rect);
1424
   }
1425
 
1426
 
1427
 
1428