Хранилища Subversion OpenInventory

Редакция

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

Редакция Автор № строки Строка
88 pingvin 1
#include "mainwindow.h"
2
#include "ui_mainwindow.h"
3
 
4
MainWindow::MainWindow(QWidget *parent) :
5
    QMainWindow(parent),
6
    ui(new Ui::MainWindow)
7
{
8
    ui->setupUi(this);
106 pingvin 9
   // connect( ui->comboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(on_currentIndexChanged(int)));
88 pingvin 10
 
89 pingvin 11
   MainWindow::readModel();
12
 
91 pingvin 13
   //QString model_str;
14
   MainWindow::creatModelString();
15
   int i;
16
   i++;
93 pingvin 17
    QFile file_tmp("./temp.txt");
92 pingvin 18
    bool ok;
19
    ok = file_tmp.open(QIODevice::ReadWrite | QIODevice::Text);
20
    QTextStream out(&file_tmp);
21
    out << MainWindow::modelString;
22
    file_tmp.close();
91 pingvin 23
 
114 pingvin 24
    ui->treeWidget->setAlternatingRowColors(true);
25
    ui->treeWidget_2->setAlternatingRowColors(true);
93 pingvin 26
    MainWindow::openDataBase();
114 pingvin 27
    MainWindow::buildPreviewModel(tr(" "), tr("7"));
104 pingvin 28
    MainWindow::initComboBox();
109 pingvin 29
    getDatabaseData();
93 pingvin 30
 
103 pingvin 31
 
88 pingvin 32
}
33
 
34
MainWindow::~MainWindow()
35
{
36
    delete ui;
37
}
38
 
39
void MainWindow::changeEvent(QEvent *e)
40
{
41
    QMainWindow::changeEvent(e);
42
    switch (e->type()) {
43
    case QEvent::LanguageChange:
44
        ui->retranslateUi(this);
45
        break;
46
    default:
47
        break;
48
    }
49
}
89 pingvin 50
 
51
 
52
bool MainWindow::readModel(){
53
    bool result;
54
    QString model_str;
55
    QFile file(":/model.txt");
92 pingvin 56
 
91 pingvin 57
   rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
89 pingvin 58
    rootItem1->setText(0, tr(" 1"));
59
   // rootItem2 = new  QTreeWidgetItem(rootItem1);
60
   // rootItem2->setText(0, tr(" 2"));
61
    rootItem1->setCheckState(0,Qt::Checked);
62
   // rootItem2->setCheckState(0,Qt::Checked);
63
 
64
 
65
 
66
    result = file.open(QIODevice::ReadOnly);
67
    if (result) {
90 pingvin 68
                     model_str = QString(tr(file.readAll()));
89 pingvin 69
                }
70
 
91 pingvin 71
    setupModelData(model_str.split(QString("\n")), rootItem1);
89 pingvin 72
 
73
 
74
    return result;
75
 
76
}
77
 
78
 
79
void MainWindow::setupModelData(const QStringList &lines, QTreeWidgetItem *parent)
80
{
81
    QList<QTreeWidgetItem*> parents;
82
    QList<int> indentations;
83
    parents << parent;
84
    indentations << 0;
85
 
86
    int number = 0;
87
 
88
    while (number < lines.count()) {
89
        int position = 0;
90
        while (position < lines[number].length()) {
91
            if (lines[number].mid(position, 1) != " ")
92
                break;
93
            position++;
94
        }
95
 
96
        QString lineData = lines[number].mid(position).trimmed();
97
 
98
        if (!lineData.isEmpty()) {
99
            // Read the column data from the rest of the line.
100
            QStringList columnStrings = lineData.split("\t", QString::SkipEmptyParts);
101
            QList<QVariant> columnData;
102
            for (int column = 0; column < columnStrings.count(); ++column)
103
                columnData << columnStrings[column];
104
 
105
            if (position > indentations.last()) {
106
                // The last child of the current parent is now the new parent
107
                // unless the current parent has no children.
108
 
109
                if (parents.last()->childCount() > 0) {
110
                    parents << parents.last()->child(parents.last()->childCount()-1);
111
                    indentations << position;
112
                }
113
            } else {
114
                while (position < indentations.last() && parents.count() > 0) {
115
                    parents.pop_back();
116
                    indentations.pop_back();
117
                }
118
            }
119
 
120
            // Append a new item to the current parent's list of children.
121
          // // //  parents.last()->appendChild(new QTreeWidgetItem(columnData, parents.last()));
122
            QTreeWidgetItem* itm_tmp;
123
            itm_tmp = new QTreeWidgetItem( parents.last());
124
 
125
            itm_tmp->setText(0, QString(columnData.at(0).toString()));
126
            if (columnData.at(1).toString() == "true") {
127
                                                            itm_tmp->setCheckState(0,Qt::Checked);
128
                                                        }
129
            else itm_tmp->setCheckState(0,Qt::Unchecked);
90 pingvin 130
            itm_tmp->setText(1, QString(columnData.at(2).toString()));
131
            itm_tmp->setText(2, QVariant(number).toString()); //    - 
132
            itm_tmp->setText(3, QVariant(indentations.size()).toString());
89 pingvin 133
        }
134
 
135
        number++;
136
    }
137
}
138
 
