Хранилища Subversion OpenInventory

Редакция

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