Хранилища Subversion OpenInventory

Редакция

Редакция 125 | Редакция 131 | К новейшей редакции | Содержимое файла | Сравнить с предыдущей | Последнее изменение | Открыть журнал | 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();
125 pingvin 27
    MainWindow::buildPreviewModel(tr(" "), tr("7"));
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");
255
    sql.setDatabaseName(tr("an_db"));
256
    sql.setHostName(tr("localhost"));
257
    sql.setUserName(tr("an"));
258
    sql.setPassword(tr("393939"));
259
 
260
    bool ok;
261
    ok = sql.open();
262
 
263
    /*
264
    if (!ok) {
265
                                QMessageBox::critical( //     .
266
                                                                                this,                      //  .
267
                                                                                QObject::tr("Database Error"),   // .
268
                                                                                sql.lastError().text() );         //  .
269
 
270
 
271
                            }
272
 
273
*/
274
    if (!ok) {
275
                            QMessageBox::critical( //     .
276
                                                                            this,                      //  .
277
                                                                            QObject::tr("Database Error"),   // .
278
                                                                            sql.lastError().text());          //  .
279
                     }
280
else {
281
 
282
        QMessageBox::information( //     .
283
                                                        this,                      //  .
284
                                                        QObject::tr("Database Connect"),   // .
285
                                                        QObject::tr("     ."));         //  .
286
 
287
 
288
 
289
 
290
 
291
    }
292
 
293
     return ok;
294
}
94 pingvin 295
 
296
 
97 pingvin 297
bool MainWindow::buildPreviewModel(QString modelName, QString rootClassID){
94 pingvin 298
QSqlQuery q;
299
QString sql_str;
95 pingvin 300
QString field_name_str;
301
QString root_class_name;
302
bool ok;
94 pingvin 303
int field_name;
95 pingvin 304
 
305
//ui->treeWidget->clear();
306
 
307
root_class_name = MainWindow::ClassName(rootClassID); //   
308
 
309
rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
106 pingvin 310
root_items_list << rootItem1;
97 pingvin 311
 rootItem1->setText(0, modelName);
312
 
95 pingvin 313
 rootItem1->setCheckState(0,Qt::Checked);
104 pingvin 314
 rootItems.append(rootItem1);
95 pingvin 315
 
316
 
104 pingvin 317
 
97 pingvin 318
rootItem2 = new  QTreeWidgetItem(rootItem1);
319
rootItem2->setText(0, root_class_name);
320
rootItem2->setText(1, rootClassID);
321
rootItem2->setCheckState(0,Qt::Checked);
95 pingvin 322
 
98 pingvin 323
addChildsItems(rootItem2);
95 pingvin 324
 
94 pingvin 325
sql_str = tr("select * "
95 pingvin 326
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
94 pingvin 327
             );
328
sql_str.append(rootClassID);
329
 
95 pingvin 330
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
94 pingvin 331
 
95 pingvin 332
q.prepare(sql_str);
333
 
334
ok = q.exec();
335
if (!ok) {
336
                        QMessageBox::critical( //     .
337
                                                                        this,                      //  .
338
                                                                        QObject::tr("Database Error"),   // .
339
                                                                        q.lastError().text());          //  .
340
                 }
341
field_name = q.record().indexOf(tr("FieldName"));
342
while(q.next()){
343
 
344
                field_name_str = q.value(field_name).toString();
345
 
346
                }
347
 
100 pingvin 348
 
349
 
350
 
94 pingvin 351
}
95 pingvin 352
 
353
 