139
 
140
/*
141
void MainWindow::openBase()
142
{
143
 
144
        db = QSqlDatabase::addDatabase("QMYSQL");
145
        pdb = &db;
146
        raportFrm.pdb = &db;
147
 
148
        pdb->setHostName(hostName);
149
        pdb->setDatabaseName(baseName);
150
        pdb->setUserName(userName);
151
        pdb->setPassword(password);
152
        bool ok = pdb->open();
153
        if (!ok) {
154
                                QMessageBox::critical( //     .
155
                                                                                this,                      //  .
156
                                                                                QObject::tr("Database Error"),   // .
157
                                                                                pdb->lastError().text());          //  .
158
                         }
159
        if (ok)
160
        {lineEdit-> insert(tr("  "));
161
        }
162
        else {lineEdit-> insert(tr(" .  : "));
163
                  lineEdit-> insert(pdb->lastError().text());
164
                 }
165
 
166
 
167
 
168
        model = new QSqlTableModel(this);
169
        model->setTable(tableName);
170
 
171
        model->setEditStrategy(QSqlTableModel::OnManualSubmit);
172
        model->setSort(0, Qt::AscendingOrder);
173
 
174
        model->select();
175
 
176
 
177
        QSqlField field(tr("age"), QVariant::Int);
178
        field.setValue(QString(tr("123")));
179
 
180
        bool okey;
181
        int index;
182
        QSqlRecord record;
183
 
184
 
185
        tableView->setModel(model);
186
        view.setModel(model);
187
 
188
 
189
        tableView->setAlternatingRowColors(true);
190
        view.setAlternatingRowColors(true);
191
 
192
        tableView->resizeColumnsToContents();
193
 
194
 
195
        view.resizeColumnsToContents();
196
 
197
        tableView->show();
198
 
199
 
200
 
201
        initTreeWidget();
202
}
203
*/
91 pingvin 204
 
205
 
206
void MainWindow::creatModelString(){
207
 
208
    MainWindow::modelString.clear(); //   
209
 //   for (int i =0; i < rootItem1->childCount(); ++i )
210
 
211
    {MainWindow::addChildsString(rootItem1, 0);}
212
 
213
    int root_item_child_count;
214
    int i;
215
    root_item_child_count = rootItem1->childCount();
216
    i++;
217
 
218
}
219
 
220
 
221
void MainWindow::addChildsString(QTreeWidgetItem *parentItem, int level){
222
    int child_count;
223
    child_count = parentItem->childCount();
224
    for (int i=0; i < child_count; ++i){
225
        for (int m=0; m < level; ++m){
226
        modelString.append(tr("    ")); //   -  ,     
227
    }
228
        modelString.append(parentItem->child(i)->text(0)); //    -  
229
        modelString.append(tr("\t\t\t\t")); //   -   
230
        if (parentItem->child(i)->checkState(0) ==  Qt::Checked) {modelString.append(tr("true"));} //   ,  "true",      
231
            else {modelString.append(tr("false"));} //     -  "false",       
232
        modelString.append(tr("\t\t\t\t")); //   -   
233
        modelString.append(parentItem->child(i)->text(1)); //    - ID 
234
         modelString.append(tr("\n")); //  
235
        if (parentItem->child(i)->childCount() > 0) { MainWindow::addChildsString(parentItem->child(i), level+1);}
236
 
237
    }
238
int i;
239
i++;
240
}
93 pingvin 241
 
242
 
