Хранилища Subversion OpenInventory

Редакция

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