Хранилища Subversion OpenInventory

Редакция

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