243
bool MainWindow::openDataBase(){
244
    QString errorString;
245
    sql = QSqlDatabase::addDatabase("QMYSQL");
246
    sql.setDatabaseName(tr("an_db"));
247
    sql.setHostName(tr("localhost"));
248
    sql.setUserName(tr("an"));
249
    sql.setPassword(tr("393939"));
250
 
251
    bool ok;
252
    ok = sql.open();
253
 
254
    /*
255
    if (!ok) {
256
                                QMessageBox::critical( //     .
257
                                                                                this,                      //  .
258
                                                                                QObject::tr("Database Error"),   // .
259
                                                                                sql.lastError().text() );         //  .
260
 
261
 
262
                            }
263
 
264
*/
265
    if (!ok) {
266
                            QMessageBox::critical( //     .
267
                                                                            this,                      //  .
268
                                                                            QObject::tr("Database Error"),   // .
269
                                                                            sql.lastError().text());          //  .
270
                     }
271
else {
272
 
273
        QMessageBox::information( //     .
274
                                                        this,                      //  .
275
                                                        QObject::tr("Database Connect"),   // .
276
                                                        QObject::tr("     ."));         //  .
277
 
278
 
279
 
280
 
281
 
282
    }
283
 
284
     return ok;
285
}
94 pingvin 286
 
287
 
97 pingvin 288
bool MainWindow::buildPreviewModel(QString modelName, QString rootClassID){
94 pingvin 289
QSqlQuery q;
290
QString sql_str;
95 pingvin 291
QString field_name_str;
292
QString root_class_name;
293
bool ok;
94 pingvin 294
int field_name;
95 pingvin 295
 
296
//ui->treeWidget->clear();
297
 
298
root_class_name = MainWindow::ClassName(rootClassID); //   
299
 
300
rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
106 pingvin 301
root_items_list << rootItem1;
97 pingvin 302
 rootItem1->setText(0, modelName);
303
 
95 pingvin 304
 rootItem1->setCheckState(0,Qt::Checked);
104 pingvin 305
 rootItems.append(rootItem1);
95 pingvin 306
 
307
 
104 pingvin 308
 
97 pingvin 309
rootItem2 = new  QTreeWidgetItem(rootItem1);
310
rootItem2->setText(0, root_class_name);
311
rootItem2->setText(1, rootClassID);
312
rootItem2->setCheckState(0,Qt::Checked);
95 pingvin 313
 
98 pingvin 314
addChildsItems(rootItem2);
95 pingvin 315
 
94 pingvin 316
sql_str = tr("select * "
95 pingvin 317
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
94 pingvin 318
             );
319
sql_str.append(rootClassID);
320
 
95 pingvin 321
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
94 pingvin 322
 
95 pingvin 323
q.prepare(sql_str);
324
 
325
ok = q.exec();
326
if (!ok) {
327
                        QMessageBox::critical( //     .
328
                                                                        this,                      //  .
329
                                                                        QObject::tr("Database Error"),   // .
330
                                                                        q.lastError().text());          //  .
331
                 }
332
field_name = q.record().indexOf(tr("FieldName"));
333
while(q.next()){
334
 
335
                field_name_str = q.value(field_name).toString();
336
 
337
                }
338
 
100 pingvin 339
 
340
 
341
 
94 pingvin 342
}
95 pingvin 343
 
344
 
107 pingvin 345
bool MainWindow::rebuildPreviewModel(QTreeWidgetItem* root_item, QString rootClassID){
346
QSqlQuery q;
347
QString sql_str;
348
QString field_name_str;
349
QString root_class_name;
350
bool ok;
351
int field_name;
95 pingvin 352
 
107 pingvin 353
//ui->treeWidget->clear();
354
 
355
root_class_name = MainWindow::ClassName(rootClassID); //   
356
 
357
 
358
// rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
359
// root_items_list << rootItem1;
360
// rootItem1->setText(0, modelName);
361
 
362
// rootItem1->setCheckState(0,Qt::Checked);
363
// rootItems.append(rootItem1);
364
 
365
// root_item->removeChild(root_item->child(0)); //    
366
if (root_item->childCount()==1){
367
root_item->child(0)->setText(0, root_class_name);
368
root_item->child(0)->setText(1, rootClassID);
369
root_item->child(0)->setCheckState(0,Qt::Checked);}
370
 
371
//delete rootItem2;
372
 
373
 
374
// rootItem2 = new  QTreeWidgetItem(rootItem1);
375
 
376
 
377
 
378
// rootItem2->setText(0, root_class_name);
379
// rootItem2->setText(1, rootClassID);
380
// rootItem2->setCheckState(0,Qt::Checked);
381
 
382
int child_count = root_item->child(0)->childCount();
383
 
384
 
385
 
386
for (int i=0; i < child_count; i++){
387
    root_item->child(0)->removeChild(root_item->child(0)->child(0));
388
}
389
 
390
child_count = root_item->child(0)->childCount();
391
addChildsItems(root_item->child(0));
392
/*
393
sql_str = tr("select * "
394
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
395
             );
396
sql_str.append(rootClassID);
397
 
398
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
399
 
400
q.prepare(sql_str);
401
 
402
ok = q.exec();
403
if (!ok) {
404
                        QMessageBox::critical( //     .
405
                                                                        this,                      //  .
406
                                                                        QObject::tr("Database Error"),   // .
407
                                                                        q.lastError().text());          //  .
408
                 }
409
field_name = q.record().indexOf(tr("FieldName"));
410
while(q.next()){
411
 
412
                field_name_str = q.value(field_name).toString();
413
 
414
                }
415
 
416
*/
417
 
418
 
419
}
420
 
