Хранилища Subversion OpenInventory

Редакция

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