107 pingvin 354
bool MainWindow::rebuildPreviewModel(QTreeWidgetItem* root_item, QString rootClassID){
355
QSqlQuery q;
356
QString sql_str;
357
QString field_name_str;
358
QString root_class_name;
359
bool ok;
360
int field_name;
95 pingvin 361
 
107 pingvin 362
//ui->treeWidget->clear();
363
 
364
root_class_name = MainWindow::ClassName(rootClassID); //   
365
 
366
 
367
// rootItem1 = new  QTreeWidgetItem(ui->treeWidget);
368
// root_items_list << rootItem1;
369
// rootItem1->setText(0, modelName);
370
 
371
// rootItem1->setCheckState(0,Qt::Checked);
372
// rootItems.append(rootItem1);
373
 
374
// root_item->removeChild(root_item->child(0)); //    
375
if (root_item->childCount()==1){
376
root_item->child(0)->setText(0, root_class_name);
377
root_item->child(0)->setText(1, rootClassID);
378
root_item->child(0)->setCheckState(0,Qt::Checked);}
379
 
380
//delete rootItem2;
381
 
382
 
383
// rootItem2 = new  QTreeWidgetItem(rootItem1);
384
 
385
 
386
 
387
// rootItem2->setText(0, root_class_name);
388
// rootItem2->setText(1, rootClassID);
389
// rootItem2->setCheckState(0,Qt::Checked);
390
 
391
int child_count = root_item->child(0)->childCount();
392
 
393
 
394
 
395
for (int i=0; i < child_count; i++){
396
    root_item->child(0)->removeChild(root_item->child(0)->child(0));
397
}
398
 
399
child_count = root_item->child(0)->childCount();
400
addChildsItems(root_item->child(0));
401
/*
402
sql_str = tr("select * "
403
             " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
404
             );
405
sql_str.append(rootClassID);
406
 
407
sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
408
 
409
q.prepare(sql_str);
410
 
411
ok = q.exec();
412
if (!ok) {
413
                        QMessageBox::critical( //     .
414
                                                                        this,                      //  .
415
                                                                        QObject::tr("Database Error"),   // .
416
                                                                        q.lastError().text());          //  .
417
                 }
418
field_name = q.record().indexOf(tr("FieldName"));
419
while(q.next()){
420
 
421
                field_name_str = q.value(field_name).toString();
422
 
423
                }
424
 
425
*/
426
 
427
 
428
}
429
 
430
 
431
 
432
 
95 pingvin 433
QString MainWindow::ClassName(QString class_id){ //     ID
434
    QSqlQuery q;
435
    QString sql_str;
436
    QString class_name_str;
437
    bool ok;
438
    int field_class_name;
439
 
440
    sql_str = tr("select * "
441
                 " from ListOfClasses where ListOfClasses.ID = '"       // ,     
442
                 );
443
 
444
    sql_str.append(class_id);
445
    sql_str.append(tr("' "));
446
 
447
    q.prepare(sql_str);
448
 
449
    ok = q.exec();
450
 
451
 
452
    if (!ok) {
453
                            QMessageBox::critical( //     .
454
                                                                            this,                      //  .
455
                                                                            QObject::tr("Database Error"),   // .
456
                                                                            q.lastError().text());          //  .
457
                     }
458
 
459
    field_class_name = q.record().indexOf(tr("ClassName"));
460
    while(q.next()){
461
 
462
                    class_name_str = q.value(field_class_name).toString();
463
 
464
                    }
465
 
466
    return class_name_str;
467
 
468
}
98 pingvin 469
 
470
void MainWindow::addChildsItems(QTreeWidgetItem *perent_class_item){ //          ,      
100 pingvin 471
QStringList chields_list;
472
QString parent_ID;
473
QTreeWidgetItem *Item_tmp;
474
parent_ID = perent_class_item->text(1);
475
int chields_count, i;
98 pingvin 476
 
100 pingvin 477
chields_list = MainWindow::classChields(parent_ID);
478
if(chields_list.isEmpty()) return;
479
chields_count = chields_list.size();
480
for (int l = 0; l < chields_count; ++l){
481
    QString chield_class_name, chield_id;
482
    chield_id = chields_list.at(l);
483
    chield_class_name = MainWindow::ClassName(chield_id);
484
    Item_tmp = new QTreeWidgetItem(perent_class_item);
485
    Item_tmp->setText(0, chield_class_name);
486
    Item_tmp->setText(1, chield_id);
487
    Item_tmp->setCheckState(0, Qt::Checked);
101 pingvin 488
    MainWindow::addChildsItems(Item_tmp);
98 pingvin 489
}
490
 
100 pingvin 491
i++;
99 pingvin 492
 
100 pingvin 493
}
494
 
495
 
99 pingvin 496
/********************************************************
497
*    "" 
498
*
499
*
500
*
501
********************************************************/
502
 
503
QStringList MainWindow::classChields(QString class_id){
504
   // QMap<QString, QString> map;
505
   // TClass class_tmp;
506
   // QList <TClass> chields_class_list; //   
507
    QStringList result;
508
    QSqlQuery q;
509
    QString sql_str;
510
    QString classIdentifer_str; //      -
511
    QString field_id_str;
512
    bool ok;
513
    int field_classIdentifer;
514
    sql_str = tr("select * "
515
                 " from DescriptionOfClasses where  DescriptionOfClasses.DefaultValue = '"       // ,     
516
                 );
517
    sql_str.append(class_id);
518
 
519
    sql_str.append(tr("' and DescriptionOfClasses.FieldType = 'pointer' "));
520
 
521
    q.prepare(sql_str);
522
 
523
    ok = q.exec();
524
    if (!ok) {
525
                            QMessageBox::critical( //     .
526
                                                                            this,                      //  .
527
                                                                            QObject::tr("Database Error"),   // .
528
                                                                            q.lastError().text());          //  .
529
                     }
530
    field_classIdentifer = q.record().indexOf(tr("ClassIdentifer"));
531
      while(q.next()){
532
 
533
                    classIdentifer_str = q.value(field_classIdentifer).toString();
534
                    result.append(classIdentifer_str);
535
                    }
536
 
537
    return result;
538
 
539
  }