421
 
422
 
423
 
95 pingvin 424
QString MainWindow::ClassName(QString class_id){ //     ID
425
    QSqlQuery q;
426
    QString sql_str;
427
    QString class_name_str;
428
    bool ok;
429
    int field_class_name;
430
 
431
    sql_str = tr("select * "
432
                 " from ListOfClasses where ListOfClasses.ID = '"       // ,     
433
                 );
434
 
435
    sql_str.append(class_id);
436
    sql_str.append(tr("' "));
437
 
438
    q.prepare(sql_str);
439
 
440
    ok = q.exec();
441
 
442
 
443
    if (!ok) {
444
                            QMessageBox::critical( //     .
445
                                                                            this,                      //  .
446
                                                                            QObject::tr("Database Error"),   // .
447
                                                                            q.lastError().text());          //  .
448
                     }
449
 
450
    field_class_name = q.record().indexOf(tr("ClassName"));
451
    while(q.next()){
452
 
453
                    class_name_str = q.value(field_class_name).toString();
454
 
455
                    }
456
 
457
    return class_name_str;
458
 
459
}
98 pingvin 460
 
461
void MainWindow::addChildsItems(QTreeWidgetItem *perent_class_item){ //          ,      
100 pingvin 462
QStringList chields_list;
463
QString parent_ID;
464
QTreeWidgetItem *Item_tmp;
465
parent_ID = perent_class_item->text(1);
466
int chields_count, i;
98 pingvin 467
 
100 pingvin 468
chields_list = MainWindow::classChields(parent_ID);
469
if(chields_list.isEmpty()) return;
470
chields_count = chields_list.size();
471
for (int l = 0; l < chields_count; ++l){
472
    QString chield_class_name, chield_id;
473
    chield_id = chields_list.at(l);
474
    chield_class_name = MainWindow::ClassName(chield_id);
475
    Item_tmp = new QTreeWidgetItem(perent_class_item);
476
    Item_tmp->setText(0, chield_class_name);
477
    Item_tmp->setText(1, chield_id);
478
    Item_tmp->setCheckState(0, Qt::Checked);
101 pingvin 479
    MainWindow::addChildsItems(Item_tmp);
98 pingvin 480
}
481
 
100 pingvin 482
i++;
99 pingvin 483
 
100 pingvin 484
}
485
 
486
 
99 pingvin 487
/********************************************************
488
*    "" 
489
*
490
*
491
*
492
********************************************************/
493
 
494
QStringList MainWindow::classChields(QString class_id){
495
   // QMap<QString, QString> map;
496
   // TClass class_tmp;
497
   // QList <TClass> chields_class_list; //   
498
    QStringList result;
499
    QSqlQuery q;
500
    QString sql_str;
501
    QString classIdentifer_str; //      -
502
    QString field_id_str;
503
    bool ok;
504
    int field_classIdentifer;
505
    sql_str = tr("select * "
506
                 " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
507
                 );
508
    sql_str.append(class_id);
509
 
510
    sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
511
 
512
    q.prepare(sql_str);
513
 
514
    ok = q.exec();
515
    if (!ok) {
516
                            QMessageBox::critical( //     .
517
                                                                            this,                      //  .
518
                                                                            QObject::tr("Database Error"),   // .
519
                                                                            q.lastError().text());          //  .
520
                     }
521
    field_classIdentifer = q.record().indexOf(tr("ClassIdentifer"));
522
      while(q.next()){
523
 
524
                    classIdentifer_str = q.value(field_classIdentifer).toString();
525
                    result.append(classIdentifer_str);
526
                    }
527
 
528
    return result;
529
 
530
  }