102 pingvin 540
 
541
 
542
QMap <QString, QString> MainWindow::getClassList(){ //   
543
QMap <QString, QString> result_map;
544
QSqlQuery q;
545
QString sql_str;
103 pingvin 546
QString class_name_str, class_id_str;
547
 
102 pingvin 548
int field_class_id, field_class_name;
549
bool ok;
550
sql_str = tr("select * "
551
             " from ListOfClasses "       // ,     
552
             );
553
 q.prepare(sql_str);
554
 ok = q.exec();
555
 if (!ok) {
556
                         QMessageBox::critical( //     .
557
                                                                         this,                      //  .
558
                                                                         QObject::tr("Database Error"),   // .
559
                                                                         q.lastError().text());          //  .
560
                         return result_map; //   -   
561
                  }
562
field_class_id = q.record().indexOf(tr("ID"));
563
field_class_name = q.record().indexOf(tr("ClassName"));
564
 
103 pingvin 565
while(q.next()){
102 pingvin 566
 
103 pingvin 567
                class_name_str = q.value(field_class_name).toString();
568
                class_id_str = q.value(field_class_id).toString();
569
                result_map[class_id_str] = class_name_str;
570
                }
102 pingvin 571
 return result_map;
572
}
104 pingvin 573
 
574
 
575
 
576
 
577
 
578
void MainWindow::initComboBox(){
579
 
106 pingvin 580
 
104 pingvin 581
    QStringList classesNameList;
582
    QStringList classesID_list;
583
    QStringList tmp_stringList;
584
 
585
 
586
 
587
    QString tmp_str;
588
 
589
    class_list_map = MainWindow::getClassList();
590
    classesID_list = class_list_map.keys();
591
    classesNameList = class_list_map.values();
592
    QMapIterator<QString, QString> interator(class_list_map);
593
//    ui->comboBox->addItems(classesID_list);
594
  //  ui->comboBox->addItems(classesNameList);
595
    while (interator.hasNext()) {
596
         interator.next();
597
         tmp_str =  interator.value();
598
         tmp_str.append(tr(" \t(ID="));
599
         tmp_str.append(interator.key());
600
         tmp_str.append(tr(")"));
601
         tmp_stringList << tmp_str;
602
 
603
     }
604
    ui->comboBox->addItems(tmp_stringList);
605
 
606
 
607
}
105 pingvin 608
 
609
 
106 pingvin 610
void MainWindow::on_comboBox_currentIndexChanged( int index ){
611
QStringList classesNameList;
612
QStringList classesID_list;
613
QString ID_str;
614
classesID_list = class_list_map.keys();
615
classesNameList = class_list_map.values();
616
ID_str =  classesID_list[index];
617
ui->label->setText(ID_str);
107 pingvin 618
if (root_items_list.indexOf(ui->treeWidget->currentItem()) != -1) rebuildPreviewModel(ui->treeWidget->currentItem(), ID_str);
118 pingvin 619
getDatabaseData();
107 pingvin 620
 
621
 
105 pingvin 622
}
623
 
106 pingvin 624
void MainWindow::on_treeWidget_itemChanged ( QTreeWidgetItem * item, int column ){
625
    int i;
626
    i++;
627
 
118 pingvin 628
    if ((column) == 0 && (model_is_build)) getDatabaseData();
629
 
106 pingvin 630
}
631
 
632
 
633
void MainWindow::on_treeWidget_itemClicked ( QTreeWidgetItem * item, int column ){
634
    int i;
635
    i++;
108 pingvin 636
    if (root_items_list.indexOf(item) != -1) {
637
        QStringList classesNameList;
638
        QStringList classesID_list;
106 pingvin 639
 
108 pingvin 640
        int index;
641
        classesID_list = class_list_map.keys();
642
        classesNameList = class_list_map.values();
643
        index = classesID_list.indexOf(item->child(0)->text(1));
644
        ui->comboBox->setCurrentIndex(index);
645
 
646
        ui->comboBox->setEnabled(true);
647
 
648
 
649
    }
106 pingvin 650
  //  if ((rootItem1) == (item) ) ui->comboBox->setEnabled(true);
651
else ( ui->comboBox->setEnabled(false));
652
}
110 pingvin 653
 
125 pingvin 654
QMap <QString, QString> MainWindow::getFieldsList(QString class_id){ //    
110 pingvin 655
 
125 pingvin 656
}