102 pingvin 531
 
532
 
533
QMap <QString, QString> MainWindow::getClassList(){ //   
534
QMap <QString, QString> result_map;
535
QSqlQuery q;
536
QString sql_str;
103 pingvin 537
QString class_name_str, class_id_str;
538
 
102 pingvin 539
int field_class_id, field_class_name;
540
bool ok;
541
sql_str = tr("select * "
542
             " from ListOfClasses "       // ,     
543
             );
544
 q.prepare(sql_str);
545
 ok = q.exec();
546
 if (!ok) {
547
                         QMessageBox::critical( //     .
548
                                                                         this,                      //  .
549
                                                                         QObject::tr("Database Error"),   // .
550
                                                                         q.lastError().text());          //  .
551
                         return result_map; //   -   
552
                  }
553
field_class_id = q.record().indexOf(tr("ID"));
554
field_class_name = q.record().indexOf(tr("ClassName"));
555
 
103 pingvin 556
while(q.next()){
102 pingvin 557
 
103 pingvin 558
                class_name_str = q.value(field_class_name).toString();
559
                class_id_str = q.value(field_class_id).toString();
560
                result_map[class_id_str] = class_name_str;
561
                }
102 pingvin 562
 return result_map;
563
}
104 pingvin 564
 
565
 
566
 
567
 
568
 
569
void MainWindow::initComboBox(){
570
 
106 pingvin 571
 
104 pingvin 572
    QStringList classesNameList;
573
    QStringList classesID_list;
574
    QStringList tmp_stringList;
575
 
576
 
577
 
578
    QString tmp_str;
579
 
580
    class_list_map = MainWindow::getClassList();
581
    classesID_list = class_list_map.keys();
582
    classesNameList = class_list_map.values();
583
    QMapIterator<QString, QString> interator(class_list_map);
584
//    ui->comboBox->addItems(classesID_list);
585
  //  ui->comboBox->addItems(classesNameList);
586
    while (interator.hasNext()) {
587
         interator.next();
588
         tmp_str =  interator.value();
589
         tmp_str.append(tr(" \t(ID="));
590
         tmp_str.append(interator.key());
591
         tmp_str.append(tr(")"));
592
         tmp_stringList << tmp_str;
593
 
594
     }
595
    ui->comboBox->addItems(tmp_stringList);
596
 
597
 
598
}
105 pingvin 599
 
600
 
106 pingvin 601
void MainWindow::on_comboBox_currentIndexChanged( int index ){
602
QStringList classesNameList;
603
QStringList classesID_list;
604
QString ID_str;
605
classesID_list = class_list_map.keys();
606
classesNameList = class_list_map.values();
607
ID_str =  classesID_list[index];
608
ui->label->setText(ID_str);
107 pingvin 609
if (root_items_list.indexOf(ui->treeWidget->currentItem()) != -1) rebuildPreviewModel(ui->treeWidget->currentItem(), ID_str);
610
 
611
 
105 pingvin 612
}
613
 
106 pingvin 614
void MainWindow::on_treeWidget_itemChanged ( QTreeWidgetItem * item, int column ){
615
    int i;
616
    i++;
617
 
618
}
619
 
620
 
621
void MainWindow::on_treeWidget_itemClicked ( QTreeWidgetItem * item, int column ){
622
    int i;
623
    i++;
108 pingvin 624
    if (root_items_list.indexOf(item) != -1) {
625
        QStringList classesNameList;
626
        QStringList classesID_list;
106 pingvin 627
 
108 pingvin 628
        int index;
629
        classesID_list = class_list_map.keys();
630
        classesNameList = class_list_map.values();
631
        index = classesID_list.indexOf(item->child(0)->text(1));
632
        ui->comboBox->setCurrentIndex(index);
633
 
634
        ui->comboBox->setEnabled(true);
635
 
636
 
637
    }
106 pingvin 638
  //  if ((rootItem1) == (item) ) ui->comboBox->setEnabled(true);
639
else ( ui->comboBox->setEnabled(false));
640
}
110 pingvin 641
 
642